diff --git a/docs/source/developers/java/development.rst b/docs/source/developers/java/development.rst index 261cd5702ae..c993d024003 100644 --- a/docs/source/developers/java/development.rst +++ b/docs/source/developers/java/development.rst @@ -110,15 +110,67 @@ integration tests, you would do: Code Style ========== -Code style is enforced with Checkstyle. The configuration is located at `checkstyle`_. -You can also just check the style without building the project. -This checks the code style of all source code under the current directory or from within an individual module. +The current Java code styles are configured as follows: + +- Indent: Tabs & spaces (2 spaces per tab) +- Google Java Format: Reformats Java source code to comply with `Google Java Style`_. +- Configure license headers for Java & XML files + +Java code style is checked by `Spotless`_ during the build, and the continuous integration build will verify +that changes adhere to the style guide. + +.. code-block:: xml + + + + ... + + + + ... + + + + + ... + + + ... + + + +Automatically fixing code style issues +-------------------------------------- + +- You can also just check the style without building the project with `mvn spotless:check`. +- The Java code style can be corrected from the command line by using the following commands: `mvn spotless:apply`. + +.. code-block:: bash + + user@machine repo % mvn spotless:check + [ERROR] > The following files had format violations: + [ERROR] src\main\java\com\diffplug\gradle\spotless\FormatExtension.java + [ERROR] -\t\t····if·(targets.length·==·0)·{ + [ERROR] +\t\tif·(targets.length·==·0)·{ + [ERROR] Run 'mvn spotless:apply' to fix these violations. + user@machine repo % mvn spotless:apply + [INFO] BUILD SUCCESS + user@machine repo % mvn spotless:check + [INFO] BUILD SUCCESS + +Code Formatter for Intellij IDEA and Eclipse +-------------------------------------------- + +Follow the instructions for: + +- `Eclipse`_ +- `IntelliJ`_ -.. code-block:: - - $ mvn checkstyle:check .. _benchmark: https://github.com/ursacomputing/benchmarks .. _archery: https://github.com/apache/arrow/blob/main/dev/conbench_envs/README.md#L188 .. _conbench: https://github.com/conbench/conbench -.. _checkstyle: https://github.com/apache/arrow/blob/main/java/dev/checkstyle/checkstyle.xml +.. _Spotless: https://github.com/diffplug/spotless +.. _Google Java Style: https://google.github.io/styleguide/javaguide.html +.. _Eclipse: https://github.com/google/google-java-format?tab=readme-ov-file#eclipse +.. _IntelliJ: https://github.com/google/google-java-format?tab=readme-ov-file#intellij-android-studio-and-other-jetbrains-ides \ No newline at end of file diff --git a/java/.mvn/.DS_Store b/java/.mvn/.DS_Store new file mode 100644 index 00000000000..e102fa8ec70 Binary files /dev/null and b/java/.mvn/.DS_Store differ diff --git a/java/.mvn/wrapper/maven-wrapper.jar b/java/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000000..cb28b0e37c7 Binary files /dev/null and b/java/.mvn/wrapper/maven-wrapper.jar differ diff --git a/java/.mvn/wrapper/maven-wrapper.properties b/java/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000000..ac184013fc0 --- /dev/null +++ b/java/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.4/apache-maven-3.9.4-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar diff --git a/java/adapter/avro/pom.xml b/java/adapter/avro/pom.xml index 6644748b5e5..05d6d6b0eb4 100644 --- a/java/adapter/avro/pom.xml +++ b/java/adapter/avro/pom.xml @@ -1,15 +1,14 @@ - + license agreements. See the NOTICE file distributed with this work for additional + information regarding copyright ownership. The ASF licenses this file to + You under the Apache License, Version 2.0 (the "License"); you may not use + this file except in compliance with the License. You may obtain a copy of + the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required + by applicable law or agreed to in writing, software distributed under the + License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS + OF ANY KIND, either express or implied. See the License for the specific + language governing permissions and limitations under the License. --> 4.0.0 diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrow.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrow.java index 8baa60a72dd..3d4380614a0 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrow.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrow.java @@ -18,20 +18,17 @@ package org.apache.arrow.adapter.avro; import java.io.IOException; - import org.apache.arrow.util.Preconditions; import org.apache.arrow.vector.VectorSchemaRoot; import org.apache.avro.Schema; import org.apache.avro.io.Decoder; -/** - * Utility class to convert Avro objects to columnar Arrow format objects. - */ +/** Utility class to convert Avro objects to columnar Arrow format objects. */ public class AvroToArrow { /** - * Fetch the data from {@link Decoder} and convert it to Arrow objects. - * Only for testing purpose. + * Fetch the data from {@link Decoder} and convert it to Arrow objects. Only for testing purpose. + * * @param schema avro schema. * @param decoder avro decoder * @param config configuration of the conversion. @@ -48,15 +45,14 @@ static VectorSchemaRoot avroToArrow(Schema schema, Decoder decoder, AvroToArrowC /** * Fetch the data from {@link Decoder} and iteratively convert it to Arrow objects. + * * @param schema avro schema * @param decoder avro decoder * @param config configuration of the conversion. * @throws IOException on error */ public static AvroToArrowVectorIterator avroToArrowIterator( - Schema schema, - Decoder decoder, - AvroToArrowConfig config) throws IOException { + Schema schema, Decoder decoder, AvroToArrowConfig config) throws IOException { Preconditions.checkNotNull(schema, "Avro schema object cannot be null"); Preconditions.checkNotNull(decoder, "Avro decoder object cannot be null"); diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowConfig.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowConfig.java index f9210fb0125..c5419c7e263 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowConfig.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowConfig.java @@ -18,36 +18,33 @@ package org.apache.arrow.adapter.avro; import java.util.Set; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.util.Preconditions; import org.apache.arrow.vector.dictionary.DictionaryProvider; -/** - * This class configures the Avro-to-Arrow conversion process. - */ +/** This class configures the Avro-to-Arrow conversion process. */ public class AvroToArrowConfig { private final BufferAllocator allocator; + /** - * The maximum rowCount to read each time when partially convert data. - * Default value is 1024 and -1 means read all data into one vector. + * The maximum rowCount to read each time when partially convert data. Default value is 1024 and + * -1 means read all data into one vector. */ private final int targetBatchSize; /** - * The dictionary provider used for enum type. - * If avro schema has enum type, will create dictionary and update this provider. + * The dictionary provider used for enum type. If avro schema has enum type, will create + * dictionary and update this provider. */ private final DictionaryProvider.MapDictionaryProvider provider; - /** - * The field names which to skip when reading decoder values. - */ + /** The field names which to skip when reading decoder values. */ private final Set skipFieldNames; /** * Instantiate an instance. + * * @param allocator The memory allocator to construct the Arrow vectors with. * @param targetBatchSize The maximum rowCount to read each time when partially convert data. * @param provider The dictionary provider used for enum type, adapter will update this provider. @@ -59,8 +56,10 @@ public class AvroToArrowConfig { DictionaryProvider.MapDictionaryProvider provider, Set skipFieldNames) { - Preconditions.checkArgument(targetBatchSize == AvroToArrowVectorIterator.NO_LIMIT_BATCH_SIZE || - targetBatchSize > 0, "invalid targetBatchSize: %s", targetBatchSize); + Preconditions.checkArgument( + targetBatchSize == AvroToArrowVectorIterator.NO_LIMIT_BATCH_SIZE || targetBatchSize > 0, + "invalid targetBatchSize: %s", + targetBatchSize); this.allocator = allocator; this.targetBatchSize = targetBatchSize; diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowConfigBuilder.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowConfigBuilder.java index 41e486d0a1c..f1db4e58909 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowConfigBuilder.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowConfigBuilder.java @@ -19,13 +19,10 @@ import java.util.HashSet; import java.util.Set; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.dictionary.DictionaryProvider; -/** - * This class builds {@link AvroToArrowConfig}s. - */ +/** This class builds {@link AvroToArrowConfig}s. */ public class AvroToArrowConfigBuilder { private BufferAllocator allocator; @@ -36,9 +33,7 @@ public class AvroToArrowConfigBuilder { private Set skipFieldNames; - /** - * Default constructor for the {@link AvroToArrowConfigBuilder}. - */ + /** Default constructor for the {@link AvroToArrowConfigBuilder}. */ public AvroToArrowConfigBuilder(BufferAllocator allocator) { this.allocator = allocator; this.targetBatchSize = AvroToArrowVectorIterator.DEFAULT_BATCH_SIZE; @@ -61,14 +56,8 @@ public AvroToArrowConfigBuilder setSkipFieldNames(Set skipFieldNames) { return this; } - /** - * This builds the {@link AvroToArrowConfig} from the provided params. - */ + /** This builds the {@link AvroToArrowConfig} from the provided params. */ public AvroToArrowConfig build() { - return new AvroToArrowConfig( - allocator, - targetBatchSize, - provider, - skipFieldNames); + return new AvroToArrowConfig(allocator, targetBatchSize, provider, skipFieldNames); } } diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowUtils.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowUtils.java index 1f5ad9e7689..a8fe7f8a4e3 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowUtils.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowUtils.java @@ -30,7 +30,6 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; - import org.apache.arrow.adapter.avro.consumers.AvroArraysConsumer; import org.apache.arrow.adapter.avro.consumers.AvroBooleanConsumer; import org.apache.arrow.adapter.avro.consumers.AvroBytesConsumer; @@ -106,36 +105,37 @@ public class AvroToArrowUtils { /** * Creates a {@link Consumer} from the {@link Schema} * -

This method currently performs following type mapping for Avro data types to corresponding Arrow data types. + *

This method currently performs following type mapping for Avro data types to corresponding + * Arrow data types. * *

    - *
  • STRING --> ArrowType.Utf8
  • - *
  • INT --> ArrowType.Int(32, signed)
  • - *
  • LONG --> ArrowType.Int(64, signed)
  • - *
  • FLOAT --> ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)
  • - *
  • DOUBLE --> ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)
  • - *
  • BOOLEAN --> ArrowType.Bool
  • - *
  • BYTES --> ArrowType.Binary
  • - *
  • ARRAY --> ArrowType.List
  • - *
  • MAP --> ArrowType.Map
  • - *
  • FIXED --> ArrowType.FixedSizeBinary
  • - *
  • RECORD --> ArrowType.Struct
  • - *
  • UNION --> ArrowType.Union
  • - *
  • ENUM--> ArrowType.Int
  • - *
  • DECIMAL --> ArrowType.Decimal
  • - *
  • Date --> ArrowType.Date(DateUnit.DAY)
  • - *
  • TimeMillis --> ArrowType.Time(TimeUnit.MILLISECOND, 32)
  • - *
  • TimeMicros --> ArrowType.Time(TimeUnit.MICROSECOND, 64)
  • - *
  • TimestampMillis --> ArrowType.Timestamp(TimeUnit.MILLISECOND, null)
  • - *
  • TimestampMicros --> ArrowType.Timestamp(TimeUnit.MICROSECOND, null)
  • + *
  • STRING --> ArrowType.Utf8 + *
  • INT --> ArrowType.Int(32, signed) + *
  • LONG --> ArrowType.Int(64, signed) + *
  • FLOAT --> ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE) + *
  • DOUBLE --> ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE) + *
  • BOOLEAN --> ArrowType.Bool + *
  • BYTES --> ArrowType.Binary + *
  • ARRAY --> ArrowType.List + *
  • MAP --> ArrowType.Map + *
  • FIXED --> ArrowType.FixedSizeBinary + *
  • RECORD --> ArrowType.Struct + *
  • UNION --> ArrowType.Union + *
  • ENUM--> ArrowType.Int + *
  • DECIMAL --> ArrowType.Decimal + *
  • Date --> ArrowType.Date(DateUnit.DAY) + *
  • TimeMillis --> ArrowType.Time(TimeUnit.MILLISECOND, 32) + *
  • TimeMicros --> ArrowType.Time(TimeUnit.MICROSECOND, 64) + *
  • TimestampMillis --> ArrowType.Timestamp(TimeUnit.MILLISECOND, null) + *
  • TimestampMicros --> ArrowType.Timestamp(TimeUnit.MICROSECOND, null) *
*/ - private static Consumer createConsumer(Schema schema, String name, AvroToArrowConfig config) { return createConsumer(schema, name, false, config, null); } - private static Consumer createConsumer(Schema schema, String name, AvroToArrowConfig config, FieldVector vector) { + private static Consumer createConsumer( + Schema schema, String name, AvroToArrowConfig config, FieldVector vector) { return createConsumer(schema, name, false, config, vector); } @@ -144,7 +144,8 @@ private static Consumer createConsumer(Schema schema, String name, AvroToArrowCo * * @param schema avro schema * @param name arrow field name - * @param consumerVector vector to keep in consumer, if v == null, will create a new vector via field. + * @param consumerVector vector to keep in consumer, if v == null, will create a new vector via + * field. * @return consumer */ private static Consumer createConsumer( @@ -185,7 +186,7 @@ private static Consumer createConsumer( break; case STRING: arrowType = new ArrowType.Utf8(); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema)); + fieldType = new FieldType(nullable, arrowType, /* dictionary= */ null, getMetaData(schema)); vector = createVector(consumerVector, fieldType, name, allocator); consumer = new AvroStringConsumer((VarCharVector) vector); break; @@ -193,12 +194,18 @@ private static Consumer createConsumer( Map extProps = createExternalProps(schema); if (logicalType instanceof LogicalTypes.Decimal) { arrowType = createDecimalArrowType((LogicalTypes.Decimal) logicalType); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema, extProps)); + fieldType = + new FieldType( + nullable, arrowType, /* dictionary= */ null, getMetaData(schema, extProps)); vector = createVector(consumerVector, fieldType, name, allocator); - consumer = new AvroDecimalConsumer.FixedDecimalConsumer((DecimalVector) vector, schema.getFixedSize()); + consumer = + new AvroDecimalConsumer.FixedDecimalConsumer( + (DecimalVector) vector, schema.getFixedSize()); } else { arrowType = new ArrowType.FixedSizeBinary(schema.getFixedSize()); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema, extProps)); + fieldType = + new FieldType( + nullable, arrowType, /* dictionary= */ null, getMetaData(schema, extProps)); vector = createVector(consumerVector, fieldType, name, allocator); consumer = new AvroFixedConsumer((FixedSizeBinaryVector) vector, schema.getFixedSize()); } @@ -206,84 +213,94 @@ private static Consumer createConsumer( case INT: if (logicalType instanceof LogicalTypes.Date) { arrowType = new ArrowType.Date(DateUnit.DAY); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema)); + fieldType = + new FieldType(nullable, arrowType, /* dictionary= */ null, getMetaData(schema)); vector = createVector(consumerVector, fieldType, name, allocator); consumer = new AvroDateConsumer((DateDayVector) vector); } else if (logicalType instanceof LogicalTypes.TimeMillis) { arrowType = new ArrowType.Time(TimeUnit.MILLISECOND, 32); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema)); + fieldType = + new FieldType(nullable, arrowType, /* dictionary= */ null, getMetaData(schema)); vector = createVector(consumerVector, fieldType, name, allocator); consumer = new AvroTimeMillisConsumer((TimeMilliVector) vector); } else { - arrowType = new ArrowType.Int(32, /*signed=*/true); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema)); + arrowType = new ArrowType.Int(32, /* signed= */ true); + fieldType = + new FieldType(nullable, arrowType, /* dictionary= */ null, getMetaData(schema)); vector = createVector(consumerVector, fieldType, name, allocator); consumer = new AvroIntConsumer((IntVector) vector); } break; case BOOLEAN: arrowType = new ArrowType.Bool(); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema)); + fieldType = new FieldType(nullable, arrowType, /* dictionary= */ null, getMetaData(schema)); vector = createVector(consumerVector, fieldType, name, allocator); consumer = new AvroBooleanConsumer((BitVector) vector); break; case LONG: if (logicalType instanceof LogicalTypes.TimeMicros) { arrowType = new ArrowType.Time(TimeUnit.MICROSECOND, 64); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema)); + fieldType = + new FieldType(nullable, arrowType, /* dictionary= */ null, getMetaData(schema)); vector = createVector(consumerVector, fieldType, name, allocator); consumer = new AvroTimeMicroConsumer((TimeMicroVector) vector); } else if (logicalType instanceof LogicalTypes.TimestampMillis) { arrowType = new ArrowType.Timestamp(TimeUnit.MILLISECOND, null); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema)); + fieldType = + new FieldType(nullable, arrowType, /* dictionary= */ null, getMetaData(schema)); vector = createVector(consumerVector, fieldType, name, allocator); consumer = new AvroTimestampMillisConsumer((TimeStampMilliVector) vector); } else if (logicalType instanceof LogicalTypes.TimestampMicros) { arrowType = new ArrowType.Timestamp(TimeUnit.MICROSECOND, null); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema)); + fieldType = + new FieldType(nullable, arrowType, /* dictionary= */ null, getMetaData(schema)); vector = createVector(consumerVector, fieldType, name, allocator); consumer = new AvroTimestampMicrosConsumer((TimeStampMicroVector) vector); } else { - arrowType = new ArrowType.Int(64, /*signed=*/true); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema)); + arrowType = new ArrowType.Int(64, /* signed= */ true); + fieldType = + new FieldType(nullable, arrowType, /* dictionary= */ null, getMetaData(schema)); vector = createVector(consumerVector, fieldType, name, allocator); consumer = new AvroLongConsumer((BigIntVector) vector); } break; case FLOAT: arrowType = new ArrowType.FloatingPoint(SINGLE); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema)); + fieldType = new FieldType(nullable, arrowType, /* dictionary= */ null, getMetaData(schema)); vector = createVector(consumerVector, fieldType, name, allocator); consumer = new AvroFloatConsumer((Float4Vector) vector); break; case DOUBLE: arrowType = new ArrowType.FloatingPoint(DOUBLE); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema)); + fieldType = new FieldType(nullable, arrowType, /* dictionary= */ null, getMetaData(schema)); vector = createVector(consumerVector, fieldType, name, allocator); consumer = new AvroDoubleConsumer((Float8Vector) vector); break; case BYTES: if (logicalType instanceof LogicalTypes.Decimal) { arrowType = createDecimalArrowType((LogicalTypes.Decimal) logicalType); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema)); + fieldType = + new FieldType(nullable, arrowType, /* dictionary= */ null, getMetaData(schema)); vector = createVector(consumerVector, fieldType, name, allocator); consumer = new AvroDecimalConsumer.BytesDecimalConsumer((DecimalVector) vector); } else { arrowType = new ArrowType.Binary(); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema)); + fieldType = + new FieldType(nullable, arrowType, /* dictionary= */ null, getMetaData(schema)); vector = createVector(consumerVector, fieldType, name, allocator); consumer = new AvroBytesConsumer((VarBinaryVector) vector); } break; case NULL: arrowType = new ArrowType.Null(); - fieldType = new FieldType(nullable, arrowType, /*dictionary=*/null, getMetaData(schema)); - vector = fieldType.createNewSingleVector(name, allocator, /*schemaCallback=*/null); + fieldType = new FieldType(nullable, arrowType, /* dictionary= */ null, getMetaData(schema)); + vector = fieldType.createNewSingleVector(name, allocator, /* schemaCallback= */ null); consumer = new AvroNullConsumer((NullVector) vector); break; default: // no-op, shouldn't get here - throw new UnsupportedOperationException("Can't convert avro type %s to arrow type." + type.getName()); + throw new UnsupportedOperationException( + "Can't convert avro type %s to arrow type." + type.getName()); } return consumer; } @@ -291,15 +308,16 @@ private static Consumer createConsumer( private static ArrowType createDecimalArrowType(LogicalTypes.Decimal logicalType) { final int scale = logicalType.getScale(); final int precision = logicalType.getPrecision(); - Preconditions.checkArgument(precision > 0 && precision <= 38, - "Precision must be in range of 1 to 38"); - Preconditions.checkArgument(scale >= 0 && scale <= 38, - "Scale must be in range of 0 to 38."); - Preconditions.checkArgument(scale <= precision, - "Invalid decimal scale: %s (greater than precision: %s)", scale, precision); + Preconditions.checkArgument( + precision > 0 && precision <= 38, "Precision must be in range of 1 to 38"); + Preconditions.checkArgument(scale >= 0 && scale <= 38, "Scale must be in range of 0 to 38."); + Preconditions.checkArgument( + scale <= precision, + "Invalid decimal scale: %s (greater than precision: %s)", + scale, + precision); return new ArrowType.Decimal(precision, scale, 128); - } private static Consumer createSkipConsumer(Schema schema) { @@ -309,41 +327,46 @@ private static Consumer createSkipConsumer(Schema schema) { switch (type) { case UNION: - List unionDelegates = schema.getTypes().stream().map(s -> - createSkipConsumer(s)).collect(Collectors.toList()); + List unionDelegates = + schema.getTypes().stream().map(s -> createSkipConsumer(s)).collect(Collectors.toList()); skipFunction = decoder -> unionDelegates.get(decoder.readInt()).consume(decoder); break; case ARRAY: Consumer elementDelegate = createSkipConsumer(schema.getElementType()); - skipFunction = decoder -> { - for (long i = decoder.skipArray(); i != 0; i = decoder.skipArray()) { - for (long j = 0; j < i; j++) { - elementDelegate.consume(decoder); - } - } - }; + skipFunction = + decoder -> { + for (long i = decoder.skipArray(); i != 0; i = decoder.skipArray()) { + for (long j = 0; j < i; j++) { + elementDelegate.consume(decoder); + } + } + }; break; case MAP: Consumer valueDelegate = createSkipConsumer(schema.getValueType()); - skipFunction = decoder -> { - for (long i = decoder.skipMap(); i != 0; i = decoder.skipMap()) { - for (long j = 0; j < i; j++) { - decoder.skipString(); // Discard key - valueDelegate.consume(decoder); - } - } - }; + skipFunction = + decoder -> { + for (long i = decoder.skipMap(); i != 0; i = decoder.skipMap()) { + for (long j = 0; j < i; j++) { + decoder.skipString(); // Discard key + valueDelegate.consume(decoder); + } + } + }; break; case RECORD: - List delegates = schema.getFields().stream().map(field -> - createSkipConsumer(field.schema())).collect(Collectors.toList()); + List delegates = + schema.getFields().stream() + .map(field -> createSkipConsumer(field.schema())) + .collect(Collectors.toList()); - skipFunction = decoder -> { - for (Consumer consumer : delegates) { - consumer.consume(decoder); - } - }; + skipFunction = + decoder -> { + for (Consumer consumer : delegates) { + consumer.consume(decoder); + } + }; break; case ENUM: @@ -374,7 +397,7 @@ private static Consumer createSkipConsumer(Schema schema) { skipFunction = decoder -> decoder.skipBytes(); break; case NULL: - skipFunction = decoder -> { }; + skipFunction = decoder -> {}; break; default: // no-op, shouldn't get here @@ -384,8 +407,7 @@ private static Consumer createSkipConsumer(Schema schema) { return new SkipConsumer(skipFunction); } - static CompositeAvroConsumer createCompositeConsumer( - Schema schema, AvroToArrowConfig config) { + static CompositeAvroConsumer createCompositeConsumer(Schema schema, AvroToArrowConfig config) { List consumers = new ArrayList<>(); final Set skipFieldNames = config.getSkipFieldNames(); @@ -399,7 +421,6 @@ static CompositeAvroConsumer createCompositeConsumer( Consumer consumer = createConsumer(field.schema(), field.name(), config); consumers.add(consumer); } - } } else { Consumer consumer = createConsumer(schema, "", config); @@ -409,9 +430,11 @@ static CompositeAvroConsumer createCompositeConsumer( return new CompositeAvroConsumer(consumers); } - private static FieldVector createVector(FieldVector consumerVector, FieldType fieldType, - String name, BufferAllocator allocator) { - return consumerVector != null ? consumerVector : fieldType.createNewSingleVector(name, allocator, null); + private static FieldVector createVector( + FieldVector consumerVector, FieldType fieldType, String name, BufferAllocator allocator) { + return consumerVector != null + ? consumerVector + : fieldType.createNewSingleVector(name, allocator, null); } private static String getDefaultFieldName(ArrowType type) { @@ -424,10 +447,7 @@ private static Field avroSchemaToField(Schema schema, String name, AvroToArrowCo } private static Field avroSchemaToField( - Schema schema, - String name, - AvroToArrowConfig config, - Map externalProps) { + Schema schema, String name, AvroToArrowConfig config, Map externalProps) { final Type type = schema.getType(); final LogicalType logicalType = schema.getLogicalType(); @@ -441,7 +461,8 @@ private static Field avroSchemaToField( // Union child vector should use default name children.add(avroSchemaToField(childSchema, null, config)); } - fieldType = createFieldType(new ArrowType.Union(UnionMode.Sparse, null), schema, externalProps); + fieldType = + createFieldType(new ArrowType.Union(UnionMode.Sparse, null), schema, externalProps); break; case ARRAY: Schema elementSchema = schema.getElementType(); @@ -450,14 +471,18 @@ private static Field avroSchemaToField( break; case MAP: // MapVector internal struct field and key field should be non-nullable - FieldType keyFieldType = new FieldType(/*nullable=*/false, new ArrowType.Utf8(), /*dictionary=*/null); - Field keyField = new Field("key", keyFieldType, /*children=*/null); + FieldType keyFieldType = + new FieldType(/* nullable= */ false, new ArrowType.Utf8(), /* dictionary= */ null); + Field keyField = new Field("key", keyFieldType, /* children= */ null); Field valueField = avroSchemaToField(schema.getValueType(), "value", config); - FieldType structFieldType = new FieldType(false, new ArrowType.Struct(), /*dictionary=*/null); - Field structField = new Field("internal", structFieldType, Arrays.asList(keyField, valueField)); + FieldType structFieldType = + new FieldType(false, new ArrowType.Struct(), /* dictionary= */ null); + Field structField = + new Field("internal", structFieldType, Arrays.asList(keyField, valueField)); children.add(structField); - fieldType = createFieldType(new ArrowType.Map(/*keySorted=*/false), schema, externalProps); + fieldType = + createFieldType(new ArrowType.Map(/* keySorted= */ false), schema, externalProps); break; case RECORD: final Set skipFieldNames = config.getSkipFieldNames(); @@ -486,8 +511,12 @@ private static Field avroSchemaToField( int enumCount = schema.getEnumSymbols().size(); ArrowType.Int indexType = DictionaryEncoder.getIndexType(enumCount); - fieldType = createFieldType(indexType, schema, externalProps, - new DictionaryEncoding(current, /*ordered=*/false, /*indexType=*/indexType)); + fieldType = + createFieldType( + indexType, + schema, + externalProps, + new DictionaryEncoding(current, /* ordered= */ false, /* indexType= */ indexType)); break; case STRING: @@ -509,7 +538,7 @@ private static Field avroSchemaToField( } else if (logicalType instanceof LogicalTypes.TimeMillis) { intArrowType = new ArrowType.Time(TimeUnit.MILLISECOND, 32); } else { - intArrowType = new ArrowType.Int(32, /*signed=*/true); + intArrowType = new ArrowType.Int(32, /* signed= */ true); } fieldType = createFieldType(intArrowType, schema, externalProps); break; @@ -525,7 +554,7 @@ private static Field avroSchemaToField( } else if (logicalType instanceof LogicalTypes.TimestampMicros) { longArrowType = new ArrowType.Timestamp(TimeUnit.MICROSECOND, null); } else { - longArrowType = new ArrowType.Int(64, /*signed=*/true); + longArrowType = new ArrowType.Int(64, /* signed= */ true); } fieldType = createFieldType(longArrowType, schema, externalProps); break; @@ -558,8 +587,8 @@ private static Field avroSchemaToField( return new Field(name, fieldType, children.size() == 0 ? null : children); } - private static Consumer createArrayConsumer(Schema schema, String name, AvroToArrowConfig config, - FieldVector consumerVector) { + private static Consumer createArrayConsumer( + Schema schema, String name, AvroToArrowConfig config, FieldVector consumerVector) { ListVector listVector; if (consumerVector == null) { @@ -578,8 +607,8 @@ private static Consumer createArrayConsumer(Schema schema, String name, AvroToAr return new AvroArraysConsumer(listVector, delegate); } - private static Consumer createStructConsumer(Schema schema, String name, AvroToArrowConfig config, - FieldVector consumerVector) { + private static Consumer createStructConsumer( + Schema schema, String name, AvroToArrowConfig config, FieldVector consumerVector) { final Set skipFieldNames = config.getSkipFieldNames(); @@ -601,19 +630,22 @@ private static Consumer createStructConsumer(Schema schema, String name, AvroToA if (skipFieldNames.contains(fullChildName)) { delegate = createSkipConsumer(childField.schema()); } else { - delegate = createConsumer(childField.schema(), fullChildName, config, - structVector.getChildrenFromFields().get(vectorIndex++)); + delegate = + createConsumer( + childField.schema(), + fullChildName, + config, + structVector.getChildrenFromFields().get(vectorIndex++)); } delegates[i] = delegate; } return new AvroStructConsumer(structVector, delegates); - } - private static Consumer createEnumConsumer(Schema schema, String name, AvroToArrowConfig config, - FieldVector consumerVector) { + private static Consumer createEnumConsumer( + Schema schema, String name, AvroToArrowConfig config, FieldVector consumerVector) { BaseIntVector indexVector; if (consumerVector == null) { @@ -630,16 +662,14 @@ private static Consumer createEnumConsumer(Schema schema, String name, AvroToArr for (int i = 0; i < valueCount; i++) { dictVector.set(i, schema.getEnumSymbols().get(i).getBytes(StandardCharsets.UTF_8)); } - Dictionary dictionary = - new Dictionary(dictVector, indexVector.getField().getDictionary()); + Dictionary dictionary = new Dictionary(dictVector, indexVector.getField().getDictionary()); config.getProvider().put(dictionary); return new AvroEnumConsumer(indexVector); - } - private static Consumer createMapConsumer(Schema schema, String name, AvroToArrowConfig config, - FieldVector consumerVector) { + private static Consumer createMapConsumer( + Schema schema, String name, AvroToArrowConfig config, FieldVector consumerVector) { MapVector mapVector; if (consumerVector == null) { @@ -653,10 +683,14 @@ private static Consumer createMapConsumer(Schema schema, String name, AvroToArro StructVector structVector = (StructVector) mapVector.getDataVector(); // keys in avro map are always assumed to be strings. - Consumer keyConsumer = new AvroStringConsumer( - (VarCharVector) structVector.getChildrenFromFields().get(0)); - Consumer valueConsumer = createConsumer(schema.getValueType(), schema.getValueType().getName(), - config, structVector.getChildrenFromFields().get(1)); + Consumer keyConsumer = + new AvroStringConsumer((VarCharVector) structVector.getChildrenFromFields().get(0)); + Consumer valueConsumer = + createConsumer( + schema.getValueType(), + schema.getValueType().getName(), + config, + structVector.getChildrenFromFields().get(1)); AvroStructConsumer internalConsumer = new AvroStructConsumer(structVector, new Consumer[] {keyConsumer, valueConsumer}); @@ -664,8 +698,8 @@ private static Consumer createMapConsumer(Schema schema, String name, AvroToArro return new AvroMapConsumer(mapVector, internalConsumer); } - private static Consumer createUnionConsumer(Schema schema, String name, AvroToArrowConfig config, - FieldVector consumerVector) { + private static Consumer createUnionConsumer( + Schema schema, String name, AvroToArrowConfig config, FieldVector consumerVector) { final int size = schema.getTypes().size(); final boolean nullable = schema.getTypes().stream().anyMatch(t -> t.getType() == Type.NULL); @@ -695,14 +729,12 @@ private static Consumer createUnionConsumer(Schema schema, String name, AvroToAr /** * Read data from {@link Decoder} and generate a {@link VectorSchemaRoot}. + * * @param schema avro schema * @param decoder avro decoder to read data from */ static VectorSchemaRoot avroToArrowVectors( - Schema schema, - Decoder decoder, - AvroToArrowConfig config) - throws IOException { + Schema schema, Decoder decoder, AvroToArrowConfig config) throws IOException { List vectors = new ArrayList<>(); List consumers = new ArrayList<>(); @@ -726,8 +758,8 @@ static VectorSchemaRoot avroToArrowVectors( } long validConsumerCount = consumers.stream().filter(c -> !c.skippable()).count(); - Preconditions.checkArgument(vectors.size() == validConsumerCount, - "vectors size not equals consumers size."); + Preconditions.checkArgument( + vectors.size() == validConsumerCount, "vectors size not equals consumers size."); List fields = vectors.stream().map(t -> t.getField()).collect(Collectors.toList()); @@ -767,9 +799,7 @@ private static Map getMetaData(Schema schema, Map createExternalProps(Schema schema) { final Map extProps = new HashMap<>(); String doc = schema.getDoc(); @@ -783,8 +813,9 @@ private static Map createExternalProps(Schema schema) { return extProps; } - private static FieldType createFieldType(ArrowType arrowType, Schema schema, Map externalProps) { - return createFieldType(arrowType, schema, externalProps, /*dictionary=*/null); + private static FieldType createFieldType( + ArrowType arrowType, Schema schema, Map externalProps) { + return createFieldType(arrowType, schema, externalProps, /* dictionary= */ null); } private static FieldType createFieldType( @@ -793,8 +824,8 @@ private static FieldType createFieldType( Map externalProps, DictionaryEncoding dictionary) { - return new FieldType(/*nullable=*/false, arrowType, dictionary, - getMetaData(schema, externalProps)); + return new FieldType( + /* nullable= */ false, arrowType, dictionary, getMetaData(schema, externalProps)); } private static String convertAliases(Set aliases) { diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowVectorIterator.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowVectorIterator.java index 4a439ade811..bdf554d7371 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowVectorIterator.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowVectorIterator.java @@ -22,7 +22,6 @@ import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; - import org.apache.arrow.adapter.avro.consumers.CompositeAvroConsumer; import org.apache.arrow.util.Preconditions; import org.apache.arrow.vector.FieldVector; @@ -32,9 +31,7 @@ import org.apache.avro.Schema; import org.apache.avro.io.Decoder; -/** - * VectorSchemaRoot iterator for partially converting avro data. - */ +/** VectorSchemaRoot iterator for partially converting avro data. */ public class AvroToArrowVectorIterator implements Iterator, AutoCloseable { public static final int NO_LIMIT_BATCH_SIZE = -1; @@ -53,28 +50,18 @@ public class AvroToArrowVectorIterator implements Iterator, Au private final int targetBatchSize; - /** - * Construct an instance. - */ - private AvroToArrowVectorIterator( - Decoder decoder, - Schema schema, - AvroToArrowConfig config) { + /** Construct an instance. */ + private AvroToArrowVectorIterator(Decoder decoder, Schema schema, AvroToArrowConfig config) { this.decoder = decoder; this.schema = schema; this.config = config; this.targetBatchSize = config.getTargetBatchSize(); - } - /** - * Create a ArrowVectorIterator to partially convert data. - */ + /** Create a ArrowVectorIterator to partially convert data. */ public static AvroToArrowVectorIterator create( - Decoder decoder, - Schema schema, - AvroToArrowConfig config) { + Decoder decoder, Schema schema, AvroToArrowConfig config) { AvroToArrowVectorIterator iterator = new AvroToArrowVectorIterator(decoder, schema, config); try { @@ -136,9 +123,10 @@ private void load(VectorSchemaRoot root) { ValueVectorUtility.preAllocate(root, targetBatchSize); } - long validConsumerCount = compositeConsumer.getConsumers().stream().filter(c -> - !c.skippable()).count(); - Preconditions.checkArgument(root.getFieldVectors().size() == validConsumerCount, + long validConsumerCount = + compositeConsumer.getConsumers().stream().filter(c -> !c.skippable()).count(); + Preconditions.checkArgument( + root.getFieldVectors().size() == validConsumerCount, "Schema root vectors size not equals to consumers size."); compositeConsumer.resetConsumerVectors(root); @@ -159,9 +147,7 @@ public boolean hasNext() { return nextBatch != null; } - /** - * Gets the next vector. The user is responsible for freeing its resources. - */ + /** Gets the next vector. The user is responsible for freeing its resources. */ public VectorSchemaRoot next() { Preconditions.checkArgument(hasNext()); VectorSchemaRoot returned = nextBatch; @@ -174,9 +160,7 @@ public VectorSchemaRoot next() { return returned; } - /** - * Clean up resources. - */ + /** Clean up resources. */ public void close() { if (nextBatch != null) { nextBatch.close(); diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroArraysConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroArraysConsumer.java index fd25986c32b..ae84a4d9af5 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroArraysConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroArraysConsumer.java @@ -18,21 +18,17 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.arrow.vector.complex.ListVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume array type values from avro decoder. - * Write the data to {@link ListVector}. + * Consumer which consume array type values from avro decoder. Write the data to {@link ListVector}. */ public class AvroArraysConsumer extends BaseAvroConsumer { private final Consumer delegate; - /** - * Instantiate a ArrayConsumer. - */ + /** Instantiate a ArrayConsumer. */ public AvroArraysConsumer(ListVector vector, Consumer delegate) { super(vector); this.delegate = delegate; diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroBooleanConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroBooleanConsumer.java index bf41828d19f..88c07173328 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroBooleanConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroBooleanConsumer.java @@ -18,19 +18,16 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.arrow.vector.BitVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume boolean type values from avro decoder. - * Write the data to {@link BitVector}. + * Consumer which consume boolean type values from avro decoder. Write the data to {@link + * BitVector}. */ public class AvroBooleanConsumer extends BaseAvroConsumer { - /** - * Instantiate a AvroBooleanConsumer. - */ + /** Instantiate a AvroBooleanConsumer. */ public AvroBooleanConsumer(BitVector vector) { super(vector); } diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroBytesConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroBytesConsumer.java index c8370e48060..aa063c31c67 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroBytesConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroBytesConsumer.java @@ -19,21 +19,18 @@ import java.io.IOException; import java.nio.ByteBuffer; - import org.apache.arrow.vector.VarBinaryVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume bytes type values from avro decoder. - * Write the data to {@link VarBinaryVector}. + * Consumer which consume bytes type values from avro decoder. Write the data to {@link + * VarBinaryVector}. */ public class AvroBytesConsumer extends BaseAvroConsumer { private ByteBuffer cacheBuffer; - /** - * Instantiate a AvroBytesConsumer. - */ + /** Instantiate a AvroBytesConsumer. */ public AvroBytesConsumer(VarBinaryVector vector) { super(vector); } diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroDoubleConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroDoubleConsumer.java index 7cc7dd33b15..c387896ffc0 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroDoubleConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroDoubleConsumer.java @@ -18,19 +18,16 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.arrow.vector.Float8Vector; import org.apache.avro.io.Decoder; /** - * Consumer which consume double type values from avro decoder. - * Write the data to {@link Float8Vector}. + * Consumer which consume double type values from avro decoder. Write the data to {@link + * Float8Vector}. */ public class AvroDoubleConsumer extends BaseAvroConsumer { - /** - * Instantiate a AvroDoubleConsumer. - */ + /** Instantiate a AvroDoubleConsumer. */ public AvroDoubleConsumer(Float8Vector vector) { super(vector); } diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroEnumConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroEnumConsumer.java index 32a2c85f6fc..6d06f3fbce2 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroEnumConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroEnumConsumer.java @@ -18,20 +18,16 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.arrow.vector.BaseIntVector; import org.apache.arrow.vector.IntVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume enum type values from avro decoder. - * Write the data to {@link IntVector}. + * Consumer which consume enum type values from avro decoder. Write the data to {@link IntVector}. */ public class AvroEnumConsumer extends BaseAvroConsumer { - /** - * Instantiate a AvroEnumConsumer. - */ + /** Instantiate a AvroEnumConsumer. */ public AvroEnumConsumer(BaseIntVector vector) { super(vector); } diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroFixedConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroFixedConsumer.java index 16b70898fd3..151fd3e653c 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroFixedConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroFixedConsumer.java @@ -18,21 +18,18 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.arrow.vector.FixedSizeBinaryVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume fixed type values from avro decoder. - * Write the data to {@link org.apache.arrow.vector.FixedSizeBinaryVector}. + * Consumer which consume fixed type values from avro decoder. Write the data to {@link + * org.apache.arrow.vector.FixedSizeBinaryVector}. */ public class AvroFixedConsumer extends BaseAvroConsumer { private final byte[] reuseBytes; - /** - * Instantiate a AvroFixedConsumer. - */ + /** Instantiate a AvroFixedConsumer. */ public AvroFixedConsumer(FixedSizeBinaryVector vector, int size) { super(vector); reuseBytes = new byte[size]; diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroFloatConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroFloatConsumer.java index b09d2881875..ec37fe0d1f6 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroFloatConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroFloatConsumer.java @@ -18,19 +18,16 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.arrow.vector.Float4Vector; import org.apache.avro.io.Decoder; /** - * Consumer which consume float type values from avro decoder. - * Write the data to {@link Float4Vector}. + * Consumer which consume float type values from avro decoder. Write the data to {@link + * Float4Vector}. */ public class AvroFloatConsumer extends BaseAvroConsumer { - /** - * Instantiate a AvroFloatConsumer. - */ + /** Instantiate a AvroFloatConsumer. */ public AvroFloatConsumer(Float4Vector vector) { super(vector); } diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroIntConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroIntConsumer.java index ae5a2719c56..68c93cf8e9e 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroIntConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroIntConsumer.java @@ -18,19 +18,15 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.arrow.vector.IntVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume int type values from avro decoder. - * Write the data to {@link IntVector}. + * Consumer which consume int type values from avro decoder. Write the data to {@link IntVector}. */ public class AvroIntConsumer extends BaseAvroConsumer { - /** - * Instantiate a AvroIntConsumer. - */ + /** Instantiate a AvroIntConsumer. */ public AvroIntConsumer(IntVector vector) { super(vector); } diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroLongConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroLongConsumer.java index 4db836acc45..525a6303d9e 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroLongConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroLongConsumer.java @@ -18,19 +18,16 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.arrow.vector.BigIntVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume long type values from avro decoder. - * Write the data to {@link BigIntVector}. + * Consumer which consume long type values from avro decoder. Write the data to {@link + * BigIntVector}. */ public class AvroLongConsumer extends BaseAvroConsumer { - /** - * Instantiate a AvroLongConsumer. - */ + /** Instantiate a AvroLongConsumer. */ public AvroLongConsumer(BigIntVector vector) { super(vector); } diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroMapConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroMapConsumer.java index 1ea97e63b61..87e8a0666df 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroMapConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroMapConsumer.java @@ -18,23 +18,19 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.complex.MapVector; import org.apache.arrow.vector.complex.StructVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume map type values from avro decoder. - * Write the data to {@link MapVector}. + * Consumer which consume map type values from avro decoder. Write the data to {@link MapVector}. */ public class AvroMapConsumer extends BaseAvroConsumer { private final Consumer delegate; - /** - * Instantiate a AvroMapConsumer. - */ + /** Instantiate a AvroMapConsumer. */ public AvroMapConsumer(MapVector vector, Consumer delegate) { super(vector); this.delegate = delegate; diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroNullConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroNullConsumer.java index 4c7bb8c03ba..5e43264a8b9 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroNullConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroNullConsumer.java @@ -18,13 +18,12 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.arrow.vector.NullVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume null type values from avro decoder. - * Corresponding to {@link org.apache.arrow.vector.NullVector}. + * Consumer which consume null type values from avro decoder. Corresponding to {@link + * org.apache.arrow.vector.NullVector}. */ public class AvroNullConsumer extends BaseAvroConsumer { diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroStringConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroStringConsumer.java index 072270aa6c0..fe33af4d422 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroStringConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroStringConsumer.java @@ -19,21 +19,18 @@ import java.io.IOException; import java.nio.ByteBuffer; - import org.apache.arrow.vector.VarCharVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume string type values from avro decoder. - * Write the data to {@link VarCharVector}. + * Consumer which consume string type values from avro decoder. Write the data to {@link + * VarCharVector}. */ public class AvroStringConsumer extends BaseAvroConsumer { private ByteBuffer cacheBuffer; - /** - * Instantiate a AvroStringConsumer. - */ + /** Instantiate a AvroStringConsumer. */ public AvroStringConsumer(VarCharVector vector) { super(vector); } diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroStructConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroStructConsumer.java index a02b1577f9f..b5c143e0c9a 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroStructConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroStructConsumer.java @@ -18,23 +18,20 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.arrow.util.AutoCloseables; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.complex.StructVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume nested record type values from avro decoder. - * Write the data to {@link org.apache.arrow.vector.complex.StructVector}. + * Consumer which consume nested record type values from avro decoder. Write the data to {@link + * org.apache.arrow.vector.complex.StructVector}. */ public class AvroStructConsumer extends BaseAvroConsumer { private final Consumer[] delegates; - /** - * Instantiate a AvroStructConsumer. - */ + /** Instantiate a AvroStructConsumer. */ public AvroStructConsumer(StructVector vector, Consumer[] delegates) { super(vector); this.delegates = delegates; @@ -49,7 +46,6 @@ public void consume(Decoder decoder) throws IOException { } vector.setIndexDefined(currentIndex); currentIndex++; - } @Override diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroUnionsConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroUnionsConsumer.java index 76287543b06..b25b0ef91b8 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroUnionsConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroUnionsConsumer.java @@ -18,7 +18,6 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.arrow.util.AutoCloseables; import org.apache.arrow.vector.ValueVector; import org.apache.arrow.vector.complex.UnionVector; @@ -26,17 +25,15 @@ import org.apache.avro.io.Decoder; /** - * Consumer which consume unions type values from avro decoder. - * Write the data to {@link org.apache.arrow.vector.complex.UnionVector}. + * Consumer which consume unions type values from avro decoder. Write the data to {@link + * org.apache.arrow.vector.complex.UnionVector}. */ public class AvroUnionsConsumer extends BaseAvroConsumer { private Consumer[] delegates; private Types.MinorType[] types; - /** - * Instantiate an AvroUnionConsumer. - */ + /** Instantiate an AvroUnionConsumer. */ public AvroUnionsConsumer(UnionVector vector, Consumer[] delegates, Types.MinorType[] types) { super(vector); @@ -53,7 +50,8 @@ public void consume(Decoder decoder) throws IOException { vector.setType(currentIndex, types[fieldIndex]); // In UnionVector we need to set sub vector writer position before consume a value - // because in the previous iterations we might not have written to the specific union sub vector. + // because in the previous iterations we might not have written to the specific union sub + // vector. delegate.setPosition(currentIndex); delegate.consume(decoder); diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/BaseAvroConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/BaseAvroConsumer.java index 66a6cda6840..3bf8704e45b 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/BaseAvroConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/BaseAvroConsumer.java @@ -21,6 +21,7 @@ /** * Base class for non-skippable avro consumers. + * * @param vector type. */ public abstract class BaseAvroConsumer implements Consumer { @@ -30,6 +31,7 @@ public abstract class BaseAvroConsumer implements Consume /** * Constructs a base avro consumer. + * * @param vector the vector to consume. */ public BaseAvroConsumer(T vector) { diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/CompositeAvroConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/CompositeAvroConsumer.java index 97812226180..cd01af28209 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/CompositeAvroConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/CompositeAvroConsumer.java @@ -19,15 +19,11 @@ import java.io.IOException; import java.util.List; - import org.apache.arrow.util.AutoCloseables; import org.apache.arrow.vector.VectorSchemaRoot; import org.apache.avro.io.Decoder; -/** - * Composite consumer which hold all consumers. - * It manages the consume and cleanup process. - */ +/** Composite consumer which hold all consumers. It manages the consume and cleanup process. */ public class CompositeAvroConsumer implements AutoCloseable { private final List consumers; @@ -40,18 +36,14 @@ public CompositeAvroConsumer(List consumers) { this.consumers = consumers; } - /** - * Consume decoder data. - */ + /** Consume decoder data. */ public void consume(Decoder decoder) throws IOException { for (Consumer consumer : consumers) { consumer.consume(decoder); } } - /** - * Reset vector of consumers with the given {@link VectorSchemaRoot}. - */ + /** Reset vector of consumers with the given {@link VectorSchemaRoot}. */ public void resetConsumerVectors(VectorSchemaRoot root) { int index = 0; for (Consumer consumer : consumers) { diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/Consumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/Consumer.java index c2ae1ce77b2..65925630b74 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/Consumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/Consumer.java @@ -18,54 +18,45 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.arrow.vector.FieldVector; import org.apache.avro.io.Decoder; /** * Interface that is used to consume values from avro decoder. + * * @param The vector within consumer or its delegate, used for partially consume purpose. */ public interface Consumer extends AutoCloseable { /** * Consume a specific type value from avro decoder and write it to vector. + * * @param decoder avro decoder to read data * @throws IOException on error */ void consume(Decoder decoder) throws IOException; - /** - * Add null value to vector by making writer position + 1. - */ + /** Add null value to vector by making writer position + 1. */ void addNull(); - /** - * Set the position to write value into vector. - */ + /** Set the position to write value into vector. */ void setPosition(int index); - /** - * Get the vector within the consumer. - */ + /** Get the vector within the consumer. */ FieldVector getVector(); - /** - * Close this consumer when occurs exception to avoid potential leak. - */ + /** Close this consumer when occurs exception to avoid potential leak. */ void close() throws Exception; /** * Reset the vector within consumer for partial read purpose. + * * @return true if reset is successful, false if reset is not needed. */ boolean resetValueVector(T vector); - /** - * Indicates whether the consumer is type of {@link SkipConsumer}. - */ + /** Indicates whether the consumer is type of {@link SkipConsumer}. */ default boolean skippable() { return false; } - } diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/SkipConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/SkipConsumer.java index 1ac0a6d7155..05a843afd1a 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/SkipConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/SkipConsumer.java @@ -18,13 +18,10 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.arrow.vector.FieldVector; import org.apache.avro.io.Decoder; -/** - * Consumer which skip (throw away) data from the decoder. - */ +/** Consumer which skip (throw away) data from the decoder. */ public class SkipConsumer implements Consumer { private final SkipFunction skipFunction; @@ -39,12 +36,10 @@ public void consume(Decoder decoder) throws IOException { } @Override - public void addNull() { - } + public void addNull() {} @Override - public void setPosition(int index) { - } + public void setPosition(int index) {} @Override public FieldVector getVector() { @@ -52,8 +47,7 @@ public FieldVector getVector() { } @Override - public void close() throws Exception { - } + public void close() throws Exception {} @Override public boolean resetValueVector(FieldVector vector) { diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/SkipFunction.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/SkipFunction.java index 93fc4a7fede..61530cb4e4e 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/SkipFunction.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/SkipFunction.java @@ -18,12 +18,9 @@ package org.apache.arrow.adapter.avro.consumers; import java.io.IOException; - import org.apache.avro.io.Decoder; -/** - * Adapter function to skip (throw away) data from the decoder. - */ +/** Adapter function to skip (throw away) data from the decoder. */ @FunctionalInterface public interface SkipFunction { void apply(Decoder decoder) throws IOException; diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroDateConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroDateConsumer.java index a5c36d88fb7..ee4de339429 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroDateConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroDateConsumer.java @@ -18,20 +18,17 @@ package org.apache.arrow.adapter.avro.consumers.logical; import java.io.IOException; - import org.apache.arrow.adapter.avro.consumers.BaseAvroConsumer; import org.apache.arrow.vector.DateDayVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume date type values from avro decoder. - * Write the data to {@link DateDayVector}. + * Consumer which consume date type values from avro decoder. Write the data to {@link + * DateDayVector}. */ public class AvroDateConsumer extends BaseAvroConsumer { - /** - * Instantiate a AvroDateConsumer. - */ + /** Instantiate a AvroDateConsumer. */ public AvroDateConsumer(DateDayVector vector) { super(vector); } diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroDecimalConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroDecimalConsumer.java index ebe5ca3884e..2defa736b19 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroDecimalConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroDecimalConsumer.java @@ -19,35 +19,28 @@ import java.io.IOException; import java.nio.ByteBuffer; - import org.apache.arrow.adapter.avro.consumers.BaseAvroConsumer; import org.apache.arrow.util.Preconditions; import org.apache.arrow.vector.DecimalVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume decimal type values from avro decoder. - * Write the data to {@link DecimalVector}. + * Consumer which consume decimal type values from avro decoder. Write the data to {@link + * DecimalVector}. */ public abstract class AvroDecimalConsumer extends BaseAvroConsumer { - /** - * Instantiate a AvroDecimalConsumer. - */ + /** Instantiate a AvroDecimalConsumer. */ public AvroDecimalConsumer(DecimalVector vector) { super(vector); } - /** - * Consumer for decimal logical type with original bytes type. - */ + /** Consumer for decimal logical type with original bytes type. */ public static class BytesDecimalConsumer extends AvroDecimalConsumer { private ByteBuffer cacheBuffer; - /** - * Instantiate a BytesDecimalConsumer. - */ + /** Instantiate a BytesDecimalConsumer. */ public BytesDecimalConsumer(DecimalVector vector) { super(vector); } @@ -60,19 +53,14 @@ public void consume(Decoder decoder) throws IOException { cacheBuffer.get(bytes); vector.setBigEndian(currentIndex++, bytes); } - } - /** - * Consumer for decimal logical type with original fixed type. - */ + /** Consumer for decimal logical type with original fixed type. */ public static class FixedDecimalConsumer extends AvroDecimalConsumer { private byte[] reuseBytes; - /** - * Instantiate a FixedDecimalConsumer. - */ + /** Instantiate a FixedDecimalConsumer. */ public FixedDecimalConsumer(DecimalVector vector, int size) { super(vector); Preconditions.checkArgument(size <= 16, "Decimal bytes length should <= 16."); diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimeMicroConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimeMicroConsumer.java index 89216d4ad14..e838a269765 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimeMicroConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimeMicroConsumer.java @@ -18,20 +18,17 @@ package org.apache.arrow.adapter.avro.consumers.logical; import java.io.IOException; - import org.apache.arrow.adapter.avro.consumers.BaseAvroConsumer; import org.apache.arrow.vector.TimeMicroVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume date time-micro values from avro decoder. - * Write the data to {@link TimeMicroVector}. + * Consumer which consume date time-micro values from avro decoder. Write the data to {@link + * TimeMicroVector}. */ public class AvroTimeMicroConsumer extends BaseAvroConsumer { - /** - * Instantiate a AvroTimeMicroConsumer. - */ + /** Instantiate a AvroTimeMicroConsumer. */ public AvroTimeMicroConsumer(TimeMicroVector vector) { super(vector); } diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimeMillisConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimeMillisConsumer.java index ab5df8d4bc8..3780e4bb4a0 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimeMillisConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimeMillisConsumer.java @@ -18,20 +18,17 @@ package org.apache.arrow.adapter.avro.consumers.logical; import java.io.IOException; - import org.apache.arrow.adapter.avro.consumers.BaseAvroConsumer; import org.apache.arrow.vector.TimeMilliVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume date time-millis values from avro decoder. - * Write the data to {@link TimeMilliVector}. + * Consumer which consume date time-millis values from avro decoder. Write the data to {@link + * TimeMilliVector}. */ public class AvroTimeMillisConsumer extends BaseAvroConsumer { - /** - * Instantiate a AvroTimeMilliConsumer. - */ + /** Instantiate a AvroTimeMilliConsumer. */ public AvroTimeMillisConsumer(TimeMilliVector vector) { super(vector); } diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimestampMicrosConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimestampMicrosConsumer.java index 93b39d479ff..9eb01ac76db 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimestampMicrosConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimestampMicrosConsumer.java @@ -18,20 +18,17 @@ package org.apache.arrow.adapter.avro.consumers.logical; import java.io.IOException; - import org.apache.arrow.adapter.avro.consumers.BaseAvroConsumer; import org.apache.arrow.vector.TimeStampMicroVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume date timestamp-micro values from avro decoder. - * Write the data to {@link TimeStampMicroVector}. + * Consumer which consume date timestamp-micro values from avro decoder. Write the data to {@link + * TimeStampMicroVector}. */ public class AvroTimestampMicrosConsumer extends BaseAvroConsumer { - /** - * Instantiate a AvroTimestampMicroConsumer. - */ + /** Instantiate a AvroTimestampMicroConsumer. */ public AvroTimestampMicrosConsumer(TimeStampMicroVector vector) { super(vector); } diff --git a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimestampMillisConsumer.java b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimestampMillisConsumer.java index 9e651c3959f..e665082d143 100644 --- a/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimestampMillisConsumer.java +++ b/java/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/logical/AvroTimestampMillisConsumer.java @@ -18,20 +18,17 @@ package org.apache.arrow.adapter.avro.consumers.logical; import java.io.IOException; - import org.apache.arrow.adapter.avro.consumers.BaseAvroConsumer; import org.apache.arrow.vector.TimeStampMilliVector; import org.apache.avro.io.Decoder; /** - * Consumer which consume date timestamp-millis values from avro decoder. - * Write the data to {@link TimeStampMilliVector}. + * Consumer which consume date timestamp-millis values from avro decoder. Write the data to {@link + * TimeStampMilliVector}. */ public class AvroTimestampMillisConsumer extends BaseAvroConsumer { - /** - * Instantiate a AvroTimestampMillisConsumer. - */ + /** Instantiate a AvroTimestampMillisConsumer. */ public AvroTimestampMillisConsumer(TimeStampMilliVector vector) { super(vector); } diff --git a/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroLogicalTypesTest.java b/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroLogicalTypesTest.java index 6ee04e33a5c..956760fa34b 100644 --- a/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroLogicalTypesTest.java +++ b/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroLogicalTypesTest.java @@ -27,7 +27,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; - import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.VectorSchemaRoot; import org.apache.arrow.vector.util.DateUtility; @@ -43,13 +42,13 @@ public void testTimestampMicros() throws Exception { Schema schema = getSchema("logical/test_timestamp_micros.avsc"); List data = Arrays.asList(10000L, 20000L, 30000L, 40000L, 50000L); - List expected = Arrays.asList( - DateUtility.getLocalDateTimeFromEpochMicro(10000), - DateUtility.getLocalDateTimeFromEpochMicro(20000), - DateUtility.getLocalDateTimeFromEpochMicro(30000), - DateUtility.getLocalDateTimeFromEpochMicro(40000), - DateUtility.getLocalDateTimeFromEpochMicro(50000) - ); + List expected = + Arrays.asList( + DateUtility.getLocalDateTimeFromEpochMicro(10000), + DateUtility.getLocalDateTimeFromEpochMicro(20000), + DateUtility.getLocalDateTimeFromEpochMicro(30000), + DateUtility.getLocalDateTimeFromEpochMicro(40000), + DateUtility.getLocalDateTimeFromEpochMicro(50000)); VectorSchemaRoot root = writeAndRead(schema, data); FieldVector vector = root.getFieldVectors().get(0); @@ -62,13 +61,13 @@ public void testTimestampMillis() throws Exception { Schema schema = getSchema("logical/test_timestamp_millis.avsc"); List data = Arrays.asList(10000L, 20000L, 30000L, 40000L, 50000L); - List expected = Arrays.asList( - DateUtility.getLocalDateTimeFromEpochMilli(10000), - DateUtility.getLocalDateTimeFromEpochMilli(20000), - DateUtility.getLocalDateTimeFromEpochMilli(30000), - DateUtility.getLocalDateTimeFromEpochMilli(40000), - DateUtility.getLocalDateTimeFromEpochMilli(50000) - ); + List expected = + Arrays.asList( + DateUtility.getLocalDateTimeFromEpochMilli(10000), + DateUtility.getLocalDateTimeFromEpochMilli(20000), + DateUtility.getLocalDateTimeFromEpochMilli(30000), + DateUtility.getLocalDateTimeFromEpochMilli(40000), + DateUtility.getLocalDateTimeFromEpochMilli(50000)); VectorSchemaRoot root = writeAndRead(schema, data); FieldVector vector = root.getFieldVectors().get(0); @@ -93,13 +92,13 @@ public void testTimeMillis() throws Exception { Schema schema = getSchema("logical/test_time_millis.avsc"); List data = Arrays.asList(100, 200, 300, 400, 500); - List expected = Arrays.asList( - DateUtility.getLocalDateTimeFromEpochMilli(100), - DateUtility.getLocalDateTimeFromEpochMilli(200), - DateUtility.getLocalDateTimeFromEpochMilli(300), - DateUtility.getLocalDateTimeFromEpochMilli(400), - DateUtility.getLocalDateTimeFromEpochMilli(500) - ); + List expected = + Arrays.asList( + DateUtility.getLocalDateTimeFromEpochMilli(100), + DateUtility.getLocalDateTimeFromEpochMilli(200), + DateUtility.getLocalDateTimeFromEpochMilli(300), + DateUtility.getLocalDateTimeFromEpochMilli(400), + DateUtility.getLocalDateTimeFromEpochMilli(500)); VectorSchemaRoot root = writeAndRead(schema, data); FieldVector vector = root.getFieldVectors().get(0); @@ -137,7 +136,6 @@ public void testDecimalWithOriginalBytes() throws Exception { VectorSchemaRoot root = writeAndRead(schema, data); FieldVector vector = root.getFieldVectors().get(0); checkPrimitiveResult(expected, vector); - } @Test @@ -174,10 +172,9 @@ public void testInvalidDecimalPrecision() throws Exception { data.add(buffer); } - IllegalArgumentException e = assertThrows(IllegalArgumentException.class, - () -> writeAndRead(schema, data)); + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> writeAndRead(schema, data)); assertTrue(e.getMessage().contains("Precision must be in range of 1 to 38")); - } @Test @@ -197,5 +194,4 @@ public void testFailedToCreateDecimalLogicalType() throws Exception { Schema schema3 = getSchema("logical/test_decimal_invalid4.avsc"); assertNull(schema3.getLogicalType()); } - } diff --git a/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroSkipFieldTest.java b/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroSkipFieldTest.java index a37eca6514e..7c102610812 100644 --- a/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroSkipFieldTest.java +++ b/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroSkipFieldTest.java @@ -25,7 +25,6 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Set; - import org.apache.arrow.vector.VectorSchemaRoot; import org.apache.arrow.vector.complex.StructVector; import org.apache.arrow.vector.types.Types; @@ -40,7 +39,10 @@ public class AvroSkipFieldTest extends AvroTestBase { public void testSkipUnionWithOneField() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f0"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_union_before.avsc"); Schema expectedSchema = getSchema("skip/test_skip_union_one_field_expected.avsc"); @@ -69,7 +71,10 @@ public void testSkipUnionWithOneField() throws Exception { public void testSkipUnionWithNullableOneField() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f1"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_union_before.avsc"); Schema expectedSchema = getSchema("skip/test_skip_union_nullable_field_expected.avsc"); @@ -98,7 +103,10 @@ public void testSkipUnionWithNullableOneField() throws Exception { public void testSkipUnionWithMultiFields() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f2"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_union_before.avsc"); Schema expectedSchema = getSchema("skip/test_skip_union_multi_fields_expected.avsc"); @@ -127,7 +135,10 @@ public void testSkipUnionWithMultiFields() throws Exception { public void testSkipMapField() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f1"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_map_before.avsc"); Schema expectedSchema = getSchema("skip/test_skip_map_expected.avsc"); @@ -159,7 +170,10 @@ public void testSkipMapField() throws Exception { public void testSkipArrayField() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f1"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_array_before.avsc"); Schema expectedSchema = getSchema("skip/test_skip_array_expected.avsc"); @@ -188,7 +202,10 @@ public void testSkipMultiFields() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f1"); skipFieldNames.add("f2"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("test_record.avsc"); Schema expectedSchema = getSchema("skip/test_skip_multi_fields_expected.avsc"); @@ -215,7 +232,10 @@ public void testSkipMultiFields() throws Exception { public void testSkipStringField() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f2"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_base1.avsc"); Schema expectedSchema = getSchema("skip/test_skip_string_expected.avsc"); @@ -228,7 +248,8 @@ public void testSkipStringField() throws Exception { GenericData.Fixed fixed = new GenericData.Fixed(schema.getField("f0").schema()); fixed.bytes(testBytes); record.put(0, fixed); - GenericData.EnumSymbol symbol = new GenericData.EnumSymbol(schema.getField("f1").schema(), "TEST" + i % 2); + GenericData.EnumSymbol symbol = + new GenericData.EnumSymbol(schema.getField("f1").schema(), "TEST" + i % 2); record.put(1, symbol); record.put(2, "testtest" + i); record.put(3, ByteBuffer.wrap(testBytes)); @@ -249,7 +270,10 @@ public void testSkipStringField() throws Exception { public void testSkipBytesField() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f3"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_base1.avsc"); Schema expectedSchema = getSchema("skip/test_skip_bytes_expected.avsc"); @@ -262,7 +286,8 @@ public void testSkipBytesField() throws Exception { GenericData.Fixed fixed = new GenericData.Fixed(schema.getField("f0").schema()); fixed.bytes(testBytes); record.put(0, fixed); - GenericData.EnumSymbol symbol = new GenericData.EnumSymbol(schema.getField("f1").schema(), "TEST" + i % 2); + GenericData.EnumSymbol symbol = + new GenericData.EnumSymbol(schema.getField("f1").schema(), "TEST" + i % 2); record.put(1, symbol); record.put(2, "testtest" + i); record.put(3, ByteBuffer.wrap(testBytes)); @@ -283,7 +308,10 @@ public void testSkipBytesField() throws Exception { public void testSkipFixedField() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f0"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_base1.avsc"); Schema expectedSchema = getSchema("skip/test_skip_fixed_expected.avsc"); @@ -296,7 +324,8 @@ public void testSkipFixedField() throws Exception { GenericData.Fixed fixed = new GenericData.Fixed(schema.getField("f0").schema()); fixed.bytes(testBytes); record.put(0, fixed); - GenericData.EnumSymbol symbol = new GenericData.EnumSymbol(schema.getField("f1").schema(), "TEST" + i % 2); + GenericData.EnumSymbol symbol = + new GenericData.EnumSymbol(schema.getField("f1").schema(), "TEST" + i % 2); record.put(1, symbol); record.put(2, "testtest" + i); record.put(3, ByteBuffer.wrap(testBytes)); @@ -317,7 +346,10 @@ public void testSkipFixedField() throws Exception { public void testSkipEnumField() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f1"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_base1.avsc"); Schema expectedSchema = getSchema("skip/test_skip_fixed_expected.avsc"); @@ -330,7 +362,8 @@ public void testSkipEnumField() throws Exception { GenericData.Fixed fixed = new GenericData.Fixed(schema.getField("f0").schema()); fixed.bytes(testBytes); record.put(0, fixed); - GenericData.EnumSymbol symbol = new GenericData.EnumSymbol(schema.getField("f1").schema(), "TEST" + i % 2); + GenericData.EnumSymbol symbol = + new GenericData.EnumSymbol(schema.getField("f1").schema(), "TEST" + i % 2); record.put(1, symbol); record.put(2, "testtest" + i); record.put(3, ByteBuffer.wrap(testBytes)); @@ -351,7 +384,10 @@ public void testSkipEnumField() throws Exception { public void testSkipBooleanField() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f0"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_base2.avsc"); Schema expectedSchema = getSchema("skip/test_skip_boolean_expected.avsc"); @@ -384,7 +420,10 @@ public void testSkipBooleanField() throws Exception { public void testSkipIntField() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f1"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_base2.avsc"); Schema expectedSchema = getSchema("skip/test_skip_int_expected.avsc"); @@ -417,7 +456,10 @@ public void testSkipIntField() throws Exception { public void testSkipLongField() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f2"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_base2.avsc"); Schema expectedSchema = getSchema("skip/test_skip_long_expected.avsc"); @@ -450,7 +492,10 @@ public void testSkipLongField() throws Exception { public void testSkipFloatField() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f3"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_base2.avsc"); Schema expectedSchema = getSchema("skip/test_skip_float_expected.avsc"); @@ -483,7 +528,10 @@ public void testSkipFloatField() throws Exception { public void testSkipDoubleField() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f4"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_base2.avsc"); Schema expectedSchema = getSchema("skip/test_skip_double_expected.avsc"); @@ -516,7 +564,10 @@ public void testSkipDoubleField() throws Exception { public void testSkipRecordField() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f0"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("skip/test_skip_record_before.avsc"); Schema nestedSchema = schema.getFields().get(0).schema(); ArrayList data = new ArrayList<>(); @@ -546,7 +597,10 @@ public void testSkipRecordField() throws Exception { public void testSkipNestedFields() throws Exception { Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f0.f0"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); Schema schema = getSchema("test_nested_record.avsc"); Schema nestedSchema = schema.getFields().get(0).schema(); ArrayList data = new ArrayList<>(); @@ -602,21 +656,26 @@ public void testSkipThirdLevelField() throws Exception { assertEquals(Types.MinorType.STRUCT, root1.getFieldVectors().get(0).getMinorType()); StructVector secondLevelVector = (StructVector) root1.getFieldVectors().get(0); assertEquals(1, secondLevelVector.getChildrenFromFields().size()); - assertEquals(Types.MinorType.STRUCT, secondLevelVector.getChildrenFromFields().get(0).getMinorType()); + assertEquals( + Types.MinorType.STRUCT, secondLevelVector.getChildrenFromFields().get(0).getMinorType()); StructVector thirdLevelVector = (StructVector) secondLevelVector.getChildrenFromFields().get(0); assertEquals(3, thirdLevelVector.getChildrenFromFields().size()); // skip third level field and validate Set skipFieldNames = new HashSet<>(); skipFieldNames.add("f0.f0.f0"); - config = new AvroToArrowConfigBuilder(config.getAllocator()).setSkipFieldNames(skipFieldNames).build(); + config = + new AvroToArrowConfigBuilder(config.getAllocator()) + .setSkipFieldNames(skipFieldNames) + .build(); VectorSchemaRoot root2 = writeAndRead(firstLevelSchema, data); assertEquals(1, root2.getFieldVectors().size()); assertEquals(Types.MinorType.STRUCT, root2.getFieldVectors().get(0).getMinorType()); StructVector secondStruct = (StructVector) root2.getFieldVectors().get(0); assertEquals(1, secondStruct.getChildrenFromFields().size()); - assertEquals(Types.MinorType.STRUCT, secondStruct.getChildrenFromFields().get(0).getMinorType()); + assertEquals( + Types.MinorType.STRUCT, secondStruct.getChildrenFromFields().get(0).getMinorType()); StructVector thirdStruct = (StructVector) secondStruct.getChildrenFromFields().get(0); assertEquals(2, thirdStruct.getChildrenFromFields().size()); diff --git a/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroTestBase.java b/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroTestBase.java index 60a3a285db3..1c2e551c631 100644 --- a/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroTestBase.java +++ b/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroTestBase.java @@ -29,7 +29,6 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.FieldVector; @@ -51,8 +50,7 @@ public class AvroTestBase { - @ClassRule - public static final TemporaryFolder TMP = new TemporaryFolder(); + @ClassRule public static final TemporaryFolder TMP = new TemporaryFolder(); protected AvroToArrowConfig config; @@ -64,18 +62,21 @@ public void init() { public static Schema getSchema(String schemaName) throws Exception { try { - // Attempt to use JDK 9 behavior of getting the module then the resource stream from the module. + // Attempt to use JDK 9 behavior of getting the module then the resource stream from the + // module. // Note that this code is caller-sensitive. Method getModuleMethod = Class.class.getMethod("getModule"); Object module = getModuleMethod.invoke(TestWriteReadAvroRecord.class); - Method getResourceAsStreamFromModule = module.getClass().getMethod("getResourceAsStream", String.class); - try (InputStream is = (InputStream) getResourceAsStreamFromModule.invoke(module, "/schema/" + schemaName)) { - return new Schema.Parser() - .parse(is); + Method getResourceAsStreamFromModule = + module.getClass().getMethod("getResourceAsStream", String.class); + try (InputStream is = + (InputStream) getResourceAsStreamFromModule.invoke(module, "/schema/" + schemaName)) { + return new Schema.Parser().parse(is); } } catch (NoSuchMethodException ex) { // Use JDK8 behavior. - try (InputStream is = TestWriteReadAvroRecord.class.getResourceAsStream("/schema/" + schemaName)) { + try (InputStream is = + TestWriteReadAvroRecord.class.getResourceAsStream("/schema/" + schemaName)) { return new Schema.Parser().parse(is); } } @@ -84,11 +85,11 @@ public static Schema getSchema(String schemaName) throws Exception { protected VectorSchemaRoot writeAndRead(Schema schema, List data) throws Exception { File dataFile = TMP.newFile(); - BinaryEncoder - encoder = new EncoderFactory().directBinaryEncoder(new FileOutputStream(dataFile), null); + BinaryEncoder encoder = + new EncoderFactory().directBinaryEncoder(new FileOutputStream(dataFile), null); DatumWriter writer = new GenericDatumWriter(schema); - BinaryDecoder - decoder = new DecoderFactory().directBinaryDecoder(new FileInputStream(dataFile), null); + BinaryDecoder decoder = + new DecoderFactory().directBinaryDecoder(new FileInputStream(dataFile), null); for (Object value : data) { writer.write(value, encoder); @@ -145,7 +146,8 @@ protected void checkPrimitiveResult(List data, FieldVector vector) { } } - protected void checkRecordResult(Schema schema, ArrayList data, VectorSchemaRoot root) { + protected void checkRecordResult( + Schema schema, ArrayList data, VectorSchemaRoot root) { assertEquals(data.size(), root.getRowCount()); assertEquals(schema.getFields().size(), root.getFieldVectors().size()); @@ -157,10 +159,10 @@ protected void checkRecordResult(Schema schema, ArrayList data, V checkPrimitiveResult(fieldData, root.getFieldVectors().get(i)); } - } - protected void checkNestedRecordResult(Schema schema, List data, VectorSchemaRoot root) { + protected void checkNestedRecordResult( + Schema schema, List data, VectorSchemaRoot root) { assertEquals(data.size(), root.getRowCount()); assertTrue(schema.getFields().size() == 1); @@ -176,10 +178,8 @@ protected void checkNestedRecordResult(Schema schema, List data, checkPrimitiveResult(fieldData, structVector.getChildrenFromFields().get(i)); } - } - // belows are for iterator api protected void checkArrayResult(List> expected, List vectors) { @@ -194,10 +194,12 @@ protected void checkArrayResult(List> expected, List vectors } } - protected void checkRecordResult(Schema schema, ArrayList data, List roots) { - roots.forEach(root -> { - assertEquals(schema.getFields().size(), root.getFieldVectors().size()); - }); + protected void checkRecordResult( + Schema schema, ArrayList data, List roots) { + roots.forEach( + root -> { + assertEquals(schema.getFields().size(), root.getFieldVectors().size()); + }); for (int i = 0; i < schema.getFields().size(); i++) { List fieldData = new ArrayList(); @@ -210,7 +212,6 @@ protected void checkRecordResult(Schema schema, ArrayList data, L checkPrimitiveResult(fieldData, vectors); } - } protected void checkPrimitiveResult(List data, List vectors) { diff --git a/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroToArrowIteratorTest.java b/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroToArrowIteratorTest.java index 02f7a373373..0479e3693e3 100644 --- a/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroToArrowIteratorTest.java +++ b/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroToArrowIteratorTest.java @@ -28,7 +28,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.util.AutoCloseables; @@ -59,11 +58,11 @@ public void init() { private AvroToArrowVectorIterator convert(Schema schema, List data) throws Exception { File dataFile = TMP.newFile(); - BinaryEncoder - encoder = new EncoderFactory().directBinaryEncoder(new FileOutputStream(dataFile), null); + BinaryEncoder encoder = + new EncoderFactory().directBinaryEncoder(new FileOutputStream(dataFile), null); DatumWriter writer = new GenericDatumWriter(schema); - BinaryDecoder - decoder = new DecoderFactory().directBinaryDecoder(new FileInputStream(dataFile), null); + BinaryDecoder decoder = + new DecoderFactory().directBinaryDecoder(new FileInputStream(dataFile), null); for (Object value : data) { writer.write(value, encoder); @@ -107,7 +106,7 @@ public void testNullableStringType() throws Exception { List roots = new ArrayList<>(); List vectors = new ArrayList<>(); - try (AvroToArrowVectorIterator iterator = convert(schema, data);) { + try (AvroToArrowVectorIterator iterator = convert(schema, data); ) { while (iterator.hasNext()) { VectorSchemaRoot root = iterator.next(); FieldVector vector = root.getFieldVectors().get(0); @@ -117,7 +116,6 @@ public void testNullableStringType() throws Exception { } checkPrimitiveResult(expected, vectors); AutoCloseables.close(roots); - } @Test @@ -140,18 +138,18 @@ public void testRecordType() throws Exception { } checkRecordResult(schema, data, roots); AutoCloseables.close(roots); - } @Test public void testArrayType() throws Exception { Schema schema = getSchema("test_array.avsc"); - List> data = Arrays.asList( - Arrays.asList("11", "222", "999"), - Arrays.asList("12222", "2333", "1000"), - Arrays.asList("1rrr", "2ggg"), - Arrays.asList("1vvv", "2bbb"), - Arrays.asList("1fff", "2")); + List> data = + Arrays.asList( + Arrays.asList("11", "222", "999"), + Arrays.asList("12222", "2333", "1000"), + Arrays.asList("1rrr", "2ggg"), + Arrays.asList("1vvv", "2bbb"), + Arrays.asList("1fff", "2")); List roots = new ArrayList<>(); List vectors = new ArrayList<>(); @@ -172,8 +170,9 @@ public void runLargeNumberOfRows() throws Exception { int x = 0; final int targetRows = 600000; Decoder fakeDecoder = new FakeDecoder(targetRows); - try (AvroToArrowVectorIterator iter = AvroToArrow.avroToArrowIterator(schema, fakeDecoder, - new AvroToArrowConfigBuilder(config.getAllocator()).build())) { + try (AvroToArrowVectorIterator iter = + AvroToArrow.avroToArrowIterator( + schema, fakeDecoder, new AvroToArrowConfigBuilder(config.getAllocator()).build())) { while (iter.hasNext()) { VectorSchemaRoot root = iter.next(); x += root.getRowCount(); @@ -184,9 +183,7 @@ public void runLargeNumberOfRows() throws Exception { assertEquals(x, targetRows); } - /** - * Fake avro decoder to test large data. - */ + /** Fake avro decoder to test large data. */ private class FakeDecoder extends Decoder { private int numRows; @@ -204,8 +201,7 @@ private void validate() throws EOFException { } @Override - public void readNull() throws IOException { - } + public void readNull() throws IOException {} @Override public boolean readBoolean() throws IOException { @@ -243,9 +239,7 @@ public String readString() throws IOException { } @Override - public void skipString() throws IOException { - - } + public void skipString() throws IOException {} @Override public ByteBuffer readBytes(ByteBuffer old) throws IOException { @@ -253,9 +247,7 @@ public ByteBuffer readBytes(ByteBuffer old) throws IOException { } @Override - public void skipBytes() throws IOException { - - } + public void skipBytes() throws IOException {} @Override public void readFixed(byte[] bytes, int start, int length) throws IOException { @@ -264,9 +256,7 @@ public void readFixed(byte[] bytes, int start, int length) throws IOException { } @Override - public void skipFixed(int length) throws IOException { - - } + public void skipFixed(int length) throws IOException {} @Override public int readEnum() throws IOException { diff --git a/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroToArrowTest.java b/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroToArrowTest.java index 1c642041917..12ec191d953 100644 --- a/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroToArrowTest.java +++ b/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroToArrowTest.java @@ -26,7 +26,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; - import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.VarCharVector; import org.apache.arrow.vector.VectorSchemaRoot; @@ -107,12 +106,13 @@ public void testFixedAttributes() throws Exception { @Test public void testEnumAttributes() throws Exception { Schema schema = getSchema("attrs/test_enum_attrs.avsc"); - List data = Arrays.asList( - new GenericData.EnumSymbol(schema, "SPADES"), - new GenericData.EnumSymbol(schema, "HEARTS"), - new GenericData.EnumSymbol(schema, "DIAMONDS"), - new GenericData.EnumSymbol(schema, "CLUBS"), - new GenericData.EnumSymbol(schema, "SPADES")); + List data = + Arrays.asList( + new GenericData.EnumSymbol(schema, "SPADES"), + new GenericData.EnumSymbol(schema, "HEARTS"), + new GenericData.EnumSymbol(schema, "DIAMONDS"), + new GenericData.EnumSymbol(schema, "CLUBS"), + new GenericData.EnumSymbol(schema, "SPADES")); VectorSchemaRoot root = writeAndRead(schema, data); FieldVector vector = root.getFieldVectors().get(0); @@ -174,12 +174,13 @@ public void testNestedRecordType() throws Exception { @Test public void testEnumType() throws Exception { Schema schema = getSchema("test_primitive_enum.avsc"); - List data = Arrays.asList( - new GenericData.EnumSymbol(schema, "SPADES"), - new GenericData.EnumSymbol(schema, "HEARTS"), - new GenericData.EnumSymbol(schema, "DIAMONDS"), - new GenericData.EnumSymbol(schema, "CLUBS"), - new GenericData.EnumSymbol(schema, "SPADES")); + List data = + Arrays.asList( + new GenericData.EnumSymbol(schema, "SPADES"), + new GenericData.EnumSymbol(schema, "HEARTS"), + new GenericData.EnumSymbol(schema, "DIAMONDS"), + new GenericData.EnumSymbol(schema, "CLUBS"), + new GenericData.EnumSymbol(schema, "SPADES")); List expectedIndices = Arrays.asList(0, 1, 2, 3, 0); @@ -304,12 +305,13 @@ public void testNullableDoubleType() throws Exception { @Test public void testBytesType() throws Exception { Schema schema = getSchema("test_primitive_bytes.avsc"); - List data = Arrays.asList( - ByteBuffer.wrap("value1".getBytes(StandardCharsets.UTF_8)), - ByteBuffer.wrap("value2".getBytes(StandardCharsets.UTF_8)), - ByteBuffer.wrap("value3".getBytes(StandardCharsets.UTF_8)), - ByteBuffer.wrap("value4".getBytes(StandardCharsets.UTF_8)), - ByteBuffer.wrap("value5".getBytes(StandardCharsets.UTF_8))); + List data = + Arrays.asList( + ByteBuffer.wrap("value1".getBytes(StandardCharsets.UTF_8)), + ByteBuffer.wrap("value2".getBytes(StandardCharsets.UTF_8)), + ByteBuffer.wrap("value3".getBytes(StandardCharsets.UTF_8)), + ByteBuffer.wrap("value4".getBytes(StandardCharsets.UTF_8)), + ByteBuffer.wrap("value5".getBytes(StandardCharsets.UTF_8))); VectorSchemaRoot root = writeAndRead(schema, data); FieldVector vector = root.getFieldVectors().get(0); @@ -324,7 +326,8 @@ public void testNullableBytesType() throws Exception { ArrayList data = new ArrayList<>(); for (int i = 0; i < 5; i++) { GenericRecord record = new GenericData.Record(schema); - record.put(0, i % 2 == 0 ? ByteBuffer.wrap(("test" + i).getBytes(StandardCharsets.UTF_8)) : null); + record.put( + 0, i % 2 == 0 ? ByteBuffer.wrap(("test" + i).getBytes(StandardCharsets.UTF_8)) : null); data.add(record); } @@ -361,12 +364,13 @@ public void testNullableBooleanType() throws Exception { @Test public void testArrayType() throws Exception { Schema schema = getSchema("test_array.avsc"); - List> data = Arrays.asList( - Arrays.asList("11", "222", "999"), - Arrays.asList("12222", "2333", "1000"), - Arrays.asList("1rrr", "2ggg"), - Arrays.asList("1vvv", "2bbb"), - Arrays.asList("1fff", "2")); + List> data = + Arrays.asList( + Arrays.asList("11", "222", "999"), + Arrays.asList("12222", "2333", "1000"), + Arrays.asList("1rrr", "2ggg"), + Arrays.asList("1vvv", "2bbb"), + Arrays.asList("1fff", "2")); VectorSchemaRoot root = writeAndRead(schema, data); FieldVector vector = root.getFieldVectors().get(0); @@ -473,5 +477,4 @@ public void testNullableUnionType() throws Exception { checkPrimitiveResult(expected, vector); } - } diff --git a/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/TestWriteReadAvroRecord.java b/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/TestWriteReadAvroRecord.java index afbddaa6ed8..1924a58a94d 100644 --- a/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/TestWriteReadAvroRecord.java +++ b/java/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/TestWriteReadAvroRecord.java @@ -22,7 +22,6 @@ import java.io.File; import java.util.ArrayList; import java.util.List; - import org.apache.avro.Schema; import org.apache.avro.file.DataFileReader; import org.apache.avro.file.DataFileWriter; @@ -36,11 +35,9 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; - public class TestWriteReadAvroRecord { - @ClassRule - public static final TemporaryFolder TMP = new TemporaryFolder(); + @ClassRule public static final TemporaryFolder TMP = new TemporaryFolder(); @Test public void testWriteAndRead() throws Exception { @@ -48,7 +45,7 @@ public void testWriteAndRead() throws Exception { File dataFile = TMP.newFile(); Schema schema = AvroTestBase.getSchema("test.avsc"); - //write data to disk + // write data to disk GenericRecord user1 = new GenericData.Record(schema); user1.put("name", "Alyssa"); user1.put("favorite_number", 256); @@ -65,10 +62,10 @@ public void testWriteAndRead() throws Exception { dataFileWriter.append(user2); dataFileWriter.close(); - //read data from disk + // read data from disk DatumReader datumReader = new GenericDatumReader(schema); - DataFileReader - dataFileReader = new DataFileReader(dataFile, datumReader); + DataFileReader dataFileReader = + new DataFileReader(dataFile, datumReader); List result = new ArrayList<>(); while (dataFileReader.hasNext()) { GenericRecord user = dataFileReader.next(); @@ -86,5 +83,4 @@ public void testWriteAndRead() throws Exception { assertEquals(7, deUser2.get("favorite_number")); assertEquals("red", deUser2.get("favorite_color").toString()); } - } diff --git a/java/adapter/avro/src/test/resources/logback.xml b/java/adapter/avro/src/test/resources/logback.xml index 4c54d18a210..9fa3f1e3b43 100644 --- a/java/adapter/avro/src/test/resources/logback.xml +++ b/java/adapter/avro/src/test/resources/logback.xml @@ -1,4 +1,4 @@ - + - diff --git a/java/adapter/jdbc/pom.xml b/java/adapter/jdbc/pom.xml index dfcd4cfe8f6..e3ce10c02e3 100644 --- a/java/adapter/jdbc/pom.xml +++ b/java/adapter/jdbc/pom.xml @@ -1,116 +1,115 @@ - + license agreements. See the NOTICE file distributed with this work for additional + information regarding copyright ownership. The ASF licenses this file to + You under the Apache License, Version 2.0 (the "License"); you may not use + this file except in compliance with the License. You may obtain a copy of + the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required + by applicable law or agreed to in writing, software distributed under the + License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS + OF ANY KIND, either express or implied. See the License for the specific + language governing permissions and limitations under the License. --> - 4.0.0 - - - org.apache.arrow - arrow-java-root - 16.0.0-SNAPSHOT - ../../pom.xml - - - arrow-jdbc - Arrow JDBC Adapter - (Contrib/Experimental)A library for converting JDBC data to Arrow data. - http://maven.apache.org - - - - - - org.apache.arrow - arrow-memory-core - - - - - org.apache.arrow - arrow-memory-netty - runtime - - - - - org.apache.arrow - arrow-vector - ${arrow.vector.classifier} - - - - org.immutables - value - - - - - com.h2database - h2 - 2.2.224 - test - - - - com.fasterxml.jackson.dataformat - jackson-dataformat-yaml - test - - - - com.fasterxml.jackson.core - jackson-databind - - - - com.fasterxml.jackson.core - jackson-core - - - - com.fasterxml.jackson.core - jackson-annotations - test - - - - org.assertj - assertj-core - test - - - - - - - jdk11+ - - [11,] - - !m2e.version - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - --add-reads=org.apache.arrow.adapter.jdbc=com.fasterxml.jackson.dataformat.yaml --add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED - - - - - - + 4.0.0 + + + org.apache.arrow + arrow-java-root + 16.0.0-SNAPSHOT + ../../pom.xml + + + arrow-jdbc + Arrow JDBC Adapter + (Contrib/Experimental)A library for converting JDBC data to Arrow data. + http://maven.apache.org + + + + + + org.apache.arrow + arrow-memory-core + + + + + org.apache.arrow + arrow-memory-netty + runtime + + + + + org.apache.arrow + arrow-vector + ${arrow.vector.classifier} + + + + org.immutables + value + + + + + com.h2database + h2 + 2.2.224 + test + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + test + + + + com.fasterxml.jackson.core + jackson-databind + + + + com.fasterxml.jackson.core + jackson-core + + + + com.fasterxml.jackson.core + jackson-annotations + test + + + + org.assertj + assertj-core + test + + + + + + + jdk11+ + + [11,] + + !m2e.version + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + --add-reads=org.apache.arrow.adapter.jdbc=com.fasterxml.jackson.dataformat.yaml --add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED + + + + + + diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/ArrowVectorIterator.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/ArrowVectorIterator.java index 427c766982f..56aea04db9d 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/ArrowVectorIterator.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/ArrowVectorIterator.java @@ -23,7 +23,6 @@ import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.Iterator; - import org.apache.arrow.adapter.jdbc.consumer.CompositeJdbcConsumer; import org.apache.arrow.adapter.jdbc.consumer.JdbcConsumer; import org.apache.arrow.adapter.jdbc.consumer.exceptions.JdbcConsumerException; @@ -35,9 +34,7 @@ import org.apache.arrow.vector.types.pojo.Schema; import org.apache.arrow.vector.util.ValueVectorUtility; -/** - * VectorSchemaRoot iterator for partially converting JDBC data. - */ +/** VectorSchemaRoot iterator for partially converting JDBC data. */ public class ArrowVectorIterator implements Iterator, AutoCloseable { private final ResultSet resultSet; @@ -54,13 +51,12 @@ public class ArrowVectorIterator implements Iterator, AutoClos private final int targetBatchSize; - // This is used to track whether the ResultSet has been fully read, and is needed specifically for cases where there + // This is used to track whether the ResultSet has been fully read, and is needed specifically for + // cases where there // is a ResultSet having zero rows (empty): private boolean readComplete = false; - /** - * Construct an instance. - */ + /** Construct an instance. */ private ArrowVectorIterator(ResultSet resultSet, JdbcToArrowConfig config) throws SQLException { this.resultSet = resultSet; this.config = config; @@ -73,12 +69,8 @@ private ArrowVectorIterator(ResultSet resultSet, JdbcToArrowConfig config) throw this.nextBatch = config.isReuseVectorSchemaRoot() ? createVectorSchemaRoot() : null; } - /** - * Create a ArrowVectorIterator to partially convert data. - */ - public static ArrowVectorIterator create( - ResultSet resultSet, - JdbcToArrowConfig config) + /** Create a ArrowVectorIterator to partially convert data. */ + public static ArrowVectorIterator create(ResultSet resultSet, JdbcToArrowConfig config) throws SQLException { ArrowVectorIterator iterator = null; try { @@ -142,10 +134,18 @@ private VectorSchemaRoot createVectorSchemaRoot() throws SQLException { private void initialize(VectorSchemaRoot root) throws SQLException { for (int i = 1; i <= consumers.length; i++) { - final JdbcFieldInfo columnFieldInfo = JdbcToArrowUtils.getJdbcFieldInfoForColumn(rsmd, i, config); + final JdbcFieldInfo columnFieldInfo = + JdbcToArrowUtils.getJdbcFieldInfoForColumn(rsmd, i, config); ArrowType arrowType = config.getJdbcToArrowTypeConverter().apply(columnFieldInfo); - consumers[i - 1] = config.getJdbcConsumerGetter().apply( - arrowType, i, isColumnNullable(resultSet.getMetaData(), i, columnFieldInfo), root.getVector(i - 1), config); + consumers[i - 1] = + config + .getJdbcConsumerGetter() + .apply( + arrowType, + i, + isColumnNullable(resultSet.getMetaData(), i, columnFieldInfo), + root.getVector(i - 1), + config); } } @@ -170,16 +170,17 @@ public boolean hasNext() { } /** - * Gets the next vector. - * If {@link JdbcToArrowConfig#isReuseVectorSchemaRoot()} is false, - * the client is responsible for freeing its resources. + * Gets the next vector. If {@link JdbcToArrowConfig#isReuseVectorSchemaRoot()} is false, the + * client is responsible for freeing its resources. + * * @throws JdbcConsumerException on error from VectorConsumer */ @Override public VectorSchemaRoot next() { Preconditions.checkArgument(hasNext()); try { - VectorSchemaRoot ret = config.isReuseVectorSchemaRoot() ? nextBatch : createVectorSchemaRoot(); + VectorSchemaRoot ret = + config.isReuseVectorSchemaRoot() ? nextBatch : createVectorSchemaRoot(); load(ret); return ret; } catch (Exception e) { @@ -193,8 +194,9 @@ public VectorSchemaRoot next() { } /** - * Clean up resources ONLY WHEN THE {@link VectorSchemaRoot} HOLDING EACH BATCH IS REUSED. If a new VectorSchemaRoot - * is created for each batch, each root must be closed manually by the client code. + * Clean up resources ONLY WHEN THE {@link VectorSchemaRoot} HOLDING EACH BATCH IS REUSED. If a + * new VectorSchemaRoot is created for each batch, each root must be closed manually by the client + * code. */ @Override public void close() { diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/Constants.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/Constants.java index 5b01077b179..ce5f28680d8 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/Constants.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/Constants.java @@ -17,9 +17,7 @@ package org.apache.arrow.adapter.jdbc; -/** - * String constants used for metadata returned on Vectors. - */ +/** String constants used for metadata returned on Vectors. */ public class Constants { private Constants() {} @@ -28,5 +26,4 @@ private Constants() {} public static final String SQL_TABLE_NAME_KEY = "SQL_TABLE_NAME"; public static final String SQL_COLUMN_NAME_KEY = "SQL_COLUMN_NAME"; public static final String SQL_TYPE_KEY = "SQL_TYPE"; - } 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 d16964ea144..536b837d5bd 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 @@ -21,18 +21,19 @@ import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Types; - import org.apache.arrow.util.Preconditions; /** - * This class represents the information about a JDBC ResultSet Field that is - * needed to construct an {@link org.apache.arrow.vector.types.pojo.ArrowType}. - * Currently, this is: + * This class represents the information about a JDBC ResultSet Field that is needed to construct an + * {@link org.apache.arrow.vector.types.pojo.ArrowType}. Currently, this is: + * *
    - *
  • The JDBC {@link java.sql.Types} type.
  • - *
  • The nullability.
  • - *
  • The field's precision (used for {@link java.sql.Types#DECIMAL} and {@link java.sql.Types#NUMERIC} types).
  • - *
  • The field's scale (used for {@link java.sql.Types#DECIMAL} and {@link java.sql.Types#NUMERIC} types).
  • + *
  • The JDBC {@link java.sql.Types} type. + *
  • The nullability. + *
  • The field's precision (used for {@link java.sql.Types#DECIMAL} and {@link + * java.sql.Types#NUMERIC} types). + *
  • The field's scale (used for {@link java.sql.Types#DECIMAL} and {@link + * java.sql.Types#NUMERIC} types). *
*/ public class JdbcFieldInfo { @@ -45,12 +46,13 @@ public class JdbcFieldInfo { private final int displaySize; /** - * Builds a JdbcFieldInfo using only the {@link java.sql.Types} type. Do not use this constructor - * if the field type is {@link java.sql.Types#DECIMAL} or {@link java.sql.Types#NUMERIC}; the precision and - * scale will be set to 0. + * Builds a JdbcFieldInfo using only the {@link java.sql.Types} type. Do not use this + * constructor if the field type is {@link java.sql.Types#DECIMAL} or {@link + * java.sql.Types#NUMERIC}; the precision and scale will be set to 0. * * @param jdbcType The {@link java.sql.Types} type. - * @throws IllegalArgumentException if jdbcType is {@link java.sql.Types#DECIMAL} or {@link java.sql.Types#NUMERIC}. + * @throws IllegalArgumentException if jdbcType is {@link java.sql.Types#DECIMAL} or {@link + * java.sql.Types#NUMERIC}. */ public JdbcFieldInfo(int jdbcType) { Preconditions.checkArgument( @@ -67,7 +69,8 @@ public JdbcFieldInfo(int jdbcType) { /** * Builds a JdbcFieldInfo from the {@link java.sql.Types} type, precision, and scale. - * Use this constructor for {@link java.sql.Types#DECIMAL} and {@link java.sql.Types#NUMERIC} types. + * Use this constructor for {@link java.sql.Types#DECIMAL} and {@link java.sql.Types#NUMERIC} + * types. * * @param jdbcType The {@link java.sql.Types} type. * @param precision The field's numeric precision. @@ -84,11 +87,13 @@ public JdbcFieldInfo(int jdbcType, int precision, int scale) { } /** - * Builds a JdbcFieldInfo from the {@link java.sql.Types} type, nullability, precision, and scale. + * Builds a JdbcFieldInfo from the {@link java.sql.Types} type, nullability, + * precision, and scale. * * @param jdbcType The {@link java.sql.Types} type. * @param nullability The nullability. Must be one of {@link ResultSetMetaData#columnNoNulls}, - * {@link ResultSetMetaData#columnNullable}, or {@link ResultSetMetaData#columnNullableUnknown}. + * {@link ResultSetMetaData#columnNullable}, or {@link + * ResultSetMetaData#columnNullableUnknown}. * @param precision The field's numeric precision. * @param scale The field's numeric scale. */ @@ -103,7 +108,8 @@ public JdbcFieldInfo(int jdbcType, int nullability, int precision, int scale) { } /** - * Builds a JdbcFieldInfo from the corresponding {@link java.sql.ResultSetMetaData} column. + * Builds a JdbcFieldInfo from the corresponding {@link java.sql.ResultSetMetaData} + * column. * * @param rsmd The {@link java.sql.ResultSetMetaData} to get the field information from. * @param column The column to get the field information for (on a 1-based index). @@ -113,10 +119,12 @@ public JdbcFieldInfo(int jdbcType, int nullability, int precision, int scale) { */ public JdbcFieldInfo(ResultSetMetaData rsmd, int column) throws SQLException { Preconditions.checkNotNull(rsmd, "ResultSetMetaData cannot be null."); - Preconditions.checkArgument(column > 0, "ResultSetMetaData columns have indices starting at 1."); + Preconditions.checkArgument( + column > 0, "ResultSetMetaData columns have indices starting at 1."); Preconditions.checkArgument( column <= rsmd.getColumnCount(), - "The index must be within the number of columns (1 to %s, inclusive)", rsmd.getColumnCount()); + "The index must be within the number of columns (1 to %s, inclusive)", + rsmd.getColumnCount()); this.column = column; this.jdbcType = rsmd.getColumnType(column); @@ -128,8 +136,8 @@ public JdbcFieldInfo(ResultSetMetaData rsmd, int column) throws SQLException { } /** - * Builds a JdbcFieldInfo from the corresponding row from a {@link java.sql.DatabaseMetaData#getColumns} - * ResultSet. + * Builds a JdbcFieldInfo from the corresponding row from a {@link + * java.sql.DatabaseMetaData#getColumns} ResultSet. * * @param rs The {@link java.sql.ResultSet} to get the field information from. * @throws SQLException If the column information cannot be retrieved. @@ -144,51 +152,42 @@ public JdbcFieldInfo(ResultSet rs) throws SQLException { this.displaySize = rs.getInt("CHAR_OCTET_LENGTH"); } - /** - * The {@link java.sql.Types} type. - */ + /** The {@link java.sql.Types} type. */ public int getJdbcType() { return jdbcType; } - /** - * The nullability. - */ + /** The nullability. */ public int isNullable() { return nullability; } /** - * The numeric precision, for {@link java.sql.Types#NUMERIC} and {@link java.sql.Types#DECIMAL} types. + * The numeric precision, for {@link java.sql.Types#NUMERIC} and {@link java.sql.Types#DECIMAL} + * types. */ public int getPrecision() { return precision; } /** - * The numeric scale, for {@link java.sql.Types#NUMERIC} and {@link java.sql.Types#DECIMAL} types. + * The numeric scale, for {@link java.sql.Types#NUMERIC} and {@link java.sql.Types#DECIMAL} types. */ public int getScale() { return scale; } - /** - * The column index for query column. - */ + /** The column index for query column. */ public int getColumn() { return column; } - /** - * The type name as reported by the database. - */ + /** The type name as reported by the database. */ public String getTypeName() { return typeName; } - /** - * The max number of characters for the column. - */ + /** The max number of characters for the column. */ public int getDisplaySize() { return displaySize; } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcParameterBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcParameterBinder.java index 2dfc0658cb8..05159d04c86 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcParameterBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcParameterBinder.java @@ -21,7 +21,6 @@ import java.sql.SQLException; import java.util.HashMap; import java.util.Map; - import org.apache.arrow.adapter.jdbc.binder.ColumnBinder; import org.apache.arrow.util.Preconditions; import org.apache.arrow.vector.VectorSchemaRoot; @@ -29,8 +28,8 @@ /** * A binder binds JDBC prepared statement parameters to rows of Arrow data from a VectorSchemaRoot. * - * Each row of the VectorSchemaRoot will be bound to the configured parameters of the PreparedStatement. - * One row of data is bound at a time. + *

Each row of the VectorSchemaRoot will be bound to the configured parameters of the + * PreparedStatement. One row of data is bound at a time. */ public class JdbcParameterBinder { private final PreparedStatement statement; @@ -44,8 +43,10 @@ public class JdbcParameterBinder { * * @param statement The statement to bind parameters to. * @param root The VectorSchemaRoot to pull data from. - * @param binders Column binders to translate from Arrow data to JDBC parameters, one per parameter. - * @param parameterIndices For each binder in binders, the index of the parameter to bind to. + * @param binders Column binders to translate from Arrow data to JDBC parameters, one per + * parameter. + * @param parameterIndices For each binder in binders, the index of the parameter to bind + * to. */ private JdbcParameterBinder( final PreparedStatement statement, @@ -55,7 +56,8 @@ private JdbcParameterBinder( Preconditions.checkArgument( binders.length == parameterIndices.length, "Number of column binders (%s) must equal number of parameter indices (%s)", - binders.length, parameterIndices.length); + binders.length, + parameterIndices.length); this.statement = statement; this.root = root; this.binders = binders; @@ -66,9 +68,10 @@ private JdbcParameterBinder( /** * Initialize a binder with a builder. * - * @param statement The statement to bind to. The binder does not maintain ownership of the statement. - * @param root The {@link VectorSchemaRoot} to pull data from. The binder does not maintain ownership - * of the vector schema root. + * @param statement The statement to bind to. The binder does not maintain ownership of the + * statement. + * @param root The {@link VectorSchemaRoot} to pull data from. The binder does not maintain + * ownership of the vector schema root. */ public static Builder builder(final PreparedStatement statement, final VectorSchemaRoot root) { return new Builder(statement, root); @@ -82,8 +85,8 @@ public void reset() { /** * Bind the next row of data to the parameters of the statement. * - * After this, the application should call the desired method on the prepared statement, - * such as {@link PreparedStatement#executeUpdate()}, or {@link PreparedStatement#addBatch()}. + *

After this, the application should call the desired method on the prepared statement, such + * as {@link PreparedStatement#executeUpdate()}, or {@link PreparedStatement#addBatch()}. * * @return true if a row was bound, false if rows were exhausted */ @@ -99,9 +102,7 @@ public boolean next() throws SQLException { return true; } - /** - * A builder for a {@link JdbcParameterBinder}. - */ + /** A builder for a {@link JdbcParameterBinder}. */ public static class Builder { private final PreparedStatement statement; private final VectorSchemaRoot root; @@ -116,16 +117,14 @@ public static class Builder { /** Bind each column to the corresponding parameter in order. */ public Builder bindAll() { for (int i = 0; i < root.getFieldVectors().size(); i++) { - bind(/*parameterIndex=*/ i + 1, /*columnIndex=*/ i); + bind(/* parameterIndex= */ i + 1, /* columnIndex= */ i); } return this; } /** Bind the given parameter to the given column using the default binder. */ public Builder bind(int parameterIndex, int columnIndex) { - return bind( - parameterIndex, - ColumnBinder.forVector(root.getVector(columnIndex))); + return bind(parameterIndex, ColumnBinder.forVector(root.getVector(columnIndex))); } /** Bind the given parameter using the given binder. */ diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrow.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrow.java index 246451b5b22..89ebd420abc 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrow.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrow.java @@ -20,7 +20,6 @@ import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.util.Preconditions; @@ -29,44 +28,32 @@ * *

This utility uses following data mapping to map JDBC/SQL datatype to Arrow data types. * - *

CHAR --> ArrowType.Utf8 - * NCHAR --> ArrowType.Utf8 - * VARCHAR --> ArrowType.Utf8 - * NVARCHAR --> ArrowType.Utf8 - * LONGVARCHAR --> ArrowType.Utf8 - * LONGNVARCHAR --> ArrowType.Utf8 - * NUMERIC --> ArrowType.Decimal(precision, scale) - * DECIMAL --> ArrowType.Decimal(precision, scale) - * BIT --> ArrowType.Bool - * TINYINT --> ArrowType.Int(8, signed) - * SMALLINT --> ArrowType.Int(16, signed) - * INTEGER --> ArrowType.Int(32, signed) - * BIGINT --> ArrowType.Int(64, signed) - * REAL --> ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE) - * FLOAT --> ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE) - * DOUBLE --> ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE) - * BINARY --> ArrowType.Binary - * VARBINARY --> ArrowType.Binary - * LONGVARBINARY --> ArrowType.Binary - * DATE --> ArrowType.Date(DateUnit.MILLISECOND) - * TIME --> ArrowType.Time(TimeUnit.MILLISECOND, 32) - * TIMESTAMP --> ArrowType.Timestamp(TimeUnit.MILLISECOND, timezone=null) - * CLOB --> ArrowType.Utf8 - * BLOB --> ArrowType.Binary + *

CHAR --> ArrowType.Utf8 NCHAR --> ArrowType.Utf8 VARCHAR --> ArrowType.Utf8 NVARCHAR --> + * ArrowType.Utf8 LONGVARCHAR --> ArrowType.Utf8 LONGNVARCHAR --> ArrowType.Utf8 NUMERIC --> + * ArrowType.Decimal(precision, scale) DECIMAL --> ArrowType.Decimal(precision, scale) BIT --> + * ArrowType.Bool TINYINT --> ArrowType.Int(8, signed) SMALLINT --> ArrowType.Int(16, signed) + * INTEGER --> ArrowType.Int(32, signed) BIGINT --> ArrowType.Int(64, signed) REAL --> + * ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE) FLOAT --> + * ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE) DOUBLE --> + * ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE) BINARY --> ArrowType.Binary VARBINARY --> + * ArrowType.Binary LONGVARBINARY --> ArrowType.Binary DATE --> ArrowType.Date(DateUnit.MILLISECOND) + * TIME --> ArrowType.Time(TimeUnit.MILLISECOND, 32) TIMESTAMP --> + * ArrowType.Timestamp(TimeUnit.MILLISECOND, timezone=null) CLOB --> ArrowType.Utf8 BLOB --> + * ArrowType.Binary * * @since 0.10.0 */ public class JdbcToArrow { /*----------------------------------------------------------------* - | | - | Partial Convert API | - | | + | | + | Partial Convert API | + | | *----------------------------------------------------------------*/ /** - * For the given JDBC {@link ResultSet}, fetch the data from Relational DB and convert it to Arrow objects. - * Note here uses the default targetBatchSize = 1024. + * For the given JDBC {@link ResultSet}, fetch the data from Relational DB and convert it to Arrow + * objects. Note here uses the default targetBatchSize = 1024. * * @param resultSet ResultSet to use to fetch the data from underlying database * @param allocator Memory allocator @@ -74,28 +61,25 @@ public class JdbcToArrow { * @throws SQLException on error */ public static ArrowVectorIterator sqlToArrowVectorIterator( - ResultSet resultSet, - BufferAllocator allocator) - throws SQLException, IOException { + ResultSet resultSet, BufferAllocator allocator) throws SQLException, IOException { Preconditions.checkNotNull(allocator, "Memory Allocator object cannot be null"); - JdbcToArrowConfig config = - new JdbcToArrowConfig(allocator, JdbcToArrowUtils.getUtcCalendar()); + JdbcToArrowConfig config = new JdbcToArrowConfig(allocator, JdbcToArrowUtils.getUtcCalendar()); return sqlToArrowVectorIterator(resultSet, config); } /** - * For the given JDBC {@link ResultSet}, fetch the data from Relational DB and convert it to Arrow objects. - * Note if not specify {@link JdbcToArrowConfig#targetBatchSize}, will use default value 1024. + * For the given JDBC {@link ResultSet}, fetch the data from Relational DB and convert it to Arrow + * objects. Note if not specify {@link JdbcToArrowConfig#targetBatchSize}, will use default value + * 1024. + * * @param resultSet ResultSet to use to fetch the data from underlying database - * @param config Configuration of the conversion from JDBC to Arrow. + * @param config Configuration of the conversion from JDBC to Arrow. * @return Arrow Data Objects {@link ArrowVectorIterator} * @throws SQLException on error */ public static ArrowVectorIterator sqlToArrowVectorIterator( - ResultSet resultSet, - JdbcToArrowConfig config) - throws SQLException, IOException { + ResultSet resultSet, JdbcToArrowConfig config) throws SQLException, IOException { Preconditions.checkNotNull(resultSet, "JDBC ResultSet object cannot be null"); Preconditions.checkNotNull(config, "The configuration cannot be null"); return ArrowVectorIterator.create(resultSet, config); diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowConfig.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowConfig.java index 68851f4a98b..6470dc58a6f 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowConfig.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowConfig.java @@ -21,7 +21,6 @@ import java.util.Calendar; import java.util.Map; import java.util.function.Function; - import org.apache.arrow.adapter.jdbc.consumer.JdbcConsumer; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.util.Preconditions; @@ -30,25 +29,23 @@ /** * This class configures the JDBC-to-Arrow conversion process. - *

- * The allocator is used to construct the {@link org.apache.arrow.vector.VectorSchemaRoot}, - * and the calendar is used to define the time zone of any - * {@link org.apache.arrow.vector.types.pojo.ArrowType.Timestamp} - * fields that are created during the conversion. Neither field may be null. - *

- *

- * If the includeMetadata flag is set, the Arrow field metadata will contain information - * from the corresponding {@link java.sql.ResultSetMetaData} that was used to create the - * {@link org.apache.arrow.vector.types.pojo.FieldType} of the corresponding - * {@link org.apache.arrow.vector.FieldVector}. - *

- *

- * If there are any {@link java.sql.Types#ARRAY} fields in the {@link java.sql.ResultSet}, the corresponding - * {@link JdbcFieldInfo} for the array's contents must be defined here. Unfortunately, the sub-type - * information cannot be retrieved from all JDBC implementations (H2 for example, returns - * {@link java.sql.Types#NULL} for the array sub-type), so it must be configured here. The column index - * or name can be used to map to a {@link JdbcFieldInfo}, and that will be used for the conversion. - *

+ * + *

The allocator is used to construct the {@link org.apache.arrow.vector.VectorSchemaRoot}, and + * the calendar is used to define the time zone of any {@link + * org.apache.arrow.vector.types.pojo.ArrowType.Timestamp} fields that are created during the + * conversion. Neither field may be null. + * + *

If the includeMetadata flag is set, the Arrow field metadata will contain + * information from the corresponding {@link java.sql.ResultSetMetaData} that was used to create the + * {@link org.apache.arrow.vector.types.pojo.FieldType} of the corresponding {@link + * org.apache.arrow.vector.FieldVector}. + * + *

If there are any {@link java.sql.Types#ARRAY} fields in the {@link java.sql.ResultSet}, the + * corresponding {@link JdbcFieldInfo} for the array's contents must be defined here. Unfortunately, + * the sub-type information cannot be retrieved from all JDBC implementations (H2 for example, + * returns {@link java.sql.Types#NULL} for the array sub-type), so it must be configured here. The + * column index or name can be used to map to a {@link JdbcFieldInfo}, and that will be used for the + * conversion. */ public final class JdbcToArrowConfig { @@ -65,15 +62,14 @@ public final class JdbcToArrowConfig { private final Map schemaMetadata; private final Map> columnMetadataByColumnIndex; private final RoundingMode bigDecimalRoundingMode; + /** - * The maximum rowCount to read each time when partially convert data. - * Default value is 1024 and -1 means disable partial read. - * default is -1 which means disable partial read. - * Note that this flag only useful for {@link JdbcToArrow#sqlToArrowVectorIterator} - * 1) if targetBatchSize != -1, it will convert full data into multiple vectors - * with valueCount no more than targetBatchSize. - * 2) if targetBatchSize == -1, it will convert full data into a single vector in {@link ArrowVectorIterator} - *

+ * The maximum rowCount to read each time when partially convert data. Default value is 1024 and + * -1 means disable partial read. default is -1 which means disable partial read. Note that this + * flag only useful for {@link JdbcToArrow#sqlToArrowVectorIterator} 1) if targetBatchSize != -1, + * it will convert full data into multiple vectors with valueCount no more than targetBatchSize. + * 2) if targetBatchSize == -1, it will convert full data into a single vector in {@link + * ArrowVectorIterator} */ private final int targetBatchSize; @@ -81,81 +77,100 @@ public final class JdbcToArrowConfig { private final JdbcConsumerFactory jdbcConsumerGetter; /** - * Constructs a new configuration from the provided allocator and calendar. The allocator - * is used when constructing the Arrow vectors from the ResultSet, and the calendar is used to define - * Arrow Timestamp fields, and to read time-based fields from the JDBC ResultSet. + * Constructs a new configuration from the provided allocator and calendar. The allocator + * is used when constructing the Arrow vectors from the ResultSet, and the calendar is + * used to define Arrow Timestamp fields, and to read time-based fields from the JDBC + * ResultSet. * - * @param allocator The memory allocator to construct the Arrow vectors with. - * @param calendar The calendar to use when constructing Timestamp fields and reading time-based results. + * @param allocator The memory allocator to construct the Arrow vectors with. + * @param calendar The calendar to use when constructing Timestamp fields and reading time-based + * results. */ JdbcToArrowConfig(BufferAllocator allocator, Calendar calendar) { - this(allocator, calendar, + this( + allocator, + calendar, /* include metadata */ false, /* reuse vector schema root */ false, /* array sub-types by column index */ null, /* array sub-types by column name */ null, - DEFAULT_TARGET_BATCH_SIZE, null, null); + DEFAULT_TARGET_BATCH_SIZE, + null, + null); } JdbcToArrowConfig( - BufferAllocator allocator, - Calendar calendar, - boolean includeMetadata, - boolean reuseVectorSchemaRoot, - Map arraySubTypesByColumnIndex, - Map arraySubTypesByColumnName, - int targetBatchSize, - Function jdbcToArrowTypeConverter) { - this(allocator, calendar, includeMetadata, reuseVectorSchemaRoot, arraySubTypesByColumnIndex, - arraySubTypesByColumnName, targetBatchSize, jdbcToArrowTypeConverter, null); + BufferAllocator allocator, + Calendar calendar, + boolean includeMetadata, + boolean reuseVectorSchemaRoot, + Map arraySubTypesByColumnIndex, + Map arraySubTypesByColumnName, + int targetBatchSize, + Function jdbcToArrowTypeConverter) { + this( + allocator, + calendar, + includeMetadata, + reuseVectorSchemaRoot, + arraySubTypesByColumnIndex, + arraySubTypesByColumnName, + targetBatchSize, + jdbcToArrowTypeConverter, + null); } /** - * Constructs a new configuration from the provided allocator and calendar. The allocator - * is used when constructing the Arrow vectors from the ResultSet, and the calendar is used to define - * Arrow Timestamp fields, and to read time-based fields from the JDBC ResultSet. + * Constructs a new configuration from the provided allocator and calendar. The allocator + * is used when constructing the Arrow vectors from the ResultSet, and the calendar is + * used to define Arrow Timestamp fields, and to read time-based fields from the JDBC + * ResultSet. * - * @param allocator The memory allocator to construct the Arrow vectors with. - * @param calendar The calendar to use when constructing Timestamp fields and reading time-based results. - * @param includeMetadata Whether to include JDBC field metadata in the Arrow Schema Field metadata. + * @param allocator The memory allocator to construct the Arrow vectors with. + * @param calendar The calendar to use when constructing Timestamp fields and reading time-based + * results. + * @param includeMetadata Whether to include JDBC field metadata in the Arrow Schema Field + * metadata. * @param reuseVectorSchemaRoot Whether to reuse the vector schema root for each data load. * @param arraySubTypesByColumnIndex The type of the JDBC array at the column index (1-based). - * @param arraySubTypesByColumnName The type of the JDBC array at the column name. - * @param targetBatchSize The target batch size to be used in preallocation of the resulting vectors. - * @param jdbcToArrowTypeConverter The function that maps JDBC field type information to arrow type. If set to null, - * the default mapping will be used, which is defined as: - *
    - *
  • CHAR --> ArrowType.Utf8
  • - *
  • NCHAR --> ArrowType.Utf8
  • - *
  • VARCHAR --> ArrowType.Utf8
  • - *
  • NVARCHAR --> ArrowType.Utf8
  • - *
  • LONGVARCHAR --> ArrowType.Utf8
  • - *
  • LONGNVARCHAR --> ArrowType.Utf8
  • - *
  • NUMERIC --> ArrowType.Decimal(precision, scale)
  • - *
  • DECIMAL --> ArrowType.Decimal(precision, scale)
  • - *
  • BIT --> ArrowType.Bool
  • - *
  • TINYINT --> ArrowType.Int(8, signed)
  • - *
  • SMALLINT --> ArrowType.Int(16, signed)
  • - *
  • INTEGER --> ArrowType.Int(32, signed)
  • - *
  • BIGINT --> ArrowType.Int(64, signed)
  • - *
  • REAL --> ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)
  • - *
  • FLOAT --> ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)
  • - *
  • DOUBLE --> ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)
  • - *
  • BINARY --> ArrowType.Binary
  • - *
  • VARBINARY --> ArrowType.Binary
  • - *
  • LONGVARBINARY --> ArrowType.Binary
  • - *
  • DATE --> ArrowType.Date(DateUnit.DAY)
  • - *
  • TIME --> ArrowType.Time(TimeUnit.MILLISECOND, 32)
  • - *
  • TIMESTAMP --> ArrowType.Timestamp(TimeUnit.MILLISECOND, calendar timezone)
  • - *
  • CLOB --> ArrowType.Utf8
  • - *
  • BLOB --> ArrowType.Binary
  • - *
  • ARRAY --> ArrowType.List
  • - *
  • STRUCT --> ArrowType.Struct
  • - *
  • NULL --> ArrowType.Null
  • - *
- * @param bigDecimalRoundingMode The java.math.RoundingMode to be used in coercion of a BigDecimal from a - * ResultSet having a scale which does not match that of the target vector. Use null - * (default value) to require strict scale matching. + * @param arraySubTypesByColumnName The type of the JDBC array at the column name. + * @param targetBatchSize The target batch size to be used in preallocation of the resulting + * vectors. + * @param jdbcToArrowTypeConverter The function that maps JDBC field type information to arrow + * type. If set to null, the default mapping will be used, which is defined as: + *
    + *
  • CHAR --> ArrowType.Utf8 + *
  • NCHAR --> ArrowType.Utf8 + *
  • VARCHAR --> ArrowType.Utf8 + *
  • NVARCHAR --> ArrowType.Utf8 + *
  • LONGVARCHAR --> ArrowType.Utf8 + *
  • LONGNVARCHAR --> ArrowType.Utf8 + *
  • NUMERIC --> ArrowType.Decimal(precision, scale) + *
  • DECIMAL --> ArrowType.Decimal(precision, scale) + *
  • BIT --> ArrowType.Bool + *
  • TINYINT --> ArrowType.Int(8, signed) + *
  • SMALLINT --> ArrowType.Int(16, signed) + *
  • INTEGER --> ArrowType.Int(32, signed) + *
  • BIGINT --> ArrowType.Int(64, signed) + *
  • REAL --> ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE) + *
  • FLOAT --> ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE) + *
  • DOUBLE --> ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE) + *
  • BINARY --> ArrowType.Binary + *
  • VARBINARY --> ArrowType.Binary + *
  • LONGVARBINARY --> ArrowType.Binary + *
  • DATE --> ArrowType.Date(DateUnit.DAY) + *
  • TIME --> ArrowType.Time(TimeUnit.MILLISECOND, 32) + *
  • TIMESTAMP --> ArrowType.Timestamp(TimeUnit.MILLISECOND, calendar timezone) + *
  • CLOB --> ArrowType.Utf8 + *
  • BLOB --> ArrowType.Binary + *
  • ARRAY --> ArrowType.List + *
  • STRUCT --> ArrowType.Struct + *
  • NULL --> ArrowType.Null + *
+ * + * @param bigDecimalRoundingMode The java.math.RoundingMode to be used in coercion of a BigDecimal + * from a ResultSet having a scale which does not match that of the target vector. Use null + * (default value) to require strict scale matching. */ JdbcToArrowConfig( BufferAllocator allocator, @@ -245,16 +260,19 @@ public final class JdbcToArrowConfig { this.bigDecimalRoundingMode = bigDecimalRoundingMode; // set up type converter - this.jdbcToArrowTypeConverter = jdbcToArrowTypeConverter != null ? jdbcToArrowTypeConverter : - (jdbcFieldInfo) -> JdbcToArrowUtils.getArrowTypeFromJdbcType(jdbcFieldInfo, calendar); + this.jdbcToArrowTypeConverter = + jdbcToArrowTypeConverter != null + ? jdbcToArrowTypeConverter + : (jdbcFieldInfo) -> JdbcToArrowUtils.getArrowTypeFromJdbcType(jdbcFieldInfo, calendar); - this.jdbcConsumerGetter = jdbcConsumerGetter != null ? jdbcConsumerGetter : JdbcToArrowUtils::getConsumer; + this.jdbcConsumerGetter = + jdbcConsumerGetter != null ? jdbcConsumerGetter : JdbcToArrowUtils::getConsumer; } /** - * The calendar to use when defining Arrow Timestamp fields - * and retrieving {@link java.sql.Date}, {@link java.sql.Time}, or {@link java.sql.Timestamp} - * data types from the {@link java.sql.ResultSet}, or null if not converting. + * The calendar to use when defining Arrow Timestamp fields and retrieving {@link java.sql.Date}, + * {@link java.sql.Time}, or {@link java.sql.Timestamp} data types from the {@link + * java.sql.ResultSet}, or null if not converting. * * @return the calendar. */ @@ -280,30 +298,22 @@ public boolean shouldIncludeMetadata() { return includeMetadata; } - /** - * Get the target batch size for partial read. - */ + /** Get the target batch size for partial read. */ public int getTargetBatchSize() { return targetBatchSize; } - /** - * Get whether it is allowed to reuse the vector schema root. - */ + /** Get whether it is allowed to reuse the vector schema root. */ public boolean isReuseVectorSchemaRoot() { return reuseVectorSchemaRoot; } - /** - * Gets the mapping between JDBC type information to Arrow type. - */ + /** Gets the mapping between JDBC type information to Arrow type. */ public Function getJdbcToArrowTypeConverter() { return jdbcToArrowTypeConverter; } - /** - * Gets the JDBC consumer getter. - */ + /** Gets the JDBC consumer getter. */ public JdbcConsumerFactory getJdbcConsumerGetter() { return jdbcConsumerGetter; } @@ -311,8 +321,10 @@ public JdbcConsumerFactory getJdbcConsumerGetter() { /** * Returns the array sub-type {@link JdbcFieldInfo} defined for the provided column index. * - * @param index The {@link java.sql.ResultSetMetaData} column index of an {@link java.sql.Types#ARRAY} type. - * @return The {@link JdbcFieldInfo} for that array's sub-type, or null if not defined. + * @param index The {@link java.sql.ResultSetMetaData} column index of an {@link + * java.sql.Types#ARRAY} type. + * @return The {@link JdbcFieldInfo} for that array's sub-type, or null if not + * defined. */ public JdbcFieldInfo getArraySubTypeByColumnIndex(int index) { if (arraySubTypesByColumnIndex == null) { @@ -325,8 +337,10 @@ public JdbcFieldInfo getArraySubTypeByColumnIndex(int index) { /** * Returns the array sub-type {@link JdbcFieldInfo} defined for the provided column name. * - * @param name The {@link java.sql.ResultSetMetaData} column name of an {@link java.sql.Types#ARRAY} type. - * @return The {@link JdbcFieldInfo} for that array's sub-type, or null if not defined. + * @param name The {@link java.sql.ResultSetMetaData} column name of an {@link + * java.sql.Types#ARRAY} type. + * @return The {@link JdbcFieldInfo} for that array's sub-type, or null if not + * defined. */ public JdbcFieldInfo getArraySubTypeByColumnName(String name) { if (arraySubTypesByColumnName == null) { @@ -339,7 +353,8 @@ public JdbcFieldInfo getArraySubTypeByColumnName(String name) { /** * Returns the type {@link JdbcFieldInfo} explicitly defined for the provided column index. * - * @param index The {@link java.sql.ResultSetMetaData} column index to evaluate for explicit type mapping. + * @param index The {@link java.sql.ResultSetMetaData} column index to evaluate for explicit type + * mapping. * @return The {@link JdbcFieldInfo} defined for the column, or null if not defined. */ public JdbcFieldInfo getExplicitTypeByColumnIndex(int index) { @@ -353,7 +368,8 @@ public JdbcFieldInfo getExplicitTypeByColumnIndex(int index) { /** * Returns the type {@link JdbcFieldInfo} explicitly defined for the provided column name. * - * @param name The {@link java.sql.ResultSetMetaData} column name to evaluate for explicit type mapping. + * @param name The {@link java.sql.ResultSetMetaData} column name to evaluate for explicit type + * mapping. * @return The {@link JdbcFieldInfo} defined for the column, or null if not defined. */ public JdbcFieldInfo getExplicitTypeByColumnName(String name) { @@ -364,17 +380,12 @@ public JdbcFieldInfo getExplicitTypeByColumnName(String name) { } } - /** - * Return schema level metadata or null if not provided. - */ + /** Return schema level metadata or null if not provided. */ public Map getSchemaMetadata() { return schemaMetadata; } - /** - * Return metadata from columnIndex->meta map on per field basis - * or null if not provided. - */ + /** Return metadata from columnIndex->meta map on per field basis or null if not provided. */ public Map> getColumnMetadataByColumnIndex() { return columnMetadataByColumnIndex; } @@ -383,12 +394,14 @@ public RoundingMode getBigDecimalRoundingMode() { return bigDecimalRoundingMode; } - /** - * Interface for a function that gets a JDBC consumer for the given values. - */ + /** Interface for a function that gets a JDBC consumer for the given values. */ @FunctionalInterface public interface JdbcConsumerFactory { - JdbcConsumer apply(ArrowType arrowType, int columnIndex, boolean nullable, FieldVector vector, - JdbcToArrowConfig config); + JdbcConsumer apply( + ArrowType arrowType, + int columnIndex, + boolean nullable, + FieldVector vector, + JdbcToArrowConfig config); } } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowConfigBuilder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowConfigBuilder.java index 7d88c238320..cef26b27b7a 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowConfigBuilder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowConfigBuilder.java @@ -23,15 +23,12 @@ import java.util.Calendar; import java.util.Map; import java.util.function.Function; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.util.Preconditions; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.types.pojo.ArrowType; -/** - * This class builds {@link JdbcToArrowConfig}s. - */ +/** This class builds {@link JdbcToArrowConfig}s. */ public class JdbcToArrowConfigBuilder { private Calendar calendar; private BufferAllocator allocator; @@ -49,9 +46,9 @@ public class JdbcToArrowConfigBuilder { private RoundingMode bigDecimalRoundingMode; /** - * Default constructor for the JdbcToArrowConfigBuilder}. - * Use the setter methods for the allocator and calendar; the allocator must be - * set. Otherwise, {@link #build()} will throw a {@link NullPointerException}. + * Default constructor for the JdbcToArrowConfigBuilder}. Use the setter methods for + * the allocator and calendar; the allocator must be set. Otherwise, {@link #build()} will throw a + * {@link NullPointerException}. */ public JdbcToArrowConfigBuilder() { this.allocator = null; @@ -68,16 +65,13 @@ public JdbcToArrowConfigBuilder() { } /** - * Constructor for the JdbcToArrowConfigBuilder. The - * allocator is required, and a {@link NullPointerException} - * will be thrown if it is null. - *

- * The allocator is used to construct Arrow vectors from the JDBC ResultSet. - * The calendar is used to determine the time zone of {@link java.sql.Timestamp} - * fields and convert {@link java.sql.Date}, {@link java.sql.Time}, and - * {@link java.sql.Timestamp} fields to a single, common time zone when reading - * from the result set. - *

+ * Constructor for the JdbcToArrowConfigBuilder. The allocator is required, and a + * {@link NullPointerException} will be thrown if it is null. + * + *

The allocator is used to construct Arrow vectors from the JDBC ResultSet. The calendar is + * used to determine the time zone of {@link java.sql.Timestamp} fields and convert {@link + * java.sql.Date}, {@link java.sql.Time}, and {@link java.sql.Timestamp} fields to a single, + * common time zone when reading from the result set. * * @param allocator The Arrow Vector memory allocator. * @param calendar The calendar to use when constructing timestamp fields. @@ -95,26 +89,23 @@ public JdbcToArrowConfigBuilder(BufferAllocator allocator, Calendar calendar) { } /** - * Constructor for the JdbcToArrowConfigBuilder. Both the - * allocator and calendar are required. A {@link NullPointerException} - * will be thrown if either of those arguments is null. - *

- * The allocator is used to construct Arrow vectors from the JDBC ResultSet. - * The calendar is used to determine the time zone of {@link java.sql.Timestamp} - * fields and convert {@link java.sql.Date}, {@link java.sql.Time}, and - * {@link java.sql.Timestamp} fields to a single, common time zone when reading - * from the result set. - *

- *

- * The includeMetadata argument, if true will cause - * various information about each database field to be added to the Vector - * Schema's field metadata. - *

+ * Constructor for the JdbcToArrowConfigBuilder. Both the allocator and calendar are + * required. A {@link NullPointerException} will be thrown if either of those arguments is + * null. + * + *

The allocator is used to construct Arrow vectors from the JDBC ResultSet. The calendar is + * used to determine the time zone of {@link java.sql.Timestamp} fields and convert {@link + * java.sql.Date}, {@link java.sql.Time}, and {@link java.sql.Timestamp} fields to a single, + * common time zone when reading from the result set. + * + *

The includeMetadata argument, if true will cause various + * information about each database field to be added to the Vector Schema's field metadata. * * @param allocator The Arrow Vector memory allocator. * @param calendar The calendar to use when constructing timestamp fields. */ - public JdbcToArrowConfigBuilder(BufferAllocator allocator, Calendar calendar, boolean includeMetadata) { + public JdbcToArrowConfigBuilder( + BufferAllocator allocator, Calendar calendar, boolean includeMetadata) { this(allocator, calendar); this.includeMetadata = includeMetadata; } @@ -132,8 +123,8 @@ public JdbcToArrowConfigBuilder setAllocator(BufferAllocator allocator) { } /** - * Sets the {@link Calendar} to use when constructing timestamp fields in the - * Arrow schema, and reading time-based fields from the JDBC ResultSet. + * Sets the {@link Calendar} to use when constructing timestamp fields in the Arrow schema, and + * reading time-based fields from the JDBC ResultSet. * * @param calendar the calendar to set. */ @@ -145,7 +136,8 @@ public JdbcToArrowConfigBuilder setCalendar(Calendar calendar) { /** * Sets whether to include JDBC ResultSet field metadata in the Arrow Schema field metadata. * - * @param includeMetadata Whether to include or exclude JDBC metadata in the Arrow Schema field metadata. + * @param includeMetadata Whether to include or exclude JDBC metadata in the Arrow Schema field + * metadata. * @return This instance of the JdbcToArrowConfig, for chaining. */ public JdbcToArrowConfigBuilder setIncludeMetadata(boolean includeMetadata) { @@ -154,8 +146,8 @@ public JdbcToArrowConfigBuilder setIncludeMetadata(boolean includeMetadata) { } /** - * Sets the mapping of column-index-to-{@link JdbcFieldInfo} used for columns of type {@link java.sql.Types#ARRAY}. - * The column index is 1-based, to match the JDBC column index. + * Sets the mapping of column-index-to-{@link JdbcFieldInfo} used for columns of type {@link + * java.sql.Types#ARRAY}. The column index is 1-based, to match the JDBC column index. * * @param map The mapping. * @return This instance of the JdbcToArrowConfig, for chaining. @@ -166,7 +158,8 @@ public JdbcToArrowConfigBuilder setArraySubTypeByColumnIndexMap(MapJdbcToArrowConfig, for chaining. @@ -178,11 +171,12 @@ public JdbcToArrowConfigBuilder setArraySubTypeByColumnNameMap(Map - * This can be useful to override type information from JDBC drivers that provide incomplete type info, - * e.g. DECIMAL with precision = scale = 0. - *

- * The column index is 1-based, to match the JDBC column index. + * + *

This can be useful to override type information from JDBC drivers that provide incomplete + * type info, e.g. DECIMAL with precision = scale = 0. + * + *

The column index is 1-based, to match the JDBC column index. + * * @param map The mapping. */ public JdbcToArrowConfigBuilder setExplicitTypesByColumnIndex(Map map) { @@ -192,9 +186,10 @@ public JdbcToArrowConfigBuilder setExplicitTypesByColumnIndex(Map - * This can be useful to override type information from JDBC drivers that provide incomplete type info, - * e.g. DECIMAL with precision = scale = 0. + * + *

This can be useful to override type information from JDBC drivers that provide incomplete + * type info, e.g. DECIMAL with precision = scale = 0. + * * @param map The mapping. */ public JdbcToArrowConfigBuilder setExplicitTypesByColumnName(Map map) { @@ -204,8 +199,8 @@ public JdbcToArrowConfigBuilder setExplicitTypesByColumnName(Map - * Use {@link JdbcToArrowConfig#NO_LIMIT_BATCH_SIZE} to read all rows at once. + * + *

Use {@link JdbcToArrowConfig#NO_LIMIT_BATCH_SIZE} to read all rows at once. */ public JdbcToArrowConfigBuilder setTargetBatchSize(int targetBatchSize) { this.targetBatchSize = targetBatchSize; @@ -214,8 +209,9 @@ public JdbcToArrowConfigBuilder setTargetBatchSize(int targetBatchSize) { /** * Set the function used to convert JDBC types to Arrow types. - *

- * Defaults to wrapping {@link JdbcToArrowUtils#getArrowTypeFromJdbcType(JdbcFieldInfo, Calendar)}. + * + *

Defaults to wrapping {@link JdbcToArrowUtils#getArrowTypeFromJdbcType(JdbcFieldInfo, + * Calendar)}. */ public JdbcToArrowConfigBuilder setJdbcToArrowTypeConverter( Function jdbcToArrowTypeConverter) { @@ -225,9 +221,9 @@ public JdbcToArrowConfigBuilder setJdbcToArrowTypeConverter( /** * Set the function used to get a JDBC consumer for a given type. - *

- * Defaults to wrapping {@link - * JdbcToArrowUtils#getConsumer(ArrowType, Integer, Boolean, FieldVector, JdbcToArrowConfig)}. + * + *

Defaults to wrapping {@link JdbcToArrowUtils#getConsumer(ArrowType, Integer, Boolean, + * FieldVector, JdbcToArrowConfig)}. */ public JdbcToArrowConfigBuilder setJdbcConsumerGetter( JdbcToArrowConfig.JdbcConsumerFactory jdbcConsumerGetter) { @@ -236,35 +232,32 @@ public JdbcToArrowConfigBuilder setJdbcConsumerGetter( } /** - * Set whether to use the same {@link org.apache.arrow.vector.VectorSchemaRoot} instance on each iteration, - * or to allocate a new one. + * Set whether to use the same {@link org.apache.arrow.vector.VectorSchemaRoot} instance on each + * iteration, or to allocate a new one. */ public JdbcToArrowConfigBuilder setReuseVectorSchemaRoot(boolean reuseVectorSchemaRoot) { this.reuseVectorSchemaRoot = reuseVectorSchemaRoot; return this; } - /** - * Set metadata for schema. - */ + /** Set metadata for schema. */ public JdbcToArrowConfigBuilder setSchemaMetadata(Map schemaMetadata) { this.schemaMetadata = schemaMetadata; return this; } - /** - * Set metadata from columnIndex->meta map on per field basis. - */ + /** Set metadata from columnIndex->meta map on per field basis. */ public JdbcToArrowConfigBuilder setColumnMetadataByColumnIndex( - Map> columnMetadataByColumnIndex) { + Map> columnMetadataByColumnIndex) { this.columnMetadataByColumnIndex = columnMetadataByColumnIndex; return this; } /** - * Set the rounding mode used when the scale of the actual value does not match the declared scale. - *

- * By default, an error is raised in such cases. + * Set the rounding mode used when the scale of the actual value does not match the declared + * scale. + * + *

By default, an error is raised in such cases. */ public JdbcToArrowConfigBuilder setBigDecimalRoundingMode(RoundingMode bigDecimalRoundingMode) { this.bigDecimalRoundingMode = bigDecimalRoundingMode; @@ -272,8 +265,8 @@ public JdbcToArrowConfigBuilder setBigDecimalRoundingMode(RoundingMode bigDecima } /** - * This builds the {@link JdbcToArrowConfig} from the provided - * {@link BufferAllocator} and {@link Calendar}. + * This builds the {@link JdbcToArrowConfig} from the provided {@link BufferAllocator} and {@link + * Calendar}. * * @return The built {@link JdbcToArrowConfig} * @throws NullPointerException if either the allocator or calendar was not set. diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java index eaee4993607..959083136a7 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java @@ -38,7 +38,6 @@ import java.util.Locale; import java.util.Map; import java.util.TimeZone; - import org.apache.arrow.adapter.jdbc.consumer.ArrayConsumer; import org.apache.arrow.adapter.jdbc.consumer.BigIntConsumer; import org.apache.arrow.adapter.jdbc.consumer.BinaryConsumer; @@ -91,7 +90,8 @@ import org.apache.arrow.vector.util.ValueVectorUtility; /** - * Class that does most of the work to convert JDBC ResultSet data into Arrow columnar format Vector objects. + * Class that does most of the work to convert JDBC ResultSet data into Arrow columnar format Vector + * objects. * * @since 0.10.0 */ @@ -99,9 +99,7 @@ public class JdbcToArrowUtils { private static final int JDBC_ARRAY_VALUE_COLUMN = 2; - /** - * Returns the instance of a {java.util.Calendar} with the UTC time zone and root locale. - */ + /** Returns the instance of a {java.util.Calendar} with the UTC time zone and root locale. */ public static Calendar getUtcCalendar() { return Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT); } @@ -114,7 +112,8 @@ public static Calendar getUtcCalendar() { * @return {@link Schema} * @throws SQLException on error */ - public static Schema jdbcToArrowSchema(ResultSetMetaData rsmd, Calendar calendar) throws SQLException { + public static Schema jdbcToArrowSchema(ResultSetMetaData rsmd, Calendar calendar) + throws SQLException { Preconditions.checkNotNull(calendar, "Calendar object can't be null"); return jdbcToArrowSchema(rsmd, new JdbcToArrowConfig(new RootAllocator(0), calendar)); @@ -123,25 +122,28 @@ public static Schema jdbcToArrowSchema(ResultSetMetaData rsmd, Calendar calendar /** * Create Arrow {@link Schema} object for the given JDBC {@link ResultSetMetaData}. * - * @param parameterMetaData The ResultSetMetaData containing the results, to read the JDBC metadata from. - * @param calendar The calendar to use the time zone field of, to construct Timestamp fields from. + * @param parameterMetaData The ResultSetMetaData containing the results, to read the JDBC + * metadata from. + * @param calendar The calendar to use the time zone field of, to construct Timestamp fields from. * @return {@link Schema} * @throws SQLException on error */ - public static Schema jdbcToArrowSchema(final ParameterMetaData parameterMetaData, final Calendar calendar) - throws SQLException { + public static Schema jdbcToArrowSchema( + final ParameterMetaData parameterMetaData, final Calendar calendar) throws SQLException { Preconditions.checkNotNull(calendar, "Calendar object can't be null"); Preconditions.checkNotNull(parameterMetaData); final List parameterFields = new ArrayList<>(parameterMetaData.getParameterCount()); - for (int parameterCounter = 1; parameterCounter <= parameterMetaData.getParameterCount(); - parameterCounter++) { + for (int parameterCounter = 1; + parameterCounter <= parameterMetaData.getParameterCount(); + parameterCounter++) { final int jdbcDataType = parameterMetaData.getParameterType(parameterCounter); final int jdbcIsNullable = parameterMetaData.isNullable(parameterCounter); final boolean arrowIsNullable = jdbcIsNullable != ParameterMetaData.parameterNoNulls; final int precision = parameterMetaData.getPrecision(parameterCounter); final int scale = parameterMetaData.getScale(parameterCounter); - final ArrowType arrowType = getArrowTypeFromJdbcType(new JdbcFieldInfo(jdbcDataType, precision, scale), calendar); - final FieldType fieldType = new FieldType(arrowIsNullable, arrowType, /*dictionary=*/null); + final ArrowType arrowType = + getArrowTypeFromJdbcType(new JdbcFieldInfo(jdbcDataType, precision, scale), calendar); + final FieldType fieldType = new FieldType(arrowIsNullable, arrowType, /* dictionary= */ null); parameterFields.add(new Field(null, fieldType, null)); } @@ -152,10 +154,11 @@ public static Schema jdbcToArrowSchema(final ParameterMetaData parameterMetaData * Converts the provided JDBC type to its respective {@link ArrowType} counterpart. * * @param fieldInfo the {@link JdbcFieldInfo} with information about the original JDBC type. - * @param calendar the {@link Calendar} to use for datetime data types. + * @param calendar the {@link Calendar} to use for datetime data types. * @return a new {@link ArrowType}. */ - public static ArrowType getArrowTypeFromJdbcType(final JdbcFieldInfo fieldInfo, final Calendar calendar) { + public static ArrowType getArrowTypeFromJdbcType( + final JdbcFieldInfo fieldInfo, final Calendar calendar) { switch (fieldInfo.getJdbcType()) { case Types.BOOLEAN: case Types.BIT: @@ -222,30 +225,34 @@ public static ArrowType getArrowTypeFromJdbcType(final JdbcFieldInfo fieldInfo, /** * Create Arrow {@link Schema} object for the given JDBC {@link java.sql.ResultSetMetaData}. * - *

- * If {@link JdbcToArrowConfig#shouldIncludeMetadata()} returns true, the following fields - * will be added to the {@link FieldType#getMetadata()}: + *

If {@link JdbcToArrowConfig#shouldIncludeMetadata()} returns true, the + * following fields will be added to the {@link FieldType#getMetadata()}: + * *

    - *
  • {@link Constants#SQL_CATALOG_NAME_KEY} representing {@link ResultSetMetaData#getCatalogName(int)}
  • - *
  • {@link Constants#SQL_TABLE_NAME_KEY} representing {@link ResultSetMetaData#getTableName(int)}
  • - *
  • {@link Constants#SQL_COLUMN_NAME_KEY} representing {@link ResultSetMetaData#getColumnLabel(int)}
  • - *
  • {@link Constants#SQL_TYPE_KEY} representing {@link ResultSetMetaData#getColumnTypeName(int)}
  • + *
  • {@link Constants#SQL_CATALOG_NAME_KEY} representing {@link + * ResultSetMetaData#getCatalogName(int)} + *
  • {@link Constants#SQL_TABLE_NAME_KEY} representing {@link + * ResultSetMetaData#getTableName(int)} + *
  • {@link Constants#SQL_COLUMN_NAME_KEY} representing {@link + * ResultSetMetaData#getColumnLabel(int)} + *
  • {@link Constants#SQL_TYPE_KEY} representing {@link + * ResultSetMetaData#getColumnTypeName(int)} *
- *

- *

- * If any columns are of type {@link java.sql.Types#ARRAY}, the configuration object will be used to look up - * the array sub-type field. The {@link JdbcToArrowConfig#getArraySubTypeByColumnIndex(int)} method will be - * checked first, followed by the {@link JdbcToArrowConfig#getArraySubTypeByColumnName(String)} method. - *

+ * + *

If any columns are of type {@link java.sql.Types#ARRAY}, the configuration object will be + * used to look up the array sub-type field. The {@link + * JdbcToArrowConfig#getArraySubTypeByColumnIndex(int)} method will be checked first, followed by + * the {@link JdbcToArrowConfig#getArraySubTypeByColumnName(String)} method. * * @param rsmd The ResultSetMetaData containing the results, to read the JDBC metadata from. * @param config The configuration to use when constructing the schema. * @return {@link Schema} * @throws SQLException on error - * @throws IllegalArgumentException if rsmd contains an {@link java.sql.Types#ARRAY} but the - * config does not have a sub-type definition for it. + * @throws IllegalArgumentException if rsmd contains an {@link java.sql.Types#ARRAY} + * but the config does not have a sub-type definition for it. */ - public static Schema jdbcToArrowSchema(ResultSetMetaData rsmd, JdbcToArrowConfig config) throws SQLException { + public static Schema jdbcToArrowSchema(ResultSetMetaData rsmd, JdbcToArrowConfig config) + throws SQLException { Preconditions.checkNotNull(rsmd, "JDBC ResultSetMetaData object can't be null"); Preconditions.checkNotNull(config, "The configuration object must not be null"); @@ -254,8 +261,10 @@ public static Schema jdbcToArrowSchema(ResultSetMetaData rsmd, JdbcToArrowConfig for (int i = 1; i <= columnCount; i++) { final String columnName = rsmd.getColumnLabel(i); - final Map columnMetadata = config.getColumnMetadataByColumnIndex() != null ? - config.getColumnMetadataByColumnIndex().get(i) : null; + final Map columnMetadata = + config.getColumnMetadataByColumnIndex() != null + ? config.getColumnMetadataByColumnIndex().get(i) + : null; final Map metadata; if (config.shouldIncludeMetadata()) { metadata = new HashMap<>(); @@ -278,14 +287,19 @@ public static Schema jdbcToArrowSchema(ResultSetMetaData rsmd, JdbcToArrowConfig final JdbcFieldInfo columnFieldInfo = getJdbcFieldInfoForColumn(rsmd, i, config); final ArrowType arrowType = config.getJdbcToArrowTypeConverter().apply(columnFieldInfo); if (arrowType != null) { - final FieldType fieldType = new FieldType( - isColumnNullable(rsmd, i, columnFieldInfo), arrowType, /* dictionary encoding */ null, metadata); + final FieldType fieldType = + new FieldType( + isColumnNullable(rsmd, i, columnFieldInfo), + arrowType, /* dictionary encoding */ + null, + metadata); List children = null; if (arrowType.getTypeID() == ArrowType.List.TYPE_TYPE) { final JdbcFieldInfo arrayFieldInfo = getJdbcFieldInfoForArraySubType(rsmd, i, config); if (arrayFieldInfo == null) { - throw new IllegalArgumentException("Configuration does not provide a mapping for array column " + i); + throw new IllegalArgumentException( + "Configuration does not provide a mapping for array column " + i); } children = new ArrayList(); final ArrowType childType = config.getJdbcToArrowTypeConverter().apply(arrayFieldInfo); @@ -295,9 +309,13 @@ public static Schema jdbcToArrowSchema(ResultSetMetaData rsmd, JdbcToArrowConfig FieldType keyType = new FieldType(false, new ArrowType.Utf8(), null, null); FieldType valueType = new FieldType(false, new ArrowType.Utf8(), null, null); children = new ArrayList<>(); - children.add(new Field("child", mapType, - Arrays.asList(new Field(MapVector.KEY_NAME, keyType, null), - new Field(MapVector.VALUE_NAME, valueType, null)))); + children.add( + new Field( + "child", + mapType, + Arrays.asList( + new Field(MapVector.KEY_NAME, keyType, null), + new Field(MapVector.VALUE_NAME, valueType, null)))); } fields.add(new Field(columnName, fieldType, children)); @@ -307,18 +325,14 @@ public static Schema jdbcToArrowSchema(ResultSetMetaData rsmd, JdbcToArrowConfig } static JdbcFieldInfo getJdbcFieldInfoForColumn( - ResultSetMetaData rsmd, - int arrayColumn, - JdbcToArrowConfig config) - throws SQLException { + ResultSetMetaData rsmd, int arrayColumn, JdbcToArrowConfig config) throws SQLException { Preconditions.checkNotNull(rsmd, "ResultSet MetaData object cannot be null"); Preconditions.checkNotNull(config, "Configuration must not be null"); Preconditions.checkArgument( - arrayColumn > 0, - "ResultSetMetaData columns start with 1; column cannot be less than 1"); + arrayColumn > 0, "ResultSetMetaData columns start with 1; column cannot be less than 1"); Preconditions.checkArgument( - arrayColumn <= rsmd.getColumnCount(), - "Column number cannot be more than the number of columns"); + arrayColumn <= rsmd.getColumnCount(), + "Column number cannot be more than the number of columns"); JdbcFieldInfo fieldInfo = config.getExplicitTypeByColumnIndex(arrayColumn); if (fieldInfo == null) { @@ -334,16 +348,12 @@ static JdbcFieldInfo getJdbcFieldInfoForColumn( * If no sub-type can be found, returns null. */ private static JdbcFieldInfo getJdbcFieldInfoForArraySubType( - ResultSetMetaData rsmd, - int arrayColumn, - JdbcToArrowConfig config) - throws SQLException { + ResultSetMetaData rsmd, int arrayColumn, JdbcToArrowConfig config) throws SQLException { Preconditions.checkNotNull(rsmd, "ResultSet MetaData object cannot be null"); Preconditions.checkNotNull(config, "Configuration must not be null"); Preconditions.checkArgument( - arrayColumn > 0, - "ResultSetMetaData columns start with 1; column cannot be less than 1"); + arrayColumn > 0, "ResultSetMetaData columns start with 1; column cannot be less than 1"); Preconditions.checkArgument( arrayColumn <= rsmd.getColumnCount(), "Column number cannot be more than the number of columns"); @@ -359,10 +369,10 @@ private static JdbcFieldInfo getJdbcFieldInfoForArraySubType( * Iterate the given JDBC {@link ResultSet} object to fetch the data and transpose it to populate * the given Arrow Vector objects. * - * @param rs ResultSet to use to fetch the data from underlying database - * @param root Arrow {@link VectorSchemaRoot} object to populate - * @param calendar The calendar to use when reading {@link Date}, {@link Time}, or {@link Timestamp} - * data types from the {@link ResultSet}, or null if not converting. + * @param rs ResultSet to use to fetch the data from underlying database + * @param root Arrow {@link VectorSchemaRoot} object to populate + * @param calendar The calendar to use when reading {@link Date}, {@link Time}, or {@link + * Timestamp} data types from the {@link ResultSet}, or null if not converting. * @throws SQLException on error */ public static void jdbcToArrowVectors(ResultSet rs, VectorSchemaRoot root, Calendar calendar) @@ -373,29 +383,30 @@ public static void jdbcToArrowVectors(ResultSet rs, VectorSchemaRoot root, Calen jdbcToArrowVectors(rs, root, new JdbcToArrowConfig(new RootAllocator(0), calendar)); } - static boolean isColumnNullable(ResultSetMetaData resultSetMetadata, int index, JdbcFieldInfo info) - throws SQLException { + static boolean isColumnNullable( + ResultSetMetaData resultSetMetadata, int index, JdbcFieldInfo info) throws SQLException { int nullableValue; if (info != null && info.isNullable() != ResultSetMetaData.columnNullableUnknown) { nullableValue = info.isNullable(); } else { nullableValue = resultSetMetadata.isNullable(index); } - return nullableValue == ResultSetMetaData.columnNullable || - nullableValue == ResultSetMetaData.columnNullableUnknown; + return nullableValue == ResultSetMetaData.columnNullable + || nullableValue == ResultSetMetaData.columnNullableUnknown; } /** * Iterate the given JDBC {@link ResultSet} object to fetch the data and transpose it to populate * the given Arrow Vector objects. * - * @param rs ResultSet to use to fetch the data from underlying database - * @param root Arrow {@link VectorSchemaRoot} object to populate + * @param rs ResultSet to use to fetch the data from underlying database + * @param root Arrow {@link VectorSchemaRoot} object to populate * @param config The configuration to use when reading the data. * @throws SQLException on error * @throws JdbcConsumerException on error from VectorConsumer */ - public static void jdbcToArrowVectors(ResultSet rs, VectorSchemaRoot root, JdbcToArrowConfig config) + public static void jdbcToArrowVectors( + ResultSet rs, VectorSchemaRoot root, JdbcToArrowConfig config) throws SQLException, IOException { ResultSetMetaData rsmd = rs.getMetaData(); @@ -405,8 +416,13 @@ public static void jdbcToArrowVectors(ResultSet rs, VectorSchemaRoot root, JdbcT for (int i = 1; i <= columnCount; i++) { FieldVector vector = root.getVector(rsmd.getColumnLabel(i)); final JdbcFieldInfo columnFieldInfo = getJdbcFieldInfoForColumn(rsmd, i, config); - consumers[i - 1] = getConsumer( - vector.getField().getType(), i, isColumnNullable(rsmd, i, columnFieldInfo), vector, config); + consumers[i - 1] = + getConsumer( + vector.getField().getType(), + i, + isColumnNullable(rsmd, i, columnFieldInfo), + vector, + config); } CompositeJdbcConsumer compositeConsumer = null; @@ -439,18 +455,22 @@ public static void jdbcToArrowVectors(ResultSet rs, VectorSchemaRoot root, JdbcT } /** - * Default function used for JdbcConsumerFactory. This function gets a JdbcConsumer for the - * given column based on the Arrow type and provided vector. + * Default function used for JdbcConsumerFactory. This function gets a JdbcConsumer for the given + * column based on the Arrow type and provided vector. * - * @param arrowType Arrow type for the column. + * @param arrowType Arrow type for the column. * @param columnIndex Column index to fetch from the ResultSet - * @param nullable Whether the value is nullable or not - * @param vector Vector to store the consumed value - * @param config Associated JdbcToArrowConfig, used mainly for the Calendar. + * @param nullable Whether the value is nullable or not + * @param vector Vector to store the consumed value + * @param config Associated JdbcToArrowConfig, used mainly for the Calendar. * @return {@link JdbcConsumer} */ - public static JdbcConsumer getConsumer(ArrowType arrowType, int columnIndex, boolean nullable, - FieldVector vector, JdbcToArrowConfig config) { + public static JdbcConsumer getConsumer( + ArrowType arrowType, + int columnIndex, + boolean nullable, + FieldVector vector, + JdbcToArrowConfig config) { final Calendar calendar = config.getCalendar(); switch (arrowType.getTypeID()) { @@ -472,10 +492,11 @@ public static JdbcConsumer getConsumer(ArrowType arrowType, int columnIndex, boo case Decimal: final RoundingMode bigDecimalRoundingMode = config.getBigDecimalRoundingMode(); if (((ArrowType.Decimal) arrowType).getBitWidth() == 256) { - return Decimal256Consumer.createConsumer((Decimal256Vector) vector, columnIndex, nullable, - bigDecimalRoundingMode); + return Decimal256Consumer.createConsumer( + (Decimal256Vector) vector, columnIndex, nullable, bigDecimalRoundingMode); } else { - return DecimalConsumer.createConsumer((DecimalVector) vector, columnIndex, nullable, bigDecimalRoundingMode); + return DecimalConsumer.createConsumer( + (DecimalVector) vector, columnIndex, nullable, bigDecimalRoundingMode); } case FloatingPoint: switch (((ArrowType.FloatingPoint) arrowType).getPrecision()) { @@ -495,17 +516,25 @@ public static JdbcConsumer getConsumer(ArrowType arrowType, int columnIndex, boo case Date: return DateConsumer.createConsumer((DateDayVector) vector, columnIndex, nullable, calendar); case Time: - return TimeConsumer.createConsumer((TimeMilliVector) vector, columnIndex, nullable, calendar); + return TimeConsumer.createConsumer( + (TimeMilliVector) vector, columnIndex, nullable, calendar); case Timestamp: if (config.getCalendar() == null) { - return TimestampConsumer.createConsumer((TimeStampMilliVector) vector, columnIndex, nullable); + return TimestampConsumer.createConsumer( + (TimeStampMilliVector) vector, columnIndex, nullable); } else { - return TimestampTZConsumer.createConsumer((TimeStampMilliTZVector) vector, columnIndex, nullable, calendar); + return TimestampTZConsumer.createConsumer( + (TimeStampMilliTZVector) vector, columnIndex, nullable, calendar); } case List: FieldVector childVector = ((ListVector) vector).getDataVector(); - JdbcConsumer delegate = getConsumer(childVector.getField().getType(), JDBC_ARRAY_VALUE_COLUMN, - childVector.getField().isNullable(), childVector, config); + JdbcConsumer delegate = + getConsumer( + childVector.getField().getType(), + JDBC_ARRAY_VALUE_COLUMN, + childVector.getField().isNullable(), + childVector, + config); return ArrayConsumer.createConsumer((ListVector) vector, delegate, columnIndex, nullable); case Map: return MapConsumer.createConsumer((MapVector) vector, columnIndex, nullable); diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/BaseColumnBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/BaseColumnBinder.java index f24f409072c..e1df0041982 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/BaseColumnBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/BaseColumnBinder.java @@ -21,6 +21,7 @@ /** * Base class for ColumnBinder implementations. + * * @param The concrete FieldVector subtype. */ public abstract class BaseColumnBinder implements ColumnBinder { diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/BigIntBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/BigIntBinder.java index fde4642ef90..f84a7c3e22b 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/BigIntBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/BigIntBinder.java @@ -20,7 +20,6 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Types; - import org.apache.arrow.vector.BigIntVector; /** A column binder for 8-bit integers. */ @@ -34,7 +33,8 @@ public BigIntBinder(BigIntVector vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { final long value = vector.getDataBuffer().getLong((long) rowIndex * BigIntVector.TYPE_WIDTH); statement.setLong(parameterIndex, value); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/BitBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/BitBinder.java index adae513e99e..86930dc35cb 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/BitBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/BitBinder.java @@ -20,7 +20,6 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Types; - import org.apache.arrow.vector.BitVector; /** A column binder for booleans. */ @@ -34,7 +33,8 @@ public BitBinder(BitVector vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { // See BitVector#getBit final int byteIndex = rowIndex >> 3; final byte b = vector.getDataBuffer().getByte(byteIndex); diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/ColumnBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/ColumnBinder.java index c2b1259e142..0e16b61ef84 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/ColumnBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/ColumnBinder.java @@ -19,12 +19,9 @@ import java.sql.PreparedStatement; import java.sql.SQLException; - import org.apache.arrow.vector.FieldVector; -/** - * A helper to bind values from a wrapped Arrow vector to a JDBC PreparedStatement. - */ +/** A helper to bind values from a wrapped Arrow vector to a JDBC PreparedStatement. */ public interface ColumnBinder { /** * Bind the given row to the given parameter. @@ -43,14 +40,10 @@ public interface ColumnBinder { */ int getJdbcType(); - /** - * Get the vector used by this binder. - */ + /** Get the vector used by this binder. */ FieldVector getVector(); - /** - * Create a column binder for a vector, using the default JDBC type code for null values. - */ + /** Create a column binder for a vector, using the default JDBC type code for null values. */ static ColumnBinder forVector(FieldVector vector) { return forVector(vector, /*jdbcType*/ null); } @@ -62,7 +55,8 @@ static ColumnBinder forVector(FieldVector vector) { * @param jdbcType The JDBC type code to use (or null to use the default). */ static ColumnBinder forVector(FieldVector vector, Integer jdbcType) { - final ColumnBinder binder = vector.getField().getType().accept(new ColumnBinderArrowTypeVisitor(vector, jdbcType)); + final ColumnBinder binder = + vector.getField().getType().accept(new ColumnBinderArrowTypeVisitor(vector, jdbcType)); if (vector.getField().isNullable()) { return new NullableColumnBinder(binder); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/ColumnBinderArrowTypeVisitor.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/ColumnBinderArrowTypeVisitor.java index dc708724043..8dd43f25b66 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/ColumnBinderArrowTypeVisitor.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/ColumnBinderArrowTypeVisitor.java @@ -21,7 +21,6 @@ import java.time.ZoneId; import java.util.Calendar; import java.util.TimeZone; - import org.apache.arrow.vector.BigIntVector; import org.apache.arrow.vector.BitVector; import org.apache.arrow.vector.DateDayVector; @@ -50,8 +49,8 @@ /** * Visitor to create the base ColumnBinder for a vector. - *

- * To handle null values, wrap the returned binder in a {@link NullableColumnBinder}. + * + *

To handle null values, wrap the returned binder in a {@link NullableColumnBinder}. */ public class ColumnBinderArrowTypeVisitor implements ArrowType.ArrowTypeVisitor { private final FieldVector vector; @@ -111,17 +110,21 @@ public ColumnBinder visit(ArrowType.Int type) { } switch (type.getBitWidth()) { case 8: - return jdbcType == null ? new TinyIntBinder((TinyIntVector) vector) : - new TinyIntBinder((TinyIntVector) vector, jdbcType); + return jdbcType == null + ? new TinyIntBinder((TinyIntVector) vector) + : new TinyIntBinder((TinyIntVector) vector, jdbcType); case 16: - return jdbcType == null ? new SmallIntBinder((SmallIntVector) vector) : - new SmallIntBinder((SmallIntVector) vector, jdbcType); + return jdbcType == null + ? new SmallIntBinder((SmallIntVector) vector) + : new SmallIntBinder((SmallIntVector) vector, jdbcType); case 32: - return jdbcType == null ? new IntBinder((IntVector) vector) : - new IntBinder((IntVector) vector, jdbcType); + return jdbcType == null + ? new IntBinder((IntVector) vector) + : new IntBinder((IntVector) vector, jdbcType); case 64: - return jdbcType == null ? new BigIntBinder((BigIntVector) vector) : - new BigIntBinder((BigIntVector) vector, jdbcType); + return jdbcType == null + ? new BigIntBinder((BigIntVector) vector) + : new BigIntBinder((BigIntVector) vector, jdbcType); default: throw new UnsupportedOperationException("No column binder implemented for type " + type); } @@ -131,11 +134,13 @@ public ColumnBinder visit(ArrowType.Int type) { public ColumnBinder visit(ArrowType.FloatingPoint type) { switch (type.getPrecision()) { case SINGLE: - return jdbcType == null ? new Float4Binder((Float4Vector) vector) : - new Float4Binder((Float4Vector) vector, jdbcType); + return jdbcType == null + ? new Float4Binder((Float4Vector) vector) + : new Float4Binder((Float4Vector) vector, jdbcType); case DOUBLE: - return jdbcType == null ? new Float8Binder((Float8Vector) vector) : - new Float8Binder((Float8Vector) vector, jdbcType); + return jdbcType == null + ? new Float8Binder((Float8Vector) vector) + : new Float8Binder((Float8Vector) vector, jdbcType); default: throw new UnsupportedOperationException("No column binder implemented for type " + type); } @@ -144,51 +149,62 @@ public ColumnBinder visit(ArrowType.FloatingPoint type) { @Override public ColumnBinder visit(ArrowType.Utf8 type) { VarCharVector varChar = (VarCharVector) vector; - return jdbcType == null ? new VarCharBinder<>(varChar, Types.VARCHAR) : - new VarCharBinder<>(varChar, jdbcType); + return jdbcType == null + ? new VarCharBinder<>(varChar, Types.VARCHAR) + : new VarCharBinder<>(varChar, jdbcType); } @Override public ColumnBinder visit(ArrowType.LargeUtf8 type) { LargeVarCharVector varChar = (LargeVarCharVector) vector; - return jdbcType == null ? new VarCharBinder<>(varChar, Types.LONGVARCHAR) : - new VarCharBinder<>(varChar, jdbcType); + return jdbcType == null + ? new VarCharBinder<>(varChar, Types.LONGVARCHAR) + : new VarCharBinder<>(varChar, jdbcType); } @Override public ColumnBinder visit(ArrowType.Binary type) { VarBinaryVector varBinary = (VarBinaryVector) vector; - return jdbcType == null ? new VarBinaryBinder<>(varBinary, Types.VARBINARY) : - new VarBinaryBinder<>(varBinary, jdbcType); + return jdbcType == null + ? new VarBinaryBinder<>(varBinary, Types.VARBINARY) + : new VarBinaryBinder<>(varBinary, jdbcType); } @Override public ColumnBinder visit(ArrowType.LargeBinary type) { LargeVarBinaryVector varBinary = (LargeVarBinaryVector) vector; - return jdbcType == null ? new VarBinaryBinder<>(varBinary, Types.LONGVARBINARY) : - new VarBinaryBinder<>(varBinary, jdbcType); + return jdbcType == null + ? new VarBinaryBinder<>(varBinary, Types.LONGVARBINARY) + : new VarBinaryBinder<>(varBinary, jdbcType); } @Override public ColumnBinder visit(ArrowType.FixedSizeBinary type) { FixedSizeBinaryVector binary = (FixedSizeBinaryVector) vector; - return jdbcType == null ? new FixedSizeBinaryBinder(binary, Types.BINARY) : - new FixedSizeBinaryBinder(binary, jdbcType); + return jdbcType == null + ? new FixedSizeBinaryBinder(binary, Types.BINARY) + : new FixedSizeBinaryBinder(binary, jdbcType); } @Override public ColumnBinder visit(ArrowType.Bool type) { - return jdbcType == null ? new BitBinder((BitVector) vector) : new BitBinder((BitVector) vector, jdbcType); + return jdbcType == null + ? new BitBinder((BitVector) vector) + : new BitBinder((BitVector) vector, jdbcType); } @Override public ColumnBinder visit(ArrowType.Decimal type) { if (type.getBitWidth() == 128) { DecimalVector decimalVector = (DecimalVector) vector; - return jdbcType == null ? new Decimal128Binder(decimalVector) : new Decimal128Binder(decimalVector, jdbcType); + return jdbcType == null + ? new Decimal128Binder(decimalVector) + : new Decimal128Binder(decimalVector, jdbcType); } else if (type.getBitWidth() == 256) { Decimal256Vector decimalVector = (Decimal256Vector) vector; - return jdbcType == null ? new Decimal256Binder(decimalVector) : new Decimal256Binder(decimalVector, jdbcType); + return jdbcType == null + ? new Decimal256Binder(decimalVector) + : new Decimal256Binder(decimalVector, jdbcType); } throw new UnsupportedOperationException("No column binder implemented for type " + type); } @@ -197,11 +213,13 @@ public ColumnBinder visit(ArrowType.Decimal type) { public ColumnBinder visit(ArrowType.Date type) { switch (type.getUnit()) { case DAY: - return jdbcType == null ? new DateDayBinder((DateDayVector) vector) : - new DateDayBinder((DateDayVector) vector, /*calendar*/null, jdbcType); + return jdbcType == null + ? new DateDayBinder((DateDayVector) vector) + : new DateDayBinder((DateDayVector) vector, /*calendar*/ null, jdbcType); case MILLISECOND: - return jdbcType == null ? new DateMilliBinder((DateMilliVector) vector) : - new DateMilliBinder((DateMilliVector) vector, /*calendar*/null, jdbcType); + return jdbcType == null + ? new DateMilliBinder((DateMilliVector) vector) + : new DateMilliBinder((DateMilliVector) vector, /*calendar*/ null, jdbcType); default: throw new UnsupportedOperationException("No column binder implemented for type " + type); } @@ -211,17 +229,21 @@ public ColumnBinder visit(ArrowType.Date type) { public ColumnBinder visit(ArrowType.Time type) { switch (type.getUnit()) { case SECOND: - return jdbcType == null ? new Time32Binder((TimeSecVector) vector) : - new Time32Binder((TimeSecVector) vector, jdbcType); + return jdbcType == null + ? new Time32Binder((TimeSecVector) vector) + : new Time32Binder((TimeSecVector) vector, jdbcType); case MILLISECOND: - return jdbcType == null ? new Time32Binder((TimeMilliVector) vector) : - new Time32Binder((TimeMilliVector) vector, jdbcType); + return jdbcType == null + ? new Time32Binder((TimeMilliVector) vector) + : new Time32Binder((TimeMilliVector) vector, jdbcType); case MICROSECOND: - return jdbcType == null ? new Time64Binder((TimeMicroVector) vector) : - new Time64Binder((TimeMicroVector) vector, jdbcType); + return jdbcType == null + ? new Time64Binder((TimeMicroVector) vector) + : new Time64Binder((TimeMicroVector) vector, jdbcType); case NANOSECOND: - return jdbcType == null ? new Time64Binder((TimeNanoVector) vector) : - new Time64Binder((TimeNanoVector) vector, jdbcType); + return jdbcType == null + ? new Time64Binder((TimeNanoVector) vector) + : new Time64Binder((TimeNanoVector) vector, jdbcType); default: throw new UnsupportedOperationException("No column binder implemented for type " + type); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/DateDayBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/DateDayBinder.java index bc16790c8f3..92686d54203 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/DateDayBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/DateDayBinder.java @@ -22,12 +22,9 @@ import java.sql.SQLException; import java.sql.Types; import java.util.Calendar; - import org.apache.arrow.vector.DateDayVector; -/** - * A column binder for 32-bit dates. - */ +/** A column binder for 32-bit dates. */ public class DateDayBinder extends BaseColumnBinder { private static final long MILLIS_PER_DAY = 86_400_000; private final Calendar calendar; @@ -46,7 +43,8 @@ public DateDayBinder(DateDayVector vector, Calendar calendar, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { // TODO: multiply with overflow final long index = (long) rowIndex * DateDayVector.TYPE_WIDTH; final Date value = new Date(vector.getDataBuffer().getInt(index) * MILLIS_PER_DAY); diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/DateMilliBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/DateMilliBinder.java index 5cb91b46ac1..df2dcc8639d 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/DateMilliBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/DateMilliBinder.java @@ -22,12 +22,9 @@ import java.sql.SQLException; import java.sql.Types; import java.util.Calendar; - import org.apache.arrow.vector.DateMilliVector; -/** - * A column binder for 64-bit dates. - */ +/** A column binder for 64-bit dates. */ public class DateMilliBinder extends BaseColumnBinder { private final Calendar calendar; @@ -39,14 +36,14 @@ public DateMilliBinder(DateMilliVector vector, Calendar calendar) { this(vector, calendar, Types.DATE); } - public DateMilliBinder(DateMilliVector vector, Calendar calendar, int jdbcType) { super(vector, jdbcType); this.calendar = calendar; } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { final long index = (long) rowIndex * DateMilliVector.TYPE_WIDTH; final Date value = new Date(vector.getDataBuffer().getLong(index)); if (calendar == null) { diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Decimal128Binder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Decimal128Binder.java index 9e9d0e4fdb2..7f7e8774510 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Decimal128Binder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Decimal128Binder.java @@ -21,13 +21,10 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Types; - import org.apache.arrow.vector.DecimalVector; import org.apache.arrow.vector.util.DecimalUtility; -/** - * A binder for 128-bit decimals. - */ +/** A binder for 128-bit decimals. */ public class Decimal128Binder extends BaseColumnBinder { public Decimal128Binder(DecimalVector vector) { this(vector, Types.DECIMAL); @@ -38,9 +35,11 @@ public Decimal128Binder(DecimalVector vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { - final BigDecimal value = DecimalUtility.getBigDecimalFromArrowBuf( - vector.getDataBuffer(), rowIndex, vector.getScale(), DecimalVector.TYPE_WIDTH); + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { + final BigDecimal value = + DecimalUtility.getBigDecimalFromArrowBuf( + vector.getDataBuffer(), rowIndex, vector.getScale(), DecimalVector.TYPE_WIDTH); statement.setBigDecimal(parameterIndex, value); } } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Decimal256Binder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Decimal256Binder.java index bd29e083b45..38de58f76e1 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Decimal256Binder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Decimal256Binder.java @@ -21,13 +21,10 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Types; - import org.apache.arrow.vector.Decimal256Vector; import org.apache.arrow.vector.util.DecimalUtility; -/** - * A binder for 256-bit decimals. - */ +/** A binder for 256-bit decimals. */ public class Decimal256Binder extends BaseColumnBinder { public Decimal256Binder(Decimal256Vector vector) { this(vector, Types.DECIMAL); @@ -38,9 +35,11 @@ public Decimal256Binder(Decimal256Vector vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { - final BigDecimal value = DecimalUtility.getBigDecimalFromArrowBuf( - vector.getDataBuffer(), rowIndex, vector.getScale(), Decimal256Vector.TYPE_WIDTH); + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { + final BigDecimal value = + DecimalUtility.getBigDecimalFromArrowBuf( + vector.getDataBuffer(), rowIndex, vector.getScale(), Decimal256Vector.TYPE_WIDTH); statement.setBigDecimal(parameterIndex, value); } } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/FixedSizeBinaryBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/FixedSizeBinaryBinder.java index 7edc5e45329..6f39ffe4db3 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/FixedSizeBinaryBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/FixedSizeBinaryBinder.java @@ -19,17 +19,14 @@ import java.sql.PreparedStatement; import java.sql.SQLException; - import org.apache.arrow.vector.FixedSizeBinaryVector; -/** - * A binder for fixed-width binary types. - */ +/** A binder for fixed-width binary types. */ public class FixedSizeBinaryBinder extends BaseColumnBinder { /** * Create a binder for the given vector using the given JDBC type for null values. * - * @param vector The vector to draw values from. + * @param vector The vector to draw values from. * @param jdbcType The JDBC type code. */ public FixedSizeBinaryBinder(FixedSizeBinaryVector vector, int jdbcType) { @@ -37,9 +34,12 @@ public FixedSizeBinaryBinder(FixedSizeBinaryVector vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { byte[] binaryData = new byte[vector.getByteWidth()]; - vector.getDataBuffer().getBytes((long) rowIndex * binaryData.length, binaryData, 0, binaryData.length); + vector + .getDataBuffer() + .getBytes((long) rowIndex * binaryData.length, binaryData, 0, binaryData.length); statement.setBytes(parameterIndex, binaryData); } } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Float4Binder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Float4Binder.java index a471c1ebadd..af3a3b21604 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Float4Binder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Float4Binder.java @@ -20,12 +20,9 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Types; - import org.apache.arrow.vector.Float4Vector; -/** - * A binder for 32-bit floats. - */ +/** A binder for 32-bit floats. */ public class Float4Binder extends BaseColumnBinder { public Float4Binder(Float4Vector vector) { this(vector, Types.REAL); @@ -36,7 +33,8 @@ public Float4Binder(Float4Vector vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { final float value = vector.getDataBuffer().getFloat((long) rowIndex * Float4Vector.TYPE_WIDTH); statement.setFloat(parameterIndex, value); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Float8Binder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Float8Binder.java index 4710c3b5986..c85dc926ac0 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Float8Binder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Float8Binder.java @@ -20,12 +20,9 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Types; - import org.apache.arrow.vector.Float8Vector; -/** - * A binder for 64-bit floats. - */ +/** A binder for 64-bit floats. */ public class Float8Binder extends BaseColumnBinder { public Float8Binder(Float8Vector vector) { this(vector, Types.DOUBLE); @@ -36,8 +33,10 @@ public Float8Binder(Float8Vector vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { - final double value = vector.getDataBuffer().getDouble((long) rowIndex * Float8Vector.TYPE_WIDTH); + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { + final double value = + vector.getDataBuffer().getDouble((long) rowIndex * Float8Vector.TYPE_WIDTH); statement.setDouble(parameterIndex, value); } } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/IntBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/IntBinder.java index 7d47f585a39..2d3bdbab4a8 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/IntBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/IntBinder.java @@ -20,7 +20,6 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Types; - import org.apache.arrow.vector.IntVector; /** A column binder for 32-bit integers. */ @@ -34,7 +33,8 @@ public IntBinder(IntVector vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { final int value = vector.getDataBuffer().getInt((long) rowIndex * IntVector.TYPE_WIDTH); statement.setInt(parameterIndex, value); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/ListBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/ListBinder.java index b8aa61234f4..7d59cb8ea01 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/ListBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/ListBinder.java @@ -20,15 +20,12 @@ import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; - import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.complex.ListVector; import org.apache.arrow.vector.complex.impl.UnionListReader; import org.apache.arrow.vector.util.Text; -/** - * A column binder for list of primitive values. - */ +/** A column binder for list of primitive values. */ public class ListBinder extends BaseColumnBinder { private final UnionListReader listReader; @@ -52,7 +49,9 @@ public ListBinder(ListVector vector, int jdbcType) { try { arrayElementClass = dataVectorClass.getMethod("getObject", Integer.TYPE).getReturnType(); } catch (NoSuchMethodException e) { - final String message = String.format("Issue to determine type for getObject method of data vector class %s ", + final String message = + String.format( + "Issue to determine type for getObject method of data vector class %s ", dataVectorClass.getName()); throw new RuntimeException(message); } @@ -60,7 +59,8 @@ public ListBinder(ListVector vector, int jdbcType) { } @Override - public void bind(java.sql.PreparedStatement statement, int parameterIndex, int rowIndex)throws java.sql.SQLException { + public void bind(java.sql.PreparedStatement statement, int parameterIndex, int rowIndex) + throws java.sql.SQLException { listReader.setPosition(rowIndex); ArrayList sourceArray = (ArrayList) listReader.readObject(); Object array; @@ -69,7 +69,9 @@ public void bind(java.sql.PreparedStatement statement, int parameterIndex, int r Arrays.setAll((Object[]) array, sourceArray::get); } else { array = new String[sourceArray.size()]; - Arrays.setAll((Object[]) array, idx -> sourceArray.get(idx) != null ? sourceArray.get(idx).toString() : null); + Arrays.setAll( + (Object[]) array, + idx -> sourceArray.get(idx) != null ? sourceArray.get(idx).toString() : null); } statement.setObject(parameterIndex, array); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/MapBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/MapBinder.java index 07391eb7cbf..6e347a18123 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/MapBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/MapBinder.java @@ -23,16 +23,13 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Objects; - import org.apache.arrow.vector.complex.MapVector; import org.apache.arrow.vector.complex.impl.UnionMapReader; import org.apache.arrow.vector.types.pojo.ArrowType; import org.apache.arrow.vector.types.pojo.Field; import org.apache.arrow.vector.util.JsonStringHashMap; -/** - * A column binder for map of primitive values. - */ +/** A column binder for map of primitive values. */ public class MapBinder extends BaseColumnBinder { private UnionMapReader reader; @@ -58,8 +55,8 @@ public MapBinder(MapVector vector, int jdbcType) { } List keyValueFields = Objects.requireNonNull(structField.get(0)).getChildren(); if (keyValueFields.size() != 2) { - throw new IllegalArgumentException("Expected two children fields " + - "inside nested Struct field in Map"); + throw new IllegalArgumentException( + "Expected two children fields " + "inside nested Struct field in Map"); } ArrowType keyType = Objects.requireNonNull(keyValueFields.get(0)).getType(); ArrowType valueType = Objects.requireNonNull(keyValueFields.get(1)).getType(); @@ -68,15 +65,16 @@ public MapBinder(MapVector vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, - int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { reader.setPosition(rowIndex); LinkedHashMap tags = new JsonStringHashMap<>(); while (reader.next()) { Object key = reader.key().readObject(); Object value = reader.value().readObject(); - tags.put(isTextKey && key != null ? key.toString() : key, - isTextValue && value != null ? value.toString() : value); + tags.put( + isTextKey && key != null ? key.toString() : key, + isTextValue && value != null ? value.toString() : value); } switch (jdbcType) { case Types.VARCHAR: diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/NullableColumnBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/NullableColumnBinder.java index 123b587ca50..202a723c51a 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/NullableColumnBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/NullableColumnBinder.java @@ -19,12 +19,9 @@ import java.sql.PreparedStatement; import java.sql.SQLException; - import org.apache.arrow.vector.FieldVector; -/** - * A ColumnBinder that checks for nullability before deferring to a type-specific binder. - */ +/** A ColumnBinder that checks for nullability before deferring to a type-specific binder. */ public class NullableColumnBinder implements ColumnBinder { private final ColumnBinder wrapped; @@ -33,7 +30,8 @@ public NullableColumnBinder(ColumnBinder wrapped) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { if (wrapped.getVector().isNull(rowIndex)) { statement.setNull(parameterIndex, wrapped.getJdbcType()); } else { diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/SmallIntBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/SmallIntBinder.java index f9d744b9f54..81af48d83f7 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/SmallIntBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/SmallIntBinder.java @@ -20,7 +20,6 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Types; - import org.apache.arrow.vector.SmallIntVector; /** A column binder for 8-bit integers. */ @@ -34,8 +33,10 @@ public SmallIntBinder(SmallIntVector vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { - final short value = vector.getDataBuffer().getShort((short) rowIndex * SmallIntVector.TYPE_WIDTH); + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { + final short value = + vector.getDataBuffer().getShort((short) rowIndex * SmallIntVector.TYPE_WIDTH); statement.setShort(parameterIndex, value); } } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Time32Binder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Time32Binder.java index 5dc7e3f513f..ec09ae53cd8 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Time32Binder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Time32Binder.java @@ -21,14 +21,11 @@ import java.sql.SQLException; import java.sql.Time; import java.sql.Types; - import org.apache.arrow.vector.BaseFixedWidthVector; import org.apache.arrow.vector.TimeMilliVector; import org.apache.arrow.vector.TimeSecVector; -/** - * A binder for 32-bit time types. - */ +/** A binder for 32-bit time types. */ public class Time32Binder extends BaseColumnBinder { private static final long TYPE_WIDTH = 4; @@ -43,11 +40,11 @@ public Time32Binder(TimeMilliVector vector) { } public Time32Binder(TimeSecVector vector, int jdbcType) { - this(vector, /*factor*/1_000, jdbcType); + this(vector, /*factor*/ 1_000, jdbcType); } public Time32Binder(TimeMilliVector vector, int jdbcType) { - this(vector, /*factor*/1, jdbcType); + this(vector, /*factor*/ 1, jdbcType); } Time32Binder(BaseFixedWidthVector vector, long factor, int jdbcType) { @@ -56,7 +53,8 @@ public Time32Binder(TimeMilliVector vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { // TODO: multiply with overflow // TODO: take in a Calendar as well? final Time value = new Time(vector.getDataBuffer().getInt(rowIndex * TYPE_WIDTH) * factor); diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Time64Binder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Time64Binder.java index 8d62ae0eb36..0c858bc46be 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Time64Binder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/Time64Binder.java @@ -21,14 +21,11 @@ import java.sql.SQLException; import java.sql.Time; import java.sql.Types; - import org.apache.arrow.vector.BaseFixedWidthVector; import org.apache.arrow.vector.TimeMicroVector; import org.apache.arrow.vector.TimeNanoVector; -/** - * A binder for 64-bit time types. - */ +/** A binder for 64-bit time types. */ public class Time64Binder extends BaseColumnBinder { private static final long TYPE_WIDTH = 8; @@ -43,11 +40,11 @@ public Time64Binder(TimeNanoVector vector) { } public Time64Binder(TimeMicroVector vector, int jdbcType) { - this(vector, /*factor*/1_000, jdbcType); + this(vector, /*factor*/ 1_000, jdbcType); } public Time64Binder(TimeNanoVector vector, int jdbcType) { - this(vector, /*factor*/1_000_000, jdbcType); + this(vector, /*factor*/ 1_000_000, jdbcType); } Time64Binder(BaseFixedWidthVector vector, long factor, int jdbcType) { @@ -56,7 +53,8 @@ public Time64Binder(TimeNanoVector vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { // TODO: option to throw on truncation (vendor Guava IntMath#multiply) final Time value = new Time(vector.getDataBuffer().getLong(rowIndex * TYPE_WIDTH) / factor); statement.setTime(parameterIndex, value); diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/TimeStampBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/TimeStampBinder.java index 6677e590990..7e0de9ee59b 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/TimeStampBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/TimeStampBinder.java @@ -22,7 +22,6 @@ import java.sql.Timestamp; import java.sql.Types; import java.util.Calendar; - import org.apache.arrow.vector.TimeStampVector; import org.apache.arrow.vector.types.pojo.ArrowType; @@ -32,15 +31,17 @@ public class TimeStampBinder extends BaseColumnBinder { private final long unitsPerSecond; private final long nanosPerUnit; - /** - * Create a binder for a timestamp vector using the default JDBC type code. - */ + /** Create a binder for a timestamp vector using the default JDBC type code. */ public TimeStampBinder(TimeStampVector vector, Calendar calendar) { - this(vector, calendar, isZoned(vector.getField().getType()) ? Types.TIMESTAMP_WITH_TIMEZONE : Types.TIMESTAMP); + this( + vector, + calendar, + isZoned(vector.getField().getType()) ? Types.TIMESTAMP_WITH_TIMEZONE : Types.TIMESTAMP); } /** * Create a binder for a timestamp vector. + * * @param vector The vector to pull values from. * @param calendar Optionally, the calendar to pass to JDBC. * @param jdbcType The JDBC type code to use for null values. @@ -73,19 +74,23 @@ public TimeStampBinder(TimeStampVector vector, Calendar calendar, int jdbcType) } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { // TODO: option to throw on truncation (vendor Guava IntMath#multiply) or overflow - final long rawValue = vector.getDataBuffer().getLong((long) rowIndex * TimeStampVector.TYPE_WIDTH); + final long rawValue = + vector.getDataBuffer().getLong((long) rowIndex * TimeStampVector.TYPE_WIDTH); final long seconds = rawValue / unitsPerSecond; final int nanos = (int) ((rawValue - (seconds * unitsPerSecond)) * nanosPerUnit); final Timestamp value = new Timestamp(seconds * 1_000); value.setNanos(nanos); if (calendar != null) { - // Timestamp == Date == UTC timestamp (confusingly). Arrow's timestamp with timezone is a UTC value with a + // Timestamp == Date == UTC timestamp (confusingly). Arrow's timestamp with timezone is a UTC + // value with a // zone offset, so we don't need to do any conversion. statement.setTimestamp(parameterIndex, value, calendar); } else { - // Arrow timestamp without timezone isn't strictly convertible to any timezone. So this is technically wrong, + // Arrow timestamp without timezone isn't strictly convertible to any timezone. So this is + // technically wrong, // but there is no 'correct' interpretation here. The application should provide a calendar. statement.setTimestamp(parameterIndex, value); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/TinyIntBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/TinyIntBinder.java index f51d139be86..d430b74ead3 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/TinyIntBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/TinyIntBinder.java @@ -20,7 +20,6 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Types; - import org.apache.arrow.vector.TinyIntVector; /** A column binder for 8-bit integers. */ @@ -34,7 +33,8 @@ public TinyIntBinder(TinyIntVector vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { final byte value = vector.getDataBuffer().getByte((long) rowIndex * TinyIntVector.TYPE_WIDTH); statement.setByte(parameterIndex, value); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarBinaryBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarBinaryBinder.java index a94cff6a004..54082d8aa52 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarBinaryBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarBinaryBinder.java @@ -19,7 +19,6 @@ import java.sql.PreparedStatement; import java.sql.SQLException; - import org.apache.arrow.memory.util.ArrowBufPointer; import org.apache.arrow.vector.ElementAddressableVector; import org.apache.arrow.vector.FieldVector; @@ -29,13 +28,14 @@ * * @param The binary vector. */ -public class VarBinaryBinder extends BaseColumnBinder { +public class VarBinaryBinder + extends BaseColumnBinder { private final ArrowBufPointer element; /** * Create a binder for the given vector using the given JDBC type for null values. * - * @param vector The vector to draw values from. + * @param vector The vector to draw values from. * @param jdbcType The JDBC type code. */ public VarBinaryBinder(T vector, int jdbcType) { @@ -44,15 +44,18 @@ public VarBinaryBinder(T vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { vector.getDataPointer(rowIndex, element); if (element.getBuf() == null) { statement.setNull(parameterIndex, jdbcType); return; } if (element.getLength() > (long) Integer.MAX_VALUE) { - final String message = String.format("Length of value at index %d (%d) exceeds Integer.MAX_VALUE", - rowIndex, element.getLength()); + final String message = + String.format( + "Length of value at index %d (%d) exceeds Integer.MAX_VALUE", + rowIndex, element.getLength()); throw new RuntimeException(message); } byte[] binaryData = new byte[(int) element.getLength()]; diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarCharBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarCharBinder.java index 73bd5598149..5200506d333 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarCharBinder.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarCharBinder.java @@ -20,7 +20,6 @@ import java.nio.charset.StandardCharsets; import java.sql.PreparedStatement; import java.sql.SQLException; - import org.apache.arrow.memory.util.ArrowBufPointer; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.VariableWidthVector; @@ -30,13 +29,14 @@ * * @param The text vector. */ -public class VarCharBinder extends BaseColumnBinder { +public class VarCharBinder + extends BaseColumnBinder { private final ArrowBufPointer element; /** * Create a binder for the given vector using the given JDBC type for null values. * - * @param vector The vector to draw values from. + * @param vector The vector to draw values from. * @param jdbcType The JDBC type code. */ public VarCharBinder(T vector, int jdbcType) { @@ -45,15 +45,18 @@ public VarCharBinder(T vector, int jdbcType) { } @Override - public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { + public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) + throws SQLException { vector.getDataPointer(rowIndex, element); if (element.getBuf() == null) { statement.setNull(parameterIndex, jdbcType); return; } if (element.getLength() > (long) Integer.MAX_VALUE) { - final String message = String.format("Length of value at index %d (%d) exceeds Integer.MAX_VALUE", - rowIndex, element.getLength()); + final String message = + String.format( + "Length of value at index %d (%d) exceeds Integer.MAX_VALUE", + rowIndex, element.getLength()); throw new RuntimeException(message); } byte[] utf8Bytes = new byte[(int) element.getLength()]; diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/package-info.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/package-info.java index 4f8936e0c27..945c3c9f84f 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/package-info.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/package-info.java @@ -15,8 +15,5 @@ * limitations under the License. */ -/** - * Utilities to bind Arrow data as JDBC prepared statement parameters. - */ - +/** Utilities to bind Arrow data as JDBC prepared statement parameters. */ package org.apache.arrow.adapter.jdbc.binder; diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/ArrayConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/ArrayConsumer.java index 2f18b8a416d..620f9bb0b30 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/ArrayConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/ArrayConsumer.java @@ -21,22 +21,19 @@ import java.sql.Array; import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.ValueVector; import org.apache.arrow.vector.complex.ListVector; /** - * Consumer which consume array type values from {@link ResultSet}. - * Write the data to {@link org.apache.arrow.vector.complex.ListVector}. + * Consumer which consume array type values from {@link ResultSet}. Write the data to {@link + * org.apache.arrow.vector.complex.ListVector}. */ public abstract class ArrayConsumer extends BaseConsumer { - /** - * Creates a consumer for {@link ListVector}. - */ + /** Creates a consumer for {@link ListVector}. */ public static ArrayConsumer createConsumer( - ListVector vector, JdbcConsumer delegate, int index, boolean nullable) { + ListVector vector, JdbcConsumer delegate, int index, boolean nullable) { if (nullable) { return new ArrayConsumer.NullableArrayConsumer(vector, delegate, index); } else { @@ -50,9 +47,7 @@ public static ArrayConsumer createConsumer( protected int innerVectorIndex = 0; - /** - * Instantiate a ArrayConsumer. - */ + /** Instantiate a ArrayConsumer. */ public ArrayConsumer(ListVector vector, JdbcConsumer delegate, int index) { super(vector, index); this.delegate = delegate; @@ -81,14 +76,10 @@ void ensureInnerVectorCapacity(int targetCapacity) { } } - /** - * Nullable consumer for {@link ListVector}. - */ + /** Nullable consumer for {@link ListVector}. */ static class NullableArrayConsumer extends ArrayConsumer { - /** - * Instantiate a nullable array consumer. - */ + /** Instantiate a nullable array consumer. */ public NullableArrayConsumer(ListVector vector, JdbcConsumer delegate, int index) { super(vector, delegate, index); } @@ -113,14 +104,10 @@ public void consume(ResultSet resultSet) throws SQLException, IOException { } } - /** - * Non-nullable consumer for {@link ListVector}. - */ + /** Non-nullable consumer for {@link ListVector}. */ static class NonNullableArrayConsumer extends ArrayConsumer { - /** - * Instantiate a nullable array consumer. - */ + /** Instantiate a nullable array consumer. */ public NonNullableArrayConsumer(ListVector vector, JdbcConsumer delegate, int index) { super(vector, delegate, index); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BaseConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BaseConsumer.java index 2db128d3e2b..8c5c0209455 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BaseConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BaseConsumer.java @@ -21,6 +21,7 @@ /** * Base class for all consumers. + * * @param vector type. */ public abstract class BaseConsumer implements JdbcConsumer { @@ -33,6 +34,7 @@ public abstract class BaseConsumer implements JdbcConsume /** * Constructs a new consumer. + * * @param vector the underlying vector for the consumer. * @param index the column id for the consumer. */ diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BigIntConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BigIntConsumer.java index 19c8efa9171..532103c8900 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BigIntConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BigIntConsumer.java @@ -19,19 +19,17 @@ import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.vector.BigIntVector; /** - * Consumer which consume bigint type values from {@link ResultSet}. - * Write the data to {@link org.apache.arrow.vector.BigIntVector}. + * Consumer which consume bigint type values from {@link ResultSet}. Write the data to {@link + * org.apache.arrow.vector.BigIntVector}. */ public class BigIntConsumer { - /** - * Creates a consumer for {@link BigIntVector}. - */ - public static JdbcConsumer createConsumer(BigIntVector vector, int index, boolean nullable) { + /** Creates a consumer for {@link BigIntVector}. */ + public static JdbcConsumer createConsumer( + BigIntVector vector, int index, boolean nullable) { if (nullable) { return new NullableBigIntConsumer(vector, index); } else { @@ -39,14 +37,10 @@ public static JdbcConsumer createConsumer(BigIntVector vector, int } } - /** - * Nullable consumer for big int. - */ + /** Nullable consumer for big int. */ static class NullableBigIntConsumer extends BaseConsumer { - /** - * Instantiate a BigIntConsumer. - */ + /** Instantiate a BigIntConsumer. */ public NullableBigIntConsumer(BigIntVector vector, int index) { super(vector, index); } @@ -63,14 +57,10 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Non-nullable consumer for big int. - */ + /** Non-nullable consumer for big int. */ static class NonNullableBigIntConsumer extends BaseConsumer { - /** - * Instantiate a BigIntConsumer. - */ + /** Instantiate a BigIntConsumer. */ public NonNullableBigIntConsumer(BigIntVector vector, int index) { super(vector, index); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BinaryConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BinaryConsumer.java index 8c5f61169d4..b0f6fce30b3 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BinaryConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BinaryConsumer.java @@ -21,20 +21,17 @@ import java.io.InputStream; import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.vector.BitVectorHelper; import org.apache.arrow.vector.VarBinaryVector; /** - * Consumer which consume binary type values from {@link ResultSet}. - * Write the data to {@link org.apache.arrow.vector.VarBinaryVector}. + * Consumer which consume binary type values from {@link ResultSet}. Write the data to {@link + * org.apache.arrow.vector.VarBinaryVector}. */ public abstract class BinaryConsumer extends BaseConsumer { - /** - * Creates a consumer for {@link VarBinaryVector}. - */ + /** Creates a consumer for {@link VarBinaryVector}. */ public static BinaryConsumer createConsumer(VarBinaryVector vector, int index, boolean nullable) { if (nullable) { return new NullableBinaryConsumer(vector, index); @@ -45,9 +42,7 @@ public static BinaryConsumer createConsumer(VarBinaryVector vector, int index, b private final byte[] reuseBytes = new byte[1024]; - /** - * Instantiate a BinaryConsumer. - */ + /** Instantiate a BinaryConsumer. */ public BinaryConsumer(VarBinaryVector vector, int index) { super(vector, index); if (vector != null) { @@ -55,9 +50,7 @@ public BinaryConsumer(VarBinaryVector vector, int index) { } } - /** - * consume a InputStream. - */ + /** consume a InputStream. */ public void consume(InputStream is) throws IOException { if (is != null) { while (currentIndex >= vector.getValueCapacity()) { @@ -74,7 +67,8 @@ public void consume(InputStream is) throws IOException { vector.getDataBuffer().setBytes(startOffset + dataLength, reuseBytes, 0, read); dataLength += read; } - offsetBuffer.setInt((currentIndex + 1) * VarBinaryVector.OFFSET_WIDTH, startOffset + dataLength); + offsetBuffer.setInt( + (currentIndex + 1) * VarBinaryVector.OFFSET_WIDTH, startOffset + dataLength); BitVectorHelper.setBit(vector.getValidityBuffer(), currentIndex); vector.setLastSet(currentIndex); } @@ -91,14 +85,10 @@ public void resetValueVector(VarBinaryVector vector) { this.currentIndex = 0; } - /** - * Consumer for nullable binary data. - */ + /** Consumer for nullable binary data. */ static class NullableBinaryConsumer extends BinaryConsumer { - - /** - * Instantiate a BinaryConsumer. - */ + + /** Instantiate a BinaryConsumer. */ public NullableBinaryConsumer(VarBinaryVector vector, int index) { super(vector, index); } @@ -113,14 +103,10 @@ public void consume(ResultSet resultSet) throws SQLException, IOException { } } - /** - * Consumer for non-nullable binary data. - */ + /** Consumer for non-nullable binary data. */ static class NonNullableBinaryConsumer extends BinaryConsumer { - /** - * Instantiate a BinaryConsumer. - */ + /** Instantiate a BinaryConsumer. */ public NonNullableBinaryConsumer(VarBinaryVector vector, int index) { super(vector, index); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BitConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BitConsumer.java index d2d94d0a40e..18ce8fd9746 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BitConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BitConsumer.java @@ -19,19 +19,17 @@ import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.vector.BitVector; /** - * Consumer which consume bit type values from {@link ResultSet}. - * Write the data to {@link BitVector}. + * Consumer which consume bit type values from {@link ResultSet}. Write the data to {@link + * BitVector}. */ public class BitConsumer { - /** - * Creates a consumer for {@link BitVector}. - */ - public static JdbcConsumer createConsumer(BitVector vector, int index, boolean nullable) { + /** Creates a consumer for {@link BitVector}. */ + public static JdbcConsumer createConsumer( + BitVector vector, int index, boolean nullable) { if (nullable) { return new NullableBitConsumer(vector, index); } else { @@ -39,14 +37,10 @@ public static JdbcConsumer createConsumer(BitVector vector, int index } } - /** - * Nullable consumer for {@link BitVector}. - */ + /** Nullable consumer for {@link BitVector}. */ static class NullableBitConsumer extends BaseConsumer { - /** - * Instantiate a BitConsumer. - */ + /** Instantiate a BitConsumer. */ public NullableBitConsumer(BitVector vector, int index) { super(vector, index); } @@ -63,14 +57,10 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Non-nullable consumer for {@link BitVector}. - */ + /** Non-nullable consumer for {@link BitVector}. */ static class NonNullableBitConsumer extends BaseConsumer { - /** - * Instantiate a BitConsumer. - */ + /** Instantiate a BitConsumer. */ public NonNullableBitConsumer(BitVector vector, int index) { super(vector, index); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BlobConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BlobConsumer.java index e57ecdf9170..d2cf0c7f66c 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BlobConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/BlobConsumer.java @@ -21,12 +21,11 @@ import java.sql.Blob; import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.vector.VarBinaryVector; /** - * Consumer which consume blob type values from {@link ResultSet}. - * Write the data to {@link VarBinaryVector}. + * Consumer which consume blob type values from {@link ResultSet}. Write the data to {@link + * VarBinaryVector}. */ public class BlobConsumer extends BaseConsumer { @@ -34,17 +33,12 @@ public class BlobConsumer extends BaseConsumer { private final boolean nullable; - /** - * Creates a consumer for {@link VarBinaryVector}. - */ - public static BlobConsumer createConsumer( - BinaryConsumer delegate, int index, boolean nullable) { + /** Creates a consumer for {@link VarBinaryVector}. */ + public static BlobConsumer createConsumer(BinaryConsumer delegate, int index, boolean nullable) { return new BlobConsumer(delegate, index, nullable); } - /** - * Instantiate a BlobConsumer. - */ + /** Instantiate a BlobConsumer. */ public BlobConsumer(BinaryConsumer delegate, int index, boolean nullable) { super(null, index); this.delegate = delegate; diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/ClobConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/ClobConsumer.java index a52d9b73b4d..454b25416b8 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/ClobConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/ClobConsumer.java @@ -21,21 +21,18 @@ import java.sql.Clob; import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.util.MemoryUtil; import org.apache.arrow.vector.BitVectorHelper; import org.apache.arrow.vector.VarCharVector; /** - * Consumer which consume clob type values from {@link ResultSet}. - * Write the data to {@link org.apache.arrow.vector.VarCharVector}. + * Consumer which consume clob type values from {@link ResultSet}. Write the data to {@link + * org.apache.arrow.vector.VarCharVector}. */ public abstract class ClobConsumer extends BaseConsumer { - /** - * Creates a consumer for {@link VarCharVector}. - */ + /** Creates a consumer for {@link VarCharVector}. */ public static ClobConsumer createConsumer(VarCharVector vector, int index, boolean nullable) { if (nullable) { return new NullableClobConsumer(vector, index); @@ -46,9 +43,7 @@ public static ClobConsumer createConsumer(VarCharVector vector, int index, boole private static final int BUFFER_SIZE = 256; - /** - * Instantiate a ClobConsumer. - */ + /** Instantiate a ClobConsumer. */ public ClobConsumer(VarCharVector vector, int index) { super(vector, index); if (vector != null) { @@ -63,14 +58,10 @@ public void resetValueVector(VarCharVector vector) { this.currentIndex = 0; } - /** - * Nullable consumer for clob data. - */ + /** Nullable consumer for clob data. */ static class NullableClobConsumer extends ClobConsumer { - - /** - * Instantiate a ClobConsumer. - */ + + /** Instantiate a ClobConsumer. */ public NullableClobConsumer(VarCharVector vector, int index) { super(vector, index); } @@ -97,11 +88,11 @@ public void consume(ResultSet resultSet) throws SQLException { vector.reallocDataBuffer(); } MemoryUtil.UNSAFE.copyMemory( - bytes, - MemoryUtil.BYTE_ARRAY_BASE_OFFSET, - null, - dataBuffer.memoryAddress() + startIndex + totalBytes, - bytes.length); + bytes, + MemoryUtil.BYTE_ARRAY_BASE_OFFSET, + null, + dataBuffer.memoryAddress() + startIndex + totalBytes, + bytes.length); totalBytes += bytes.length; read += readSize; @@ -115,14 +106,10 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Non-nullable consumer for clob data. - */ + /** Non-nullable consumer for clob data. */ static class NonNullableClobConsumer extends ClobConsumer { - /** - * Instantiate a ClobConsumer. - */ + /** Instantiate a ClobConsumer. */ public NonNullableClobConsumer(VarCharVector vector, int index) { super(vector, index); } @@ -148,11 +135,11 @@ public void consume(ResultSet resultSet) throws SQLException { vector.reallocDataBuffer(); } MemoryUtil.UNSAFE.copyMemory( - bytes, - MemoryUtil.BYTE_ARRAY_BASE_OFFSET, - null, - dataBuffer.memoryAddress() + startIndex + totalBytes, - bytes.length); + bytes, + MemoryUtil.BYTE_ARRAY_BASE_OFFSET, + null, + dataBuffer.memoryAddress() + startIndex + totalBytes, + bytes.length); totalBytes += bytes.length; read += readSize; @@ -161,7 +148,7 @@ public void consume(ResultSet resultSet) throws SQLException { BitVectorHelper.setBit(vector.getValidityBuffer(), currentIndex); vector.setLastSet(currentIndex); } - + currentIndex++; } } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/CompositeJdbcConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/CompositeJdbcConsumer.java index e6d780956d5..4e6049b9d83 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/CompositeJdbcConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/CompositeJdbcConsumer.java @@ -20,7 +20,6 @@ import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.adapter.jdbc.JdbcFieldInfo; import org.apache.arrow.adapter.jdbc.consumer.exceptions.JdbcConsumerException; import org.apache.arrow.util.AutoCloseables; @@ -28,17 +27,12 @@ import org.apache.arrow.vector.VectorSchemaRoot; import org.apache.arrow.vector.types.pojo.ArrowType; -/** - * Composite consumer which hold all consumers. - * It manages the consume and cleanup process. - */ +/** Composite consumer which hold all consumers. It manages the consume and cleanup process. */ public class CompositeJdbcConsumer implements JdbcConsumer { private final JdbcConsumer[] consumers; - /** - * Construct an instance. - */ + /** Construct an instance. */ public CompositeJdbcConsumer(JdbcConsumer[] consumers) { this.consumers = consumers; } @@ -51,9 +45,11 @@ public void consume(ResultSet rs) throws SQLException, IOException { } catch (Exception e) { if (consumers[i] instanceof BaseConsumer) { BaseConsumer consumer = (BaseConsumer) consumers[i]; - JdbcFieldInfo fieldInfo = new JdbcFieldInfo(rs.getMetaData(), consumer.columnIndexInResultSet); + JdbcFieldInfo fieldInfo = + new JdbcFieldInfo(rs.getMetaData(), consumer.columnIndexInResultSet); ArrowType arrowType = consumer.vector.getMinorType().getType(); - throw new JdbcConsumerException("Exception while consuming JDBC value", e, fieldInfo, arrowType); + throw new JdbcConsumerException( + "Exception while consuming JDBC value", e, fieldInfo, arrowType); } else { throw e; } @@ -70,17 +66,12 @@ public void close() { } catch (Exception e) { throw new RuntimeException("Error occurred while releasing resources.", e); } - } @Override - public void resetValueVector(ValueVector vector) { + public void resetValueVector(ValueVector vector) {} - } - - /** - * Reset inner consumers through vectors in the vector schema root. - */ + /** Reset inner consumers through vectors in the vector schema root. */ public void resetVectorSchemaRoot(VectorSchemaRoot root) { assert root.getFieldVectors().size() == consumers.length; for (int i = 0; i < consumers.length; i++) { @@ -88,4 +79,3 @@ public void resetVectorSchemaRoot(VectorSchemaRoot root) { } } } - diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DateConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DateConsumer.java index b9b83daccc2..aa69516526c 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DateConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DateConsumer.java @@ -22,19 +22,16 @@ import java.sql.SQLException; import java.util.Calendar; import java.util.concurrent.TimeUnit; - import org.apache.arrow.vector.DateDayVector; import org.apache.arrow.vector.DateMilliVector; /** - * Consumer which consume date type values from {@link ResultSet}. - * Write the data to {@link org.apache.arrow.vector.DateDayVector}. + * Consumer which consume date type values from {@link ResultSet}. Write the data to {@link + * org.apache.arrow.vector.DateDayVector}. */ public class DateConsumer { - /** - * Creates a consumer for {@link DateMilliVector}. - */ + /** Creates a consumer for {@link DateMilliVector}. */ public static JdbcConsumer createConsumer( DateDayVector vector, int index, boolean nullable, Calendar calendar) { if (nullable) { @@ -44,23 +41,17 @@ public static JdbcConsumer createConsumer( } } - /** - * Nullable consumer for date. - */ + /** Nullable consumer for date. */ static class NullableDateConsumer extends BaseConsumer { protected final Calendar calendar; - /** - * Instantiate a DateConsumer. - */ + /** Instantiate a DateConsumer. */ public NullableDateConsumer(DateDayVector vector, int index) { - this(vector, index, /* calendar */null); + this(vector, index, /* calendar */ null); } - /** - * Instantiate a DateConsumer. - */ + /** Instantiate a DateConsumer. */ public NullableDateConsumer(DateDayVector vector, int index, Calendar calendar) { super(vector, index); this.calendar = calendar; @@ -68,8 +59,10 @@ public NullableDateConsumer(DateDayVector vector, int index, Calendar calendar) @Override public void consume(ResultSet resultSet) throws SQLException { - Date date = calendar == null ? resultSet.getDate(columnIndexInResultSet) : - resultSet.getDate(columnIndexInResultSet, calendar); + Date date = + calendar == null + ? resultSet.getDate(columnIndexInResultSet) + : resultSet.getDate(columnIndexInResultSet, calendar); if (!resultSet.wasNull()) { // for fixed width vectors, we have allocated enough memory proactively, // so there is no need to call the setSafe method here. @@ -79,23 +72,17 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Non-nullable consumer for date. - */ + /** Non-nullable consumer for date. */ static class NonNullableDateConsumer extends BaseConsumer { protected final Calendar calendar; - /** - * Instantiate a DateConsumer. - */ + /** Instantiate a DateConsumer. */ public NonNullableDateConsumer(DateDayVector vector, int index) { - this(vector, index, /* calendar */null); + this(vector, index, /* calendar */ null); } - /** - * Instantiate a DateConsumer. - */ + /** Instantiate a DateConsumer. */ public NonNullableDateConsumer(DateDayVector vector, int index, Calendar calendar) { super(vector, index); this.calendar = calendar; @@ -103,8 +90,10 @@ public NonNullableDateConsumer(DateDayVector vector, int index, Calendar calenda @Override public void consume(ResultSet resultSet) throws SQLException { - Date date = calendar == null ? resultSet.getDate(columnIndexInResultSet) : - resultSet.getDate(columnIndexInResultSet, calendar); + Date date = + calendar == null + ? resultSet.getDate(columnIndexInResultSet) + : resultSet.getDate(columnIndexInResultSet, calendar); // for fixed width vectors, we have allocated enough memory proactively, // so there is no need to call the setSafe method here. vector.set(currentIndex, Math.toIntExact(TimeUnit.MILLISECONDS.toDays(date.getTime()))); @@ -112,5 +101,3 @@ public void consume(ResultSet resultSet) throws SQLException { } } } - - diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/Decimal256Consumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/Decimal256Consumer.java index ad00d9b5a24..c670a4248b6 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/Decimal256Consumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/Decimal256Consumer.java @@ -21,12 +21,11 @@ import java.math.RoundingMode; import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.vector.Decimal256Vector; /** - * Consumer which consume decimal type values from {@link ResultSet}. - * Write the data to {@link org.apache.arrow.vector.Decimal256Vector}. + * Consumer which consume decimal type values from {@link ResultSet}. Write the data to {@link + * org.apache.arrow.vector.Decimal256Vector}. */ public abstract class Decimal256Consumer extends BaseConsumer { private final RoundingMode bigDecimalRoundingMode; @@ -36,7 +35,7 @@ public abstract class Decimal256Consumer extends BaseConsumer * Constructs a new consumer. * * @param vector the underlying vector for the consumer. - * @param index the column id for the consumer. + * @param index the column id for the consumer. */ public Decimal256Consumer(Decimal256Vector vector, int index) { this(vector, index, null); @@ -44,27 +43,23 @@ public Decimal256Consumer(Decimal256Vector vector, int index) { /** * Constructs a new consumer, with optional coercibility. + * * @param vector the underlying vector for the consumer. * @param index the column index for the consumer. - * @param bigDecimalRoundingMode java.math.RoundingMode to be applied if the BigDecimal scale does not match that - * of the target vector. Set to null to retain strict matching behavior (scale of - * source and target vector must match exactly). + * @param bigDecimalRoundingMode java.math.RoundingMode to be applied if the BigDecimal scale does + * not match that of the target vector. Set to null to retain strict matching behavior (scale + * of source and target vector must match exactly). */ - public Decimal256Consumer(Decimal256Vector vector, int index, RoundingMode bigDecimalRoundingMode) { + public Decimal256Consumer( + Decimal256Vector vector, int index, RoundingMode bigDecimalRoundingMode) { super(vector, index); this.bigDecimalRoundingMode = bigDecimalRoundingMode; this.scale = vector.getScale(); } - /** - * Creates a consumer for {@link Decimal256Vector}. - */ + /** Creates a consumer for {@link Decimal256Vector}. */ public static JdbcConsumer createConsumer( - Decimal256Vector vector, - int index, - boolean nullable, - RoundingMode bigDecimalRoundingMode - ) { + Decimal256Vector vector, int index, boolean nullable, RoundingMode bigDecimalRoundingMode) { if (nullable) { return new NullableDecimal256Consumer(vector, index, bigDecimalRoundingMode); } else { @@ -79,16 +74,12 @@ protected void set(BigDecimal value) { vector.set(currentIndex, value); } - - /** - * Consumer for nullable decimal. - */ + /** Consumer for nullable decimal. */ static class NullableDecimal256Consumer extends Decimal256Consumer { - /** - * Instantiate a Decimal256Consumer. - */ - public NullableDecimal256Consumer(Decimal256Vector vector, int index, RoundingMode bigDecimalRoundingMode) { + /** Instantiate a Decimal256Consumer. */ + public NullableDecimal256Consumer( + Decimal256Vector vector, int index, RoundingMode bigDecimalRoundingMode) { super(vector, index, bigDecimalRoundingMode); } @@ -104,15 +95,12 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Consumer for non-nullable decimal. - */ + /** Consumer for non-nullable decimal. */ static class NonNullableDecimal256Consumer extends Decimal256Consumer { - /** - * Instantiate a Decimal256Consumer. - */ - public NonNullableDecimal256Consumer(Decimal256Vector vector, int index, RoundingMode bigDecimalRoundingMode) { + /** Instantiate a Decimal256Consumer. */ + public NonNullableDecimal256Consumer( + Decimal256Vector vector, int index, RoundingMode bigDecimalRoundingMode) { super(vector, index, bigDecimalRoundingMode); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DecimalConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DecimalConsumer.java index bed96dda8b6..034cdf14ae6 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DecimalConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DecimalConsumer.java @@ -21,12 +21,11 @@ import java.math.RoundingMode; import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.vector.DecimalVector; /** - * Consumer which consume decimal type values from {@link ResultSet}. - * Write the data to {@link org.apache.arrow.vector.DecimalVector}. + * Consumer which consume decimal type values from {@link ResultSet}. Write the data to {@link + * org.apache.arrow.vector.DecimalVector}. */ public abstract class DecimalConsumer extends BaseConsumer { private final RoundingMode bigDecimalRoundingMode; @@ -36,7 +35,7 @@ public abstract class DecimalConsumer extends BaseConsumer { * Constructs a new consumer. * * @param vector the underlying vector for the consumer. - * @param index the column id for the consumer. + * @param index the column id for the consumer. */ public DecimalConsumer(DecimalVector vector, int index) { this(vector, index, null); @@ -44,11 +43,12 @@ public DecimalConsumer(DecimalVector vector, int index) { /** * Constructs a new consumer, with optional coercibility. + * * @param vector the underlying vector for the consumer. * @param index the column index for the consumer. - * @param bigDecimalRoundingMode java.math.RoundingMode to be applied if the BigDecimal scale does not match that - * of the target vector. Set to null to retain strict matching behavior (scale of - * source and target vector must match exactly). + * @param bigDecimalRoundingMode java.math.RoundingMode to be applied if the BigDecimal scale does + * not match that of the target vector. Set to null to retain strict matching behavior (scale + * of source and target vector must match exactly). */ public DecimalConsumer(DecimalVector vector, int index, RoundingMode bigDecimalRoundingMode) { super(vector, index); @@ -56,15 +56,9 @@ public DecimalConsumer(DecimalVector vector, int index, RoundingMode bigDecimalR this.scale = vector.getScale(); } - /** - * Creates a consumer for {@link DecimalVector}. - */ + /** Creates a consumer for {@link DecimalVector}. */ public static JdbcConsumer createConsumer( - DecimalVector vector, - int index, - boolean nullable, - RoundingMode bigDecimalRoundingMode - ) { + DecimalVector vector, int index, boolean nullable, RoundingMode bigDecimalRoundingMode) { if (nullable) { return new NullableDecimalConsumer(vector, index, bigDecimalRoundingMode); } else { @@ -79,16 +73,12 @@ protected void set(BigDecimal value) { vector.set(currentIndex, value); } - - /** - * Consumer for nullable decimal. - */ + /** Consumer for nullable decimal. */ static class NullableDecimalConsumer extends DecimalConsumer { - /** - * Instantiate a DecimalConsumer. - */ - public NullableDecimalConsumer(DecimalVector vector, int index, RoundingMode bigDecimalRoundingMode) { + /** Instantiate a DecimalConsumer. */ + public NullableDecimalConsumer( + DecimalVector vector, int index, RoundingMode bigDecimalRoundingMode) { super(vector, index, bigDecimalRoundingMode); } @@ -104,15 +94,12 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Consumer for non-nullable decimal. - */ + /** Consumer for non-nullable decimal. */ static class NonNullableDecimalConsumer extends DecimalConsumer { - /** - * Instantiate a DecimalConsumer. - */ - public NonNullableDecimalConsumer(DecimalVector vector, int index, RoundingMode bigDecimalRoundingMode) { + /** Instantiate a DecimalConsumer. */ + public NonNullableDecimalConsumer( + DecimalVector vector, int index, RoundingMode bigDecimalRoundingMode) { super(vector, index, bigDecimalRoundingMode); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DoubleConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DoubleConsumer.java index e3db95d1535..79cb4f65bb6 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DoubleConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DoubleConsumer.java @@ -19,19 +19,17 @@ import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.vector.Float8Vector; /** - * Consumer which consume double type values from {@link ResultSet}. - * Write the data to {@link org.apache.arrow.vector.Float8Vector}. + * Consumer which consume double type values from {@link ResultSet}. Write the data to {@link + * org.apache.arrow.vector.Float8Vector}. */ public class DoubleConsumer { - /** - * Creates a consumer for {@link Float8Vector}. - */ - public static JdbcConsumer createConsumer(Float8Vector vector, int index, boolean nullable) { + /** Creates a consumer for {@link Float8Vector}. */ + public static JdbcConsumer createConsumer( + Float8Vector vector, int index, boolean nullable) { if (nullable) { return new NullableDoubleConsumer(vector, index); } else { @@ -39,14 +37,10 @@ public static JdbcConsumer createConsumer(Float8Vector vector, int } } - /** - * Nullable double consumer. - */ + /** Nullable double consumer. */ static class NullableDoubleConsumer extends BaseConsumer { - /** - * Instantiate a DoubleConsumer. - */ + /** Instantiate a DoubleConsumer. */ public NullableDoubleConsumer(Float8Vector vector, int index) { super(vector, index); } @@ -63,14 +57,10 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Non-nullable double consumer. - */ + /** Non-nullable double consumer. */ static class NonNullableDoubleConsumer extends BaseConsumer { - /** - * Instantiate a DoubleConsumer. - */ + /** Instantiate a DoubleConsumer. */ public NonNullableDoubleConsumer(Float8Vector vector, int index) { super(vector, index); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/FloatConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/FloatConsumer.java index 830348fe94c..70b6e50e483 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/FloatConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/FloatConsumer.java @@ -19,19 +19,17 @@ import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.vector.Float4Vector; /** - * Consumer which consume float type values from {@link ResultSet}. - * Write the data to {@link org.apache.arrow.vector.Float4Vector}. + * Consumer which consume float type values from {@link ResultSet}. Write the data to {@link + * org.apache.arrow.vector.Float4Vector}. */ public class FloatConsumer { - /** - * Creates a consumer for {@link Float4Vector}. - */ - public static JdbcConsumer createConsumer(Float4Vector vector, int index, boolean nullable) { + /** Creates a consumer for {@link Float4Vector}. */ + public static JdbcConsumer createConsumer( + Float4Vector vector, int index, boolean nullable) { if (nullable) { return new NullableFloatConsumer(vector, index); } else { @@ -39,14 +37,10 @@ public static JdbcConsumer createConsumer(Float4Vector vector, int } } - /** - * Nullable float consumer. - */ + /** Nullable float consumer. */ static class NullableFloatConsumer extends BaseConsumer { - /** - * Instantiate a FloatConsumer. - */ + /** Instantiate a FloatConsumer. */ public NullableFloatConsumer(Float4Vector vector, int index) { super(vector, index); } @@ -63,14 +57,10 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Non-nullable float consumer. - */ + /** Non-nullable float consumer. */ static class NonNullableFloatConsumer extends BaseConsumer { - /** - * Instantiate a FloatConsumer. - */ + /** Instantiate a FloatConsumer. */ public NonNullableFloatConsumer(Float4Vector vector, int index) { super(vector, index); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/IntConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/IntConsumer.java index 4e537d682ff..883cc1b8ffa 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/IntConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/IntConsumer.java @@ -19,19 +19,17 @@ import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.vector.IntVector; /** - * Consumer which consume int type values from {@link ResultSet}. - * Write the data to {@link org.apache.arrow.vector.IntVector}. + * Consumer which consume int type values from {@link ResultSet}. Write the data to {@link + * org.apache.arrow.vector.IntVector}. */ public class IntConsumer { - /** - * Creates a consumer for {@link IntVector}. - */ - public static JdbcConsumer createConsumer(IntVector vector, int index, boolean nullable) { + /** Creates a consumer for {@link IntVector}. */ + public static JdbcConsumer createConsumer( + IntVector vector, int index, boolean nullable) { if (nullable) { return new NullableIntConsumer(vector, index); } else { @@ -39,14 +37,10 @@ public static JdbcConsumer createConsumer(IntVector vector, int index } } - /** - * Nullable consumer for int. - */ + /** Nullable consumer for int. */ static class NullableIntConsumer extends BaseConsumer { - /** - * Instantiate a IntConsumer. - */ + /** Instantiate a IntConsumer. */ public NullableIntConsumer(IntVector vector, int index) { super(vector, index); } @@ -63,14 +57,10 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Non-nullable consumer for int. - */ + /** Non-nullable consumer for int. */ static class NonNullableIntConsumer extends BaseConsumer { - /** - * Instantiate a IntConsumer. - */ + /** Instantiate a IntConsumer. */ public NonNullableIntConsumer(IntVector vector, int index) { super(vector, index); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/JdbcConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/JdbcConsumer.java index 480dfe3a1c5..4d20e27d1d6 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/JdbcConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/JdbcConsumer.java @@ -20,27 +20,21 @@ import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.vector.ValueVector; /** * An abstraction that is used to consume values from {@link ResultSet}. + * * @param The vector within consumer or its delegate, used for partially consume purpose. */ public interface JdbcConsumer extends AutoCloseable { - /** - * Consume a specific type value from {@link ResultSet} and write it to vector. - */ + /** Consume a specific type value from {@link ResultSet} and write it to vector. */ void consume(ResultSet resultSet) throws SQLException, IOException; - /** - * Close this consumer, do some clean work such as clear reuse ArrowBuf. - */ + /** Close this consumer, do some clean work such as clear reuse ArrowBuf. */ void close() throws Exception; - /** - * Reset the vector within consumer for partial read purpose. - */ + /** Reset the vector within consumer for partial read purpose. */ void resetValueVector(T vector); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/MapConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/MapConsumer.java index 07a071bfc09..b556c14e9e6 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/MapConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/MapConsumer.java @@ -17,43 +17,37 @@ package org.apache.arrow.adapter.jdbc.consumer; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Map; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.complex.MapVector; import org.apache.arrow.vector.complex.impl.UnionMapWriter; import org.apache.arrow.vector.util.ObjectMapperFactory; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - /** - * Consumer which consume map type values from {@link ResultSet}. - * Write the data into {@link org.apache.arrow.vector.complex.MapVector}. + * Consumer which consume map type values from {@link ResultSet}. Write the data into {@link + * org.apache.arrow.vector.complex.MapVector}. */ public class MapConsumer extends BaseConsumer { - private final UnionMapWriter writer; private final ObjectMapper objectMapper = ObjectMapperFactory.newObjectMapper(); - private final TypeReference> typeReference = new TypeReference>() {}; + private final TypeReference> typeReference = + new TypeReference>() {}; private int currentRow; - /** - * Creates a consumer for {@link MapVector}. - */ + /** Creates a consumer for {@link MapVector}. */ public static MapConsumer createConsumer(MapVector mapVector, int index, boolean nullable) { return new MapConsumer(mapVector, index); } - /** - * Instantiate a MapConsumer. - */ + /** Instantiate a MapConsumer. */ public MapConsumer(MapVector vector, int index) { super(vector, index); writer = vector.getWriter(); @@ -69,7 +63,8 @@ public void consume(ResultSet resultSet) throws SQLException, IOException { } else if (map instanceof Map) { writeJavaMapIntoVector((Map) map); } else { - throw new IllegalArgumentException("Unknown type of map type column from JDBC " + map.getClass().getName()); + throw new IllegalArgumentException( + "Unknown type of map type column from JDBC " + map.getClass().getName()); } } else { writer.writeNull(); @@ -79,26 +74,25 @@ public void consume(ResultSet resultSet) throws SQLException, IOException { private void writeJavaMapIntoVector(Map map) { BufferAllocator allocator = vector.getAllocator(); writer.startMap(); - map.forEach((key, value) -> { - byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8); - byte[] valueBytes = value != null ? value.getBytes(StandardCharsets.UTF_8) : null; - try ( - ArrowBuf keyBuf = allocator.buffer(keyBytes.length); - ArrowBuf valueBuf = valueBytes != null ? allocator.buffer(valueBytes.length) : null; - ) { - writer.startEntry(); - keyBuf.writeBytes(keyBytes); - writer.key().varChar().writeVarChar(0, keyBytes.length, keyBuf); - if (valueBytes != null) { - valueBuf.writeBytes(valueBytes); - writer.value().varChar().writeVarChar(0, valueBytes.length, valueBuf); - } else { - writer.value().varChar().writeNull(); - } - writer.endEntry(); - } - }); + map.forEach( + (key, value) -> { + byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8); + byte[] valueBytes = value != null ? value.getBytes(StandardCharsets.UTF_8) : null; + try (ArrowBuf keyBuf = allocator.buffer(keyBytes.length); + ArrowBuf valueBuf = + valueBytes != null ? allocator.buffer(valueBytes.length) : null; ) { + writer.startEntry(); + keyBuf.writeBytes(keyBytes); + writer.key().varChar().writeVarChar(0, keyBytes.length, keyBuf); + if (valueBytes != null) { + valueBuf.writeBytes(valueBytes); + writer.value().varChar().writeVarChar(0, valueBytes.length, valueBuf); + } else { + writer.value().varChar().writeNull(); + } + writer.endEntry(); + } + }); writer.endMap(); } } - diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/NullConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/NullConsumer.java index a79a029f45d..5b9aee84212 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/NullConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/NullConsumer.java @@ -19,12 +19,11 @@ import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.vector.NullVector; /** - * Consumer which consume null type values from ResultSet. - * Corresponding to {@link org.apache.arrow.vector.NullVector}. + * Consumer which consume null type values from ResultSet. Corresponding to {@link + * org.apache.arrow.vector.NullVector}. */ public class NullConsumer extends BaseConsumer { @@ -33,6 +32,5 @@ public NullConsumer(NullVector vector) { } @Override - public void consume(ResultSet resultSet) throws SQLException { - } + public void consume(ResultSet resultSet) throws SQLException {} } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/SmallIntConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/SmallIntConsumer.java index 2edb3605b17..0fe3eb3d2df 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/SmallIntConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/SmallIntConsumer.java @@ -19,19 +19,17 @@ import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.vector.SmallIntVector; /** - * Consumer which consume smallInt type values from {@link ResultSet}. - * Write the data to {@link org.apache.arrow.vector.SmallIntVector}. + * Consumer which consume smallInt type values from {@link ResultSet}. Write the data to {@link + * org.apache.arrow.vector.SmallIntVector}. */ public class SmallIntConsumer { - /** - * Creates a consumer for {@link SmallIntVector}. - */ - public static BaseConsumer createConsumer(SmallIntVector vector, int index, boolean nullable) { + /** Creates a consumer for {@link SmallIntVector}. */ + public static BaseConsumer createConsumer( + SmallIntVector vector, int index, boolean nullable) { if (nullable) { return new NullableSmallIntConsumer(vector, index); } else { @@ -39,14 +37,10 @@ public static BaseConsumer createConsumer(SmallIntVector vector, } } - /** - * Nullable consumer for small int. - */ + /** Nullable consumer for small int. */ static class NullableSmallIntConsumer extends BaseConsumer { - /** - * Instantiate a SmallIntConsumer. - */ + /** Instantiate a SmallIntConsumer. */ public NullableSmallIntConsumer(SmallIntVector vector, int index) { super(vector, index); } @@ -63,14 +57,10 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Non-nullable consumer for small int. - */ + /** Non-nullable consumer for small int. */ static class NonNullableSmallIntConsumer extends BaseConsumer { - /** - * Instantiate a SmallIntConsumer. - */ + /** Instantiate a SmallIntConsumer. */ public NonNullableSmallIntConsumer(SmallIntVector vector, int index) { super(vector, index); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TimeConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TimeConsumer.java index 4fa15ad7903..d5b647a6189 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TimeConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TimeConsumer.java @@ -21,20 +21,17 @@ import java.sql.SQLException; import java.sql.Time; import java.util.Calendar; - import org.apache.arrow.vector.TimeMilliVector; /** - * Consumer which consume time type values from {@link ResultSet}. - * Write the data to {@link org.apache.arrow.vector.TimeMilliVector}. + * Consumer which consume time type values from {@link ResultSet}. Write the data to {@link + * org.apache.arrow.vector.TimeMilliVector}. */ public abstract class TimeConsumer { - /** - * Creates a consumer for {@link TimeMilliVector}. - */ + /** Creates a consumer for {@link TimeMilliVector}. */ public static JdbcConsumer createConsumer( - TimeMilliVector vector, int index, boolean nullable, Calendar calendar) { + TimeMilliVector vector, int index, boolean nullable, Calendar calendar) { if (nullable) { return new NullableTimeConsumer(vector, index, calendar); } else { @@ -42,23 +39,17 @@ public static JdbcConsumer createConsumer( } } - /** - * Nullable consumer for {@link TimeMilliVector}. - */ + /** Nullable consumer for {@link TimeMilliVector}. */ static class NullableTimeConsumer extends BaseConsumer { protected final Calendar calendar; - /** - * Instantiate a TimeConsumer. - */ + /** Instantiate a TimeConsumer. */ public NullableTimeConsumer(TimeMilliVector vector, int index) { - this(vector, index, /* calendar */null); + this(vector, index, /* calendar */ null); } - /** - * Instantiate a TimeConsumer. - */ + /** Instantiate a TimeConsumer. */ public NullableTimeConsumer(TimeMilliVector vector, int index, Calendar calendar) { super(vector, index); this.calendar = calendar; @@ -66,8 +57,10 @@ public NullableTimeConsumer(TimeMilliVector vector, int index, Calendar calendar @Override public void consume(ResultSet resultSet) throws SQLException { - Time time = calendar == null ? resultSet.getTime(columnIndexInResultSet) : - resultSet.getTime(columnIndexInResultSet, calendar); + Time time = + calendar == null + ? resultSet.getTime(columnIndexInResultSet) + : resultSet.getTime(columnIndexInResultSet, calendar); if (!resultSet.wasNull()) { // for fixed width vectors, we have allocated enough memory proactively, // so there is no need to call the setSafe method here. @@ -77,23 +70,17 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Non-nullable consumer for {@link TimeMilliVector}. - */ + /** Non-nullable consumer for {@link TimeMilliVector}. */ static class NonNullableTimeConsumer extends BaseConsumer { protected final Calendar calendar; - /** - * Instantiate a TimeConsumer. - */ + /** Instantiate a TimeConsumer. */ public NonNullableTimeConsumer(TimeMilliVector vector, int index) { - this(vector, index, /* calendar */null); + this(vector, index, /* calendar */ null); } - /** - * Instantiate a TimeConsumer. - */ + /** Instantiate a TimeConsumer. */ public NonNullableTimeConsumer(TimeMilliVector vector, int index, Calendar calendar) { super(vector, index); this.calendar = calendar; @@ -101,8 +88,10 @@ public NonNullableTimeConsumer(TimeMilliVector vector, int index, Calendar calen @Override public void consume(ResultSet resultSet) throws SQLException { - Time time = calendar == null ? resultSet.getTime(columnIndexInResultSet) : - resultSet.getTime(columnIndexInResultSet, calendar); + Time time = + calendar == null + ? resultSet.getTime(columnIndexInResultSet) + : resultSet.getTime(columnIndexInResultSet, calendar); // for fixed width vectors, we have allocated enough memory proactively, // so there is no need to call the setSafe method here. vector.set(currentIndex, (int) time.getTime()); diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TimestampConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TimestampConsumer.java index 3351e7e78a7..0b96092465c 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TimestampConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TimestampConsumer.java @@ -20,20 +20,17 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; - import org.apache.arrow.vector.TimeStampMilliVector; /** - * Consumer which consume timestamp type values from {@link ResultSet}. - * Write the data to {@link TimeStampMilliVector}. + * Consumer which consume timestamp type values from {@link ResultSet}. Write the data to {@link + * TimeStampMilliVector}. */ public abstract class TimestampConsumer { - /** - * Creates a consumer for {@link TimeStampMilliVector}. - */ + /** Creates a consumer for {@link TimeStampMilliVector}. */ public static JdbcConsumer createConsumer( - TimeStampMilliVector vector, int index, boolean nullable) { + TimeStampMilliVector vector, int index, boolean nullable) { if (nullable) { return new NullableTimestampConsumer(vector, index); } else { @@ -41,14 +38,10 @@ public static JdbcConsumer createConsumer( } } - /** - * Nullable consumer for timestamp. - */ + /** Nullable consumer for timestamp. */ static class NullableTimestampConsumer extends BaseConsumer { - /** - * Instantiate a TimestampConsumer. - */ + /** Instantiate a TimestampConsumer. */ public NullableTimestampConsumer(TimeStampMilliVector vector, int index) { super(vector, index); } @@ -65,14 +58,10 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Non-nullable consumer for timestamp. - */ + /** Non-nullable consumer for timestamp. */ static class NonNullableTimestampConsumer extends BaseConsumer { - /** - * Instantiate a TimestampConsumer. - */ + /** Instantiate a TimestampConsumer. */ public NonNullableTimestampConsumer(TimeStampMilliVector vector, int index) { super(vector, index); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TimestampTZConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TimestampTZConsumer.java index f08671f0be6..c55b6669326 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TimestampTZConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TimestampTZConsumer.java @@ -21,18 +21,15 @@ import java.sql.SQLException; import java.sql.Timestamp; import java.util.Calendar; - import org.apache.arrow.util.Preconditions; import org.apache.arrow.vector.TimeStampMilliTZVector; /** - * Consumer which consume timestamp (with time zone) type values from {@link ResultSet}. - * Write the data to {@link TimeStampMilliTZVector}. + * Consumer which consume timestamp (with time zone) type values from {@link ResultSet}. Write the + * data to {@link TimeStampMilliTZVector}. */ public class TimestampTZConsumer { - /** - * Creates a consumer for {@link TimeStampMilliTZVector}. - */ + /** Creates a consumer for {@link TimeStampMilliTZVector}. */ public static JdbcConsumer createConsumer( TimeStampMilliTZVector vector, int index, boolean nullable, Calendar calendar) { Preconditions.checkArgument(calendar != null, "Calendar cannot be null"); @@ -43,17 +40,14 @@ public static JdbcConsumer createConsumer( } } - /** - * Nullable consumer for timestamp (with time zone). - */ + /** Nullable consumer for timestamp (with time zone). */ static class NullableTimestampTZConsumer extends BaseConsumer { protected final Calendar calendar; - /** - * Instantiate a TimestampConsumer. - */ - public NullableTimestampTZConsumer(TimeStampMilliTZVector vector, int index, Calendar calendar) { + /** Instantiate a TimestampConsumer. */ + public NullableTimestampTZConsumer( + TimeStampMilliTZVector vector, int index, Calendar calendar) { super(vector, index); this.calendar = calendar; } @@ -70,17 +64,14 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Non-nullable consumer for timestamp (with time zone). - */ + /** Non-nullable consumer for timestamp (with time zone). */ static class NonNullableTimestampConsumer extends BaseConsumer { protected final Calendar calendar; - /** - * Instantiate a TimestampConsumer. - */ - public NonNullableTimestampConsumer(TimeStampMilliTZVector vector, int index, Calendar calendar) { + /** Instantiate a TimestampConsumer. */ + public NonNullableTimestampConsumer( + TimeStampMilliTZVector vector, int index, Calendar calendar) { super(vector, index); this.calendar = calendar; } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TinyIntConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TinyIntConsumer.java index 40cf087a5ec..1191f6ac8e7 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TinyIntConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/TinyIntConsumer.java @@ -19,19 +19,17 @@ import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.vector.TinyIntVector; /** - * Consumer which consume tinyInt type values from {@link ResultSet}. - * Write the data to {@link org.apache.arrow.vector.TinyIntVector}. + * Consumer which consume tinyInt type values from {@link ResultSet}. Write the data to {@link + * org.apache.arrow.vector.TinyIntVector}. */ public abstract class TinyIntConsumer { - /** - * Creates a consumer for {@link TinyIntVector}. - */ - public static JdbcConsumer createConsumer(TinyIntVector vector, int index, boolean nullable) { + /** Creates a consumer for {@link TinyIntVector}. */ + public static JdbcConsumer createConsumer( + TinyIntVector vector, int index, boolean nullable) { if (nullable) { return new NullableTinyIntConsumer(vector, index); } else { @@ -39,14 +37,10 @@ public static JdbcConsumer createConsumer(TinyIntVector vector, i } } - /** - * Nullable consumer for tiny int. - */ + /** Nullable consumer for tiny int. */ static class NullableTinyIntConsumer extends BaseConsumer { - /** - * Instantiate a TinyIntConsumer. - */ + /** Instantiate a TinyIntConsumer. */ public NullableTinyIntConsumer(TinyIntVector vector, int index) { super(vector, index); } @@ -63,14 +57,10 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Non-nullable consumer for tiny int. - */ + /** Non-nullable consumer for tiny int. */ static class NonNullableTinyIntConsumer extends BaseConsumer { - /** - * Instantiate a TinyIntConsumer. - */ + /** Instantiate a TinyIntConsumer. */ public NonNullableTinyIntConsumer(TinyIntVector vector, int index) { super(vector, index); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/VarCharConsumer.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/VarCharConsumer.java index 05333715b8c..2a9f87b0cbe 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/VarCharConsumer.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/VarCharConsumer.java @@ -20,19 +20,17 @@ import java.nio.charset.StandardCharsets; import java.sql.ResultSet; import java.sql.SQLException; - import org.apache.arrow.vector.VarCharVector; /** - * Consumer which consume varchar type values from {@link ResultSet}. - * Write the data to {@link org.apache.arrow.vector.VarCharVector}. + * Consumer which consume varchar type values from {@link ResultSet}. Write the data to {@link + * org.apache.arrow.vector.VarCharVector}. */ public abstract class VarCharConsumer { - /** - * Creates a consumer for {@link VarCharVector}. - */ - public static JdbcConsumer createConsumer(VarCharVector vector, int index, boolean nullable) { + /** Creates a consumer for {@link VarCharVector}. */ + public static JdbcConsumer createConsumer( + VarCharVector vector, int index, boolean nullable) { if (nullable) { return new NullableVarCharConsumer(vector, index); } else { @@ -40,14 +38,10 @@ public static JdbcConsumer createConsumer(VarCharVector vector, i } } - /** - * Nullable consumer for var char. - */ + /** Nullable consumer for var char. */ static class NullableVarCharConsumer extends BaseConsumer { - /** - * Instantiate a VarCharConsumer. - */ + /** Instantiate a VarCharConsumer. */ public NullableVarCharConsumer(VarCharVector vector, int index) { super(vector, index); } @@ -63,14 +57,10 @@ public void consume(ResultSet resultSet) throws SQLException { } } - /** - * Non-nullable consumer for var char. - */ + /** Non-nullable consumer for var char. */ static class NonNullableVarCharConsumer extends BaseConsumer { - /** - * Instantiate a VarCharConsumer. - */ + /** Instantiate a VarCharConsumer. */ public NonNullableVarCharConsumer(VarCharVector vector, int index) { super(vector, index); } diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/exceptions/JdbcConsumerException.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/exceptions/JdbcConsumerException.java index b235be173cf..52b9a278b1f 100644 --- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/exceptions/JdbcConsumerException.java +++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/exceptions/JdbcConsumerException.java @@ -21,8 +21,8 @@ import org.apache.arrow.vector.types.pojo.ArrowType; /** - * Exception while consuming JDBC data. This exception stores the JdbcFieldInfo for the column and the - * ArrowType for the corresponding vector for easier debugging. + * Exception while consuming JDBC data. This exception stores the JdbcFieldInfo for the column and + * the ArrowType for the corresponding vector for easier debugging. */ public class JdbcConsumerException extends RuntimeException { final JdbcFieldInfo fieldInfo; @@ -31,12 +31,13 @@ public class JdbcConsumerException extends RuntimeException { /** * Construct JdbcConsumerException with all fields. * - * @param message error message - * @param cause original exception + * @param message error message + * @param cause original exception * @param fieldInfo JdbcFieldInfo for the column * @param arrowType ArrowType for the corresponding vector */ - public JdbcConsumerException(String message, Throwable cause, JdbcFieldInfo fieldInfo, ArrowType arrowType) { + public JdbcConsumerException( + String message, Throwable cause, JdbcFieldInfo fieldInfo, ArrowType arrowType) { super(message, cause); this.fieldInfo = fieldInfo; this.arrowType = arrowType; diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/AbstractJdbcToArrowTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/AbstractJdbcToArrowTest.java index 88a66a31aa2..8811844bed9 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/AbstractJdbcToArrowTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/AbstractJdbcToArrowTest.java @@ -17,6 +17,8 @@ package org.apache.arrow.adapter.jdbc; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; @@ -30,7 +32,6 @@ import java.util.Map; import java.util.TimeZone; import java.util.function.Function; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.util.Preconditions; @@ -41,12 +42,7 @@ import org.junit.Before; import org.junit.Test; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; - -/** - * Class to abstract out some common test functionality for testing JDBC to Arrow. - */ +/** Class to abstract out some common test functionality for testing JDBC to Arrow. */ public abstract class AbstractJdbcToArrowTest { protected static final String BIGINT = "BIGINT_FIELD5"; @@ -69,7 +65,8 @@ public abstract class AbstractJdbcToArrowTest { protected static final String TINYINT = "TINYINT_FIELD3"; protected static final String VARCHAR = "VARCHAR_FIELD13"; protected static final String NULL = "NULL_FIELD18"; - protected static final Map ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP = new HashMap<>(); + protected static final Map ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP = + new HashMap<>(); static { ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP.put(LIST, new JdbcFieldInfo(Types.INTEGER)); @@ -86,12 +83,12 @@ public abstract class AbstractJdbcToArrowTest { * @return Table object * @throws IOException on error */ - protected static Table getTable(String ymlFilePath, @SuppressWarnings("rawtypes") Class clss) throws IOException { - return new ObjectMapper(new YAMLFactory()).readValue( - clss.getClassLoader().getResourceAsStream(ymlFilePath), Table.class); + protected static Table getTable(String ymlFilePath, @SuppressWarnings("rawtypes") Class clss) + throws IOException { + return new ObjectMapper(new YAMLFactory()) + .readValue(clss.getClassLoader().getResourceAsStream(ymlFilePath), Table.class); } - /** * This method creates Connection object and DB table and also populate data into table for test. * @@ -105,7 +102,7 @@ public void setUp() throws SQLException, ClassNotFoundException { String driver = "org.h2.Driver"; Class.forName(driver); conn = DriverManager.getConnection(url); - try (Statement stmt = conn.createStatement();) { + try (Statement stmt = conn.createStatement(); ) { stmt.executeUpdate(table.getCreate()); for (String insert : table.getData()) { stmt.executeUpdate(insert); @@ -136,12 +133,13 @@ public void destroy() throws SQLException { * @throws ClassNotFoundException on error * @throws IOException on error */ - public static Object[][] prepareTestData(String[] testFiles, @SuppressWarnings("rawtypes") Class clss) + public static Object[][] prepareTestData( + String[] testFiles, @SuppressWarnings("rawtypes") Class clss) throws SQLException, ClassNotFoundException, IOException { Object[][] tableArr = new Object[testFiles.length][]; int i = 0; for (String testFile : testFiles) { - tableArr[i++] = new Object[]{getTable(testFile, clss)}; + tableArr[i++] = new Object[] {getTable(testFile, clss)}; } return tableArr; } @@ -159,86 +157,90 @@ public static Object[][] prepareTestData(String[] testFiles, @SuppressWarnings(" * Abstract method to implement logic to assert test various datatype values. * * @param root VectorSchemaRoot for test - * @param isIncludeMapVector is this dataset checks includes map column. - * Jdbc type to 'map' mapping declared in configuration only manually + * @param isIncludeMapVector is this dataset checks includes map column. Jdbc type to 'map' + * mapping declared in configuration only manually */ public abstract void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector); /** - * For the given SQL query, execute and fetch the data from Relational DB and convert it to Arrow objects. - * This method uses the default Calendar instance with default TimeZone and Locale as returned by the JVM. - * If you wish to use specific TimeZone or Locale for any Date, Time and Timestamp datasets, you may want use - * overloaded API that taken Calendar object instance. + * For the given SQL query, execute and fetch the data from Relational DB and convert it to Arrow + * objects. This method uses the default Calendar instance with default TimeZone and Locale as + * returned by the JVM. If you wish to use specific TimeZone or Locale for any Date, Time and + * Timestamp datasets, you may want use overloaded API that taken Calendar object instance. * - * This method is for test only. + *

This method is for test only. * - * @param connection Database connection to be used. This method will not close the passed connection object. Since - * the caller has passed the connection object it's the responsibility of the caller to close or - * return the connection to the pool. - * @param query The DB Query to fetch the data. - * @param allocator Memory allocator + * @param connection Database connection to be used. This method will not close the passed + * connection object. Since the caller has passed the connection object it's the + * responsibility of the caller to close or return the connection to the pool. + * @param query The DB Query to fetch the data. + * @param allocator Memory allocator * @return Arrow Data Objects {@link VectorSchemaRoot} - * @throws SQLException Propagate any SQL Exceptions to the caller after closing any resources opened such as - * ResultSet and Statement objects. + * @throws SQLException Propagate any SQL Exceptions to the caller after closing any resources + * opened such as ResultSet and Statement objects. */ public VectorSchemaRoot sqlToArrow(Connection connection, String query, BufferAllocator allocator) throws SQLException, IOException { Preconditions.checkNotNull(allocator, "Memory allocator object cannot be null"); - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(allocator, JdbcToArrowUtils.getUtcCalendar()) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder(allocator, JdbcToArrowUtils.getUtcCalendar()) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build(); return sqlToArrow(connection, query, config); } /** - * For the given SQL query, execute and fetch the data from Relational DB and convert it to Arrow objects. + * For the given SQL query, execute and fetch the data from Relational DB and convert it to Arrow + * objects. * - * This method is for test only. + *

This method is for test only. * - * @param connection Database connection to be used. This method will not close the passed connection object. Since - * the caller has passed the connection object it's the responsibility of the caller to close or - * return the connection to the pool. - * @param query The DB Query to fetch the data. - * @param allocator Memory allocator - * @param calendar Calendar object to use to handle Date, Time and Timestamp datasets. + * @param connection Database connection to be used. This method will not close the passed + * connection object. Since the caller has passed the connection object it's the + * responsibility of the caller to close or return the connection to the pool. + * @param query The DB Query to fetch the data. + * @param allocator Memory allocator + * @param calendar Calendar object to use to handle Date, Time and Timestamp datasets. * @return Arrow Data Objects {@link VectorSchemaRoot} - * @throws SQLException Propagate any SQL Exceptions to the caller after closing any resources opened such as - * ResultSet and Statement objects. + * @throws SQLException Propagate any SQL Exceptions to the caller after closing any resources + * opened such as ResultSet and Statement objects. */ public VectorSchemaRoot sqlToArrow( - Connection connection, - String query, - BufferAllocator allocator, - Calendar calendar) throws SQLException, IOException { + Connection connection, String query, BufferAllocator allocator, Calendar calendar) + throws SQLException, IOException { Preconditions.checkNotNull(allocator, "Memory allocator object cannot be null"); Preconditions.checkNotNull(calendar, "Calendar object cannot be null"); - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(allocator, calendar) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder(allocator, calendar) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build(); return sqlToArrow(connection, query, config); } /** - * For the given SQL query, execute and fetch the data from Relational DB and convert it to Arrow objects. + * For the given SQL query, execute and fetch the data from Relational DB and convert it to Arrow + * objects. * - * This method is for test only. + *

This method is for test only. * - * @param connection Database connection to be used. This method will not close the passed connection object. - * Since the caller has passed the connection object it's the responsibility of the caller - * to close or return the connection to the pool. - * @param query The DB Query to fetch the data. - * @param config Configuration + * @param connection Database connection to be used. This method will not close the passed + * connection object. Since the caller has passed the connection object it's the + * responsibility of the caller to close or return the connection to the pool. + * @param query The DB Query to fetch the data. + * @param config Configuration * @return Arrow Data Objects {@link VectorSchemaRoot} - * @throws SQLException Propagate any SQL Exceptions to the caller after closing any resources opened such as - * ResultSet and Statement objects. + * @throws SQLException Propagate any SQL Exceptions to the caller after closing any resources + * opened such as ResultSet and Statement objects. */ - public static VectorSchemaRoot sqlToArrow(Connection connection, String query, JdbcToArrowConfig config) + public static VectorSchemaRoot sqlToArrow( + Connection connection, String query, JdbcToArrowConfig config) throws SQLException, IOException { Preconditions.checkNotNull(connection, "JDBC connection object cannot be null"); - Preconditions.checkArgument(query != null && query.length() > 0, "SQL query cannot be null or empty"); + Preconditions.checkArgument( + query != null && query.length() > 0, "SQL query cannot be null or empty"); try (Statement stmt = connection.createStatement()) { return sqlToArrow(stmt.executeQuery(query), config); @@ -246,10 +248,10 @@ public static VectorSchemaRoot sqlToArrow(Connection connection, String query, J } /** - * For the given JDBC {@link ResultSet}, fetch the data from Relational DB and convert it to Arrow objects. This - * method uses the default RootAllocator and Calendar object. + * For the given JDBC {@link ResultSet}, fetch the data from Relational DB and convert it to Arrow + * objects. This method uses the default RootAllocator and Calendar object. * - * This method is for test only. + *

This method is for test only. * * @param resultSet ResultSet to use to fetch the data from underlying database * @return Arrow Data Objects {@link VectorSchemaRoot} @@ -262,9 +264,10 @@ public static VectorSchemaRoot sqlToArrow(ResultSet resultSet) throws SQLExcepti } /** - * For the given JDBC {@link ResultSet}, fetch the data from Relational DB and convert it to Arrow objects. + * For the given JDBC {@link ResultSet}, fetch the data from Relational DB and convert it to Arrow + * objects. * - * This method is for test only. + *

This method is for test only. * * @param resultSet ResultSet to use to fetch the data from underlying database * @param allocator Memory allocator @@ -275,62 +278,69 @@ public static VectorSchemaRoot sqlToArrow(ResultSet resultSet, BufferAllocator a throws SQLException, IOException { Preconditions.checkNotNull(allocator, "Memory Allocator object cannot be null"); - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(allocator, JdbcToArrowUtils.getUtcCalendar()) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder(allocator, JdbcToArrowUtils.getUtcCalendar()) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build(); return sqlToArrow(resultSet, config); } /** - * For the given JDBC {@link ResultSet}, fetch the data from Relational DB and convert it to Arrow objects. + * For the given JDBC {@link ResultSet}, fetch the data from Relational DB and convert it to Arrow + * objects. * - * This method is for test only. + *

This method is for test only. * * @param resultSet ResultSet to use to fetch the data from underlying database - * @param calendar Calendar instance to use for Date, Time and Timestamp datasets, or null if none. + * @param calendar Calendar instance to use for Date, Time and Timestamp datasets, or null + * if none. * @return Arrow Data Objects {@link VectorSchemaRoot} * @throws SQLException on error */ - public static VectorSchemaRoot sqlToArrow(ResultSet resultSet, Calendar calendar) throws SQLException, IOException { + public static VectorSchemaRoot sqlToArrow(ResultSet resultSet, Calendar calendar) + throws SQLException, IOException { Preconditions.checkNotNull(resultSet, "JDBC ResultSet object cannot be null"); - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), calendar) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), calendar) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build(); return sqlToArrow(resultSet, config); } /** - * For the given JDBC {@link ResultSet}, fetch the data from Relational DB and convert it to Arrow objects. + * For the given JDBC {@link ResultSet}, fetch the data from Relational DB and convert it to Arrow + * objects. * - * This method is for test only. + *

This method is for test only. * * @param resultSet ResultSet to use to fetch the data from underlying database * @param allocator Memory allocator to use. - * @param calendar Calendar instance to use for Date, Time and Timestamp datasets, or null if none. + * @param calendar Calendar instance to use for Date, Time and Timestamp datasets, or null + * if none. * @return Arrow Data Objects {@link VectorSchemaRoot} * @throws SQLException on error */ public static VectorSchemaRoot sqlToArrow( - ResultSet resultSet, - BufferAllocator allocator, - Calendar calendar) + ResultSet resultSet, BufferAllocator allocator, Calendar calendar) throws SQLException, IOException { Preconditions.checkNotNull(allocator, "Memory Allocator object cannot be null"); - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(allocator, calendar) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder(allocator, calendar) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build(); return sqlToArrow(resultSet, config); } /** - * For the given JDBC {@link ResultSet}, fetch the data from Relational DB and convert it to Arrow objects. + * For the given JDBC {@link ResultSet}, fetch the data from Relational DB and convert it to Arrow + * objects. * - * This method is for test only. + *

This method is for test only. * * @param resultSet ResultSet to use to fetch the data from underlying database - * @param config Configuration of the conversion from JDBC to Arrow. + * @param config Configuration of the conversion from JDBC to Arrow. * @return Arrow Data Objects {@link VectorSchemaRoot} * @throws SQLException on error */ @@ -339,8 +349,10 @@ public static VectorSchemaRoot sqlToArrow(ResultSet resultSet, JdbcToArrowConfig Preconditions.checkNotNull(resultSet, "JDBC ResultSet object cannot be null"); Preconditions.checkNotNull(config, "The configuration cannot be null"); - VectorSchemaRoot root = VectorSchemaRoot.create( - JdbcToArrowUtils.jdbcToArrowSchema(resultSet.getMetaData(), config), config.getAllocator()); + VectorSchemaRoot root = + VectorSchemaRoot.create( + JdbcToArrowUtils.jdbcToArrowSchema(resultSet.getMetaData(), config), + config.getAllocator()); if (config.getTargetBatchSize() != JdbcToArrowConfig.NO_LIMIT_BATCH_SIZE) { ValueVectorUtility.preAllocate(root, config.getTargetBatchSize()); } @@ -350,12 +362,14 @@ public static VectorSchemaRoot sqlToArrow(ResultSet resultSet, JdbcToArrowConfig /** * Register MAP_FIELD20 as ArrowType.Map - * @param calendar Calendar instance to use for Date, Time and Timestamp datasets, or null if none. + * + * @param calendar Calendar instance to use for Date, Time and Timestamp datasets, or null + * if none. * @param rsmd ResultSetMetaData to lookup column name from result set metadata * @return typeConverter instance with mapping column to Map type */ protected Function jdbcToArrowTypeConverter( - Calendar calendar, ResultSetMetaData rsmd) { + Calendar calendar, ResultSetMetaData rsmd) { return (jdbcFieldInfo) -> { String columnLabel = null; try { @@ -377,5 +391,4 @@ protected Function jdbcToArrowTypeConverter( protected ResultSetMetaData getQueryMetaData(String query) throws SQLException { return conn.createStatement().executeQuery(query).getMetaData(); } - } diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcFieldInfoTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcFieldInfoTest.java index b1a8b8f2267..a5f8a5ccb78 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcFieldInfoTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcFieldInfoTest.java @@ -20,7 +20,6 @@ import static org.junit.Assert.*; import java.sql.Types; - import org.junit.Test; public class JdbcFieldInfoTest { diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcParameterBinderTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcParameterBinderTest.java index 15b9ab03861..f1de99e28ac 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcParameterBinderTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcParameterBinderTest.java @@ -32,7 +32,6 @@ import java.util.List; import java.util.Map; import java.util.function.BiConsumer; - import org.apache.arrow.adapter.jdbc.binder.ColumnBinder; import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; @@ -106,11 +105,11 @@ void bindOrder() throws SQLException { Field.nullable("ints1", new ArrowType.Int(32, true)), Field.nullable("ints2", new ArrowType.Int(32, true)))); try (final MockPreparedStatement statement = new MockPreparedStatement(); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { final JdbcParameterBinder binder = JdbcParameterBinder.builder(statement, root) - .bind(/*paramIndex=*/ 1, /*colIndex=*/ 2) - .bind(/*paramIndex=*/ 2, /*colIndex=*/ 0) + .bind(/* paramIndex= */ 1, /* colIndex= */ 2) + .bind(/* paramIndex= */ 2, /* colIndex= */ 0) .build(); assertThat(binder.next()).isFalse(); @@ -161,17 +160,17 @@ void bindOrder() throws SQLException { @Test void customBinder() throws SQLException { final Schema schema = - new Schema(Collections.singletonList( - Field.nullable("ints0", new ArrowType.Int(32, true)))); + new Schema(Collections.singletonList(Field.nullable("ints0", new ArrowType.Int(32, true)))); try (final MockPreparedStatement statement = new MockPreparedStatement(); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { final JdbcParameterBinder binder = JdbcParameterBinder.builder(statement, root) .bind( - /*paramIndex=*/ 1, + /* paramIndex= */ 1, new ColumnBinder() { private final IntVector vector = (IntVector) root.getVector(0); + @Override public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException { @@ -212,7 +211,9 @@ public FieldVector getVector() { @Test void bool() throws SQLException { - testSimpleType(ArrowType.Bool.INSTANCE, Types.BOOLEAN, + testSimpleType( + ArrowType.Bool.INSTANCE, + Types.BOOLEAN, (BitVector vector, Integer index, Boolean value) -> vector.setSafe(index, value ? 1 : 0), BitVector::setNull, Arrays.asList(true, false, true)); @@ -220,53 +221,76 @@ void bool() throws SQLException { @Test void int8() throws SQLException { - testSimpleType(new ArrowType.Int(8, true), Types.TINYINT, - TinyIntVector::setSafe, TinyIntVector::setNull, + testSimpleType( + new ArrowType.Int(8, true), + Types.TINYINT, + TinyIntVector::setSafe, + TinyIntVector::setNull, Arrays.asList(Byte.MAX_VALUE, Byte.MIN_VALUE, (byte) 42)); } @Test void int16() throws SQLException { - testSimpleType(new ArrowType.Int(16, true), Types.SMALLINT, - SmallIntVector::setSafe, SmallIntVector::setNull, + testSimpleType( + new ArrowType.Int(16, true), + Types.SMALLINT, + SmallIntVector::setSafe, + SmallIntVector::setNull, Arrays.asList(Short.MAX_VALUE, Short.MIN_VALUE, (short) 42)); } @Test void int32() throws SQLException { - testSimpleType(new ArrowType.Int(32, true), Types.INTEGER, - IntVector::setSafe, IntVector::setNull, + testSimpleType( + new ArrowType.Int(32, true), + Types.INTEGER, + IntVector::setSafe, + IntVector::setNull, Arrays.asList(Integer.MAX_VALUE, Integer.MIN_VALUE, 42)); } @Test void int64() throws SQLException { - testSimpleType(new ArrowType.Int(64, true), Types.BIGINT, - BigIntVector::setSafe, BigIntVector::setNull, + testSimpleType( + new ArrowType.Int(64, true), + Types.BIGINT, + BigIntVector::setSafe, + BigIntVector::setNull, Arrays.asList(Long.MAX_VALUE, Long.MIN_VALUE, 42L)); } @Test void float32() throws SQLException { - testSimpleType(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE), Types.REAL, - Float4Vector::setSafe, Float4Vector::setNull, + testSimpleType( + new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE), + Types.REAL, + Float4Vector::setSafe, + Float4Vector::setNull, Arrays.asList(Float.MIN_VALUE, Float.MAX_VALUE, Float.POSITIVE_INFINITY)); } @Test void float64() throws SQLException { - testSimpleType(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE), Types.DOUBLE, - Float8Vector::setSafe, Float8Vector::setNull, + testSimpleType( + new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE), + Types.DOUBLE, + Float8Vector::setSafe, + Float8Vector::setNull, Arrays.asList(Double.MIN_VALUE, Double.MAX_VALUE, Double.POSITIVE_INFINITY)); } @Test void time32() throws SQLException { - testSimpleType(new ArrowType.Time(TimeUnit.SECOND, 32), Types.TIME, - (valueVectors, index, value) -> valueVectors.setSafe(index, (int) (value.getTime() / 1_000)), + testSimpleType( + new ArrowType.Time(TimeUnit.SECOND, 32), + Types.TIME, + (valueVectors, index, value) -> + valueVectors.setSafe(index, (int) (value.getTime() / 1_000)), TimeSecVector::setNull, Arrays.asList(new Time(-128_000), new Time(104_000), new Time(-42_000))); - testSimpleType(new ArrowType.Time(TimeUnit.MILLISECOND, 32), Types.TIME, + testSimpleType( + new ArrowType.Time(TimeUnit.MILLISECOND, 32), + Types.TIME, (valueVectors, index, value) -> valueVectors.setSafe(index, (int) value.getTime()), TimeMilliVector::setNull, Arrays.asList(new Time(-128_000), new Time(104_000), new Time(-42_000))); @@ -274,69 +298,110 @@ void time32() throws SQLException { @Test void time64() throws SQLException { - testSimpleType(new ArrowType.Time(TimeUnit.MICROSECOND, 64), Types.TIME, - (valueVectors, index, value) -> valueVectors.setSafe(index, (int) (value.getTime() * 1_000)), + testSimpleType( + new ArrowType.Time(TimeUnit.MICROSECOND, 64), + Types.TIME, + (valueVectors, index, value) -> + valueVectors.setSafe(index, (int) (value.getTime() * 1_000)), TimeMicroVector::setNull, Arrays.asList(new Time(-128_000), new Time(104_000), new Time(-42_000))); - testSimpleType(new ArrowType.Time(TimeUnit.NANOSECOND, 64), Types.TIME, - (valueVectors, index, value) -> valueVectors.setSafe(index, (int) (value.getTime() * 1_000_000)), + testSimpleType( + new ArrowType.Time(TimeUnit.NANOSECOND, 64), + Types.TIME, + (valueVectors, index, value) -> + valueVectors.setSafe(index, (int) (value.getTime() * 1_000_000)), TimeNanoVector::setNull, Arrays.asList(new Time(-128), new Time(104), new Time(-42))); } @Test void date32() throws SQLException { - testSimpleType(new ArrowType.Date(DateUnit.DAY), Types.DATE, - (valueVectors, index, value) -> valueVectors.setSafe(index, (int) (value.getTime() / MILLIS_PER_DAY)), + testSimpleType( + new ArrowType.Date(DateUnit.DAY), + Types.DATE, + (valueVectors, index, value) -> + valueVectors.setSafe(index, (int) (value.getTime() / MILLIS_PER_DAY)), DateDayVector::setNull, - Arrays.asList(new Date(-5 * MILLIS_PER_DAY), new Date(2 * MILLIS_PER_DAY), new Date(MILLIS_PER_DAY))); + Arrays.asList( + new Date(-5 * MILLIS_PER_DAY), new Date(2 * MILLIS_PER_DAY), new Date(MILLIS_PER_DAY))); } @Test void date64() throws SQLException { - testSimpleType(new ArrowType.Date(DateUnit.MILLISECOND), Types.DATE, + testSimpleType( + new ArrowType.Date(DateUnit.MILLISECOND), + Types.DATE, (valueVectors, index, value) -> valueVectors.setSafe(index, value.getTime()), DateMilliVector::setNull, - Arrays.asList(new Date(-5 * MILLIS_PER_DAY), new Date(2 * MILLIS_PER_DAY), new Date(MILLIS_PER_DAY))); + Arrays.asList( + new Date(-5 * MILLIS_PER_DAY), new Date(2 * MILLIS_PER_DAY), new Date(MILLIS_PER_DAY))); } @Test void timestamp() throws SQLException { - List values = Arrays.asList(new Timestamp(-128_000), new Timestamp(104_000), new Timestamp(-42_000)); - testSimpleType(new ArrowType.Timestamp(TimeUnit.SECOND, null), Types.TIMESTAMP, + List values = + Arrays.asList(new Timestamp(-128_000), new Timestamp(104_000), new Timestamp(-42_000)); + testSimpleType( + new ArrowType.Timestamp(TimeUnit.SECOND, null), + Types.TIMESTAMP, (valueVectors, index, value) -> valueVectors.setSafe(index, value.getTime() / 1_000), - TimeStampSecVector::setNull, values); - testSimpleType(new ArrowType.Timestamp(TimeUnit.MILLISECOND, null), Types.TIMESTAMP, + TimeStampSecVector::setNull, + values); + testSimpleType( + new ArrowType.Timestamp(TimeUnit.MILLISECOND, null), + Types.TIMESTAMP, (valueVectors, index, value) -> valueVectors.setSafe(index, value.getTime()), - TimeStampMilliVector::setNull, values); - testSimpleType(new ArrowType.Timestamp(TimeUnit.MICROSECOND, null), Types.TIMESTAMP, + TimeStampMilliVector::setNull, + values); + testSimpleType( + new ArrowType.Timestamp(TimeUnit.MICROSECOND, null), + Types.TIMESTAMP, (valueVectors, index, value) -> valueVectors.setSafe(index, value.getTime() * 1_000), - TimeStampMicroVector::setNull, values); - testSimpleType(new ArrowType.Timestamp(TimeUnit.NANOSECOND, null), Types.TIMESTAMP, + TimeStampMicroVector::setNull, + values); + testSimpleType( + new ArrowType.Timestamp(TimeUnit.NANOSECOND, null), + Types.TIMESTAMP, (valueVectors, index, value) -> valueVectors.setSafe(index, value.getTime() * 1_000_000), - TimeStampNanoVector::setNull, values); + TimeStampNanoVector::setNull, + values); } @Test void timestampTz() throws SQLException { - List values = Arrays.asList(new Timestamp(-128_000), new Timestamp(104_000), new Timestamp(-42_000)); - testSimpleType(new ArrowType.Timestamp(TimeUnit.SECOND, "UTC"), Types.TIMESTAMP_WITH_TIMEZONE, + List values = + Arrays.asList(new Timestamp(-128_000), new Timestamp(104_000), new Timestamp(-42_000)); + testSimpleType( + new ArrowType.Timestamp(TimeUnit.SECOND, "UTC"), + Types.TIMESTAMP_WITH_TIMEZONE, (valueVectors, index, value) -> valueVectors.setSafe(index, value.getTime() / 1_000), - TimeStampSecTZVector::setNull, values); - testSimpleType(new ArrowType.Timestamp(TimeUnit.MILLISECOND, "UTC"), Types.TIMESTAMP_WITH_TIMEZONE, + TimeStampSecTZVector::setNull, + values); + testSimpleType( + new ArrowType.Timestamp(TimeUnit.MILLISECOND, "UTC"), + Types.TIMESTAMP_WITH_TIMEZONE, (valueVectors, index, value) -> valueVectors.setSafe(index, value.getTime()), - TimeStampMilliTZVector::setNull, values); - testSimpleType(new ArrowType.Timestamp(TimeUnit.MICROSECOND, "UTC"), Types.TIMESTAMP_WITH_TIMEZONE, + TimeStampMilliTZVector::setNull, + values); + testSimpleType( + new ArrowType.Timestamp(TimeUnit.MICROSECOND, "UTC"), + Types.TIMESTAMP_WITH_TIMEZONE, (valueVectors, index, value) -> valueVectors.setSafe(index, value.getTime() * 1_000), - TimeStampMicroTZVector::setNull, values); - testSimpleType(new ArrowType.Timestamp(TimeUnit.NANOSECOND, "UTC"), Types.TIMESTAMP_WITH_TIMEZONE, + TimeStampMicroTZVector::setNull, + values); + testSimpleType( + new ArrowType.Timestamp(TimeUnit.NANOSECOND, "UTC"), + Types.TIMESTAMP_WITH_TIMEZONE, (valueVectors, index, value) -> valueVectors.setSafe(index, value.getTime() * 1_000_000), - TimeStampNanoTZVector::setNull, values); + TimeStampNanoTZVector::setNull, + values); } @Test void utf8() throws SQLException { - testSimpleType(ArrowType.Utf8.INSTANCE, Types.VARCHAR, + testSimpleType( + ArrowType.Utf8.INSTANCE, + Types.VARCHAR, (VarCharVector vector, Integer index, String value) -> vector.setSafe(index, value.getBytes(StandardCharsets.UTF_8)), BaseVariableWidthVector::setNull, @@ -345,7 +410,9 @@ void utf8() throws SQLException { @Test void largeUtf8() throws SQLException { - testSimpleType(ArrowType.LargeUtf8.INSTANCE, Types.LONGVARCHAR, + testSimpleType( + ArrowType.LargeUtf8.INSTANCE, + Types.LONGVARCHAR, (LargeVarCharVector vector, Integer index, String value) -> vector.setSafe(index, value.getBytes(StandardCharsets.UTF_8)), BaseLargeVariableWidthVector::setNull, @@ -354,155 +421,200 @@ void largeUtf8() throws SQLException { @Test void binary() throws SQLException { - testSimpleType(ArrowType.Binary.INSTANCE, Types.VARBINARY, - (VarBinaryVector vector, Integer index, byte[] value) -> - vector.setSafe(index, value), + testSimpleType( + ArrowType.Binary.INSTANCE, + Types.VARBINARY, + (VarBinaryVector vector, Integer index, byte[] value) -> vector.setSafe(index, value), BaseVariableWidthVector::setNull, Arrays.asList(new byte[0], new byte[] {2, -4}, new byte[] {0, -1, 127, -128})); } @Test void largeBinary() throws SQLException { - testSimpleType(ArrowType.LargeBinary.INSTANCE, Types.LONGVARBINARY, - (LargeVarBinaryVector vector, Integer index, byte[] value) -> - vector.setSafe(index, value), + testSimpleType( + ArrowType.LargeBinary.INSTANCE, + Types.LONGVARBINARY, + (LargeVarBinaryVector vector, Integer index, byte[] value) -> vector.setSafe(index, value), BaseLargeVariableWidthVector::setNull, Arrays.asList(new byte[0], new byte[] {2, -4}, new byte[] {0, -1, 127, -128})); } @Test void fixedSizeBinary() throws SQLException { - testSimpleType(new ArrowType.FixedSizeBinary(3), Types.BINARY, - FixedSizeBinaryVector::setSafe, FixedSizeBinaryVector::setNull, + testSimpleType( + new ArrowType.FixedSizeBinary(3), + Types.BINARY, + FixedSizeBinaryVector::setSafe, + FixedSizeBinaryVector::setNull, Arrays.asList(new byte[3], new byte[] {1, 2, -4}, new byte[] {-1, 127, -128})); } @Test void decimal128() throws SQLException { - testSimpleType(new ArrowType.Decimal(/*precision*/ 12, /*scale*/3, 128), Types.DECIMAL, - DecimalVector::setSafe, DecimalVector::setNull, - Arrays.asList(new BigDecimal("120.429"), new BigDecimal("-10590.123"), new BigDecimal("0.000"))); + testSimpleType( + new ArrowType.Decimal(/*precision*/ 12, /*scale*/ 3, 128), + Types.DECIMAL, + DecimalVector::setSafe, + DecimalVector::setNull, + Arrays.asList( + new BigDecimal("120.429"), new BigDecimal("-10590.123"), new BigDecimal("0.000"))); } @Test void decimal256() throws SQLException { - testSimpleType(new ArrowType.Decimal(/*precision*/ 12, /*scale*/3, 256), Types.DECIMAL, - Decimal256Vector::setSafe, Decimal256Vector::setNull, - Arrays.asList(new BigDecimal("120.429"), new BigDecimal("-10590.123"), new BigDecimal("0.000"))); + testSimpleType( + new ArrowType.Decimal(/*precision*/ 12, /*scale*/ 3, 256), + Types.DECIMAL, + Decimal256Vector::setSafe, + Decimal256Vector::setNull, + Arrays.asList( + new BigDecimal("120.429"), new BigDecimal("-10590.123"), new BigDecimal("0.000"))); } @Test void listOfDouble() throws SQLException { - TriConsumer setValue = (listVector, index, values) -> { - org.apache.arrow.vector.complex.impl.UnionListWriter writer = listVector.getWriter(); - writer.setPosition(index); - writer.startList(); - Arrays.stream(values).forEach(doubleValue -> writer.float8().writeFloat8(doubleValue)); - writer.endList(); - listVector.setLastSet(index); - }; - List values = Arrays.asList(new Double[]{0.0, Math.PI}, new Double[]{1.1, -352346.2, 2355.6}, - new Double[]{-1024.3}, new Double[]{}); - testListType(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE), setValue, ListVector::setNull, values); + TriConsumer setValue = + (listVector, index, values) -> { + org.apache.arrow.vector.complex.impl.UnionListWriter writer = listVector.getWriter(); + writer.setPosition(index); + writer.startList(); + Arrays.stream(values).forEach(doubleValue -> writer.float8().writeFloat8(doubleValue)); + writer.endList(); + listVector.setLastSet(index); + }; + List values = + Arrays.asList( + new Double[] {0.0, Math.PI}, new Double[] {1.1, -352346.2, 2355.6}, + new Double[] {-1024.3}, new Double[] {}); + testListType( + new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE), + setValue, + ListVector::setNull, + values); } @Test void listOfInt64() throws SQLException { - TriConsumer setValue = (listVector, index, values) -> { - org.apache.arrow.vector.complex.impl.UnionListWriter writer = listVector.getWriter(); - writer.setPosition(index); - writer.startList(); - Arrays.stream(values).forEach(longValue -> writer.bigInt().writeBigInt(longValue)); - writer.endList(); - listVector.setLastSet(index); - }; - List values = Arrays.asList(new Long[]{1L, 2L, 3L}, new Long[]{4L, 5L}, - new Long[]{512L, 1024L, 2048L, 4096L}, new Long[]{}); + TriConsumer setValue = + (listVector, index, values) -> { + org.apache.arrow.vector.complex.impl.UnionListWriter writer = listVector.getWriter(); + writer.setPosition(index); + writer.startList(); + Arrays.stream(values).forEach(longValue -> writer.bigInt().writeBigInt(longValue)); + writer.endList(); + listVector.setLastSet(index); + }; + List values = + Arrays.asList( + new Long[] {1L, 2L, 3L}, + new Long[] {4L, 5L}, + new Long[] {512L, 1024L, 2048L, 4096L}, + new Long[] {}); testListType((ArrowType) new ArrowType.Int(64, true), setValue, ListVector::setNull, values); } @Test void listOfInt32() throws SQLException { - TriConsumer setValue = (listVector, index, values) -> { - org.apache.arrow.vector.complex.impl.UnionListWriter writer = listVector.getWriter(); - writer.setPosition(index); - writer.startList(); - Arrays.stream(values).forEach(integerValue -> writer.integer().writeInt(integerValue)); - writer.endList(); - listVector.setLastSet(index); - }; - List values = Arrays.asList(new Integer[]{1, 2, 3}, new Integer[]{4, 5}, - new Integer[]{512, 1024, 2048, 4096}, new Integer[]{}); + TriConsumer setValue = + (listVector, index, values) -> { + org.apache.arrow.vector.complex.impl.UnionListWriter writer = listVector.getWriter(); + writer.setPosition(index); + writer.startList(); + Arrays.stream(values).forEach(integerValue -> writer.integer().writeInt(integerValue)); + writer.endList(); + listVector.setLastSet(index); + }; + List values = + Arrays.asList( + new Integer[] {1, 2, 3}, + new Integer[] {4, 5}, + new Integer[] {512, 1024, 2048, 4096}, + new Integer[] {}); testListType((ArrowType) new ArrowType.Int(32, true), setValue, ListVector::setNull, values); } @Test void listOfBoolean() throws SQLException { - TriConsumer setValue = (listVector, index, values) -> { - org.apache.arrow.vector.complex.impl.UnionListWriter writer = listVector.getWriter(); - writer.setPosition(index); - writer.startList(); - Arrays.stream(values).forEach(booleanValue -> writer.bit().writeBit(booleanValue ? 1 : 0)); - writer.endList(); - listVector.setLastSet(index); - }; - List values = Arrays.asList(new Boolean[]{true, false}, - new Boolean[]{false, false}, new Boolean[]{true, true, false, true}, new Boolean[]{}); + TriConsumer setValue = + (listVector, index, values) -> { + org.apache.arrow.vector.complex.impl.UnionListWriter writer = listVector.getWriter(); + writer.setPosition(index); + writer.startList(); + Arrays.stream(values) + .forEach(booleanValue -> writer.bit().writeBit(booleanValue ? 1 : 0)); + writer.endList(); + listVector.setLastSet(index); + }; + List values = + Arrays.asList( + new Boolean[] {true, false}, + new Boolean[] {false, false}, + new Boolean[] {true, true, false, true}, + new Boolean[] {}); testListType((ArrowType) new ArrowType.Bool(), setValue, ListVector::setNull, values); } @Test void listOfString() throws SQLException { - TriConsumer setValue = (listVector, index, values) -> { - org.apache.arrow.vector.complex.impl.UnionListWriter writer = listVector.getWriter(); - writer.setPosition(index); - writer.startList(); - Arrays.stream(values).forEach(stringValue -> { - if (stringValue != null) { - byte[] stringValueBytes = stringValue.getBytes(StandardCharsets.UTF_8); - try (ArrowBuf stringBuffer = allocator.buffer(stringValueBytes.length)) { - stringBuffer.writeBytes(stringValueBytes); - writer.varChar().writeVarChar(0, stringValueBytes.length, stringBuffer); - } - } else { - writer.varChar().writeNull(); - } - }); - writer.endList(); - listVector.setLastSet(index); - }; - List values = Arrays.asList(new String[]{"aaaa", "b1"}, - new String[]{"c", null, "d"}, new String[]{"e", "f", "g", "h"}, new String[]{}); + TriConsumer setValue = + (listVector, index, values) -> { + org.apache.arrow.vector.complex.impl.UnionListWriter writer = listVector.getWriter(); + writer.setPosition(index); + writer.startList(); + Arrays.stream(values) + .forEach( + stringValue -> { + if (stringValue != null) { + byte[] stringValueBytes = stringValue.getBytes(StandardCharsets.UTF_8); + try (ArrowBuf stringBuffer = allocator.buffer(stringValueBytes.length)) { + stringBuffer.writeBytes(stringValueBytes); + writer.varChar().writeVarChar(0, stringValueBytes.length, stringBuffer); + } + } else { + writer.varChar().writeNull(); + } + }); + writer.endList(); + listVector.setLastSet(index); + }; + List values = + Arrays.asList( + new String[] {"aaaa", "b1"}, + new String[] {"c", null, "d"}, + new String[] {"e", "f", "g", "h"}, + new String[] {}); testListType((ArrowType) new ArrowType.Utf8(), setValue, ListVector::setNull, values); } @Test void mapOfString() throws SQLException { - TriConsumer> setValue = (mapVector, index, values) -> { - org.apache.arrow.vector.complex.impl.UnionMapWriter mapWriter = mapVector.getWriter(); - mapWriter.setPosition(index); - mapWriter.startMap(); - values.entrySet().forEach(mapValue -> { - if (mapValue != null) { - byte[] keyBytes = mapValue.getKey().getBytes(StandardCharsets.UTF_8); - byte[] valueBytes = mapValue.getValue().getBytes(StandardCharsets.UTF_8); - try ( - ArrowBuf keyBuf = allocator.buffer(keyBytes.length); - ArrowBuf valueBuf = allocator.buffer(valueBytes.length); - ) { - mapWriter.startEntry(); - keyBuf.writeBytes(keyBytes); - valueBuf.writeBytes(valueBytes); - mapWriter.key().varChar().writeVarChar(0, keyBytes.length, keyBuf); - mapWriter.value().varChar().writeVarChar(0, valueBytes.length, valueBuf); - mapWriter.endEntry(); - } - } else { - mapWriter.writeNull(); - } - }); - mapWriter.endMap(); - }; + TriConsumer> setValue = + (mapVector, index, values) -> { + org.apache.arrow.vector.complex.impl.UnionMapWriter mapWriter = mapVector.getWriter(); + mapWriter.setPosition(index); + mapWriter.startMap(); + values + .entrySet() + .forEach( + mapValue -> { + if (mapValue != null) { + byte[] keyBytes = mapValue.getKey().getBytes(StandardCharsets.UTF_8); + byte[] valueBytes = mapValue.getValue().getBytes(StandardCharsets.UTF_8); + try (ArrowBuf keyBuf = allocator.buffer(keyBytes.length); + ArrowBuf valueBuf = allocator.buffer(valueBytes.length); ) { + mapWriter.startEntry(); + keyBuf.writeBytes(keyBytes); + valueBuf.writeBytes(valueBytes); + mapWriter.key().varChar().writeVarChar(0, keyBytes.length, keyBuf); + mapWriter.value().varChar().writeVarChar(0, valueBytes.length, valueBuf); + mapWriter.endEntry(); + } + } else { + mapWriter.writeNull(); + } + }); + mapWriter.endMap(); + }; JsonStringHashMap value1 = new JsonStringHashMap(); value1.put("a", "b"); @@ -514,28 +626,34 @@ void mapOfString() throws SQLException { JsonStringHashMap value3 = new JsonStringHashMap(); value3.put("y", "z"); value3.put("arrow", "cool"); - List> values = Arrays.asList(value1, value2, value3, Collections.emptyMap()); - testMapType(new ArrowType.Map(true), setValue, MapVector::setNull, values, new ArrowType.Utf8()); + List> values = + Arrays.asList(value1, value2, value3, Collections.emptyMap()); + testMapType( + new ArrowType.Map(true), setValue, MapVector::setNull, values, new ArrowType.Utf8()); } @Test void mapOfInteger() throws SQLException { - TriConsumer> setValue = (mapVector, index, values) -> { - org.apache.arrow.vector.complex.impl.UnionMapWriter mapWriter = mapVector.getWriter(); - mapWriter.setPosition(index); - mapWriter.startMap(); - values.entrySet().forEach(mapValue -> { - if (mapValue != null) { - mapWriter.startEntry(); - mapWriter.key().integer().writeInt(mapValue.getKey()); - mapWriter.value().integer().writeInt(mapValue.getValue()); - mapWriter.endEntry(); - } else { - mapWriter.writeNull(); - } - }); - mapWriter.endMap(); - }; + TriConsumer> setValue = + (mapVector, index, values) -> { + org.apache.arrow.vector.complex.impl.UnionMapWriter mapWriter = mapVector.getWriter(); + mapWriter.setPosition(index); + mapWriter.startMap(); + values + .entrySet() + .forEach( + mapValue -> { + if (mapValue != null) { + mapWriter.startEntry(); + mapWriter.key().integer().writeInt(mapValue.getKey()); + mapWriter.value().integer().writeInt(mapValue.getValue()); + mapWriter.endEntry(); + } else { + mapWriter.writeNull(); + } + }); + mapWriter.endMap(); + }; JsonStringHashMap value1 = new JsonStringHashMap(); value1.put(1, 2); @@ -547,8 +665,10 @@ void mapOfInteger() throws SQLException { JsonStringHashMap value3 = new JsonStringHashMap(); value3.put(Integer.MIN_VALUE, Integer.MAX_VALUE); value3.put(0, 4096); - List> values = Arrays.asList(value1, value2, value3, Collections.emptyMap()); - testMapType(new ArrowType.Map(true), setValue, MapVector::setNull, values, new ArrowType.Int(32, true)); + List> values = + Arrays.asList(value1, value2, value3, Collections.emptyMap()); + testMapType( + new ArrowType.Map(true), setValue, MapVector::setNull, values, new ArrowType.Int(32, true)); } @FunctionalInterface @@ -556,11 +676,16 @@ interface TriConsumer { void accept(T value1, U value2, V value3); } - void testSimpleType(ArrowType arrowType, int jdbcType, TriConsumer setValue, - BiConsumer setNull, List values) throws SQLException { + void testSimpleType( + ArrowType arrowType, + int jdbcType, + TriConsumer setValue, + BiConsumer setNull, + List values) + throws SQLException { Schema schema = new Schema(Collections.singletonList(Field.nullable("field", arrowType))); try (final MockPreparedStatement statement = new MockPreparedStatement(); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { final JdbcParameterBinder binder = JdbcParameterBinder.builder(statement, root).bindAll().build(); assertThat(binder.next()).isFalse(); @@ -610,7 +735,7 @@ void testSimpleType(ArrowType arrowType, int jdbcType // Non-nullable (since some types have a specialized binder) schema = new Schema(Collections.singletonList(Field.notNullable("field", arrowType))); try (final MockPreparedStatement statement = new MockPreparedStatement(); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { final JdbcParameterBinder binder = JdbcParameterBinder.builder(statement, root).bindAll().build(); assertThat(binder.next()).isFalse(); @@ -650,15 +775,23 @@ void testSimpleType(ArrowType arrowType, int jdbcType } } - void testListType(ArrowType arrowType, TriConsumer setValue, - BiConsumer setNull, List values) throws SQLException { + void testListType( + ArrowType arrowType, + TriConsumer setValue, + BiConsumer setNull, + List values) + throws SQLException { int jdbcType = Types.ARRAY; - Schema schema = new Schema(Collections.singletonList(new Field("field", FieldType.nullable( - new ArrowType.List()), Collections.singletonList( - new Field("element", FieldType.notNullable(arrowType), null) - )))); + Schema schema = + new Schema( + Collections.singletonList( + new Field( + "field", + FieldType.nullable(new ArrowType.List()), + Collections.singletonList( + new Field("element", FieldType.notNullable(arrowType), null))))); try (final MockPreparedStatement statement = new MockPreparedStatement(); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { final JdbcParameterBinder binder = JdbcParameterBinder.builder(statement, root).bindAll().build(); assertThat(binder.next()).isFalse(); @@ -706,12 +839,16 @@ void testListType(ArrowType arrowType, TriConsumer void testListType(ArrowType arrowType, TriConsumer void testMapType(ArrowType arrowType, TriConsumer setValue, - BiConsumer setNull, List values, - ArrowType elementType) throws SQLException { + void testMapType( + ArrowType arrowType, + TriConsumer setValue, + BiConsumer setNull, + List values, + ArrowType elementType) + throws SQLException { int jdbcType = Types.VARCHAR; FieldType keyType = new FieldType(false, elementType, null, null); FieldType mapType = new FieldType(false, ArrowType.Struct.INSTANCE, null, null); - Schema schema = new Schema(Collections.singletonList(new Field("field", FieldType.nullable(arrowType), - Collections.singletonList(new Field(MapVector.KEY_NAME, mapType, - Arrays.asList(new Field(MapVector.KEY_NAME, keyType, null), - new Field(MapVector.VALUE_NAME, keyType, null))))))); + Schema schema = + new Schema( + Collections.singletonList( + new Field( + "field", + FieldType.nullable(arrowType), + Collections.singletonList( + new Field( + MapVector.KEY_NAME, + mapType, + Arrays.asList( + new Field(MapVector.KEY_NAME, keyType, null), + new Field(MapVector.VALUE_NAME, keyType, null))))))); try (final MockPreparedStatement statement = new MockPreparedStatement(); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { final JdbcParameterBinder binder = JdbcParameterBinder.builder(statement, root).bindAll().build(); assertThat(binder.next()).isFalse(); @@ -810,18 +960,31 @@ void testMapType(ArrowType arrowType, TriConsumer metadata) { + private static Field field( + String name, boolean nullable, ArrowType type, Map metadata) { return new Field(name, new FieldType(nullable, type, null, metadata), Collections.emptyList()); } @@ -94,16 +96,26 @@ private static Map metadata(String... entries) { public void schemaComment() throws Exception { boolean includeMetadata = false; Schema schema = getSchemaWithCommentFromQuery(includeMetadata); - Schema expectedSchema = new Schema(Arrays.asList( - field("ID", false, Types.MinorType.BIGINT.getType(), - metadata("comment", "Record identifier")), - field("NAME", true, Types.MinorType.VARCHAR.getType(), - metadata("comment", "Name of record")), - field("COLUMN1", true, Types.MinorType.BIT.getType(), - metadata()), - field("COLUMNN", true, Types.MinorType.INT.getType(), - metadata("comment", "Informative description of columnN")) - ), metadata("comment", "This is super special table with valuable data")); + Schema expectedSchema = + new Schema( + Arrays.asList( + field( + "ID", + false, + Types.MinorType.BIGINT.getType(), + metadata("comment", "Record identifier")), + field( + "NAME", + true, + Types.MinorType.VARCHAR.getType(), + metadata("comment", "Name of record")), + field("COLUMN1", true, Types.MinorType.BIT.getType(), metadata()), + field( + "COLUMNN", + true, + Types.MinorType.INT.getType(), + metadata("comment", "Informative description of columnN"))), + metadata("comment", "This is super special table with valuable data")); assertThat(schema).isEqualTo(expectedSchema); } @@ -111,47 +123,60 @@ public void schemaComment() throws Exception { public void schemaCommentWithDatabaseMetadata() throws Exception { boolean includeMetadata = true; Schema schema = getSchemaWithCommentFromQuery(includeMetadata); - Schema expectedSchema = new Schema(Arrays.asList( - field("ID", false, Types.MinorType.BIGINT.getType(), - metadata( - "SQL_CATALOG_NAME", "JDBCTOARROWTEST?CHARACTERENCODING=UTF-8", - "SQL_SCHEMA_NAME", "PUBLIC", - "SQL_TABLE_NAME", "TABLE1", - "SQL_COLUMN_NAME", "ID", - "SQL_TYPE", "BIGINT", - "comment", "Record identifier" - )), - field("NAME", true, Types.MinorType.VARCHAR.getType(), - metadata( - "SQL_CATALOG_NAME", "JDBCTOARROWTEST?CHARACTERENCODING=UTF-8", - "SQL_SCHEMA_NAME", "PUBLIC", - "SQL_TABLE_NAME", "TABLE1", - "SQL_COLUMN_NAME", "NAME", - "SQL_TYPE", "CHARACTER VARYING", - "comment", "Name of record")), - field("COLUMN1", true, Types.MinorType.BIT.getType(), - metadata( - "SQL_CATALOG_NAME", "JDBCTOARROWTEST?CHARACTERENCODING=UTF-8", - "SQL_SCHEMA_NAME", "PUBLIC", - "SQL_TABLE_NAME", "TABLE1", - "SQL_COLUMN_NAME", "COLUMN1", - "SQL_TYPE", "BOOLEAN")), - field("COLUMNN", true, Types.MinorType.INT.getType(), - metadata( - "SQL_CATALOG_NAME", "JDBCTOARROWTEST?CHARACTERENCODING=UTF-8", - "SQL_SCHEMA_NAME", "PUBLIC", - "SQL_TABLE_NAME", "TABLE1", - "SQL_COLUMN_NAME", "COLUMNN", - "SQL_TYPE", "INTEGER", - "comment", "Informative description of columnN")) - ), metadata("comment", "This is super special table with valuable data")); + Schema expectedSchema = + new Schema( + Arrays.asList( + field( + "ID", + false, + Types.MinorType.BIGINT.getType(), + metadata( + "SQL_CATALOG_NAME", "JDBCTOARROWTEST?CHARACTERENCODING=UTF-8", + "SQL_SCHEMA_NAME", "PUBLIC", + "SQL_TABLE_NAME", "TABLE1", + "SQL_COLUMN_NAME", "ID", + "SQL_TYPE", "BIGINT", + "comment", "Record identifier")), + field( + "NAME", + true, + Types.MinorType.VARCHAR.getType(), + metadata( + "SQL_CATALOG_NAME", "JDBCTOARROWTEST?CHARACTERENCODING=UTF-8", + "SQL_SCHEMA_NAME", "PUBLIC", + "SQL_TABLE_NAME", "TABLE1", + "SQL_COLUMN_NAME", "NAME", + "SQL_TYPE", "CHARACTER VARYING", + "comment", "Name of record")), + field( + "COLUMN1", + true, + Types.MinorType.BIT.getType(), + metadata( + "SQL_CATALOG_NAME", "JDBCTOARROWTEST?CHARACTERENCODING=UTF-8", + "SQL_SCHEMA_NAME", "PUBLIC", + "SQL_TABLE_NAME", "TABLE1", + "SQL_COLUMN_NAME", "COLUMN1", + "SQL_TYPE", "BOOLEAN")), + field( + "COLUMNN", + true, + Types.MinorType.INT.getType(), + metadata( + "SQL_CATALOG_NAME", "JDBCTOARROWTEST?CHARACTERENCODING=UTF-8", + "SQL_SCHEMA_NAME", "PUBLIC", + "SQL_TABLE_NAME", "TABLE1", + "SQL_COLUMN_NAME", "COLUMNN", + "SQL_TYPE", "INTEGER", + "comment", "Informative description of columnN"))), + metadata("comment", "This is super special table with valuable data")); assertThat(schema).isEqualTo(expectedSchema); /* corresponding Apache Spark DDL after conversion: - ID BIGINT NOT NULL COMMENT 'Record identifier', - NAME STRING COMMENT 'Name of record', - COLUMN1 BOOLEAN, - COLUMNN INT COMMENT 'Informative description of columnN' - */ + ID BIGINT NOT NULL COMMENT 'Record identifier', + NAME STRING COMMENT 'Name of record', + COLUMN1 BOOLEAN, + COLUMNN INT COMMENT 'Informative description of columnN' + */ assertThat(schema).isEqualTo(expectedSchema); } @@ -160,19 +185,25 @@ private Schema getSchemaWithCommentFromQuery(boolean includeMetadata) throws SQL try (Statement statement = conn.createStatement()) { try (ResultSet resultSet = statement.executeQuery("select * from table1")) { ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); - Map> columnCommentByColumnIndex = getColumnComments(metaData, resultSetMetaData); + Map> columnCommentByColumnIndex = + getColumnComments(metaData, resultSetMetaData); String tableName = getTableNameFromResultSetMetaData(resultSetMetaData); String tableComment = getTableComment(metaData, tableName); - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder() - .setAllocator(new RootAllocator()).setSchemaMetadata(Collections.singletonMap(COMMENT, tableComment)) - .setColumnMetadataByColumnIndex(columnCommentByColumnIndex).setIncludeMetadata(includeMetadata).build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder() + .setAllocator(new RootAllocator()) + .setSchemaMetadata(Collections.singletonMap(COMMENT, tableComment)) + .setColumnMetadataByColumnIndex(columnCommentByColumnIndex) + .setIncludeMetadata(includeMetadata) + .build(); return JdbcToArrowUtils.jdbcToArrowSchema(resultSetMetaData, config); } } } - private String getTableNameFromResultSetMetaData(ResultSetMetaData resultSetMetaData) throws SQLException { + private String getTableNameFromResultSetMetaData(ResultSetMetaData resultSetMetaData) + throws SQLException { Set tablesFromQuery = new HashSet<>(); for (int idx = 1, columnCount = resultSetMetaData.getColumnCount(); idx <= columnCount; idx++) { String tableName = resultSetMetaData.getTableName(idx); @@ -186,11 +217,16 @@ private String getTableNameFromResultSetMetaData(ResultSetMetaData resultSetMeta throw new RuntimeException("Table metadata is absent or ambiguous"); } - private Map> getColumnComments(DatabaseMetaData metaData, - ResultSetMetaData resultSetMetaData) throws SQLException { + private Map> getColumnComments( + DatabaseMetaData metaData, ResultSetMetaData resultSetMetaData) throws SQLException { Map> columnCommentByColumnIndex = new HashMap<>(); - for (int columnIdx = 1, columnCount = resultSetMetaData.getColumnCount(); columnIdx <= columnCount; columnIdx++) { - String columnComment = getColumnComment(metaData, resultSetMetaData.getTableName(columnIdx), + for (int columnIdx = 1, columnCount = resultSetMetaData.getColumnCount(); + columnIdx <= columnCount; + columnIdx++) { + String columnComment = + getColumnComment( + metaData, + resultSetMetaData.getTableName(columnIdx), resultSetMetaData.getColumnName(columnIdx)); if (columnComment != null && !columnComment.isEmpty()) { columnCommentByColumnIndex.put(columnIdx, Collections.singletonMap(COMMENT, columnComment)); @@ -220,7 +256,8 @@ private String getTableComment(DatabaseMetaData metaData, String tableName) thro throw new RuntimeException("Table comment not found"); } - private String getColumnComment(DatabaseMetaData metaData, String tableName, String columnName) throws SQLException { + private String getColumnComment(DatabaseMetaData metaData, String tableName, String columnName) + throws SQLException { try (ResultSet tableMetadata = metaData.getColumns(null, null, tableName, columnName)) { if (tableMetadata.next()) { return tableMetadata.getString("REMARKS"); @@ -229,8 +266,14 @@ private String getColumnComment(DatabaseMetaData metaData, String tableName, Str return null; } - private String getExpectedSchema(String expectedResource) throws java.io.IOException, java.net.URISyntaxException { - return new String(Files.readAllBytes(Paths.get(Objects.requireNonNull( - JdbcToArrowCommentMetadataTest.class.getResource(expectedResource)).toURI())), StandardCharsets.UTF_8); + private String getExpectedSchema(String expectedResource) + throws java.io.IOException, java.net.URISyntaxException { + return new String( + Files.readAllBytes( + Paths.get( + Objects.requireNonNull( + JdbcToArrowCommentMetadataTest.class.getResource(expectedResource)) + .toURI())), + StandardCharsets.UTF_8); } } diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcToArrowConfigTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcToArrowConfigTest.java index 68a681b052c..c4dda5dc152 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcToArrowConfigTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcToArrowConfigTest.java @@ -28,7 +28,6 @@ import java.util.HashMap; import java.util.Locale; import java.util.TimeZone; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.junit.Test; @@ -36,7 +35,8 @@ public class JdbcToArrowConfigTest { private static final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - private static final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT); + private static final Calendar calendar = + Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT); @Test(expected = NullPointerException.class) public void testConfigNullArguments() { @@ -116,13 +116,29 @@ public void testIncludeMetadata() { config = new JdbcToArrowConfigBuilder(allocator, calendar, true).build(); assertTrue(config.shouldIncludeMetadata()); - config = new JdbcToArrowConfig(allocator, calendar, /* include metadata */ true, - /* reuse vector schema root */ true, null, null, JdbcToArrowConfig.NO_LIMIT_BATCH_SIZE, null); + config = + new JdbcToArrowConfig( + allocator, + calendar, /* include metadata */ + true, + /* reuse vector schema root */ true, + null, + null, + JdbcToArrowConfig.NO_LIMIT_BATCH_SIZE, + null); assertTrue(config.shouldIncludeMetadata()); assertTrue(config.isReuseVectorSchemaRoot()); - config = new JdbcToArrowConfig(allocator, calendar, /* include metadata */ false, - /* reuse vector schema root */ false, null, null, JdbcToArrowConfig.NO_LIMIT_BATCH_SIZE, null); + config = + new JdbcToArrowConfig( + allocator, + calendar, /* include metadata */ + false, + /* reuse vector schema root */ false, + null, + null, + JdbcToArrowConfig.NO_LIMIT_BATCH_SIZE, + null); assertFalse(config.shouldIncludeMetadata()); assertFalse(config.isReuseVectorSchemaRoot()); } diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcToArrowTestHelper.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcToArrowTestHelper.java index 91f2f465dd9..e16a39d3f49 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcToArrowTestHelper.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcToArrowTestHelper.java @@ -22,6 +22,9 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import java.math.BigDecimal; import java.nio.charset.Charset; import java.sql.ResultSetMetaData; @@ -31,7 +34,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; - import org.apache.arrow.vector.BaseValueVector; import org.apache.arrow.vector.BigIntVector; import org.apache.arrow.vector.BitVector; @@ -57,12 +59,9 @@ import org.apache.arrow.vector.util.ObjectMapperFactory; import org.apache.arrow.vector.util.Text; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - /** - * This is a Helper class which has functionalities to read and assert the values from the given FieldVector object. + * This is a Helper class which has functionalities to read and assert the values from the given + * FieldVector object. */ public class JdbcToArrowTestHelper { @@ -78,7 +77,8 @@ public static void assertIntVectorValues(IntVector intVector, int rowCount, Inte } } - public static void assertBooleanVectorValues(BitVector bitVector, int rowCount, Boolean[] values) { + public static void assertBooleanVectorValues( + BitVector bitVector, int rowCount, Boolean[] values) { assertEquals(rowCount, bitVector.getValueCount()); for (int j = 0; j < bitVector.getValueCount(); j++) { @@ -102,7 +102,8 @@ public static void assertBitVectorValues(BitVector bitVector, int rowCount, Inte } } - public static void assertTinyIntVectorValues(TinyIntVector tinyIntVector, int rowCount, Integer[] values) { + public static void assertTinyIntVectorValues( + TinyIntVector tinyIntVector, int rowCount, Integer[] values) { assertEquals(rowCount, tinyIntVector.getValueCount()); for (int j = 0; j < tinyIntVector.getValueCount(); j++) { @@ -114,7 +115,8 @@ public static void assertTinyIntVectorValues(TinyIntVector tinyIntVector, int ro } } - public static void assertSmallIntVectorValues(SmallIntVector smallIntVector, int rowCount, Integer[] values) { + public static void assertSmallIntVectorValues( + SmallIntVector smallIntVector, int rowCount, Integer[] values) { assertEquals(rowCount, smallIntVector.getValueCount()); for (int j = 0; j < smallIntVector.getValueCount(); j++) { @@ -126,7 +128,8 @@ public static void assertSmallIntVectorValues(SmallIntVector smallIntVector, int } } - public static void assertBigIntVectorValues(BigIntVector bigIntVector, int rowCount, Long[] values) { + public static void assertBigIntVectorValues( + BigIntVector bigIntVector, int rowCount, Long[] values) { assertEquals(rowCount, bigIntVector.getValueCount()); for (int j = 0; j < bigIntVector.getValueCount(); j++) { @@ -138,7 +141,8 @@ public static void assertBigIntVectorValues(BigIntVector bigIntVector, int rowCo } } - public static void assertDecimalVectorValues(DecimalVector decimalVector, int rowCount, BigDecimal[] values) { + public static void assertDecimalVectorValues( + DecimalVector decimalVector, int rowCount, BigDecimal[] values) { assertEquals(rowCount, decimalVector.getValueCount()); for (int j = 0; j < decimalVector.getValueCount(); j++) { @@ -150,7 +154,8 @@ public static void assertDecimalVectorValues(DecimalVector decimalVector, int ro } } - public static void assertFloat8VectorValues(Float8Vector float8Vector, int rowCount, Double[] values) { + public static void assertFloat8VectorValues( + Float8Vector float8Vector, int rowCount, Double[] values) { assertEquals(rowCount, float8Vector.getValueCount()); for (int j = 0; j < float8Vector.getValueCount(); j++) { @@ -162,7 +167,8 @@ public static void assertFloat8VectorValues(Float8Vector float8Vector, int rowCo } } - public static void assertFloat4VectorValues(Float4Vector float4Vector, int rowCount, Float[] values) { + public static void assertFloat4VectorValues( + Float4Vector float4Vector, int rowCount, Float[] values) { assertEquals(rowCount, float4Vector.getValueCount()); for (int j = 0; j < float4Vector.getValueCount(); j++) { @@ -174,7 +180,8 @@ public static void assertFloat4VectorValues(Float4Vector float4Vector, int rowCo } } - public static void assertTimeVectorValues(TimeMilliVector timeMilliVector, int rowCount, Long[] values) { + public static void assertTimeVectorValues( + TimeMilliVector timeMilliVector, int rowCount, Long[] values) { assertEquals(rowCount, timeMilliVector.getValueCount()); for (int j = 0; j < timeMilliVector.getValueCount(); j++) { @@ -186,7 +193,8 @@ public static void assertTimeVectorValues(TimeMilliVector timeMilliVector, int r } } - public static void assertDateVectorValues(DateDayVector dateDayVector, int rowCount, Integer[] values) { + public static void assertDateVectorValues( + DateDayVector dateDayVector, int rowCount, Integer[] values) { assertEquals(rowCount, dateDayVector.getValueCount()); for (int j = 0; j < dateDayVector.getValueCount(); j++) { @@ -198,7 +206,8 @@ public static void assertDateVectorValues(DateDayVector dateDayVector, int rowCo } } - public static void assertTimeStampVectorValues(TimeStampVector timeStampVector, int rowCount, Long[] values) { + public static void assertTimeStampVectorValues( + TimeStampVector timeStampVector, int rowCount, Long[] values) { assertEquals(rowCount, timeStampVector.getValueCount()); for (int j = 0; j < timeStampVector.getValueCount(); j++) { @@ -210,7 +219,8 @@ public static void assertTimeStampVectorValues(TimeStampVector timeStampVector, } } - public static void assertVarBinaryVectorValues(VarBinaryVector varBinaryVector, int rowCount, byte[][] values) { + public static void assertVarBinaryVectorValues( + VarBinaryVector varBinaryVector, int rowCount, byte[][] values) { assertEquals(rowCount, varBinaryVector.getValueCount()); for (int j = 0; j < varBinaryVector.getValueCount(); j++) { @@ -222,7 +232,8 @@ public static void assertVarBinaryVectorValues(VarBinaryVector varBinaryVector, } } - public static void assertVarcharVectorValues(VarCharVector varCharVector, int rowCount, byte[][] values) { + public static void assertVarcharVectorValues( + VarCharVector varCharVector, int rowCount, byte[][] values) { assertEquals(rowCount, varCharVector.getValueCount()); for (int j = 0; j < varCharVector.getValueCount(); j++) { @@ -238,7 +249,8 @@ public static void assertNullVectorValues(NullVector vector, int rowCount) { assertEquals(rowCount, vector.getValueCount()); } - public static void assertListVectorValues(ListVector listVector, int rowCount, Integer[][] values) { + public static void assertListVectorValues( + ListVector listVector, int rowCount, Integer[][] values) { assertEquals(rowCount, listVector.getValueCount()); for (int j = 0; j < listVector.getValueCount(); j++) { @@ -251,7 +263,8 @@ public static void assertListVectorValues(ListVector listVector, int rowCount, I } } - public static void assertMapVectorValues(MapVector mapVector, int rowCount, Map[] values) { + public static void assertMapVectorValues( + MapVector mapVector, int rowCount, Map[] values) { assertEquals(rowCount, mapVector.getValueCount()); for (int j = 0; j < mapVector.getValueCount(); j++) { @@ -262,10 +275,17 @@ public static void assertMapVectorValues(MapVector mapVector, int rowCount, Map< (JsonStringArrayList>) mapVector.getObject(j); Map actualMap = null; if (actualSource != null && !actualSource.isEmpty()) { - actualMap = actualSource.stream().map(entry -> - new AbstractMap.SimpleEntry<>(entry.get("key").toString(), - entry.get("value") != null ? entry.get("value").toString() : null)) - .collect(HashMap::new, (collector, val) -> collector.put(val.getKey(), val.getValue()), HashMap::putAll); + actualMap = + actualSource.stream() + .map( + entry -> + new AbstractMap.SimpleEntry<>( + entry.get("key").toString(), + entry.get("value") != null ? entry.get("value").toString() : null)) + .collect( + HashMap::new, + (collector, val) -> collector.put(val.getKey(), val.getValue()), + HashMap::putAll); } assertEquals(values[j], actualMap); } @@ -309,8 +329,8 @@ public static void assertFieldMetadataIsEmpty(VectorSchemaRoot schema) { } } - public static void assertFieldMetadataMatchesResultSetMetadata(ResultSetMetaData rsmd, Schema schema) - throws SQLException { + public static void assertFieldMetadataMatchesResultSetMetadata( + ResultSetMetaData rsmd, Schema schema) throws SQLException { assertNotNull(schema); assertNotNull(schema.getFields()); assertNotNull(rsmd); @@ -404,7 +424,8 @@ public static byte[][] getCharArray(String[] values, String dataType) { return valueArr; } - public static byte[][] getCharArrayWithCharSet(String[] values, String dataType, Charset charSet) { + public static byte[][] getCharArrayWithCharSet( + String[] values, String dataType, Charset charSet) { String[] dataArr = getValues(values, dataType); byte[][] valueArr = new byte[dataArr.length][]; int i = 0; 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 c712741b51f..e2547fa8966 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 @@ -48,13 +48,11 @@ public class ResultSetUtility { public static ResultSet generateEmptyResultSet() throws SQLException { MockDataElement element = new MockDataElement("string_example"); MockResultSetMetaData.MockColumnMetaData columnMetaData = - MockResultSetMetaData.MockColumnMetaData.fromDataElement(element, 1); + MockResultSetMetaData.MockColumnMetaData.fromDataElement(element, 1); ArrayList cols = new ArrayList<>(); cols.add(columnMetaData); ResultSetMetaData metadata = new MockResultSetMetaData(cols); - return MockResultSet.builder() - .setMetaData(metadata) - .build(); + return MockResultSet.builder().setMetaData(metadata).build(); } public static MockResultSet generateBasicResultSet(int rows) throws SQLException { @@ -319,15 +317,19 @@ public String getColumnTypeName(int column) throws SQLException { } 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 + // 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 // an exception will be thrown. if (rows.size() == 0) { - throw new SQLException("Unable to dynamically generate ResultSetMetaData because row count is zero!"); + throw new SQLException( + "Unable to dynamically generate ResultSetMetaData because row count is zero!"); } MockRow firstRow = rows.get(0); if (firstRow.dataElements.size() == 0) { - throw new SQLException("Unable to dynamically generate ResultSetMetaData because column count is zero!"); + throw new SQLException( + "Unable to dynamically generate ResultSetMetaData because column count is zero!"); } ArrayList columns = new ArrayList<>(); for (int i = 0; i < firstRow.dataElements.size(); i++) { @@ -347,7 +349,6 @@ public static class MockColumnMetaData { private String typeName; private int displaySize; - private MockColumnMetaData() {} private String getLabel() { @@ -382,17 +383,18 @@ private int getDisplaySize() { return displaySize; } - public static MockColumnMetaData fromDataElement(MockDataElement element, int i) throws SQLException { + public static MockColumnMetaData fromDataElement(MockDataElement element, int i) + throws SQLException { return MockColumnMetaData.builder() - .index(i) - .sqlType(element.getSqlType()) - .precision(element.getPrecision()) - .scale(element.getScale()) - .nullable(element.isNullable()) - .setTypeName("TYPE") - .setDisplaySize(420) - .label("col_" + i) - .build(); + .index(i) + .sqlType(element.getSqlType()) + .precision(element.getPrecision()) + .scale(element.getScale()) + .nullable(element.isNullable()) + .setTypeName("TYPE") + .setDisplaySize(420) + .label("col_" + i) + .build(); } public static Builder builder() { @@ -446,9 +448,7 @@ public MockColumnMetaData build() { return this.columnMetaData; } } - } - } public static class MockRow { @@ -641,7 +641,6 @@ public short getShort() throws SQLException { } } - public static class ThrowingResultSet implements ResultSet { @Override @@ -1145,17 +1144,20 @@ public void updateTimestamp(String columnLabel, Timestamp x) throws SQLException } @Override - public void updateAsciiStream(String columnLabel, InputStream x, int length) throws SQLException { + public void updateAsciiStream(String columnLabel, InputStream x, int length) + throws SQLException { throw getExceptionToThrow(); } @Override - public void updateBinaryStream(String columnLabel, InputStream x, int length) throws SQLException { + public void updateBinaryStream(String columnLabel, InputStream x, int length) + throws SQLException { throw getExceptionToThrow(); } @Override - public void updateCharacterStream(String columnLabel, Reader reader, int length) throws SQLException { + public void updateCharacterStream(String columnLabel, Reader reader, int length) + throws SQLException { throw getExceptionToThrow(); } @@ -1445,7 +1447,8 @@ public void updateNCharacterStream(int columnIndex, Reader x, long length) throw } @Override - public void updateNCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { + public void updateNCharacterStream(String columnLabel, Reader reader, long length) + throws SQLException { throw getExceptionToThrow(); } @@ -1455,7 +1458,8 @@ public void updateAsciiStream(int columnIndex, InputStream x, long length) throw } @Override - public void updateBinaryStream(int columnIndex, InputStream x, long length) throws SQLException { + public void updateBinaryStream(int columnIndex, InputStream x, long length) + throws SQLException { throw getExceptionToThrow(); } @@ -1465,27 +1469,32 @@ public void updateCharacterStream(int columnIndex, Reader x, long length) throws } @Override - public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException { + public void updateAsciiStream(String columnLabel, InputStream x, long length) + throws SQLException { throw getExceptionToThrow(); } @Override - public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException { + public void updateBinaryStream(String columnLabel, InputStream x, long length) + throws SQLException { throw getExceptionToThrow(); } @Override - public void updateCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { + public void updateCharacterStream(String columnLabel, Reader reader, long length) + throws SQLException { throw getExceptionToThrow(); } @Override - public void updateBlob(int columnIndex, InputStream inputStream, long length) throws SQLException { + public void updateBlob(int columnIndex, InputStream inputStream, long length) + throws SQLException { throw getExceptionToThrow(); } @Override - public void updateBlob(String columnLabel, InputStream inputStream, long length) throws SQLException { + public void updateBlob(String columnLabel, InputStream inputStream, long length) + throws SQLException { throw getExceptionToThrow(); } @@ -1590,13 +1599,14 @@ public T getObject(String columnLabel, Class type) throws SQLException { } @Override - public void updateObject(int columnIndex, Object x, SQLType targetSqlType, int scaleOrLength) throws SQLException { + public void updateObject(int columnIndex, Object x, SQLType targetSqlType, int scaleOrLength) + throws SQLException { throw getExceptionToThrow(); } @Override public void updateObject(String columnLabel, Object x, SQLType targetSqlType, int scaleOrLength) - throws SQLException { + throws SQLException { throw getExceptionToThrow(); } @@ -1606,7 +1616,8 @@ public void updateObject(int columnIndex, Object x, SQLType targetSqlType) throw } @Override - public void updateObject(String columnLabel, Object x, SQLType targetSqlType) throws SQLException { + public void updateObject(String columnLabel, Object x, SQLType targetSqlType) + throws SQLException { throw getExceptionToThrow(); } @@ -1629,7 +1640,6 @@ private static SQLException getExceptionToThrow(String message) { return new SQLException(message); } - public static class ThrowingResultSetMetaData implements ResultSetMetaData { @Override public int getColumnCount() throws SQLException { diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/ResultSetUtilityTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/ResultSetUtilityTest.java index 2424ed62524..4a505c48d28 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/ResultSetUtilityTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/ResultSetUtilityTest.java @@ -26,7 +26,6 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.VectorSchemaRoot; @@ -36,20 +35,24 @@ public class ResultSetUtilityTest { @Test public void testZeroRowResultSet() throws Exception { - for (boolean reuseVectorSchemaRoot : new boolean[]{false, true}) { + for (boolean reuseVectorSchemaRoot : new boolean[] {false, true}) { try (BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE)) { ResultSet rs = ResultSetUtility.generateEmptyResultSet(); - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder( - allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) - .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder( + allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) + .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) + .build(); ArrowVectorIterator iter = JdbcToArrow.sqlToArrowVectorIterator(rs, config); assertTrue("Iterator on zero row ResultSet should haveNext() before use", iter.hasNext()); VectorSchemaRoot root = iter.next(); assertNotNull("VectorSchemaRoot from first next() result should never be null", root); - assertEquals("VectorSchemaRoot from empty ResultSet should have zero rows", 0, root.getRowCount()); - assertFalse("hasNext() should return false on empty ResultSets after initial next() call", iter.hasNext()); + assertEquals( + "VectorSchemaRoot from empty ResultSet should have zero rows", 0, root.getRowCount()); + assertFalse( + "hasNext() should return false on empty ResultSets after initial next() call", + iter.hasNext()); } } } @@ -99,7 +102,8 @@ public void testBasicResultSet() throws Exception { @Test public void testMockDataTypes() throws SQLException { - ResultSetUtility.MockDataElement element = new ResultSetUtility.MockDataElement(1L, Types.NUMERIC); + ResultSetUtility.MockDataElement element = + new ResultSetUtility.MockDataElement(1L, Types.NUMERIC); assertEquals(1L, element.getLong()); assertEquals(1, element.getInt()); assertEquals("1", element.getString()); diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/Table.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/Table.java index 50c4fe6db2a..7c25af68cca 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/Table.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/Table.java @@ -17,14 +17,11 @@ package org.apache.arrow.adapter.jdbc; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import java.math.BigDecimal; import java.nio.charset.StandardCharsets; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; - -/** - * POJO to handle the YAML data from the test YAML file. - */ +/** POJO to handle the YAML data from the test YAML file. */ @JsonIgnoreProperties(ignoreUnknown = true) public class Table { private String name; @@ -39,8 +36,7 @@ public class Table { private String[] vectors; private int rowCount; - public Table() { - } + public Table() {} public String getName() { return name; diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/UnreliableMetaDataTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/UnreliableMetaDataTest.java index 3eb886faabc..ae2451d339e 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/UnreliableMetaDataTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/UnreliableMetaDataTest.java @@ -34,7 +34,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.IntVector; @@ -48,9 +47,7 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -/** - * Test options for dealing with unreliable ResultSetMetaData from JDBC drivers. - */ +/** Test options for dealing with unreliable ResultSetMetaData from JDBC drivers. */ @RunWith(Parameterized.class) public class UnreliableMetaDataTest { private final boolean reuseVectorSchemaRoot; @@ -72,7 +69,7 @@ public void afterEach() { @Parameterized.Parameters(name = "reuseVectorSchemaRoot = {0}") public static Collection getTestData() { - return Arrays.asList(new Object[][] { {false}, {true} }); + return Arrays.asList(new Object[][] {{false}, {true}}); } @Test @@ -91,13 +88,15 @@ public void testUnreliableMetaDataPrecisionAndScale() throws Exception { // reset the ResultSet: rs.beforeFirst(); - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder( - allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) - .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder( + allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) + .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) + .build(); try (ArrowVectorIterator iter = JdbcToArrow.sqlToArrowVectorIterator(rs, config)) { assertTrue(iter.hasNext()); - assertThrows(RuntimeException.class, iter::next, "Expected to fail due to mismatched metadata!"); + assertThrows( + RuntimeException.class, iter::next, "Expected to fail due to mismatched metadata!"); } // reset the ResultSet: @@ -105,11 +104,12 @@ public void testUnreliableMetaDataPrecisionAndScale() throws Exception { JdbcFieldInfo explicitMappingField = new JdbcFieldInfo(Types.DECIMAL, 18, 2); Map explicitMapping = new HashMap<>(); explicitMapping.put(1, explicitMappingField); - config = new JdbcToArrowConfigBuilder( - allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) - .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) - .setExplicitTypesByColumnIndex(explicitMapping) - .build(); + config = + new JdbcToArrowConfigBuilder( + allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) + .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) + .setExplicitTypesByColumnIndex(explicitMapping) + .build(); try (ArrowVectorIterator iter = JdbcToArrow.sqlToArrowVectorIterator(rs, config)) { while (iter.hasNext()) { @@ -133,7 +133,8 @@ public void testInconsistentPrecisionAndScale() throws Exception { assertEquals("Value precision should be 18", 18, bd1.precision()); rs.next(); BigDecimal bd2 = rs.getBigDecimal(1); - assertEquals("Value should be 1000000000300.0000001", new BigDecimal("1000000000300.0000001"), bd2); + assertEquals( + "Value should be 1000000000300.0000001", new BigDecimal("1000000000300.0000001"), bd2); assertEquals("Value scale should be 7", 7, bd2.scale()); assertEquals("Value precision should be 20", 20, bd2.precision()); rs.beforeFirst(); @@ -141,23 +142,27 @@ public void testInconsistentPrecisionAndScale() throws Exception { Map explicitMapping = new HashMap<>(); explicitMapping.put(1, explicitMappingField); - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder( - allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) - .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) - .setExplicitTypesByColumnIndex(explicitMapping) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder( + allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) + .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) + .setExplicitTypesByColumnIndex(explicitMapping) + .build(); try (ArrowVectorIterator iter = JdbcToArrow.sqlToArrowVectorIterator(rs, config)) { assertTrue(iter.hasNext()); - assertThrows(RuntimeException.class, iter::next, + assertThrows( + RuntimeException.class, + iter::next, "This is expected to fail due to inconsistent BigDecimal scales, while strict matching is enabled."); } // Reuse same ResultSet, with RoundingMode.UNNECESSARY set to coerce BigDecimal scale as needed: - config = new JdbcToArrowConfigBuilder( - allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) - .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) - .setExplicitTypesByColumnIndex(explicitMapping) - .setBigDecimalRoundingMode(RoundingMode.UNNECESSARY) - .build(); + config = + new JdbcToArrowConfigBuilder( + allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) + .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) + .setExplicitTypesByColumnIndex(explicitMapping) + .setBigDecimalRoundingMode(RoundingMode.UNNECESSARY) + .build(); try (ArrowVectorIterator iter = JdbcToArrow.sqlToArrowVectorIterator(rs, config)) { while (iter.hasNext()) { VectorSchemaRoot root = iter.next(); @@ -175,23 +180,30 @@ public void testIncorrectNullability() throws Exception { .sqlType(Types.INTEGER) .nullable(ResultSetMetaData.columnNoNulls) .build(); - ResultSetMetaData metadata = new ResultSetUtility.MockResultSetMetaData(Collections.singletonList(columnMetaData)); - final ResultSetUtility.MockResultSet.Builder resultSetBuilder = ResultSetUtility.MockResultSet.builder() - .setMetaData(metadata) - .addDataElement(new ResultSetUtility.MockDataElement(1024, Types.INTEGER)) - .finishRow() - .addDataElement(new ResultSetUtility.MockDataElement(null, Types.INTEGER)) - .finishRow(); - final Schema notNullSchema = new Schema( - Collections.singletonList(Field.notNullable(/*name=*/null, new ArrowType.Int(32, true)))); - final Schema nullSchema = new Schema( - Collections.singletonList(Field.nullable(/*name=*/null, new ArrowType.Int(32, true)))); + ResultSetMetaData metadata = + new ResultSetUtility.MockResultSetMetaData(Collections.singletonList(columnMetaData)); + final ResultSetUtility.MockResultSet.Builder resultSetBuilder = + ResultSetUtility.MockResultSet.builder() + .setMetaData(metadata) + .addDataElement(new ResultSetUtility.MockDataElement(1024, Types.INTEGER)) + .finishRow() + .addDataElement(new ResultSetUtility.MockDataElement(null, Types.INTEGER)) + .finishRow(); + final Schema notNullSchema = + new Schema( + Collections.singletonList( + Field.notNullable(/* name= */ null, new ArrowType.Int(32, true)))); + final Schema nullSchema = + new Schema( + Collections.singletonList( + Field.nullable(/* name= */ null, new ArrowType.Int(32, true)))); try (final ResultSet rs = resultSetBuilder.build()) { - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder( - allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) - .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder( + allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) + .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) + .build(); try (ArrowVectorIterator iter = JdbcToArrow.sqlToArrowVectorIterator(rs, config)) { assertTrue(iter.hasNext()); final VectorSchemaRoot root = iter.next(); @@ -209,14 +221,16 @@ public void testIncorrectNullability() throws Exception { // Override the nullability to get the correct result final Map typeMapping = new HashMap<>(); - JdbcFieldInfo realFieldInfo = new JdbcFieldInfo( - Types.INTEGER, ResultSetMetaData.columnNullable, /*precision*/0, /*scale*/0); + JdbcFieldInfo realFieldInfo = + new JdbcFieldInfo( + Types.INTEGER, ResultSetMetaData.columnNullable, /*precision*/ 0, /*scale*/ 0); typeMapping.put(1, realFieldInfo); - config = new JdbcToArrowConfigBuilder( - allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) - .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) - .setExplicitTypesByColumnIndex(typeMapping) - .build(); + config = + new JdbcToArrowConfigBuilder( + allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) + .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) + .setExplicitTypesByColumnIndex(typeMapping) + .build(); try (ArrowVectorIterator iter = JdbcToArrow.sqlToArrowVectorIterator(rs, config)) { assertTrue(iter.hasNext()); final VectorSchemaRoot root = iter.next(); @@ -232,14 +246,16 @@ public void testIncorrectNullability() throws Exception { rs.beforeFirst(); // columnNullableUnknown won't override the metadata - realFieldInfo = new JdbcFieldInfo( - Types.INTEGER, ResultSetMetaData.columnNullableUnknown, /*precision*/0, /*scale*/0); + realFieldInfo = + new JdbcFieldInfo( + Types.INTEGER, ResultSetMetaData.columnNullableUnknown, /*precision*/ 0, /*scale*/ 0); typeMapping.put(1, realFieldInfo); - config = new JdbcToArrowConfigBuilder( - allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) - .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) - .setExplicitTypesByColumnIndex(typeMapping) - .build(); + config = + new JdbcToArrowConfigBuilder( + allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) + .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) + .setExplicitTypesByColumnIndex(typeMapping) + .build(); try (ArrowVectorIterator iter = JdbcToArrow.sqlToArrowVectorIterator(rs, config)) { assertTrue(iter.hasNext()); final VectorSchemaRoot root = iter.next(); @@ -268,8 +284,8 @@ private ResultSet buildIncorrectPrecisionAndScaleMetaDataResultSet() throws SQLE return ResultSetUtility.MockResultSet.builder() .setMetaData(metadata) .addDataElement( - new ResultSetUtility.MockDataElement(new BigDecimal("1000000000000000.01"), Types.DECIMAL) - ) + new ResultSetUtility.MockDataElement( + new BigDecimal("1000000000000000.01"), Types.DECIMAL)) .finishRow() .build(); } @@ -288,12 +304,12 @@ private ResultSet buildVaryingPrecisionAndScaleResultSet() throws SQLException { return ResultSetUtility.MockResultSet.builder() .setMetaData(metadata) .addDataElement( - new ResultSetUtility.MockDataElement(new BigDecimal("1000000000000000.01"), Types.DECIMAL) - ) + new ResultSetUtility.MockDataElement( + new BigDecimal("1000000000000000.01"), Types.DECIMAL)) .finishRow() .addDataElement( - new ResultSetUtility.MockDataElement(new BigDecimal("1000000000300.0000001"), Types.DECIMAL) - ) + new ResultSetUtility.MockDataElement( + new BigDecimal("1000000000300.0000001"), Types.DECIMAL)) .finishRow() .build(); } diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/consumer/AbstractConsumerTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/consumer/AbstractConsumerTest.java index 96bac42214c..d514780827f 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/consumer/AbstractConsumerTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/consumer/AbstractConsumerTest.java @@ -35,5 +35,4 @@ public void setUp() { public void tearDown() { allocator.close(); } - } diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/consumer/BinaryConsumerTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/consumer/BinaryConsumerTest.java index a368023d490..1ba4112e5f4 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/consumer/BinaryConsumerTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/consumer/BinaryConsumerTest.java @@ -23,7 +23,6 @@ import java.io.ByteArrayInputStream; import java.io.IOException; - import org.apache.arrow.vector.BaseValueVector; import org.apache.arrow.vector.VarBinaryVector; import org.junit.Test; @@ -37,7 +36,8 @@ interface InputStreamConsumer { void consume(BinaryConsumer consumer) throws IOException; } - protected void assertConsume(boolean nullable, InputStreamConsumer dataConsumer, byte[][] expect) throws IOException { + protected void assertConsume(boolean nullable, InputStreamConsumer dataConsumer, byte[][] expect) + throws IOException { try (final VarBinaryVector vector = new VarBinaryVector("binary", allocator)) { BinaryConsumer consumer = BinaryConsumer.createConsumer(vector, 0, nullable); dataConsumer.consume(consumer); @@ -61,51 +61,59 @@ private byte[] createBytes(int length) { return bytes; } - public void testConsumeInputStream(byte[][] values, boolean nullable) throws IOException { - assertConsume(nullable, binaryConsumer -> { - for (byte[] value : values) { - binaryConsumer.consume(new ByteArrayInputStream(value)); - binaryConsumer.moveWriterPosition(); - } - }, values); + assertConsume( + nullable, + binaryConsumer -> { + for (byte[] value : values) { + binaryConsumer.consume(new ByteArrayInputStream(value)); + binaryConsumer.moveWriterPosition(); + } + }, + values); } @Test public void testConsumeInputStream() throws IOException { - testConsumeInputStream(new byte[][]{ - createBytes(DEFAULT_RECORD_BYTE_COUNT) - }, false); - - testConsumeInputStream(new byte[][]{ - createBytes(DEFAULT_RECORD_BYTE_COUNT), - createBytes(DEFAULT_RECORD_BYTE_COUNT) - }, false); - - testConsumeInputStream(new byte[][]{ - createBytes(DEFAULT_RECORD_BYTE_COUNT * 2), - createBytes(DEFAULT_RECORD_BYTE_COUNT), - createBytes(DEFAULT_RECORD_BYTE_COUNT) - }, false); - - testConsumeInputStream(new byte[][]{ - createBytes(INITIAL_VALUE_ALLOCATION * DEFAULT_RECORD_BYTE_COUNT) - }, false); - - testConsumeInputStream(new byte[][]{ - createBytes(INITIAL_VALUE_ALLOCATION * DEFAULT_RECORD_BYTE_COUNT * 10), - }, false); - - testConsumeInputStream(new byte[][]{ - createBytes(INITIAL_VALUE_ALLOCATION * DEFAULT_RECORD_BYTE_COUNT), - createBytes(INITIAL_VALUE_ALLOCATION * DEFAULT_RECORD_BYTE_COUNT) - }, false); - - testConsumeInputStream(new byte[][]{ - createBytes(INITIAL_VALUE_ALLOCATION * DEFAULT_RECORD_BYTE_COUNT), - createBytes(DEFAULT_RECORD_BYTE_COUNT), - createBytes(INITIAL_VALUE_ALLOCATION * DEFAULT_RECORD_BYTE_COUNT) - }, false); + testConsumeInputStream(new byte[][] {createBytes(DEFAULT_RECORD_BYTE_COUNT)}, false); + + testConsumeInputStream( + new byte[][] { + createBytes(DEFAULT_RECORD_BYTE_COUNT), createBytes(DEFAULT_RECORD_BYTE_COUNT) + }, + false); + + testConsumeInputStream( + new byte[][] { + createBytes(DEFAULT_RECORD_BYTE_COUNT * 2), + createBytes(DEFAULT_RECORD_BYTE_COUNT), + createBytes(DEFAULT_RECORD_BYTE_COUNT) + }, + false); + + testConsumeInputStream( + new byte[][] {createBytes(INITIAL_VALUE_ALLOCATION * DEFAULT_RECORD_BYTE_COUNT)}, false); + + testConsumeInputStream( + new byte[][] { + createBytes(INITIAL_VALUE_ALLOCATION * DEFAULT_RECORD_BYTE_COUNT * 10), + }, + false); + + testConsumeInputStream( + new byte[][] { + createBytes(INITIAL_VALUE_ALLOCATION * DEFAULT_RECORD_BYTE_COUNT), + createBytes(INITIAL_VALUE_ALLOCATION * DEFAULT_RECORD_BYTE_COUNT) + }, + false); + + testConsumeInputStream( + new byte[][] { + createBytes(INITIAL_VALUE_ALLOCATION * DEFAULT_RECORD_BYTE_COUNT), + createBytes(DEFAULT_RECORD_BYTE_COUNT), + createBytes(INITIAL_VALUE_ALLOCATION * DEFAULT_RECORD_BYTE_COUNT) + }, + false); byte[][] testRecords = new byte[INITIAL_VALUE_ALLOCATION * 2][]; for (int i = 0; i < testRecords.length; i++) { @@ -113,5 +121,4 @@ public void testConsumeInputStream() throws IOException { } testConsumeInputStream(testRecords, false); } - } diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcAliasToArrowTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcAliasToArrowTest.java index d9acfe88f4f..bc4ecf70f90 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcAliasToArrowTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcAliasToArrowTest.java @@ -28,7 +28,6 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.List; - import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.VectorSchemaRoot; import org.apache.arrow.vector.types.pojo.Field; @@ -40,10 +39,8 @@ public class JdbcAliasToArrowTest { private Connection conn = null; - private static final String CREATE_STATEMENT = - "CREATE TABLE example_table (id INTEGER);"; - private static final String INSERT_STATEMENT = - "INSERT INTO example_table (id) VALUES (?);"; + private static final String CREATE_STATEMENT = "CREATE TABLE example_table (id INTEGER);"; + private static final String INSERT_STATEMENT = "INSERT INTO example_table (id) VALUES (?);"; private static final String QUERY = "SELECT id as a, id as b FROM example_table;"; private static final String DROP_STATEMENT = "DROP TABLE example_table;"; private static final String ORIGINAL_COLUMN_NAME = "ID"; @@ -62,10 +59,9 @@ public void setUp() throws Exception { } /** - * Test h2 database query with alias for column name and column label. - * To verify reading field alias from an H2 database works as expected. - * If this test fails, something is either wrong with the setup, - * or the H2 SQL behavior changed. + * Test h2 database query with alias for column name and column label. To verify reading field + * alias from an H2 database works as expected. If this test fails, something is either wrong with + * the setup, or the H2 SQL behavior changed. */ @Test public void testReadH2Alias() throws Exception { @@ -96,8 +92,8 @@ public void testReadH2Alias() throws Exception { } /** - * Test jdbc query results with alias to arrow works expected. - * Arrow result schema name should be field alias name. + * Test jdbc query results with alias to arrow works expected. Arrow result schema name should be + * field alias name. */ @Test public void testJdbcAliasToArrow() throws Exception { @@ -105,8 +101,7 @@ public void testJdbcAliasToArrow() throws Exception { insertRows(rowCount); try (ResultSet resultSet = conn.createStatement().executeQuery(QUERY)) { - final VectorSchemaRoot vector = - sqlToArrow(resultSet, new RootAllocator(Integer.MAX_VALUE)); + final VectorSchemaRoot vector = sqlToArrow(resultSet, new RootAllocator(Integer.MAX_VALUE)); assertEquals(rowCount, vector.getRowCount()); Schema vectorSchema = vector.getSchema(); diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowArrayTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowArrayTest.java index 377e332b43a..075fee0ad47 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowArrayTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowArrayTest.java @@ -33,7 +33,6 @@ import java.sql.Types; import java.util.HashMap; import java.util.Map; - import org.apache.arrow.adapter.jdbc.JdbcFieldInfo; import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig; import org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder; @@ -53,11 +52,12 @@ public class JdbcToArrowArrayTest { private Connection conn = null; private static final String CREATE_STATEMENT = - "CREATE TABLE array_table (id INTEGER, int_array INTEGER ARRAY, float_array REAL ARRAY, " + - "string_array VARCHAR ARRAY);"; + "CREATE TABLE array_table (id INTEGER, int_array INTEGER ARRAY, float_array REAL ARRAY, " + + "string_array VARCHAR ARRAY);"; private static final String INSERT_STATEMENT = "INSERT INTO array_table (id, int_array, float_array, string_array) VALUES (?, ?, ?, ?);"; - private static final String QUERY = "SELECT int_array, float_array, string_array FROM array_table ORDER BY id;"; + private static final String QUERY = + "SELECT int_array, float_array, string_array FROM array_table ORDER BY id;"; private static final String DROP_STATEMENT = "DROP TABLE array_table;"; private static Map arrayFieldMapping; @@ -157,7 +157,8 @@ public void testJdbcToArrow() throws Exception { insertRows(rowCount, intArrays, floatArrays, strArrays); final JdbcToArrowConfigBuilder builder = - new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), JdbcToArrowUtils.getUtcCalendar(), false); + new JdbcToArrowConfigBuilder( + new RootAllocator(Integer.MAX_VALUE), JdbcToArrowUtils.getUtcCalendar(), false); builder.setArraySubTypeByColumnNameMap(arrayFieldMapping); final JdbcToArrowConfig config = builder.build(); @@ -167,9 +168,12 @@ public void testJdbcToArrow() throws Exception { assertEquals(rowCount, vector.getRowCount()); - assertIntegerVectorEquals((ListVector) vector.getVector(INT_ARRAY_FIELD_NAME), rowCount, intArrays); - assertFloatVectorEquals((ListVector) vector.getVector(FLOAT_ARRAY_FIELD_NAME), rowCount, floatArrays); - assertStringVectorEquals((ListVector) vector.getVector(STRING_ARRAY_FIELD_NAME), rowCount, strArrays); + assertIntegerVectorEquals( + (ListVector) vector.getVector(INT_ARRAY_FIELD_NAME), rowCount, intArrays); + assertFloatVectorEquals( + (ListVector) vector.getVector(FLOAT_ARRAY_FIELD_NAME), rowCount, floatArrays); + assertStringVectorEquals( + (ListVector) vector.getVector(STRING_ARRAY_FIELD_NAME), rowCount, strArrays); } } @@ -178,30 +182,22 @@ public void testJdbcToArrowWithNulls() throws Exception { int rowCount = 4; Integer[][] intArrays = { - null, - {0}, - {1}, - {}, + null, {0}, {1}, {}, }; Float[][] floatArrays = { - { 2.0f }, - null, - { 3.0f }, - {}, + {2.0f}, null, {3.0f}, {}, }; String[][] stringArrays = { - {"4"}, - null, - {"5"}, - {}, + {"4"}, null, {"5"}, {}, }; insertRows(rowCount, intArrays, floatArrays, stringArrays); final JdbcToArrowConfigBuilder builder = - new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), JdbcToArrowUtils.getUtcCalendar(), false); + new JdbcToArrowConfigBuilder( + new RootAllocator(Integer.MAX_VALUE), JdbcToArrowUtils.getUtcCalendar(), false); builder.setArraySubTypeByColumnNameMap(arrayFieldMapping); final JdbcToArrowConfig config = builder.build(); @@ -211,13 +207,17 @@ public void testJdbcToArrowWithNulls() throws Exception { assertEquals(rowCount, vector.getRowCount()); - assertIntegerVectorEquals((ListVector) vector.getVector(INT_ARRAY_FIELD_NAME), rowCount, intArrays); - assertFloatVectorEquals((ListVector) vector.getVector(FLOAT_ARRAY_FIELD_NAME), rowCount, floatArrays); - assertStringVectorEquals((ListVector) vector.getVector(STRING_ARRAY_FIELD_NAME), rowCount, stringArrays); + assertIntegerVectorEquals( + (ListVector) vector.getVector(INT_ARRAY_FIELD_NAME), rowCount, intArrays); + assertFloatVectorEquals( + (ListVector) vector.getVector(FLOAT_ARRAY_FIELD_NAME), rowCount, floatArrays); + assertStringVectorEquals( + (ListVector) vector.getVector(STRING_ARRAY_FIELD_NAME), rowCount, stringArrays); } } - private void assertIntegerVectorEquals(ListVector listVector, int rowCount, Integer[][] expectedValues) { + private void assertIntegerVectorEquals( + ListVector listVector, int rowCount, Integer[][] expectedValues) { IntVector vector = (IntVector) listVector.getDataVector(); ArrowBuf offsetBuffer = listVector.getOffsetBuffer(); @@ -242,7 +242,8 @@ private void assertIntegerVectorEquals(ListVector listVector, int rowCount, Inte } } - private void assertFloatVectorEquals(ListVector listVector, int rowCount, Float[][] expectedValues) { + private void assertFloatVectorEquals( + ListVector listVector, int rowCount, Float[][] expectedValues) { Float4Vector vector = (Float4Vector) listVector.getDataVector(); ArrowBuf offsetBuffer = listVector.getOffsetBuffer(); @@ -267,7 +268,8 @@ private void assertFloatVectorEquals(ListVector listVector, int rowCount, Float[ } } - private void assertStringVectorEquals(ListVector listVector, int rowCount, String[][] expectedValues) { + private void assertStringVectorEquals( + ListVector listVector, int rowCount, String[][] expectedValues) { VarCharVector vector = (VarCharVector) listVector.getDataVector(); ArrowBuf offsetBuffer = listVector.getOffsetBuffer(); @@ -308,7 +310,7 @@ private Integer[][] generateIntegerArrayField(int numRows) { for (int i = 0; i < numRows; ++i) { int val = i * 4; - result[i] = new Integer[]{val, val + 1, val + 2, val + 3}; + result[i] = new Integer[] {val, val + 1, val + 2, val + 3}; } return result; @@ -316,10 +318,10 @@ private Integer[][] generateIntegerArrayField(int numRows) { private Float[][] generateFloatArrayField(int numRows) { Float[][] result = new Float[numRows][]; - + for (int i = 0; i < numRows; ++i) { int val = i * 4; - result[i] = new Float[]{(float) val, (float) val + 1, (float) val + 2, (float) val + 3}; + result[i] = new Float[] {(float) val, (float) val + 1, (float) val + 2, (float) val + 3}; } return result; @@ -330,22 +332,21 @@ private String[][] generateStringArrayField(int numRows) { for (int i = 0; i < numRows; ++i) { int val = i * 4; - result[i] = new String[]{ - String.valueOf(val), - String.valueOf(val + 1), - String.valueOf(val + 2), - String.valueOf(val + 3) }; + result[i] = + new String[] { + String.valueOf(val), + String.valueOf(val + 1), + String.valueOf(val + 2), + String.valueOf(val + 3) + }; } return result; } private void insertRows( - int numRows, - Integer[][] integerArrays, - Float[][] floatArrays, - String[][] strArrays) - throws SQLException { + int numRows, Integer[][] integerArrays, Float[][] floatArrays, String[][] strArrays) + throws SQLException { // Insert 4 Rows try (PreparedStatement stmt = conn.prepareStatement(INSERT_STATEMENT)) { diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowCharSetTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowCharSetTest.java index 422b55070aa..1f28d077f71 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowCharSetTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowCharSetTest.java @@ -29,7 +29,6 @@ import java.util.Arrays; import java.util.Calendar; import java.util.Collection; - import org.apache.arrow.adapter.jdbc.AbstractJdbcToArrowTest; import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig; import org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder; @@ -47,8 +46,8 @@ import org.junit.runners.Parameterized.Parameters; /** - * JUnit Test Class which contains methods to test JDBC to Arrow data conversion functionality with UTF-8 Charset, - * including the multi-byte CJK characters for H2 database. + * JUnit Test Class which contains methods to test JDBC to Arrow data conversion functionality with + * UTF-8 Charset, including the multi-byte CJK characters for H2 database. */ @RunWith(Parameterized.class) public class JdbcToArrowCharSetTest extends AbstractJdbcToArrowTest { @@ -81,7 +80,7 @@ public void setUp() throws SQLException, ClassNotFoundException { String driver = "org.h2.Driver"; Class.forName(driver); conn = DriverManager.getConnection(url); - try (Statement stmt = conn.createStatement();) { + try (Statement stmt = conn.createStatement(); ) { stmt.executeUpdate(table.getCreate()); for (String insert : table.getData()) { stmt.executeUpdate(insert); @@ -98,38 +97,58 @@ public void setUp() throws SQLException, ClassNotFoundException { * @throws IOException on error */ @Parameters - public static Collection getTestData() throws SQLException, ClassNotFoundException, IOException { + public static Collection getTestData() + throws SQLException, ClassNotFoundException, IOException { return Arrays.asList(prepareTestData(testFiles, JdbcToArrowCharSetTest.class)); } /** - * Test Method to test JdbcToArrow Functionality for various H2 DB based datatypes with UTF-8 Charset, including - * the multi-byte CJK characters. + * Test Method to test JdbcToArrow Functionality for various H2 DB based datatypes with UTF-8 + * Charset, including the multi-byte CJK characters. */ @Test public void testJdbcToArrowValues() throws SQLException, IOException { - testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE), - Calendar.getInstance()), false); + testDataSets( + sqlToArrow( + conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()), + false); testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE)), false); - testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), - new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()), false); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), + new RootAllocator(Integer.MAX_VALUE), + Calendar.getInstance()), + false); testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery())), false); - testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), - new RootAllocator(Integer.MAX_VALUE)), false); - testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), - Calendar.getInstance()), false); - testDataSets(sqlToArrow( - conn.createStatement().executeQuery(table.getQuery()), - new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()).build()), false); - testDataSets(sqlToArrow( - conn, - table.getQuery(), - new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()).build()), false); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), + new RootAllocator(Integer.MAX_VALUE)), + false); + testDataSets( + sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), Calendar.getInstance()), + false); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), + new JdbcToArrowConfigBuilder( + new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) + .build()), + false); + testDataSets( + sqlToArrow( + conn, + table.getQuery(), + new JdbcToArrowConfigBuilder( + new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) + .build()), + false); } @Test public void testJdbcSchemaMetadata() throws SQLException { - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(0), Calendar.getInstance(), true).build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder(new RootAllocator(0), Calendar.getInstance(), true).build(); ResultSetMetaData rsmd = conn.createStatement().executeQuery(table.getQuery()).getMetaData(); Schema schema = JdbcToArrowUtils.jdbcToArrowSchema(rsmd, config); JdbcToArrowTestHelper.assertFieldMetadataMatchesResultSetMetadata(rsmd, schema); @@ -139,19 +158,25 @@ public void testJdbcSchemaMetadata() throws SQLException { * This method calls the assert methods for various DataSets. * * @param root VectorSchemaRoot for test - * @param isIncludeMapVector is this dataset checks includes map column. - * Jdbc type to 'map' mapping declared in configuration only manually + * @param isIncludeMapVector is this dataset checks includes map column. Jdbc type to 'map' + * mapping declared in configuration only manually */ public void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector) { JdbcToArrowTestHelper.assertFieldMetadataIsEmpty(root); - assertVarcharVectorValues((VarCharVector) root.getVector(CLOB), table.getRowCount(), + assertVarcharVectorValues( + (VarCharVector) root.getVector(CLOB), + table.getRowCount(), getCharArrayWithCharSet(table.getValues(), CLOB, StandardCharsets.UTF_8)); - assertVarcharVectorValues((VarCharVector) root.getVector(VARCHAR), table.getRowCount(), + assertVarcharVectorValues( + (VarCharVector) root.getVector(VARCHAR), + table.getRowCount(), getCharArrayWithCharSet(table.getValues(), VARCHAR, StandardCharsets.UTF_8)); - assertVarcharVectorValues((VarCharVector) root.getVector(CHAR), table.getRowCount(), + assertVarcharVectorValues( + (VarCharVector) root.getVector(CHAR), + table.getRowCount(), getCharArrayWithCharSet(table.getValues(), CHAR, StandardCharsets.UTF_8)); } } diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowDataTypesTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowDataTypesTest.java index ae4fffd0f94..04f178cab28 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowDataTypesTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowDataTypesTest.java @@ -40,7 +40,6 @@ import java.util.Arrays; import java.util.Calendar; import java.util.Collection; - import org.apache.arrow.adapter.jdbc.AbstractJdbcToArrowTest; import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig; import org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder; @@ -71,8 +70,8 @@ import org.junit.runners.Parameterized.Parameters; /** - * JUnit Test Class which contains methods to test JDBC to Arrow data conversion functionality with various data types - * for H2 database using multiple test data files. + * JUnit Test Class which contains methods to test JDBC to Arrow data conversion functionality with + * various data types for H2 database using multiple test data files. */ @RunWith(Parameterized.class) public class JdbcToArrowDataTypesTest extends AbstractJdbcToArrowTest { @@ -137,42 +136,59 @@ public JdbcToArrowDataTypesTest(Table table) { * @throws IOException on error */ @Parameters - public static Collection getTestData() throws SQLException, ClassNotFoundException, IOException { + public static Collection getTestData() + throws SQLException, ClassNotFoundException, IOException { return Arrays.asList(prepareTestData(testFiles, JdbcToArrowDataTypesTest.class)); } - /** - * Test Method to test JdbcToArrow Functionality for various H2 DB based datatypes. - */ + /** Test Method to test JdbcToArrow Functionality for various H2 DB based datatypes. */ @Test public void testJdbcToArrowValues() throws SQLException, IOException { - testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE), - Calendar.getInstance()), false); + testDataSets( + sqlToArrow( + conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()), + false); testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE)), false); - testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), - new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()), false); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), + new RootAllocator(Integer.MAX_VALUE), + Calendar.getInstance()), + false); testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery())), false); - testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), - new RootAllocator(Integer.MAX_VALUE)), false); - testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), Calendar.getInstance()), false); - testDataSets(sqlToArrow( - conn.createStatement().executeQuery(table.getQuery()), - new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build()), false); - testDataSets(sqlToArrow( - conn, - table.getQuery(), - new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build()), false); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), + new RootAllocator(Integer.MAX_VALUE)), + false); + testDataSets( + sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), Calendar.getInstance()), + false); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), + new JdbcToArrowConfigBuilder( + new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build()), + false); + testDataSets( + sqlToArrow( + conn, + table.getQuery(), + new JdbcToArrowConfigBuilder( + new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build()), + false); } @Test public void testJdbcSchemaMetadata() throws SQLException { - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(0), Calendar.getInstance(), true) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder(new RootAllocator(0), Calendar.getInstance(), true) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build(); ResultSetMetaData rsmd = conn.createStatement().executeQuery(table.getQuery()).getMetaData(); Schema schema = JdbcToArrowUtils.jdbcToArrowSchema(rsmd, config); JdbcToArrowTestHelper.assertFieldMetadataMatchesResultSetMetadata(rsmd, schema); @@ -182,77 +198,107 @@ public void testJdbcSchemaMetadata() throws SQLException { * This method calls the assert methods for various DataSets. * * @param root VectorSchemaRoot for test - * @param isIncludeMapVector is this dataset checks includes map column. - * Jdbc type to 'map' mapping declared in configuration only manually + * @param isIncludeMapVector is this dataset checks includes map column. Jdbc type to 'map' + * mapping declared in configuration only manually */ public void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector) { JdbcToArrowTestHelper.assertFieldMetadataIsEmpty(root); switch (table.getType()) { case BIGINT: - assertBigIntVectorValues((BigIntVector) root.getVector(table.getVector()), table.getValues().length, + assertBigIntVectorValues( + (BigIntVector) root.getVector(table.getVector()), + table.getValues().length, table.getLongValues()); break; case BINARY: case BLOB: - assertVarBinaryVectorValues((VarBinaryVector) root.getVector(table.getVector()), table.getValues().length, + assertVarBinaryVectorValues( + (VarBinaryVector) root.getVector(table.getVector()), + table.getValues().length, table.getBinaryValues()); break; case BIT: - assertBitVectorValues((BitVector) root.getVector(table.getVector()), table.getValues().length, + assertBitVectorValues( + (BitVector) root.getVector(table.getVector()), + table.getValues().length, table.getIntValues()); break; case BOOL: - assertBooleanVectorValues((BitVector) root.getVector(table.getVector()), table.getValues().length, + assertBooleanVectorValues( + (BitVector) root.getVector(table.getVector()), + table.getValues().length, table.getBoolValues()); break; case CHAR: case VARCHAR: case CLOB: - assertVarcharVectorValues((VarCharVector) root.getVector(table.getVector()), table.getValues().length, + assertVarcharVectorValues( + (VarCharVector) root.getVector(table.getVector()), + table.getValues().length, table.getCharValues()); break; case DATE: - assertDateVectorValues((DateDayVector) root.getVector(table.getVector()), table.getValues().length, + assertDateVectorValues( + (DateDayVector) root.getVector(table.getVector()), + table.getValues().length, table.getIntValues()); break; case TIME: - assertTimeVectorValues((TimeMilliVector) root.getVector(table.getVector()), table.getValues().length, + assertTimeVectorValues( + (TimeMilliVector) root.getVector(table.getVector()), + table.getValues().length, table.getLongValues()); break; case TIMESTAMP: - assertTimeStampVectorValues((TimeStampVector) root.getVector(table.getVector()), table.getValues().length, + assertTimeStampVectorValues( + (TimeStampVector) root.getVector(table.getVector()), + table.getValues().length, table.getLongValues()); break; case DECIMAL: - assertDecimalVectorValues((DecimalVector) root.getVector(table.getVector()), table.getValues().length, + assertDecimalVectorValues( + (DecimalVector) root.getVector(table.getVector()), + table.getValues().length, table.getBigDecimalValues()); break; case DOUBLE: - assertFloat8VectorValues((Float8Vector) root.getVector(table.getVector()), table.getValues().length, + assertFloat8VectorValues( + (Float8Vector) root.getVector(table.getVector()), + table.getValues().length, table.getDoubleValues()); break; case INT: - assertIntVectorValues((IntVector) root.getVector(table.getVector()), table.getValues().length, + assertIntVectorValues( + (IntVector) root.getVector(table.getVector()), + table.getValues().length, table.getIntValues()); break; case SMALLINT: - assertSmallIntVectorValues((SmallIntVector) root.getVector(table.getVector()), table.getValues().length, + assertSmallIntVectorValues( + (SmallIntVector) root.getVector(table.getVector()), + table.getValues().length, table.getIntValues()); break; case TINYINT: - assertTinyIntVectorValues((TinyIntVector) root.getVector(table.getVector()), table.getValues().length, + assertTinyIntVectorValues( + (TinyIntVector) root.getVector(table.getVector()), + table.getValues().length, table.getIntValues()); break; case REAL: - assertFloat4VectorValues((Float4Vector) root.getVector(table.getVector()), table.getValues().length, + assertFloat4VectorValues( + (Float4Vector) root.getVector(table.getVector()), + table.getValues().length, table.getFloatValues()); break; case NULL: assertNullVectorValues((NullVector) root.getVector(table.getVector()), table.getRowCount()); break; case LIST: - assertListVectorValues((ListVector) root.getVector(table.getVector()), table.getValues().length, + assertListVectorValues( + (ListVector) root.getVector(table.getVector()), + table.getValues().length, table.getListValues()); break; default: @@ -261,4 +307,3 @@ public void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector) { } } } - diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowMapDataTypeTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowMapDataTypeTest.java index 43862a93c39..b516e2a19e9 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowMapDataTypeTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowMapDataTypeTest.java @@ -24,7 +24,6 @@ import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.Calendar; - import org.apache.arrow.adapter.jdbc.AbstractJdbcToArrowTest; import org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder; import org.apache.arrow.memory.RootAllocator; @@ -32,44 +31,46 @@ import org.apache.arrow.vector.complex.MapVector; import org.junit.Test; -/** - * Test MapConsumer with OTHER jdbc type. - */ +/** Test MapConsumer with OTHER jdbc type. */ public class JdbcToArrowMapDataTypeTest extends AbstractJdbcToArrowTest { public JdbcToArrowMapDataTypeTest() throws IOException { this.table = getTable("h2/test1_map_h2.yml", JdbcToArrowMapDataTypeTest.class); } - /** - * Test Method to test JdbcToArrow Functionality for Map form Types.OTHER column - */ + /** Test Method to test JdbcToArrow Functionality for Map form Types.OTHER column */ @Test public void testJdbcToArrowValues() throws SQLException, IOException { Calendar calendar = Calendar.getInstance(); ResultSetMetaData rsmd = getQueryMetaData(table.getQuery()); - testDataSets(sqlToArrow( + testDataSets( + sqlToArrow( conn.createStatement().executeQuery(table.getQuery()), - new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) - .setJdbcToArrowTypeConverter(jdbcToArrowTypeConverter(calendar, rsmd)) - .build()), true); - testDataSets(sqlToArrow( + new JdbcToArrowConfigBuilder( + new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) + .setJdbcToArrowTypeConverter(jdbcToArrowTypeConverter(calendar, rsmd)) + .build()), + true); + testDataSets( + sqlToArrow( conn, table.getQuery(), - new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) - .setJdbcToArrowTypeConverter(jdbcToArrowTypeConverter(calendar, rsmd)) - .build()), true); + new JdbcToArrowConfigBuilder( + new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) + .setJdbcToArrowTypeConverter(jdbcToArrowTypeConverter(calendar, rsmd)) + .build()), + true); } /** * This method calls the assert methods for various DataSets. * * @param root VectorSchemaRoot for test - * @param isIncludeMapVector is this dataset checks includes map column. - * Jdbc type to 'map' mapping declared in configuration only manually + * @param isIncludeMapVector is this dataset checks includes map column. Jdbc type to 'map' + * mapping declared in configuration only manually */ public void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector) { - assertMapVectorValues((MapVector) root.getVector(MAP), table.getRowCount(), - getMapValues(table.getValues(), MAP)); + assertMapVectorValues( + (MapVector) root.getVector(MAP), table.getRowCount(), getMapValues(table.getValues(), MAP)); } } diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowNullTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowNullTest.java index 5731f27c5b3..cc4c8e5fa59 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowNullTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowNullTest.java @@ -51,7 +51,6 @@ import java.util.Arrays; import java.util.Calendar; import java.util.Collection; - import org.apache.arrow.adapter.jdbc.AbstractJdbcToArrowTest; import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig; import org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder; @@ -82,8 +81,8 @@ import org.junit.runners.Parameterized.Parameters; /** - * JUnit Test Class which contains methods to test JDBC to Arrow data conversion functionality with null values for - * H2 database. + * JUnit Test Class which contains methods to test JDBC to Arrow data conversion functionality with + * null values for H2 database. */ @RunWith(Parameterized.class) public class JdbcToArrowNullTest extends AbstractJdbcToArrowTest { @@ -116,46 +115,66 @@ public JdbcToArrowNullTest(Table table) { * @throws IOException on error */ @Parameters - public static Collection getTestData() throws SQLException, ClassNotFoundException, IOException { + public static Collection getTestData() + throws SQLException, ClassNotFoundException, IOException { return Arrays.asList(prepareTestData(testFiles, JdbcToArrowNullTest.class)); } /** - * Test Method to test JdbcToArrow Functionality for various H2 DB based datatypes with null values. + * Test Method to test JdbcToArrow Functionality for various H2 DB based datatypes with null + * values. */ @Test public void testJdbcToArrowValues() throws SQLException, IOException { - testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE), - Calendar.getInstance()), false); + testDataSets( + sqlToArrow( + conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()), + false); testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE)), false); - testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), - new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()), false); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), + new RootAllocator(Integer.MAX_VALUE), + Calendar.getInstance()), + false); testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery())), false); - testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), - new RootAllocator(Integer.MAX_VALUE)), false); - testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), Calendar.getInstance()), false); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), + new RootAllocator(Integer.MAX_VALUE)), + false); + testDataSets( + sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), Calendar.getInstance()), + false); Calendar calendar = Calendar.getInstance(); ResultSetMetaData rsmd = getQueryMetaData(table.getQuery()); - testDataSets(sqlToArrow( - conn.createStatement().executeQuery(table.getQuery()), - new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .setJdbcToArrowTypeConverter(jdbcToArrowTypeConverter(calendar, rsmd)) - .build()), true); - testDataSets(sqlToArrow( - conn, - table.getQuery(), - new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .setJdbcToArrowTypeConverter(jdbcToArrowTypeConverter(calendar, rsmd)) - .build()), true); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), + new JdbcToArrowConfigBuilder( + new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .setJdbcToArrowTypeConverter(jdbcToArrowTypeConverter(calendar, rsmd)) + .build()), + true); + testDataSets( + sqlToArrow( + conn, + table.getQuery(), + new JdbcToArrowConfigBuilder( + new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .setJdbcToArrowTypeConverter(jdbcToArrowTypeConverter(calendar, rsmd)) + .build()), + true); } @Test public void testJdbcSchemaMetadata() throws SQLException { - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(0), Calendar.getInstance(), true) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder(new RootAllocator(0), Calendar.getInstance(), true) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build(); ResultSetMetaData rsmd = conn.createStatement().executeQuery(table.getQuery()).getMetaData(); Schema schema = JdbcToArrowUtils.jdbcToArrowSchema(rsmd, config); JdbcToArrowTestHelper.assertFieldMetadataMatchesResultSetMetadata(rsmd, schema); @@ -165,8 +184,8 @@ public void testJdbcSchemaMetadata() throws SQLException { * This method calls the assert methods for various DataSets. * * @param root VectorSchemaRoot for test - * @param isIncludeMapVector is this dataset checks includes map column. - * Jdbc type to 'map' mapping declared in configuration only manually + * @param isIncludeMapVector is this dataset checks includes map column. Jdbc type to 'map' + * mapping declared in configuration only manually */ public void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector) { JdbcToArrowTestHelper.assertFieldMetadataIsEmpty(root); @@ -176,7 +195,8 @@ public void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector) { sqlToArrowTestNullValues(table.getVectors(), root, table.getRowCount()); break; case SELECTED_NULL_COLUMN: - sqlToArrowTestSelectedNullColumnsValues(table.getVectors(), root, table.getRowCount(), isIncludeMapVector); + sqlToArrowTestSelectedNullColumnsValues( + table.getVectors(), root, table.getRowCount(), isIncludeMapVector); break; case SELECTED_NULL_ROW: testAllVectorValues(root, isIncludeMapVector); @@ -190,62 +210,96 @@ public void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector) { private void testAllVectorValues(VectorSchemaRoot root, boolean isIncludeMapVector) { JdbcToArrowTestHelper.assertFieldMetadataIsEmpty(root); - assertBigIntVectorValues((BigIntVector) root.getVector(BIGINT), table.getRowCount(), + assertBigIntVectorValues( + (BigIntVector) root.getVector(BIGINT), + table.getRowCount(), getLongValues(table.getValues(), BIGINT)); - assertTinyIntVectorValues((TinyIntVector) root.getVector(TINYINT), table.getRowCount(), + assertTinyIntVectorValues( + (TinyIntVector) root.getVector(TINYINT), + table.getRowCount(), getIntValues(table.getValues(), TINYINT)); - assertSmallIntVectorValues((SmallIntVector) root.getVector(SMALLINT), table.getRowCount(), + assertSmallIntVectorValues( + (SmallIntVector) root.getVector(SMALLINT), + table.getRowCount(), getIntValues(table.getValues(), SMALLINT)); - assertVarBinaryVectorValues((VarBinaryVector) root.getVector(BINARY), table.getRowCount(), + assertVarBinaryVectorValues( + (VarBinaryVector) root.getVector(BINARY), + table.getRowCount(), getBinaryValues(table.getValues(), BINARY)); - assertVarBinaryVectorValues((VarBinaryVector) root.getVector(BLOB), table.getRowCount(), + assertVarBinaryVectorValues( + (VarBinaryVector) root.getVector(BLOB), + table.getRowCount(), getBinaryValues(table.getValues(), BLOB)); - assertVarcharVectorValues((VarCharVector) root.getVector(CLOB), table.getRowCount(), + assertVarcharVectorValues( + (VarCharVector) root.getVector(CLOB), + table.getRowCount(), getCharArray(table.getValues(), CLOB)); - assertVarcharVectorValues((VarCharVector) root.getVector(VARCHAR), table.getRowCount(), + assertVarcharVectorValues( + (VarCharVector) root.getVector(VARCHAR), + table.getRowCount(), getCharArray(table.getValues(), VARCHAR)); - assertVarcharVectorValues((VarCharVector) root.getVector(CHAR), table.getRowCount(), + assertVarcharVectorValues( + (VarCharVector) root.getVector(CHAR), + table.getRowCount(), getCharArray(table.getValues(), CHAR)); - assertIntVectorValues((IntVector) root.getVector(INT), table.getRowCount(), - getIntValues(table.getValues(), INT)); + assertIntVectorValues( + (IntVector) root.getVector(INT), table.getRowCount(), getIntValues(table.getValues(), INT)); - assertBitVectorValues((BitVector) root.getVector(BIT), table.getRowCount(), - getIntValues(table.getValues(), BIT)); + assertBitVectorValues( + (BitVector) root.getVector(BIT), table.getRowCount(), getIntValues(table.getValues(), BIT)); - assertBooleanVectorValues((BitVector) root.getVector(BOOL), table.getRowCount(), + assertBooleanVectorValues( + (BitVector) root.getVector(BOOL), + table.getRowCount(), getBooleanValues(table.getValues(), BOOL)); - assertDateVectorValues((DateDayVector) root.getVector(DATE), table.getRowCount(), + assertDateVectorValues( + (DateDayVector) root.getVector(DATE), + table.getRowCount(), getIntValues(table.getValues(), DATE)); - assertTimeVectorValues((TimeMilliVector) root.getVector(TIME), table.getRowCount(), + assertTimeVectorValues( + (TimeMilliVector) root.getVector(TIME), + table.getRowCount(), getLongValues(table.getValues(), TIME)); - assertTimeStampVectorValues((TimeStampVector) root.getVector(TIMESTAMP), table.getRowCount(), + assertTimeStampVectorValues( + (TimeStampVector) root.getVector(TIMESTAMP), + table.getRowCount(), getLongValues(table.getValues(), TIMESTAMP)); - assertDecimalVectorValues((DecimalVector) root.getVector(DECIMAL), table.getRowCount(), + assertDecimalVectorValues( + (DecimalVector) root.getVector(DECIMAL), + table.getRowCount(), getDecimalValues(table.getValues(), DECIMAL)); - assertFloat8VectorValues((Float8Vector) root.getVector(DOUBLE), table.getRowCount(), + assertFloat8VectorValues( + (Float8Vector) root.getVector(DOUBLE), + table.getRowCount(), getDoubleValues(table.getValues(), DOUBLE)); - assertFloat4VectorValues((Float4Vector) root.getVector(REAL), table.getRowCount(), + assertFloat4VectorValues( + (Float4Vector) root.getVector(REAL), + table.getRowCount(), getFloatValues(table.getValues(), REAL)); - assertListVectorValues((ListVector) root.getVector(LIST), table.getRowCount(), + assertListVectorValues( + (ListVector) root.getVector(LIST), + table.getRowCount(), getListValues(table.getValues(), LIST)); if (isIncludeMapVector) { - assertMapVectorValues((MapVector) root.getVector(MAP), table.getRowCount(), - getMapValues(table.getValues(), MAP)); + assertMapVectorValues( + (MapVector) root.getVector(MAP), + table.getRowCount(), + getMapValues(table.getValues(), MAP)); } } @@ -283,11 +337,11 @@ public void sqlToArrowTestNullValues(String[] vectors, VectorSchemaRoot root, in * @param vectors Vectors to test * @param root VectorSchemaRoot for test * @param rowCount number of rows - * @param isIncludeMapVector is this dataset checks includes map column. - * Jdbc type to 'map' mapping declared in configuration only manually + * @param isIncludeMapVector is this dataset checks includes map column. Jdbc type to 'map' + * mapping declared in configuration only manually */ - public void sqlToArrowTestSelectedNullColumnsValues(String[] vectors, VectorSchemaRoot root, int rowCount, - boolean isIncludeMapVector) { + public void sqlToArrowTestSelectedNullColumnsValues( + String[] vectors, VectorSchemaRoot root, int rowCount, boolean isIncludeMapVector) { assertNullValues((BigIntVector) root.getVector(vectors[0]), rowCount); assertNullValues((DecimalVector) root.getVector(vectors[1]), rowCount); assertNullValues((Float8Vector) root.getVector(vectors[2]), rowCount); @@ -306,5 +360,4 @@ public void sqlToArrowTestSelectedNullColumnsValues(String[] vectors, VectorSche assertNullValues((MapVector) root.getVector(vectors[14]), rowCount); } } - } diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowOptionalColumnsTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowOptionalColumnsTest.java index eebcbe64c0e..0e11835c521 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowOptionalColumnsTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowOptionalColumnsTest.java @@ -24,7 +24,6 @@ import java.sql.SQLException; import java.util.Arrays; import java.util.Collection; - import org.apache.arrow.adapter.jdbc.AbstractJdbcToArrowTest; import org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper; import org.apache.arrow.adapter.jdbc.Table; @@ -40,9 +39,7 @@ */ @RunWith(Parameterized.class) public class JdbcToArrowOptionalColumnsTest extends AbstractJdbcToArrowTest { - private static final String[] testFiles = { - "h2/test1_null_and_notnull.yml" - }; + private static final String[] testFiles = {"h2/test1_null_and_notnull.yml"}; /** * Constructor which populates the table object for each test iteration. @@ -57,17 +54,19 @@ public JdbcToArrowOptionalColumnsTest(Table table) { * Get the test data as a collection of Table objects for each test iteration. * * @return Collection of Table objects - * @throws SQLException on error + * @throws SQLException on error * @throws ClassNotFoundException on error - * @throws IOException on error + * @throws IOException on error */ @Parameterized.Parameters - public static Collection getTestData() throws SQLException, ClassNotFoundException, IOException { + public static Collection getTestData() + throws SQLException, ClassNotFoundException, IOException { return Arrays.asList(prepareTestData(testFiles, JdbcToArrowOptionalColumnsTest.class)); } /** - * Test Method to test JdbcToArrow Functionality for dealing with nullable and non-nullable columns. + * Test Method to test JdbcToArrow Functionality for dealing with nullable and non-nullable + * columns. */ @Test public void testJdbcToArrowValues() throws SQLException, IOException { @@ -75,12 +74,13 @@ public void testJdbcToArrowValues() throws SQLException, IOException { } /** - * This method calls the assert methods for various DataSets. We verify that a SQL `NULL` column becomes - * nullable in the VectorSchemaRoot, and that a SQL `NOT NULL` column becomes non-nullable. + * This method calls the assert methods for various DataSets. We verify that a SQL `NULL` column + * becomes nullable in the VectorSchemaRoot, and that a SQL `NOT NULL` column becomes + * non-nullable. * * @param root VectorSchemaRoot for test - * @param isIncludeMapVector is this dataset checks includes map column. - * Jdbc type to 'map' mapping declared in configuration only manually + * @param isIncludeMapVector is this dataset checks includes map column. Jdbc type to 'map' + * mapping declared in configuration only manually */ public void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector) { JdbcToArrowTestHelper.assertFieldMetadataIsEmpty(root); @@ -88,5 +88,4 @@ public void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector) { assertTrue(root.getSchema().getFields().get(0).isNullable()); assertFalse(root.getSchema().getFields().get(1).isNullable()); } - } diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowTest.java index 7641fa7f165..16072c894a6 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowTest.java @@ -29,7 +29,6 @@ import java.util.Collection; import java.util.stream.Collectors; import java.util.stream.Stream; - import org.apache.arrow.adapter.jdbc.AbstractJdbcToArrowTest; import org.apache.arrow.adapter.jdbc.ArrowVectorIterator; import org.apache.arrow.adapter.jdbc.JdbcToArrow; @@ -64,8 +63,8 @@ import org.junit.runners.Parameterized; /** - * JUnit Test Class which contains methods to test JDBC to Arrow data conversion functionality with various data types - * for H2 database using single test data file. + * JUnit Test Class which contains methods to test JDBC to Arrow data conversion functionality with + * various data types for H2 database using single test data file. */ @RunWith(Parameterized.class) public class JdbcToArrowTest extends AbstractJdbcToArrowTest { @@ -92,52 +91,71 @@ public JdbcToArrowTest(Table table, boolean reuseVectorSchemaRoot) { * @throws IOException on error */ @Parameterized.Parameters(name = "table = {0}, reuse batch = {1}") - public static Collection getTestData() throws SQLException, ClassNotFoundException, IOException { - return Arrays.stream(prepareTestData(testFiles, JdbcToArrowTest.class)).flatMap(row -> - Stream.of(new Object[] {row[0], true}, new Object[] {row[0], false})).collect(Collectors.toList()); + public static Collection getTestData() + throws SQLException, ClassNotFoundException, IOException { + return Arrays.stream(prepareTestData(testFiles, JdbcToArrowTest.class)) + .flatMap(row -> Stream.of(new Object[] {row[0], true}, new Object[] {row[0], false})) + .collect(Collectors.toList()); } /** - * Test Method to test JdbcToArrow Functionality for various H2 DB based datatypes with only one test data file. + * Test Method to test JdbcToArrow Functionality for various H2 DB based datatypes with only one + * test data file. */ @Test public void testJdbcToArrowValues() throws SQLException, IOException { - testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE), - Calendar.getInstance()), false); + testDataSets( + sqlToArrow( + conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()), + false); testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE)), false); - testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), - new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()), false); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), + new RootAllocator(Integer.MAX_VALUE), + Calendar.getInstance()), + false); testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery())), false); - testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), - new RootAllocator(Integer.MAX_VALUE)), false); - testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), - Calendar.getInstance()), false); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), + new RootAllocator(Integer.MAX_VALUE)), + false); + testDataSets( + sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), Calendar.getInstance()), + false); Calendar calendar = Calendar.getInstance(); ResultSetMetaData rsmd = getQueryMetaData(table.getQuery()); - testDataSets(sqlToArrow( - conn.createStatement().executeQuery(table.getQuery()), - new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .setJdbcToArrowTypeConverter(jdbcToArrowTypeConverter(calendar, rsmd)) - .build()), true); - testDataSets(sqlToArrow( - conn, - table.getQuery(), - new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), calendar) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .setJdbcToArrowTypeConverter(jdbcToArrowTypeConverter(calendar, rsmd)) - .build()), true); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), + new JdbcToArrowConfigBuilder( + new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .setJdbcToArrowTypeConverter(jdbcToArrowTypeConverter(calendar, rsmd)) + .build()), + true); + testDataSets( + sqlToArrow( + conn, + table.getQuery(), + new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), calendar) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .setJdbcToArrowTypeConverter(jdbcToArrowTypeConverter(calendar, rsmd)) + .build()), + true); } @Test public void testJdbcSchemaMetadata() throws SQLException { Calendar calendar = Calendar.getInstance(); ResultSetMetaData rsmd = getQueryMetaData(table.getQuery()); - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(0), calendar, true) - .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) - .setJdbcToArrowTypeConverter(jdbcToArrowTypeConverter(calendar, rsmd)) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder(new RootAllocator(0), calendar, true) + .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) + .setJdbcToArrowTypeConverter(jdbcToArrowTypeConverter(calendar, rsmd)) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build(); Schema schema = JdbcToArrowUtils.jdbcToArrowSchema(rsmd, config); JdbcToArrowTestHelper.assertFieldMetadataMatchesResultSetMetadata(rsmd, schema); } @@ -146,70 +164,104 @@ public void testJdbcSchemaMetadata() throws SQLException { * This method calls the assert methods for various DataSets. * * @param root VectorSchemaRoot for test - * @param isIncludeMapVector is this dataset checks includes map column. - * Jdbc type to 'map' mapping declared in configuration only manually + * @param isIncludeMapVector is this dataset checks includes map column. Jdbc type to 'map' + * mapping declared in configuration only manually */ public void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector) { JdbcToArrowTestHelper.assertFieldMetadataIsEmpty(root); - assertBigIntVectorValues((BigIntVector) root.getVector(BIGINT), table.getRowCount(), + assertBigIntVectorValues( + (BigIntVector) root.getVector(BIGINT), + table.getRowCount(), getLongValues(table.getValues(), BIGINT)); - assertTinyIntVectorValues((TinyIntVector) root.getVector(TINYINT), table.getRowCount(), + assertTinyIntVectorValues( + (TinyIntVector) root.getVector(TINYINT), + table.getRowCount(), getIntValues(table.getValues(), TINYINT)); - assertSmallIntVectorValues((SmallIntVector) root.getVector(SMALLINT), table.getRowCount(), + assertSmallIntVectorValues( + (SmallIntVector) root.getVector(SMALLINT), + table.getRowCount(), getIntValues(table.getValues(), SMALLINT)); - assertVarBinaryVectorValues((VarBinaryVector) root.getVector(BINARY), table.getRowCount(), + assertVarBinaryVectorValues( + (VarBinaryVector) root.getVector(BINARY), + table.getRowCount(), getBinaryValues(table.getValues(), BINARY)); - assertVarBinaryVectorValues((VarBinaryVector) root.getVector(BLOB), table.getRowCount(), + assertVarBinaryVectorValues( + (VarBinaryVector) root.getVector(BLOB), + table.getRowCount(), getBinaryValues(table.getValues(), BLOB)); - assertVarcharVectorValues((VarCharVector) root.getVector(CLOB), table.getRowCount(), + assertVarcharVectorValues( + (VarCharVector) root.getVector(CLOB), + table.getRowCount(), getCharArray(table.getValues(), CLOB)); - assertVarcharVectorValues((VarCharVector) root.getVector(VARCHAR), table.getRowCount(), + assertVarcharVectorValues( + (VarCharVector) root.getVector(VARCHAR), + table.getRowCount(), getCharArray(table.getValues(), VARCHAR)); - assertVarcharVectorValues((VarCharVector) root.getVector(CHAR), table.getRowCount(), + assertVarcharVectorValues( + (VarCharVector) root.getVector(CHAR), + table.getRowCount(), getCharArray(table.getValues(), CHAR)); - assertIntVectorValues((IntVector) root.getVector(INT), table.getRowCount(), - getIntValues(table.getValues(), INT)); + assertIntVectorValues( + (IntVector) root.getVector(INT), table.getRowCount(), getIntValues(table.getValues(), INT)); - assertBitVectorValues((BitVector) root.getVector(BIT), table.getRowCount(), - getIntValues(table.getValues(), BIT)); + assertBitVectorValues( + (BitVector) root.getVector(BIT), table.getRowCount(), getIntValues(table.getValues(), BIT)); - assertBooleanVectorValues((BitVector) root.getVector(BOOL), table.getRowCount(), + assertBooleanVectorValues( + (BitVector) root.getVector(BOOL), + table.getRowCount(), getBooleanValues(table.getValues(), BOOL)); - assertDateVectorValues((DateDayVector) root.getVector(DATE), table.getRowCount(), + assertDateVectorValues( + (DateDayVector) root.getVector(DATE), + table.getRowCount(), getIntValues(table.getValues(), DATE)); - assertTimeVectorValues((TimeMilliVector) root.getVector(TIME), table.getRowCount(), + assertTimeVectorValues( + (TimeMilliVector) root.getVector(TIME), + table.getRowCount(), getLongValues(table.getValues(), TIME)); - assertTimeStampVectorValues((TimeStampVector) root.getVector(TIMESTAMP), table.getRowCount(), + assertTimeStampVectorValues( + (TimeStampVector) root.getVector(TIMESTAMP), + table.getRowCount(), getLongValues(table.getValues(), TIMESTAMP)); - assertDecimalVectorValues((DecimalVector) root.getVector(DECIMAL), table.getRowCount(), + assertDecimalVectorValues( + (DecimalVector) root.getVector(DECIMAL), + table.getRowCount(), getDecimalValues(table.getValues(), DECIMAL)); - assertFloat8VectorValues((Float8Vector) root.getVector(DOUBLE), table.getRowCount(), + assertFloat8VectorValues( + (Float8Vector) root.getVector(DOUBLE), + table.getRowCount(), getDoubleValues(table.getValues(), DOUBLE)); - assertFloat4VectorValues((Float4Vector) root.getVector(REAL), table.getRowCount(), + assertFloat4VectorValues( + (Float4Vector) root.getVector(REAL), + table.getRowCount(), getFloatValues(table.getValues(), REAL)); assertNullVectorValues((NullVector) root.getVector(NULL), table.getRowCount()); - assertListVectorValues((ListVector) root.getVector(LIST), table.getRowCount(), + assertListVectorValues( + (ListVector) root.getVector(LIST), + table.getRowCount(), getListValues(table.getValues(), LIST)); if (isIncludeMapVector) { - assertMapVectorValues((MapVector) root.getVector(MAP), table.getRowCount(), - getMapValues(table.getValues(), MAP)); + assertMapVectorValues( + (MapVector) root.getVector(MAP), + table.getRowCount(), + getMapValues(table.getValues(), MAP)); } } @@ -219,11 +271,12 @@ public void runLargeNumberOfRows() throws IOException, SQLException { int x = 0; final int targetRows = 600000; ResultSet rs = ResultSetUtility.generateBasicResultSet(targetRows); - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder( - allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) - .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder( + allocator, JdbcToArrowUtils.getUtcCalendar(), /* include metadata */ false) + .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build(); try (ArrowVectorIterator iter = JdbcToArrow.sqlToArrowVectorIterator(rs, config)) { while (iter.hasNext()) { diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowTimeZoneTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowTimeZoneTest.java index 462a75da514..44be281712a 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowTimeZoneTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowTimeZoneTest.java @@ -28,7 +28,6 @@ import java.util.Calendar; import java.util.Collection; import java.util.TimeZone; - import org.apache.arrow.adapter.jdbc.AbstractJdbcToArrowTest; import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig; import org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder; @@ -47,10 +46,9 @@ import org.junit.runners.Parameterized.Parameters; /** - * JUnit Test Class which contains methods to test JDBC to Arrow data conversion functionality with TimeZone based Date, - * Time and Timestamp datatypes for H2 database. + * JUnit Test Class which contains methods to test JDBC to Arrow data conversion functionality with + * TimeZone based Date, Time and Timestamp datatypes for H2 database. */ - @RunWith(Parameterized.class) public class JdbcToArrowTimeZoneTest extends AbstractJdbcToArrowTest { @@ -94,39 +92,59 @@ public JdbcToArrowTimeZoneTest(Table table) { * @throws IOException on error */ @Parameters - public static Collection getTestData() throws SQLException, ClassNotFoundException, IOException { + public static Collection getTestData() + throws SQLException, ClassNotFoundException, IOException { return Arrays.asList(prepareTestData(testFiles, JdbcToArrowTimeZoneTest.class)); } /** - * Test Method to test JdbcToArrow Functionality for various H2 DB based datatypes with TimeZone based Date, - * Time and Timestamp datatype. + * Test Method to test JdbcToArrow Functionality for various H2 DB based datatypes with TimeZone + * based Date, Time and Timestamp datatype. */ @Test public void testJdbcToArrowValues() throws SQLException, IOException { - testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE), - Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))), false); - testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), - new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))), false); - testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), - Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))), false); - testDataSets(sqlToArrow( - conn.createStatement().executeQuery(table.getQuery()), - new JdbcToArrowConfigBuilder( + testDataSets( + sqlToArrow( + conn, + table.getQuery(), new RootAllocator(Integer.MAX_VALUE), - Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))).build()), false); - testDataSets(sqlToArrow( - conn, - table.getQuery(), - new JdbcToArrowConfigBuilder( + Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))), + false); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), new RootAllocator(Integer.MAX_VALUE), - Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))).build()), false); + Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))), + false); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), + Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))), + false); + testDataSets( + sqlToArrow( + conn.createStatement().executeQuery(table.getQuery()), + new JdbcToArrowConfigBuilder( + new RootAllocator(Integer.MAX_VALUE), + Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))) + .build()), + false); + testDataSets( + sqlToArrow( + conn, + table.getQuery(), + new JdbcToArrowConfigBuilder( + new RootAllocator(Integer.MAX_VALUE), + Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))) + .build()), + false); } @Test public void testJdbcSchemaMetadata() throws SQLException { Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone())); - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(0), calendar, true).build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder(new RootAllocator(0), calendar, true).build(); ResultSetMetaData rsmd = conn.createStatement().executeQuery(table.getQuery()).getMetaData(); Schema schema = JdbcToArrowUtils.jdbcToArrowSchema(rsmd, config); JdbcToArrowTestHelper.assertFieldMetadataMatchesResultSetMetadata(rsmd, schema); @@ -136,8 +154,8 @@ public void testJdbcSchemaMetadata() throws SQLException { * This method calls the assert methods for various DataSets. * * @param root VectorSchemaRoot for test - * @param isIncludeMapVector is this dataset checks includes map column. - * Jdbc type to 'map' mapping declared in configuration only manually + * @param isIncludeMapVector is this dataset checks includes map column. Jdbc type to 'map' + * mapping declared in configuration only manually */ public void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector) { JdbcToArrowTestHelper.assertFieldMetadataIsEmpty(root); @@ -146,19 +164,25 @@ public void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector) { case EST_DATE: case GMT_DATE: case PST_DATE: - assertDateVectorValues((DateDayVector) root.getVector(table.getVector()), table.getValues().length, + assertDateVectorValues( + (DateDayVector) root.getVector(table.getVector()), + table.getValues().length, table.getIntValues()); break; case EST_TIME: case GMT_TIME: case PST_TIME: - assertTimeVectorValues((TimeMilliVector) root.getVector(table.getVector()), table.getValues().length, + assertTimeVectorValues( + (TimeMilliVector) root.getVector(table.getVector()), + table.getValues().length, table.getLongValues()); break; case EST_TIMESTAMP: case GMT_TIMESTAMP: case PST_TIMESTAMP: - assertTimeStampVectorValues((TimeStampVector) root.getVector(table.getVector()), table.getValues().length, + assertTimeStampVectorValues( + (TimeStampVector) root.getVector(table.getVector()), + table.getValues().length, table.getLongValues()); break; default: @@ -166,5 +190,4 @@ public void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector) { break; } } - } diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowVectorIteratorTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowVectorIteratorTest.java index 1d7e2760f84..3a3c781646a 100644 --- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowVectorIteratorTest.java +++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/h2/JdbcToArrowVectorIteratorTest.java @@ -42,7 +42,6 @@ import java.util.Arrays; import java.util.Calendar; import java.util.List; - import org.apache.arrow.adapter.jdbc.ArrowVectorIterator; import org.apache.arrow.adapter.jdbc.JdbcToArrow; import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig; @@ -91,14 +90,15 @@ public JdbcToArrowVectorIteratorTest(Table table, boolean reuseVectorSchemaRoot) @Test @Override public void testJdbcToArrowValues() throws SQLException, IOException { - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), - Calendar.getInstance()) - .setTargetBatchSize(3) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) + .setTargetBatchSize(3) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build(); ArrowVectorIterator iterator = - JdbcToArrow.sqlToArrowVectorIterator(conn.createStatement().executeQuery(table.getQuery()), config); + JdbcToArrow.sqlToArrowVectorIterator( + conn.createStatement().executeQuery(table.getQuery()), config); validate(iterator); } @@ -106,27 +106,28 @@ public void testJdbcToArrowValues() throws SQLException, IOException { @Test public void testVectorSchemaRootReuse() throws SQLException, IOException { Integer[][] intValues = { - {101, 102, 103}, - {104, null, null}, - {107, 108, 109}, - {110} + {101, 102, 103}, + {104, null, null}, + {107, 108, 109}, + {110} }; Integer[][][] listValues = { - {{1, 2, 3}, {1, 2}, {1}}, - {{2, 3, 4}, {2, 3}, {2}}, - {{3, 4, 5}, {3, 4}, {3}}, - {{}} + {{1, 2, 3}, {1, 2}, {1}}, + {{2, 3, 4}, {2, 3}, {2}}, + {{3, 4, 5}, {3, 4}, {3}}, + {{}} }; - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), - Calendar.getInstance()) - .setTargetBatchSize(3) - .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) + .setTargetBatchSize(3) + .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build(); ArrowVectorIterator iterator = - JdbcToArrow.sqlToArrowVectorIterator(conn.createStatement().executeQuery(table.getQuery()), config); + JdbcToArrow.sqlToArrowVectorIterator( + conn.createStatement().executeQuery(table.getQuery()), config); int batchCount = 0; VectorSchemaRoot prev = null; @@ -178,14 +179,15 @@ public void testVectorSchemaRootReuse() throws SQLException, IOException { @Test public void testJdbcToArrowValuesNoLimit() throws SQLException, IOException { - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), - Calendar.getInstance()) - .setTargetBatchSize(JdbcToArrowConfig.NO_LIMIT_BATCH_SIZE) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) + .setTargetBatchSize(JdbcToArrowConfig.NO_LIMIT_BATCH_SIZE) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build(); ArrowVectorIterator iterator = - JdbcToArrow.sqlToArrowVectorIterator(conn.createStatement().executeQuery(table.getQuery()), config); + JdbcToArrow.sqlToArrowVectorIterator( + conn.createStatement().executeQuery(table.getQuery()), config); validate(iterator); } @@ -195,12 +197,12 @@ public void testTimeStampConsumer() throws SQLException, IOException { final String sql = "select timestamp_field11 from table1"; // first experiment, with calendar and time zone. - JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), - Calendar.getInstance()) - .setTargetBatchSize(3) - .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build(); + JdbcToArrowConfig config = + new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) + .setTargetBatchSize(3) + .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build(); assertNotNull(config.getCalendar()); try (ArrowVectorIterator iterator = @@ -213,16 +215,16 @@ public void testTimeStampConsumer() throws SQLException, IOException { } // second experiment, without calendar and time zone. - config = new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), - null) - .setTargetBatchSize(3) - .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) - .build(); + config = + new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), null) + .setTargetBatchSize(3) + .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP) + .build(); assertNull(config.getCalendar()); try (ArrowVectorIterator iterator = - JdbcToArrow.sqlToArrowVectorIterator(conn.createStatement().executeQuery(sql), config)) { + JdbcToArrow.sqlToArrowVectorIterator(conn.createStatement().executeQuery(sql), config)) { VectorSchemaRoot root = iterator.next(); assertEquals(1, root.getFieldVectors().size()); @@ -278,24 +280,40 @@ private void validate(ArrowVectorIterator iterator) throws SQLException, IOExcep float8Vectors.add((Float8Vector) root.getVector(DOUBLE)); listVectors.add((ListVector) root.getVector(LIST)); } - assertBigIntVectorValues(bigIntVectors, table.getRowCount(), getLongValues(table.getValues(), BIGINT)); - assertTinyIntVectorValues(tinyIntVectors, table.getRowCount(), getIntValues(table.getValues(), TINYINT)); + assertBigIntVectorValues( + bigIntVectors, table.getRowCount(), getLongValues(table.getValues(), BIGINT)); + assertTinyIntVectorValues( + tinyIntVectors, table.getRowCount(), getIntValues(table.getValues(), TINYINT)); assertIntVectorValues(intVectors, table.getRowCount(), getIntValues(table.getValues(), INT)); - assertSmallIntVectorValues(smallIntVectors, table.getRowCount(), getIntValues(table.getValues(), SMALLINT)); - assertBinaryVectorValues(vectorsForBinary, table.getRowCount(), getBinaryValues(table.getValues(), BINARY)); - assertBinaryVectorValues(vectorsForBlob, table.getRowCount(), getBinaryValues(table.getValues(), BLOB)); - assertVarCharVectorValues(vectorsForClob, table.getRowCount(), getCharArray(table.getValues(), CLOB)); - assertVarCharVectorValues(vectorsForVarChar, table.getRowCount(), getCharArray(table.getValues(), VARCHAR)); - assertVarCharVectorValues(vectorsForChar, table.getRowCount(), getCharArray(table.getValues(), CHAR)); + assertSmallIntVectorValues( + smallIntVectors, table.getRowCount(), getIntValues(table.getValues(), SMALLINT)); + assertBinaryVectorValues( + vectorsForBinary, table.getRowCount(), getBinaryValues(table.getValues(), BINARY)); + assertBinaryVectorValues( + vectorsForBlob, table.getRowCount(), getBinaryValues(table.getValues(), BLOB)); + assertVarCharVectorValues( + vectorsForClob, table.getRowCount(), getCharArray(table.getValues(), CLOB)); + assertVarCharVectorValues( + vectorsForVarChar, table.getRowCount(), getCharArray(table.getValues(), VARCHAR)); + assertVarCharVectorValues( + vectorsForChar, table.getRowCount(), getCharArray(table.getValues(), CHAR)); assertBitVectorValues(vectorsForBit, table.getRowCount(), getIntValues(table.getValues(), BIT)); - assertBooleanVectorValues(vectorsForBool, table.getRowCount(), getBooleanValues(table.getValues(), BOOL)); - assertDateDayVectorValues(dateDayVectors, table.getRowCount(), getLongValues(table.getValues(), DATE)); - assertTimeMilliVectorValues(timeMilliVectors, table.getRowCount(), getLongValues(table.getValues(), TIME)); - assertTimeStampVectorValues(timeStampVectors, table.getRowCount(), getLongValues(table.getValues(), TIMESTAMP)); - assertDecimalVectorValues(decimalVectors, table.getRowCount(), getDecimalValues(table.getValues(), DECIMAL)); - assertFloat4VectorValues(float4Vectors, table.getRowCount(), getFloatValues(table.getValues(), REAL)); - assertFloat8VectorValues(float8Vectors, table.getRowCount(), getDoubleValues(table.getValues(), DOUBLE)); - assertListVectorValues(listVectors, table.getRowCount(), getListValues(table.getValues(), LIST)); + assertBooleanVectorValues( + vectorsForBool, table.getRowCount(), getBooleanValues(table.getValues(), BOOL)); + assertDateDayVectorValues( + dateDayVectors, table.getRowCount(), getLongValues(table.getValues(), DATE)); + assertTimeMilliVectorValues( + timeMilliVectors, table.getRowCount(), getLongValues(table.getValues(), TIME)); + assertTimeStampVectorValues( + timeStampVectors, table.getRowCount(), getLongValues(table.getValues(), TIMESTAMP)); + assertDecimalVectorValues( + decimalVectors, table.getRowCount(), getDecimalValues(table.getValues(), DECIMAL)); + assertFloat4VectorValues( + float4Vectors, table.getRowCount(), getFloatValues(table.getValues(), REAL)); + assertFloat8VectorValues( + float8Vectors, table.getRowCount(), getDoubleValues(table.getValues(), DOUBLE)); + assertListVectorValues( + listVectors, table.getRowCount(), getListValues(table.getValues(), LIST)); roots.forEach(root -> root.close()); } @@ -324,7 +342,8 @@ private void assertFloat4VectorValues(List vectors, int rowCount, } } - private void assertDecimalVectorValues(List vectors, int rowCount, BigDecimal[] values) { + private void assertDecimalVectorValues( + List vectors, int rowCount, BigDecimal[] values) { int valueCount = vectors.stream().mapToInt(ValueVector::getValueCount).sum(); assertEquals(rowCount, valueCount); @@ -337,7 +356,8 @@ private void assertDecimalVectorValues(List vectors, int rowCount } } - private void assertTimeStampVectorValues(List vectors, int rowCount, Long[] values) { + private void assertTimeStampVectorValues( + List vectors, int rowCount, Long[] values) { int valueCount = vectors.stream().mapToInt(ValueVector::getValueCount).sum(); assertEquals(rowCount, valueCount); @@ -349,7 +369,8 @@ private void assertTimeStampVectorValues(List vectors, int rowC } } - private void assertTimeMilliVectorValues(List vectors, int rowCount, Long[] values) { + private void assertTimeMilliVectorValues( + List vectors, int rowCount, Long[] values) { int valueCount = vectors.stream().mapToInt(ValueVector::getValueCount).sum(); assertEquals(rowCount, valueCount); @@ -397,7 +418,8 @@ private void assertBooleanVectorValues(List vectors, int rowCount, Bo } } - private void assertVarCharVectorValues(List vectors, int rowCount, byte[][] values) { + private void assertVarCharVectorValues( + List vectors, int rowCount, byte[][] values) { int valueCount = vectors.stream().mapToInt(ValueVector::getValueCount).sum(); assertEquals(rowCount, valueCount); @@ -409,7 +431,8 @@ private void assertVarCharVectorValues(List vectors, int rowCount } } - private void assertBinaryVectorValues(List vectors, int rowCount, byte[][] values) { + private void assertBinaryVectorValues( + List vectors, int rowCount, byte[][] values) { int valueCount = vectors.stream().mapToInt(ValueVector::getValueCount).sum(); assertEquals(rowCount, valueCount); @@ -421,7 +444,8 @@ private void assertBinaryVectorValues(List vectors, int rowCoun } } - private void assertSmallIntVectorValues(List vectors, int rowCount, Integer[] values) { + private void assertSmallIntVectorValues( + List vectors, int rowCount, Integer[] values) { int valueCount = vectors.stream().mapToInt(ValueVector::getValueCount).sum(); assertEquals(rowCount, valueCount); @@ -433,7 +457,8 @@ private void assertSmallIntVectorValues(List vectors, int rowCou } } - private void assertTinyIntVectorValues(List vectors, int rowCount, Integer[] values) { + private void assertTinyIntVectorValues( + List vectors, int rowCount, Integer[] values) { int valueCount = vectors.stream().mapToInt(ValueVector::getValueCount).sum(); assertEquals(rowCount, valueCount); @@ -474,7 +499,8 @@ private void assertIntVectorValues(List vectors, int rowCount, Intege } } - public static void assertListVectorValues(List vectors, int rowCount, Integer[][] values) { + public static void assertListVectorValues( + List vectors, int rowCount, Integer[][] values) { int valueCount = vectors.stream().mapToInt(ValueVector::getValueCount).sum(); assertEquals(rowCount, valueCount); @@ -492,12 +518,11 @@ public static void assertListVectorValues(List vectors, int rowCount } } - /** - * Runs a simple query, and encapsulates the result into a field vector. - */ + /** Runs a simple query, and encapsulates the result into a field vector. */ private FieldVector getQueryResult(JdbcToArrowConfig config) throws SQLException, IOException { - ArrowVectorIterator iterator = JdbcToArrow.sqlToArrowVectorIterator( - conn.createStatement().executeQuery("select real_field8 from table1"), config); + ArrowVectorIterator iterator = + JdbcToArrow.sqlToArrowVectorIterator( + conn.createStatement().executeQuery("select real_field8 from table1"), config); VectorSchemaRoot root = iterator.next(); @@ -513,10 +538,11 @@ private FieldVector getQueryResult(JdbcToArrowConfig config) throws SQLException @Test public void testJdbcToArrowCustomTypeConversion() throws SQLException, IOException { - JdbcToArrowConfigBuilder builder = new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), - Calendar.getInstance()).setTargetBatchSize(JdbcToArrowConfig.NO_LIMIT_BATCH_SIZE) - .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) - .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP); + JdbcToArrowConfigBuilder builder = + new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()) + .setTargetBatchSize(JdbcToArrowConfig.NO_LIMIT_BATCH_SIZE) + .setReuseVectorSchemaRoot(reuseVectorSchemaRoot) + .setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP); // first experiment, using default type converter JdbcToArrowConfig config = builder.build(); @@ -527,15 +553,16 @@ public void testJdbcToArrowCustomTypeConversion() throws SQLException, IOExcepti } // second experiment, using customized type converter - builder.setJdbcToArrowTypeConverter((fieldInfo) -> { - switch (fieldInfo.getJdbcType()) { - case Types.REAL: - // this is different from the default type converter - return new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE); - default: - return null; - } - }); + builder.setJdbcToArrowTypeConverter( + (fieldInfo) -> { + switch (fieldInfo.getJdbcType()) { + case Types.REAL: + // this is different from the default type converter + return new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE); + default: + return null; + } + }); config = builder.build(); try (FieldVector vector = getQueryResult(config)) { diff --git a/java/adapter/jdbc/src/test/resources/logback.xml b/java/adapter/jdbc/src/test/resources/logback.xml index 4c54d18a210..9fa3f1e3b43 100644 --- a/java/adapter/jdbc/src/test/resources/logback.xml +++ b/java/adapter/jdbc/src/test/resources/logback.xml @@ -1,4 +1,4 @@ - + - diff --git a/java/adapter/orc/pom.xml b/java/adapter/orc/pom.xml index 79e51470a42..8fe5529db64 100644 --- a/java/adapter/orc/pom.xml +++ b/java/adapter/orc/pom.xml @@ -1,141 +1,138 @@ + license agreements. See the NOTICE file distributed with this work for additional + information regarding copyright ownership. The ASF licenses this file to + You under the Apache License, Version 2.0 (the "License"); you may not use + this file except in compliance with the License. You may obtain a copy of + the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required + by applicable law or agreed to in writing, software distributed under the + License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS + OF ANY KIND, either express or implied. See the License for the specific + language governing permissions and limitations under the License. --> + + 4.0.0 - - 4.0.0 - - - org.apache.arrow - arrow-memory-core - compile - - - org.apache.arrow - arrow-memory-netty - runtime - - - org.apache.arrow - arrow-vector - compile - ${arrow.vector.classifier} - - - org.immutables - value - - - org.apache.orc - orc-core - 1.9.2 - test - - - log4j - log4j - - - org.slf4j - slf4j-log4j12 - - - commons-logging - commons-logging - - - - - org.apache.hadoop - hadoop-client-runtime - 3.3.6 - test - - - commons-logging - commons-logging - - - - - org.apache.hadoop - hadoop-client-api - 3.3.2 - - - org.apache.hadoop - hadoop-common - 3.3.6 - test - - - commons-logging - commons-logging - - - log4j - log4j - - - org.slf4j - slf4j-log4j12 - - - javax.servlet - servlet-api - - - org.slf4j - slf4j-reload4j - - - ch.qos.reload4j - reload4j - - - - - org.apache.hive - hive-storage-api - 2.8.1 - test - - + + org.apache.arrow + arrow-java-root + 16.0.0-SNAPSHOT + ../../pom.xml + - - org.apache.arrow - arrow-java-root - 16.0.0-SNAPSHOT - ../../pom.xml - + org.apache.arrow.orc + arrow-orc + jar + Arrow Orc Adapter + (Experimental/Contrib)A JNI wrapper for the C++ ORC reader implementation. + + ../../../cpp/release-build/ + + + + org.apache.arrow + arrow-memory-core + compile + + + org.apache.arrow + arrow-memory-netty + runtime + + + org.apache.arrow + arrow-vector + ${arrow.vector.classifier} + compile + + + org.immutables + value + + + org.apache.orc + orc-core + 1.9.2 + test + + + log4j + log4j + + + org.slf4j + slf4j-log4j12 + + + commons-logging + commons-logging + + + + + org.apache.hadoop + hadoop-client-runtime + 3.3.6 + test + + + commons-logging + commons-logging + + + + + org.apache.hadoop + hadoop-client-api + 3.3.2 + + + org.apache.hadoop + hadoop-common + 3.3.6 + test + + + commons-logging + commons-logging + + + log4j + log4j + + + org.slf4j + slf4j-log4j12 + + + javax.servlet + servlet-api + + + org.slf4j + slf4j-reload4j + + + ch.qos.reload4j + reload4j + + + + + org.apache.hive + hive-storage-api + 2.8.1 + test + + - org.apache.arrow.orc - arrow-orc - Arrow Orc Adapter - (Experimental/Contrib)A JNI wrapper for the C++ ORC reader implementation. - jar - - ../../../cpp/release-build/ - - - - - - ${arrow.cpp.build.dir} - - **/*arrow_orc_jni.* - - - - + + + + ${arrow.cpp.build.dir} + + **/*arrow_orc_jni.* + + + + diff --git a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcFieldNode.java b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcFieldNode.java index 716a1387660..9290521c10b 100644 --- a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcFieldNode.java +++ b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcFieldNode.java @@ -17,9 +17,7 @@ package org.apache.arrow.adapter.orc; -/** - * Metadata about Vectors/Arrays that is passed via JNI interface. - */ +/** Metadata about Vectors/Arrays that is passed via JNI interface. */ class OrcFieldNode { private final int length; @@ -27,6 +25,7 @@ class OrcFieldNode { /** * Construct a new instance. + * * @param length the number of values written. * @param nullCount the number of null values. */ diff --git a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcJniUtils.java b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcJniUtils.java index 9b599234bdf..21dcfb71c7f 100644 --- a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcJniUtils.java +++ b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcJniUtils.java @@ -25,23 +25,21 @@ import java.nio.file.StandardCopyOption; import java.util.Locale; -/** - * Helper class for JNI related operations. - */ +/** Helper class for JNI related operations. */ class OrcJniUtils { private static final String LIBRARY_NAME = "arrow_orc_jni"; private static boolean isLoaded = false; private OrcJniUtils() {} - static void loadOrcAdapterLibraryFromJar() - throws IOException, IllegalAccessException { + static void loadOrcAdapterLibraryFromJar() throws IOException, IllegalAccessException { synchronized (OrcJniUtils.class) { if (!isLoaded) { final String libraryToLoad = LIBRARY_NAME + "/" + getNormalizedArch() + "/" + System.mapLibraryName(LIBRARY_NAME); final File libraryFile = - moveFileFromJarToTemp(System.getProperty("java.io.tmpdir"), libraryToLoad, LIBRARY_NAME); + moveFileFromJarToTemp( + System.getProperty("java.io.tmpdir"), libraryToLoad, LIBRARY_NAME); System.load(libraryFile.getAbsolutePath()); isLoaded = true; } @@ -63,11 +61,11 @@ private static String getNormalizedArch() { return arch; } - private static File moveFileFromJarToTemp(final String tmpDir, String libraryToLoad, String libraryName) - throws IOException { + private static File moveFileFromJarToTemp( + final String tmpDir, String libraryToLoad, String libraryName) throws IOException { final File temp = File.createTempFile(tmpDir, libraryName); - try (final InputStream is = OrcReaderJniWrapper.class.getClassLoader() - .getResourceAsStream(libraryToLoad)) { + try (final InputStream is = + OrcReaderJniWrapper.class.getClassLoader().getResourceAsStream(libraryToLoad)) { if (is == null) { throw new FileNotFoundException(libraryToLoad); } else { diff --git a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcMemoryJniWrapper.java b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcMemoryJniWrapper.java index 473e8314243..167481b05a5 100644 --- a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcMemoryJniWrapper.java +++ b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcMemoryJniWrapper.java @@ -17,9 +17,7 @@ package org.apache.arrow.adapter.orc; -/** - * Wrapper for orc memory allocated by native code. - */ +/** Wrapper for orc memory allocated by native code. */ class OrcMemoryJniWrapper implements AutoCloseable { private final long nativeInstanceId; @@ -32,6 +30,7 @@ class OrcMemoryJniWrapper implements AutoCloseable { /** * Construct a new instance. + * * @param nativeInstanceId unique id of the underlying memory. * @param memoryAddress starting memory address of the underlying memory. * @param size size of the valid data. @@ -46,6 +45,7 @@ class OrcMemoryJniWrapper implements AutoCloseable { /** * Return the size of underlying chunk of memory that has valid data. + * * @return valid data size */ long getSize() { @@ -54,6 +54,7 @@ long getSize() { /** * Return the size of underlying chunk of memory managed by this OrcMemoryJniWrapper. + * * @return underlying memory size */ long getCapacity() { @@ -62,6 +63,7 @@ long getCapacity() { /** * Return the memory address of underlying chunk of memory. + * * @return memory address */ long getMemoryAddress() { diff --git a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcReader.java b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcReader.java index 648e17e9c37..1c9577ca726 100644 --- a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcReader.java +++ b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcReader.java @@ -18,40 +18,39 @@ package org.apache.arrow.adapter.orc; import java.io.IOException; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.ipc.ArrowReader; /** - * Orc Reader that allow accessing orc stripes in Orc file. - * This orc reader basically acts like an ArrowReader iterator that - * iterate over orc stripes. Each stripe will be accessed via an - * ArrowReader. + * Orc Reader that allow accessing orc stripes in Orc file. This orc reader basically acts like an + * ArrowReader iterator that iterate over orc stripes. Each stripe will be accessed via an + * ArrowReader. */ public class OrcReader implements AutoCloseable { private final OrcReaderJniWrapper jniWrapper; private BufferAllocator allocator; - /** - * reference to native reader instance. - */ + /** reference to native reader instance. */ private final long nativeInstanceId; /** * Create an OrcReader that iterate over orc stripes. + * * @param filePath file path to target file, currently only support local file. * @param allocator allocator provided to ArrowReader. * @throws IOException throws exception in case of file not found */ - public OrcReader(String filePath, BufferAllocator allocator) throws IOException, IllegalAccessException { + public OrcReader(String filePath, BufferAllocator allocator) + throws IOException, IllegalAccessException { this.allocator = allocator; this.jniWrapper = OrcReaderJniWrapper.getInstance(); this.nativeInstanceId = jniWrapper.open(filePath); } /** - * Seek to designated row. Invoke NextStripeReader() after seek - * will return stripe reader starting from designated row. + * Seek to designated row. Invoke NextStripeReader() after seek will return stripe reader starting + * from designated row. + * * @param rowNumber the rows number to seek * @return true if seek operation is succeeded */ diff --git a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcReaderJniWrapper.java b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcReaderJniWrapper.java index ff449c343c4..e66e3270677 100644 --- a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcReaderJniWrapper.java +++ b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcReaderJniWrapper.java @@ -19,9 +19,7 @@ import java.io.IOException; -/** - * JNI wrapper for Orc reader. - */ +/** JNI wrapper for Orc reader. */ class OrcReaderJniWrapper { private static volatile OrcReaderJniWrapper INSTANCE; @@ -41,21 +39,24 @@ static OrcReaderJniWrapper getInstance() throws IOException, IllegalAccessExcept /** * Construct a orc file reader over the target file. + * * @param fileName absolute file path of target file - * @return id of the orc reader instance if file opened successfully, - * otherwise return error code * -1. + * @return id of the orc reader instance if file opened successfully, otherwise return error code + * * -1. */ native long open(String fileName); /** * Release resources associated with designated reader instance. + * * @param readerId id of the reader instance. */ native void close(long readerId); /** - * Seek to designated row. Invoke nextStripeReader() after seek - * will return id of stripe reader starting from designated row. + * Seek to designated row. Invoke nextStripeReader() after seek will return id of stripe reader + * starting from designated row. + * * @param readerId id of the reader instance * @param rowNumber the rows number to seek * @return true if seek operation is succeeded @@ -64,6 +65,7 @@ static OrcReaderJniWrapper getInstance() throws IOException, IllegalAccessExcept /** * The number of stripes in the file. + * * @param readerId id of the reader instance * @return number of stripes */ @@ -71,6 +73,7 @@ static OrcReaderJniWrapper getInstance() throws IOException, IllegalAccessExcept /** * Get a stripe level ArrowReader with specified batchSize in each record batch. + * * @param readerId id of the reader instance * @param batchSize the number of rows loaded on each iteration * @return id of the stripe reader instance. diff --git a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcRecordBatch.java b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcRecordBatch.java index a006cacab98..67b62996093 100644 --- a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcRecordBatch.java +++ b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcRecordBatch.java @@ -20,21 +20,18 @@ import java.util.Arrays; import java.util.List; -/** - * Wrapper for record batch meta and native memory. - */ +/** Wrapper for record batch meta and native memory. */ class OrcRecordBatch { final int length; - /** - * Nodes correspond to the pre-ordered flattened logical schema. - */ + /** Nodes correspond to the pre-ordered flattened logical schema. */ final List nodes; final List buffers; /** * Construct a new instance. + * * @param length number of records included in current batch * @param nodes meta data for each fields * @param buffers buffers for underlying data diff --git a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcReferenceManager.java b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcReferenceManager.java index fdec337e85d..961608fc60b 100644 --- a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcReferenceManager.java +++ b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcReferenceManager.java @@ -18,7 +18,6 @@ package org.apache.arrow.adapter.orc; import java.util.concurrent.atomic.AtomicInteger; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.OwnershipTransferResult; @@ -26,8 +25,8 @@ import org.apache.arrow.util.Preconditions; /** - * A simple reference manager implementation for memory allocated by native code. - * The underlying memory will be released when reference count reach zero. + * A simple reference manager implementation for memory allocated by native code. The underlying + * memory will be released when reference count reach zero. */ public class OrcReferenceManager implements ReferenceManager { private final AtomicInteger bufRefCnt = new AtomicInteger(0); @@ -50,8 +49,8 @@ public boolean release() { @Override public boolean release(int decrement) { - Preconditions.checkState(decrement >= 1, - "ref count decrement should be greater than or equal to 1"); + Preconditions.checkState( + decrement >= 1, "ref count decrement should be greater than or equal to 1"); // decrement the ref count final int refCnt; synchronized (this) { @@ -89,18 +88,21 @@ public ArrowBuf deriveBuffer(ArrowBuf sourceBuffer, long index, long length) { final long derivedBufferAddress = sourceBuffer.memoryAddress() + index; // create new ArrowBuf - final ArrowBuf derivedBuf = new ArrowBuf( + final ArrowBuf derivedBuf = + new ArrowBuf( this, null, length, // length (in bytes) in the underlying memory chunk for this new ArrowBuf - derivedBufferAddress // starting byte address in the underlying memory for this new ArrowBuf, + derivedBufferAddress // starting byte address in the underlying memory for this new + // ArrowBuf, ); return derivedBuf; } @Override - public OwnershipTransferResult transferOwnership(ArrowBuf sourceBuffer, BufferAllocator targetAllocator) { + public OwnershipTransferResult transferOwnership( + ArrowBuf sourceBuffer, BufferAllocator targetAllocator) { throw new UnsupportedOperationException(); } diff --git a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcStripeReader.java b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcStripeReader.java index 484296d92e0..3b60d14ee6f 100644 --- a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcStripeReader.java +++ b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcStripeReader.java @@ -20,7 +20,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.stream.Collectors; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.ipc.ArrowReader; @@ -33,19 +32,16 @@ import org.apache.arrow.vector.types.pojo.Schema; import org.apache.arrow.vector.util.ByteArrayReadableSeekableByteChannel; -/** - * Orc stripe that load data into ArrowRecordBatch. - */ +/** Orc stripe that load data into ArrowRecordBatch. */ public class OrcStripeReader extends ArrowReader { - /** - * reference to native stripe reader instance. - */ + /** reference to native stripe reader instance. */ private final long nativeInstanceId; /** * Construct a new instance. - * @param nativeInstanceId nativeInstanceId of the stripe reader instance, obtained by - * calling nextStripeReader from OrcReaderJniWrapper + * + * @param nativeInstanceId nativeInstanceId of the stripe reader instance, obtained by calling + * nextStripeReader from OrcReaderJniWrapper * @param allocator memory allocator for accounting. */ OrcStripeReader(long nativeInstanceId, BufferAllocator allocator) { @@ -62,18 +58,20 @@ public boolean loadNextBatch() throws IOException { ArrayList buffers = new ArrayList<>(); for (OrcMemoryJniWrapper buffer : recordBatch.buffers) { - buffers.add(new ArrowBuf( + buffers.add( + new ArrowBuf( new OrcReferenceManager(buffer), null, (int) buffer.getSize(), buffer.getMemoryAddress())); } - loadRecordBatch(new ArrowRecordBatch( + loadRecordBatch( + new ArrowRecordBatch( recordBatch.length, recordBatch.nodes.stream() - .map(buf -> new ArrowFieldNode(buf.getLength(), buf.getNullCount())) - .collect(Collectors.toList()), + .map(buf -> new ArrowFieldNode(buf.getLength(), buf.getNullCount())) + .collect(Collectors.toList()), buffers)); return true; } @@ -83,7 +81,6 @@ public long bytesRead() { return 0; } - @Override protected void closeReadSource() throws IOException { OrcStripeReaderJniWrapper.close(nativeInstanceId); @@ -94,9 +91,8 @@ protected Schema readSchema() throws IOException { byte[] schemaBytes = OrcStripeReaderJniWrapper.getSchema(nativeInstanceId); try (MessageChannelReader schemaReader = - new MessageChannelReader( - new ReadChannel( - new ByteArrayReadableSeekableByteChannel(schemaBytes)), allocator)) { + new MessageChannelReader( + new ReadChannel(new ByteArrayReadableSeekableByteChannel(schemaBytes)), allocator)) { MessageResult result = schemaReader.readNext(); if (result == null) { diff --git a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcStripeReaderJniWrapper.java b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcStripeReaderJniWrapper.java index 1dd96986108..2d68dc45e9a 100644 --- a/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcStripeReaderJniWrapper.java +++ b/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcStripeReaderJniWrapper.java @@ -17,13 +17,12 @@ package org.apache.arrow.adapter.orc; -/** - * JNI wrapper for orc stripe reader. - */ +/** JNI wrapper for orc stripe reader. */ class OrcStripeReaderJniWrapper { /** * Get the schema of current stripe. + * * @param readerId id of the stripe reader instance. * @return serialized schema. */ @@ -31,14 +30,15 @@ class OrcStripeReaderJniWrapper { /** * Load next record batch. + * * @param readerId id of the stripe reader instance. - * @return loaded record batch, return null when reached - * the end of current stripe. + * @return loaded record batch, return null when reached the end of current stripe. */ static native OrcRecordBatch next(long readerId); /** * Release resources of underlying reader. + * * @param readerId id of the stripe reader instance. */ static native void close(long readerId); diff --git a/java/adapter/orc/src/test/java/org/apache/arrow/adapter/orc/OrcReaderTest.java b/java/adapter/orc/src/test/java/org/apache/arrow/adapter/orc/OrcReaderTest.java index 4153a35a61c..6405c00b286 100644 --- a/java/adapter/orc/src/test/java/org/apache/arrow/adapter/orc/OrcReaderTest.java +++ b/java/adapter/orc/src/test/java/org/apache/arrow/adapter/orc/OrcReaderTest.java @@ -24,8 +24,6 @@ import java.io.File; import java.nio.charset.StandardCharsets; import java.util.List; - - import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.IntVector; @@ -45,11 +43,9 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; - public class OrcReaderTest { - @Rule - public TemporaryFolder testFolder = new TemporaryFolder(); + @Rule public TemporaryFolder testFolder = new TemporaryFolder(); private static final int MAX_ALLOCATION = 8 * 1024; private static RootAllocator allocator; @@ -64,8 +60,10 @@ public void testOrcJniReader() throws Exception { TypeDescription schema = TypeDescription.fromString("struct"); File testFile = new File(testFolder.getRoot(), "test-orc"); - Writer writer = OrcFile.createWriter(new Path(testFile.getAbsolutePath()), - OrcFile.writerOptions(new Configuration()).setSchema(schema)); + Writer writer = + OrcFile.createWriter( + new Path(testFile.getAbsolutePath()), + OrcFile.writerOptions(new Configuration()).setSchema(schema)); VectorizedRowBatch batch = schema.createRowBatch(); LongColumnVector longColumnVector = (LongColumnVector) batch.cols[0]; BytesColumnVector bytesColumnVector = (BytesColumnVector) batch.cols[1]; diff --git a/java/algorithm/pom.xml b/java/algorithm/pom.xml index 25669010d2d..537dac20aca 100644 --- a/java/algorithm/pom.xml +++ b/java/algorithm/pom.xml @@ -1,4 +1,4 @@ - + - diff --git a/java/bom/pom.xml b/java/bom/pom.xml index 025632c45a5..c29be661291 100644 --- a/java/bom/pom.xml +++ b/java/bom/pom.xml @@ -27,6 +27,8 @@ + 2.42.0 + 2.30.0 @@ -170,4 +172,61 @@ + + + + spotless-jdk11+ + + [11,] + + + + + com.diffplug.spotless + spotless-maven-plugin + ${spotless.version.jdk11} + + + + + + + + pom.xml + + + ${maven.multiModuleProjectDirectory}/spotless/asf-xml.license + (<configuration|<project) + + + + + + true + 2 + + + true + 2 + + + false + + + + + + spotless-check + + check + + validate + + + + + + + + diff --git a/java/c/pom.xml b/java/c/pom.xml index ffd41b62dd6..6711c31ca45 100644 --- a/java/c/pom.xml +++ b/java/c/pom.xml @@ -10,78 +10,78 @@ OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> - - arrow-java-root - org.apache.arrow - 16.0.0-SNAPSHOT - - 4.0.0 + 4.0.0 + + org.apache.arrow + arrow-java-root + 16.0.0-SNAPSHOT + - arrow-c-data - Arrow Java C Data Interface - Java implementation of C Data Interface - jar - - ./build - + arrow-c-data + jar + Arrow Java C Data Interface + Java implementation of C Data Interface + + ./build + - - - org.apache.arrow - arrow-vector - compile - ${arrow.vector.classifier} - - - org.apache.arrow - arrow-vector - ${project.version} - test-jar - test - - - org.apache.arrow - arrow-memory-core - compile - - - org.slf4j - slf4j-api - - - org.immutables - value - - - org.apache.arrow - arrow-memory-unsafe - test - - - org.apache.arrow - arrow-format - test - - - com.google.guava - guava - test - - - org.assertj - assertj-core - test - - - - - - ${arrow.c.jni.dist.dir} - - **/*arrow_cdata_jni.* - - - - + + + org.apache.arrow + arrow-vector + ${arrow.vector.classifier} + compile + + + org.apache.arrow + arrow-vector + ${project.version} + test-jar + test + + + org.apache.arrow + arrow-memory-core + compile + + + org.slf4j + slf4j-api + + + org.immutables + value + + + org.apache.arrow + arrow-memory-unsafe + test + + + org.apache.arrow + arrow-format + test + + + com.google.guava + guava + test + + + org.assertj + assertj-core + test + + + + + + ${arrow.c.jni.dist.dir} + + **/*arrow_cdata_jni.* + + + + diff --git a/java/c/src/main/java/module-info.java b/java/c/src/main/java/module-info.java index 0a62c9b9875..48af1bc6b72 100644 --- a/java/c/src/main/java/module-info.java +++ b/java/c/src/main/java/module-info.java @@ -1,19 +1,19 @@ /* * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ open module org.apache.arrow.c { exports org.apache.arrow.c; diff --git a/java/c/src/main/java/org/apache/arrow/c/ArrayExporter.java b/java/c/src/main/java/org/apache/arrow/c/ArrayExporter.java index d6479a3ba4c..c99a6f76c3c 100644 --- a/java/c/src/main/java/org/apache/arrow/c/ArrayExporter.java +++ b/java/c/src/main/java/org/apache/arrow/c/ArrayExporter.java @@ -23,7 +23,6 @@ import java.util.ArrayList; import java.util.List; - import org.apache.arrow.c.jni.JniWrapper; import org.apache.arrow.c.jni.PrivateData; import org.apache.arrow.memory.ArrowBuf; @@ -33,9 +32,7 @@ import org.apache.arrow.vector.dictionary.DictionaryProvider; import org.apache.arrow.vector.types.pojo.DictionaryEncoding; -/** - * Exporter for {@link ArrowArray}. - */ +/** Exporter for {@link ArrowArray}. */ final class ArrayExporter { private final BufferAllocator allocator; @@ -43,9 +40,7 @@ public ArrayExporter(BufferAllocator allocator) { this.allocator = allocator; } - /** - * Private data structure for exported arrays. - */ + /** Private data structure for exported arrays. */ static class ExportedArrayPrivateData implements PrivateData { ArrowBuf buffers_ptrs; List buffers; diff --git a/java/c/src/main/java/org/apache/arrow/c/ArrayImporter.java b/java/c/src/main/java/org/apache/arrow/c/ArrayImporter.java index 7132887ddee..cc4545ad3c6 100644 --- a/java/c/src/main/java/org/apache/arrow/c/ArrayImporter.java +++ b/java/c/src/main/java/org/apache/arrow/c/ArrayImporter.java @@ -24,7 +24,6 @@ import java.util.Collections; import java.util.List; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.util.Preconditions; @@ -34,9 +33,7 @@ import org.apache.arrow.vector.ipc.message.ArrowFieldNode; import org.apache.arrow.vector.types.pojo.DictionaryEncoding; -/** - * Importer for {@link ArrowArray}. - */ +/** Importer for {@link ArrowArray}. */ final class ArrayImporter { private static final int MAX_IMPORT_RECURSION_LEVEL = 64; @@ -47,7 +44,8 @@ final class ArrayImporter { private ReferenceCountedArrowArray underlyingAllocation; private int recursionLevel; - ArrayImporter(BufferAllocator allocator, FieldVector vector, DictionaryProvider dictionaryProvider) { + ArrayImporter( + BufferAllocator allocator, FieldVector vector, DictionaryProvider dictionaryProvider) { this.allocator = Preconditions.checkNotNull(allocator); this.vector = Preconditions.checkNotNull(vector); this.dictionaryProvider = dictionaryProvider; @@ -78,7 +76,9 @@ private void importChild(ArrayImporter parent, ArrowArray src) { ArrowArray.Snapshot snapshot = src.snapshot(); checkState(snapshot.release != NULL, "Cannot import released ArrowArray"); recursionLevel = parent.recursionLevel + 1; - checkState(recursionLevel <= MAX_IMPORT_RECURSION_LEVEL, "Recursion level in ArrowArray struct exceeded"); + checkState( + recursionLevel <= MAX_IMPORT_RECURSION_LEVEL, + "Recursion level in ArrowArray struct exceeded"); // Child buffers will keep the entire parent import alive. underlyingAllocation = parent.underlyingAllocation; doImport(snapshot); @@ -86,14 +86,19 @@ private void importChild(ArrayImporter parent, ArrowArray src) { private void doImport(ArrowArray.Snapshot snapshot) { // First import children (required for reconstituting parent array data) - long[] children = NativeUtil.toJavaArray(snapshot.children, checkedCastToInt(snapshot.n_children)); + long[] children = + NativeUtil.toJavaArray(snapshot.children, checkedCastToInt(snapshot.n_children)); if (children != null && children.length > 0) { List childVectors = vector.getChildrenFromFields(); - checkState(children.length == childVectors.size(), "ArrowArray struct has %s children (expected %s)", - children.length, childVectors.size()); + checkState( + children.length == childVectors.size(), + "ArrowArray struct has %s children (expected %s)", + children.length, + childVectors.size()); for (int i = 0; i < children.length; i++) { checkState(children[i] != NULL, "ArrowArray struct has NULL child at position %s", i); - ArrayImporter childImporter = new ArrayImporter(allocator, childVectors.get(i), dictionaryProvider); + ArrayImporter childImporter = + new ArrayImporter(allocator, childVectors.get(i), dictionaryProvider); childImporter.importChild(this, ArrowArray.wrap(children[i])); } } @@ -109,16 +114,18 @@ private void doImport(ArrowArray.Snapshot snapshot) { // reset the dictionary vector to the initial state dictionary.getVector().clear(); - ArrayImporter dictionaryImporter = new ArrayImporter(allocator, dictionary.getVector(), dictionaryProvider); + ArrayImporter dictionaryImporter = + new ArrayImporter(allocator, dictionary.getVector(), dictionaryProvider); dictionaryImporter.importChild(this, ArrowArray.wrap(snapshot.dictionary)); } // Import main data ArrowFieldNode fieldNode = new ArrowFieldNode(snapshot.length, snapshot.null_count); - long[] bufferPointers = NativeUtil.toJavaArray(snapshot.buffers, checkedCastToInt(snapshot.n_buffers)); + long[] bufferPointers = + NativeUtil.toJavaArray(snapshot.buffers, checkedCastToInt(snapshot.n_buffers)); - try (final BufferImportTypeVisitor visitor = new BufferImportTypeVisitor( - allocator, underlyingAllocation, fieldNode, bufferPointers)) { + try (final BufferImportTypeVisitor visitor = + new BufferImportTypeVisitor(allocator, underlyingAllocation, fieldNode, bufferPointers)) { final List buffers; if (bufferPointers == null || bufferPointers.length == 0) { buffers = Collections.emptyList(); @@ -128,7 +135,11 @@ private void doImport(ArrowArray.Snapshot snapshot) { vector.loadFieldBuffers(fieldNode, buffers); } catch (Exception e) { throw new IllegalArgumentException( - "Could not load buffers for field " + vector.getField() + ". error message: " + e.getMessage(), e); + "Could not load buffers for field " + + vector.getField() + + ". error message: " + + e.getMessage(), + e); } } } diff --git a/java/c/src/main/java/org/apache/arrow/c/ArrayStreamExporter.java b/java/c/src/main/java/org/apache/arrow/c/ArrayStreamExporter.java index 2c5ca08e717..2c47287a4cd 100644 --- a/java/c/src/main/java/org/apache/arrow/c/ArrayStreamExporter.java +++ b/java/c/src/main/java/org/apache/arrow/c/ArrayStreamExporter.java @@ -21,16 +21,13 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.nio.charset.StandardCharsets; - import org.apache.arrow.c.jni.JniWrapper; import org.apache.arrow.c.jni.PrivateData; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.ipc.ArrowReader; import org.apache.arrow.vector.types.pojo.Schema; -/** - * Utility to export an {@link ArrowReader} as an ArrowArrayStream. - */ +/** Utility to export an {@link ArrowReader} as an ArrowArrayStream. */ final class ArrayStreamExporter { private final BufferAllocator allocator; @@ -38,9 +35,7 @@ final class ArrayStreamExporter { this.allocator = allocator; } - /** - * Java-side state for the exported stream. - */ + /** Java-side state for the exported stream. */ static class ExportedArrayStreamPrivateData implements PrivateData { final BufferAllocator allocator; final ArrowReader reader; diff --git a/java/c/src/main/java/org/apache/arrow/c/ArrowArray.java b/java/c/src/main/java/org/apache/arrow/c/ArrowArray.java index a538852f47c..c1f301e7f9c 100644 --- a/java/c/src/main/java/org/apache/arrow/c/ArrowArray.java +++ b/java/c/src/main/java/org/apache/arrow/c/ArrowArray.java @@ -22,7 +22,6 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; - import org.apache.arrow.c.jni.JniWrapper; import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; @@ -32,9 +31,9 @@ /** * C Data Interface ArrowArray. - *

- * Represents a wrapper for the following C structure: - * + * + *

Represents a wrapper for the following C structure: + * *

  * struct ArrowArray {
  *     // Array data description
@@ -46,7 +45,7 @@
  *     const void** buffers;
  *     struct ArrowArray** children;
  *     struct ArrowArray* dictionary;
- * 
+ *
  *     // Release callback
  *     void (*release)(struct ArrowArray*);
  *     // Opaque producer-specific data
@@ -60,9 +59,7 @@ public class ArrowArray implements BaseStruct {
 
   private ArrowBuf data;
 
-  /**
-   * Snapshot of the ArrowArray raw data.
-   */
+  /** Snapshot of the ArrowArray raw data. */
   public static class Snapshot {
     public long length;
     public long null_count;
@@ -75,9 +72,7 @@ public static class Snapshot {
     public long release;
     public long private_data;
 
-    /**
-     * Initialize empty ArrowArray snapshot.
-     */
+    /** Initialize empty ArrowArray snapshot. */
     public Snapshot() {
       length = NULL;
       null_count = NULL;
@@ -94,21 +89,22 @@ public Snapshot() {
 
   /**
    * Create ArrowArray from an existing memory address.
-   * 

- * The resulting ArrowArray does not own the memory. - * + * + *

The resulting ArrowArray does not own the memory. + * * @param memoryAddress Memory address to wrap * @return A new ArrowArray instance */ public static ArrowArray wrap(long memoryAddress) { - return new ArrowArray(new ArrowBuf(ReferenceManager.NO_OP, null, ArrowArray.SIZE_OF, memoryAddress)); + return new ArrowArray( + new ArrowBuf(ReferenceManager.NO_OP, null, ArrowArray.SIZE_OF, memoryAddress)); } /** * Create ArrowArray by allocating memory. - *

- * The resulting ArrowArray owns the memory. - * + * + *

The resulting ArrowArray owns the memory. + * * @param allocator Allocator for memory allocations * @return A new ArrowArray instance */ @@ -123,9 +119,7 @@ public static ArrowArray allocateNew(BufferAllocator allocator) { this.data = data; } - /** - * Mark the array as released. - */ + /** Mark the array as released. */ public void markReleased() { directBuffer().putLong(INDEX_RELEASE_CALLBACK, NULL); } @@ -156,12 +150,13 @@ boolean isClosed() { } private ByteBuffer directBuffer() { - return MemoryUtil.directBuffer(memoryAddress(), ArrowArray.SIZE_OF).order(ByteOrder.nativeOrder()); + return MemoryUtil.directBuffer(memoryAddress(), ArrowArray.SIZE_OF) + .order(ByteOrder.nativeOrder()); } /** * Take a snapshot of the ArrowArray raw values. - * + * * @return snapshot */ public Snapshot snapshot() { @@ -180,12 +175,18 @@ public Snapshot snapshot() { return snapshot; } - /** - * Write values from Snapshot to the underlying ArrowArray memory buffer. - */ + /** Write values from Snapshot to the underlying ArrowArray memory buffer. */ public void save(Snapshot snapshot) { - directBuffer().putLong(snapshot.length).putLong(snapshot.null_count).putLong(snapshot.offset) - .putLong(snapshot.n_buffers).putLong(snapshot.n_children).putLong(snapshot.buffers).putLong(snapshot.children) - .putLong(snapshot.dictionary).putLong(snapshot.release).putLong(snapshot.private_data); + directBuffer() + .putLong(snapshot.length) + .putLong(snapshot.null_count) + .putLong(snapshot.offset) + .putLong(snapshot.n_buffers) + .putLong(snapshot.n_children) + .putLong(snapshot.buffers) + .putLong(snapshot.children) + .putLong(snapshot.dictionary) + .putLong(snapshot.release) + .putLong(snapshot.private_data); } } diff --git a/java/c/src/main/java/org/apache/arrow/c/ArrowArrayStream.java b/java/c/src/main/java/org/apache/arrow/c/ArrowArrayStream.java index caf1f2fe965..73064a70732 100644 --- a/java/c/src/main/java/org/apache/arrow/c/ArrowArrayStream.java +++ b/java/c/src/main/java/org/apache/arrow/c/ArrowArrayStream.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; - import org.apache.arrow.c.jni.CDataJniException; import org.apache.arrow.c.jni.JniWrapper; import org.apache.arrow.memory.ArrowBuf; @@ -33,8 +32,8 @@ /** * C Stream Interface ArrowArrayStream. - *

- * Represents a wrapper for the following C structure: + * + *

Represents a wrapper for the following C structure: * *

  * struct ArrowArrayStream {
@@ -52,9 +51,7 @@ public class ArrowArrayStream implements BaseStruct {
 
   private ArrowBuf data;
 
-  /**
-   * Snapshot of the ArrowArrayStream raw data.
-   */
+  /** Snapshot of the ArrowArrayStream raw data. */
   public static class Snapshot {
     public long get_schema;
     public long get_next;
@@ -62,9 +59,7 @@ public static class Snapshot {
     public long release;
     public long private_data;
 
-    /**
-     * Initialize empty ArrowArray snapshot.
-     */
+    /** Initialize empty ArrowArray snapshot. */
     public Snapshot() {
       get_schema = NULL;
       get_next = NULL;
@@ -76,20 +71,21 @@ public Snapshot() {
 
   /**
    * Create ArrowArrayStream from an existing memory address.
-   * 

- * The resulting ArrowArrayStream does not own the memory. + * + *

The resulting ArrowArrayStream does not own the memory. * * @param memoryAddress Memory address to wrap * @return A new ArrowArrayStream instance */ public static ArrowArrayStream wrap(long memoryAddress) { - return new ArrowArrayStream(new ArrowBuf(ReferenceManager.NO_OP, null, ArrowArrayStream.SIZE_OF, memoryAddress)); + return new ArrowArrayStream( + new ArrowBuf(ReferenceManager.NO_OP, null, ArrowArrayStream.SIZE_OF, memoryAddress)); } /** * Create ArrowArrayStream by allocating memory. - *

- * The resulting ArrowArrayStream owns the memory. + * + *

The resulting ArrowArrayStream owns the memory. * * @param allocator Allocator for memory allocations * @return A new ArrowArrayStream instance @@ -105,9 +101,7 @@ public static ArrowArrayStream allocateNew(BufferAllocator allocator) { this.data = data; } - /** - * Mark the array as released. - */ + /** Mark the array as released. */ public void markReleased() { directBuffer().putLong(INDEX_RELEASE_CALLBACK, NULL); } @@ -126,6 +120,7 @@ public void release() { /** * Get the schema of the stream. + * * @param schema The ArrowSchema struct to output to * @throws IOException if the stream returns an error */ @@ -140,6 +135,7 @@ public void getSchema(ArrowSchema schema) throws IOException { /** * Get the next batch in the stream. + * * @param array The ArrowArray struct to output to * @throws IOException if the stream returns an error */ @@ -161,7 +157,8 @@ public void close() { } private ByteBuffer directBuffer() { - return MemoryUtil.directBuffer(memoryAddress(), ArrowArrayStream.SIZE_OF).order(ByteOrder.nativeOrder()); + return MemoryUtil.directBuffer(memoryAddress(), ArrowArrayStream.SIZE_OF) + .order(ByteOrder.nativeOrder()); } /** @@ -180,9 +177,7 @@ public ArrowArrayStream.Snapshot snapshot() { return snapshot; } - /** - * Write values from Snapshot to the underlying ArrowArrayStream memory buffer. - */ + /** Write values from Snapshot to the underlying ArrowArrayStream memory buffer. */ public void save(ArrowArrayStream.Snapshot snapshot) { directBuffer() .putLong(snapshot.get_schema) diff --git a/java/c/src/main/java/org/apache/arrow/c/ArrowArrayStreamReader.java b/java/c/src/main/java/org/apache/arrow/c/ArrowArrayStreamReader.java index b39a3be9b84..024149a8b66 100644 --- a/java/c/src/main/java/org/apache/arrow/c/ArrowArrayStreamReader.java +++ b/java/c/src/main/java/org/apache/arrow/c/ArrowArrayStreamReader.java @@ -24,15 +24,12 @@ import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.dictionary.Dictionary; import org.apache.arrow.vector.ipc.ArrowReader; import org.apache.arrow.vector.types.pojo.Schema; -/** - * An implementation of an {@link ArrowReader} backed by an ArrowArrayStream. - */ +/** An implementation of an {@link ArrowReader} backed by an ArrowArrayStream. */ final class ArrowArrayStreamReader extends ArrowReader { private final ArrowArrayStream ownedStream; private final CDataDictionaryProvider provider; @@ -53,7 +50,8 @@ final class ArrowArrayStreamReader extends ArrowReader { @Override public Map getDictionaryVectors() { - return provider.getDictionaryIds().stream().collect(Collectors.toMap(Function.identity(), provider::lookup)); + return provider.getDictionaryIds().stream() + .collect(Collectors.toMap(Function.identity(), provider::lookup)); } @Override diff --git a/java/c/src/main/java/org/apache/arrow/c/ArrowSchema.java b/java/c/src/main/java/org/apache/arrow/c/ArrowSchema.java index b34ce7d5a52..e8141939a82 100644 --- a/java/c/src/main/java/org/apache/arrow/c/ArrowSchema.java +++ b/java/c/src/main/java/org/apache/arrow/c/ArrowSchema.java @@ -22,7 +22,6 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; - import org.apache.arrow.c.jni.JniWrapper; import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; @@ -31,9 +30,9 @@ /** * C Data Interface ArrowSchema. - *

- * Represents a wrapper for the following C structure: - * + * + *

Represents a wrapper for the following C structure: + * *

  * struct ArrowSchema {
  *     // Array type description
@@ -44,11 +43,11 @@
  *     int64_t n_children;
  *     struct ArrowSchema** children;
  *     struct ArrowSchema* dictionary;
- *      
+ *
  *     // Release callback
  *     void (*release)(struct ArrowSchema*);
  *     // Opaque producer-specific data
- *     void* private_data; 
+ *     void* private_data;
  * };
  * 
*/ @@ -57,9 +56,7 @@ public class ArrowSchema implements BaseStruct { private ArrowBuf data; - /** - * Snapshot of the ArrowSchema raw data. - */ + /** Snapshot of the ArrowSchema raw data. */ public static class Snapshot { public long format; public long name; @@ -71,9 +68,7 @@ public static class Snapshot { public long release; public long private_data; - /** - * Initialize empty ArrowSchema snapshot. - */ + /** Initialize empty ArrowSchema snapshot. */ public Snapshot() { format = NULL; name = NULL; @@ -89,21 +84,22 @@ public Snapshot() { /** * Create ArrowSchema from an existing memory address. - *

- * The resulting ArrowSchema does not own the memory. - * + * + *

The resulting ArrowSchema does not own the memory. + * * @param memoryAddress Memory address to wrap * @return A new ArrowSchema instance */ public static ArrowSchema wrap(long memoryAddress) { - return new ArrowSchema(new ArrowBuf(ReferenceManager.NO_OP, null, ArrowSchema.SIZE_OF, memoryAddress)); + return new ArrowSchema( + new ArrowBuf(ReferenceManager.NO_OP, null, ArrowSchema.SIZE_OF, memoryAddress)); } /** * Create ArrowSchema by allocating memory. - *

- * The resulting ArrowSchema owns the memory. - * + * + *

The resulting ArrowSchema owns the memory. + * * @param allocator Allocator for memory allocations * @return A new ArrowSchema instance */ @@ -137,12 +133,13 @@ public void close() { } private ByteBuffer directBuffer() { - return MemoryUtil.directBuffer(memoryAddress(), ArrowSchema.SIZE_OF).order(ByteOrder.nativeOrder()); + return MemoryUtil.directBuffer(memoryAddress(), ArrowSchema.SIZE_OF) + .order(ByteOrder.nativeOrder()); } /** * Take a snapshot of the ArrowSchema raw values. - * + * * @return snapshot */ public Snapshot snapshot() { @@ -160,12 +157,17 @@ public Snapshot snapshot() { return snapshot; } - /** - * Write values from Snapshot to the underlying ArrowSchema memory buffer. - */ + /** Write values from Snapshot to the underlying ArrowSchema memory buffer. */ public void save(Snapshot snapshot) { - directBuffer().putLong(snapshot.format).putLong(snapshot.name).putLong(snapshot.metadata).putLong(snapshot.flags) - .putLong(snapshot.n_children).putLong(snapshot.children).putLong(snapshot.dictionary).putLong(snapshot.release) + directBuffer() + .putLong(snapshot.format) + .putLong(snapshot.name) + .putLong(snapshot.metadata) + .putLong(snapshot.flags) + .putLong(snapshot.n_children) + .putLong(snapshot.children) + .putLong(snapshot.dictionary) + .putLong(snapshot.release) .putLong(snapshot.private_data); } } diff --git a/java/c/src/main/java/org/apache/arrow/c/BaseStruct.java b/java/c/src/main/java/org/apache/arrow/c/BaseStruct.java index d90fe817589..139f02b099b 100644 --- a/java/c/src/main/java/org/apache/arrow/c/BaseStruct.java +++ b/java/c/src/main/java/org/apache/arrow/c/BaseStruct.java @@ -17,27 +17,23 @@ package org.apache.arrow.c; -/** - * Base interface for C Data Interface structures. - */ +/** Base interface for C Data Interface structures. */ public interface BaseStruct extends AutoCloseable { /** * Get memory address. - * + * * @return Memory address */ long memoryAddress(); /** * Call the release callback of an ArrowArray. - *

- * This function must not be called for child arrays. + * + *

This function must not be called for child arrays. */ void release(); - /** - * Close to release the main buffer. - */ + /** Close to release the main buffer. */ @Override void close(); } diff --git a/java/c/src/main/java/org/apache/arrow/c/BufferImportTypeVisitor.java b/java/c/src/main/java/org/apache/arrow/c/BufferImportTypeVisitor.java index cd2a464f4fa..44ea851863d 100644 --- a/java/c/src/main/java/org/apache/arrow/c/BufferImportTypeVisitor.java +++ b/java/c/src/main/java/org/apache/arrow/c/BufferImportTypeVisitor.java @@ -24,7 +24,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.util.AutoCloseables; @@ -55,9 +54,7 @@ import org.apache.arrow.vector.types.pojo.ArrowType; import org.apache.arrow.vector.util.DataSizeRoundingUtil; -/** - * Import buffers from a C Data Interface struct. - */ +/** Import buffers from a C Data Interface struct. */ class BufferImportTypeVisitor implements ArrowType.ArrowTypeVisitor>, AutoCloseable { private final BufferAllocator allocator; private final ReferenceCountedArrowArray underlyingAllocation; @@ -65,8 +62,11 @@ class BufferImportTypeVisitor implements ArrowType.ArrowTypeVisitor imported; - BufferImportTypeVisitor(BufferAllocator allocator, ReferenceCountedArrowArray underlyingAllocation, - ArrowFieldNode fieldNode, long[] buffers) { + BufferImportTypeVisitor( + BufferAllocator allocator, + ReferenceCountedArrowArray underlyingAllocation, + ArrowFieldNode fieldNode, + long[] buffers) { this.allocator = allocator; this.underlyingAllocation = underlyingAllocation; this.fieldNode = fieldNode; @@ -82,14 +82,18 @@ public void close() throws Exception { @VisibleForTesting ArrowBuf importBuffer(ArrowType type, int index, long capacity) { checkState( - buffers.length > index, - "Expected at least %s buffers for type %s, but found %s", index + 1, type, buffers.length); + buffers.length > index, + "Expected at least %s buffers for type %s, but found %s", + index + 1, + type, + buffers.length); long bufferPtr = buffers[index]; if (bufferPtr == NULL) { // C array may be NULL but only accept that if expected capacity is zero too if (capacity != 0) { - throw new IllegalStateException(String.format("Buffer %s for type %s cannot be null", index, type)); + throw new IllegalStateException( + String.format("Buffer %s for type %s cannot be null", index, type)); } else { // no data in the C array, return an empty buffer return allocator.getEmpty(); @@ -123,18 +127,24 @@ private ArrowBuf importData(ArrowType type, long capacity) { private ArrowBuf maybeImportBitmap(ArrowType type) { checkState( buffers.length > 0, - "Expected at least %s buffers for type %s, but found %s", 1, type, buffers.length); + "Expected at least %s buffers for type %s, but found %s", + 1, + type, + buffers.length); if (buffers[0] == NULL) { return null; } - return importFixedBits(type, 0, /*bitsPerSlot=*/1); + return importFixedBits(type, 0, /* bitsPerSlot= */ 1); } @Override public List visit(ArrowType.Null type) { checkState( buffers.length == 0, - "Expected %s buffers for type %s, but found %s", 0, type, buffers.length); + "Expected %s buffers for type %s, but found %s", + 0, + type, + buffers.length); return Collections.emptyList(); } @@ -150,7 +160,8 @@ public List visit(ArrowType.List type) { @Override public List visit(ArrowType.LargeList type) { - return Arrays.asList(maybeImportBitmap(type), importOffsets(type, LargeListVector.OFFSET_WIDTH)); + return Arrays.asList( + maybeImportBitmap(type), importOffsets(type, LargeListVector.OFFSET_WIDTH)); } @Override @@ -164,7 +175,8 @@ public List visit(ArrowType.Union type) { case Sparse: return Collections.singletonList(importFixedBytes(type, 0, UnionVector.TYPE_WIDTH)); case Dense: - return Arrays.asList(importFixedBytes(type, 0, DenseUnionVector.TYPE_WIDTH), + return Arrays.asList( + importFixedBytes(type, 0, DenseUnionVector.TYPE_WIDTH), importFixedBytes(type, 1, DenseUnionVector.OFFSET_WIDTH)); default: throw new UnsupportedOperationException("Importing buffers for union type: " + type); @@ -185,11 +197,14 @@ public List visit(ArrowType.Int type) { public List visit(ArrowType.FloatingPoint type) { switch (type.getPrecision()) { case HALF: - return Arrays.asList(maybeImportBitmap(type), importFixedBytes(type, 1, /*bytesPerSlot=*/2)); + return Arrays.asList( + maybeImportBitmap(type), importFixedBytes(type, 1, /* bytesPerSlot= */ 2)); case SINGLE: - return Arrays.asList(maybeImportBitmap(type), importFixedBytes(type, 1, Float4Vector.TYPE_WIDTH)); + return Arrays.asList( + maybeImportBitmap(type), importFixedBytes(type, 1, Float4Vector.TYPE_WIDTH)); case DOUBLE: - return Arrays.asList(maybeImportBitmap(type), importFixedBytes(type, 1, Float8Vector.TYPE_WIDTH)); + return Arrays.asList( + maybeImportBitmap(type), importFixedBytes(type, 1, Float8Vector.TYPE_WIDTH)); default: throw new UnsupportedOperationException("Importing buffers for type: " + type); } @@ -202,7 +217,10 @@ public List visit(ArrowType.Utf8 type) { final int end = offsets.getInt(fieldNode.getLength() * (long) VarCharVector.OFFSET_WIDTH); checkState( end >= start, - "Offset buffer for type %s is malformed: start: %s, end: %s", type, start, end); + "Offset buffer for type %s is malformed: start: %s, end: %s", + type, + start, + end); final int len = end - start; offsets.getReferenceManager().retain(); return Arrays.asList(maybeImportBitmap(type), offsets, importData(type, len)); @@ -213,10 +231,14 @@ public List visit(ArrowType.Utf8 type) { public List visit(ArrowType.LargeUtf8 type) { try (ArrowBuf offsets = importOffsets(type, LargeVarCharVector.OFFSET_WIDTH)) { final long start = offsets.getLong(0); - final long end = offsets.getLong(fieldNode.getLength() * (long) LargeVarCharVector.OFFSET_WIDTH); + final long end = + offsets.getLong(fieldNode.getLength() * (long) LargeVarCharVector.OFFSET_WIDTH); checkState( end >= start, - "Offset buffer for type %s is malformed: start: %s, end: %s", type, start, end); + "Offset buffer for type %s is malformed: start: %s, end: %s", + type, + start, + end); final long len = end - start; offsets.getReferenceManager().retain(); return Arrays.asList(maybeImportBitmap(type), offsets, importData(type, len)); @@ -230,7 +252,10 @@ public List visit(ArrowType.Binary type) { final int end = offsets.getInt(fieldNode.getLength() * (long) VarBinaryVector.OFFSET_WIDTH); checkState( end >= start, - "Offset buffer for type %s is malformed: start: %s, end: %s", type, start, end); + "Offset buffer for type %s is malformed: start: %s, end: %s", + type, + start, + end); final int len = end - start; offsets.getReferenceManager().retain(); return Arrays.asList(maybeImportBitmap(type), offsets, importData(type, len)); @@ -242,10 +267,14 @@ public List visit(ArrowType.LargeBinary type) { try (ArrowBuf offsets = importOffsets(type, LargeVarBinaryVector.OFFSET_WIDTH)) { final long start = offsets.getLong(0); // TODO: need better tests to cover the failure when I forget to multiply by offset width - final long end = offsets.getLong(fieldNode.getLength() * (long) LargeVarBinaryVector.OFFSET_WIDTH); + final long end = + offsets.getLong(fieldNode.getLength() * (long) LargeVarBinaryVector.OFFSET_WIDTH); checkState( end >= start, - "Offset buffer for type %s is malformed: start: %s, end: %s", type, start, end); + "Offset buffer for type %s is malformed: start: %s, end: %s", + type, + start, + end); final long len = end - start; offsets.getReferenceManager().retain(); return Arrays.asList(maybeImportBitmap(type), offsets, importData(type, len)); @@ -259,7 +288,7 @@ public List visit(ArrowType.FixedSizeBinary type) { @Override public List visit(ArrowType.Bool type) { - return Arrays.asList(maybeImportBitmap(type), importFixedBits(type, 1, /*bitsPerSlot=*/1)); + return Arrays.asList(maybeImportBitmap(type), importFixedBits(type, 1, /* bitsPerSlot= */ 1)); } @Override @@ -271,9 +300,11 @@ public List visit(ArrowType.Decimal type) { public List visit(ArrowType.Date type) { switch (type.getUnit()) { case DAY: - return Arrays.asList(maybeImportBitmap(type), importFixedBytes(type, 1, DateDayVector.TYPE_WIDTH)); + return Arrays.asList( + maybeImportBitmap(type), importFixedBytes(type, 1, DateDayVector.TYPE_WIDTH)); case MILLISECOND: - return Arrays.asList(maybeImportBitmap(type), importFixedBytes(type, 1, DateMilliVector.TYPE_WIDTH)); + return Arrays.asList( + maybeImportBitmap(type), importFixedBytes(type, 1, DateMilliVector.TYPE_WIDTH)); default: throw new UnsupportedOperationException("Importing buffers for type: " + type); } @@ -283,13 +314,17 @@ public List visit(ArrowType.Date type) { public List visit(ArrowType.Time type) { switch (type.getUnit()) { case SECOND: - return Arrays.asList(maybeImportBitmap(type), importFixedBytes(type, 1, TimeSecVector.TYPE_WIDTH)); + return Arrays.asList( + maybeImportBitmap(type), importFixedBytes(type, 1, TimeSecVector.TYPE_WIDTH)); case MILLISECOND: - return Arrays.asList(maybeImportBitmap(type), importFixedBytes(type, 1, TimeMilliVector.TYPE_WIDTH)); + return Arrays.asList( + maybeImportBitmap(type), importFixedBytes(type, 1, TimeMilliVector.TYPE_WIDTH)); case MICROSECOND: - return Arrays.asList(maybeImportBitmap(type), importFixedBytes(type, 1, TimeMicroVector.TYPE_WIDTH)); + return Arrays.asList( + maybeImportBitmap(type), importFixedBytes(type, 1, TimeMicroVector.TYPE_WIDTH)); case NANOSECOND: - return Arrays.asList(maybeImportBitmap(type), importFixedBytes(type, 1, TimeNanoVector.TYPE_WIDTH)); + return Arrays.asList( + maybeImportBitmap(type), importFixedBytes(type, 1, TimeNanoVector.TYPE_WIDTH)); default: throw new UnsupportedOperationException("Importing buffers for type: " + type); } @@ -297,18 +332,23 @@ public List visit(ArrowType.Time type) { @Override public List visit(ArrowType.Timestamp type) { - return Arrays.asList(maybeImportBitmap(type), importFixedBytes(type, 1, TimeStampVector.TYPE_WIDTH)); + return Arrays.asList( + maybeImportBitmap(type), importFixedBytes(type, 1, TimeStampVector.TYPE_WIDTH)); } @Override public List visit(ArrowType.Interval type) { switch (type.getUnit()) { case YEAR_MONTH: - return Arrays.asList(maybeImportBitmap(type), importFixedBytes(type, 1, IntervalYearVector.TYPE_WIDTH)); + return Arrays.asList( + maybeImportBitmap(type), importFixedBytes(type, 1, IntervalYearVector.TYPE_WIDTH)); case DAY_TIME: - return Arrays.asList(maybeImportBitmap(type), importFixedBytes(type, 1, IntervalDayVector.TYPE_WIDTH)); + return Arrays.asList( + maybeImportBitmap(type), importFixedBytes(type, 1, IntervalDayVector.TYPE_WIDTH)); case MONTH_DAY_NANO: - return Arrays.asList(maybeImportBitmap(type), importFixedBytes(type, 1, IntervalMonthDayNanoVector.TYPE_WIDTH)); + return Arrays.asList( + maybeImportBitmap(type), + importFixedBytes(type, 1, IntervalMonthDayNanoVector.TYPE_WIDTH)); default: throw new UnsupportedOperationException("Importing buffers for type: " + type); } @@ -316,6 +356,7 @@ public List visit(ArrowType.Interval type) { @Override public List visit(ArrowType.Duration type) { - return Arrays.asList(maybeImportBitmap(type), importFixedBytes(type, 1, DurationVector.TYPE_WIDTH)); + return Arrays.asList( + maybeImportBitmap(type), importFixedBytes(type, 1, DurationVector.TYPE_WIDTH)); } } diff --git a/java/c/src/main/java/org/apache/arrow/c/CDataDictionaryProvider.java b/java/c/src/main/java/org/apache/arrow/c/CDataDictionaryProvider.java index 4a84f11704c..4e520a69b26 100644 --- a/java/c/src/main/java/org/apache/arrow/c/CDataDictionaryProvider.java +++ b/java/c/src/main/java/org/apache/arrow/c/CDataDictionaryProvider.java @@ -20,22 +20,19 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; - import org.apache.arrow.vector.dictionary.Dictionary; import org.apache.arrow.vector.dictionary.DictionaryProvider; /** * A DictionaryProvider that is used in C Data Interface for imports. - *

- * CDataDictionaryProvider is similar to - * {@link DictionaryProvider.MapDictionaryProvider} with a key difference that - * the dictionaries are owned by the provider so it must eventually be closed. - *

- * The typical usage is to create the CDataDictionaryProvider and pass it to - * {@link Data#importField} or {@link Data#importSchema} to allocate empty - * dictionaries based on the information in {@link ArrowSchema}. Then you can - * re-use the same dictionary provider in any function that imports an - * {@link ArrowArray} that has the same schema. + * + *

CDataDictionaryProvider is similar to {@link DictionaryProvider.MapDictionaryProvider} with a + * key difference that the dictionaries are owned by the provider so it must eventually be closed. + * + *

The typical usage is to create the CDataDictionaryProvider and pass it to {@link + * Data#importField} or {@link Data#importSchema} to allocate empty dictionaries based on the + * information in {@link ArrowSchema}. Then you can re-use the same dictionary provider in any + * function that imports an {@link ArrowArray} that has the same schema. */ public class CDataDictionaryProvider implements DictionaryProvider, AutoCloseable { @@ -69,5 +66,4 @@ public void close() { } map.clear(); } - } diff --git a/java/c/src/main/java/org/apache/arrow/c/Data.java b/java/c/src/main/java/org/apache/arrow/c/Data.java index c90ce7604d6..2d2a03b81f9 100644 --- a/java/c/src/main/java/org/apache/arrow/c/Data.java +++ b/java/c/src/main/java/org/apache/arrow/c/Data.java @@ -35,76 +35,78 @@ /** * Functions for working with the C data interface. - *

- * This API is EXPERIMENTAL. Note that currently only 64bit systems are - * supported. + * + *

This API is EXPERIMENTAL. Note that currently only 64bit systems are supported. */ public final class Data { - private Data() { - } + private Data() {} /** * Export Java Field using the C data interface format. - * + * * @param allocator Buffer allocator for allocating C data interface fields - * @param field Field object to export - * @param provider Dictionary provider for dictionary encoded fields (optional) - * @param out C struct where to export the field + * @param field Field object to export + * @param provider Dictionary provider for dictionary encoded fields (optional) + * @param out C struct where to export the field */ - public static void exportField(BufferAllocator allocator, Field field, DictionaryProvider provider, ArrowSchema out) { + public static void exportField( + BufferAllocator allocator, Field field, DictionaryProvider provider, ArrowSchema out) { SchemaExporter exporter = new SchemaExporter(allocator); exporter.export(out, field, provider); } /** * Export Java Schema using the C data interface format. - * + * * @param allocator Buffer allocator for allocating C data interface fields - * @param schema Schema object to export - * @param provider Dictionary provider for dictionary encoded fields (optional) - * @param out C struct where to export the field + * @param schema Schema object to export + * @param provider Dictionary provider for dictionary encoded fields (optional) + * @param out C struct where to export the field */ - public static void exportSchema(BufferAllocator allocator, Schema schema, DictionaryProvider provider, - ArrowSchema out) { + public static void exportSchema( + BufferAllocator allocator, Schema schema, DictionaryProvider provider, ArrowSchema out) { // Convert to a struct field equivalent to the input schema - FieldType fieldType = new FieldType(false, new ArrowType.Struct(), null, schema.getCustomMetadata()); + FieldType fieldType = + new FieldType(false, new ArrowType.Struct(), null, schema.getCustomMetadata()); Field field = new Field("", fieldType, schema.getFields()); exportField(allocator, field, provider, out); } /** * Export Java FieldVector using the C data interface format. - *

- * The resulting ArrowArray struct keeps the array data and buffers alive until - * its release callback is called by the consumer. - * + * + *

The resulting ArrowArray struct keeps the array data and buffers alive until its release + * callback is called by the consumer. + * * @param allocator Buffer allocator for allocating C data interface fields - * @param vector Vector object to export - * @param provider Dictionary provider for dictionary encoded vectors - * (optional) - * @param out C struct where to export the array + * @param vector Vector object to export + * @param provider Dictionary provider for dictionary encoded vectors (optional) + * @param out C struct where to export the array */ - public static void exportVector(BufferAllocator allocator, FieldVector vector, DictionaryProvider provider, - ArrowArray out) { + public static void exportVector( + BufferAllocator allocator, FieldVector vector, DictionaryProvider provider, ArrowArray out) { exportVector(allocator, vector, provider, out, null); } /** * Export Java FieldVector using the C data interface format. - *

- * The resulting ArrowArray struct keeps the array data and buffers alive until - * its release callback is called by the consumer. - * + * + *

The resulting ArrowArray struct keeps the array data and buffers alive until its release + * callback is called by the consumer. + * * @param allocator Buffer allocator for allocating C data interface fields - * @param vector Vector object to export - * @param provider Dictionary provider for dictionary encoded vectors - * (optional) - * @param out C struct where to export the array + * @param vector Vector object to export + * @param provider Dictionary provider for dictionary encoded vectors (optional) + * @param out C struct where to export the array * @param outSchema C struct where to export the array type (optional) */ - public static void exportVector(BufferAllocator allocator, FieldVector vector, DictionaryProvider provider, - ArrowArray out, ArrowSchema outSchema) { + public static void exportVector( + BufferAllocator allocator, + FieldVector vector, + DictionaryProvider provider, + ArrowArray out, + ArrowSchema outSchema) { if (outSchema != null) { exportField(allocator, vector.getField(), provider, outSchema); } @@ -114,97 +116,97 @@ public static void exportVector(BufferAllocator allocator, FieldVector vector, D } /** - * Export the current contents of a Java Table using the C data - * interface format. - *

- * The table is exported as if it were a struct array. The - * resulting ArrowArray struct keeps the record batch data and buffers alive - * until its release callback is called by the consumer. + * Export the current contents of a Java Table using the C data interface format. + * + *

The table is exported as if it were a struct array. The resulting ArrowArray struct keeps + * the record batch data and buffers alive until its release callback is called by the consumer. * * @param allocator Buffer allocator for allocating C data interface fields - * @param table Table to export - * @param out C struct where to export the record batch + * @param table Table to export + * @param out C struct where to export the record batch */ public static void exportTable(BufferAllocator allocator, Table table, ArrowArray out) { exportTable(allocator, table, table.getDictionaryProvider(), out, null); } /** - * Export the current contents of a Java Table using the C data - * interface format. - *

- * The table is exported as if it were a struct array. The - * resulting ArrowArray struct keeps the record batch data and buffers alive - * until its release callback is called by the consumer. + * Export the current contents of a Java Table using the C data interface format. + * + *

The table is exported as if it were a struct array. The resulting ArrowArray struct keeps + * the record batch data and buffers alive until its release callback is called by the consumer. * * @param allocator Buffer allocator for allocating C data interface fields - * @param table Table to export - * @param provider Dictionary provider for dictionary encoded vectors - * (optional) - * @param out C struct where to export the record batch + * @param table Table to export + * @param provider Dictionary provider for dictionary encoded vectors (optional) + * @param out C struct where to export the record batch */ - public static void exportTable(BufferAllocator allocator, Table table, - DictionaryProvider provider, ArrowArray out) { + public static void exportTable( + BufferAllocator allocator, Table table, DictionaryProvider provider, ArrowArray out) { exportTable(allocator, table, provider, out, null); } /** * Export the current contents of a Java Table using the C data interface format. - *

- * The table is exported as if it were a struct array. The - * resulting ArrowArray struct keeps the record batch data and buffers alive - * until its release callback is called by the consumer. + * + *

The table is exported as if it were a struct array. The resulting ArrowArray struct keeps + * the record batch data and buffers alive until its release callback is called by the consumer. * * @param allocator Buffer allocator for allocating C data interface fields - * @param table Table to export - * @param provider Dictionary provider for dictionary encoded vectors - * (optional) - * @param out C struct where to export the record batch + * @param table Table to export + * @param provider Dictionary provider for dictionary encoded vectors (optional) + * @param out C struct where to export the record batch * @param outSchema C struct where to export the record batch schema (optional) */ - public static void exportTable(BufferAllocator allocator, Table table, - DictionaryProvider provider, ArrowArray out, ArrowSchema outSchema) { + public static void exportTable( + BufferAllocator allocator, + Table table, + DictionaryProvider provider, + ArrowArray out, + ArrowSchema outSchema) { try (VectorSchemaRoot root = table.toVectorSchemaRoot()) { exportVectorSchemaRoot(allocator, root, provider, out, outSchema); } } /** - * Export the current contents of a Java VectorSchemaRoot using the C data - * interface format. - *

- * The vector schema root is exported as if it were a struct array. The - * resulting ArrowArray struct keeps the record batch data and buffers alive - * until its release callback is called by the consumer. + * Export the current contents of a Java VectorSchemaRoot using the C data interface format. + * + *

The vector schema root is exported as if it were a struct array. The resulting ArrowArray + * struct keeps the record batch data and buffers alive until its release callback is called by + * the consumer. * * @param allocator Buffer allocator for allocating C data interface fields - * @param vsr Vector schema root to export - * @param provider Dictionary provider for dictionary encoded vectors - * (optional) - * @param out C struct where to export the record batch + * @param vsr Vector schema root to export + * @param provider Dictionary provider for dictionary encoded vectors (optional) + * @param out C struct where to export the record batch */ - public static void exportVectorSchemaRoot(BufferAllocator allocator, VectorSchemaRoot vsr, - DictionaryProvider provider, ArrowArray out) { + public static void exportVectorSchemaRoot( + BufferAllocator allocator, + VectorSchemaRoot vsr, + DictionaryProvider provider, + ArrowArray out) { exportVectorSchemaRoot(allocator, vsr, provider, out, null); } /** - * Export the current contents of a Java VectorSchemaRoot using the C data - * interface format. - *

- * The vector schema root is exported as if it were a struct array. The - * resulting ArrowArray struct keeps the record batch data and buffers alive - * until its release callback is called by the consumer. - * + * Export the current contents of a Java VectorSchemaRoot using the C data interface format. + * + *

The vector schema root is exported as if it were a struct array. The resulting ArrowArray + * struct keeps the record batch data and buffers alive until its release callback is called by + * the consumer. + * * @param allocator Buffer allocator for allocating C data interface fields - * @param vsr Vector schema root to export - * @param provider Dictionary provider for dictionary encoded vectors - * (optional) - * @param out C struct where to export the record batch + * @param vsr Vector schema root to export + * @param provider Dictionary provider for dictionary encoded vectors (optional) + * @param out C struct where to export the record batch * @param outSchema C struct where to export the record batch schema (optional) */ - public static void exportVectorSchemaRoot(BufferAllocator allocator, VectorSchemaRoot vsr, - DictionaryProvider provider, ArrowArray out, ArrowSchema outSchema) { + public static void exportVectorSchemaRoot( + BufferAllocator allocator, + VectorSchemaRoot vsr, + DictionaryProvider provider, + ArrowArray out, + ArrowSchema outSchema) { if (outSchema != null) { exportSchema(allocator, vsr.getSchema(), provider, outSchema); } @@ -220,27 +222,30 @@ public static void exportVectorSchemaRoot(BufferAllocator allocator, VectorSchem /** * Export a reader as an ArrowArrayStream using the C Stream Interface. + * * @param allocator Buffer allocator for allocating C data interface fields * @param reader Reader to export * @param out C struct to export the stream */ - public static void exportArrayStream(BufferAllocator allocator, ArrowReader reader, ArrowArrayStream out) { + public static void exportArrayStream( + BufferAllocator allocator, ArrowReader reader, ArrowArrayStream out) { new ArrayStreamExporter(allocator).export(out, reader); } /** * Import Java Field from the C data interface. - *

- * The given ArrowSchema struct is released (as per the C data interface - * specification), even if this function fails. - * + * + *

The given ArrowSchema struct is released (as per the C data interface specification), even + * if this function fails. + * * @param allocator Buffer allocator for allocating dictionary vectors - * @param schema C data interface struct representing the field [inout] - * @param provider A dictionary provider will be initialized with empty - * dictionary vectors (optional) + * @param schema C data interface struct representing the field [inout] + * @param provider A dictionary provider will be initialized with empty dictionary vectors + * (optional) * @return Imported field object */ - public static Field importField(BufferAllocator allocator, ArrowSchema schema, CDataDictionaryProvider provider) { + public static Field importField( + BufferAllocator allocator, ArrowSchema schema, CDataDictionaryProvider provider) { try { SchemaImporter importer = new SchemaImporter(allocator); return importer.importField(schema, provider); @@ -252,36 +257,41 @@ public static Field importField(BufferAllocator allocator, ArrowSchema schema, C /** * Import Java Schema from the C data interface. - *

- * The given ArrowSchema struct is released (as per the C data interface - * specification), even if this function fails. - * + * + *

The given ArrowSchema struct is released (as per the C data interface specification), even + * if this function fails. + * * @param allocator Buffer allocator for allocating dictionary vectors - * @param schema C data interface struct representing the field - * @param provider A dictionary provider will be initialized with empty - * dictionary vectors (optional) + * @param schema C data interface struct representing the field + * @param provider A dictionary provider will be initialized with empty dictionary vectors + * (optional) * @return Imported schema object */ - public static Schema importSchema(BufferAllocator allocator, ArrowSchema schema, CDataDictionaryProvider provider) { + public static Schema importSchema( + BufferAllocator allocator, ArrowSchema schema, CDataDictionaryProvider provider) { Field structField = importField(allocator, schema, provider); if (structField.getType().getTypeID() != ArrowTypeID.Struct) { - throw new IllegalArgumentException("Cannot import schema: ArrowSchema describes non-struct type"); + throw new IllegalArgumentException( + "Cannot import schema: ArrowSchema describes non-struct type"); } return new Schema(structField.getChildren(), structField.getMetadata()); } /** * Import Java vector from the C data interface. - *

- * The ArrowArray struct has its contents moved (as per the C data interface - * specification) to a private object held alive by the resulting array. - * + * + *

The ArrowArray struct has its contents moved (as per the C data interface specification) to + * a private object held alive by the resulting array. + * * @param allocator Buffer allocator - * @param array C data interface struct holding the array data - * @param vector Imported vector object [out] - * @param provider Dictionary provider to load dictionary vectors to (optional) + * @param array C data interface struct holding the array data + * @param vector Imported vector object [out] + * @param provider Dictionary provider to load dictionary vectors to (optional) */ - public static void importIntoVector(BufferAllocator allocator, ArrowArray array, FieldVector vector, + public static void importIntoVector( + BufferAllocator allocator, + ArrowArray array, + FieldVector vector, DictionaryProvider provider) { ArrayImporter importer = new ArrayImporter(allocator, vector, provider); importer.importArray(array); @@ -289,18 +299,21 @@ public static void importIntoVector(BufferAllocator allocator, ArrowArray array, /** * Import Java vector and its type from the C data interface. - *

- * The ArrowArray struct has its contents moved (as per the C data interface - * specification) to a private object held alive by the resulting vector. The - * ArrowSchema struct is released, even if this function fails. - * + * + *

The ArrowArray struct has its contents moved (as per the C data interface specification) to + * a private object held alive by the resulting vector. The ArrowSchema struct is released, even + * if this function fails. + * * @param allocator Buffer allocator for allocating the output FieldVector - * @param array C data interface struct holding the array data - * @param schema C data interface struct holding the array type - * @param provider Dictionary provider to load dictionary vectors to (optional) + * @param array C data interface struct holding the array data + * @param schema C data interface struct holding the array type + * @param provider Dictionary provider to load dictionary vectors to (optional) * @return Imported vector object */ - public static FieldVector importVector(BufferAllocator allocator, ArrowArray array, ArrowSchema schema, + public static FieldVector importVector( + BufferAllocator allocator, + ArrowArray array, + ArrowSchema schema, CDataDictionaryProvider provider) { Field field = importField(allocator, schema, provider); FieldVector vector = field.createVector(allocator); @@ -310,20 +323,22 @@ public static FieldVector importVector(BufferAllocator allocator, ArrowArray arr /** * Import record batch from the C data interface into vector schema root. - * - * The ArrowArray struct has its contents moved (as per the C data interface - * specification) to a private object held alive by the resulting vector schema - * root. - * - * The schema of the vector schema root must match the input array (undefined - * behavior otherwise). - * + * + *

The ArrowArray struct has its contents moved (as per the C data interface specification) to + * a private object held alive by the resulting vector schema root. + * + *

The schema of the vector schema root must match the input array (undefined behavior + * otherwise). + * * @param allocator Buffer allocator - * @param array C data interface struct holding the record batch data - * @param root vector schema root to load into - * @param provider Dictionary provider to load dictionary vectors to (optional) + * @param array C data interface struct holding the record batch data + * @param root vector schema root to load into + * @param provider Dictionary provider to load dictionary vectors to (optional) */ - public static void importIntoVectorSchemaRoot(BufferAllocator allocator, ArrowArray array, VectorSchemaRoot root, + public static void importIntoVectorSchemaRoot( + BufferAllocator allocator, + ArrowArray array, + VectorSchemaRoot root, DictionaryProvider provider) { try (StructVector structVector = StructVector.emptyWithDuplicates("", allocator)) { structVector.initializeChildrenFromFields(root.getSchema().getFields()); @@ -338,43 +353,46 @@ public static void importIntoVectorSchemaRoot(BufferAllocator allocator, ArrowAr /** * Import Java vector schema root from a C data interface Schema. - * - * The type represented by the ArrowSchema struct must be a struct type array. - * - * The ArrowSchema struct is released, even if this function fails. - * + * + *

The type represented by the ArrowSchema struct must be a struct type array. + * + *

The ArrowSchema struct is released, even if this function fails. + * * @param allocator Buffer allocator for allocating the output VectorSchemaRoot - * @param schema C data interface struct holding the record batch schema - * @param provider Dictionary provider to load dictionary vectors to (optional) + * @param schema C data interface struct holding the record batch schema + * @param provider Dictionary provider to load dictionary vectors to (optional) * @return Imported vector schema root */ - public static VectorSchemaRoot importVectorSchemaRoot(BufferAllocator allocator, ArrowSchema schema, - CDataDictionaryProvider provider) { + public static VectorSchemaRoot importVectorSchemaRoot( + BufferAllocator allocator, ArrowSchema schema, CDataDictionaryProvider provider) { return importVectorSchemaRoot(allocator, null, schema, provider); } /** * Import Java vector schema root from the C data interface. - * - * The type represented by the ArrowSchema struct must be a struct type array. - * - * The ArrowArray struct has its contents moved (as per the C data interface - * specification) to a private object held alive by the resulting record batch. - * The ArrowSchema struct is released, even if this function fails. - * - * Prefer {@link #importIntoVectorSchemaRoot} for loading array data while - * reusing the same vector schema root. - * + * + *

The type represented by the ArrowSchema struct must be a struct type array. + * + *

The ArrowArray struct has its contents moved (as per the C data interface specification) to + * a private object held alive by the resulting record batch. The ArrowSchema struct is released, + * even if this function fails. + * + *

Prefer {@link #importIntoVectorSchemaRoot} for loading array data while reusing the same + * vector schema root. + * * @param allocator Buffer allocator for allocating the output VectorSchemaRoot - * @param array C data interface struct holding the record batch data - * (optional) - * @param schema C data interface struct holding the record batch schema - * @param provider Dictionary provider to load dictionary vectors to (optional) + * @param array C data interface struct holding the record batch data (optional) + * @param schema C data interface struct holding the record batch schema + * @param provider Dictionary provider to load dictionary vectors to (optional) * @return Imported vector schema root */ - public static VectorSchemaRoot importVectorSchemaRoot(BufferAllocator allocator, ArrowArray array, ArrowSchema schema, + public static VectorSchemaRoot importVectorSchemaRoot( + BufferAllocator allocator, + ArrowArray array, + ArrowSchema schema, CDataDictionaryProvider provider) { - VectorSchemaRoot vsr = VectorSchemaRoot.create(importSchema(allocator, schema, provider), allocator); + VectorSchemaRoot vsr = + VectorSchemaRoot.create(importSchema(allocator, schema, provider), allocator); if (array != null) { importIntoVectorSchemaRoot(allocator, array, vsr, provider); } @@ -383,6 +401,7 @@ public static VectorSchemaRoot importVectorSchemaRoot(BufferAllocator allocator, /** * Import an ArrowArrayStream as an {@link ArrowReader}. + * * @param allocator Buffer allocator for allocating the output data. * @param stream C stream interface struct to import. * @return Imported reader diff --git a/java/c/src/main/java/org/apache/arrow/c/Flags.java b/java/c/src/main/java/org/apache/arrow/c/Flags.java index 744b4695ac3..ff52e2a0eaa 100644 --- a/java/c/src/main/java/org/apache/arrow/c/Flags.java +++ b/java/c/src/main/java/org/apache/arrow/c/Flags.java @@ -21,16 +21,13 @@ import org.apache.arrow.vector.types.pojo.ArrowType.ArrowTypeID; import org.apache.arrow.vector.types.pojo.Field; -/** - * Flags as defined in the C data interface specification. - */ +/** Flags as defined in the C data interface specification. */ final class Flags { static final int ARROW_FLAG_DICTIONARY_ORDERED = 1; static final int ARROW_FLAG_NULLABLE = 2; static final int ARROW_FLAG_MAP_KEYS_SORTED = 4; - private Flags() { - } + private Flags() {} static long forField(Field field) { long flags = 0L; diff --git a/java/c/src/main/java/org/apache/arrow/c/Format.java b/java/c/src/main/java/org/apache/arrow/c/Format.java index 2875e46f749..e462cb3c725 100644 --- a/java/c/src/main/java/org/apache/arrow/c/Format.java +++ b/java/c/src/main/java/org/apache/arrow/c/Format.java @@ -19,7 +19,6 @@ import java.util.Arrays; import java.util.stream.Collectors; - import org.apache.arrow.util.Preconditions; import org.apache.arrow.vector.types.DateUnit; import org.apache.arrow.vector.types.FloatingPointPrecision; @@ -30,13 +29,11 @@ import org.apache.arrow.vector.types.pojo.ArrowType.ExtensionType; /** - * Conversion between {@link ArrowType} and string formats, as per C data - * interface specification. + * Conversion between {@link ArrowType} and string formats, as per C data interface specification. */ final class Format { - private Format() { - } + private Format() {} static String asString(ArrowType arrowType) { if (arrowType instanceof ExtensionType) { @@ -49,102 +46,112 @@ static String asString(ArrowType arrowType) { return "z"; case Bool: return "b"; - case Date: { - ArrowType.Date type = (ArrowType.Date) arrowType; - switch (type.getUnit()) { - case DAY: - return "tdD"; - case MILLISECOND: - return "tdm"; - default: - throw new UnsupportedOperationException( - String.format("Date type with unit %s is unsupported", type.getUnit())); + case Date: + { + ArrowType.Date type = (ArrowType.Date) arrowType; + switch (type.getUnit()) { + case DAY: + return "tdD"; + case MILLISECOND: + return "tdm"; + default: + throw new UnsupportedOperationException( + String.format("Date type with unit %s is unsupported", type.getUnit())); + } } - } - case Decimal: { - ArrowType.Decimal type = (ArrowType.Decimal) arrowType; - if (type.getBitWidth() == 128) { - return String.format("d:%d,%d", type.getPrecision(), type.getScale()); + case Decimal: + { + ArrowType.Decimal type = (ArrowType.Decimal) arrowType; + if (type.getBitWidth() == 128) { + return String.format("d:%d,%d", type.getPrecision(), type.getScale()); + } + return String.format( + "d:%d,%d,%d", type.getPrecision(), type.getScale(), type.getBitWidth()); } - return String.format("d:%d,%d,%d", type.getPrecision(), type.getScale(), type.getBitWidth()); - } - case Duration: { - ArrowType.Duration type = (ArrowType.Duration) arrowType; - switch (type.getUnit()) { - case SECOND: - return "tDs"; - case MILLISECOND: - return "tDm"; - case MICROSECOND: - return "tDu"; - case NANOSECOND: - return "tDn"; - default: - throw new UnsupportedOperationException( - String.format("Duration type with unit %s is unsupported", type.getUnit())); + case Duration: + { + ArrowType.Duration type = (ArrowType.Duration) arrowType; + switch (type.getUnit()) { + case SECOND: + return "tDs"; + case MILLISECOND: + return "tDm"; + case MICROSECOND: + return "tDu"; + case NANOSECOND: + return "tDn"; + default: + throw new UnsupportedOperationException( + String.format("Duration type with unit %s is unsupported", type.getUnit())); + } } - } - case FixedSizeBinary: { - ArrowType.FixedSizeBinary type = (ArrowType.FixedSizeBinary) arrowType; - return String.format("w:%d", type.getByteWidth()); - } - case FixedSizeList: { - ArrowType.FixedSizeList type = (ArrowType.FixedSizeList) arrowType; - return String.format("+w:%d", type.getListSize()); - } - case FloatingPoint: { - ArrowType.FloatingPoint type = (ArrowType.FloatingPoint) arrowType; - switch (type.getPrecision()) { - case HALF: - return "e"; - case SINGLE: - return "f"; - case DOUBLE: - return "g"; - default: - throw new UnsupportedOperationException( - String.format("FloatingPoint type with precision %s is unsupported", type.getPrecision())); + case FixedSizeBinary: + { + ArrowType.FixedSizeBinary type = (ArrowType.FixedSizeBinary) arrowType; + return String.format("w:%d", type.getByteWidth()); } - } - case Int: { - String format; - ArrowType.Int type = (ArrowType.Int) arrowType; - switch (type.getBitWidth()) { - case Byte.SIZE: - format = "C"; - break; - case Short.SIZE: - format = "S"; - break; - case Integer.SIZE: - format = "I"; - break; - case Long.SIZE: - format = "L"; - break; - default: - throw new UnsupportedOperationException( - String.format("Int type with bitwidth %d is unsupported", type.getBitWidth())); + case FixedSizeList: + { + ArrowType.FixedSizeList type = (ArrowType.FixedSizeList) arrowType; + return String.format("+w:%d", type.getListSize()); } - if (type.getIsSigned()) { - format = format.toLowerCase(); + case FloatingPoint: + { + ArrowType.FloatingPoint type = (ArrowType.FloatingPoint) arrowType; + switch (type.getPrecision()) { + case HALF: + return "e"; + case SINGLE: + return "f"; + case DOUBLE: + return "g"; + default: + throw new UnsupportedOperationException( + String.format( + "FloatingPoint type with precision %s is unsupported", type.getPrecision())); + } } - return format; - } - case Interval: { - ArrowType.Interval type = (ArrowType.Interval) arrowType; - switch (type.getUnit()) { - case DAY_TIME: - return "tiD"; - case YEAR_MONTH: - return "tiM"; - case MONTH_DAY_NANO: - return "tin"; - default: - throw new UnsupportedOperationException( - String.format("Interval type with unit %s is unsupported", type.getUnit())); + case Int: + { + String format; + ArrowType.Int type = (ArrowType.Int) arrowType; + switch (type.getBitWidth()) { + case Byte.SIZE: + format = "C"; + break; + case Short.SIZE: + format = "S"; + break; + case Integer.SIZE: + format = "I"; + break; + case Long.SIZE: + format = "L"; + break; + default: + throw new UnsupportedOperationException( + String.format("Int type with bitwidth %d is unsupported", type.getBitWidth())); + } + if (type.getIsSigned()) { + format = format.toLowerCase(); + } + return format; + } + case Interval: + { + ArrowType.Interval type = (ArrowType.Interval) arrowType; + switch (type.getUnit()) { + case DAY_TIME: + return "tiD"; + case YEAR_MONTH: + return "tiM"; + case MONTH_DAY_NANO: + return "tin"; + default: + throw new UnsupportedOperationException( + String.format("Interval type with unit %s is unsupported", type.getUnit())); + } } - } case LargeBinary: return "Z"; case LargeList: @@ -159,47 +166,54 @@ static String asString(ArrowType arrowType) { return "n"; case Struct: return "+s"; - case Time: { - ArrowType.Time type = (ArrowType.Time) arrowType; - if (type.getUnit() == TimeUnit.SECOND && type.getBitWidth() == 32) { - return "tts"; - } else if (type.getUnit() == TimeUnit.MILLISECOND && type.getBitWidth() == 32) { - return "ttm"; - } else if (type.getUnit() == TimeUnit.MICROSECOND && type.getBitWidth() == 64) { - return "ttu"; - } else if (type.getUnit() == TimeUnit.NANOSECOND && type.getBitWidth() == 64) { - return "ttn"; - } else { - throw new UnsupportedOperationException(String.format("Time type with unit %s and bitwidth %d is unsupported", - type.getUnit(), type.getBitWidth())); - } - } - case Timestamp: { - String format; - ArrowType.Timestamp type = (ArrowType.Timestamp) arrowType; - switch (type.getUnit()) { - case SECOND: - format = "tss"; - break; - case MILLISECOND: - format = "tsm"; - break; - case MICROSECOND: - format = "tsu"; - break; - case NANOSECOND: - format = "tsn"; - break; - default: + case Time: + { + ArrowType.Time type = (ArrowType.Time) arrowType; + if (type.getUnit() == TimeUnit.SECOND && type.getBitWidth() == 32) { + return "tts"; + } else if (type.getUnit() == TimeUnit.MILLISECOND && type.getBitWidth() == 32) { + return "ttm"; + } else if (type.getUnit() == TimeUnit.MICROSECOND && type.getBitWidth() == 64) { + return "ttu"; + } else if (type.getUnit() == TimeUnit.NANOSECOND && type.getBitWidth() == 64) { + return "ttn"; + } else { throw new UnsupportedOperationException( - String.format("Timestamp type with unit %s is unsupported", type.getUnit())); + String.format( + "Time type with unit %s and bitwidth %d is unsupported", + type.getUnit(), type.getBitWidth())); + } + } + case Timestamp: + { + String format; + ArrowType.Timestamp type = (ArrowType.Timestamp) arrowType; + switch (type.getUnit()) { + case SECOND: + format = "tss"; + break; + case MILLISECOND: + format = "tsm"; + break; + case MICROSECOND: + format = "tsu"; + break; + case NANOSECOND: + format = "tsn"; + break; + default: + throw new UnsupportedOperationException( + String.format("Timestamp type with unit %s is unsupported", type.getUnit())); + } + String timezone = type.getTimezone(); + return String.format("%s:%s", format, timezone == null ? "" : timezone); } - String timezone = type.getTimezone(); - return String.format("%s:%s", format, timezone == null ? "" : timezone); - } case Union: ArrowType.Union type = (ArrowType.Union) arrowType; - String typeIDs = Arrays.stream(type.getTypeIds()).mapToObj(String::valueOf).collect(Collectors.joining(",")); + String typeIDs = + Arrays.stream(type.getTypeIds()) + .mapToObj(String::valueOf) + .collect(Collectors.joining(",")); switch (type.getMode()) { case Dense: return String.format("+ud:%s", typeIDs); @@ -214,7 +228,8 @@ static String asString(ArrowType arrowType) { case NONE: throw new IllegalArgumentException("Arrow type ID is NONE"); default: - throw new UnsupportedOperationException(String.format("Unknown type id %s", arrowType.getTypeID())); + throw new UnsupportedOperationException( + String.format("Unknown type id %s", arrowType.getTypeID())); } } @@ -295,21 +310,24 @@ static ArrowType asType(String format, long flags) if (parts.length == 2) { return parseComplexFormat(parts[0], parts[1]); } - throw new UnsupportedOperationException(String.format("Format %s is not supported", format)); + throw new UnsupportedOperationException( + String.format("Format %s is not supported", format)); } } private static ArrowType parseComplexFormat(String format, String payload) throws NumberFormatException, UnsupportedOperationException, IllegalStateException { switch (format) { - case "d": { - int[] parts = payloadToIntArray(payload); - Preconditions.checkState(parts.length == 2 || parts.length == 3, "Format %s:%s is illegal", format, payload); - int precision = parts[0]; - int scale = parts[1]; - Integer bitWidth = (parts.length == 3) ? parts[2] : null; - return ArrowType.Decimal.createDecimal(precision, scale, bitWidth); - } + case "d": + { + int[] parts = payloadToIntArray(payload); + Preconditions.checkState( + parts.length == 2 || parts.length == 3, "Format %s:%s is illegal", format, payload); + int precision = parts[0]; + int scale = parts[1]; + Integer bitWidth = (parts.length == 3) ? parts[2] : null; + return ArrowType.Decimal.createDecimal(precision, scale, bitWidth); + } case "w": return new ArrowType.FixedSizeBinary(Integer.parseInt(payload)); case "+w": @@ -327,7 +345,8 @@ private static ArrowType parseComplexFormat(String format, String payload) case "tsn": return new ArrowType.Timestamp(TimeUnit.NANOSECOND, payloadToTimezone(payload)); default: - throw new UnsupportedOperationException(String.format("Format %s:%s is not supported", format, payload)); + throw new UnsupportedOperationException( + String.format("Format %s:%s is not supported", format, payload)); } } diff --git a/java/c/src/main/java/org/apache/arrow/c/Metadata.java b/java/c/src/main/java/org/apache/arrow/c/Metadata.java index b81b24fe4ce..d378ca315eb 100644 --- a/java/c/src/main/java/org/apache/arrow/c/Metadata.java +++ b/java/c/src/main/java/org/apache/arrow/c/Metadata.java @@ -27,18 +27,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.util.MemoryUtil; -/** - * Encode and decode metadata. - */ +/** Encode and decode metadata. */ final class Metadata { - private Metadata() { - } + private Metadata() {} static ArrowBuf encode(BufferAllocator allocator, Map metadata) { if (metadata == null || metadata.size() == 0) { @@ -57,7 +53,8 @@ static ArrowBuf encode(BufferAllocator allocator, Map metadata) } ArrowBuf result = allocator.buffer(totalSize); - ByteBuffer writer = MemoryUtil.directBuffer(result.memoryAddress(), totalSize).order(ByteOrder.nativeOrder()); + ByteBuffer writer = + MemoryUtil.directBuffer(result.memoryAddress(), totalSize).order(ByteOrder.nativeOrder()); writer.putInt(metadata.size()); for (byte[] buffer : buffers) { writer.putInt(buffer.length); @@ -71,7 +68,8 @@ static Map decode(long bufferAddress) { return null; } - ByteBuffer reader = MemoryUtil.directBuffer(bufferAddress, Integer.MAX_VALUE).order(ByteOrder.nativeOrder()); + ByteBuffer reader = + MemoryUtil.directBuffer(bufferAddress, Integer.MAX_VALUE).order(ByteOrder.nativeOrder()); int size = reader.getInt(); checkState(size >= 0, "Metadata size must not be negative"); diff --git a/java/c/src/main/java/org/apache/arrow/c/NativeUtil.java b/java/c/src/main/java/org/apache/arrow/c/NativeUtil.java index ba65fd80c41..ebbd736c8c0 100644 --- a/java/c/src/main/java/org/apache/arrow/c/NativeUtil.java +++ b/java/c/src/main/java/org/apache/arrow/c/NativeUtil.java @@ -21,24 +21,20 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.util.MemoryUtil; -/** - * Utility functions for working with native memory. - */ +/** Utility functions for working with native memory. */ public final class NativeUtil { public static final byte NULL = 0; static final int MAX_STRING_LENGTH = Short.MAX_VALUE; - private NativeUtil() { - } + private NativeUtil() {} /** * Convert a pointer to a null terminated string into a Java String. - * + * * @param cstringPtr pointer to C string * @return Converted string */ @@ -46,7 +42,8 @@ public static String toJavaString(long cstringPtr) { if (cstringPtr == NULL) { return null; } - ByteBuffer reader = MemoryUtil.directBuffer(cstringPtr, MAX_STRING_LENGTH).order(ByteOrder.nativeOrder()); + ByteBuffer reader = + MemoryUtil.directBuffer(cstringPtr, MAX_STRING_LENGTH).order(ByteOrder.nativeOrder()); int length = 0; while (reader.get() != NULL) { @@ -60,9 +57,9 @@ public static String toJavaString(long cstringPtr) { /** * Convert a native array pointer (void**) to Java array of pointers. - * + * * @param arrayPtr Array pointer - * @param size Array size + * @param size Array size * @return Array of pointer values as longs */ public static long[] toJavaArray(long arrayPtr, int size) { @@ -74,7 +71,8 @@ public static long[] toJavaArray(long arrayPtr, int size) { } long[] result = new long[size]; - ByteBuffer reader = MemoryUtil.directBuffer(arrayPtr, Long.BYTES * size).order(ByteOrder.nativeOrder()); + ByteBuffer reader = + MemoryUtil.directBuffer(arrayPtr, Long.BYTES * size).order(ByteOrder.nativeOrder()); for (int i = 0; i < size; i++) { result[i] = reader.getLong(); } @@ -83,9 +81,9 @@ public static long[] toJavaArray(long arrayPtr, int size) { /** * Convert Java string to a null terminated string. - * + * * @param allocator Buffer allocator for allocating the native string - * @param string Input String to convert + * @param string Input String to convert * @return Buffer with a null terminated string or null if the input is null */ public static ArrowBuf toNativeString(BufferAllocator allocator, String string) { @@ -102,7 +100,7 @@ public static ArrowBuf toNativeString(BufferAllocator allocator, String string) /** * Close a buffer if it's not null. - * + * * @param buf Buffer to close */ public static void closeBuffer(ArrowBuf buf) { @@ -113,7 +111,7 @@ public static void closeBuffer(ArrowBuf buf) { /** * Get the address of a buffer or {@value #NULL} if the input buffer is null. - * + * * @param buf Buffer to get the address of * @return Memory address or {@value #NULL} */ @@ -125,9 +123,8 @@ public static long addressOrNull(ArrowBuf buf) { } /** - * Get the address of a C Data Interface struct or {@value #NULL} if the input - * struct is null. - * + * Get the address of a C Data Interface struct or {@value #NULL} if the input struct is null. + * * @param struct C Data Interface struct to get the address of * @return Memory address or {@value #NULL} */ @@ -137,5 +134,4 @@ public static long addressOrNull(BaseStruct struct) { } return struct.memoryAddress(); } - } diff --git a/java/c/src/main/java/org/apache/arrow/c/ReferenceCountedArrowArray.java b/java/c/src/main/java/org/apache/arrow/c/ReferenceCountedArrowArray.java index f09f14817b6..9a14543801c 100644 --- a/java/c/src/main/java/org/apache/arrow/c/ReferenceCountedArrowArray.java +++ b/java/c/src/main/java/org/apache/arrow/c/ReferenceCountedArrowArray.java @@ -18,7 +18,6 @@ package org.apache.arrow.c; import java.util.concurrent.atomic.AtomicInteger; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.ForeignAllocation; @@ -26,10 +25,11 @@ /** * The owner of an imported C Data Interface array. * - *

There is a fundamental mismatch here between memory allocation schemes: AllocationManager represents a single - * allocation (= a single address and length). But an ArrowArray combines multiple allocations behind a single - * deallocation callback. This class bridges the two by tracking a reference count, so that the single callback - * can be managed by multiple {@link ForeignAllocation} instances. + *

There is a fundamental mismatch here between memory allocation schemes: AllocationManager + * represents a single allocation (= a single address and length). But an ArrowArray combines + * multiple allocations behind a single deallocation callback. This class bridges the two by + * tracking a reference count, so that the single callback can be managed by multiple {@link + * ForeignAllocation} instances. */ final class ReferenceCountedArrowArray { private final ArrowArray array; @@ -57,18 +57,21 @@ void release() { } /** - * Create an ArrowBuf wrapping a buffer from this ArrowArray associated with the given BufferAllocator. + * Create an ArrowBuf wrapping a buffer from this ArrowArray associated with the given + * BufferAllocator. * - *

This method is "unsafe" because there is no validation of the given capacity or address. If the returned - * buffer is not freed, a memory leak will occur. + *

This method is "unsafe" because there is no validation of the given capacity or address. If + * the returned buffer is not freed, a memory leak will occur. */ - ArrowBuf unsafeAssociateAllocation(BufferAllocator trackingAllocator, long capacity, long memoryAddress) { + ArrowBuf unsafeAssociateAllocation( + BufferAllocator trackingAllocator, long capacity, long memoryAddress) { retain(); - return trackingAllocator.wrapForeignAllocation(new ForeignAllocation(capacity, memoryAddress) { - @Override - protected void release0() { - ReferenceCountedArrowArray.this.release(); - } - }); + return trackingAllocator.wrapForeignAllocation( + new ForeignAllocation(capacity, memoryAddress) { + @Override + protected void release0() { + ReferenceCountedArrowArray.this.release(); + } + }); } } diff --git a/java/c/src/main/java/org/apache/arrow/c/SchemaExporter.java b/java/c/src/main/java/org/apache/arrow/c/SchemaExporter.java index 04d41a4e4f9..36c9e4eab55 100644 --- a/java/c/src/main/java/org/apache/arrow/c/SchemaExporter.java +++ b/java/c/src/main/java/org/apache/arrow/c/SchemaExporter.java @@ -23,7 +23,6 @@ import java.util.ArrayList; import java.util.List; - import org.apache.arrow.c.jni.JniWrapper; import org.apache.arrow.c.jni.PrivateData; import org.apache.arrow.memory.ArrowBuf; @@ -33,9 +32,7 @@ import org.apache.arrow.vector.types.pojo.DictionaryEncoding; import org.apache.arrow.vector.types.pojo.Field; -/** - * Exporter for {@link ArrowSchema}. - */ +/** Exporter for {@link ArrowSchema}. */ final class SchemaExporter { private final BufferAllocator allocator; @@ -43,9 +40,7 @@ public SchemaExporter(BufferAllocator allocator) { this.allocator = allocator; } - /** - * Private data structure for exported schemas. - */ + /** Private data structure for exported schemas. */ static class ExportedSchemaPrivateData implements PrivateData { ArrowBuf format; ArrowBuf name; diff --git a/java/c/src/main/java/org/apache/arrow/c/SchemaImporter.java b/java/c/src/main/java/org/apache/arrow/c/SchemaImporter.java index 09a6afafa0a..e66d6fffdad 100644 --- a/java/c/src/main/java/org/apache/arrow/c/SchemaImporter.java +++ b/java/c/src/main/java/org/apache/arrow/c/SchemaImporter.java @@ -25,7 +25,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.dictionary.Dictionary; import org.apache.arrow.vector.types.pojo.ArrowType; @@ -37,9 +36,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * Importer for {@link ArrowSchema}. - */ +/** Importer for {@link ArrowSchema}. */ final class SchemaImporter { private static final Logger logger = LoggerFactory.getLogger(SchemaImporter.class); @@ -56,8 +53,11 @@ Field importField(ArrowSchema schema, CDataDictionaryProvider provider) { return importField(schema, provider, 0); } - private Field importField(ArrowSchema schema, CDataDictionaryProvider provider, int recursionLevel) { - checkState(recursionLevel <= MAX_IMPORT_RECURSION_LEVEL, "Recursion level in ArrowSchema struct exceeded"); + private Field importField( + ArrowSchema schema, CDataDictionaryProvider provider, int recursionLevel) { + checkState( + recursionLevel <= MAX_IMPORT_RECURSION_LEVEL, + "Recursion level in ArrowSchema struct exceeded"); ArrowSchema.Snapshot snapshot = schema.snapshot(); checkState(snapshot.release != NULL, "Cannot import released ArrowSchema"); @@ -71,7 +71,8 @@ private Field importField(ArrowSchema schema, CDataDictionaryProvider provider, if (metadata != null && metadata.containsKey(ExtensionType.EXTENSION_METADATA_KEY_NAME)) { final String extensionName = metadata.get(ExtensionType.EXTENSION_METADATA_KEY_NAME); - final String extensionMetadata = metadata.getOrDefault(ExtensionType.EXTENSION_METADATA_KEY_METADATA, ""); + final String extensionMetadata = + metadata.getOrDefault(ExtensionType.EXTENSION_METADATA_KEY_METADATA, ""); ExtensionType extensionType = ExtensionTypeRegistry.lookup(extensionName); if (extensionType != null) { arrowType = extensionType.deserialize(arrowType, extensionMetadata); @@ -96,7 +97,8 @@ private Field importField(ArrowSchema schema, CDataDictionaryProvider provider, FieldType fieldType = new FieldType(nullable, arrowType, dictionaryEncoding, metadata); List children = null; - long[] childrenIds = NativeUtil.toJavaArray(snapshot.children, checkedCastToInt(snapshot.n_children)); + long[] childrenIds = + NativeUtil.toJavaArray(snapshot.children, checkedCastToInt(snapshot.n_children)); if (childrenIds != null && childrenIds.length > 0) { children = new ArrayList<>(childrenIds.length); for (long childAddress : childrenIds) { diff --git a/java/c/src/main/java/org/apache/arrow/c/StructVectorLoader.java b/java/c/src/main/java/org/apache/arrow/c/StructVectorLoader.java index d9afd0189d8..a1e11b5f91b 100644 --- a/java/c/src/main/java/org/apache/arrow/c/StructVectorLoader.java +++ b/java/c/src/main/java/org/apache/arrow/c/StructVectorLoader.java @@ -23,7 +23,6 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.util.Collections2; @@ -38,17 +37,15 @@ import org.apache.arrow.vector.types.pojo.Field; import org.apache.arrow.vector.types.pojo.Schema; -/** - * Loads buffers into {@link StructVector}. - */ +/** Loads buffers into {@link StructVector}. */ public class StructVectorLoader { private final Schema schema; private final CompressionCodec.Factory factory; /** - * A flag indicating if decompression is needed. This will affect the behavior - * of releasing buffers. + * A flag indicating if decompression is needed. This will affect the behavior of releasing + * buffers. */ private boolean decompressionNeeded; @@ -64,7 +61,7 @@ public StructVectorLoader(Schema schema) { /** * Construct with a schema and a compression codec factory. * - * @param schema buffers are added based on schema. + * @param schema buffers are added based on schema. * @param factory the factory to create codec. */ public StructVectorLoader(Schema schema, CompressionCodec.Factory factory) { @@ -74,9 +71,8 @@ public StructVectorLoader(Schema schema, CompressionCodec.Factory factory) { /** * Loads the record batch into the struct vector. - * - *

- * This will not close the record batch. + * + *

This will not close the record batch. * * @param recordBatch the batch to load */ @@ -86,22 +82,31 @@ public StructVector load(BufferAllocator allocator, ArrowRecordBatch recordBatch Iterator buffers = recordBatch.getBuffers().iterator(); Iterator nodes = recordBatch.getNodes().iterator(); - CompressionUtil.CodecType codecType = CompressionUtil.CodecType - .fromCompressionType(recordBatch.getBodyCompression().getCodec()); + CompressionUtil.CodecType codecType = + CompressionUtil.CodecType.fromCompressionType(recordBatch.getBodyCompression().getCodec()); decompressionNeeded = codecType != CompressionUtil.CodecType.NO_COMPRESSION; - CompressionCodec codec = decompressionNeeded ? factory.createCodec(codecType) : NoCompressionCodec.INSTANCE; + CompressionCodec codec = + decompressionNeeded ? factory.createCodec(codecType) : NoCompressionCodec.INSTANCE; for (FieldVector fieldVector : result.getChildrenFromFields()) { loadBuffers(fieldVector, fieldVector.getField(), buffers, nodes, codec); } - result.loadFieldBuffers(new ArrowFieldNode(recordBatch.getLength(), 0), Collections.singletonList(null)); + result.loadFieldBuffers( + new ArrowFieldNode(recordBatch.getLength(), 0), Collections.singletonList(null)); if (nodes.hasNext() || buffers.hasNext()) { - throw new IllegalArgumentException("not all nodes and buffers were consumed. nodes: " + - Collections2.toList(nodes).toString() + " buffers: " + Collections2.toList(buffers).toString()); + throw new IllegalArgumentException( + "not all nodes and buffers were consumed. nodes: " + + Collections2.toList(nodes).toString() + + " buffers: " + + Collections2.toList(buffers).toString()); } return result; } - private void loadBuffers(FieldVector vector, Field field, Iterator buffers, Iterator nodes, + private void loadBuffers( + FieldVector vector, + Field field, + Iterator buffers, + Iterator nodes, CompressionCodec codec) { checkArgument(nodes.hasNext(), "no more field nodes for field %s and vector %s", field, vector); ArrowFieldNode fieldNode = nodes.next(); @@ -111,7 +116,8 @@ private void loadBuffers(FieldVector vector, Field field, Iterator buf ArrowBuf nextBuf = buffers.next(); // for vectors without nulls, the buffer is empty, so there is no need to // decompress it. - ArrowBuf bufferToAdd = nextBuf.writerIndex() > 0 ? codec.decompress(vector.getAllocator(), nextBuf) : nextBuf; + ArrowBuf bufferToAdd = + nextBuf.writerIndex() > 0 ? codec.decompress(vector.getAllocator(), nextBuf) : nextBuf; ownBuffers.add(bufferToAdd); if (decompressionNeeded) { // decompression performed @@ -132,8 +138,10 @@ private void loadBuffers(FieldVector vector, Field field, Iterator buf List children = field.getChildren(); if (children.size() > 0) { List childrenFromFields = vector.getChildrenFromFields(); - checkArgument(children.size() == childrenFromFields.size(), - "should have as many children as in the schema: found %s expected %s", childrenFromFields.size(), + checkArgument( + children.size() == childrenFromFields.size(), + "should have as many children as in the schema: found %s expected %s", + childrenFromFields.size(), children.size()); for (int i = 0; i < childrenFromFields.size(); i++) { Field child = children.get(i); diff --git a/java/c/src/main/java/org/apache/arrow/c/StructVectorUnloader.java b/java/c/src/main/java/org/apache/arrow/c/StructVectorUnloader.java index aa6d9b4d0f6..7131f900f15 100644 --- a/java/c/src/main/java/org/apache/arrow/c/StructVectorUnloader.java +++ b/java/c/src/main/java/org/apache/arrow/c/StructVectorUnloader.java @@ -19,7 +19,6 @@ import java.util.ArrayList; import java.util.List; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.TypeLayout; @@ -30,10 +29,7 @@ import org.apache.arrow.vector.ipc.message.ArrowFieldNode; import org.apache.arrow.vector.ipc.message.ArrowRecordBatch; -/** - * Helper class that handles converting a {@link StructVector} to a - * {@link ArrowRecordBatch}. - */ +/** Helper class that handles converting a {@link StructVector} to a {@link ArrowRecordBatch}. */ public class StructVectorUnloader { private final StructVector root; @@ -41,9 +37,7 @@ public class StructVectorUnloader { private final CompressionCodec codec; private final boolean alignBuffers; - /** - * Constructs a new instance of the given struct vector. - */ + /** Constructs a new instance of the given struct vector. */ public StructVectorUnloader(StructVector root) { this(root, true, NoCompressionCodec.INSTANCE, true); } @@ -51,11 +45,9 @@ public StructVectorUnloader(StructVector root) { /** * Constructs a new instance. * - * @param root The struct vector to serialize to an - * {@link ArrowRecordBatch}. - * @param includeNullCount Controls whether null count is copied to the - * {@link ArrowRecordBatch} - * @param alignBuffers Controls if buffers get aligned to 8-byte boundaries. + * @param root The struct vector to serialize to an {@link ArrowRecordBatch}. + * @param includeNullCount Controls whether null count is copied to the {@link ArrowRecordBatch} + * @param alignBuffers Controls if buffers get aligned to 8-byte boundaries. */ public StructVectorUnloader(StructVector root, boolean includeNullCount, boolean alignBuffers) { this(root, includeNullCount, NoCompressionCodec.INSTANCE, alignBuffers); @@ -64,16 +56,13 @@ public StructVectorUnloader(StructVector root, boolean includeNullCount, boolean /** * Constructs a new instance. * - * @param root The struct vector to serialize to an - * {@link ArrowRecordBatch}. - * @param includeNullCount Controls whether null count is copied to the - * {@link ArrowRecordBatch} - * @param codec the codec for compressing data. If it is null, then - * no compression is needed. - * @param alignBuffers Controls if buffers get aligned to 8-byte boundaries. + * @param root The struct vector to serialize to an {@link ArrowRecordBatch}. + * @param includeNullCount Controls whether null count is copied to the {@link ArrowRecordBatch} + * @param codec the codec for compressing data. If it is null, then no compression is needed. + * @param alignBuffers Controls if buffers get aligned to 8-byte boundaries. */ - public StructVectorUnloader(StructVector root, boolean includeNullCount, CompressionCodec codec, - boolean alignBuffers) { + public StructVectorUnloader( + StructVector root, boolean includeNullCount, CompressionCodec codec, boolean alignBuffers) { this.root = root; this.includeNullCount = includeNullCount; this.codec = codec; @@ -81,8 +70,8 @@ public StructVectorUnloader(StructVector root, boolean includeNullCount, Compres } /** - * Performs the depth first traversal of the Vectors to create an - * {@link ArrowRecordBatch} suitable for serialization. + * Performs the depth first traversal of the Vectors to create an {@link ArrowRecordBatch} + * suitable for serialization. */ public ArrowRecordBatch getRecordBatch() { List nodes = new ArrayList<>(); @@ -90,17 +79,24 @@ public ArrowRecordBatch getRecordBatch() { for (FieldVector vector : root.getChildrenFromFields()) { appendNodes(vector, nodes, buffers); } - return new ArrowRecordBatch(root.getValueCount(), nodes, buffers, CompressionUtil.createBodyCompression(codec), + return new ArrowRecordBatch( + root.getValueCount(), + nodes, + buffers, + CompressionUtil.createBodyCompression(codec), alignBuffers); } private void appendNodes(FieldVector vector, List nodes, List buffers) { - nodes.add(new ArrowFieldNode(vector.getValueCount(), includeNullCount ? vector.getNullCount() : -1)); + nodes.add( + new ArrowFieldNode(vector.getValueCount(), includeNullCount ? vector.getNullCount() : -1)); List fieldBuffers = vector.getFieldBuffers(); int expectedBufferCount = TypeLayout.getTypeBufferCount(vector.getField().getType()); if (fieldBuffers.size() != expectedBufferCount) { - throw new IllegalArgumentException(String.format("wrong number of buffers for field %s in vector %s. found: %s", - vector.getField(), vector.getClass().getSimpleName(), fieldBuffers)); + throw new IllegalArgumentException( + String.format( + "wrong number of buffers for field %s in vector %s. found: %s", + vector.getField(), vector.getClass().getSimpleName(), fieldBuffers)); } for (ArrowBuf buf : fieldBuffers) { buffers.add(codec.compress(vector.getAllocator(), buf)); diff --git a/java/c/src/main/java/org/apache/arrow/c/jni/CDataJniException.java b/java/c/src/main/java/org/apache/arrow/c/jni/CDataJniException.java index bebd434f3db..01d8f1a6447 100644 --- a/java/c/src/main/java/org/apache/arrow/c/jni/CDataJniException.java +++ b/java/c/src/main/java/org/apache/arrow/c/jni/CDataJniException.java @@ -17,9 +17,7 @@ package org.apache.arrow.c.jni; -/** - * An exception raised by the JNI side of the C Data bridge. - */ +/** An exception raised by the JNI side of the C Data bridge. */ public final class CDataJniException extends Exception { private final int errno; @@ -28,18 +26,13 @@ public CDataJniException(int errno, String message) { this.errno = errno; } - /** - * The original error code returned from C. - */ + /** The original error code returned from C. */ public int getErrno() { return errno; } @Override public String toString() { - return "CDataJniException{" + - "errno=" + errno + - ", message=" + getMessage() + - '}'; + return "CDataJniException{" + "errno=" + errno + ", message=" + getMessage() + '}'; } } diff --git a/java/c/src/main/java/org/apache/arrow/c/jni/JniLoader.java b/java/c/src/main/java/org/apache/arrow/c/jni/JniLoader.java index ef9f432cf00..fdc7f5493fa 100644 --- a/java/c/src/main/java/org/apache/arrow/c/jni/JniLoader.java +++ b/java/c/src/main/java/org/apache/arrow/c/jni/JniLoader.java @@ -30,11 +30,10 @@ import java.util.Locale; import java.util.Set; -/** - * The JniLoader for C Data Interface API's native implementation. - */ +/** The JniLoader for C Data Interface API's native implementation. */ public class JniLoader { - private static final JniLoader INSTANCE = new JniLoader(Collections.singletonList("arrow_cdata_jni")); + private static final JniLoader INSTANCE = + new JniLoader(Collections.singletonList("arrow_cdata_jni")); public static JniLoader get() { return INSTANCE; @@ -50,9 +49,7 @@ private boolean finished() { return librariesToLoad.isEmpty(); } - /** - * If required JNI libraries are not loaded, then load them. - */ + /** If required JNI libraries are not loaded, then load them. */ public void ensureLoaded() { if (finished()) { return; @@ -82,9 +79,11 @@ private void load(String name) { final String libraryToLoad = name + "/" + getNormalizedArch() + "/" + System.mapLibraryName(name); try { - File temp = File.createTempFile("jnilib-", ".tmp", new File(System.getProperty("java.io.tmpdir"))); + File temp = + File.createTempFile("jnilib-", ".tmp", new File(System.getProperty("java.io.tmpdir"))); temp.deleteOnExit(); - try (final InputStream is = JniWrapper.class.getClassLoader().getResourceAsStream(libraryToLoad)) { + try (final InputStream is = + JniWrapper.class.getClassLoader().getResourceAsStream(libraryToLoad)) { if (is == null) { throw new FileNotFoundException(libraryToLoad); } diff --git a/java/c/src/main/java/org/apache/arrow/c/jni/JniWrapper.java b/java/c/src/main/java/org/apache/arrow/c/jni/JniWrapper.java index eb299b65f00..82574572393 100644 --- a/java/c/src/main/java/org/apache/arrow/c/jni/JniWrapper.java +++ b/java/c/src/main/java/org/apache/arrow/c/jni/JniWrapper.java @@ -17,9 +17,7 @@ package org.apache.arrow.c.jni; -/** - * JniWrapper for C Data Interface API implementation. - */ +/** JniWrapper for C Data Interface API implementation. */ public class JniWrapper { private static final JniWrapper INSTANCE = new JniWrapper(); @@ -41,9 +39,11 @@ private JniWrapper() { public native void releaseArray(long memoryAddress); - public native void getNextArrayStream(long streamAddress, long arrayAddress) throws CDataJniException; + public native void getNextArrayStream(long streamAddress, long arrayAddress) + throws CDataJniException; - public native void getSchemaArrayStream(long streamAddress, long arrayAddress) throws CDataJniException; + public native void getSchemaArrayStream(long streamAddress, long arrayAddress) + throws CDataJniException; public native void releaseArrayStream(long memoryAddress); diff --git a/java/c/src/main/java/org/apache/arrow/c/jni/PrivateData.java b/java/c/src/main/java/org/apache/arrow/c/jni/PrivateData.java index e6336cc6477..f4585946e26 100644 --- a/java/c/src/main/java/org/apache/arrow/c/jni/PrivateData.java +++ b/java/c/src/main/java/org/apache/arrow/c/jni/PrivateData.java @@ -21,8 +21,8 @@ /** * Interface for Java objects stored in C data interface private data. - *

- * This interface is used for exported structures. + * + *

This interface is used for exported structures. */ public interface PrivateData extends Closeable { diff --git a/java/c/src/test/java/org/apache/arrow/c/ArrowArrayUtilityTest.java b/java/c/src/test/java/org/apache/arrow/c/ArrowArrayUtilityTest.java index 4a0a966ba1f..956f36ad37c 100644 --- a/java/c/src/test/java/org/apache/arrow/c/ArrowArrayUtilityTest.java +++ b/java/c/src/test/java/org/apache/arrow/c/ArrowArrayUtilityTest.java @@ -21,7 +21,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.List; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.ReferenceManager; @@ -59,42 +58,56 @@ void afterEach() { void importBuffer() throws Exception { // Note values are all dummy values here try (BufferImportTypeVisitor notEmptyDataVisitor = - new BufferImportTypeVisitor(allocator, dummyHandle, new ArrowFieldNode(/* length= */ 1, 0), new long[]{0})) { + new BufferImportTypeVisitor( + allocator, dummyHandle, new ArrowFieldNode(/* length= */ 1, 0), new long[] {0})) { // Too few buffers - assertThrows(IllegalStateException.class, () -> notEmptyDataVisitor.importBuffer(new ArrowType.Bool(), 1, 1)); + assertThrows( + IllegalStateException.class, + () -> notEmptyDataVisitor.importBuffer(new ArrowType.Bool(), 1, 1)); // Null where one isn't expected - assertThrows(IllegalStateException.class, () -> notEmptyDataVisitor.importBuffer(new ArrowType.Bool(), 0, 1)); + assertThrows( + IllegalStateException.class, + () -> notEmptyDataVisitor.importBuffer(new ArrowType.Bool(), 0, 1)); // Expected capacity not zero but c array ptr is NULL (zero) - assertThrows(IllegalStateException.class, () -> notEmptyDataVisitor.importBuffer(new ArrowType.Bool(), 0, 1)); + assertThrows( + IllegalStateException.class, + () -> notEmptyDataVisitor.importBuffer(new ArrowType.Bool(), 0, 1)); // Expected capacity is zero and c array ptr is NULL (zero) - assertThat(notEmptyDataVisitor.importBuffer(new ArrowType.Bool(), 0, 0)).isEqualTo(allocator.getEmpty()); + assertThat(notEmptyDataVisitor.importBuffer(new ArrowType.Bool(), 0, 0)) + .isEqualTo(allocator.getEmpty()); } try (BufferImportTypeVisitor emptyDataVisitor = - new BufferImportTypeVisitor(allocator, dummyHandle, new ArrowFieldNode(/* length= */ 0, 0), new long[]{0})) { + new BufferImportTypeVisitor( + allocator, dummyHandle, new ArrowFieldNode(/* length= */ 0, 0), new long[] {0})) { // Too few buffers - assertThrows(IllegalStateException.class, () -> emptyDataVisitor.importBuffer(new ArrowType.Bool(), 1, 1)); + assertThrows( + IllegalStateException.class, + () -> emptyDataVisitor.importBuffer(new ArrowType.Bool(), 1, 1)); // Expected capacity not zero but c array ptr is NULL (zero) - assertThrows(IllegalStateException.class, () -> emptyDataVisitor.importBuffer(new ArrowType.Bool(), 0, 1)); + assertThrows( + IllegalStateException.class, + () -> emptyDataVisitor.importBuffer(new ArrowType.Bool(), 0, 1)); // Expected capacity is zero and c array ptr is NULL (zero) - assertThat(emptyDataVisitor.importBuffer(new ArrowType.Bool(), 0, 0)).isEqualTo(allocator.getEmpty()); + assertThat(emptyDataVisitor.importBuffer(new ArrowType.Bool(), 0, 0)) + .isEqualTo(allocator.getEmpty()); } } - @Test void cleanupAfterFailure() throws Exception { // Note values are all dummy values here long address = MemoryUtil.UNSAFE.allocateMemory(16); try (BufferImportTypeVisitor visitor = - new BufferImportTypeVisitor(allocator, dummyHandle, new ArrowFieldNode(0, 0), new long[] {address})) { + new BufferImportTypeVisitor( + allocator, dummyHandle, new ArrowFieldNode(0, 0), new long[] {address})) { // This fails, but only after we've already imported a buffer. assertThrows(IllegalStateException.class, () -> visitor.visit(new ArrowType.Int(32, true))); } finally { @@ -111,7 +124,7 @@ void bufferAssociatedWithAllocator() throws Exception { long baseline = allocator.getAllocatedMemory(); ArrowFieldNode fieldNode = new ArrowFieldNode(fieldLength, 0); try (BufferImportTypeVisitor visitor = - new BufferImportTypeVisitor(allocator, dummyHandle, fieldNode, new long[] {0, address})) { + new BufferImportTypeVisitor(allocator, dummyHandle, fieldNode, new long[] {0, address})) { List buffers = visitor.visit(new ArrowType.Int(32, true)); assertThat(buffers).hasSize(2); assertThat(buffers.get(0)).isNull(); diff --git a/java/c/src/test/java/org/apache/arrow/c/DictionaryTest.java b/java/c/src/test/java/org/apache/arrow/c/DictionaryTest.java index d892781756e..04a28513ae7 100644 --- a/java/c/src/test/java/org/apache/arrow/c/DictionaryTest.java +++ b/java/c/src/test/java/org/apache/arrow/c/DictionaryTest.java @@ -24,11 +24,6 @@ import java.io.IOException; import java.nio.channels.Channels; import java.util.Collections; - -import org.apache.arrow.c.ArrowArray; -import org.apache.arrow.c.ArrowSchema; -import org.apache.arrow.c.CDataDictionaryProvider; -import org.apache.arrow.c.Data; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.util.AutoCloseables; import org.apache.arrow.vector.FieldVector; @@ -75,14 +70,19 @@ void roundtrip(FieldVector vector, DictionaryProvider provider, Class clazz) // Consumer imports vector try (CDataDictionaryProvider cDictionaryProvider = new CDataDictionaryProvider(); - FieldVector imported = Data.importVector(allocator, consumerArrowArray, consumerArrowSchema, - cDictionaryProvider);) { - assertTrue(clazz.isInstance(imported), String.format("expected %s but was %s", clazz, imported.getClass())); - assertTrue(VectorEqualsVisitor.vectorEquals(vector, imported), "vectors are not equivalent"); + FieldVector imported = + Data.importVector( + allocator, consumerArrowArray, consumerArrowSchema, cDictionaryProvider); ) { + assertTrue( + clazz.isInstance(imported), + String.format("expected %s but was %s", clazz, imported.getClass())); + assertTrue( + VectorEqualsVisitor.vectorEquals(vector, imported), "vectors are not equivalent"); for (long id : cDictionaryProvider.getDictionaryIds()) { ValueVector exportedDictionaryVector = provider.lookup(id).getVector(); ValueVector importedDictionaryVector = cDictionaryProvider.lookup(id).getVector(); - assertTrue(VectorEqualsVisitor.vectorEquals(exportedDictionaryVector, importedDictionaryVector), + assertTrue( + VectorEqualsVisitor.vectorEquals(exportedDictionaryVector, importedDictionaryVector), String.format("Dictionary vectors for ID %d are not equivalent", id)); } } @@ -91,7 +91,8 @@ void roundtrip(FieldVector vector, DictionaryProvider provider, Class clazz) @Test public void testWithDictionary() throws Exception { - DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); + DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); // create dictionary and provider final VarCharVector dictVector = new VarCharVector("dict", allocator); dictVector.allocateNewSafe(); @@ -100,7 +101,8 @@ public void testWithDictionary() throws Exception { dictVector.setSafe(2, "cc".getBytes()); dictVector.setValueCount(3); - Dictionary dictionary = new Dictionary(dictVector, new DictionaryEncoding(0L, false, /* indexType= */null)); + Dictionary dictionary = + new Dictionary(dictVector, new DictionaryEncoding(0L, false, /* indexType= */ null)); provider.put(dictionary); // create vector and encode it @@ -129,29 +131,37 @@ public void testRoundtripMultipleBatches() throws IOException { // Load first batch reader.loadNextBatch(); // Producer fills consumer schema structure - Data.exportSchema(allocator, reader.getVectorSchemaRoot().getSchema(), reader, consumerArrowSchema); + Data.exportSchema( + allocator, reader.getVectorSchemaRoot().getSchema(), reader, consumerArrowSchema); // Consumer loads it as an empty vector schema root try (CDataDictionaryProvider consumerDictionaryProvider = new CDataDictionaryProvider(); - VectorSchemaRoot consumerRoot = Data.importVectorSchemaRoot(allocator, consumerArrowSchema, - consumerDictionaryProvider)) { + VectorSchemaRoot consumerRoot = + Data.importVectorSchemaRoot( + allocator, consumerArrowSchema, consumerDictionaryProvider)) { do { try (ArrowArray consumerArray = ArrowArray.allocateNew(allocator)) { // Producer exports next data - Data.exportVectorSchemaRoot(allocator, reader.getVectorSchemaRoot(), reader, consumerArray); + Data.exportVectorSchemaRoot( + allocator, reader.getVectorSchemaRoot(), reader, consumerArray); // Consumer loads next data - Data.importIntoVectorSchemaRoot(allocator, consumerArray, consumerRoot, consumerDictionaryProvider); + Data.importIntoVectorSchemaRoot( + allocator, consumerArray, consumerRoot, consumerDictionaryProvider); // Roundtrip validation - assertTrue(consumerRoot.equals(reader.getVectorSchemaRoot()), "vector schema roots are not equivalent"); + assertTrue( + consumerRoot.equals(reader.getVectorSchemaRoot()), + "vector schema roots are not equivalent"); for (long id : consumerDictionaryProvider.getDictionaryIds()) { ValueVector exportedDictionaryVector = reader.lookup(id).getVector(); - ValueVector importedDictionaryVector = consumerDictionaryProvider.lookup(id).getVector(); - assertTrue(VectorEqualsVisitor.vectorEquals(exportedDictionaryVector, importedDictionaryVector), + ValueVector importedDictionaryVector = + consumerDictionaryProvider.lookup(id).getVector(); + assertTrue( + VectorEqualsVisitor.vectorEquals( + exportedDictionaryVector, importedDictionaryVector), String.format("Dictionary vectors for ID %d are not equivalent", id)); } } - } - while (reader.loadNextBatch()); + } while (reader.loadNextBatch()); } } } @@ -161,7 +171,8 @@ private ArrowStreamReader createMultiBatchReader() throws IOException { try (final VarCharVector dictVector = new VarCharVector("dict", allocator); IntVector vector = new IntVector("foo", allocator)) { // create dictionary and provider - DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); + DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); dictVector.allocateNewSafe(); dictVector.setSafe(0, "aa".getBytes()); dictVector.setSafe(1, "bb".getBytes()); @@ -169,14 +180,16 @@ private ArrowStreamReader createMultiBatchReader() throws IOException { dictVector.setSafe(3, "dd".getBytes()); dictVector.setSafe(4, "ee".getBytes()); dictVector.setValueCount(5); - Dictionary dictionary = new Dictionary(dictVector, new DictionaryEncoding(0L, false, /* indexType= */null)); + Dictionary dictionary = + new Dictionary(dictVector, new DictionaryEncoding(0L, false, /* indexType= */ null)); provider.put(dictionary); Schema schema = new Schema(Collections.singletonList(vector.getField())); - try ( - VectorSchemaRoot root = new VectorSchemaRoot(schema, Collections.singletonList(vector), - vector.getValueCount()); - ArrowStreamWriter writer = new ArrowStreamWriter(root, provider, Channels.newChannel(os));) { + try (VectorSchemaRoot root = + new VectorSchemaRoot( + schema, Collections.singletonList(vector), vector.getValueCount()); + ArrowStreamWriter writer = + new ArrowStreamWriter(root, provider, Channels.newChannel(os)); ) { writer.start(); @@ -215,5 +228,4 @@ private ArrowStreamReader createMultiBatchReader() throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(os.toByteArray()); return new ArrowStreamReader(in, allocator); } - } diff --git a/java/c/src/test/java/org/apache/arrow/c/FlagsTest.java b/java/c/src/test/java/org/apache/arrow/c/FlagsTest.java index 35f836f71f5..31af2dbd456 100644 --- a/java/c/src/test/java/org/apache/arrow/c/FlagsTest.java +++ b/java/c/src/test/java/org/apache/arrow/c/FlagsTest.java @@ -20,8 +20,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.ArrayList; - -import org.apache.arrow.c.Flags; import org.apache.arrow.vector.types.pojo.ArrowType; import org.apache.arrow.vector.types.pojo.DictionaryEncoding; import org.apache.arrow.vector.types.pojo.Field; @@ -31,46 +29,66 @@ public class FlagsTest { @Test public void testForFieldNullableOrderedDict() { - FieldType fieldType = new FieldType(true, ArrowType.Binary.INSTANCE, - new DictionaryEncoding(123L, true, new ArrowType.Int(8, true))); + FieldType fieldType = + new FieldType( + true, + ArrowType.Binary.INSTANCE, + new DictionaryEncoding(123L, true, new ArrowType.Int(8, true))); - assertEquals(Flags.ARROW_FLAG_DICTIONARY_ORDERED | Flags.ARROW_FLAG_NULLABLE, + assertEquals( + Flags.ARROW_FLAG_DICTIONARY_ORDERED | Flags.ARROW_FLAG_NULLABLE, Flags.forField(new Field("Name", fieldType, new ArrayList<>()))); } @Test public void testForFieldOrderedDict() { - FieldType fieldType = new FieldType(false, ArrowType.Binary.INSTANCE, - new DictionaryEncoding(123L, true, new ArrowType.Int(8, true))); - assertEquals(Flags.ARROW_FLAG_DICTIONARY_ORDERED, Flags.forField(new Field("Name", fieldType, new ArrayList<>()))); + FieldType fieldType = + new FieldType( + false, + ArrowType.Binary.INSTANCE, + new DictionaryEncoding(123L, true, new ArrowType.Int(8, true))); + assertEquals( + Flags.ARROW_FLAG_DICTIONARY_ORDERED, + Flags.forField(new Field("Name", fieldType, new ArrayList<>()))); } @Test public void testForFieldNullableDict() { - FieldType fieldType = new FieldType(true, ArrowType.Binary.INSTANCE, - new DictionaryEncoding(123L, false, new ArrowType.Int(8, true))); - assertEquals(Flags.ARROW_FLAG_NULLABLE, Flags.forField(new Field("Name", fieldType, new ArrayList<>()))); + FieldType fieldType = + new FieldType( + true, + ArrowType.Binary.INSTANCE, + new DictionaryEncoding(123L, false, new ArrowType.Int(8, true))); + assertEquals( + Flags.ARROW_FLAG_NULLABLE, Flags.forField(new Field("Name", fieldType, new ArrayList<>()))); } @Test public void testForFieldNullable() { FieldType fieldType = new FieldType(true, ArrowType.Binary.INSTANCE, null); - assertEquals(Flags.ARROW_FLAG_NULLABLE, Flags.forField(new Field("Name", fieldType, new ArrayList<>()))); + assertEquals( + Flags.ARROW_FLAG_NULLABLE, Flags.forField(new Field("Name", fieldType, new ArrayList<>()))); } @Test public void testForFieldNullableOrderedSortedMap() { ArrowType.Map type = new ArrowType.Map(true); - FieldType fieldType = new FieldType(true, type, new DictionaryEncoding(123L, true, new ArrowType.Int(8, true))); - assertEquals(Flags.ARROW_FLAG_DICTIONARY_ORDERED | Flags.ARROW_FLAG_NULLABLE | Flags.ARROW_FLAG_MAP_KEYS_SORTED, + FieldType fieldType = + new FieldType(true, type, new DictionaryEncoding(123L, true, new ArrowType.Int(8, true))); + assertEquals( + Flags.ARROW_FLAG_DICTIONARY_ORDERED + | Flags.ARROW_FLAG_NULLABLE + | Flags.ARROW_FLAG_MAP_KEYS_SORTED, Flags.forField(new Field("Name", fieldType, new ArrayList<>()))); } @Test public void testForFieldNullableOrderedMap() { ArrowType.Map type = new ArrowType.Map(false); - FieldType fieldType = new FieldType(true, type, new DictionaryEncoding(123L, true, new ArrowType.Int(8, true))); - assertEquals(Flags.ARROW_FLAG_DICTIONARY_ORDERED | Flags.ARROW_FLAG_NULLABLE, + FieldType fieldType = + new FieldType(true, type, new DictionaryEncoding(123L, true, new ArrowType.Int(8, true))); + assertEquals( + Flags.ARROW_FLAG_DICTIONARY_ORDERED | Flags.ARROW_FLAG_NULLABLE, Flags.forField(new Field("Name", fieldType, new ArrayList<>()))); } } diff --git a/java/c/src/test/java/org/apache/arrow/c/FormatTest.java b/java/c/src/test/java/org/apache/arrow/c/FormatTest.java index 1f7f86b36f6..1489254ccda 100644 --- a/java/c/src/test/java/org/apache/arrow/c/FormatTest.java +++ b/java/c/src/test/java/org/apache/arrow/c/FormatTest.java @@ -21,8 +21,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import org.apache.arrow.c.Flags; -import org.apache.arrow.c.Format; import org.apache.arrow.vector.types.DateUnit; import org.apache.arrow.vector.types.FloatingPointPrecision; import org.apache.arrow.vector.types.IntervalUnit; @@ -71,22 +69,33 @@ public void testAsString() { assertEquals("ttm", Format.asString(new ArrowType.Time(TimeUnit.MILLISECOND, 32))); assertEquals("ttu", Format.asString(new ArrowType.Time(TimeUnit.MICROSECOND, 64))); assertEquals("ttn", Format.asString(new ArrowType.Time(TimeUnit.NANOSECOND, 64))); - assertEquals("tss:Timezone", Format.asString(new ArrowType.Timestamp(TimeUnit.SECOND, "Timezone"))); - assertEquals("tsm:Timezone", Format.asString(new ArrowType.Timestamp(TimeUnit.MILLISECOND, "Timezone"))); - assertEquals("tsu:Timezone", Format.asString(new ArrowType.Timestamp(TimeUnit.MICROSECOND, "Timezone"))); - assertEquals("tsn:Timezone", Format.asString(new ArrowType.Timestamp(TimeUnit.NANOSECOND, "Timezone"))); - assertEquals("+us:1,1,1", Format.asString(new ArrowType.Union(UnionMode.Sparse, new int[] { 1, 1, 1 }))); - assertEquals("+ud:1,1,1", Format.asString(new ArrowType.Union(UnionMode.Dense, new int[] { 1, 1, 1 }))); + assertEquals( + "tss:Timezone", Format.asString(new ArrowType.Timestamp(TimeUnit.SECOND, "Timezone"))); + assertEquals( + "tsm:Timezone", Format.asString(new ArrowType.Timestamp(TimeUnit.MILLISECOND, "Timezone"))); + assertEquals( + "tsu:Timezone", Format.asString(new ArrowType.Timestamp(TimeUnit.MICROSECOND, "Timezone"))); + assertEquals( + "tsn:Timezone", Format.asString(new ArrowType.Timestamp(TimeUnit.NANOSECOND, "Timezone"))); + assertEquals( + "+us:1,1,1", Format.asString(new ArrowType.Union(UnionMode.Sparse, new int[] {1, 1, 1}))); + assertEquals( + "+ud:1,1,1", Format.asString(new ArrowType.Union(UnionMode.Dense, new int[] {1, 1, 1}))); assertEquals("u", Format.asString(new ArrowType.Utf8())); - assertThrows(UnsupportedOperationException.class, () -> Format.asString(new ArrowType.Int(1, true))); - assertThrows(UnsupportedOperationException.class, () -> Format.asString(new ArrowType.Time(TimeUnit.SECOND, 1))); - assertThrows(UnsupportedOperationException.class, + assertThrows( + UnsupportedOperationException.class, () -> Format.asString(new ArrowType.Int(1, true))); + assertThrows( + UnsupportedOperationException.class, + () -> Format.asString(new ArrowType.Time(TimeUnit.SECOND, 1))); + assertThrows( + UnsupportedOperationException.class, () -> Format.asString(new ArrowType.Time(TimeUnit.MILLISECOND, 64))); } @Test - public void testAsType() throws IllegalStateException, NumberFormatException, UnsupportedOperationException { + public void testAsType() + throws IllegalStateException, NumberFormatException, UnsupportedOperationException { assertTrue(Format.asType("n", 0L) instanceof ArrowType.Null); assertTrue(Format.asType("b", 0L) instanceof ArrowType.Bool); assertEquals(new ArrowType.Int(Byte.SIZE, true), Format.asType("c", 0L)); @@ -98,8 +107,10 @@ public void testAsType() throws IllegalStateException, NumberFormatException, Un assertEquals(new ArrowType.Int(Long.SIZE, true), Format.asType("l", 0L)); assertEquals(new ArrowType.Int(Long.SIZE, false), Format.asType("L", 0L)); assertEquals(new ArrowType.FloatingPoint(FloatingPointPrecision.HALF), Format.asType("e", 0L)); - assertEquals(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE), Format.asType("f", 0L)); - assertEquals(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE), Format.asType("g", 0L)); + assertEquals( + new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE), Format.asType("f", 0L)); + assertEquals( + new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE), Format.asType("g", 0L)); assertTrue(Format.asType("z", 0L) instanceof ArrowType.Binary); assertTrue(Format.asType("Z", 0L) instanceof ArrowType.LargeBinary); assertTrue(Format.asType("u", 0L) instanceof ArrowType.Utf8); @@ -126,12 +137,21 @@ public void testAsType() throws IllegalStateException, NumberFormatException, Un assertEquals(new ArrowType.Decimal(9, 1, 1), Format.asType("d:9,1,1", 0L)); assertEquals(new ArrowType.FixedSizeBinary(1), Format.asType("w:1", 0L)); assertEquals(new ArrowType.FixedSizeList(3), Format.asType("+w:3", 0L)); - assertEquals(new ArrowType.Union(UnionMode.Dense, new int[] { 1, 1, 1 }), Format.asType("+ud:1,1,1", 0L)); - assertEquals(new ArrowType.Union(UnionMode.Sparse, new int[] { 1, 1, 1 }), Format.asType("+us:1,1,1", 0L)); - assertEquals(new ArrowType.Timestamp(TimeUnit.SECOND, "Timezone"), Format.asType("tss:Timezone", 0L)); - assertEquals(new ArrowType.Timestamp(TimeUnit.MILLISECOND, "Timezone"), Format.asType("tsm:Timezone", 0L)); - assertEquals(new ArrowType.Timestamp(TimeUnit.MICROSECOND, "Timezone"), Format.asType("tsu:Timezone", 0L)); - assertEquals(new ArrowType.Timestamp(TimeUnit.NANOSECOND, "Timezone"), Format.asType("tsn:Timezone", 0L)); + assertEquals( + new ArrowType.Union(UnionMode.Dense, new int[] {1, 1, 1}), Format.asType("+ud:1,1,1", 0L)); + assertEquals( + new ArrowType.Union(UnionMode.Sparse, new int[] {1, 1, 1}), Format.asType("+us:1,1,1", 0L)); + assertEquals( + new ArrowType.Timestamp(TimeUnit.SECOND, "Timezone"), Format.asType("tss:Timezone", 0L)); + assertEquals( + new ArrowType.Timestamp(TimeUnit.MILLISECOND, "Timezone"), + Format.asType("tsm:Timezone", 0L)); + assertEquals( + new ArrowType.Timestamp(TimeUnit.MICROSECOND, "Timezone"), + Format.asType("tsu:Timezone", 0L)); + assertEquals( + new ArrowType.Timestamp(TimeUnit.NANOSECOND, "Timezone"), + Format.asType("tsn:Timezone", 0L)); assertThrows(UnsupportedOperationException.class, () -> Format.asType("Format", 0L)); assertThrows(UnsupportedOperationException.class, () -> Format.asType(":", 0L)); diff --git a/java/c/src/test/java/org/apache/arrow/c/MetadataTest.java b/java/c/src/test/java/org/apache/arrow/c/MetadataTest.java index 1d9703b1a87..05bea762d5a 100644 --- a/java/c/src/test/java/org/apache/arrow/c/MetadataTest.java +++ b/java/c/src/test/java/org/apache/arrow/c/MetadataTest.java @@ -23,9 +23,6 @@ import java.nio.ByteOrder; import java.util.HashMap; import java.util.Map; - -import org.apache.arrow.c.Metadata; -import org.apache.arrow.c.NativeUtil; import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.memory.util.LargeMemoryUtil; @@ -48,11 +45,17 @@ static void beforeAll() { metadata.put("key2", "bar"); if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) { - encoded = new byte[] { 2, 0, 0, 0, 4, 0, 0, 0, 'k', 'e', 'y', '1', 0, 0, 0, 0, 4, 0, 0, 0, 'k', 'e', 'y', '2', 3, - 0, 0, 0, 'b', 'a', 'r' }; + encoded = + new byte[] { + 2, 0, 0, 0, 4, 0, 0, 0, 'k', 'e', 'y', '1', 0, 0, 0, 0, 4, 0, 0, 0, 'k', 'e', 'y', '2', + 3, 0, 0, 0, 'b', 'a', 'r' + }; } else { - encoded = new byte[] { 0, 0, 0, 2, 0, 0, 0, 4, 'k', 'e', 'y', '1', 0, 0, 0, 0, 0, 0, 0, 4, 'k', 'e', 'y', '2', 0, - 0, 0, 3, 'b', 'a', 'r' }; + encoded = + new byte[] { + 0, 0, 0, 2, 0, 0, 0, 4, 'k', 'e', 'y', '1', 0, 0, 0, 0, 0, 0, 0, 4, 'k', 'e', 'y', '2', + 0, 0, 0, 3, 'b', 'a', 'r' + }; } } @@ -70,7 +73,8 @@ public void tearDown() { public void testEncode() { try (ArrowBuf buffer = Metadata.encode(allocator, metadata)) { int totalSize = LargeMemoryUtil.checkedCastToInt(buffer.readableBytes()); - ByteBuffer reader = MemoryUtil.directBuffer(buffer.memoryAddress(), totalSize).order(ByteOrder.nativeOrder()); + ByteBuffer reader = + MemoryUtil.directBuffer(buffer.memoryAddress(), totalSize).order(ByteOrder.nativeOrder()); byte[] result = new byte[totalSize]; reader.get(result); assertArrayEquals(encoded, result); @@ -100,5 +104,4 @@ public void testDecodeEmpty() { Map decoded = Metadata.decode(NativeUtil.NULL); assertNull(decoded); } - } diff --git a/java/c/src/test/java/org/apache/arrow/c/NativeUtilTest.java b/java/c/src/test/java/org/apache/arrow/c/NativeUtilTest.java index f46a0128c86..b39c84d5640 100644 --- a/java/c/src/test/java/org/apache/arrow/c/NativeUtilTest.java +++ b/java/c/src/test/java/org/apache/arrow/c/NativeUtilTest.java @@ -22,8 +22,6 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; - -import org.apache.arrow.c.NativeUtil; import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.memory.util.LargeMemoryUtil; @@ -49,10 +47,11 @@ public void tearDown() { @Test public void testString() { String javaString = "abc"; - byte[] nativeString = new byte[] { 97, 98, 99, 0 }; + byte[] nativeString = new byte[] {97, 98, 99, 0}; try (ArrowBuf buffer = NativeUtil.toNativeString(allocator, javaString)) { int totalSize = LargeMemoryUtil.checkedCastToInt(buffer.readableBytes()); - ByteBuffer reader = MemoryUtil.directBuffer(buffer.memoryAddress(), totalSize).order(ByteOrder.nativeOrder()); + ByteBuffer reader = + MemoryUtil.directBuffer(buffer.memoryAddress(), totalSize).order(ByteOrder.nativeOrder()); byte[] result = new byte[totalSize]; reader.get(result); assertArrayEquals(nativeString, result); @@ -63,7 +62,7 @@ public void testString() { @Test public void testToJavaArray() { - long[] nativeArray = new long[] { 1, 2, 3 }; + long[] nativeArray = new long[] {1, 2, 3}; try (ArrowBuf buffer = allocator.buffer(Long.BYTES * nativeArray.length, null)) { for (long value : nativeArray) { buffer.writeLong(value); @@ -78,5 +77,4 @@ public void testToZeroJavaArray() { long[] actual = NativeUtil.toJavaArray(0xDEADBEEF, 0); assertEquals(0, actual.length); } - } diff --git a/java/c/src/test/java/org/apache/arrow/c/RoundtripTest.java b/java/c/src/test/java/org/apache/arrow/c/RoundtripTest.java index fe070400ad9..b069bc59bb6 100644 --- a/java/c/src/test/java/org/apache/arrow/c/RoundtripTest.java +++ b/java/c/src/test/java/org/apache/arrow/c/RoundtripTest.java @@ -34,7 +34,6 @@ import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.Stream; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -134,7 +133,8 @@ FieldVector vectorRoundtrip(FieldVector vector) { } // Consumer imports vector - FieldVector imported = Data.importVector(childAllocator, consumerArrowArray, consumerArrowSchema, null); + FieldVector imported = + Data.importVector(childAllocator, consumerArrowArray, consumerArrowSchema, null); if (!(imported instanceof NullVector)) { assertEquals(childAllocator, imported.getAllocator()); } @@ -165,7 +165,9 @@ VectorSchemaRoot vectorSchemaRootRoundtrip(VectorSchemaRoot root) { boolean roundtrip(FieldVector vector, Class clazz) { try (ValueVector imported = vectorRoundtrip(vector)) { - assertTrue(clazz.isInstance(imported), String.format("expected %s but was %s", clazz, imported.getClass())); + assertTrue( + clazz.isInstance(imported), + String.format("expected %s but was %s", clazz, imported.getClass())); return VectorEqualsVisitor.vectorEquals(vector, imported); } } @@ -292,7 +294,7 @@ public void testZeroVectorEquals() { @Test public void testFixedSizeBinaryVector() { try (final FixedSizeBinaryVector vector = new FixedSizeBinaryVector("v", allocator, 2)) { - setVector(vector, new byte[] { 0b0000, 0b0001 }, new byte[] { 0b0010, 0b0011 }); + setVector(vector, new byte[] {0b0000, 0b0001}, new byte[] {0b0010, 0b0011}); assertTrue(roundtrip(vector, FixedSizeBinaryVector.class)); } } @@ -535,8 +537,11 @@ public void testLargeVarCharVector() { @Test public void testListVector() { try (final ListVector vector = ListVector.empty("v", allocator)) { - setVector(vector, Arrays.stream(new int[] { 1, 2 }).boxed().collect(Collectors.toList()), - Arrays.stream(new int[] { 3, 4 }).boxed().collect(Collectors.toList()), new ArrayList()); + setVector( + vector, + Arrays.stream(new int[] {1, 2}).boxed().collect(Collectors.toList()), + Arrays.stream(new int[] {3, 4}).boxed().collect(Collectors.toList()), + new ArrayList()); assertTrue(roundtrip(vector, ListVector.class)); } } @@ -552,8 +557,11 @@ public void testEmptyListVector() { @Test public void testLargeListVector() { try (final LargeListVector vector = LargeListVector.empty("v", allocator)) { - setVector(vector, Arrays.stream(new int[] { 1, 2 }).boxed().collect(Collectors.toList()), - Arrays.stream(new int[] { 3, 4 }).boxed().collect(Collectors.toList()), new ArrayList()); + setVector( + vector, + Arrays.stream(new int[] {1, 2}).boxed().collect(Collectors.toList()), + Arrays.stream(new int[] {3, 4}).boxed().collect(Collectors.toList()), + new ArrayList()); assertTrue(roundtrip(vector, LargeListVector.class)); } } @@ -561,8 +569,10 @@ public void testLargeListVector() { @Test public void testFixedSizeListVector() { try (final FixedSizeListVector vector = FixedSizeListVector.empty("v", 2, allocator)) { - setVector(vector, Arrays.stream(new int[] { 1, 2 }).boxed().collect(Collectors.toList()), - Arrays.stream(new int[] { 3, 4 }).boxed().collect(Collectors.toList())); + setVector( + vector, + Arrays.stream(new int[] {1, 2}).boxed().collect(Collectors.toList()), + Arrays.stream(new int[] {3, 4}).boxed().collect(Collectors.toList())); assertTrue(roundtrip(vector, FixedSizeListVector.class)); } } @@ -613,8 +623,8 @@ public void testUnionVector() { public void testStructVector() { try (final StructVector vector = StructVector.empty("v", allocator)) { Map> data = new HashMap<>(); - data.put("col_1", Arrays.stream(new int[] { 1, 2 }).boxed().collect(Collectors.toList())); - data.put("col_2", Arrays.stream(new int[] { 3, 4 }).boxed().collect(Collectors.toList())); + data.put("col_1", Arrays.stream(new int[] {1, 2}).boxed().collect(Collectors.toList())); + data.put("col_2", Arrays.stream(new int[] {3, 4}).boxed().collect(Collectors.toList())); setVector(vector, data); assertTrue(roundtrip(vector, StructVector.class)); } @@ -623,7 +633,8 @@ public void testStructVector() { @Test public void testExtensionTypeVector() { ExtensionTypeRegistry.register(new UuidType()); - final Schema schema = new Schema(Collections.singletonList(Field.nullable("a", new UuidType()))); + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("a", new UuidType()))); try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { // Fill with data UUID u1 = UUID.randomUUID(); @@ -642,8 +653,12 @@ public void testExtensionTypeVector() { final Field field = importedRoot.getSchema().getFields().get(0); final UuidType expectedType = new UuidType(); - assertEquals(field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_NAME), expectedType.extensionName()); - assertEquals(field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_METADATA), expectedType.serialize()); + assertEquals( + field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_NAME), + expectedType.extensionName()); + assertEquals( + field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_METADATA), + expectedType.serialize()); final UuidVector deserialized = (UuidVector) importedRoot.getFieldVectors().get(0); assertEquals(vector.getValueCount(), deserialized.getValueCount()); @@ -674,7 +689,8 @@ public void testVectorSchemaRoot() { } } // Consumer imports vector - imported = Data.importVectorSchemaRoot(allocator, consumerArrowArray, consumerArrowSchema, null); + imported = + Data.importVectorSchemaRoot(allocator, consumerArrowArray, consumerArrowSchema, null); } // Ensure that imported VectorSchemaRoot is valid even after C Data Interface @@ -686,7 +702,8 @@ public void testVectorSchemaRoot() { } /** - * Tests exporting Table and importing back to VSR. Importing back to Table is not supported at present. + * Tests exporting Table and importing back to VSR. Importing back to Table is not supported at + * present. */ @Test public void testTable() { @@ -695,8 +712,7 @@ public void testTable() { // Consumer allocates empty structures try (ArrowSchema consumerArrowSchema = ArrowSchema.allocateNew(allocator); ArrowArray consumerArrowArray = ArrowArray.allocateNew(allocator)) { - try ( - VectorSchemaRoot vsr = createTestVSR(); + try (VectorSchemaRoot vsr = createTestVSR(); Table table = new Table(vsr)) { // Producer creates structures from existing memory pointers try (ArrowSchema arrowSchema = ArrowSchema.wrap(consumerArrowSchema.memoryAddress()); @@ -706,7 +722,8 @@ public void testTable() { } } // Consumer imports vector - imported = Data.importVectorSchemaRoot(allocator, consumerArrowArray, consumerArrowSchema, null); + imported = + Data.importVectorSchemaRoot(allocator, consumerArrowArray, consumerArrowSchema, null); } // Ensure that imported VectorSchemaRoot is valid even after C Data Interface @@ -728,10 +745,11 @@ public void testVectorSchemaRootWithDuplicatedFieldNames() { try (VectorSchemaRoot testVSR1 = createTestVSR(); VectorSchemaRoot testVSR2 = createTestVSR()) { // Merge two VSRs to produce duplicated field names - final VectorSchemaRoot vsr = new VectorSchemaRoot( - Stream.concat( - testVSR1.getFieldVectors().stream(), - testVSR2.getFieldVectors().stream()).collect(Collectors.toList())); + final VectorSchemaRoot vsr = + new VectorSchemaRoot( + Stream.concat( + testVSR1.getFieldVectors().stream(), testVSR2.getFieldVectors().stream()) + .collect(Collectors.toList())); // Producer creates structures from existing memory pointers try (ArrowSchema arrowSchema = ArrowSchema.wrap(consumerArrowSchema.memoryAddress()); ArrowArray arrowArray = ArrowArray.wrap(consumerArrowArray.memoryAddress())) { @@ -740,17 +758,19 @@ public void testVectorSchemaRootWithDuplicatedFieldNames() { } } // Consumer imports vector - imported = Data.importVectorSchemaRoot(allocator, consumerArrowArray, consumerArrowSchema, null); + imported = + Data.importVectorSchemaRoot(allocator, consumerArrowArray, consumerArrowSchema, null); } // Ensure that imported VectorSchemaRoot is valid even after C Data Interface // structures are closed try (VectorSchemaRoot testVSR1 = createTestVSR(); VectorSchemaRoot testVSR2 = createTestVSR()) { - final VectorSchemaRoot original = new VectorSchemaRoot( - Stream.concat( - testVSR1.getFieldVectors().stream(), - testVSR2.getFieldVectors().stream()).collect(Collectors.toList())); + final VectorSchemaRoot original = + new VectorSchemaRoot( + Stream.concat( + testVSR1.getFieldVectors().stream(), testVSR2.getFieldVectors().stream()) + .collect(Collectors.toList())); assertTrue(imported.equals(original)); } imported.close(); @@ -758,10 +778,14 @@ public void testVectorSchemaRootWithDuplicatedFieldNames() { @Test public void testSchema() { - Field decimalField = new Field("inner1", FieldType.nullable(new ArrowType.Decimal(19, 4, 128)), null); + Field decimalField = + new Field("inner1", FieldType.nullable(new ArrowType.Decimal(19, 4, 128)), null); Field strField = new Field("inner2", FieldType.nullable(new ArrowType.Utf8()), null); - Field itemField = new Field("col1", FieldType.nullable(new ArrowType.Struct()), - Arrays.asList(decimalField, strField)); + Field itemField = + new Field( + "col1", + FieldType.nullable(new ArrowType.Struct()), + Arrays.asList(decimalField, strField)); Field intField = new Field("col2", FieldType.nullable(new ArrowType.Int(32, true)), null); Schema schema = new Schema(Arrays.asList(itemField, intField)); // Consumer allocates empty ArrowSchema @@ -810,9 +834,12 @@ public void testImportReleasedArray() { consumerArrowArray.markReleased(); // Consumer tried to imports vector but fails - Exception e = assertThrows(IllegalStateException.class, () -> { - Data.importVector(allocator, consumerArrowArray, consumerArrowSchema, null); - }); + Exception e = + assertThrows( + IllegalStateException.class, + () -> { + Data.importVector(allocator, consumerArrowArray, consumerArrowSchema, null); + }); assertEquals("Cannot import released ArrowArray", e.getMessage()); } @@ -861,7 +888,8 @@ public boolean extensionEquals(ExtensionType other) { @Override public ArrowType deserialize(ArrowType storageType, String serializedData) { if (!storageType.equals(storageType())) { - throw new UnsupportedOperationException("Cannot construct UuidType from underlying type " + storageType); + throw new UnsupportedOperationException( + "Cannot construct UuidType from underlying type " + storageType); } return new UuidType(); } @@ -879,7 +907,8 @@ public FieldVector getNewVector(String name, FieldType fieldType, BufferAllocato static class UuidVector extends ExtensionTypeVector { - public UuidVector(String name, BufferAllocator allocator, FixedSizeBinaryVector underlyingVector) { + public UuidVector( + String name, BufferAllocator allocator, FixedSizeBinaryVector underlyingVector) { super(name, allocator, underlyingVector); } diff --git a/java/c/src/test/java/org/apache/arrow/c/StreamTest.java b/java/c/src/test/java/org/apache/arrow/c/StreamTest.java index 68d4fc2a81e..46aefa3af4c 100644 --- a/java/c/src/test/java/org/apache/arrow/c/StreamTest.java +++ b/java/c/src/test/java/org/apache/arrow/c/StreamTest.java @@ -30,7 +30,6 @@ import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.util.AutoCloseables; @@ -70,7 +69,8 @@ public void tearDown() { @Test public void testRoundtripInts() throws Exception { - final Schema schema = new Schema(Collections.singletonList(Field.nullable("ints", new ArrowType.Int(32, true)))); + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("ints", new ArrowType.Int(32, true)))); final List batches = new ArrayList<>(); try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { final IntVector ints = (IntVector) root.getVector(0); @@ -97,8 +97,11 @@ public void testRoundtripInts() throws Exception { @Test public void roundtripStrings() throws Exception { - final Schema schema = new Schema(Arrays.asList(Field.nullable("ints", new ArrowType.Int(32, true)), - Field.nullable("strs", new ArrowType.Utf8()))); + final Schema schema = + new Schema( + Arrays.asList( + Field.nullable("ints", new ArrowType.Int(32, true)), + Field.nullable("strs", new ArrowType.Utf8()))); final List batches = new ArrayList<>(); try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { final IntVector ints = (IntVector) root.getVector(0); @@ -136,11 +139,16 @@ public void roundtripStrings() throws Exception { public void roundtripDictionary() throws Exception { final ArrowType.Int indexType = new ArrowType.Int(32, true); final DictionaryEncoding encoding = new DictionaryEncoding(0L, false, indexType); - final Schema schema = new Schema(Collections.singletonList( - new Field("dict", new FieldType(/*nullable=*/true, indexType, encoding), Collections.emptyList()))); + final Schema schema = + new Schema( + Collections.singletonList( + new Field( + "dict", + new FieldType(/* nullable= */ true, indexType, encoding), + Collections.emptyList()))); final List batches = new ArrayList<>(); try (final CDataDictionaryProvider provider = new CDataDictionaryProvider(); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { final VarCharVector dictionary = new VarCharVector("values", allocator); dictionary.allocateNew(); dictionary.setSafe(0, "foo".getBytes(StandardCharsets.UTF_8)); @@ -173,22 +181,27 @@ public void roundtripDictionary() throws Exception { @Test public void importReleasedStream() { try (final ArrowArrayStream stream = ArrowArrayStream.allocateNew(allocator)) { - Exception e = assertThrows(IllegalStateException.class, () -> Data.importArrayStream(allocator, stream)); + Exception e = + assertThrows( + IllegalStateException.class, () -> Data.importArrayStream(allocator, stream)); assertThat(e).hasMessageContaining("Cannot import released ArrowArrayStream"); } } @Test public void getNextError() throws Exception { - final Schema schema = new Schema(Collections.singletonList(Field.nullable("ints", new ArrowType.Int(32, true)))); + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("ints", new ArrowType.Int(32, true)))); final List batches = new ArrayList<>(); - try (final ArrowReader source = new InMemoryArrowReader(allocator, schema, batches, - new DictionaryProvider.MapDictionaryProvider()) { - @Override - public boolean loadNextBatch() throws IOException { - throw new IOException("Failed to load batch!"); - } - }; final ArrowArrayStream stream = ArrowArrayStream.allocateNew(allocator)) { + try (final ArrowReader source = + new InMemoryArrowReader( + allocator, schema, batches, new DictionaryProvider.MapDictionaryProvider()) { + @Override + public boolean loadNextBatch() throws IOException { + throw new IOException("Failed to load batch!"); + } + }; + final ArrowArrayStream stream = ArrowArrayStream.allocateNew(allocator)) { Data.exportArrayStream(allocator, source, stream); try (final ArrowReader reader = Data.importArrayStream(allocator, stream)) { assertThat(reader.getVectorSchemaRoot().getSchema()).isEqualTo(schema); @@ -201,15 +214,18 @@ public boolean loadNextBatch() throws IOException { @Test public void getSchemaError() throws Exception { - final Schema schema = new Schema(Collections.singletonList(Field.nullable("ints", new ArrowType.Int(32, true)))); + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("ints", new ArrowType.Int(32, true)))); final List batches = new ArrayList<>(); - try (final ArrowReader source = new InMemoryArrowReader(allocator, schema, batches, - new DictionaryProvider.MapDictionaryProvider()) { - @Override - protected Schema readSchema() { - throw new IllegalArgumentException("Failed to read schema!"); - } - }; final ArrowArrayStream stream = ArrowArrayStream.allocateNew(allocator)) { + try (final ArrowReader source = + new InMemoryArrowReader( + allocator, schema, batches, new DictionaryProvider.MapDictionaryProvider()) { + @Override + protected Schema readSchema() { + throw new IllegalArgumentException("Failed to read schema!"); + } + }; + final ArrowArrayStream stream = ArrowArrayStream.allocateNew(allocator)) { Data.exportArrayStream(allocator, source, stream); try (final ArrowReader reader = Data.importArrayStream(allocator, stream)) { final IOException e = assertThrows(IOException.class, reader::getVectorSchemaRoot); @@ -219,11 +235,12 @@ protected Schema readSchema() { } } - void roundtrip(Schema schema, List batches, DictionaryProvider provider) throws Exception { + void roundtrip(Schema schema, List batches, DictionaryProvider provider) + throws Exception { ArrowReader source = new InMemoryArrowReader(allocator, schema, batches, provider); try (final ArrowArrayStream stream = ArrowArrayStream.allocateNew(allocator); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { final VectorLoader loader = new VectorLoader(root); Data.exportArrayStream(allocator, source, stream); @@ -260,24 +277,25 @@ void roundtrip(Schema schema, List batches) throws Exception { private static void assertVectorsEqual(FieldVector expected, FieldVector actual) { assertThat(actual.getField().getType()).isEqualTo(expected.getField().getType()); assertThat(actual.getValueCount()).isEqualTo(expected.getValueCount()); - final Range range = new Range(/*leftStart=*/0, /*rightStart=*/0, expected.getValueCount()); - assertThat(new RangeEqualsVisitor(expected, actual) - .rangeEquals(range)) + final Range range = + new Range(/* leftStart= */ 0, /* rightStart= */ 0, expected.getValueCount()); + assertThat(new RangeEqualsVisitor(expected, actual).rangeEquals(range)) .as("Vectors were not equal.\nExpected: %s\nGot: %s", expected, actual) .isTrue(); } - /** - * An ArrowReader backed by a fixed list of batches. - */ + /** An ArrowReader backed by a fixed list of batches. */ static class InMemoryArrowReader extends ArrowReader { private final Schema schema; private final List batches; private final DictionaryProvider provider; private int nextBatch; - InMemoryArrowReader(BufferAllocator allocator, Schema schema, List batches, - DictionaryProvider provider) { + InMemoryArrowReader( + BufferAllocator allocator, + Schema schema, + List batches, + DictionaryProvider provider) { super(allocator); this.schema = schema; this.batches = batches; @@ -297,7 +315,8 @@ public Set getDictionaryIds() { @Override public Map getDictionaryVectors() { - return getDictionaryIds().stream().collect(Collectors.toMap(Function.identity(), this::lookup)); + return getDictionaryIds().stream() + .collect(Collectors.toMap(Function.identity(), this::lookup)); } @Override diff --git a/java/compression/pom.xml b/java/compression/pom.xml index dea8c778735..860c290a880 100644 --- a/java/compression/pom.xml +++ b/java/compression/pom.xml @@ -1,4 +1,4 @@ - + - diff --git a/java/dataset/pom.xml b/java/dataset/pom.xml index 8723fafa8da..b34a5485593 100644 --- a/java/dataset/pom.xml +++ b/java/dataset/pom.xml @@ -9,216 +9,213 @@ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> - - - arrow-java-root - org.apache.arrow - 16.0.0-SNAPSHOT - - 4.0.0 + + 4.0.0 + + org.apache.arrow + arrow-java-root + 16.0.0-SNAPSHOT + - arrow-dataset - Arrow Java Dataset - Java implementation of Arrow Dataset API/Framework - jar - - ../../../cpp/release-build/ - 2.5.0 - 1.11.0 - 1.11.3 - + arrow-dataset + jar + Arrow Java Dataset + Java implementation of Arrow Dataset API/Framework + + ../../../cpp/release-build/ + 2.5.0 + 1.11.0 + 1.11.3 + - - - org.apache.arrow - arrow-vector - compile - ${arrow.vector.classifier} - - - org.apache.arrow - arrow-memory-core - compile - - - org.apache.arrow - arrow-c-data - compile - - - org.immutables - value - - - org.apache.arrow - arrow-memory-netty - test - - - org.apache.parquet - parquet-avro - ${parquet.version} - test - - - org.apache.avro - avro - ${avro.version} - test - - - org.apache.parquet - parquet-hadoop - ${parquet.version} - test - - - org.apache.arrow - arrow-vector - ${project.version} - test - tests - - - org.apache.hadoop - hadoop-common - ${dep.hadoop.version} - test - - - commons-logging - commons-logging - - - javax.servlet - servlet-api - - - log4j - log4j - - - org.slf4j - slf4j-log4j12 - - - - - com.google.guava - guava - test - - - com.fasterxml.jackson.core - jackson-databind - test - - - org.apache.arrow.orc - arrow-orc - ${project.version} - test - - - org.apache.orc - orc-core - 1.9.2 - test - - - log4j - log4j - - - org.slf4j - slf4j-log4j12 - - - commons-logging - commons-logging - - - - - org.apache.hive - hive-storage-api - 2.8.1 - test - - - commons-io - commons-io - 2.7 - test - - - - - - ${arrow.cpp.build.dir} - - **/*arrow_dataset_jni.* - - - + + + org.apache.arrow + arrow-vector + ${arrow.vector.classifier} + compile + + + org.apache.arrow + arrow-memory-core + compile + + + org.apache.arrow + arrow-c-data + compile + + + org.immutables + value + + + org.apache.arrow + arrow-memory-netty + test + + + org.apache.parquet + parquet-avro + ${parquet.version} + test + + + org.apache.avro + avro + ${avro.version} + test + + + org.apache.parquet + parquet-hadoop + ${parquet.version} + test + + + org.apache.arrow + arrow-vector + ${project.version} + tests + test + + + org.apache.hadoop + hadoop-common + ${dep.hadoop.version} + test + + + commons-logging + commons-logging + + + javax.servlet + servlet-api + + + log4j + log4j + + + org.slf4j + slf4j-log4j12 + + + + + com.google.guava + guava + test + + + com.fasterxml.jackson.core + jackson-databind + test + + + org.apache.arrow.orc + arrow-orc + ${project.version} + test + + + org.apache.orc + orc-core + 1.9.2 + test + + + log4j + log4j + + + org.slf4j + slf4j-log4j12 + + + commons-logging + commons-logging + + + + + org.apache.hive + hive-storage-api + 2.8.1 + test + + + commons-io + commons-io + 2.7 + test + + + + + + ${arrow.cpp.build.dir} + + **/*arrow_dataset_jni.* + + + + + + maven-surefire-plugin + + false + + ${project.basedir}/../../testing/data + + + + + org.xolstice.maven.plugins + protobuf-maven-plugin + 0.6.1 + + com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier} + ../../cpp/src/jni/dataset/proto + + + + + compile + test-compile + + + + + + + + + + jdk11+ + + [11,] + + !m2e.version + + + - - maven-surefire-plugin - - false - - ${project.basedir}/../../testing/data - - - - - org.xolstice.maven.plugins - protobuf-maven-plugin - 0.6.1 - - com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier} - - ../../cpp/src/jni/dataset/proto - - - - - compile - test-compile - - - - + + org.apache.maven.plugins + maven-surefire-plugin + + false + + ${project.basedir}/../../testing/data + + --add-reads=org.apache.arrow.dataset=com.fasterxml.jackson.databind --add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED + + - - - - - jdk11+ - - [11,] - - !m2e.version - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - false - - ${project.basedir}/../../testing/data - - --add-reads=org.apache.arrow.dataset=com.fasterxml.jackson.databind --add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED - - - - - - + + + diff --git a/java/dataset/src/main/java/module-info.java b/java/dataset/src/main/java/module-info.java index 1672d12ffec..3092281a97e 100644 --- a/java/dataset/src/main/java/module-info.java +++ b/java/dataset/src/main/java/module-info.java @@ -1,20 +1,20 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +* Licensed to the Apache Software Foundation (ASF) under one or more +* contributor license agreements. See the NOTICE file distributed with +* this work for additional information regarding copyright ownership. +* The ASF licenses this file to You under the Apache License, Version 2.0 +* (the "License"); you may not use this file except in compliance with +* the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ open module org.apache.arrow.dataset { exports org.apache.arrow.dataset.file; diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/file/DatasetFileWriter.java b/java/dataset/src/main/java/org/apache/arrow/dataset/file/DatasetFileWriter.java index b2369b853ad..9265c5e8b79 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/file/DatasetFileWriter.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/file/DatasetFileWriter.java @@ -36,15 +36,27 @@ public class DatasetFileWriter { * @param uri target file uri * @param maxPartitions maximum partitions to be included in written files * @param partitionColumns columns used to partition output files. Empty to disable partitioning - * @param baseNameTemplate file name template used to make partitions. E.g. "dat_{i}", i is current partition - * ID around all written files. + * @param baseNameTemplate file name template used to make partitions. E.g. "dat_{i}", i is + * current partition ID around all written files. */ - public static void write(BufferAllocator allocator, ArrowReader reader, FileFormat format, String uri, - String[] partitionColumns, int maxPartitions, String baseNameTemplate) { + public static void write( + BufferAllocator allocator, + ArrowReader reader, + FileFormat format, + String uri, + String[] partitionColumns, + int maxPartitions, + String baseNameTemplate) { try (final ArrowArrayStream stream = ArrowArrayStream.allocateNew(allocator)) { Data.exportArrayStream(allocator, reader, stream); - JniWrapper.get().writeFromScannerToFile(stream.memoryAddress(), - format.id(), uri, partitionColumns, maxPartitions, baseNameTemplate); + JniWrapper.get() + .writeFromScannerToFile( + stream.memoryAddress(), + format.id(), + uri, + partitionColumns, + maxPartitions, + baseNameTemplate); } } @@ -55,7 +67,8 @@ public static void write(BufferAllocator allocator, ArrowReader reader, FileForm * @param format target file format * @param uri target file uri */ - public static void write(BufferAllocator allocator, ArrowReader reader, FileFormat format, String uri) { + public static void write( + BufferAllocator allocator, ArrowReader reader, FileFormat format, String uri) { write(allocator, reader, format, uri, new String[0], 1024, "data_{i}"); } } diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/file/FileFormat.java b/java/dataset/src/main/java/org/apache/arrow/dataset/file/FileFormat.java index 70a1faab79e..64cfeeb3903 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/file/FileFormat.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/file/FileFormat.java @@ -17,9 +17,7 @@ package org.apache.arrow.dataset.file; -/** - * File format definitions. - */ +/** File format definitions. */ public enum FileFormat { PARQUET(0), ARROW_IPC(1), diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/file/FileSystemDatasetFactory.java b/java/dataset/src/main/java/org/apache/arrow/dataset/file/FileSystemDatasetFactory.java index aa315690592..a2fe8daf974 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/file/FileSystemDatasetFactory.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/file/FileSystemDatasetFactory.java @@ -21,18 +21,16 @@ import org.apache.arrow.dataset.jni.NativeMemoryPool; import org.apache.arrow.memory.BufferAllocator; -/** - * Java binding of the C++ FileSystemDatasetFactory. - */ +/** Java binding of the C++ FileSystemDatasetFactory. */ public class FileSystemDatasetFactory extends NativeDatasetFactory { - public FileSystemDatasetFactory(BufferAllocator allocator, NativeMemoryPool memoryPool, FileFormat format, - String uri) { + public FileSystemDatasetFactory( + BufferAllocator allocator, NativeMemoryPool memoryPool, FileFormat format, String uri) { super(allocator, memoryPool, createNative(format, uri)); } - public FileSystemDatasetFactory(BufferAllocator allocator, NativeMemoryPool memoryPool, FileFormat format, - String[] uris) { + public FileSystemDatasetFactory( + BufferAllocator allocator, NativeMemoryPool memoryPool, FileFormat format, String[] uris) { super(allocator, memoryPool, createNative(format, uris)); } @@ -43,5 +41,4 @@ private static long createNative(FileFormat format, String uri) { private static long createNative(FileFormat format, String[] uris) { return JniWrapper.get().makeFileSystemDatasetFactory(uris, format.id()); } - } diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/file/JniWrapper.java b/java/dataset/src/main/java/org/apache/arrow/dataset/file/JniWrapper.java index c3a1a4e58a1..3a7756cc434 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/file/JniWrapper.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/file/JniWrapper.java @@ -31,8 +31,7 @@ public static JniWrapper get() { return INSTANCE; } - private JniWrapper() { - } + private JniWrapper() {} /** * Create FileSystemDatasetFactory and return its native pointer. The pointer is pointing to a @@ -65,14 +64,14 @@ private JniWrapper() { * @param uri target file uri * @param partitionColumns columns used to partition output files * @param maxPartitions maximum partitions to be included in written files - * @param baseNameTemplate file name template used to make partitions. E.g. "dat_{i}", i is current partition - * ID around all written files. + * @param baseNameTemplate file name template used to make partitions. E.g. "dat_{i}", i is + * current partition ID around all written files. */ - public native void writeFromScannerToFile(long streamAddress, - long fileFormat, - String uri, - String[] partitionColumns, - int maxPartitions, - String baseNameTemplate); - + public native void writeFromScannerToFile( + long streamAddress, + long fileFormat, + String uri, + String[] partitionColumns, + int maxPartitions, + String baseNameTemplate); } diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/DirectReservationListener.java b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/DirectReservationListener.java index 3922e90335d..7792ad458da 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/DirectReservationListener.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/DirectReservationListener.java @@ -20,12 +20,11 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.concurrent.atomic.AtomicLong; - import org.apache.arrow.util.VisibleForTesting; /** - * Reserving Java direct memory bytes from java.nio.Bits. Used by Java Dataset API's C++ memory - * pool implementation. This makes memory allocated by the pool to be controlled by JVM option + * Reserving Java direct memory bytes from java.nio.Bits. Used by Java Dataset API's C++ memory pool + * implementation. This makes memory allocated by the pool to be controlled by JVM option * "-XX:MaxDirectMemorySize". */ public class DirectReservationListener implements ReservationListener { @@ -40,10 +39,12 @@ private DirectReservationListener() { methodUnreserve = this.getDeclaredMethodBaseOnJDKVersion(classBits, "unreserveMemory"); methodUnreserve.setAccessible(true); } catch (Exception e) { - final RuntimeException failure = new RuntimeException( - "Failed to initialize DirectReservationListener. When starting Java you must include " + - "`--add-opens=java.base/java.nio=org.apache.arrow.dataset,org.apache.arrow.memory.core,ALL-UNNAMED` " + - "(See https://arrow.apache.org/docs/java/install.html)", e); + final RuntimeException failure = + new RuntimeException( + "Failed to initialize DirectReservationListener. When starting Java you must include " + + "`--add-opens=java.base/java.nio=org.apache.arrow.dataset,org.apache.arrow.memory.core,ALL-UNNAMED` " + + "(See https://arrow.apache.org/docs/java/install.html)", + e); failure.printStackTrace(); throw failure; } @@ -55,14 +56,13 @@ public static DirectReservationListener instance() { return INSTANCE; } - /** - * Reserve bytes by invoking java.nio.java.Bitjava.nio.Bitss#reserveMemory. - */ + /** Reserve bytes by invoking java.nio.java.Bitjava.nio.Bitss#reserveMemory. */ @Override public void reserve(long size) { try { if (size > Integer.MAX_VALUE) { - throw new IllegalArgumentException("reserve size should not be larger than Integer.MAX_VALUE (0x7fffffff)"); + throw new IllegalArgumentException( + "reserve size should not be larger than Integer.MAX_VALUE (0x7fffffff)"); } methodReserve.invoke(null, (int) size, (int) size); } catch (Exception e) { @@ -70,14 +70,13 @@ public void reserve(long size) { } } - /** - * Unreserve bytes by invoking java.nio.java.Bitjava.nio.Bitss#unreserveMemory. - */ + /** Unreserve bytes by invoking java.nio.java.Bitjava.nio.Bitss#unreserveMemory. */ @Override public void unreserve(long size) { try { if (size > Integer.MAX_VALUE) { - throw new IllegalArgumentException("unreserve size should not be larger than Integer.MAX_VALUE (0x7fffffff)"); + throw new IllegalArgumentException( + "unreserve size should not be larger than Integer.MAX_VALUE (0x7fffffff)"); } methodUnreserve.invoke(null, (int) size, (int) size); } catch (Exception e) { @@ -85,9 +84,7 @@ public void unreserve(long size) { } } - /** - * Get current reservation of jVM direct memory. Visible for testing. - */ + /** Get current reservation of jVM direct memory. Visible for testing. */ @VisibleForTesting public long getCurrentDirectMemReservation() { try { @@ -110,7 +107,9 @@ public long getCurrentDirectMemReservation() { } /** - * Get the given method via reflection, searching for different signatures based on the Java version. + * Get the given method via reflection, searching for different signatures based on the Java + * version. + * * @param classBits The java.nio.Bits class. * @param name The method being requested. * @return The method object. diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/JniExceptionDescriber.java b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/JniExceptionDescriber.java index a14e35b7a83..6a20daab0eb 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/JniExceptionDescriber.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/JniExceptionDescriber.java @@ -20,16 +20,13 @@ import java.io.PrintWriter; import java.io.StringWriter; -/** - * For native code to invoke to convert a java/lang/Throwable to jstring. - */ +/** For native code to invoke to convert a java/lang/Throwable to jstring. */ class JniExceptionDescriber { - private JniExceptionDescriber() { - } + private JniExceptionDescriber() {} /** - * Convert a java/lang/Throwable to jstring. See codes in arrow::dataset::jni::CheckException - * for more details. + * Convert a java/lang/Throwable to jstring. See codes in arrow::dataset::jni::CheckException for + * more details. * * @param throwable the exception instance. * @return a String including error message and stack trace of the exception. diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/JniLoader.java b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/JniLoader.java index cf2f8fe29e8..62cb540e23d 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/JniLoader.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/JniLoader.java @@ -30,12 +30,11 @@ import java.util.Locale; import java.util.Set; -/** - * The JniLoader for Dataset API's native implementation. - */ +/** The JniLoader for Dataset API's native implementation. */ public final class JniLoader { - private static final JniLoader INSTANCE = new JniLoader(Collections.singletonList("arrow_dataset_jni")); + private static final JniLoader INSTANCE = + new JniLoader(Collections.singletonList("arrow_dataset_jni")); public static JniLoader get() { return INSTANCE; @@ -51,9 +50,7 @@ private boolean finished() { return librariesToLoad.isEmpty(); } - /** - * If required JNI libraries are not loaded, then load them. - */ + /** If required JNI libraries are not loaded, then load them. */ public void ensureLoaded() { if (finished()) { return; @@ -65,7 +62,8 @@ public void ensureLoaded() { private synchronized void loadRemaining() { // The method is protected by a mutex via synchronized, if more than one thread race to call // loadRemaining, at same time only one will do the actual loading and the others will wait for - // the mutex to be acquired then check on the remaining list: if there are libraries that were not + // the mutex to be acquired then check on the remaining list: if there are libraries that were + // not // successfully loaded then the mutex owner will try to load them again. if (finished()) { return; @@ -81,10 +79,11 @@ private void load(String name) { final String libraryToLoad = name + "/" + getNormalizedArch() + "/" + System.mapLibraryName(name); try { - File temp = File.createTempFile("jnilib-", ".tmp", new File(System.getProperty("java.io.tmpdir"))); + File temp = + File.createTempFile("jnilib-", ".tmp", new File(System.getProperty("java.io.tmpdir"))); temp.deleteOnExit(); - try (final InputStream is - = JniWrapper.class.getClassLoader().getResourceAsStream(libraryToLoad)) { + try (final InputStream is = + JniWrapper.class.getClassLoader().getResourceAsStream(libraryToLoad)) { if (is == null) { throw new FileNotFoundException(libraryToLoad); } @@ -112,6 +111,11 @@ private String getNormalizedArch() { } private void ensureS3FinalizedOnShutdown() { - Runtime.getRuntime().addShutdownHook(new Thread(() -> { JniWrapper.get().ensureS3Finalized(); })); + Runtime.getRuntime() + .addShutdownHook( + new Thread( + () -> { + JniWrapper.get().ensureS3Finalized(); + })); } } diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/JniWrapper.java b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/JniWrapper.java index 637a3e8f22a..791d88a68e9 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/JniWrapper.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/JniWrapper.java @@ -19,9 +19,7 @@ import java.nio.ByteBuffer; -/** - * JNI wrapper for Dataset API's native implementation. - */ +/** JNI wrapper for Dataset API's native implementation. */ public class JniWrapper { private static final JniWrapper INSTANCE = new JniWrapper(); @@ -31,8 +29,7 @@ public static JniWrapper get() { return INSTANCE; } - private JniWrapper() { - } + private JniWrapper() {} /** * Release the DatasetFactory by destroying its reference held by JNI wrapper. @@ -70,17 +67,21 @@ private JniWrapper() { * Create Scanner from a Dataset and get the native pointer of the Dataset. * * @param datasetId the native pointer of the arrow::dataset::Dataset instance. - * @param columns desired column names. - * Columns not in this list will not be emitted when performing scan operation. Null equals - * to "all columns". + * @param columns desired column names. Columns not in this list will not be emitted when + * performing scan operation. Null equals to "all columns". * @param substraitProjection substrait extended expression to evaluate for project new columns * @param substraitFilter substrait extended expression to evaluate for apply filter * @param batchSize batch size of scanned record batches. * @param memoryPool identifier of memory pool used in the native scanner. * @return the native pointer of the arrow::dataset::Scanner instance. */ - public native long createScanner(long datasetId, String[] columns, ByteBuffer substraitProjection, - ByteBuffer substraitFilter, long batchSize, long memoryPool); + public native long createScanner( + long datasetId, + String[] columns, + ByteBuffer substraitProjection, + ByteBuffer substraitFilter, + long batchSize, + long memoryPool); /** * Get a serialized schema from native instance of a Scanner. @@ -102,8 +103,8 @@ public native long createScanner(long datasetId, String[] columns, ByteBuffer su * Read next record batch from the specified scanner. * * @param scannerId the native pointer of the arrow::dataset::Scanner instance. - * @param arrowArray pointer to an empty {@link org.apache.arrow.c.ArrowArray} struct to - * store C++ side record batch that conforms to C data interface. + * @param arrowArray pointer to an empty {@link org.apache.arrow.c.ArrowArray} struct to store C++ + * side record batch that conforms to C data interface. * @return true if valid record batch is returned; false if stream ended. */ public native boolean nextRecordBatch(long scannerId, long arrowArray); @@ -116,8 +117,8 @@ public native long createScanner(long datasetId, String[] columns, ByteBuffer su public native void releaseBuffer(long bufferId); /** - * Ensure the S3 APIs are shutdown, but only if not already done. If the S3 APIs are uninitialized, - * then this is a noop. + * Ensure the S3 APIs are shutdown, but only if not already done. If the S3 APIs are + * uninitialized, then this is a noop. */ public native void ensureS3Finalized(); } diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeContext.java b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeContext.java index 7f6dfbc028f..18e88b5aeb8 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeContext.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeContext.java @@ -19,9 +19,7 @@ import org.apache.arrow.memory.BufferAllocator; -/** - * Context for relevant classes of NativeDataset. - */ +/** Context for relevant classes of NativeDataset. */ public class NativeContext { private final BufferAllocator allocator; private final NativeMemoryPool memoryPool; @@ -37,16 +35,12 @@ public NativeContext(BufferAllocator allocator, NativeMemoryPool memoryPool) { this.memoryPool = memoryPool; } - /** - * Returns the allocator which is in use. - */ + /** Returns the allocator which is in use. */ public BufferAllocator getAllocator() { return allocator; } - /** - * Returns the native memory pool. - */ + /** Returns the native memory pool. */ public NativeMemoryPool getMemoryPool() { return memoryPool; } diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeDataset.java b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeDataset.java index d9abad9971c..92bfb1e6291 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeDataset.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeDataset.java @@ -20,9 +20,7 @@ import org.apache.arrow.dataset.scanner.ScanOptions; import org.apache.arrow.dataset.source.Dataset; -/** - * Native implementation of {@link Dataset}. - */ +/** Native implementation of {@link Dataset}. */ public class NativeDataset implements Dataset { private final NativeContext context; @@ -41,10 +39,15 @@ public synchronized NativeScanner newScan(ScanOptions options) { throw new NativeInstanceReleasedException(); } - long scannerId = JniWrapper.get().createScanner(datasetId, options.getColumns().orElse(null), - options.getSubstraitProjection().orElse(null), - options.getSubstraitFilter().orElse(null), - options.getBatchSize(), context.getMemoryPool().getNativeInstanceId()); + long scannerId = + JniWrapper.get() + .createScanner( + datasetId, + options.getColumns().orElse(null), + options.getSubstraitProjection().orElse(null), + options.getSubstraitFilter().orElse(null), + options.getBatchSize(), + context.getMemoryPool().getNativeInstanceId()); return new NativeScanner(context, scannerId); } diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeDatasetFactory.java b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeDatasetFactory.java index 993d44fa26f..9130eb64765 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeDatasetFactory.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeDatasetFactory.java @@ -18,15 +18,12 @@ package org.apache.arrow.dataset.jni; import java.io.IOException; - import org.apache.arrow.dataset.source.DatasetFactory; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.types.pojo.Schema; import org.apache.arrow.vector.util.SchemaUtility; -/** - * Native implementation of {@link DatasetFactory}. - */ +/** Native implementation of {@link DatasetFactory}. */ public class NativeDatasetFactory implements DatasetFactory { private final long datasetFactoryId; private final NativeMemoryPool memoryPool; @@ -37,17 +34,19 @@ public class NativeDatasetFactory implements DatasetFactory { /** * Constructor. * - * @param allocator a context allocator associated with this factory. Any buffer that will be created natively will - * be then bound to this allocator. - * @param memoryPool the native memory pool associated with this factory. Any buffer created natively should request - * for memory spaces from this memory pool. This is a mapped instance of c++ arrow::MemoryPool. - * @param datasetFactoryId an ID, at the same time the native pointer of the underlying native instance of this - * factory. Make sure in c++ side the pointer is pointing to the shared pointer wrapping - * the actual instance so we could successfully decrease the reference count once - * {@link #close} is called. + * @param allocator a context allocator associated with this factory. Any buffer that will be + * created natively will be then bound to this allocator. + * @param memoryPool the native memory pool associated with this factory. Any buffer created + * natively should request for memory spaces from this memory pool. This is a mapped instance + * of c++ arrow::MemoryPool. + * @param datasetFactoryId an ID, at the same time the native pointer of the underlying native + * instance of this factory. Make sure in c++ side the pointer is pointing to the shared + * pointer wrapping the actual instance so we could successfully decrease the reference count + * once {@link #close} is called. * @see #close() */ - public NativeDatasetFactory(BufferAllocator allocator, NativeMemoryPool memoryPool, long datasetFactoryId) { + public NativeDatasetFactory( + BufferAllocator allocator, NativeMemoryPool memoryPool, long datasetFactoryId) { this.allocator = allocator; this.memoryPool = memoryPool; this.datasetFactoryId = datasetFactoryId; @@ -82,7 +81,8 @@ public NativeDataset finish(Schema schema) { if (closed) { throw new NativeInstanceReleasedException(); } - return new NativeDataset(new NativeContext(allocator, memoryPool), + return new NativeDataset( + new NativeContext(allocator, memoryPool), JniWrapper.get().createDataset(datasetFactoryId, serialized)); } } catch (IOException e) { @@ -90,9 +90,7 @@ public NativeDataset finish(Schema schema) { } } - /** - * Close this factory by release the pointer of the native instance. - */ + /** Close this factory by release the pointer of the native instance. */ @Override public synchronized void close() { if (closed) { diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeInstanceReleasedException.java b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeInstanceReleasedException.java index 3231ca23abe..629ec898b4d 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeInstanceReleasedException.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeInstanceReleasedException.java @@ -17,9 +17,7 @@ package org.apache.arrow.dataset.jni; -/** - * Thrown if trying to operate on a native instance that is already released. - */ +/** Thrown if trying to operate on a native instance that is already released. */ public class NativeInstanceReleasedException extends RuntimeException { public NativeInstanceReleasedException() { super("Native instance has been released"); diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeMemoryPool.java b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeMemoryPool.java index e20e1e351c5..3339c4c9067 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeMemoryPool.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeMemoryPool.java @@ -17,9 +17,7 @@ package org.apache.arrow.dataset.jni; -/** - * C++ memory pool(arrow::MemoryPool)'s Java mapped instance. - */ +/** C++ memory pool(arrow::MemoryPool)'s Java mapped instance. */ public class NativeMemoryPool implements AutoCloseable { private final long nativeInstanceId; @@ -27,34 +25,28 @@ private NativeMemoryPool(long nativeInstanceId) { this.nativeInstanceId = nativeInstanceId; } - /** - * Get the default memory pool. This will return arrow::default_memory_pool() directly. - */ + /** Get the default memory pool. This will return arrow::default_memory_pool() directly. */ public static NativeMemoryPool getDefault() { JniLoader.get().ensureLoaded(); return new NativeMemoryPool(getDefaultMemoryPool()); } /** - * Create a listenable memory pool (see also: arrow::ReservationListenableMemoryPool) with - * a specific listener. All buffers created from the memory pool should take enough reservation - * from the listener in advance. + * Create a listenable memory pool (see also: arrow::ReservationListenableMemoryPool) with a + * specific listener. All buffers created from the memory pool should take enough reservation from + * the listener in advance. */ public static NativeMemoryPool createListenable(ReservationListener listener) { JniLoader.get().ensureLoaded(); return new NativeMemoryPool(createListenableMemoryPool(listener)); } - /** - * Return native instance ID of this memory pool. - */ + /** Return native instance ID of this memory pool. */ public long getNativeInstanceId() { return nativeInstanceId; } - /** - * Get current allocated bytes. - */ + /** Get current allocated bytes. */ public long getBytesAllocated() { return bytesAllocated(nativeInstanceId); } diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeScanTask.java b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeScanTask.java index 7747dd60340..cd17348fb0d 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeScanTask.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeScanTask.java @@ -21,17 +21,16 @@ import org.apache.arrow.vector.ipc.ArrowReader; /** - * Native implementation of {@link ScanTask}. Currently RecordBatches are iterated directly by the scanner - * id via {@link JniWrapper}, thus we allow only one-time execution of method {@link #execute()}. If a re-scan - * operation is expected, call {@link NativeDataset#newScan} to create a new scanner instance. + * Native implementation of {@link ScanTask}. Currently RecordBatches are iterated directly by the + * scanner id via {@link JniWrapper}, thus we allow only one-time execution of method {@link + * #execute()}. If a re-scan operation is expected, call {@link NativeDataset#newScan} to create a + * new scanner instance. */ @Deprecated public class NativeScanTask implements ScanTask { private final NativeScanner scanner; - /** - * Constructor. - */ + /** Constructor. */ public NativeScanTask(NativeScanner scanner) { this.scanner = scanner; } diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeScanner.java b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeScanner.java index 8ca8e5cf50e..574ea075a5f 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeScanner.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/NativeScanner.java @@ -23,7 +23,6 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; - import org.apache.arrow.c.ArrowArray; import org.apache.arrow.c.Data; import org.apache.arrow.dataset.scanner.Scanner; @@ -36,9 +35,9 @@ import org.apache.arrow.vector.util.SchemaUtility; /** - * Native implementation of {@link Scanner}. Note that it currently emits only a single scan task of type - * {@link NativeScanTask}, which is internally a combination of all scan task instances returned by the - * native scanner. + * Native implementation of {@link Scanner}. Note that it currently emits only a single scan task of + * type {@link NativeScanTask}, which is internally a combination of all scan task instances + * returned by the native scanner. */ public class NativeScanner implements Scanner { @@ -61,8 +60,9 @@ ArrowReader execute() { throw new NativeInstanceReleasedException(); } if (!executed.compareAndSet(false, true)) { - throw new UnsupportedOperationException("NativeScanner cannot be executed more than once. Consider creating " + - "new scanner instead"); + throw new UnsupportedOperationException( + "NativeScanner cannot be executed more than once. Consider creating " + + "new scanner instead"); } return new NativeReader(context.getAllocator()); } @@ -73,8 +73,8 @@ public ArrowReader scanBatches() { throw new NativeInstanceReleasedException(); } if (!executed.compareAndSet(false, true)) { - throw new UnsupportedOperationException("NativeScanner can only be executed once. Create a " + - "new scanner instead"); + throw new UnsupportedOperationException( + "NativeScanner can only be executed once. Create a " + "new scanner instead"); } return new NativeReader(context.getAllocator()); } @@ -95,7 +95,8 @@ public Schema schema() { if (closed) { throw new NativeInstanceReleasedException(); } - return SchemaUtility.deserialize(JniWrapper.get().getSchemaFromScanner(scannerId), context.getAllocator()); + return SchemaUtility.deserialize( + JniWrapper.get().getSchemaFromScanner(scannerId), context.getAllocator()); } catch (IOException e) { throw new RuntimeException(e); } finally { @@ -117,9 +118,7 @@ public void close() { } } - /** - * {@link ArrowReader} implementation for NativeDataset. - */ + /** {@link ArrowReader} implementation for NativeDataset. */ public class NativeReader extends ArrowReader { private NativeReader(BufferAllocator allocator) { diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/ReservationListener.java b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/ReservationListener.java index f1ffdd2acbd..858bba2020d 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/jni/ReservationListener.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/jni/ReservationListener.java @@ -17,9 +17,7 @@ package org.apache.arrow.dataset.jni; -/** - * Listener of buffer memory reservation. Used by native datasets. - */ +/** Listener of buffer memory reservation. Used by native datasets. */ public interface ReservationListener { /** @@ -29,8 +27,6 @@ public interface ReservationListener { */ void reserve(long size); - /** - * Unreserve bytes. - */ + /** Unreserve bytes. */ void unreserve(long size); } diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/ArrowScannerReader.java b/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/ArrowScannerReader.java index 417ba837a3b..e5853500c56 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/ArrowScannerReader.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/ArrowScannerReader.java @@ -19,7 +19,6 @@ import java.io.IOException; import java.util.Iterator; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.VectorLoader; import org.apache.arrow.vector.VectorUnloader; @@ -28,10 +27,7 @@ import org.apache.arrow.vector.ipc.message.ArrowRecordBatch; import org.apache.arrow.vector.types.pojo.Schema; -/** - * An implementation of {@link ArrowReader} that reads - * the dataset from {@link Scanner}. - */ +/** An implementation of {@link ArrowReader} that reads the dataset from {@link Scanner}. */ public class ArrowScannerReader extends ArrowReader { private final Scanner scanner; @@ -93,8 +89,7 @@ public boolean loadNextBatch() throws IOException { } VectorLoader loader = new VectorLoader(this.getVectorSchemaRoot()); - VectorUnloader unloader = - new VectorUnloader(currentReader.getVectorSchemaRoot()); + VectorUnloader unloader = new VectorUnloader(currentReader.getVectorSchemaRoot()); try (ArrowRecordBatch recordBatch = unloader.getRecordBatch()) { loader.load(recordBatch); } diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/ScanOptions.java b/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/ScanOptions.java index 995d05ac3b3..1579771e863 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/ScanOptions.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/ScanOptions.java @@ -19,12 +19,9 @@ import java.nio.ByteBuffer; import java.util.Optional; - import org.apache.arrow.util.Preconditions; -/** - * Options used during scanning. - */ +/** Options used during scanning. */ public class ScanOptions { private final long batchSize; private final Optional columns; @@ -33,27 +30,35 @@ public class ScanOptions { /** * Constructor. - * @param columns Projected columns. Empty for scanning all columns. - * @param batchSize Maximum row number of each returned {@link org.apache.arrow.vector.ipc.message.ArrowRecordBatch} * + * @param columns Projected columns. Empty for scanning all columns. + * @param batchSize Maximum row number of each returned {@link + * org.apache.arrow.vector.ipc.message.ArrowRecordBatch} * @deprecated Deprecated. Use {@link #ScanOptions(long, Optional)} instead. */ @Deprecated public ScanOptions(String[] columns, long batchSize) { - this(batchSize, Optional.of(columns).map(present -> { - if (present.length == 0) { - // Backwards compatibility: See ARROW-13257, in the new constructor, we now use null to scan for all columns. - return null; - } - return present; - })); + this( + batchSize, + Optional.of(columns) + .map( + present -> { + if (present.length == 0) { + // Backwards compatibility: See ARROW-13257, in the new constructor, we now use + // null to scan for all columns. + return null; + } + return present; + })); } /** * Constructor. - * @param batchSize Maximum row number of each returned {@link org.apache.arrow.vector.ipc.message.ArrowRecordBatch} - * @param columns (Optional) Projected columns. {@link Optional#empty()} for scanning all columns. Otherwise, - * Only columns present in the Array will be scanned. + * + * @param batchSize Maximum row number of each returned {@link + * org.apache.arrow.vector.ipc.message.ArrowRecordBatch} + * @param columns (Optional) Projected columns. {@link Optional#empty()} for scanning all columns. + * Otherwise, Only columns present in the Array will be scanned. */ public ScanOptions(long batchSize, Optional columns) { Preconditions.checkNotNull(columns); @@ -83,9 +88,7 @@ public Optional getSubstraitFilter() { return substraitFilter; } - /** - * Builder for Options used during scanning. - */ + /** Builder for Options used during scanning. */ public static class Builder { private final long batchSize; private Optional columns; @@ -94,7 +97,9 @@ public static class Builder { /** * Constructor. - * @param batchSize Maximum row number of each returned {@link org.apache.arrow.vector.ipc.message.ArrowRecordBatch} + * + * @param batchSize Maximum row number of each returned {@link + * org.apache.arrow.vector.ipc.message.ArrowRecordBatch} */ public Builder(long batchSize) { this.batchSize = batchSize; diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/ScanTask.java b/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/ScanTask.java index 16b8aeefb61..5a6058cb6c4 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/ScanTask.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/ScanTask.java @@ -18,19 +18,15 @@ package org.apache.arrow.dataset.scanner; import java.io.Reader; - import org.apache.arrow.vector.ipc.ArrowReader; /** - * Read record batches from a range of a single data fragment. A - * ScanTask is meant to be a unit of work to be dispatched. The implementation - * must be thread and concurrent safe. + * Read record batches from a range of a single data fragment. A ScanTask is meant to be a unit of + * work to be dispatched. The implementation must be thread and concurrent safe. */ @Deprecated public interface ScanTask extends AutoCloseable { - /** - * Execute this ScanTask and return a {@link Reader} instance. - */ + /** Execute this ScanTask and return a {@link Reader} instance. */ ArrowReader execute(); } diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/Scanner.java b/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/Scanner.java index 43749b7db8e..c066d266cf4 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/Scanner.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/scanner/Scanner.java @@ -20,9 +20,7 @@ import org.apache.arrow.vector.ipc.ArrowReader; import org.apache.arrow.vector.types.pojo.Schema; -/** - * A high level interface for scanning data over dataset. - */ +/** A high level interface for scanning data over dataset. */ public interface Scanner extends AutoCloseable { /** @@ -35,8 +33,8 @@ public interface Scanner extends AutoCloseable { /** * Perform the scan operation. * - * @return a iterable set of {@link ScanTask}s. Each task is considered independent and it is allowed - * to execute the tasks concurrently to gain better performance. + * @return a iterable set of {@link ScanTask}s. Each task is considered independent and it is + * allowed to execute the tasks concurrently to gain better performance. * @deprecated use {@link #scanBatches()} instead. */ @Deprecated diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/source/Dataset.java b/java/dataset/src/main/java/org/apache/arrow/dataset/source/Dataset.java index ce193581f40..da5b721ac8b 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/source/Dataset.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/source/Dataset.java @@ -20,9 +20,7 @@ import org.apache.arrow.dataset.scanner.ScanOptions; import org.apache.arrow.dataset.scanner.Scanner; -/** - * A container of Fragments which are the internal iterable unit of read data. - */ +/** A container of Fragments which are the internal iterable unit of read data. */ public interface Dataset extends AutoCloseable { /** diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/source/DatasetFactory.java b/java/dataset/src/main/java/org/apache/arrow/dataset/source/DatasetFactory.java index 46b8545d662..88ea08bf51e 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/source/DatasetFactory.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/source/DatasetFactory.java @@ -20,9 +20,8 @@ import org.apache.arrow.vector.types.pojo.Schema; /** - * DatasetFactory provides a way to inspect a Dataset potential - * schema before materializing it. Thus, the user can peek the schema for - * data sources and decide on a unified schema. + * DatasetFactory provides a way to inspect a Dataset potential schema before materializing it. + * Thus, the user can peek the schema for data sources and decide on a unified schema. */ public interface DatasetFactory extends AutoCloseable { @@ -34,8 +33,8 @@ public interface DatasetFactory extends AutoCloseable { Schema inspect(); /** - * Create a Dataset with auto-inferred schema. Which means, the schema of the resulting Dataset will be - * the same with calling {@link #inspect()} manually. + * Create a Dataset with auto-inferred schema. Which means, the schema of the resulting Dataset + * will be the same with calling {@link #inspect()} manually. * * @return the Dataset instance */ diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/substrait/AceroSubstraitConsumer.java b/java/dataset/src/main/java/org/apache/arrow/dataset/substrait/AceroSubstraitConsumer.java index d5a29ad4e93..44ff9d7ed53 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/substrait/AceroSubstraitConsumer.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/substrait/AceroSubstraitConsumer.java @@ -22,7 +22,6 @@ import java.util.Collections; import java.util.List; import java.util.Map; - import org.apache.arrow.c.ArrowArrayStream; import org.apache.arrow.c.Data; import org.apache.arrow.memory.BufferAllocator; @@ -30,8 +29,8 @@ import org.apache.arrow.vector.ipc.ArrowReader; /** - * Class to expose Java Substrait API for end users, currently operations supported are only to Consume Substrait Plan - * in Plan format (JSON) or Binary format (ByteBuffer). + * Class to expose Java Substrait API for end users, currently operations supported are only to + * Consume Substrait Plan in Plan format (JSON) or Binary format (ByteBuffer). */ public final class AceroSubstraitConsumer { private final BufferAllocator allocator; @@ -54,11 +53,13 @@ public ArrowReader runQuery(String plan) throws Exception { * Run Substrait plan. * * @param plan The JSON Substrait plan. - * @param namedTables A mapping of named tables referenced by the plan to an ArrowReader providing the data - * for the table. Contains the Table Name to Query as a Key and ArrowReader as a Value. - *

{@code ArrowReader nationReader = scanner.scanBatches();
+   * @param namedTables A mapping of named tables referenced by the plan to an ArrowReader providing
+   *     the data for the table. Contains the Table Name to Query as a Key and ArrowReader as a
+   *     Value.
+   *     
{@code ArrowReader nationReader = scanner.scanBatches();
    * Map namedTables = new HashMap<>();
    * namedTables.put("NATION", nationReader);}
+ * * @return the ArrowReader to iterate for record batches. */ public ArrowReader runQuery(String plan, Map namedTables) throws Exception { @@ -68,7 +69,7 @@ public ArrowReader runQuery(String plan, Map namedTables) t /** * Run Substrait plan. * - * @param plan the binary Substrait plan. + * @param plan the binary Substrait plan. * @return the ArrowReader to iterate for record batches. */ public ArrowReader runQuery(ByteBuffer plan) throws Exception { @@ -76,56 +77,51 @@ public ArrowReader runQuery(ByteBuffer plan) throws Exception { } /** - * Read binary Substrait plan, execute and return an ArrowReader to read Schema and ArrowRecordBatches. + * Read binary Substrait plan, execute and return an ArrowReader to read Schema and + * ArrowRecordBatches. * - * @param plan the binary Substrait plan. - * @param namedTables A mapping of named tables referenced by the plan to an ArrowReader providing the data - * for the table. Contains the Table Name to Query as a Key and ArrowReader as a Value. - *
{@code ArrowReader nationReader = scanner.scanBatches();
+   * @param plan the binary Substrait plan.
+   * @param namedTables A mapping of named tables referenced by the plan to an ArrowReader providing
+   *     the data for the table. Contains the Table Name to Query as a Key and ArrowReader as a
+   *     Value.
+   *     
{@code ArrowReader nationReader = scanner.scanBatches();
    * Map namedTables = new HashMap<>();
    * namedTables.put("NATION", nationReader);}
+ * * @return the ArrowReader to iterate for record batches. */ - public ArrowReader runQuery(ByteBuffer plan, Map namedTables) throws Exception { + public ArrowReader runQuery(ByteBuffer plan, Map namedTables) + throws Exception { return execute(plan, namedTables); } private ArrowReader execute(String plan, Map namedTables) throws Exception { List arrowArrayStream = new ArrayList<>(); - try ( - ArrowArrayStream streamOutput = ArrowArrayStream.allocateNew(this.allocator) - ) { + try (ArrowArrayStream streamOutput = ArrowArrayStream.allocateNew(this.allocator)) { String[] mapTableToMemoryAddress = getMapTableToMemoryAddress(namedTables, arrowArrayStream); - JniWrapper.get().executeSerializedPlan( - plan, - mapTableToMemoryAddress, - streamOutput.memoryAddress() - ); + JniWrapper.get() + .executeSerializedPlan(plan, mapTableToMemoryAddress, streamOutput.memoryAddress()); return Data.importArrayStream(this.allocator, streamOutput); } finally { AutoCloseables.close(arrowArrayStream); } } - private ArrowReader execute(ByteBuffer plan, Map namedTables) throws Exception { + private ArrowReader execute(ByteBuffer plan, Map namedTables) + throws Exception { List arrowArrayStream = new ArrayList<>(); - try ( - ArrowArrayStream streamOutput = ArrowArrayStream.allocateNew(this.allocator) - ) { + try (ArrowArrayStream streamOutput = ArrowArrayStream.allocateNew(this.allocator)) { String[] mapTableToMemoryAddress = getMapTableToMemoryAddress(namedTables, arrowArrayStream); - JniWrapper.get().executeSerializedPlan( - plan, - mapTableToMemoryAddress, - streamOutput.memoryAddress() - ); + JniWrapper.get() + .executeSerializedPlan(plan, mapTableToMemoryAddress, streamOutput.memoryAddress()); return Data.importArrayStream(this.allocator, streamOutput); } finally { AutoCloseables.close(arrowArrayStream); } } - private String[] getMapTableToMemoryAddress(Map mapTableToArrowReader, - List listStreamInput) { + private String[] getMapTableToMemoryAddress( + Map mapTableToArrowReader, List listStreamInput) { String[] mapTableToMemoryAddress = new String[mapTableToArrowReader.size() * 2]; ArrowArrayStream streamInput; int pos = 0; diff --git a/java/dataset/src/main/java/org/apache/arrow/dataset/substrait/JniWrapper.java b/java/dataset/src/main/java/org/apache/arrow/dataset/substrait/JniWrapper.java index 5cb68f85146..436abee6351 100644 --- a/java/dataset/src/main/java/org/apache/arrow/dataset/substrait/JniWrapper.java +++ b/java/dataset/src/main/java/org/apache/arrow/dataset/substrait/JniWrapper.java @@ -18,12 +18,13 @@ package org.apache.arrow.dataset.substrait; import java.nio.ByteBuffer; - import org.apache.arrow.dataset.jni.JniLoader; /** - * Class that contains Native methods to call Acero C++ Substrait API. It internally depends on C++ function - * arrow::engine::ExecuteSerializedPlan. Currently supported input parameters supported are: + * Class that contains Native methods to call Acero C++ Substrait API. It internally depends on C++ + * function arrow::engine::ExecuteSerializedPlan. Currently supported input parameters supported + * are: + * *
  * - arrow::Buffer: Substrait Plan (JSON or Binary format).
  * - arrow::engine::ConversionOptions: Mapping for arrow::engine::NamedTableProvider.
@@ -32,8 +33,7 @@
 final class JniWrapper {
   private static final JniWrapper INSTANCE = new JniWrapper();
 
-  private JniWrapper() {
-  }
+  private JniWrapper() {}
 
   public static JniWrapper get() {
     JniLoader.get().ensureLoaded();
@@ -41,33 +41,34 @@ public static JniWrapper get() {
   }
 
   /**
-   * Consume the JSON Substrait Plan that contains Named Tables and export the RecordBatchReader into
-   * C-Data Interface ArrowArrayStream.
+   * Consume the JSON Substrait Plan that contains Named Tables and export the RecordBatchReader
+   * into C-Data Interface ArrowArrayStream.
    *
    * @param planInput the JSON Substrait plan.
-   * @param mapTableToMemoryAddressInput the mapping name of Tables Name on position `i` and theirs Memory Address
-   *                                     representation on `i+1` position linearly.
-   * 
{@code String[] mapTableToMemoryAddress = new String[2];
+   * @param mapTableToMemoryAddressInput the mapping name of Tables Name on position `i` and theirs
+   *     Memory Address representation on `i+1` position linearly.
+   *     
{@code String[] mapTableToMemoryAddress = new String[2];
    * mapTableToMemoryAddress[0]="NATION";
    * mapTableToMemoryAddress[1]="140650250895360";}
- * @param memoryAddressOutput the memory address where RecordBatchReader is exported. * + * @param memoryAddressOutput the memory address where RecordBatchReader is exported. */ - public native void executeSerializedPlan(String planInput, String[] mapTableToMemoryAddressInput, - long memoryAddressOutput); + public native void executeSerializedPlan( + String planInput, String[] mapTableToMemoryAddressInput, long memoryAddressOutput); /** - * Consume the binary Substrait Plan that contains Named Tables and export the RecordBatchReader into - * C-Data Interface ArrowArrayStream. + * Consume the binary Substrait Plan that contains Named Tables and export the RecordBatchReader + * into C-Data Interface ArrowArrayStream. * * @param planInput the binary Substrait plan. - * @param mapTableToMemoryAddressInput the mapping name of Tables Name on position `i` and theirs Memory Address - * representation on `i+1` position linearly. - *
{@code String[] mapTableToMemoryAddress = new String[2];
+   * @param mapTableToMemoryAddressInput the mapping name of Tables Name on position `i` and theirs
+   *     Memory Address representation on `i+1` position linearly.
+   *     
{@code String[] mapTableToMemoryAddress = new String[2];
    * mapTableToMemoryAddress[0]="NATION";
    * mapTableToMemoryAddress[1]="140650250895360";}
+ * * @param memoryAddressOutput the memory address where RecordBatchReader is exported. */ - public native void executeSerializedPlan(ByteBuffer planInput, String[] mapTableToMemoryAddressInput, - long memoryAddressOutput); + public native void executeSerializedPlan( + ByteBuffer planInput, String[] mapTableToMemoryAddressInput, long memoryAddressOutput); } diff --git a/java/dataset/src/test/java/org/apache/arrow/dataset/OrcWriteSupport.java b/java/dataset/src/test/java/org/apache/arrow/dataset/OrcWriteSupport.java index c49612995ee..66c117d6322 100644 --- a/java/dataset/src/test/java/org/apache/arrow/dataset/OrcWriteSupport.java +++ b/java/dataset/src/test/java/org/apache/arrow/dataset/OrcWriteSupport.java @@ -18,7 +18,6 @@ package org.apache.arrow.dataset; import java.io.IOException; - import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector; @@ -28,8 +27,10 @@ import org.apache.orc.Writer; public class OrcWriteSupport { - public static void writeTempFile(TypeDescription orcSchema, Path path, Integer[] values) throws IOException { - Writer writer = OrcFile.createWriter(path, OrcFile.writerOptions(new Configuration()).setSchema(orcSchema)); + public static void writeTempFile(TypeDescription orcSchema, Path path, Integer[] values) + throws IOException { + Writer writer = + OrcFile.createWriter(path, OrcFile.writerOptions(new Configuration()).setSchema(orcSchema)); VectorizedRowBatch batch = orcSchema.createRowBatch(); LongColumnVector longColumnVector = (LongColumnVector) batch.cols[0]; for (int idx = 0; idx < values.length; idx++) { diff --git a/java/dataset/src/test/java/org/apache/arrow/dataset/ParquetWriteSupport.java b/java/dataset/src/test/java/org/apache/arrow/dataset/ParquetWriteSupport.java index 2352a65e8fb..90ac0b3b76a 100644 --- a/java/dataset/src/test/java/org/apache/arrow/dataset/ParquetWriteSupport.java +++ b/java/dataset/src/test/java/org/apache/arrow/dataset/ParquetWriteSupport.java @@ -24,7 +24,6 @@ import java.util.Collections; import java.util.List; import java.util.Random; - import org.apache.arrow.util.Preconditions; import org.apache.avro.Schema; import org.apache.avro.generic.GenericData; @@ -32,9 +31,7 @@ import org.apache.parquet.avro.AvroParquetWriter; import org.apache.parquet.hadoop.ParquetWriter; -/** - * Utility class for writing Parquet files using Avro based tools. - */ +/** Utility class for writing Parquet files using Avro based tools. */ public class ParquetWriteSupport implements AutoCloseable { private final String path; @@ -45,39 +42,42 @@ public class ParquetWriteSupport implements AutoCloseable { private final GenericRecordListBuilder recordListBuilder = new GenericRecordListBuilder(); private final Random random = new Random(); - public ParquetWriteSupport(String schemaName, File outputFolder) throws Exception { avroSchema = getSchema(schemaName); path = outputFolder.getPath() + "/" + "generated-" + random.nextLong() + ".parquet"; uri = "file://" + path; - writer = AvroParquetWriter - .builder(new org.apache.hadoop.fs.Path(path)) - .withSchema(avroSchema) - .build(); + writer = + AvroParquetWriter.builder(new org.apache.hadoop.fs.Path(path)) + .withSchema(avroSchema) + .build(); } public static Schema getSchema(String schemaName) throws Exception { try { - // Attempt to use JDK 9 behavior of getting the module then the resource stream from the module. + // Attempt to use JDK 9 behavior of getting the module then the resource stream from the + // module. // Note that this code is caller-sensitive. Method getModuleMethod = Class.class.getMethod("getModule"); Object module = getModuleMethod.invoke(ParquetWriteSupport.class); - Method getResourceAsStreamFromModule = module.getClass().getMethod("getResourceAsStream", String.class); - try (InputStream is = (InputStream) getResourceAsStreamFromModule.invoke(module, "/avroschema/" + schemaName)) { - return new Schema.Parser() - .parse(is); + Method getResourceAsStreamFromModule = + module.getClass().getMethod("getResourceAsStream", String.class); + try (InputStream is = + (InputStream) getResourceAsStreamFromModule.invoke(module, "/avroschema/" + schemaName)) { + return new Schema.Parser().parse(is); } } catch (NoSuchMethodException ex) { // Use JDK8 behavior. - try (InputStream is = ParquetWriteSupport.class.getResourceAsStream("/avroschema/" + schemaName)) { + try (InputStream is = + ParquetWriteSupport.class.getResourceAsStream("/avroschema/" + schemaName)) { return new Schema.Parser().parse(is); } } } - public static ParquetWriteSupport writeTempFile(String schemaName, File outputFolder, - Object... values) throws Exception { - try (final ParquetWriteSupport writeSupport = new ParquetWriteSupport(schemaName, outputFolder)) { + public static ParquetWriteSupport writeTempFile( + String schemaName, File outputFolder, Object... values) throws Exception { + try (final ParquetWriteSupport writeSupport = + new ParquetWriteSupport(schemaName, outputFolder)) { writeSupport.writeRecords(values); return writeSupport; } @@ -123,8 +123,8 @@ public void close() throws Exception { public class GenericRecordListBuilder { public final List createRecordList(Object... values) { final int fieldCount = avroSchema.getFields().size(); - Preconditions.checkArgument(values.length % fieldCount == 0, - "arg count of values should be divide by field number"); + Preconditions.checkArgument( + values.length % fieldCount == 0, "arg count of values should be divide by field number"); final List recordList = new ArrayList<>(); for (int i = 0; i < values.length / fieldCount; i++) { final GenericRecord record = new GenericData.Record(avroSchema); diff --git a/java/dataset/src/test/java/org/apache/arrow/dataset/TestAllTypes.java b/java/dataset/src/test/java/org/apache/arrow/dataset/TestAllTypes.java index 13b24745234..56eb032e34f 100644 --- a/java/dataset/src/test/java/org/apache/arrow/dataset/TestAllTypes.java +++ b/java/dataset/src/test/java/org/apache/arrow/dataset/TestAllTypes.java @@ -28,7 +28,6 @@ import java.util.Collections; import java.util.List; import java.util.Objects; - import org.apache.arrow.dataset.file.DatasetFileWriter; import org.apache.arrow.dataset.file.FileFormat; import org.apache.arrow.memory.BufferAllocator; @@ -84,56 +83,72 @@ public class TestAllTypes extends TestDataset { - @ClassRule - public static final TemporaryFolder TMP = new TemporaryFolder(); + @ClassRule public static final TemporaryFolder TMP = new TemporaryFolder(); private VectorSchemaRoot generateAllTypesVector(BufferAllocator allocator) { // Notes: // - Float16 is not supported by Java. // - IntervalMonthDayNano is not supported by Parquet. - // - Map (GH-38250) and SparseUnion are resulting in serialization errors when writing with the Dataset API. - // "Unhandled type for Arrow to Parquet schema conversion" errors: IntervalDay, IntervalYear, DenseUnion + // - Map (GH-38250) and SparseUnion are resulting in serialization errors when writing with the + // Dataset API. + // "Unhandled type for Arrow to Parquet schema conversion" errors: IntervalDay, IntervalYear, + // DenseUnion List childFields = new ArrayList<>(); - childFields.add(new Field("int-child", - new FieldType(false, new ArrowType.Int(32, true), null, null), null)); - Field structField = new Field("struct", - new FieldType(true, ArrowType.Struct.INSTANCE, null, null), childFields); - Field[] fields = new Field[] { - Field.nullablePrimitive("null", ArrowType.Null.INSTANCE), - Field.nullablePrimitive("bool", ArrowType.Bool.INSTANCE), - Field.nullablePrimitive("int8", new ArrowType.Int(8, true)), - Field.nullablePrimitive("int16", new ArrowType.Int(16, true)), - Field.nullablePrimitive("int32", new ArrowType.Int(32, true)), - Field.nullablePrimitive("int64", new ArrowType.Int(64, true)), - Field.nullablePrimitive("uint8", new ArrowType.Int(8, false)), - Field.nullablePrimitive("uint16", new ArrowType.Int(16, false)), - Field.nullablePrimitive("uint32", new ArrowType.Int(32, false)), - Field.nullablePrimitive("uint64", new ArrowType.Int(64, false)), - Field.nullablePrimitive("float32", new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)), - Field.nullablePrimitive("float64", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), - Field.nullablePrimitive("utf8", ArrowType.Utf8.INSTANCE), - Field.nullablePrimitive("binary", ArrowType.Binary.INSTANCE), - Field.nullablePrimitive("largeutf8", ArrowType.LargeUtf8.INSTANCE), - Field.nullablePrimitive("largebinary", ArrowType.LargeBinary.INSTANCE), - Field.nullablePrimitive("fixed_size_binary", new ArrowType.FixedSizeBinary(1)), - Field.nullablePrimitive("date_ms", new ArrowType.Date(DateUnit.MILLISECOND)), - Field.nullablePrimitive("time_ms", new ArrowType.Time(TimeUnit.MILLISECOND, 32)), - Field.nullablePrimitive("timestamp_ms", new ArrowType.Timestamp(TimeUnit.MILLISECOND, null)), - Field.nullablePrimitive("timestamptz_ms", new ArrowType.Timestamp(TimeUnit.MILLISECOND, "UTC")), - Field.nullablePrimitive("time_ns", new ArrowType.Time(TimeUnit.NANOSECOND, 64)), - Field.nullablePrimitive("timestamp_ns", new ArrowType.Timestamp(TimeUnit.NANOSECOND, null)), - Field.nullablePrimitive("timestamptz_ns", new ArrowType.Timestamp(TimeUnit.NANOSECOND, "UTC")), - Field.nullablePrimitive("duration", new ArrowType.Duration(TimeUnit.MILLISECOND)), - Field.nullablePrimitive("decimal128", new ArrowType.Decimal(10, 2, 128)), - Field.nullablePrimitive("decimal256", new ArrowType.Decimal(10, 2, 256)), - new Field("list", FieldType.nullable(ArrowType.List.INSTANCE), - Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))), - new Field("largelist", FieldType.nullable(ArrowType.LargeList.INSTANCE), - Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))), - new Field("fixedsizelist", FieldType.nullable(new ArrowType.FixedSizeList(2)), - Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))), - structField - }; + childFields.add( + new Field( + "int-child", new FieldType(false, new ArrowType.Int(32, true), null, null), null)); + Field structField = + new Field( + "struct", new FieldType(true, ArrowType.Struct.INSTANCE, null, null), childFields); + Field[] fields = + new Field[] { + Field.nullablePrimitive("null", ArrowType.Null.INSTANCE), + Field.nullablePrimitive("bool", ArrowType.Bool.INSTANCE), + Field.nullablePrimitive("int8", new ArrowType.Int(8, true)), + Field.nullablePrimitive("int16", new ArrowType.Int(16, true)), + Field.nullablePrimitive("int32", new ArrowType.Int(32, true)), + Field.nullablePrimitive("int64", new ArrowType.Int(64, true)), + Field.nullablePrimitive("uint8", new ArrowType.Int(8, false)), + Field.nullablePrimitive("uint16", new ArrowType.Int(16, false)), + Field.nullablePrimitive("uint32", new ArrowType.Int(32, false)), + Field.nullablePrimitive("uint64", new ArrowType.Int(64, false)), + Field.nullablePrimitive( + "float32", new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)), + Field.nullablePrimitive( + "float64", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), + Field.nullablePrimitive("utf8", ArrowType.Utf8.INSTANCE), + Field.nullablePrimitive("binary", ArrowType.Binary.INSTANCE), + Field.nullablePrimitive("largeutf8", ArrowType.LargeUtf8.INSTANCE), + Field.nullablePrimitive("largebinary", ArrowType.LargeBinary.INSTANCE), + Field.nullablePrimitive("fixed_size_binary", new ArrowType.FixedSizeBinary(1)), + Field.nullablePrimitive("date_ms", new ArrowType.Date(DateUnit.MILLISECOND)), + Field.nullablePrimitive("time_ms", new ArrowType.Time(TimeUnit.MILLISECOND, 32)), + Field.nullablePrimitive( + "timestamp_ms", new ArrowType.Timestamp(TimeUnit.MILLISECOND, null)), + Field.nullablePrimitive( + "timestamptz_ms", new ArrowType.Timestamp(TimeUnit.MILLISECOND, "UTC")), + Field.nullablePrimitive("time_ns", new ArrowType.Time(TimeUnit.NANOSECOND, 64)), + Field.nullablePrimitive( + "timestamp_ns", new ArrowType.Timestamp(TimeUnit.NANOSECOND, null)), + Field.nullablePrimitive( + "timestamptz_ns", new ArrowType.Timestamp(TimeUnit.NANOSECOND, "UTC")), + Field.nullablePrimitive("duration", new ArrowType.Duration(TimeUnit.MILLISECOND)), + Field.nullablePrimitive("decimal128", new ArrowType.Decimal(10, 2, 128)), + Field.nullablePrimitive("decimal256", new ArrowType.Decimal(10, 2, 256)), + new Field( + "list", + FieldType.nullable(ArrowType.List.INSTANCE), + Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))), + new Field( + "largelist", + FieldType.nullable(ArrowType.LargeList.INSTANCE), + Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))), + new Field( + "fixedsizelist", + FieldType.nullable(new ArrowType.FixedSizeList(2)), + Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))), + structField + }; VectorSchemaRoot root = VectorSchemaRoot.create(new Schema(Arrays.asList(fields)), allocator); root.allocateNew(); root.setRowCount(2); @@ -197,7 +212,8 @@ private VectorSchemaRoot generateAllTypesVector(BufferAllocator allocator) { ((DurationVector) root.getVector("duration")).set(1, 0); ((DecimalVector) root.getVector("decimal128")).set(1, 0); ((Decimal256Vector) root.getVector("decimal256")).set(1, 0); - UnionFixedSizeListWriter fixedListWriter = ((FixedSizeListVector) root.getVector("fixedsizelist")).getWriter(); + UnionFixedSizeListWriter fixedListWriter = + ((FixedSizeListVector) root.getVector("fixedsizelist")).getWriter(); fixedListWriter.allocate(); fixedListWriter.setPosition(1); fixedListWriter.startList(); @@ -211,7 +227,8 @@ private VectorSchemaRoot generateAllTypesVector(BufferAllocator allocator) { listWriter.integer().writeInt(1); listWriter.endList(); - UnionLargeListWriter largeListWriter = ((LargeListVector) root.getVector("largelist")).getWriter(); + UnionLargeListWriter largeListWriter = + ((LargeListVector) root.getVector("largelist")).getWriter(); largeListWriter.allocate(); largeListWriter.setPosition(1); largeListWriter.startList(); @@ -223,11 +240,9 @@ private VectorSchemaRoot generateAllTypesVector(BufferAllocator allocator) { } private byte[] serializeFile(VectorSchemaRoot root) { - try ( - ByteArrayOutputStream out = new ByteArrayOutputStream(); + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); WritableByteChannel channel = Channels.newChannel(out); - ArrowStreamWriter writer = new ArrowStreamWriter(root, null, channel) - ) { + ArrowStreamWriter writer = new ArrowStreamWriter(root, null, channel)) { writer.start(); writer.writeBatch(); writer.end(); @@ -246,15 +261,18 @@ public void testAllTypesParquet() throws Exception { TMP.create(); final File writtenFolder = TMP.newFolder(); final String writtenParquet = writtenFolder.toURI().toString(); - DatasetFileWriter.write(rootAllocator(), reader, FileFormat.PARQUET, - writtenParquet); + DatasetFileWriter.write(rootAllocator(), reader, FileFormat.PARQUET, writtenParquet); - // Load the reference file from the test resources and write to a temporary file on the OS. - String referenceFile = ArrowTestDataUtil.getTestDataRoot() - .resolve("parquet") - .resolve("alltypes-java.parquet") - .toUri().toString(); - assertParquetFileEquals(referenceFile, + // Load the reference file from the test resources and write to a temporary file on the + // OS. + String referenceFile = + ArrowTestDataUtil.getTestDataRoot() + .resolve("parquet") + .resolve("alltypes-java.parquet") + .toUri() + .toString(); + assertParquetFileEquals( + referenceFile, Objects.requireNonNull(writtenFolder.listFiles())[0].toURI().toString()); } } diff --git a/java/dataset/src/test/java/org/apache/arrow/dataset/TestDataset.java b/java/dataset/src/test/java/org/apache/arrow/dataset/TestDataset.java index cafa63b7880..1740f56c5f4 100644 --- a/java/dataset/src/test/java/org/apache/arrow/dataset/TestDataset.java +++ b/java/dataset/src/test/java/org/apache/arrow/dataset/TestDataset.java @@ -26,7 +26,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.StreamSupport; - import org.apache.arrow.dataset.file.FileFormat; import org.apache.arrow.dataset.file.FileSystemDatasetFactory; import org.apache.arrow.dataset.jni.NativeMemoryPool; @@ -49,7 +48,6 @@ import org.junit.Assert; import org.junit.Before; - public abstract class TestDataset { private BufferAllocator allocator = null; @@ -67,7 +65,8 @@ protected BufferAllocator rootAllocator() { return allocator; } - protected List collectResultFromFactory(DatasetFactory factory, ScanOptions options) { + protected List collectResultFromFactory( + DatasetFactory factory, ScanOptions options) { final Dataset dataset = factory.finish(); final Scanner scanner = dataset.newScan(options); try { @@ -108,17 +107,20 @@ protected Schema inferResultSchemaFromFactory(DatasetFactory factory, ScanOption } protected void assertParquetFileEquals(String expectedURI, String actualURI) throws Exception { - final FileSystemDatasetFactory expectedFactory = new FileSystemDatasetFactory( - rootAllocator(), NativeMemoryPool.getDefault(), FileFormat.PARQUET, expectedURI); - final FileSystemDatasetFactory actualFactory = new FileSystemDatasetFactory( - rootAllocator(), NativeMemoryPool.getDefault(), FileFormat.PARQUET, actualURI); - List expectedBatches = collectResultFromFactory(expectedFactory, - new ScanOptions(new String[0], 100)); - List actualBatches = collectResultFromFactory(actualFactory, - new ScanOptions(new String[0], 100)); - try ( - VectorSchemaRoot expectVsr = VectorSchemaRoot.create(expectedFactory.inspect(), rootAllocator()); - VectorSchemaRoot actualVsr = VectorSchemaRoot.create(actualFactory.inspect(), rootAllocator())) { + final FileSystemDatasetFactory expectedFactory = + new FileSystemDatasetFactory( + rootAllocator(), NativeMemoryPool.getDefault(), FileFormat.PARQUET, expectedURI); + final FileSystemDatasetFactory actualFactory = + new FileSystemDatasetFactory( + rootAllocator(), NativeMemoryPool.getDefault(), FileFormat.PARQUET, actualURI); + List expectedBatches = + collectResultFromFactory(expectedFactory, new ScanOptions(new String[0], 100)); + List actualBatches = + collectResultFromFactory(actualFactory, new ScanOptions(new String[0], 100)); + try (VectorSchemaRoot expectVsr = + VectorSchemaRoot.create(expectedFactory.inspect(), rootAllocator()); + VectorSchemaRoot actualVsr = + VectorSchemaRoot.create(actualFactory.inspect(), rootAllocator())) { // fast-fail by comparing metadata Assert.assertEquals(expectedBatches.toString(), actualBatches.toString()); @@ -150,7 +152,8 @@ protected List collect(Iterable iterable) { } protected Stream stream(Iterator iterator) { - return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false); + return StreamSupport.stream( + Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false); } protected List collect(Iterator iterator) { diff --git a/java/dataset/src/test/java/org/apache/arrow/dataset/TextBasedWriteSupport.java b/java/dataset/src/test/java/org/apache/arrow/dataset/TextBasedWriteSupport.java index 43f96f6d4d6..8f5cb0c87c5 100644 --- a/java/dataset/src/test/java/org/apache/arrow/dataset/TextBasedWriteSupport.java +++ b/java/dataset/src/test/java/org/apache/arrow/dataset/TextBasedWriteSupport.java @@ -29,11 +29,19 @@ public class TextBasedWriteSupport { private final Random random = new Random(); public TextBasedWriteSupport(File outputFolder, String fileExtension) throws URISyntaxException { - uri = new URI("file", outputFolder.getPath() + File.separator + - "generated-" + random.nextLong() + fileExtension, null); + uri = + new URI( + "file", + outputFolder.getPath() + + File.separator + + "generated-" + + random.nextLong() + + fileExtension, + null); } - public static TextBasedWriteSupport writeTempFile(File outputFolder, String fileExtension, String... values) + public static TextBasedWriteSupport writeTempFile( + File outputFolder, String fileExtension, String... values) throws URISyntaxException, IOException { TextBasedWriteSupport writer = new TextBasedWriteSupport(outputFolder, fileExtension); try (FileWriter addValues = new FileWriter(new File(writer.uri), true)) { diff --git a/java/dataset/src/test/java/org/apache/arrow/dataset/file/TestDatasetFileWriter.java b/java/dataset/src/test/java/org/apache/arrow/dataset/file/TestDatasetFileWriter.java index 86797c165b2..d1c9f8f9313 100644 --- a/java/dataset/src/test/java/org/apache/arrow/dataset/file/TestDatasetFileWriter.java +++ b/java/dataset/src/test/java/org/apache/arrow/dataset/file/TestDatasetFileWriter.java @@ -23,7 +23,6 @@ import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; - import org.apache.arrow.dataset.ParquetWriteSupport; import org.apache.arrow.dataset.TestDataset; import org.apache.arrow.dataset.jni.NativeMemoryPool; @@ -39,58 +38,70 @@ public class TestDatasetFileWriter extends TestDataset { - @ClassRule - public static final TemporaryFolder TMP = new TemporaryFolder(); + @ClassRule public static final TemporaryFolder TMP = new TemporaryFolder(); public static final String AVRO_SCHEMA_USER = "user.avsc"; @Test public void testParquetWriteSimple() throws Exception { - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), - 1, "a", 2, "b", 3, "c", 2, "d"); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a", 2, "b", 3, "c", 2, "d"); String sampleParquet = writeSupport.getOutputURI(); ScanOptions options = new ScanOptions(new String[0], 100); final File writtenFolder = TMP.newFolder(); final String writtenParquet = writtenFolder.toURI().toString(); - try (FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, sampleParquet); - final Dataset dataset = factory.finish(); - final Scanner scanner = dataset.newScan(options); - final ArrowScannerReader reader = new ArrowScannerReader(scanner, rootAllocator()); - ) { + try (FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), NativeMemoryPool.getDefault(), FileFormat.PARQUET, sampleParquet); + final Dataset dataset = factory.finish(); + final Scanner scanner = dataset.newScan(options); + final ArrowScannerReader reader = new ArrowScannerReader(scanner, rootAllocator()); ) { DatasetFileWriter.write(rootAllocator(), reader, FileFormat.PARQUET, writtenParquet); - assertParquetFileEquals(sampleParquet, Objects.requireNonNull(writtenFolder.listFiles())[0].toURI().toString()); + assertParquetFileEquals( + sampleParquet, Objects.requireNonNull(writtenFolder.listFiles())[0].toURI().toString()); } } @Test public void testParquetWriteWithPartitions() throws Exception { - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), - 1, "a", 2, "b", 3, "c", 2, "d"); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a", 2, "b", 3, "c", 2, "d"); String sampleParquet = writeSupport.getOutputURI(); ScanOptions options = new ScanOptions(new String[0], 100); final File writtenFolder = TMP.newFolder(); final String writtenParquet = writtenFolder.toURI().toString(); - try (FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, sampleParquet); - final Dataset dataset = factory.finish(); - final Scanner scanner = dataset.newScan(options); - final ArrowScannerReader reader = new ArrowScannerReader(scanner, rootAllocator()); - ) { - DatasetFileWriter.write(rootAllocator(), reader, - FileFormat.PARQUET, writtenParquet, new String[]{"id", "name"}, - 100, "data_{i}"); - final Set expectedOutputFiles = new HashSet<>( - Arrays.asList("id=1/name=a/data_0", "id=2/name=b/data_0", "id=3/name=c/data_0", "id=2/name=d/data_0")); - final Set outputFiles = FileUtils.listFiles(writtenFolder, null, true) - .stream() - .map(file -> { - return writtenFolder.toURI().relativize(file.toURI()).toString(); - }) - .collect(Collectors.toSet()); + try (FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), NativeMemoryPool.getDefault(), FileFormat.PARQUET, sampleParquet); + final Dataset dataset = factory.finish(); + final Scanner scanner = dataset.newScan(options); + final ArrowScannerReader reader = new ArrowScannerReader(scanner, rootAllocator()); ) { + DatasetFileWriter.write( + rootAllocator(), + reader, + FileFormat.PARQUET, + writtenParquet, + new String[] {"id", "name"}, + 100, + "data_{i}"); + final Set expectedOutputFiles = + new HashSet<>( + Arrays.asList( + "id=1/name=a/data_0", + "id=2/name=b/data_0", + "id=3/name=c/data_0", + "id=2/name=d/data_0")); + final Set outputFiles = + FileUtils.listFiles(writtenFolder, null, true).stream() + .map( + file -> { + return writtenFolder.toURI().relativize(file.toURI()).toString(); + }) + .collect(Collectors.toSet()); Assert.assertEquals(expectedOutputFiles, outputFiles); } } } - diff --git a/java/dataset/src/test/java/org/apache/arrow/dataset/file/TestFileSystemDataset.java b/java/dataset/src/test/java/org/apache/arrow/dataset/file/TestFileSystemDataset.java index 6ca559ee2d7..8ed8f316727 100644 --- a/java/dataset/src/test/java/org/apache/arrow/dataset/file/TestFileSystemDataset.java +++ b/java/dataset/src/test/java/org/apache/arrow/dataset/file/TestFileSystemDataset.java @@ -22,6 +22,8 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.primitives.Primitives; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -36,7 +38,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.Collectors; - import org.apache.arrow.dataset.OrcWriteSupport; import org.apache.arrow.dataset.ParquetWriteSupport; import org.apache.arrow.dataset.TextBasedWriteSupport; @@ -68,22 +69,23 @@ import org.junit.jupiter.api.Assertions; import org.junit.rules.TemporaryFolder; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.primitives.Primitives; - public class TestFileSystemDataset extends TestNativeDataset { - @ClassRule - public static final TemporaryFolder TMP = new TemporaryFolder(); + @ClassRule public static final TemporaryFolder TMP = new TemporaryFolder(); public static final String AVRO_SCHEMA_USER = "user.avsc"; @Test public void testBaseParquetRead() throws Exception { - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); - - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); + + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); ScanOptions options = new ScanOptions(100); Schema schema = inferResultSchemaFromFactory(factory, options); List datum = collectResultFromFactory(factory, options); @@ -103,15 +105,19 @@ public void testBaseParquetRead() throws Exception { @Test public void testMultipleParquetReadFromUris() throws Exception { - ParquetWriteSupport writeSupport1 = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), - 1, "a"); - ParquetWriteSupport writeSupport2 = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), - 2, "b"); + ParquetWriteSupport writeSupport1 = + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); + ParquetWriteSupport writeSupport2 = + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 2, "b"); String expectedJsonUnordered = "[[1,\"a\"],[2,\"b\"]]"; ScanOptions options = new ScanOptions(1); - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, new String[]{writeSupport1.getOutputURI(), writeSupport2.getOutputURI()}); + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + new String[] {writeSupport1.getOutputURI(), writeSupport2.getOutputURI()}); Schema schema = inferResultSchemaFromFactory(factory, options); List datum = collectResultFromFactory(factory, options); @@ -124,33 +130,50 @@ public void testMultipleParquetReadFromUris() throws Exception { AutoCloseables.close(factory); } - @Test public void testMultipleParquetInvalidUri() throws Exception { - RuntimeException exc = assertThrows(RuntimeException.class, - () -> new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, new String[]{"https://example.com", "file:///test/location"})); - Assertions.assertEquals("Unrecognized filesystem type in URI: https://example.com", exc.getMessage()); + RuntimeException exc = + assertThrows( + RuntimeException.class, + () -> + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + new String[] {"https://example.com", "file:///test/location"})); + Assertions.assertEquals( + "Unrecognized filesystem type in URI: https://example.com", exc.getMessage()); } @Test public void testMultipleParquetMultipleFilesystemTypes() throws Exception { - RuntimeException exc = assertThrows(RuntimeException.class, - () -> new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, new String[]{"file:///test/location", "s3:///test/bucket/file" })); + RuntimeException exc = + assertThrows( + RuntimeException.class, + () -> + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + new String[] {"file:///test/location", "s3:///test/bucket/file"})); Assertions.assertTrue( - exc.getMessage().startsWith("The filesystem expected a URI with one of the schemes (file) but received s3" - ) - ); + exc.getMessage() + .startsWith( + "The filesystem expected a URI with one of the schemes (file) but received s3")); } @Test public void testParquetProjectSingleColumn() throws Exception { - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); - - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); - ScanOptions options = new ScanOptions(100, Optional.of(new String[]{"id"})); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); + + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); + ScanOptions options = new ScanOptions(100, Optional.of(new String[] {"id"})); Schema schema = inferResultSchemaFromFactory(factory, options); List datum = collectResultFromFactory(factory, options); org.apache.avro.Schema expectedSchema = truncateAvroSchema(writeSupport.getAvroSchema(), 0, 1); @@ -160,12 +183,10 @@ public void testParquetProjectSingleColumn() throws Exception { assertEquals("id", schema.getFields().get(0).getName()); assertEquals(Types.MinorType.INT.getType(), schema.getFields().get(0).getType()); assertEquals(1, datum.size()); - checkParquetReadResult(schema, - Collections.singletonList( - new GenericRecordBuilder( - expectedSchema) - .set("id", 1) - .build()), datum); + checkParquetReadResult( + schema, + Collections.singletonList(new GenericRecordBuilder(expectedSchema).set("id", 1).build()), + datum); AutoCloseables.close(datum); AutoCloseables.close(factory); @@ -173,12 +194,17 @@ public void testParquetProjectSingleColumn() throws Exception { @Test public void testParquetBatchSize() throws Exception { - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), - 1, "a", 2, "b", 3, "c"); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a", 2, "b", 3, "c"); ScanOptions options = new ScanOptions(1); - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); Schema schema = inferResultSchemaFromFactory(factory, options); List datum = collectResultFromFactory(factory, options); @@ -194,15 +220,19 @@ public void testParquetBatchSize() throws Exception { @Test public void testParquetDirectoryRead() throws Exception { final File outputFolder = TMP.newFolder(); - ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, outputFolder, - 1, "a", 2, "b", 3, "c"); - ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, outputFolder, - 4, "e", 5, "f", 6, "g", 7, "h"); - String expectedJsonUnordered = "[[1,\"a\"],[2,\"b\"],[3,\"c\"],[4,\"e\"],[5,\"f\"],[6,\"g\"],[7,\"h\"]]"; + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, outputFolder, 1, "a", 2, "b", 3, "c"); + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, outputFolder, 4, "e", 5, "f", 6, "g", 7, "h"); + String expectedJsonUnordered = + "[[1,\"a\"],[2,\"b\"],[3,\"c\"],[4,\"e\"],[5,\"f\"],[6,\"g\"],[7,\"h\"]]"; ScanOptions options = new ScanOptions(new String[0], 1); - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, outputFolder.toURI().toString()); + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + outputFolder.toURI().toString()); Schema schema = inferResultSchemaFromFactory(factory, options); List datum = collectResultFromFactory(factory, options); @@ -216,33 +246,41 @@ public void testParquetDirectoryRead() throws Exception { @Test public void testEmptyProjectSelectsZeroColumns() throws Exception { - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); - - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); + + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); ScanOptions options = new ScanOptions(100, Optional.of(new String[0])); Schema schema = inferResultSchemaFromFactory(factory, options); List datum = collectResultFromFactory(factory, options); - org.apache.avro.Schema expectedSchema = org.apache.avro.Schema.createRecord(Collections.emptyList()); + org.apache.avro.Schema expectedSchema = + org.apache.avro.Schema.createRecord(Collections.emptyList()); assertScanBatchesProduced(factory, options); assertEquals(0, schema.getFields().size()); assertEquals(1, datum.size()); - checkParquetReadResult(schema, - Collections.singletonList( - new GenericRecordBuilder( - expectedSchema) - .build()), datum); + checkParquetReadResult( + schema, Collections.singletonList(new GenericRecordBuilder(expectedSchema).build()), datum); AutoCloseables.close(datum); } @Test public void testNullProjectSelectsAllColumns() throws Exception { - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); - - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); + + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); ScanOptions options = new ScanOptions(100, Optional.empty()); Schema schema = inferResultSchemaFromFactory(factory, options); List datum = collectResultFromFactory(factory, options); @@ -261,35 +299,46 @@ public void testNullProjectSelectsAllColumns() throws Exception { @Test public void testNoErrorWhenCloseAgain() throws Exception { - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); - - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); - - assertDoesNotThrow(() -> { - NativeDataset dataset = factory.finish(); - dataset.close(); - dataset.close(); - }); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); + + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); + + assertDoesNotThrow( + () -> { + NativeDataset dataset = factory.finish(); + dataset.close(); + dataset.close(); + }); AutoCloseables.close(factory); } @Test public void testErrorThrownWhenScanBatchesAgain() throws Exception { - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); - - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); + + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); NativeDataset dataset = factory.finish(); ScanOptions options = new ScanOptions(100); NativeScanner scanner = dataset.newScan(options); List datum = collectTaskData(scanner); AutoCloseables.close(datum); - UnsupportedOperationException uoe = assertThrows(UnsupportedOperationException.class, - scanner::scanBatches); - Assertions.assertEquals("NativeScanner can only be executed once. Create a new scanner instead", - uoe.getMessage()); + UnsupportedOperationException uoe = + assertThrows(UnsupportedOperationException.class, scanner::scanBatches); + Assertions.assertEquals( + "NativeScanner can only be executed once. Create a new scanner instead", uoe.getMessage()); AutoCloseables.close(scanner, dataset, factory); } @@ -297,10 +346,15 @@ public void testErrorThrownWhenScanBatchesAgain() throws Exception { @Test public void testScanBatchesInOtherThread() throws Exception { ExecutorService executor = Executors.newSingleThreadExecutor(); - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); - - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); + + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); NativeDataset dataset = factory.finish(); ScanOptions options = new ScanOptions(100); NativeScanner scanner = dataset.newScan(options); @@ -312,10 +366,15 @@ public void testScanBatchesInOtherThread() throws Exception { @Test public void testErrorThrownWhenScanBatchesAfterScannerClose() throws Exception { - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); - - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); + + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); NativeDataset dataset = factory.finish(); ScanOptions options = new ScanOptions(100); NativeScanner scanner = dataset.newScan(options); @@ -327,10 +386,15 @@ public void testErrorThrownWhenScanBatchesAfterScannerClose() throws Exception { @Test public void testErrorThrownWhenReadAfterNativeReaderClose() throws Exception { - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); - - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); + + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); NativeDataset dataset = factory.finish(); ScanOptions options = new ScanOptions(100); NativeScanner scanner = dataset.newScan(options); @@ -344,10 +408,12 @@ public void testErrorThrownWhenReadAfterNativeReaderClose() throws Exception { @Test public void testBaseArrowIpcRead() throws Exception { File dataFile = TMP.newFile(); - Schema sourceSchema = new Schema(Collections.singletonList(Field.nullable("ints", new ArrowType.Int(32, true)))); + Schema sourceSchema = + new Schema(Collections.singletonList(Field.nullable("ints", new ArrowType.Int(32, true)))); try (VectorSchemaRoot root = VectorSchemaRoot.create(sourceSchema, rootAllocator()); - FileOutputStream sink = new FileOutputStream(dataFile); - ArrowFileWriter writer = new ArrowFileWriter(root, /*dictionaryProvider=*/null, sink.getChannel())) { + FileOutputStream sink = new FileOutputStream(dataFile); + ArrowFileWriter writer = + new ArrowFileWriter(root, /* dictionaryProvider= */ null, sink.getChannel())) { IntVector ints = (IntVector) root.getVector(0); ints.setSafe(0, 0); ints.setSafe(1, 1024); @@ -359,8 +425,9 @@ public void testBaseArrowIpcRead() throws Exception { } String arrowDataURI = dataFile.toURI().toString(); - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.ARROW_IPC, arrowDataURI); + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), NativeMemoryPool.getDefault(), FileFormat.ARROW_IPC, arrowDataURI); ScanOptions options = new ScanOptions(100); Schema schema = inferResultSchemaFromFactory(factory, options); List datum = collectResultFromFactory(factory, options); @@ -384,11 +451,13 @@ public void testBaseOrcRead() throws Exception { TypeDescription orcSchema = TypeDescription.fromString("struct"); Path path = new Path(basePath, dataName); - OrcWriteSupport.writeTempFile(orcSchema, path, new Integer[]{Integer.MIN_VALUE, Integer.MAX_VALUE}); + OrcWriteSupport.writeTempFile( + orcSchema, path, new Integer[] {Integer.MIN_VALUE, Integer.MAX_VALUE}); String orcDatasetUri = new File(basePath, dataName).toURI().toString(); - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.ORC, orcDatasetUri); + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), NativeMemoryPool.getDefault(), FileFormat.ORC, orcDatasetUri); ScanOptions options = new ScanOptions(100); Schema schema = inferResultSchemaFromFactory(factory, options); List datum = collectResultFromFactory(factory, options); @@ -407,14 +476,18 @@ public void testBaseOrcRead() throws Exception { @Test public void testBaseCsvRead() throws Exception { - TextBasedWriteSupport writeSupport = TextBasedWriteSupport.writeTempFile( + TextBasedWriteSupport writeSupport = + TextBasedWriteSupport.writeTempFile( TMP.newFolder(), ".csv", "Name,Language", "Juno,Java", "Peter,Python", "Celin,C++"); - String expectedJsonUnordered = "[[\"Juno\", \"Java\"], [\"Peter\", \"Python\"], [\"Celin\", \"C++\"]]"; + String expectedJsonUnordered = + "[[\"Juno\", \"Java\"], [\"Peter\", \"Python\"], [\"Celin\", \"C++\"]]"; ScanOptions options = new ScanOptions(100); - try ( - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.CSV, writeSupport.getOutputURI()) - ) { + try (FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.CSV, + writeSupport.getOutputURI())) { List datum = collectResultFromFactory(factory, options); Schema schema = inferResultSchemaFromFactory(factory, options); @@ -431,17 +504,20 @@ public void testBaseCsvRead() throws Exception { @Test public void testBaseJsonRead() throws Exception { - TextBasedWriteSupport writeSupport = TextBasedWriteSupport.writeTempFile( - TMP.newFolder(), ".json", - "{\"Type\": \"Compiled\", \"Language\": \"Java\"}", - "{\"Type\": \"Interpreted\", \"Language\": \"Python\"}"); - String expectedJsonUnordered = "[[\"Compiled\", \"Java\"], " + - "[\"Interpreted\", \"Python\"]]"; + TextBasedWriteSupport writeSupport = + TextBasedWriteSupport.writeTempFile( + TMP.newFolder(), + ".json", + "{\"Type\": \"Compiled\", \"Language\": \"Java\"}", + "{\"Type\": \"Interpreted\", \"Language\": \"Python\"}"); + String expectedJsonUnordered = "[[\"Compiled\", \"Java\"], " + "[\"Interpreted\", \"Python\"]]"; ScanOptions options = new ScanOptions(100); - try ( - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.JSON, writeSupport.getOutputURI()) - ) { + try (FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.JSON, + writeSupport.getOutputURI())) { List datum = collectResultFromFactory(factory, options); Schema schema = inferResultSchemaFromFactory(factory, options); @@ -457,8 +533,8 @@ public void testBaseJsonRead() throws Exception { } } - private void checkParquetReadResult(Schema schema, String expectedJson, List actual) - throws IOException { + private void checkParquetReadResult( + Schema schema, String expectedJson, List actual) throws IOException { final ObjectMapper json = new ObjectMapper(); final Set expectedSet = json.readValue(expectedJson, Set.class); final Set> actualSet = new HashSet<>(); @@ -482,14 +558,15 @@ private void checkParquetReadResult(Schema schema, String expectedJson, List expected, List actual) { - assertEquals(expected.size(), actual.stream() - .mapToInt(ArrowRecordBatch::getLength) - .sum()); + private void checkParquetReadResult( + Schema schema, List expected, List actual) { + assertEquals(expected.size(), actual.stream().mapToInt(ArrowRecordBatch::getLength).sum()); final int fieldCount = schema.getFields().size(); LinkedList expectedRemovable = new LinkedList<>(expected); try (VectorSchemaRoot vsr = VectorSchemaRoot.create(schema, rootAllocator())) { @@ -503,8 +580,7 @@ private void checkParquetReadResult(Schema schema, List expected, for (int j = 0; j < batchRowCount; j++) { Object object = vector.getObject(j); Object expectedObject = expectedRemovable.get(j).get(i); - assertEquals(Objects.toString(expectedObject), - Objects.toString(object)); + assertEquals(Objects.toString(expectedObject), Objects.toString(object)); } } for (int i = 0; i < batchRowCount; i++) { @@ -515,11 +591,15 @@ private void checkParquetReadResult(Schema schema, List expected, } } - private org.apache.avro.Schema truncateAvroSchema(org.apache.avro.Schema schema, int from, int to) { + private org.apache.avro.Schema truncateAvroSchema( + org.apache.avro.Schema schema, int from, int to) { List fields = schema.getFields().subList(from, to); return org.apache.avro.Schema.createRecord( fields.stream() - .map(f -> new org.apache.avro.Schema.Field(f.name(), f.schema(), f.doc(), f.defaultVal(), f.order())) + .map( + f -> + new org.apache.avro.Schema.Field( + f.name(), f.schema(), f.doc(), f.defaultVal(), f.order())) .collect(Collectors.toList())); } } diff --git a/java/dataset/src/test/java/org/apache/arrow/dataset/file/TestFileSystemDatasetFactory.java b/java/dataset/src/test/java/org/apache/arrow/dataset/file/TestFileSystemDatasetFactory.java index bddf96b5ec9..69b07d42e90 100644 --- a/java/dataset/src/test/java/org/apache/arrow/dataset/file/TestFileSystemDatasetFactory.java +++ b/java/dataset/src/test/java/org/apache/arrow/dataset/file/TestFileSystemDatasetFactory.java @@ -29,20 +29,31 @@ public class TestFileSystemDatasetFactory { @Test public void testErrorHandling() { - RuntimeException e = assertThrows(RuntimeException.class, () -> { - new FileSystemDatasetFactory(new RootAllocator(Long.MAX_VALUE), NativeMemoryPool.getDefault(), - FileFormat.NONE, "file:///NON_EXIST_FILE"); - }); + RuntimeException e = + assertThrows( + RuntimeException.class, + () -> { + new FileSystemDatasetFactory( + new RootAllocator(Long.MAX_VALUE), + NativeMemoryPool.getDefault(), + FileFormat.NONE, + "file:///NON_EXIST_FILE"); + }); assertEquals("illegal file format id: -1", e.getMessage()); } @Test public void testCloseAgain() { - assertDoesNotThrow(() -> { - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(new RootAllocator(Long.MAX_VALUE), - NativeMemoryPool.getDefault(), FileFormat.PARQUET, "file:///NON_EXIST_FILE"); - factory.close(); - factory.close(); - }); + assertDoesNotThrow( + () -> { + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + new RootAllocator(Long.MAX_VALUE), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + "file:///NON_EXIST_FILE"); + factory.close(); + factory.close(); + }); } } diff --git a/java/dataset/src/test/java/org/apache/arrow/dataset/jni/TestReservationListener.java b/java/dataset/src/test/java/org/apache/arrow/dataset/jni/TestReservationListener.java index fa42f7ee7cd..d839f1a0c2d 100644 --- a/java/dataset/src/test/java/org/apache/arrow/dataset/jni/TestReservationListener.java +++ b/java/dataset/src/test/java/org/apache/arrow/dataset/jni/TestReservationListener.java @@ -19,7 +19,6 @@ import java.util.List; import java.util.concurrent.atomic.AtomicLong; - import org.apache.arrow.dataset.ParquetWriteSupport; import org.apache.arrow.dataset.TestDataset; import org.apache.arrow.dataset.file.FileFormat; @@ -35,18 +34,18 @@ public class TestReservationListener extends TestDataset { - @ClassRule - public static final TemporaryFolder TMP = new TemporaryFolder(); + @ClassRule public static final TemporaryFolder TMP = new TemporaryFolder(); public static final String AVRO_SCHEMA_USER = "user.avsc"; @Test public void testDirectReservationListener() throws Exception { - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); NativeMemoryPool pool = NativeMemoryPool.createListenable(DirectReservationListener.instance()); - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), - pool, FileFormat.PARQUET, - writeSupport.getOutputURI()); + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), pool, FileFormat.PARQUET, writeSupport.getOutputURI()); ScanOptions options = new ScanOptions(100); long initReservation = DirectReservationListener.instance().getCurrentDirectMemReservation(); List datum = collectResultFromFactory(factory, options); @@ -60,22 +59,25 @@ public void testDirectReservationListener() throws Exception { @Test public void testCustomReservationListener() throws Exception { - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); final AtomicLong reserved = new AtomicLong(0L); - ReservationListener listener = new ReservationListener() { - @Override - public void reserve(long size) { - reserved.getAndAdd(size); - } + ReservationListener listener = + new ReservationListener() { + @Override + public void reserve(long size) { + reserved.getAndAdd(size); + } - @Override - public void unreserve(long size) { - reserved.getAndAdd(-size); - } - }; + @Override + public void unreserve(long size) { + reserved.getAndAdd(-size); + } + }; NativeMemoryPool pool = NativeMemoryPool.createListenable(listener); - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), - pool, FileFormat.PARQUET, writeSupport.getOutputURI()); + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), pool, FileFormat.PARQUET, writeSupport.getOutputURI()); ScanOptions options = new ScanOptions(100); long initReservation = reserved.get(); List datum = collectResultFromFactory(factory, options); @@ -90,27 +92,33 @@ public void unreserve(long size) { @Test public void testErrorThrownFromReservationListener() throws Exception { final String errorMessage = "ERROR_MESSAGE"; - ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a"); final AtomicLong reserved = new AtomicLong(0L); - ReservationListener listener = new ReservationListener() { - @Override - public void reserve(long size) { - throw new IllegalArgumentException(errorMessage); - } + ReservationListener listener = + new ReservationListener() { + @Override + public void reserve(long size) { + throw new IllegalArgumentException(errorMessage); + } - @Override - public void unreserve(long size) { - // no-op - } - }; + @Override + public void unreserve(long size) { + // no-op + } + }; NativeMemoryPool pool = NativeMemoryPool.createListenable(listener); - FileSystemDatasetFactory factory = new FileSystemDatasetFactory(rootAllocator(), - pool, FileFormat.PARQUET, writeSupport.getOutputURI()); + FileSystemDatasetFactory factory = + new FileSystemDatasetFactory( + rootAllocator(), pool, FileFormat.PARQUET, writeSupport.getOutputURI()); ScanOptions options = new ScanOptions(100); long initReservation = reserved.get(); - Assertions.assertThrows(IllegalArgumentException.class, () -> { - collectResultFromFactory(factory, options); - }, errorMessage); + Assertions.assertThrows( + IllegalArgumentException.class, + () -> { + collectResultFromFactory(factory, options); + }, + errorMessage); long reservation = reserved.get(); AutoCloseables.close(pool); long finalReservation = reserved.get(); diff --git a/java/dataset/src/test/java/org/apache/arrow/dataset/substrait/TestAceroSubstraitConsumer.java b/java/dataset/src/test/java/org/apache/arrow/dataset/substrait/TestAceroSubstraitConsumer.java index 0fba72892cd..14248fb420c 100644 --- a/java/dataset/src/test/java/org/apache/arrow/dataset/substrait/TestAceroSubstraitConsumer.java +++ b/java/dataset/src/test/java/org/apache/arrow/dataset/substrait/TestAceroSubstraitConsumer.java @@ -30,7 +30,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; - import org.apache.arrow.dataset.ParquetWriteSupport; import org.apache.arrow.dataset.TestDataset; import org.apache.arrow.dataset.file.FileFormat; @@ -51,33 +50,42 @@ public class TestAceroSubstraitConsumer extends TestDataset { - @ClassRule - public static final TemporaryFolder TMP = new TemporaryFolder(); + @ClassRule public static final TemporaryFolder TMP = new TemporaryFolder(); public static final String AVRO_SCHEMA_USER = "user.avsc"; @Test public void testRunQueryLocalFiles() throws Exception { - //Query: - //SELECT id, name FROM Users - //Isthmus: - //./isthmus-macOS-0.7.0 -c "CREATE TABLE USERS ( id INT NOT NULL, name VARCHAR(150));" "SELECT id, name FROM Users" - //VARCHAR(150) -> is mapping to -> {ARROW:extension:name=varchar, ARROW:extension:metadata=varchar{length:150}} + // Query: + // SELECT id, name FROM Users + // Isthmus: + // ./isthmus-macOS-0.7.0 -c "CREATE TABLE USERS ( id INT NOT NULL, name VARCHAR(150));" "SELECT + // id, name FROM Users" + // VARCHAR(150) -> is mapping to -> {ARROW:extension:name=varchar, + // ARROW:extension:metadata=varchar{length:150}} Map metadataName = new HashMap<>(); metadataName.put("ARROW:extension:name", "varchar"); metadataName.put("ARROW:extension:metadata", "varchar{length:150}"); - final Schema schema = new Schema(Arrays.asList( - Field.nullable("ID", new ArrowType.Int(32, true)), - new Field("NAME", new FieldType(true, new ArrowType.Utf8(), null, metadataName), null) - ), Collections.emptyMap()); - ParquetWriteSupport writeSupport = ParquetWriteSupport - .writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a", 11, "b", 21, "c"); - try (ArrowReader arrowReader = new AceroSubstraitConsumer(rootAllocator()) - .runQuery( - new String(Files.readAllBytes(Paths.get(TestAceroSubstraitConsumer.class.getClassLoader() - .getResource("substrait/local_files_users.json").toURI()))).replace("FILENAME_PLACEHOLDER", - writeSupport.getOutputURI()) - ) - ) { + final Schema schema = + new Schema( + Arrays.asList( + Field.nullable("ID", new ArrowType.Int(32, true)), + new Field( + "NAME", new FieldType(true, new ArrowType.Utf8(), null, metadataName), null)), + Collections.emptyMap()); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a", 11, "b", 21, "c"); + try (ArrowReader arrowReader = + new AceroSubstraitConsumer(rootAllocator()) + .runQuery( + new String( + Files.readAllBytes( + Paths.get( + TestAceroSubstraitConsumer.class + .getClassLoader() + .getResource("substrait/local_files_users.json") + .toURI()))) + .replace("FILENAME_PLACEHOLDER", writeSupport.getOutputURI()))) { assertEquals(schema, arrowReader.getVectorSchemaRoot().getSchema()); int rowcount = 0; while (arrowReader.loadNextBatch()) { @@ -89,31 +97,43 @@ public void testRunQueryLocalFiles() throws Exception { @Test public void testRunQueryNamedTable() throws Exception { - //Query: - //SELECT id, name FROM Users - //Isthmus: - //./isthmus-macOS-0.7.0 -c "CREATE TABLE USERS ( id INT NOT NULL, name VARCHAR(150));" "SELECT id, name FROM Users" - final Schema schema = new Schema(Arrays.asList( - Field.nullable("ID", new ArrowType.Int(32, true)), - Field.nullable("NAME", new ArrowType.Utf8()) - ), Collections.emptyMap()); - ParquetWriteSupport writeSupport = ParquetWriteSupport - .writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a", 11, "b", 21, "c"); + // Query: + // SELECT id, name FROM Users + // Isthmus: + // ./isthmus-macOS-0.7.0 -c "CREATE TABLE USERS ( id INT NOT NULL, name VARCHAR(150));" "SELECT + // id, name FROM Users" + final Schema schema = + new Schema( + Arrays.asList( + Field.nullable("ID", new ArrowType.Int(32, true)), + Field.nullable("NAME", new ArrowType.Utf8())), + Collections.emptyMap()); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a", 11, "b", 21, "c"); ScanOptions options = new ScanOptions(/*batchSize*/ 32768); - try ( - DatasetFactory datasetFactory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + try (DatasetFactory datasetFactory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); Dataset dataset = datasetFactory.finish(); Scanner scanner = dataset.newScan(options); - ArrowReader reader = scanner.scanBatches() - ) { + ArrowReader reader = scanner.scanBatches()) { Map mapTableToArrowReader = new HashMap<>(); mapTableToArrowReader.put("USERS", reader); - try (ArrowReader arrowReader = new AceroSubstraitConsumer(rootAllocator()).runQuery( - new String(Files.readAllBytes(Paths.get(TestAceroSubstraitConsumer.class.getClassLoader() - .getResource("substrait/named_table_users.json").toURI()))), - mapTableToArrowReader - )) { + try (ArrowReader arrowReader = + new AceroSubstraitConsumer(rootAllocator()) + .runQuery( + new String( + Files.readAllBytes( + Paths.get( + TestAceroSubstraitConsumer.class + .getClassLoader() + .getResource("substrait/named_table_users.json") + .toURI()))), + mapTableToArrowReader)) { assertEquals(schema, arrowReader.getVectorSchemaRoot().getSchema()); assertEquals(arrowReader.getVectorSchemaRoot().getSchema(), schema); int rowcount = 0; @@ -127,31 +147,43 @@ public void testRunQueryNamedTable() throws Exception { @Test(expected = RuntimeException.class) public void testRunQueryNamedTableWithException() throws Exception { - //Query: - //SELECT id, name FROM Users - //Isthmus: - //./isthmus-macOS-0.7.0 -c "CREATE TABLE USERS ( id INT NOT NULL, name VARCHAR(150));" "SELECT id, name FROM Users" - final Schema schema = new Schema(Arrays.asList( - Field.nullable("ID", new ArrowType.Int(32, true)), - Field.nullable("NAME", new ArrowType.Utf8()) - ), Collections.emptyMap()); - ParquetWriteSupport writeSupport = ParquetWriteSupport - .writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a", 11, "b", 21, "c"); + // Query: + // SELECT id, name FROM Users + // Isthmus: + // ./isthmus-macOS-0.7.0 -c "CREATE TABLE USERS ( id INT NOT NULL, name VARCHAR(150));" "SELECT + // id, name FROM Users" + final Schema schema = + new Schema( + Arrays.asList( + Field.nullable("ID", new ArrowType.Int(32, true)), + Field.nullable("NAME", new ArrowType.Utf8())), + Collections.emptyMap()); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a", 11, "b", 21, "c"); ScanOptions options = new ScanOptions(/*batchSize*/ 32768); - try ( - DatasetFactory datasetFactory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + try (DatasetFactory datasetFactory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); Dataset dataset = datasetFactory.finish(); Scanner scanner = dataset.newScan(options); - ArrowReader reader = scanner.scanBatches() - ) { + ArrowReader reader = scanner.scanBatches()) { Map mapTableToArrowReader = new HashMap<>(); mapTableToArrowReader.put("USERS_INVALID_MAP", reader); - try (ArrowReader arrowReader = new AceroSubstraitConsumer(rootAllocator()).runQuery( - new String(Files.readAllBytes(Paths.get(TestAceroSubstraitConsumer.class.getClassLoader() - .getResource("substrait/named_table_users.json").toURI()))), - mapTableToArrowReader - )) { + try (ArrowReader arrowReader = + new AceroSubstraitConsumer(rootAllocator()) + .runQuery( + new String( + Files.readAllBytes( + Paths.get( + TestAceroSubstraitConsumer.class + .getClassLoader() + .getResource("substrait/named_table_users.json") + .toURI()))), + mapTableToArrowReader)) { assertEquals(schema, arrowReader.getVectorSchemaRoot().getSchema()); int rowcount = 0; while (arrowReader.loadNextBatch()) { @@ -164,38 +196,43 @@ public void testRunQueryNamedTableWithException() throws Exception { @Test public void testRunBinaryQueryNamedTable() throws Exception { - //Query: - //SELECT id, name FROM Users - //Isthmus: - //./isthmus-macOS-0.7.0 -c "CREATE TABLE USERS ( id INT NOT NULL, name VARCHAR(150));" "SELECT id, name FROM Users" - final Schema schema = new Schema(Arrays.asList( - Field.nullable("ID", new ArrowType.Int(32, true)), - Field.nullable("NAME", new ArrowType.Utf8()) - ), Collections.emptyMap()); + // Query: + // SELECT id, name FROM Users + // Isthmus: + // ./isthmus-macOS-0.7.0 -c "CREATE TABLE USERS ( id INT NOT NULL, name VARCHAR(150));" "SELECT + // id, name FROM Users" + final Schema schema = + new Schema( + Arrays.asList( + Field.nullable("ID", new ArrowType.Int(32, true)), + Field.nullable("NAME", new ArrowType.Utf8())), + Collections.emptyMap()); // Base64.getEncoder().encodeToString(plan.toByteArray()); String binaryPlan = - "Gl8SXQpROk8KBhIECgICAxIvCi0KAgoAEh4KAklECgROQU1FEhIKBCoCEAEKC" + - "LIBBQiWARgBGAI6BwoFVVNFUlMaCBIGCgISACIAGgoSCAoEEgIIASIAEgJJRBIETkFNRQ=="; - ParquetWriteSupport writeSupport = ParquetWriteSupport - .writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a", 11, "b", 21, "c"); + "Gl8SXQpROk8KBhIECgICAxIvCi0KAgoAEh4KAklECgROQU1FEhIKBCoCEAEKC" + + "LIBBQiWARgBGAI6BwoFVVNFUlMaCBIGCgISACIAGgoSCAoEEgIIASIAEgJJRBIETkFNRQ=="; + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a", 11, "b", 21, "c"); ScanOptions options = new ScanOptions(/*batchSize*/ 32768); - try ( - DatasetFactory datasetFactory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + try (DatasetFactory datasetFactory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); Dataset dataset = datasetFactory.finish(); Scanner scanner = dataset.newScan(options); - ArrowReader reader = scanner.scanBatches() - ) { + ArrowReader reader = scanner.scanBatches()) { // map table to reader Map mapTableToArrowReader = new HashMap<>(); mapTableToArrowReader.put("USERS", reader); // get binary plan ByteBuffer substraitPlan = getByteBuffer(binaryPlan); // run query - try (ArrowReader arrowReader = new AceroSubstraitConsumer(rootAllocator()).runQuery( - substraitPlan, - mapTableToArrowReader - )) { + try (ArrowReader arrowReader = + new AceroSubstraitConsumer(rootAllocator()) + .runQuery(substraitPlan, mapTableToArrowReader)) { assertEquals(schema, arrowReader.getVectorSchemaRoot().getSchema()); int rowcount = 0; while (arrowReader.loadNextBatch()) { @@ -208,134 +245,218 @@ public void testRunBinaryQueryNamedTable() throws Exception { @Test public void testRunExtendedExpressionsFilter() throws Exception { - final Schema schema = new Schema(Arrays.asList( - Field.nullable("id", new ArrowType.Int(32, true)), - Field.nullable("name", new ArrowType.Utf8()) - ), null); + final Schema schema = + new Schema( + Arrays.asList( + Field.nullable("id", new ArrowType.Int(32, true)), + Field.nullable("name", new ArrowType.Utf8())), + null); // Substrait Extended Expression: Filter: // Expression 01: WHERE ID < 20 - String base64EncodedSubstraitFilter = "Ch4IARIaL2Z1bmN0aW9uc19jb21wYXJpc29uLnlhbWwSEhoQCAIQAhoKbHQ6YW55X2F" + - "ueRo3ChwaGggCGgQKAhABIggaBhIECgISACIGGgQKAigUGhdmaWx0ZXJfaWRfbG93ZXJfdGhhbl8yMCIaCgJJRAoETkFNRRIOCgQqAhA" + - "BCgRiAhABGAI="; + String base64EncodedSubstraitFilter = + "Ch4IARIaL2Z1bmN0aW9uc19jb21wYXJpc29uLnlhbWwSEhoQCAIQAhoKbHQ6YW55X2F" + + "ueRo3ChwaGggCGgQKAhABIggaBhIECgISACIGGgQKAigUGhdmaWx0ZXJfaWRfbG93ZXJfdGhhbl8yMCIaCgJJRAoETkFNRRIOCgQqAhA" + + "BCgRiAhABGAI="; ByteBuffer substraitExpressionFilter = getByteBuffer(base64EncodedSubstraitFilter); - ParquetWriteSupport writeSupport = ParquetWriteSupport - .writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 19, "value_19", 1, "value_1", - 11, "value_11", 21, "value_21", 45, "value_45"); - ScanOptions options = new ScanOptions.Builder(/*batchSize*/ 32768) - .columns(Optional.empty()) - .substraitFilter(substraitExpressionFilter) - .build(); - try ( - DatasetFactory datasetFactory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, + TMP.newFolder(), + 19, + "value_19", + 1, + "value_1", + 11, + "value_11", + 21, + "value_21", + 45, + "value_45"); + ScanOptions options = + new ScanOptions.Builder(/*batchSize*/ 32768) + .columns(Optional.empty()) + .substraitFilter(substraitExpressionFilter) + .build(); + try (DatasetFactory datasetFactory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); Dataset dataset = datasetFactory.finish(); Scanner scanner = dataset.newScan(options); - ArrowReader reader = scanner.scanBatches() - ) { + ArrowReader reader = scanner.scanBatches()) { assertEquals(schema.getFields(), reader.getVectorSchemaRoot().getSchema().getFields()); int rowcount = 0; while (reader.loadNextBatch()) { rowcount += reader.getVectorSchemaRoot().getRowCount(); assertTrue(reader.getVectorSchemaRoot().getVector("id").toString().equals("[19, 1, 11]")); - assertTrue(reader.getVectorSchemaRoot().getVector("name").toString() - .equals("[value_19, value_1, value_11]")); + assertTrue( + reader + .getVectorSchemaRoot() + .getVector("name") + .toString() + .equals("[value_19, value_1, value_11]")); } assertEquals(3, rowcount); } } @Test - public void testRunExtendedExpressionsFilterWithProjectionsInsteadOfFilterException() throws Exception { - final Schema schema = new Schema(Arrays.asList( - Field.nullable("id", new ArrowType.Int(32, true)), - Field.nullable("name", new ArrowType.Utf8()) - ), null); + public void testRunExtendedExpressionsFilterWithProjectionsInsteadOfFilterException() + throws Exception { + final Schema schema = + new Schema( + Arrays.asList( + Field.nullable("id", new ArrowType.Int(32, true)), + Field.nullable("name", new ArrowType.Utf8())), + null); // Substrait Extended Expression: Project New Column: // Expression ADD: id + 2 // Expression CONCAT: name + '-' + name - String base64EncodedSubstraitFilter = "Ch4IARIaL2Z1bmN0aW9uc19hcml0aG1ldGljLnlhbWwSERoPCAEaC2FkZDppM" + - "zJfaTMyEhQaEggCEAEaDGNvbmNhdDp2Y2hhchoxChoaGBoEKgIQASIIGgYSBAoCEgAiBhoECgIoAhoTYWRkX3R3b190b19jb2x1" + - "bW5fYRpGCi0aKwgBGgRiAhABIgoaCBIGCgQSAggBIgkaBwoFYgMgLSAiChoIEgYKBBICCAEaFWNvbmNhdF9jb2x1bW5fYV9hbmR" + - "fYiIaCgJJRAoETkFNRRIOCgQqAhABCgRiAhABGAI="; + String base64EncodedSubstraitFilter = + "Ch4IARIaL2Z1bmN0aW9uc19hcml0aG1ldGljLnlhbWwSERoPCAEaC2FkZDppM" + + "zJfaTMyEhQaEggCEAEaDGNvbmNhdDp2Y2hhchoxChoaGBoEKgIQASIIGgYSBAoCEgAiBhoECgIoAhoTYWRkX3R3b190b19jb2x1" + + "bW5fYRpGCi0aKwgBGgRiAhABIgoaCBIGCgQSAggBIgkaBwoFYgMgLSAiChoIEgYKBBICCAEaFWNvbmNhdF9jb2x1bW5fYV9hbmR" + + "fYiIaCgJJRAoETkFNRRIOCgQqAhABCgRiAhABGAI="; ByteBuffer substraitExpressionFilter = getByteBuffer(base64EncodedSubstraitFilter); - ParquetWriteSupport writeSupport = ParquetWriteSupport - .writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 19, "value_19", 1, "value_1", - 11, "value_11", 21, "value_21", 45, "value_45"); - ScanOptions options = new ScanOptions.Builder(/*batchSize*/ 32768) - .columns(Optional.empty()) - .substraitFilter(substraitExpressionFilter) - .build(); - try ( - DatasetFactory datasetFactory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); - Dataset dataset = datasetFactory.finish() - ) { + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, + TMP.newFolder(), + 19, + "value_19", + 1, + "value_1", + 11, + "value_11", + 21, + "value_21", + 45, + "value_45"); + ScanOptions options = + new ScanOptions.Builder(/*batchSize*/ 32768) + .columns(Optional.empty()) + .substraitFilter(substraitExpressionFilter) + .build(); + try (DatasetFactory datasetFactory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); + Dataset dataset = datasetFactory.finish()) { Exception e = assertThrows(RuntimeException.class, () -> dataset.newScan(options)); - assertTrue(e.getMessage().startsWith("There is no filter expression in the expression provided")); + assertTrue( + e.getMessage().startsWith("There is no filter expression in the expression provided")); } } @Test public void testRunExtendedExpressionsFilterWithEmptyFilterException() throws Exception { - final Schema schema = new Schema(Arrays.asList( - Field.nullable("id", new ArrowType.Int(32, true)), - Field.nullable("name", new ArrowType.Utf8()) - ), null); + final Schema schema = + new Schema( + Arrays.asList( + Field.nullable("id", new ArrowType.Int(32, true)), + Field.nullable("name", new ArrowType.Utf8())), + null); String base64EncodedSubstraitFilter = ""; ByteBuffer substraitExpressionFilter = getByteBuffer(base64EncodedSubstraitFilter); - ParquetWriteSupport writeSupport = ParquetWriteSupport - .writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 19, "value_19", 1, "value_1", - 11, "value_11", 21, "value_21", 45, "value_45"); - ScanOptions options = new ScanOptions.Builder(/*batchSize*/ 32768) - .columns(Optional.empty()) - .substraitFilter(substraitExpressionFilter) - .build(); - try ( - DatasetFactory datasetFactory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); - Dataset dataset = datasetFactory.finish() - ) { + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, + TMP.newFolder(), + 19, + "value_19", + 1, + "value_1", + 11, + "value_11", + 21, + "value_21", + 45, + "value_45"); + ScanOptions options = + new ScanOptions.Builder(/*batchSize*/ 32768) + .columns(Optional.empty()) + .substraitFilter(substraitExpressionFilter) + .build(); + try (DatasetFactory datasetFactory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); + Dataset dataset = datasetFactory.finish()) { Exception e = assertThrows(RuntimeException.class, () -> dataset.newScan(options)); - assertTrue(e.getMessage().contains("no anonymous struct type was provided to which names could be attached.")); + assertTrue( + e.getMessage() + .contains("no anonymous struct type was provided to which names could be attached.")); } } @Test public void testRunExtendedExpressionsProjection() throws Exception { - final Schema schema = new Schema(Arrays.asList( - Field.nullable("add_two_to_column_a", new ArrowType.Int(32, true)), - Field.nullable("concat_column_a_and_b", new ArrowType.Utf8()) - ), null); + final Schema schema = + new Schema( + Arrays.asList( + Field.nullable("add_two_to_column_a", new ArrowType.Int(32, true)), + Field.nullable("concat_column_a_and_b", new ArrowType.Utf8())), + null); // Substrait Extended Expression: Project New Column: // Expression ADD: id + 2 // Expression CONCAT: name + '-' + name - String binarySubstraitExpressionProject = "Ch4IARIaL2Z1bmN0aW9uc19hcml0aG1ldGljLnlhbWwSERoPCAEaC2FkZDppM" + - "zJfaTMyEhQaEggCEAEaDGNvbmNhdDp2Y2hhchoxChoaGBoEKgIQASIIGgYSBAoCEgAiBhoECgIoAhoTYWRkX3R3b190b19jb2x1" + - "bW5fYRpGCi0aKwgBGgRiAhABIgoaCBIGCgQSAggBIgkaBwoFYgMgLSAiChoIEgYKBBICCAEaFWNvbmNhdF9jb2x1bW5fYV9hbmR" + - "fYiIaCgJJRAoETkFNRRIOCgQqAhABCgRiAhABGAI="; + String binarySubstraitExpressionProject = + "Ch4IARIaL2Z1bmN0aW9uc19hcml0aG1ldGljLnlhbWwSERoPCAEaC2FkZDppM" + + "zJfaTMyEhQaEggCEAEaDGNvbmNhdDp2Y2hhchoxChoaGBoEKgIQASIIGgYSBAoCEgAiBhoECgIoAhoTYWRkX3R3b190b19jb2x1" + + "bW5fYRpGCi0aKwgBGgRiAhABIgoaCBIGCgQSAggBIgkaBwoFYgMgLSAiChoIEgYKBBICCAEaFWNvbmNhdF9jb2x1bW5fYV9hbmR" + + "fYiIaCgJJRAoETkFNRRIOCgQqAhABCgRiAhABGAI="; ByteBuffer substraitExpressionProject = getByteBuffer(binarySubstraitExpressionProject); - ParquetWriteSupport writeSupport = ParquetWriteSupport - .writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 19, "value_19", 1, "value_1", - 11, "value_11", 21, "value_21", 45, "value_45"); - ScanOptions options = new ScanOptions.Builder(/*batchSize*/ 32768) - .columns(Optional.empty()) - .substraitProjection(substraitExpressionProject) - .build(); - try ( - DatasetFactory datasetFactory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, + TMP.newFolder(), + 19, + "value_19", + 1, + "value_1", + 11, + "value_11", + 21, + "value_21", + 45, + "value_45"); + ScanOptions options = + new ScanOptions.Builder(/*batchSize*/ 32768) + .columns(Optional.empty()) + .substraitProjection(substraitExpressionProject) + .build(); + try (DatasetFactory datasetFactory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); Dataset dataset = datasetFactory.finish(); Scanner scanner = dataset.newScan(options); - ArrowReader reader = scanner.scanBatches() - ) { + ArrowReader reader = scanner.scanBatches()) { assertEquals(schema.getFields(), reader.getVectorSchemaRoot().getSchema().getFields()); int rowcount = 0; while (reader.loadNextBatch()) { - assertTrue(reader.getVectorSchemaRoot().getVector("add_two_to_column_a").toString() - .equals("[21, 3, 13, 23, 47]")); - assertTrue(reader.getVectorSchemaRoot().getVector("concat_column_a_and_b").toString() - .equals("[value_19 - value_19, value_1 - value_1, value_11 - value_11, " + - "value_21 - value_21, value_45 - value_45]")); + assertTrue( + reader + .getVectorSchemaRoot() + .getVector("add_two_to_column_a") + .toString() + .equals("[21, 3, 13, 23, 47]")); + assertTrue( + reader + .getVectorSchemaRoot() + .getVector("concat_column_a_and_b") + .toString() + .equals( + "[value_19 - value_19, value_1 - value_1, value_11 - value_11, " + + "value_21 - value_21, value_45 - value_45]")); rowcount += reader.getVectorSchemaRoot().getRowCount(); } assertEquals(5, rowcount); @@ -343,35 +464,55 @@ public void testRunExtendedExpressionsProjection() throws Exception { } @Test - public void testRunExtendedExpressionsProjectionWithFilterInsteadOfProjectionException() throws Exception { - final Schema schema = new Schema(Arrays.asList( - Field.nullable("filter_id_lower_than_20", new ArrowType.Bool()) - ), null); + public void testRunExtendedExpressionsProjectionWithFilterInsteadOfProjectionException() + throws Exception { + final Schema schema = + new Schema( + Arrays.asList(Field.nullable("filter_id_lower_than_20", new ArrowType.Bool())), null); // Substrait Extended Expression: Filter: // Expression 01: WHERE ID < 20 - String binarySubstraitExpressionFilter = "Ch4IARIaL2Z1bmN0aW9uc19jb21wYXJpc29uLnlhbWwSEhoQCAIQAhoKbHQ6YW55X2F" + - "ueRo3ChwaGggCGgQKAhABIggaBhIECgISACIGGgQKAigUGhdmaWx0ZXJfaWRfbG93ZXJfdGhhbl8yMCIaCgJJRAoETkFNRRIOCgQqAhA" + - "BCgRiAhABGAI="; + String binarySubstraitExpressionFilter = + "Ch4IARIaL2Z1bmN0aW9uc19jb21wYXJpc29uLnlhbWwSEhoQCAIQAhoKbHQ6YW55X2F" + + "ueRo3ChwaGggCGgQKAhABIggaBhIECgISACIGGgQKAigUGhdmaWx0ZXJfaWRfbG93ZXJfdGhhbl8yMCIaCgJJRAoETkFNRRIOCgQqAhA" + + "BCgRiAhABGAI="; ByteBuffer substraitExpressionFilter = getByteBuffer(binarySubstraitExpressionFilter); - ParquetWriteSupport writeSupport = ParquetWriteSupport - .writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 19, "value_19", 1, "value_1", - 11, "value_11", 21, "value_21", 45, "value_45"); - ScanOptions options = new ScanOptions.Builder(/*batchSize*/ 32768) - .columns(Optional.empty()) - .substraitProjection(substraitExpressionFilter) - .build(); - try ( - DatasetFactory datasetFactory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, + TMP.newFolder(), + 19, + "value_19", + 1, + "value_1", + 11, + "value_11", + 21, + "value_21", + 45, + "value_45"); + ScanOptions options = + new ScanOptions.Builder(/*batchSize*/ 32768) + .columns(Optional.empty()) + .substraitProjection(substraitExpressionFilter) + .build(); + try (DatasetFactory datasetFactory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); Dataset dataset = datasetFactory.finish(); Scanner scanner = dataset.newScan(options); - ArrowReader reader = scanner.scanBatches() - ) { + ArrowReader reader = scanner.scanBatches()) { assertEquals(schema.getFields(), reader.getVectorSchemaRoot().getSchema().getFields()); int rowcount = 0; while (reader.loadNextBatch()) { - assertTrue(reader.getVectorSchemaRoot().getVector("filter_id_lower_than_20").toString() - .equals("[true, true, true, false, false]")); + assertTrue( + reader + .getVectorSchemaRoot() + .getVector("filter_id_lower_than_20") + .toString() + .equals("[true, true, true, false, false]")); rowcount += reader.getVectorSchemaRoot().getRowCount(); } assertEquals(5, rowcount); @@ -380,71 +521,115 @@ public void testRunExtendedExpressionsProjectionWithFilterInsteadOfProjectionExc @Test public void testRunExtendedExpressionsProjectionWithEmptyProjectionException() throws Exception { - final Schema schema = new Schema(Arrays.asList( - Field.nullable("id", new ArrowType.Int(32, true)), - Field.nullable("name", new ArrowType.Utf8()) - ), null); + final Schema schema = + new Schema( + Arrays.asList( + Field.nullable("id", new ArrowType.Int(32, true)), + Field.nullable("name", new ArrowType.Utf8())), + null); String base64EncodedSubstraitFilter = ""; ByteBuffer substraitExpressionProjection = getByteBuffer(base64EncodedSubstraitFilter); - ParquetWriteSupport writeSupport = ParquetWriteSupport - .writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 19, "value_19", 1, "value_1", - 11, "value_11", 21, "value_21", 45, "value_45"); - ScanOptions options = new ScanOptions.Builder(/*batchSize*/ 32768) - .columns(Optional.empty()) - .substraitProjection(substraitExpressionProjection) - .build(); - try ( - DatasetFactory datasetFactory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); - Dataset dataset = datasetFactory.finish() - ) { + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, + TMP.newFolder(), + 19, + "value_19", + 1, + "value_1", + 11, + "value_11", + 21, + "value_21", + 45, + "value_45"); + ScanOptions options = + new ScanOptions.Builder(/*batchSize*/ 32768) + .columns(Optional.empty()) + .substraitProjection(substraitExpressionProjection) + .build(); + try (DatasetFactory datasetFactory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); + Dataset dataset = datasetFactory.finish()) { Exception e = assertThrows(RuntimeException.class, () -> dataset.newScan(options)); - assertTrue(e.getMessage().contains("no anonymous struct type was provided to which names could be attached.")); + assertTrue( + e.getMessage() + .contains("no anonymous struct type was provided to which names could be attached.")); } } @Test public void testRunExtendedExpressionsProjectAndFilter() throws Exception { - final Schema schema = new Schema(Arrays.asList( - Field.nullable("add_two_to_column_a", new ArrowType.Int(32, true)), - Field.nullable("concat_column_a_and_b", new ArrowType.Utf8()) - ), null); + final Schema schema = + new Schema( + Arrays.asList( + Field.nullable("add_two_to_column_a", new ArrowType.Int(32, true)), + Field.nullable("concat_column_a_and_b", new ArrowType.Utf8())), + null); // Substrait Extended Expression: Project New Column: // Expression ADD: id + 2 // Expression CONCAT: name + '-' + name - String binarySubstraitExpressionProject = "Ch4IARIaL2Z1bmN0aW9uc19hcml0aG1ldGljLnlhbWwSERoPCAEaC2FkZDppM" + - "zJfaTMyEhQaEggCEAEaDGNvbmNhdDp2Y2hhchoxChoaGBoEKgIQASIIGgYSBAoCEgAiBhoECgIoAhoTYWRkX3R3b190b19jb2x1" + - "bW5fYRpGCi0aKwgBGgRiAhABIgoaCBIGCgQSAggBIgkaBwoFYgMgLSAiChoIEgYKBBICCAEaFWNvbmNhdF9jb2x1bW5fYV9hbmR" + - "fYiIaCgJJRAoETkFNRRIOCgQqAhABCgRiAhABGAI="; + String binarySubstraitExpressionProject = + "Ch4IARIaL2Z1bmN0aW9uc19hcml0aG1ldGljLnlhbWwSERoPCAEaC2FkZDppM" + + "zJfaTMyEhQaEggCEAEaDGNvbmNhdDp2Y2hhchoxChoaGBoEKgIQASIIGgYSBAoCEgAiBhoECgIoAhoTYWRkX3R3b190b19jb2x1" + + "bW5fYRpGCi0aKwgBGgRiAhABIgoaCBIGCgQSAggBIgkaBwoFYgMgLSAiChoIEgYKBBICCAEaFWNvbmNhdF9jb2x1bW5fYV9hbmR" + + "fYiIaCgJJRAoETkFNRRIOCgQqAhABCgRiAhABGAI="; ByteBuffer substraitExpressionProject = getByteBuffer(binarySubstraitExpressionProject); // Substrait Extended Expression: Filter: // Expression 01: WHERE ID < 20 - String base64EncodedSubstraitFilter = "Ch4IARIaL2Z1bmN0aW9uc19jb21wYXJpc29uLnlhbWwSEhoQCAIQAhoKbHQ6YW55X2F" + - "ueRo3ChwaGggCGgQKAhABIggaBhIECgISACIGGgQKAigUGhdmaWx0ZXJfaWRfbG93ZXJfdGhhbl8yMCIaCgJJRAoETkFNRRIOCgQqAhA" + - "BCgRiAhABGAI="; + String base64EncodedSubstraitFilter = + "Ch4IARIaL2Z1bmN0aW9uc19jb21wYXJpc29uLnlhbWwSEhoQCAIQAhoKbHQ6YW55X2F" + + "ueRo3ChwaGggCGgQKAhABIggaBhIECgISACIGGgQKAigUGhdmaWx0ZXJfaWRfbG93ZXJfdGhhbl8yMCIaCgJJRAoETkFNRRIOCgQqAhA" + + "BCgRiAhABGAI="; ByteBuffer substraitExpressionFilter = getByteBuffer(base64EncodedSubstraitFilter); - ParquetWriteSupport writeSupport = ParquetWriteSupport - .writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 19, "value_19", 1, "value_1", - 11, "value_11", 21, "value_21", 45, "value_45"); - ScanOptions options = new ScanOptions.Builder(/*batchSize*/ 32768) - .columns(Optional.empty()) - .substraitProjection(substraitExpressionProject) - .substraitFilter(substraitExpressionFilter) - .build(); - try ( - DatasetFactory datasetFactory = new FileSystemDatasetFactory(rootAllocator(), NativeMemoryPool.getDefault(), - FileFormat.PARQUET, writeSupport.getOutputURI()); + ParquetWriteSupport writeSupport = + ParquetWriteSupport.writeTempFile( + AVRO_SCHEMA_USER, + TMP.newFolder(), + 19, + "value_19", + 1, + "value_1", + 11, + "value_11", + 21, + "value_21", + 45, + "value_45"); + ScanOptions options = + new ScanOptions.Builder(/*batchSize*/ 32768) + .columns(Optional.empty()) + .substraitProjection(substraitExpressionProject) + .substraitFilter(substraitExpressionFilter) + .build(); + try (DatasetFactory datasetFactory = + new FileSystemDatasetFactory( + rootAllocator(), + NativeMemoryPool.getDefault(), + FileFormat.PARQUET, + writeSupport.getOutputURI()); Dataset dataset = datasetFactory.finish(); Scanner scanner = dataset.newScan(options); - ArrowReader reader = scanner.scanBatches() - ) { + ArrowReader reader = scanner.scanBatches()) { assertEquals(schema.getFields(), reader.getVectorSchemaRoot().getSchema().getFields()); int rowcount = 0; while (reader.loadNextBatch()) { - assertTrue(reader.getVectorSchemaRoot().getVector("add_two_to_column_a").toString() - .equals("[21, 3, 13]")); - assertTrue(reader.getVectorSchemaRoot().getVector("concat_column_a_and_b").toString() - .equals("[value_19 - value_19, value_1 - value_1, value_11 - value_11]")); + assertTrue( + reader + .getVectorSchemaRoot() + .getVector("add_two_to_column_a") + .toString() + .equals("[21, 3, 13]")); + assertTrue( + reader + .getVectorSchemaRoot() + .getVector("concat_column_a_and_b") + .toString() + .equals("[value_19 - value_19, value_1 - value_1, value_11 - value_11]")); rowcount += reader.getVectorSchemaRoot().getRowCount(); } assertEquals(3, rowcount); diff --git a/java/dev/checkstyle/checkstyle.xml b/java/dev/checkstyle/checkstyle.xml deleted file mode 100644 index c27f382ddda..00000000000 --- a/java/dev/checkstyle/checkstyle.xml +++ /dev/null @@ -1,280 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/java/dev/checkstyle/suppressions.xml b/java/dev/checkstyle/suppressions.xml deleted file mode 100644 index 585985bf32d..00000000000 --- a/java/dev/checkstyle/suppressions.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/java/flight/flight-core/pom.xml b/java/flight/flight-core/pom.xml index 0346172f610..afc4d13f478 100644 --- a/java/flight/flight-core/pom.xml +++ b/java/flight/flight-core/pom.xml @@ -1,4 +1,4 @@ - + + --> 3.2.4 shade-main - package shade + package true shaded @@ -188,16 +181,16 @@ - + shade-ext - package shade + package true shaded-ext @@ -219,7 +212,7 @@ - + @@ -238,25 +231,25 @@ src + + compile + compile-custom + ${basedir}/../../../format/ ${project.build.directory}/generated-sources/protobuf + + + test compile compile-custom - - - test ${basedir}/src/test/protobuf ${project.build.directory}/generated-test-sources//protobuf - - compile - compile-custom - @@ -267,10 +260,10 @@ analyze - verify analyze-only + verify io.netty:netty-tcnative-boringssl-static:* @@ -279,17 +272,18 @@ - + + org.codehaus.mojo build-helper-maven-plugin 1.9.1 add-generated-sources-to-classpath - generate-sources add-source + generate-sources ${project.build.directory}/generated-sources/protobuf @@ -309,14 +303,21 @@ make-assembly - package single + package + + + kr.motd.maven + os-maven-plugin + 1.7.0 + + diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Action.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Action.java index 524ffcab9ad..110cd151f18 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Action.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Action.java @@ -17,9 +17,8 @@ package org.apache.arrow.flight; -import org.apache.arrow.flight.impl.Flight; - import com.google.protobuf.ByteString; +import org.apache.arrow.flight.impl.Flight; /** * An opaque action for the service to perform. diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ActionType.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ActionType.java index d89365612f3..524864099d4 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ActionType.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ActionType.java @@ -19,9 +19,7 @@ import org.apache.arrow.flight.impl.Flight; -/** - * POJO wrapper around protocol specifics for Flight actions. - */ +/** POJO wrapper around protocol specifics for Flight actions. */ public class ActionType { private final String type; private final String description; @@ -38,9 +36,7 @@ public ActionType(String type, String description) { this.description = description; } - /** - * Constructs a new instance from the corresponding protocol buffer object. - */ + /** Constructs a new instance from the corresponding protocol buffer object. */ ActionType(Flight.ActionType type) { this.type = type.getType(); this.description = type.getDescription(); @@ -50,21 +46,13 @@ public String getType() { return type; } - /** - * Converts the POJO to the corresponding protocol buffer type. - */ + /** Converts the POJO to the corresponding protocol buffer type. */ Flight.ActionType toProtocol() { - return Flight.ActionType.newBuilder() - .setType(type) - .setDescription(description) - .build(); + return Flight.ActionType.newBuilder().setType(type).setDescription(description).build(); } @Override public String toString() { - return "ActionType{" + - "type='" + type + '\'' + - ", description='" + description + '\'' + - '}'; + return "ActionType{" + "type='" + type + '\'' + ", description='" + description + '\'' + '}'; } } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ArrowMessage.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ArrowMessage.java index 46cb282e9f3..939783ce138 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ArrowMessage.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ArrowMessage.java @@ -17,6 +17,21 @@ package org.apache.arrow.flight; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; +import com.google.common.io.ByteStreams; +import com.google.protobuf.ByteString; +import com.google.protobuf.CodedInputStream; +import com.google.protobuf.CodedOutputStream; +import com.google.protobuf.WireFormat; +import io.grpc.Drainable; +import io.grpc.MethodDescriptor.Marshaller; +import io.grpc.protobuf.ProtoUtils; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufInputStream; +import io.netty.buffer.CompositeByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.buffer.UnpooledByteBufAllocator; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -26,7 +41,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; - import org.apache.arrow.flight.grpc.AddWritableBuffer; import org.apache.arrow.flight.grpc.GetReadableBuffer; import org.apache.arrow.flight.impl.Flight.FlightData; @@ -43,26 +57,7 @@ import org.apache.arrow.vector.types.MetadataVersion; import org.apache.arrow.vector.types.pojo.Schema; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; -import com.google.common.io.ByteStreams; -import com.google.protobuf.ByteString; -import com.google.protobuf.CodedInputStream; -import com.google.protobuf.CodedOutputStream; -import com.google.protobuf.WireFormat; - -import io.grpc.Drainable; -import io.grpc.MethodDescriptor.Marshaller; -import io.grpc.protobuf.ProtoUtils; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufInputStream; -import io.netty.buffer.CompositeByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.buffer.UnpooledByteBufAllocator; - -/** - * The in-memory representation of FlightData used to manage a stream of Arrow messages. - */ +/** The in-memory representation of FlightData used to manage a stream of Arrow messages. */ class ArrowMessage implements AutoCloseable { // If true, deserialize Arrow data by giving Arrow a reference to the underlying gRPC buffer @@ -97,7 +92,10 @@ class ArrowMessage implements AutoCloseable { private static final Marshaller NO_BODY_MARSHALLER = ProtoUtils.marshaller(FlightData.getDefaultInstance()); - /** Get the application-specific metadata in this message. The ArrowMessage retains ownership of the buffer. */ + /** + * Get the application-specific metadata in this message. The ArrowMessage retains ownership of + * the buffer. + */ public ArrowBuf getApplicationMetadata() { return appMetadata; } @@ -108,34 +106,37 @@ public enum HeaderType { SCHEMA, DICTIONARY_BATCH, RECORD_BATCH, - TENSOR - ; + TENSOR; public static HeaderType getHeader(byte b) { switch (b) { - case 0: return NONE; - case 1: return SCHEMA; - case 2: return DICTIONARY_BATCH; - case 3: return RECORD_BATCH; - case 4: return TENSOR; + case 0: + return NONE; + case 1: + return SCHEMA; + case 2: + return DICTIONARY_BATCH; + case 3: + return RECORD_BATCH; + case 4: + return TENSOR; default: throw new UnsupportedOperationException("unknown type: " + b); } } - } // Pre-allocated buffers for padding serialized ArrowMessages. - private static final List PADDING_BUFFERS = Arrays.asList( - null, - Unpooled.copiedBuffer(new byte[] { 0 }), - Unpooled.copiedBuffer(new byte[] { 0, 0 }), - Unpooled.copiedBuffer(new byte[] { 0, 0, 0 }), - Unpooled.copiedBuffer(new byte[] { 0, 0, 0, 0 }), - Unpooled.copiedBuffer(new byte[] { 0, 0, 0, 0, 0 }), - Unpooled.copiedBuffer(new byte[] { 0, 0, 0, 0, 0, 0 }), - Unpooled.copiedBuffer(new byte[] { 0, 0, 0, 0, 0, 0, 0 }) - ); + private static final List PADDING_BUFFERS = + Arrays.asList( + null, + Unpooled.copiedBuffer(new byte[] {0}), + Unpooled.copiedBuffer(new byte[] {0, 0}), + Unpooled.copiedBuffer(new byte[] {0, 0, 0}), + Unpooled.copiedBuffer(new byte[] {0, 0, 0, 0}), + Unpooled.copiedBuffer(new byte[] {0, 0, 0, 0, 0}), + Unpooled.copiedBuffer(new byte[] {0, 0, 0, 0, 0, 0}), + Unpooled.copiedBuffer(new byte[] {0, 0, 0, 0, 0, 0, 0})); private final IpcOption writeOption; private final FlightDescriptor descriptor; @@ -147,8 +148,8 @@ public static HeaderType getHeader(byte b) { public ArrowMessage(FlightDescriptor descriptor, Schema schema, IpcOption option) { this.writeOption = option; ByteBuffer serializedMessage = MessageSerializer.serializeMetadata(schema, writeOption); - this.message = MessageMetadataResult.create(serializedMessage.slice(), - serializedMessage.remaining()); + this.message = + MessageMetadataResult.create(serializedMessage.slice(), serializedMessage.remaining()); bufs = ImmutableList.of(); this.descriptor = descriptor; this.appMetadata = null; @@ -157,14 +158,17 @@ public ArrowMessage(FlightDescriptor descriptor, Schema schema, IpcOption option /** * Create an ArrowMessage from a record batch and app metadata. + * * @param batch The record batch. * @param appMetadata The app metadata. May be null. Takes ownership of the buffer otherwise. * @param tryZeroCopy Whether to enable the zero-copy optimization. */ - public ArrowMessage(ArrowRecordBatch batch, ArrowBuf appMetadata, boolean tryZeroCopy, IpcOption option) { + public ArrowMessage( + ArrowRecordBatch batch, ArrowBuf appMetadata, boolean tryZeroCopy, IpcOption option) { this.writeOption = option; ByteBuffer serializedMessage = MessageSerializer.serializeMetadata(batch, writeOption); - this.message = MessageMetadataResult.create(serializedMessage.slice(), serializedMessage.remaining()); + this.message = + MessageMetadataResult.create(serializedMessage.slice(), serializedMessage.remaining()); this.bufs = ImmutableList.copyOf(batch.getBuffers()); this.descriptor = null; this.appMetadata = appMetadata; @@ -186,6 +190,7 @@ public ArrowMessage(ArrowDictionaryBatch batch, IpcOption option) { /** * Create an ArrowMessage containing only application metadata. + * * @param appMetadata The application-provided metadata buffer. */ public ArrowMessage(ArrowBuf appMetadata) { @@ -208,13 +213,18 @@ public ArrowMessage(FlightDescriptor descriptor) { this.tryZeroCopyWrite = false; } - private ArrowMessage(FlightDescriptor descriptor, MessageMetadataResult message, ArrowBuf appMetadata, - ArrowBuf buf) { + private ArrowMessage( + FlightDescriptor descriptor, + MessageMetadataResult message, + ArrowBuf appMetadata, + ArrowBuf buf) { // No need to take IpcOption as this is used for deserialized ArrowMessage coming from the wire. - this.writeOption = message != null ? - // avoid writing legacy ipc format by default - new IpcOption(false, MetadataVersion.fromFlatbufID(message.getMessage().version())) : - IpcOption.DEFAULT; + this.writeOption = + message != null + ? + // avoid writing legacy ipc format by default + new IpcOption(false, MetadataVersion.fromFlatbufID(message.getMessage().version())) + : IpcOption.DEFAULT; this.message = message; this.descriptor = descriptor; this.appMetadata = appMetadata; @@ -245,7 +255,8 @@ public Schema asSchema() { } public ArrowRecordBatch asRecordBatch() throws IOException { - Preconditions.checkArgument(bufs.size() == 1, "A batch can only be consumed if it contains a single ArrowBuf."); + Preconditions.checkArgument( + bufs.size() == 1, "A batch can only be consumed if it contains a single ArrowBuf."); Preconditions.checkArgument(getMessageType() == HeaderType.RECORD_BATCH); ArrowBuf underlying = bufs.get(0); @@ -255,7 +266,8 @@ public ArrowRecordBatch asRecordBatch() throws IOException { } public ArrowDictionaryBatch asDictionaryBatch() throws IOException { - Preconditions.checkArgument(bufs.size() == 1, "A batch can only be consumed if it contains a single ArrowBuf."); + Preconditions.checkArgument( + bufs.size() == 1, "A batch can only be consumed if it contains a single ArrowBuf."); Preconditions.checkArgument(getMessageType() == HeaderType.DICTIONARY_BATCH); ArrowBuf underlying = bufs.get(0); // Retain a reference to keep the batch alive when the message is closed @@ -278,27 +290,29 @@ private static ArrowMessage frame(BufferAllocator allocator, final InputStream s while (stream.available() > 0) { int tag = readRawVarint32(stream); switch (tag) { - - case DESCRIPTOR_TAG: { - int size = readRawVarint32(stream); - byte[] bytes = new byte[size]; - ByteStreams.readFully(stream, bytes); - descriptor = FlightDescriptor.parseFrom(bytes); - break; - } - case HEADER_TAG: { - int size = readRawVarint32(stream); - byte[] bytes = new byte[size]; - ByteStreams.readFully(stream, bytes); - header = MessageMetadataResult.create(ByteBuffer.wrap(bytes), size); - break; - } - case APP_METADATA_TAG: { - int size = readRawVarint32(stream); - appMetadata = allocator.buffer(size); - GetReadableBuffer.readIntoBuffer(stream, appMetadata, size, ENABLE_ZERO_COPY_READ); - break; - } + case DESCRIPTOR_TAG: + { + int size = readRawVarint32(stream); + byte[] bytes = new byte[size]; + ByteStreams.readFully(stream, bytes); + descriptor = FlightDescriptor.parseFrom(bytes); + break; + } + case HEADER_TAG: + { + int size = readRawVarint32(stream); + byte[] bytes = new byte[size]; + ByteStreams.readFully(stream, bytes); + header = MessageMetadataResult.create(ByteBuffer.wrap(bytes), size); + break; + } + case APP_METADATA_TAG: + { + int size = readRawVarint32(stream); + appMetadata = allocator.buffer(size); + GetReadableBuffer.readIntoBuffer(stream, appMetadata, size, ENABLE_ZERO_COPY_READ); + break; + } case BODY_TAG: if (body != null) { // only read last body. @@ -314,10 +328,13 @@ private static ArrowMessage frame(BufferAllocator allocator, final InputStream s // ignore unknown fields. } } - // Protobuf implementations can omit empty fields, such as body; for some message types, like RecordBatch, - // this will fail later as we still expect an empty buffer. In those cases only, fill in an empty buffer here - + // Protobuf implementations can omit empty fields, such as body; for some message types, like + // RecordBatch, + // this will fail later as we still expect an empty buffer. In those cases only, fill in an + // empty buffer here - // in other cases, like Schema, having an unexpected empty buffer will also cause failures. - // We don't fill in defaults for fields like header, for which there is no reasonable default, or for appMetadata + // We don't fill in defaults for fields like header, for which there is no reasonable default, + // or for appMetadata // or descriptor, which are intended to be empty in some cases. if (header != null) { switch (HeaderType.getHeader(header.headerType())) { @@ -346,7 +363,6 @@ private static ArrowMessage frame(BufferAllocator allocator, final InputStream s } catch (Exception ioe) { throw new RuntimeException(ioe); } - } private static int readRawVarint32(InputStream is) throws IOException { @@ -375,13 +391,12 @@ private InputStream asInputStream() { } try { - final ByteString bytes = ByteString.copyFrom(message.getMessageBuffer(), - message.bytesAfterMessage()); + final ByteString bytes = + ByteString.copyFrom(message.getMessageBuffer(), message.bytesAfterMessage()); if (getMessageType() == HeaderType.SCHEMA) { - final FlightData.Builder builder = FlightData.newBuilder() - .setDataHeader(bytes); + final FlightData.Builder builder = FlightData.newBuilder().setDataHeader(bytes); if (descriptor != null) { builder.setFlightDescriptor(descriptor); @@ -391,17 +406,20 @@ private InputStream asInputStream() { return NO_BODY_MARSHALLER.stream(builder.build()); } - Preconditions.checkArgument(getMessageType() == HeaderType.RECORD_BATCH || - getMessageType() == HeaderType.DICTIONARY_BATCH); + Preconditions.checkArgument( + getMessageType() == HeaderType.RECORD_BATCH + || getMessageType() == HeaderType.DICTIONARY_BATCH); // There may be no buffers in the case that we write only a null array - Preconditions.checkArgument(descriptor == null, "Descriptor should only be included in the schema message."); + Preconditions.checkArgument( + descriptor == null, "Descriptor should only be included in the schema message."); ByteArrayOutputStream baos = new ByteArrayOutputStream(); CodedOutputStream cos = CodedOutputStream.newInstance(baos); cos.writeBytes(FlightData.DATA_HEADER_FIELD_NUMBER, bytes); if (appMetadata != null && appMetadata.capacity() > 0) { - // Must call slice() as CodedOutputStream#writeByteBuffer writes -capacity- bytes, not -limit- bytes + // Must call slice() as CodedOutputStream#writeByteBuffer writes -capacity- bytes, not + // -limit- bytes cos.writeByteBuffer(FlightData.APP_METADATA_FIELD_NUMBER, appMetadata.nioBuffer().slice()); } @@ -414,7 +432,8 @@ private InputStream asInputStream() { // below to tie the Arrow buffer refcnt to the Netty buffer refcnt allBufs.add(Unpooled.wrappedBuffer(b.nioBuffer()).retain()); size += (int) b.readableBytes(); - // [ARROW-4213] These buffers must be aligned to an 8-byte boundary in order to be readable from C++. + // [ARROW-4213] These buffers must be aligned to an 8-byte boundary in order to be readable + // from C++. if (b.readableBytes() % 8 != 0) { int paddingBytes = (int) (8 - (b.readableBytes() % 8)); assert paddingBytes > 0 && paddingBytes < 8; @@ -430,39 +449,36 @@ private InputStream asInputStream() { initialBuf.writeBytes(baos.toByteArray()); final CompositeByteBuf bb; final int maxNumComponents = Math.max(2, bufs.size() + 1); - final ImmutableList byteBufs = ImmutableList.builder() - .add(initialBuf) - .addAll(allBufs) - .build(); + final ImmutableList byteBufs = + ImmutableList.builder().add(initialBuf).addAll(allBufs).build(); if (tryZeroCopyWrite) { bb = new ArrowBufRetainingCompositeByteBuf(maxNumComponents, byteBufs, bufs); } else { // Don't retain the buffers in the non-zero-copy path since we're copying them - bb = new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, /* direct */ true, maxNumComponents, byteBufs); + bb = + new CompositeByteBuf( + UnpooledByteBufAllocator.DEFAULT, /* direct */ true, maxNumComponents, byteBufs); } return new DrainableByteBufInputStream(bb, tryZeroCopyWrite); } catch (Exception ex) { throw new RuntimeException("Unexpected IO Exception", ex); } - } /** * ARROW-11066: enable the zero-copy optimization and protect against use-after-free. * - * When you send a message through gRPC, the following happens: - * 1. gRPC immediately serializes the message, eventually calling asInputStream above. - * 2. gRPC buffers the serialized message for sending. - * 3. Later, gRPC will actually write out the message. + *

When you send a message through gRPC, the following happens: 1. gRPC immediately serializes + * the message, eventually calling asInputStream above. 2. gRPC buffers the serialized message for + * sending. 3. Later, gRPC will actually write out the message. * - * The problem with this is that when the zero-copy optimization is enabled, Flight - * "serializes" the message by handing gRPC references to Arrow data. That means we need - * a way to keep the Arrow buffers valid until gRPC actually writes them, else, we'll read - * invalid data or segfault. gRPC doesn't know anything about Arrow buffers, either. + *

The problem with this is that when the zero-copy optimization is enabled, Flight + * "serializes" the message by handing gRPC references to Arrow data. That means we need a way to + * keep the Arrow buffers valid until gRPC actually writes them, else, we'll read invalid data or + * segfault. gRPC doesn't know anything about Arrow buffers, either. * - * This class solves that issue by bridging Arrow and Netty/gRPC. We increment the refcnt - * on a set of Arrow backing buffers and decrement them once the Netty buffers are freed - * by gRPC. + *

This class solves that issue by bridging Arrow and Netty/gRPC. We increment the refcnt on a + * set of Arrow backing buffers and decrement them once the Netty buffers are freed by gRPC. */ private static final class ArrowBufRetainingCompositeByteBuf extends CompositeByteBuf { // Arrow buffers that back the Netty ByteBufs here; ByteBufs held by this class are @@ -470,7 +486,8 @@ private static final class ArrowBufRetainingCompositeByteBuf extends CompositeBy final List backingBuffers; boolean freed; - ArrowBufRetainingCompositeByteBuf(int maxNumComponents, Iterable buffers, List backingBuffers) { + ArrowBufRetainingCompositeByteBuf( + int maxNumComponents, Iterable buffers, List backingBuffers) { super(UnpooledByteBufAllocator.DEFAULT, /* direct */ true, maxNumComponents, buffers); this.backingBuffers = backingBuffers; this.freed = false; @@ -515,9 +532,6 @@ public int drainTo(OutputStream target) throws IOException { public void close() { buf.release(); } - - - } public static Marshaller createMarshaller(BufferAllocator allocator) { @@ -541,7 +555,6 @@ public InputStream stream(ArrowMessage value) { public ArrowMessage parse(InputStream stream) { return ArrowMessage.frame(allocator, stream); } - } @Override diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/AsyncPutListener.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/AsyncPutListener.java index a454632258a..81ea4a3ebb9 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/AsyncPutListener.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/AsyncPutListener.java @@ -19,14 +19,13 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; - import org.apache.arrow.flight.grpc.StatusUtils; /** * A handler for server-sent application metadata messages during a Flight DoPut operation. * - *

To handle messages, create an instance of this class overriding {@link #onNext(PutResult)}. The other methods - * should not be overridden. + *

To handle messages, create an instance of this class overriding {@link #onNext(PutResult)}. + * The other methods should not be overridden. */ public class AsyncPutListener implements FlightClient.PutListener { @@ -37,8 +36,8 @@ public AsyncPutListener() { } /** - * Wait for the stream to finish on the server side. You must call this to be notified of any errors that may have - * happened during the upload. + * Wait for the stream to finish on the server side. You must call this to be notified of any + * errors that may have happened during the upload. */ @Override public final void getResult() { @@ -52,8 +51,7 @@ public final void getResult() { } @Override - public void onNext(PutResult val) { - } + public void onNext(PutResult val) {} @Override public final void onError(Throwable t) { diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/BackpressureStrategy.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/BackpressureStrategy.java index de34643a758..b332a011bfd 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/BackpressureStrategy.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/BackpressureStrategy.java @@ -17,46 +17,35 @@ package org.apache.arrow.flight; -import org.apache.arrow.vector.VectorSchemaRoot; - import com.google.common.base.Preconditions; +import org.apache.arrow.vector.VectorSchemaRoot; /** - * Helper interface to dynamically handle backpressure when implementing FlightProducers. - * This must only be used in FlightProducer implementations that are non-blocking. + * Helper interface to dynamically handle backpressure when implementing FlightProducers. This must + * only be used in FlightProducer implementations that are non-blocking. */ public interface BackpressureStrategy { - /** - * The state of the client after a call to waitForListener. - */ + /** The state of the client after a call to waitForListener. */ enum WaitResult { - /** - * Listener is ready. - */ + /** Listener is ready. */ READY, - /** - * Listener was cancelled by the client. - */ + /** Listener was cancelled by the client. */ CANCELLED, - /** - * Timed out waiting for the listener to change state. - */ + /** Timed out waiting for the listener to change state. */ TIMEOUT, - /** - * Indicates that the wait was interrupted for a reason - * unrelated to the listener itself. - */ + /** Indicates that the wait was interrupted for a reason unrelated to the listener itself. */ OTHER } /** * Set up operations to work against the given listener. * - * This must be called exactly once and before any calls to {@link #waitForListener(long)} and + *

This must be called exactly once and before any calls to {@link #waitForListener(long)} and * {@link OutboundStreamListener#start(VectorSchemaRoot)} + * * @param listener The listener this strategy applies to. */ void register(FlightProducer.ServerStreamListener listener); @@ -121,9 +110,9 @@ public WaitResult waitForListener(long timeout) { /** * Interrupt waiting on the listener to change state. * - * This method can be used in conjunction with - * {@link #shouldContinueWaiting(FlightProducer.ServerStreamListener, long)} to allow FlightProducers to - * terminate streams internally and notify clients. + *

This method can be used in conjunction with {@link + * #shouldContinueWaiting(FlightProducer.ServerStreamListener, long)} to allow FlightProducers + * to terminate streams internally and notify clients. */ public void interruptWait() { synchronized (lock) { @@ -132,28 +121,23 @@ public void interruptWait() { } /** - * Callback function to run to check if the listener should continue - * to be waited on if it leaves the waiting state without being cancelled, - * ready, or timed out. + * Callback function to run to check if the listener should continue to be waited on if it + * leaves the waiting state without being cancelled, ready, or timed out. * - * This method should be used to determine if the wait on the listener was interrupted explicitly using a - * call to {@link #interruptWait()} or if it was woken up due to a spurious wake. + *

This method should be used to determine if the wait on the listener was interrupted + * explicitly using a call to {@link #interruptWait()} or if it was woken up due to a spurious + * wake. */ - protected boolean shouldContinueWaiting(FlightProducer.ServerStreamListener listener, long remainingTimeout) { + protected boolean shouldContinueWaiting( + FlightProducer.ServerStreamListener listener, long remainingTimeout) { return true; } - /** - * Callback to execute when the listener becomes ready. - */ - protected void readyCallback() { - } + /** Callback to execute when the listener becomes ready. */ + protected void readyCallback() {} - /** - * Callback to execute when the listener is cancelled. - */ - protected void cancelCallback() { - } + /** Callback to execute when the listener is cancelled. */ + protected void cancelCallback() {} private void onReady() { synchronized (lock) { diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallHeaders.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallHeaders.java index 32f9a8430d1..ed4ef0828ea 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallHeaders.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallHeaders.java @@ -19,28 +19,18 @@ import java.util.Set; -/** - * A set of metadata key value pairs for a call (request or response). - */ +/** A set of metadata key value pairs for a call (request or response). */ public interface CallHeaders { - /** - * Get the value of a metadata key. If multiple values are present, then get the last one. - */ + /** Get the value of a metadata key. If multiple values are present, then get the last one. */ String get(String key); - /** - * Get the value of a metadata key. If multiple values are present, then get the last one. - */ + /** Get the value of a metadata key. If multiple values are present, then get the last one. */ byte[] getByte(String key); - /** - * Get all values present for the given metadata key. - */ + /** Get all values present for the given metadata key. */ Iterable getAll(String key); - /** - * Get all values present for the given metadata key. - */ + /** Get all values present for the given metadata key. */ Iterable getAllByte(String key); /** diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallInfo.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallInfo.java index 744584bdfd5..961aeba4e4c 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallInfo.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallInfo.java @@ -17,9 +17,7 @@ package org.apache.arrow.flight; -/** - * A description of a Flight call for middleware to inspect. - */ +/** A description of a Flight call for middleware to inspect. */ public final class CallInfo { private final FlightMethod method; diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallOption.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallOption.java index d3ee3ab4cee..6a5251aa8c1 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallOption.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallOption.java @@ -17,8 +17,5 @@ package org.apache.arrow.flight; -/** - * Per-call RPC options. These are hints to the underlying RPC layer and may not be respected. - */ -public interface CallOption { -} +/** Per-call RPC options. These are hints to the underlying RPC layer and may not be respected. */ +public interface CallOption {} diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallOptions.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallOptions.java index bbb4edef9c0..70a1d1fcf30 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallOptions.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallOptions.java @@ -17,13 +17,10 @@ package org.apache.arrow.flight; -import java.util.concurrent.TimeUnit; - import io.grpc.stub.AbstractStub; +import java.util.concurrent.TimeUnit; -/** - * Common call options. - */ +/** Common call options. */ public class CallOptions { public static CallOption timeout(long duration, TimeUnit unit) { return new Timeout(duration, unit); @@ -53,9 +50,7 @@ public > T wrapStub(T stub) { } } - /** - * CallOptions specific to GRPC stubs. - */ + /** CallOptions specific to GRPC stubs. */ public interface GrpcCallOption extends CallOption { > T wrapStub(T stub); } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallStatus.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallStatus.java index 991d0ed6a04..b4bc51b434f 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallStatus.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CallStatus.java @@ -18,18 +18,17 @@ package org.apache.arrow.flight; import java.util.Objects; - import org.apache.arrow.flight.FlightProducer.ServerStreamListener; import org.apache.arrow.flight.FlightProducer.StreamListener; /** - * The result of a Flight RPC, consisting of a status code with an optional description and/or exception that led - * to the status. + * The result of a Flight RPC, consisting of a status code with an optional description and/or + * exception that led to the status. * - *

If raised or sent through {@link StreamListener#onError(Throwable)} or - * {@link ServerStreamListener#error(Throwable)}, the client call will raise the same error (a - * {@link FlightRuntimeException} with the same {@link FlightStatusCode} and description). The exception within, if - * present, will not be sent to the client. + *

If raised or sent through {@link StreamListener#onError(Throwable)} or {@link + * ServerStreamListener#error(Throwable)}, the client call will raise the same error (a {@link + * FlightRuntimeException} with the same {@link FlightStatusCode} and description). The exception + * within, if present, will not be sent to the client. */ public class CallStatus { @@ -57,7 +56,8 @@ public class CallStatus { * @param cause An exception that resulted in this status (or null). * @param description A description of the status (or null). */ - public CallStatus(FlightStatusCode code, Throwable cause, String description, ErrorFlightMetadata metadata) { + public CallStatus( + FlightStatusCode code, Throwable cause, String description, ErrorFlightMetadata metadata) { this.code = Objects.requireNonNull(code); this.cause = cause; this.description = description == null ? "" : description; @@ -73,23 +73,17 @@ public CallStatus(FlightStatusCode code) { this(code, /* no cause */ null, /* no description */ null, /* no metadata */ null); } - /** - * The status code describing the result of the RPC. - */ + /** The status code describing the result of the RPC. */ public FlightStatusCode code() { return code; } - /** - * The exception that led to this result. May be null. - */ + /** The exception that led to this result. May be null. */ public Throwable cause() { return cause; } - /** - * A description of the result. - */ + /** A description of the result. */ public String description() { return description; } @@ -97,47 +91,47 @@ public String description() { /** * Metadata associated with the exception. * - * May be null. + *

May be null. */ public ErrorFlightMetadata metadata() { return metadata; } - /** - * Return a copy of this status with an error message. - */ + /** Return a copy of this status with an error message. */ public CallStatus withDescription(String message) { return new CallStatus(code, cause, message, metadata); } /** - * Return a copy of this status with the given exception as the cause. This will not be sent over the wire. + * Return a copy of this status with the given exception as the cause. This will not be sent over + * the wire. */ public CallStatus withCause(Throwable t) { return new CallStatus(code, t, description, metadata); } - /** - * Return a copy of this status with associated exception metadata. - */ + /** Return a copy of this status with associated exception metadata. */ public CallStatus withMetadata(ErrorFlightMetadata metadata) { return new CallStatus(code, cause, description, metadata); } - /** - * Convert the status to an equivalent exception. - */ + /** Convert the status to an equivalent exception. */ public FlightRuntimeException toRuntimeException() { return new FlightRuntimeException(this); } @Override public String toString() { - return "CallStatus{" + - "code=" + code + - ", cause=" + cause + - ", description='" + description + - "', metadata='" + metadata + '\'' + - '}'; + return "CallStatus{" + + "code=" + + code + + ", cause=" + + cause + + ", description='" + + description + + "', metadata='" + + metadata + + '\'' + + '}'; } } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CancelFlightInfoRequest.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CancelFlightInfoRequest.java index 5d605f79410..cfcf9757e22 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CancelFlightInfoRequest.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CancelFlightInfoRequest.java @@ -21,7 +21,6 @@ import java.net.URISyntaxException; import java.nio.ByteBuffer; import java.util.Objects; - import org.apache.arrow.flight.impl.Flight; /** A request to cancel a FlightInfo. */ @@ -49,7 +48,8 @@ Flight.CancelFlightInfoRequest toProtocol() { /** * Get the serialized form of this protocol message. * - *

Intended to help interoperability by allowing non-Flight services to still return Flight types. + *

Intended to help interoperability by allowing non-Flight services to still return Flight + * types. */ public ByteBuffer serialize() { return ByteBuffer.wrap(toProtocol().toByteArray()); @@ -58,13 +58,15 @@ public ByteBuffer serialize() { /** * Parse the serialized form of this protocol message. * - *

Intended to help interoperability by allowing Flight clients to obtain stream info from non-Flight services. + *

Intended to help interoperability by allowing Flight clients to obtain stream info from + * non-Flight services. * * @param serialized The serialized form of the message, as returned by {@link #serialize()}. * @return The deserialized message. * @throws IOException if the serialized form is invalid. */ - public static CancelFlightInfoRequest deserialize(ByteBuffer serialized) throws IOException, URISyntaxException { + public static CancelFlightInfoRequest deserialize(ByteBuffer serialized) + throws IOException, URISyntaxException { return new CancelFlightInfoRequest(Flight.CancelFlightInfoRequest.parseFrom(serialized)); } } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CancelFlightInfoResult.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CancelFlightInfoResult.java index 165afdff553..ae35f957b0d 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CancelFlightInfoResult.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CancelFlightInfoResult.java @@ -20,12 +20,9 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.util.Objects; - import org.apache.arrow.flight.impl.Flight; -/** - * The result of cancelling a FlightInfo. - */ +/** The result of cancelling a FlightInfo. */ public class CancelFlightInfoResult { private final CancelStatus status; @@ -81,7 +78,8 @@ Flight.CancelFlightInfoResult toProtocol() { /** * Get the serialized form of this protocol message. * - *

Intended to help interoperability by allowing non-Flight services to still return Flight types. + *

Intended to help interoperability by allowing non-Flight services to still return Flight + * types. */ public ByteBuffer serialize() { return ByteBuffer.wrap(toProtocol().toByteArray()); @@ -90,7 +88,8 @@ public ByteBuffer serialize() { /** * Parse the serialized form of this protocol message. * - *

Intended to help interoperability by allowing Flight clients to obtain stream info from non-Flight services. + *

Intended to help interoperability by allowing Flight clients to obtain stream info from + * non-Flight services. * * @param serialized The serialized form of the message, as returned by {@link #serialize()}. * @return The deserialized message. @@ -119,8 +118,6 @@ public int hashCode() { @Override public String toString() { - return "CancelFlightInfoResult{" + - "status=" + status + - '}'; + return "CancelFlightInfoResult{" + "status=" + status + '}'; } } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CancelStatus.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CancelStatus.java index e92bcc8b258..093799a6aaa 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CancelStatus.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/CancelStatus.java @@ -20,25 +20,18 @@ /** The result of cancelling a FlightInfo. */ public enum CancelStatus { /** - * The cancellation status is unknown. Servers should avoid using - * this value (send a NOT_FOUND error if the requested query is - * not known). Clients can retry the request. + * The cancellation status is unknown. Servers should avoid using this value (send a NOT_FOUND + * error if the requested query is not known). Clients can retry the request. */ UNSPECIFIED, /** - * The cancellation request is complete. Subsequent requests with - * the same payload may return CANCELLED or a NOT_FOUND error. + * The cancellation request is complete. Subsequent requests with the same payload may return + * CANCELLED or a NOT_FOUND error. */ CANCELLED, - /** - * The cancellation request is in progress. The client may retry - * the cancellation request. - */ + /** The cancellation request is in progress. The client may retry the cancellation request. */ CANCELLING, - /** - * The query is not cancellable. The client should not retry the - * cancellation request. - */ + /** The query is not cancellable. The client should not retry the cancellation request. */ NOT_CANCELLABLE, ; } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Criteria.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Criteria.java index 989cd658121..717f5cb21bf 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Criteria.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Criteria.java @@ -17,9 +17,8 @@ package org.apache.arrow.flight; -import org.apache.arrow.flight.impl.Flight; - import com.google.protobuf.ByteString; +import org.apache.arrow.flight.impl.Flight; /** * An opaque object that can be used to filter a list of streams available from a server. @@ -40,9 +39,7 @@ public Criteria(byte[] bytes) { this.bytes = criteria.getExpression().toByteArray(); } - /** - * Get the contained filter criteria. - */ + /** Get the contained filter criteria. */ public byte[] getExpression() { return bytes; } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/DictionaryUtils.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/DictionaryUtils.java index 516dab01d8a..825bb8400eb 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/DictionaryUtils.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/DictionaryUtils.java @@ -24,7 +24,6 @@ import java.util.Set; import java.util.function.Consumer; import java.util.stream.Collectors; - import org.apache.arrow.flight.impl.Flight; import org.apache.arrow.util.AutoCloseables; import org.apache.arrow.vector.FieldVector; @@ -39,9 +38,7 @@ import org.apache.arrow.vector.util.DictionaryUtility; import org.apache.arrow.vector.validate.MetadataV4UnionChecker; -/** - * Utilities to work with dictionaries in Flight. - */ +/** Utilities to work with dictionaries in Flight. */ final class DictionaryUtils { private DictionaryUtils() { @@ -51,16 +48,22 @@ private DictionaryUtils() { /** * Generate all the necessary Flight messages to send a schema and associated dictionaries. * - * @throws Exception if there was an error closing {@link ArrowMessage} objects. This is not generally expected. + * @throws Exception if there was an error closing {@link ArrowMessage} objects. This is not + * generally expected. */ - static Schema generateSchemaMessages(final Schema originalSchema, final FlightDescriptor descriptor, - final DictionaryProvider provider, final IpcOption option, - final Consumer messageCallback) throws Exception { + static Schema generateSchemaMessages( + final Schema originalSchema, + final FlightDescriptor descriptor, + final DictionaryProvider provider, + final IpcOption option, + final Consumer messageCallback) + throws Exception { final Set dictionaryIds = new HashSet<>(); final Schema schema = generateSchema(originalSchema, provider, dictionaryIds); MetadataV4UnionChecker.checkForUnion(schema.getFields().iterator(), option.metadataVersion); // Send the schema message - final Flight.FlightDescriptor protoDescriptor = descriptor == null ? null : descriptor.toProtocol(); + final Flight.FlightDescriptor protoDescriptor = + descriptor == null ? null : descriptor.toProtocol(); try (final ArrowMessage message = new ArrowMessage(protoDescriptor, schema, option)) { messageCallback.accept(message); } @@ -70,13 +73,14 @@ static Schema generateSchemaMessages(final Schema originalSchema, final FlightDe final FieldVector vector = dictionary.getVector(); final int count = vector.getValueCount(); // Do NOT close this root, as it does not actually own the vector. - final VectorSchemaRoot dictRoot = new VectorSchemaRoot( - Collections.singletonList(vector.getField()), - Collections.singletonList(vector), - count); + final VectorSchemaRoot dictRoot = + new VectorSchemaRoot( + Collections.singletonList(vector.getField()), + Collections.singletonList(vector), + count); final VectorUnloader unloader = new VectorUnloader(dictRoot); - try (final ArrowDictionaryBatch dictionaryBatch = new ArrowDictionaryBatch( - id, unloader.getRecordBatch()); + try (final ArrowDictionaryBatch dictionaryBatch = + new ArrowDictionaryBatch(id, unloader.getRecordBatch()); final ArrowMessage message = new ArrowMessage(dictionaryBatch, option)) { messageCallback.accept(message); } @@ -84,27 +88,33 @@ static Schema generateSchemaMessages(final Schema originalSchema, final FlightDe return schema; } - static void closeDictionaries(final Schema schema, final DictionaryProvider provider) throws Exception { + static void closeDictionaries(final Schema schema, final DictionaryProvider provider) + throws Exception { // Close dictionaries final Set dictionaryIds = new HashSet<>(); - schema.getFields().forEach(field -> DictionaryUtility.toMessageFormat(field, provider, dictionaryIds)); + schema + .getFields() + .forEach(field -> DictionaryUtility.toMessageFormat(field, provider, dictionaryIds)); - final List dictionaryVectors = dictionaryIds.stream() - .map(id -> (AutoCloseable) provider.lookup(id).getVector()).collect(Collectors.toList()); + final List dictionaryVectors = + dictionaryIds.stream() + .map(id -> (AutoCloseable) provider.lookup(id).getVector()) + .collect(Collectors.toList()); AutoCloseables.close(dictionaryVectors); } /** - * Generates the schema to send with flight messages. - * If the schema contains no field with a dictionary, it will return the schema as is. - * Otherwise, it will return a newly created a new schema after converting the fields. + * Generates the schema to send with flight messages. If the schema contains no field with a + * dictionary, it will return the schema as is. Otherwise, it will return a newly created a new + * schema after converting the fields. + * * @param originalSchema the original schema. * @param provider the dictionary provider. * @param dictionaryIds dictionary IDs that are used. * @return the schema to send with the flight messages. */ static Schema generateSchema( - final Schema originalSchema, final DictionaryProvider provider, Set dictionaryIds) { + final Schema originalSchema, final DictionaryProvider provider, Set dictionaryIds) { // first determine if a new schema needs to be created. boolean createSchema = false; for (Field field : originalSchema.getFields()) { diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ErrorFlightMetadata.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ErrorFlightMetadata.java index 6e19d2750cb..d54c693d385 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ErrorFlightMetadata.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ErrorFlightMetadata.java @@ -17,24 +17,19 @@ package org.apache.arrow.flight; +import com.google.common.collect.Iterables; +import com.google.common.collect.LinkedListMultimap; +import com.google.common.collect.Multimap; import java.nio.charset.StandardCharsets; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.StreamSupport; -import com.google.common.collect.Iterables; -import com.google.common.collect.LinkedListMultimap; -import com.google.common.collect.Multimap; - -/** - * metadata container specific to the binary metadata held in the grpc trailer. - */ +/** metadata container specific to the binary metadata held in the grpc trailer. */ public class ErrorFlightMetadata implements CallHeaders { private final Multimap metadata = LinkedListMultimap.create(); - public ErrorFlightMetadata() { - } - + public ErrorFlightMetadata() {} @Override public String get(String key) { @@ -48,8 +43,7 @@ public byte[] getByte(String key) { @Override public Iterable getAll(String key) { - return StreamSupport.stream( - getAllByte(key).spliterator(), false) + return StreamSupport.stream(getAllByte(key).spliterator(), false) .map(b -> new String(b, StandardCharsets.US_ASCII)) .collect(Collectors.toList()); } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightBindingService.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightBindingService.java index ba5249b4a53..2a775e79e34 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightBindingService.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightBindingService.java @@ -17,17 +17,7 @@ package org.apache.arrow.flight; -import java.util.Set; -import java.util.concurrent.ExecutorService; - -import org.apache.arrow.flight.auth.ServerAuthHandler; -import org.apache.arrow.flight.impl.Flight; -import org.apache.arrow.flight.impl.Flight.PutResult; -import org.apache.arrow.flight.impl.FlightServiceGrpc; -import org.apache.arrow.memory.BufferAllocator; - import com.google.common.collect.ImmutableSet; - import io.grpc.BindableService; import io.grpc.MethodDescriptor; import io.grpc.MethodDescriptor.MethodType; @@ -37,28 +27,39 @@ import io.grpc.protobuf.ProtoUtils; import io.grpc.stub.ServerCalls; import io.grpc.stub.StreamObserver; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import org.apache.arrow.flight.auth.ServerAuthHandler; +import org.apache.arrow.flight.impl.Flight; +import org.apache.arrow.flight.impl.Flight.PutResult; +import org.apache.arrow.flight.impl.FlightServiceGrpc; +import org.apache.arrow.memory.BufferAllocator; -/** - * Extends the basic flight service to override some methods for more efficient implementations. - */ +/** Extends the basic flight service to override some methods for more efficient implementations. */ class FlightBindingService implements BindableService { - private static final String DO_GET = MethodDescriptor.generateFullMethodName(FlightConstants.SERVICE, "DoGet"); - private static final String DO_PUT = MethodDescriptor.generateFullMethodName(FlightConstants.SERVICE, "DoPut"); - private static final String DO_EXCHANGE = MethodDescriptor.generateFullMethodName( - FlightConstants.SERVICE, "DoExchange"); + private static final String DO_GET = + MethodDescriptor.generateFullMethodName(FlightConstants.SERVICE, "DoGet"); + private static final String DO_PUT = + MethodDescriptor.generateFullMethodName(FlightConstants.SERVICE, "DoPut"); + private static final String DO_EXCHANGE = + MethodDescriptor.generateFullMethodName(FlightConstants.SERVICE, "DoExchange"); private static final Set OVERRIDE_METHODS = ImmutableSet.of(DO_GET, DO_PUT, DO_EXCHANGE); private final FlightService delegate; private final BufferAllocator allocator; - public FlightBindingService(BufferAllocator allocator, FlightProducer producer, - ServerAuthHandler authHandler, ExecutorService executor) { + public FlightBindingService( + BufferAllocator allocator, + FlightProducer producer, + ServerAuthHandler authHandler, + ExecutorService executor) { this.allocator = allocator; this.delegate = new FlightService(allocator, producer, authHandler, executor); } - public static MethodDescriptor getDoGetDescriptor(BufferAllocator allocator) { + public static MethodDescriptor getDoGetDescriptor( + BufferAllocator allocator) { return MethodDescriptor.newBuilder() .setType(io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING) .setFullMethodName(DO_GET) @@ -69,7 +70,8 @@ public static MethodDescriptor getDoGetDescriptor(B .build(); } - public static MethodDescriptor getDoPutDescriptor(BufferAllocator allocator) { + public static MethodDescriptor getDoPutDescriptor( + BufferAllocator allocator) { return MethodDescriptor.newBuilder() .setType(MethodType.BIDI_STREAMING) .setFullMethodName(DO_PUT) @@ -80,28 +82,33 @@ public static MethodDescriptor getDoPutDescripto .build(); } - public static MethodDescriptor getDoExchangeDescriptor(BufferAllocator allocator) { + public static MethodDescriptor getDoExchangeDescriptor( + BufferAllocator allocator) { return MethodDescriptor.newBuilder() - .setType(MethodType.BIDI_STREAMING) - .setFullMethodName(DO_EXCHANGE) - .setSampledToLocalTracing(false) - .setRequestMarshaller(ArrowMessage.createMarshaller(allocator)) - .setResponseMarshaller(ArrowMessage.createMarshaller(allocator)) - .setSchemaDescriptor(FlightServiceGrpc.getDoExchangeMethod().getSchemaDescriptor()) - .build(); + .setType(MethodType.BIDI_STREAMING) + .setFullMethodName(DO_EXCHANGE) + .setSampledToLocalTracing(false) + .setRequestMarshaller(ArrowMessage.createMarshaller(allocator)) + .setResponseMarshaller(ArrowMessage.createMarshaller(allocator)) + .setSchemaDescriptor(FlightServiceGrpc.getDoExchangeMethod().getSchemaDescriptor()) + .build(); } @Override public ServerServiceDefinition bindService() { final ServerServiceDefinition baseDefinition = delegate.bindService(); - final MethodDescriptor doGetDescriptor = getDoGetDescriptor(allocator); - final MethodDescriptor doPutDescriptor = getDoPutDescriptor(allocator); - final MethodDescriptor doExchangeDescriptor = getDoExchangeDescriptor(allocator); + final MethodDescriptor doGetDescriptor = + getDoGetDescriptor(allocator); + final MethodDescriptor doPutDescriptor = + getDoPutDescriptor(allocator); + final MethodDescriptor doExchangeDescriptor = + getDoExchangeDescriptor(allocator); // Make sure we preserve SchemaDescriptor fields on methods so that gRPC reflection still works. - final ServiceDescriptor.Builder serviceDescriptorBuilder = ServiceDescriptor.newBuilder(FlightConstants.SERVICE) - .setSchemaDescriptor(baseDefinition.getServiceDescriptor().getSchemaDescriptor()); + final ServiceDescriptor.Builder serviceDescriptorBuilder = + ServiceDescriptor.newBuilder(FlightConstants.SERVICE) + .setSchemaDescriptor(baseDefinition.getServiceDescriptor().getSchemaDescriptor()); serviceDescriptorBuilder.addMethod(doGetDescriptor); serviceDescriptorBuilder.addMethod(doPutDescriptor); serviceDescriptorBuilder.addMethod(doExchangeDescriptor); @@ -114,10 +121,14 @@ public ServerServiceDefinition bindService() { } final ServiceDescriptor serviceDescriptor = serviceDescriptorBuilder.build(); - ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition.builder(serviceDescriptor); - serviceBuilder.addMethod(doGetDescriptor, ServerCalls.asyncServerStreamingCall(new DoGetMethod(delegate))); - serviceBuilder.addMethod(doPutDescriptor, ServerCalls.asyncBidiStreamingCall(new DoPutMethod(delegate))); - serviceBuilder.addMethod(doExchangeDescriptor, ServerCalls.asyncBidiStreamingCall(new DoExchangeMethod(delegate))); + ServerServiceDefinition.Builder serviceBuilder = + ServerServiceDefinition.builder(serviceDescriptor); + serviceBuilder.addMethod( + doGetDescriptor, ServerCalls.asyncServerStreamingCall(new DoGetMethod(delegate))); + serviceBuilder.addMethod( + doPutDescriptor, ServerCalls.asyncBidiStreamingCall(new DoPutMethod(delegate))); + serviceBuilder.addMethod( + doExchangeDescriptor, ServerCalls.asyncBidiStreamingCall(new DoExchangeMethod(delegate))); // copy over not-overridden methods. for (ServerMethodDefinition definition : baseDefinition.getMethods()) { @@ -131,7 +142,8 @@ public ServerServiceDefinition bindService() { return serviceBuilder.build(); } - private static class DoGetMethod implements ServerCalls.ServerStreamingMethod { + private static class DoGetMethod + implements ServerCalls.ServerStreamingMethod { private final FlightService delegate; @@ -145,7 +157,8 @@ public void invoke(Flight.Ticket request, StreamObserver responseO } } - private static class DoPutMethod implements ServerCalls.BidiStreamingMethod { + private static class DoPutMethod + implements ServerCalls.BidiStreamingMethod { private final FlightService delegate; public DoPutMethod(FlightService delegate) { @@ -158,7 +171,8 @@ public StreamObserver invoke(StreamObserver responseObs } } - private static class DoExchangeMethod implements ServerCalls.BidiStreamingMethod { + private static class DoExchangeMethod + implements ServerCalls.BidiStreamingMethod { private final FlightService delegate; public DoExchangeMethod(FlightService delegate) { @@ -170,5 +184,4 @@ public StreamObserver invoke(StreamObserver response return delegate.doExchangeCustom(responseObserver); } } - } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightCallHeaders.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightCallHeaders.java index 93b89e77550..6d7df3cc58b 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightCallHeaders.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightCallHeaders.java @@ -17,21 +17,17 @@ package org.apache.arrow.flight; -import java.nio.charset.StandardCharsets; -import java.util.Collection; -import java.util.Set; -import java.util.stream.Collectors; - import com.google.common.base.Preconditions; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Iterables; import com.google.common.collect.Multimap; - import io.grpc.Metadata; +import java.nio.charset.StandardCharsets; +import java.util.Collection; +import java.util.Set; +import java.util.stream.Collectors; -/** - * An implementation of the Flight headers interface for headers. - */ +/** An implementation of the Flight headers interface for headers. */ public class FlightCallHeaders implements CallHeaders { private final Multimap keysAndValues; @@ -70,7 +66,8 @@ public byte[] getByte(String key) { @Override public Iterable getAll(String key) { if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) { - return this.keysAndValues.get(key).stream().map(o -> new String((byte[]) o, StandardCharsets.UTF_8)) + return this.keysAndValues.get(key).stream() + .map(o -> new String((byte[]) o, StandardCharsets.UTF_8)) .collect(Collectors.toList()); } return (Collection) (Collection) this.keysAndValues.get(key); @@ -81,7 +78,8 @@ public Iterable getAllByte(String key) { if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) { return (Collection) (Collection) this.keysAndValues.get(key); } - return this.keysAndValues.get(key).stream().map(o -> ((String) o).getBytes(StandardCharsets.UTF_8)) + return this.keysAndValues.get(key).stream() + .map(o -> ((String) o).getBytes(StandardCharsets.UTF_8)) .collect(Collectors.toList()); } @@ -92,7 +90,8 @@ public void insert(String key, String value) { @Override public void insert(String key, byte[] value) { - Preconditions.checkArgument(key.endsWith("-bin"), "Binary header is named %s. It must end with %s", key, "-bin"); + Preconditions.checkArgument( + key.endsWith("-bin"), "Binary header is named %s. It must end with %s", key, "-bin"); Preconditions.checkArgument(key.length() > "-bin".length(), "empty key name"); this.keysAndValues.put(key, value); diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightClient.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightClient.java index fc491ebe0df..517c449c879 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightClient.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightClient.java @@ -17,6 +17,23 @@ package org.apache.arrow.flight; +import io.grpc.Channel; +import io.grpc.ClientCall; +import io.grpc.ClientInterceptor; +import io.grpc.ClientInterceptors; +import io.grpc.ManagedChannel; +import io.grpc.MethodDescriptor; +import io.grpc.StatusRuntimeException; +import io.grpc.netty.GrpcSslContexts; +import io.grpc.netty.NettyChannelBuilder; +import io.grpc.stub.ClientCallStreamObserver; +import io.grpc.stub.ClientCalls; +import io.grpc.stub.ClientResponseObserver; +import io.grpc.stub.StreamObserver; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.ServerChannel; +import io.netty.handler.ssl.SslContextBuilder; +import io.netty.handler.ssl.util.InsecureTrustManagerFactory; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; @@ -29,9 +46,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.function.BooleanSupplier; - import javax.net.ssl.SSLException; - import org.apache.arrow.flight.FlightProducer.StreamListener; import org.apache.arrow.flight.auth.BasicClientAuthHandler; import org.apache.arrow.flight.auth.ClientAuthHandler; @@ -55,31 +70,16 @@ import org.apache.arrow.vector.dictionary.DictionaryProvider; import org.apache.arrow.vector.dictionary.DictionaryProvider.MapDictionaryProvider; -import io.grpc.Channel; -import io.grpc.ClientCall; -import io.grpc.ClientInterceptor; -import io.grpc.ClientInterceptors; -import io.grpc.ManagedChannel; -import io.grpc.MethodDescriptor; -import io.grpc.StatusRuntimeException; -import io.grpc.netty.GrpcSslContexts; -import io.grpc.netty.NettyChannelBuilder; -import io.grpc.stub.ClientCallStreamObserver; -import io.grpc.stub.ClientCalls; -import io.grpc.stub.ClientResponseObserver; -import io.grpc.stub.StreamObserver; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.ServerChannel; -import io.netty.handler.ssl.SslContextBuilder; -import io.netty.handler.ssl.util.InsecureTrustManagerFactory; - -/** - * Client for Flight services. - */ +/** Client for Flight services. */ public class FlightClient implements AutoCloseable { private static final int PENDING_REQUESTS = 5; - /** The maximum number of trace events to keep on the gRPC Channel. This value disables channel tracing. */ + + /** + * The maximum number of trace events to keep on the gRPC Channel. This value disables channel + * tracing. + */ private static final int MAX_CHANNEL_TRACE_EVENTS = 0; + private final BufferAllocator allocator; private final ManagedChannel channel; @@ -91,17 +91,18 @@ public class FlightClient implements AutoCloseable { private final MethodDescriptor doExchangeDescriptor; private final List middleware; - /** - * Create a Flight client from an allocator and a gRPC channel. - */ - FlightClient(BufferAllocator incomingAllocator, ManagedChannel channel, + /** Create a Flight client from an allocator and a gRPC channel. */ + FlightClient( + BufferAllocator incomingAllocator, + ManagedChannel channel, List middleware) { this.allocator = incomingAllocator.newChildAllocator("flight-client", 0, Long.MAX_VALUE); this.channel = channel; this.middleware = middleware; final ClientInterceptor[] interceptors; - interceptors = new ClientInterceptor[]{authInterceptor, new ClientInterceptorAdapter(middleware)}; + interceptors = + new ClientInterceptor[] {authInterceptor, new ClientInterceptorAdapter(middleware)}; // Create a channel with interceptors pre-applied for DoGet and DoPut Channel interceptedChannel = ClientInterceptors.intercept(channel, interceptors); @@ -123,20 +124,23 @@ public class FlightClient implements AutoCloseable { public Iterable listFlights(Criteria criteria, CallOption... options) { final Iterator flights; try { - flights = CallOptions.wrapStub(blockingStub, options) - .listFlights(criteria.asCriteria()); + flights = CallOptions.wrapStub(blockingStub, options).listFlights(criteria.asCriteria()); } catch (StatusRuntimeException sre) { throw StatusUtils.fromGrpcRuntimeException(sre); } - return () -> StatusUtils.wrapIterator(flights, t -> { - try { - return new FlightInfo(t); - } catch (URISyntaxException e) { - // We don't expect this will happen for conforming Flight implementations. For instance, a Java server - // itself wouldn't be able to construct an invalid Location. - throw new RuntimeException(e); - } - }); + return () -> + StatusUtils.wrapIterator( + flights, + t -> { + try { + return new FlightInfo(t); + } catch (URISyntaxException e) { + // We don't expect this will happen for conforming Flight implementations. For + // instance, a Java server + // itself wouldn't be able to construct an invalid Location. + throw new RuntimeException(e); + } + }); } /** @@ -147,8 +151,7 @@ public Iterable listFlights(Criteria criteria, CallOption... options public Iterable listActions(CallOption... options) { final Iterator actions; try { - actions = CallOptions.wrapStub(blockingStub, options) - .listActions(Empty.getDefaultInstance()); + actions = CallOptions.wrapStub(blockingStub, options).listActions(Empty.getDefaultInstance()); } catch (StatusRuntimeException sre) { throw StatusUtils.fromGrpcRuntimeException(sre); } @@ -163,13 +166,11 @@ public Iterable listActions(CallOption... options) { * @return An iterator of results. */ public Iterator doAction(Action action, CallOption... options) { - return StatusUtils - .wrapIterator(CallOptions.wrapStub(blockingStub, options).doAction(action.toProtocol()), Result::new); + return StatusUtils.wrapIterator( + CallOptions.wrapStub(blockingStub, options).doAction(action.toProtocol()), Result::new); } - /** - * Authenticates with a username and password. - */ + /** Authenticates with a username and password. */ public void authenticateBasic(String username, String password) { BasicClientAuthHandler basicClient = new BasicClientAuthHandler(username, password); authenticate(basicClient); @@ -192,12 +193,12 @@ public void authenticate(ClientAuthHandler handler, CallOption... options) { * * @param username the username. * @param password the password. - * @return a CredentialCallOption containing a bearer token if the server emitted one, or - * empty if no bearer token was returned. This can be used in subsequent API calls. + * @return a CredentialCallOption containing a bearer token if the server emitted one, or empty if + * no bearer token was returned. This can be used in subsequent API calls. */ public Optional authenticateBasicToken(String username, String password) { final ClientIncomingAuthHeaderMiddleware.Factory clientAuthMiddleware = - new ClientIncomingAuthHeaderMiddleware.Factory(new ClientBearerHeaderHandler()); + new ClientIncomingAuthHeaderMiddleware.Factory(new ClientBearerHeaderHandler()); middleware.add(clientAuthMiddleware); handshake(new CredentialCallOption(new BasicAuthCredentialWriter(username, password))); @@ -218,27 +219,36 @@ public void handshake(CallOption... options) { * * @param descriptor FlightDescriptor the descriptor for the data * @param root VectorSchemaRoot the root containing data - * @param metadataListener A handler for metadata messages from the server. This will be passed buffers that will be - * freed after {@link StreamListener#onNext(Object)} is called! + * @param metadataListener A handler for metadata messages from the server. This will be passed + * buffers that will be freed after {@link StreamListener#onNext(Object)} is called! * @param options RPC-layer hints for this call. * @return ClientStreamListener an interface to control uploading data */ - public ClientStreamListener startPut(FlightDescriptor descriptor, VectorSchemaRoot root, - PutListener metadataListener, CallOption... options) { + public ClientStreamListener startPut( + FlightDescriptor descriptor, + VectorSchemaRoot root, + PutListener metadataListener, + CallOption... options) { return startPut(descriptor, root, new MapDictionaryProvider(), metadataListener, options); } /** * Create or append a descriptor with another stream. + * * @param descriptor FlightDescriptor the descriptor for the data * @param root VectorSchemaRoot the root containing data * @param metadataListener A handler for metadata messages from the server. * @param options RPC-layer hints for this call. - * @return ClientStreamListener an interface to control uploading data. - * {@link ClientStreamListener#start(VectorSchemaRoot, DictionaryProvider)} will already have been called. + * @return ClientStreamListener an interface to control uploading data. {@link + * ClientStreamListener#start(VectorSchemaRoot, DictionaryProvider)} will already have been + * called. */ - public ClientStreamListener startPut(FlightDescriptor descriptor, VectorSchemaRoot root, DictionaryProvider provider, - PutListener metadataListener, CallOption... options) { + public ClientStreamListener startPut( + FlightDescriptor descriptor, + VectorSchemaRoot root, + DictionaryProvider provider, + PutListener metadataListener, + CallOption... options) { Preconditions.checkNotNull(root, "root must not be null"); Preconditions.checkNotNull(provider, "provider must not be null"); final ClientStreamListener writer = startPut(descriptor, metadataListener, options); @@ -248,22 +258,26 @@ public ClientStreamListener startPut(FlightDescriptor descriptor, VectorSchemaRo /** * Create or append a descriptor with another stream. + * * @param descriptor FlightDescriptor the descriptor for the data * @param metadataListener A handler for metadata messages from the server. * @param options RPC-layer hints for this call. - * @return ClientStreamListener an interface to control uploading data. - * {@link ClientStreamListener#start(VectorSchemaRoot, DictionaryProvider)} will NOT already have been called. + * @return ClientStreamListener an interface to control uploading data. {@link + * ClientStreamListener#start(VectorSchemaRoot, DictionaryProvider)} will NOT already have + * been called. */ - public ClientStreamListener startPut(FlightDescriptor descriptor, PutListener metadataListener, - CallOption... options) { + public ClientStreamListener startPut( + FlightDescriptor descriptor, PutListener metadataListener, CallOption... options) { Preconditions.checkNotNull(descriptor, "descriptor must not be null"); Preconditions.checkNotNull(metadataListener, "metadataListener must not be null"); try { - final ClientCall call = asyncStubNewCall(doPutDescriptor, options); + final ClientCall call = + asyncStubNewCall(doPutDescriptor, options); final SetStreamObserver resultObserver = new SetStreamObserver(allocator, metadataListener); - ClientCallStreamObserver observer = (ClientCallStreamObserver) - ClientCalls.asyncBidiStreamingCall(call, resultObserver); + ClientCallStreamObserver observer = + (ClientCallStreamObserver) + ClientCalls.asyncBidiStreamingCall(call, resultObserver); return new PutObserver( descriptor, observer, metadataListener::isCancelled, metadataListener::getResult); } catch (StatusRuntimeException sre) { @@ -273,14 +287,17 @@ public ClientStreamListener startPut(FlightDescriptor descriptor, PutListener me /** * Get info on a stream. + * * @param descriptor The descriptor for the stream. * @param options RPC-layer hints for this call. */ public FlightInfo getInfo(FlightDescriptor descriptor, CallOption... options) { try { - return new FlightInfo(CallOptions.wrapStub(blockingStub, options).getFlightInfo(descriptor.toProtocol())); + return new FlightInfo( + CallOptions.wrapStub(blockingStub, options).getFlightInfo(descriptor.toProtocol())); } catch (URISyntaxException e) { - // We don't expect this will happen for conforming Flight implementations. For instance, a Java server + // We don't expect this will happen for conforming Flight implementations. For instance, a + // Java server // itself wouldn't be able to construct an invalid Location. throw new RuntimeException(e); } catch (StatusRuntimeException sre) { @@ -297,7 +314,8 @@ public FlightInfo getInfo(FlightDescriptor descriptor, CallOption... options) { */ public PollInfo pollInfo(FlightDescriptor descriptor, CallOption... options) { try { - return new PollInfo(CallOptions.wrapStub(blockingStub, options).pollFlightInfo(descriptor.toProtocol())); + return new PollInfo( + CallOptions.wrapStub(blockingStub, options).pollFlightInfo(descriptor.toProtocol())); } catch (URISyntaxException e) { throw new RuntimeException(e); } catch (StatusRuntimeException sre) { @@ -307,13 +325,14 @@ public PollInfo pollInfo(FlightDescriptor descriptor, CallOption... options) { /** * Get schema for a stream. + * * @param descriptor The descriptor for the stream. * @param options RPC-layer hints for this call. */ public SchemaResult getSchema(FlightDescriptor descriptor, CallOption... options) { try { - return SchemaResult.fromProtocol(CallOptions.wrapStub(blockingStub, options) - .getSchema(descriptor.toProtocol())); + return SchemaResult.fromProtocol( + CallOptions.wrapStub(blockingStub, options).getSchema(descriptor.toProtocol())); } catch (StatusRuntimeException sre) { throw StatusUtils.fromGrpcRuntimeException(sre); } @@ -321,23 +340,26 @@ public SchemaResult getSchema(FlightDescriptor descriptor, CallOption... options /** * Retrieve a stream from the server. + * * @param ticket The ticket granting access to the data stream. * @param options RPC-layer hints for this call. */ public FlightStream getStream(Ticket ticket, CallOption... options) { final ClientCall call = asyncStubNewCall(doGetDescriptor, options); - FlightStream stream = new FlightStream( - allocator, - PENDING_REQUESTS, - (String message, Throwable cause) -> call.cancel(message, cause), - (count) -> call.request(count)); + FlightStream stream = + new FlightStream( + allocator, + PENDING_REQUESTS, + (String message, Throwable cause) -> call.cancel(message, cause), + (count) -> call.request(count)); final StreamObserver delegate = stream.asObserver(); ClientResponseObserver clientResponseObserver = new ClientResponseObserver() { @Override - public void beforeStart(ClientCallStreamObserver requestStream) { + public void beforeStart( + ClientCallStreamObserver requestStream) { requestStream.disableAutoInboundFlowControl(); } @@ -355,7 +377,6 @@ public void onError(Throwable t) { public void onCompleted() { delegate.onCompleted(); } - }; ClientCalls.asyncServerStreamingCall(call, ticket.toProtocol(), clientResponseObserver); @@ -373,28 +394,34 @@ public ExchangeReaderWriter doExchange(FlightDescriptor descriptor, CallOption.. Preconditions.checkNotNull(descriptor, "descriptor must not be null"); try { - final ClientCall call = asyncStubNewCall(doExchangeDescriptor, options); - final FlightStream stream = new FlightStream(allocator, PENDING_REQUESTS, call::cancel, call::request); - final ClientCallStreamObserver observer = (ClientCallStreamObserver) + final ClientCall call = + asyncStubNewCall(doExchangeDescriptor, options); + final FlightStream stream = + new FlightStream(allocator, PENDING_REQUESTS, call::cancel, call::request); + final ClientCallStreamObserver observer = + (ClientCallStreamObserver) ClientCalls.asyncBidiStreamingCall(call, stream.asObserver()); - final ClientStreamListener writer = new PutObserver( - descriptor, observer, stream.cancelled::isDone, - () -> { - try { - stream.completed.get(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw CallStatus.INTERNAL - .withDescription("Client error: interrupted while completing call") - .withCause(e) - .toRuntimeException(); - } catch (ExecutionException e) { - throw CallStatus.INTERNAL - .withDescription("Client error: internal while completing call") - .withCause(e) - .toRuntimeException(); - } - }); + final ClientStreamListener writer = + new PutObserver( + descriptor, + observer, + stream.cancelled::isDone, + () -> { + try { + stream.completed.get(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw CallStatus.INTERNAL + .withDescription("Client error: interrupted while completing call") + .withCause(e) + .toRuntimeException(); + } catch (ExecutionException e) { + throw CallStatus.INTERNAL + .withDescription("Client error: internal while completing call") + .withCause(e) + .toRuntimeException(); + } + }); // Send the descriptor to start. try (final ArrowMessage message = new ArrowMessage(descriptor.toProtocol())) { observer.onNext(message); @@ -432,12 +459,13 @@ public ClientStreamListener getWriter() { /** * Make sure stream is drained. You must call this to be notified of any errors that may have - * happened after the exchange is complete. This should be called after `getWriter().completed()` - * and instead of `getWriter().getResult()`. + * happened after the exchange is complete. This should be called after + * `getWriter().completed()` and instead of `getWriter().getResult()`. */ public void getResult() { // After exchange is complete, make sure stream is drained to propagate errors through reader - while (reader.next()) { }; + while (reader.next()) {} + ; } /** Shut down the streams in this call. */ @@ -447,9 +475,7 @@ public void close() throws Exception { } } - /** - * A stream observer for Flight.PutResult - */ + /** A stream observer for Flight.PutResult */ private static class SetStreamObserver implements StreamObserver { private final BufferAllocator allocator; private final StreamListener listener; @@ -478,9 +504,7 @@ public void onCompleted() { } } - /** - * The implementation of a {@link ClientStreamListener} for writing data to a Flight server. - */ + /** The implementation of a {@link ClientStreamListener} for writing data to a Flight server. */ static class PutObserver extends OutboundStreamListenerImpl implements ClientStreamListener { private final BooleanSupplier isCancelled; private final Runnable getResult; @@ -493,8 +517,11 @@ static class PutObserver extends OutboundStreamListenerImpl implements ClientStr * @param isCancelled A flag to check if the call has been cancelled. * @param getResult A flag that blocks until the overall call completes. */ - PutObserver(FlightDescriptor descriptor, ClientCallStreamObserver observer, - BooleanSupplier isCancelled, Runnable getResult) { + PutObserver( + FlightDescriptor descriptor, + ClientCallStreamObserver observer, + BooleanSupplier isCancelled, + Runnable getResult) { super(descriptor, observer); Preconditions.checkNotNull(descriptor, "descriptor must be provided"); Preconditions.checkNotNull(isCancelled, "isCancelled must be provided"); @@ -526,8 +553,10 @@ public void getResult() { * @param options Call options. * @return The server response. */ - public CancelFlightInfoResult cancelFlightInfo(CancelFlightInfoRequest request, CallOption... options) { - Action action = new Action(FlightConstants.CANCEL_FLIGHT_INFO.getType(), request.serialize().array()); + public CancelFlightInfoResult cancelFlightInfo( + CancelFlightInfoRequest request, CallOption... options) { + Action action = + new Action(FlightConstants.CANCEL_FLIGHT_INFO.getType(), request.serialize().array()); Iterator results = doAction(action, options); if (!results.hasNext()) { throw CallStatus.INTERNAL @@ -544,8 +573,7 @@ public CancelFlightInfoResult cancelFlightInfo(CancelFlightInfoRequest request, .withCause(e) .toRuntimeException(); } - results.forEachRemaining((ignored) -> { - }); + results.forEachRemaining((ignored) -> {}); return result; } @@ -556,8 +584,10 @@ public CancelFlightInfoResult cancelFlightInfo(CancelFlightInfoRequest request, * @param options Call options. * @return The new endpoint with an updated expiration time. */ - public FlightEndpoint renewFlightEndpoint(RenewFlightEndpointRequest request, CallOption... options) { - Action action = new Action(FlightConstants.RENEW_FLIGHT_ENDPOINT.getType(), request.serialize().array()); + public FlightEndpoint renewFlightEndpoint( + RenewFlightEndpointRequest request, CallOption... options) { + Action action = + new Action(FlightConstants.RENEW_FLIGHT_ENDPOINT.getType(), request.serialize().array()); Iterator results = doAction(action, options); if (!results.hasNext()) { throw CallStatus.INTERNAL @@ -574,19 +604,16 @@ public FlightEndpoint renewFlightEndpoint(RenewFlightEndpointRequest request, Ca .withCause(e) .toRuntimeException(); } - results.forEachRemaining((ignored) -> { - }); + results.forEachRemaining((ignored) -> {}); return result; } - /** - * Interface for writers to an Arrow data stream. - */ + /** Interface for writers to an Arrow data stream. */ public interface ClientStreamListener extends OutboundStreamListener { /** - * Wait for the stream to finish on the server side. You must call this to be notified of any errors that may have - * happened during the upload. + * Wait for the stream to finish on the server side. You must call this to be notified of any + * errors that may have happened during the upload. */ void getResult(); } @@ -594,22 +621,22 @@ public interface ClientStreamListener extends OutboundStreamListener { /** * A handler for server-sent application metadata messages during a Flight DoPut operation. * - *

Generally, instead of implementing this yourself, you should use {@link AsyncPutListener} or {@link - * SyncPutListener}. + *

Generally, instead of implementing this yourself, you should use {@link AsyncPutListener} or + * {@link SyncPutListener}. */ public interface PutListener extends StreamListener { /** - * Wait for the stream to finish on the server side. You must call this to be notified of any errors that may have - * happened during the upload. + * Wait for the stream to finish on the server side. You must call this to be notified of any + * errors that may have happened during the upload. */ void getResult(); /** * Called when a message from the server is received. * - * @param val The application metadata. This buffer will be reclaimed once onNext returns; you must retain a - * reference to use it outside this method. + * @param val The application metadata. This buffer will be reclaimed once onNext returns; you + * must retain a reference to use it outside this method. */ @Override void onNext(PutResult val); @@ -617,32 +644,29 @@ public interface PutListener extends StreamListener { /** * Check if the call has been cancelled. * - *

By default, this always returns false. Implementations should provide an appropriate implementation, as - * otherwise, a DoPut operation may inadvertently block forever. + *

By default, this always returns false. Implementations should provide an appropriate + * implementation, as otherwise, a DoPut operation may inadvertently block forever. */ default boolean isCancelled() { return false; } } - /** - * Shut down this client. - */ + /** Shut down this client. */ @Override public void close() throws InterruptedException { channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); allocator.close(); } - /** - * Create a builder for a Flight client. - */ + /** Create a builder for a Flight client. */ public static Builder builder() { return new Builder(); } /** * Create a builder for a Flight client. + * * @param allocator The allocator to use for the client. * @param location The location to connect to. */ @@ -650,9 +674,7 @@ public static Builder builder(BufferAllocator allocator, Location location) { return new Builder(allocator, location); } - /** - * A builder for Flight clients. - */ + /** A builder for Flight clients. */ public static final class Builder { private BufferAllocator allocator; private Location location; @@ -665,17 +687,14 @@ public static final class Builder { private List middleware = new ArrayList<>(); private boolean verifyServer = true; - private Builder() { - } + private Builder() {} private Builder(BufferAllocator allocator, Location location) { this.allocator = Preconditions.checkNotNull(allocator); this.location = Preconditions.checkNotNull(location); } - /** - * Force the client to connect over TLS. - */ + /** Force the client to connect over TLS. */ public Builder useTls() { this.forceTls = true; return this; @@ -701,7 +720,8 @@ public Builder trustedCertificates(final InputStream stream) { } /** Set the trusted TLS certificates. */ - public Builder clientCertificate(final InputStream clientCertificate, final InputStream clientKey) { + public Builder clientCertificate( + final InputStream clientCertificate, final InputStream clientKey) { Preconditions.checkNotNull(clientKey); this.clientCertificate = Preconditions.checkNotNull(clientCertificate); this.clientKey = Preconditions.checkNotNull(clientKey); @@ -728,51 +748,59 @@ public Builder verifyServer(boolean verifyServer) { return this; } - /** - * Create the client from this builder. - */ + /** Create the client from this builder. */ public FlightClient build() { final NettyChannelBuilder builder; switch (location.getUri().getScheme()) { case LocationSchemes.GRPC: case LocationSchemes.GRPC_INSECURE: - case LocationSchemes.GRPC_TLS: { - builder = NettyChannelBuilder.forAddress(location.toSocketAddress()); - break; - } - case LocationSchemes.GRPC_DOMAIN_SOCKET: { - // The implementation is platform-specific, so we have to find the classes at runtime - builder = NettyChannelBuilder.forAddress(location.toSocketAddress()); - try { + case LocationSchemes.GRPC_TLS: + { + builder = NettyChannelBuilder.forAddress(location.toSocketAddress()); + break; + } + case LocationSchemes.GRPC_DOMAIN_SOCKET: + { + // The implementation is platform-specific, so we have to find the classes at runtime + builder = NettyChannelBuilder.forAddress(location.toSocketAddress()); try { - // Linux - builder.channelType( - Class.forName("io.netty.channel.epoll.EpollDomainSocketChannel") - .asSubclass(ServerChannel.class)); - final EventLoopGroup elg = - Class.forName("io.netty.channel.epoll.EpollEventLoopGroup").asSubclass(EventLoopGroup.class) - .getDeclaredConstructor().newInstance(); - builder.eventLoopGroup(elg); - } catch (ClassNotFoundException e) { - // BSD - builder.channelType( - Class.forName("io.netty.channel.kqueue.KQueueDomainSocketChannel") - .asSubclass(ServerChannel.class)); - final EventLoopGroup elg = Class.forName("io.netty.channel.kqueue.KQueueEventLoopGroup") - .asSubclass(EventLoopGroup.class) - .getDeclaredConstructor().newInstance(); - builder.eventLoopGroup(elg); + try { + // Linux + builder.channelType( + Class.forName("io.netty.channel.epoll.EpollDomainSocketChannel") + .asSubclass(ServerChannel.class)); + final EventLoopGroup elg = + Class.forName("io.netty.channel.epoll.EpollEventLoopGroup") + .asSubclass(EventLoopGroup.class) + .getDeclaredConstructor() + .newInstance(); + builder.eventLoopGroup(elg); + } catch (ClassNotFoundException e) { + // BSD + builder.channelType( + Class.forName("io.netty.channel.kqueue.KQueueDomainSocketChannel") + .asSubclass(ServerChannel.class)); + final EventLoopGroup elg = + Class.forName("io.netty.channel.kqueue.KQueueEventLoopGroup") + .asSubclass(EventLoopGroup.class) + .getDeclaredConstructor() + .newInstance(); + builder.eventLoopGroup(elg); + } + } catch (ClassNotFoundException + | InstantiationException + | IllegalAccessException + | NoSuchMethodException + | InvocationTargetException e) { + throw new UnsupportedOperationException( + "Could not find suitable Netty native transport implementation for domain socket address."); } - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | - NoSuchMethodException | InvocationTargetException e) { - throw new UnsupportedOperationException( - "Could not find suitable Netty native transport implementation for domain socket address."); + break; } - break; - } default: - throw new IllegalArgumentException("Scheme is not supported: " + location.getUri().getScheme()); + throw new IllegalArgumentException( + "Scheme is not supported: " + location.getUri().getScheme()); } if (this.forceTls || LocationSchemes.GRPC_TLS.equals(location.getUri().getScheme())) { @@ -781,15 +809,18 @@ public FlightClient build() { final boolean hasTrustedCerts = this.trustedCertificates != null; final boolean hasKeyCertPair = this.clientCertificate != null && this.clientKey != null; if (!this.verifyServer && (hasTrustedCerts || hasKeyCertPair)) { - throw new IllegalArgumentException("FlightClient has been configured to disable server verification, " + - "but certificate options have been specified."); + throw new IllegalArgumentException( + "FlightClient has been configured to disable server verification, " + + "but certificate options have been specified."); } final SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient(); if (!this.verifyServer) { sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE); - } else if (this.trustedCertificates != null || this.clientCertificate != null || this.clientKey != null) { + } else if (this.trustedCertificates != null + || this.clientCertificate != null + || this.clientKey != null) { if (this.trustedCertificates != null) { sslContextBuilder.trustManager(this.trustedCertificates); } @@ -810,19 +841,17 @@ public FlightClient build() { builder.usePlaintext(); } - builder - .maxTraceEvents(MAX_CHANNEL_TRACE_EVENTS) - .maxInboundMessageSize(maxInboundMessageSize); + builder.maxTraceEvents(MAX_CHANNEL_TRACE_EVENTS).maxInboundMessageSize(maxInboundMessageSize); return new FlightClient(allocator, builder.build(), middleware); } } /** - * Helper method to create a call from the asyncStub, method descriptor, and list of calling options. + * Helper method to create a call from the asyncStub, method descriptor, and list of calling + * options. */ private ClientCall asyncStubNewCall( - MethodDescriptor descriptor, - CallOption... options) { + MethodDescriptor descriptor, CallOption... options) { FlightServiceStub wrappedStub = CallOptions.wrapStub(asyncStub, options); return wrappedStub.getChannel().newCall(descriptor, wrappedStub.getCallOptions()); } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightClientMiddleware.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightClientMiddleware.java index 1528ca6c67e..467516ae01f 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightClientMiddleware.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightClientMiddleware.java @@ -23,29 +23,22 @@ *

Middleware are instantiated per-call and should store state in the middleware instance. */ public interface FlightClientMiddleware { - /** - * A callback used before request headers are sent. The headers may be manipulated. - */ + /** A callback used before request headers are sent. The headers may be manipulated. */ void onBeforeSendingHeaders(CallHeaders outgoingHeaders); - /** - * A callback called after response headers are received. The headers may be manipulated. - */ + /** A callback called after response headers are received. The headers may be manipulated. */ void onHeadersReceived(CallHeaders incomingHeaders); - /** - * A callback called after the call completes. - */ + /** A callback called after the call completes. */ void onCallCompleted(CallStatus status); - /** - * A factory for client middleware instances. - */ + /** A factory for client middleware instances. */ interface Factory { /** * Create a new middleware instance for the given call. * - * @throws FlightRuntimeException if the middleware wants to reject the call with the given status + * @throws FlightRuntimeException if the middleware wants to reject the call with the given + * status */ FlightClientMiddleware onCallStarted(CallInfo info); } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightConstants.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightConstants.java index 2a240abad6d..c8549d42bd7 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightConstants.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightConstants.java @@ -17,9 +17,7 @@ package org.apache.arrow.flight; -/** - * String constants relevant to flight implementations. - */ +/** String constants relevant to flight implementations. */ public interface FlightConstants { String SERVICE = "arrow.flight.protocol.FlightService"; @@ -27,12 +25,16 @@ public interface FlightConstants { FlightServerMiddleware.Key HEADER_KEY = FlightServerMiddleware.Key.of("org.apache.arrow.flight.ServerHeaderMiddleware"); - ActionType CANCEL_FLIGHT_INFO = new ActionType("CancelFlightInfo", - "Explicitly cancel a running FlightInfo.\n" + - "Request Message: CancelFlightInfoRequest\n" + - "Response Message: CancelFlightInfoResult"); - ActionType RENEW_FLIGHT_ENDPOINT = new ActionType("RenewFlightEndpoint", - "Extend expiration time of the given FlightEndpoint.\n" + - "Request Message: RenewFlightEndpointRequest\n" + - "Response Message: Renewed FlightEndpoint"); + ActionType CANCEL_FLIGHT_INFO = + new ActionType( + "CancelFlightInfo", + "Explicitly cancel a running FlightInfo.\n" + + "Request Message: CancelFlightInfoRequest\n" + + "Response Message: CancelFlightInfoResult"); + ActionType RENEW_FLIGHT_ENDPOINT = + new ActionType( + "RenewFlightEndpoint", + "Extend expiration time of the given FlightEndpoint.\n" + + "Request Message: RenewFlightEndpointRequest\n" + + "Response Message: Renewed FlightEndpoint"); } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightDescriptor.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightDescriptor.java index 1836f2edd94..9adb0c6410f 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightDescriptor.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightDescriptor.java @@ -17,22 +17,20 @@ package org.apache.arrow.flight; +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; +import com.google.protobuf.ByteString; import java.io.IOException; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.List; - import org.apache.arrow.flight.impl.Flight; import org.apache.arrow.flight.impl.Flight.FlightDescriptor.DescriptorType; import org.apache.arrow.util.Preconditions; -import com.google.common.base.Joiner; -import com.google.common.collect.ImmutableList; -import com.google.protobuf.ByteString; - /** - * An identifier for a particular set of data. This can either be an opaque command that generates - * the data or a static "path" to the data. This is a POJO wrapper around the protobuf message with + * An identifier for a particular set of data. This can either be an opaque command that generates + * the data or a static "path" to the data. This is a POJO wrapper around the protobuf message with * the same name. */ public class FlightDescriptor { @@ -56,7 +54,7 @@ public static FlightDescriptor path(Iterable path) { return new FlightDescriptor(false, ImmutableList.copyOf(path), null); } - public static FlightDescriptor path(String...path) { + public static FlightDescriptor path(String... path) { return new FlightDescriptor(false, ImmutableList.copyOf(path), null); } @@ -98,7 +96,8 @@ Flight.FlightDescriptor toProtocol() { /** * Get the serialized form of this protocol message. * - *

Intended to help interoperability by allowing non-Flight services to still return Flight types. + *

Intended to help interoperability by allowing non-Flight services to still return Flight + * types. */ public ByteBuffer serialize() { return ByteBuffer.wrap(toProtocol().toByteArray()); @@ -107,9 +106,11 @@ public ByteBuffer serialize() { /** * Parse the serialized form of this protocol message. * - *

Intended to help interoperability by allowing Flight clients to obtain stream info from non-Flight services. + *

Intended to help interoperability by allowing Flight clients to obtain stream info from + * non-Flight services. * - * @param serialized The serialized form of the FlightDescriptor, as returned by {@link #serialize()}. + * @param serialized The serialized form of the FlightDescriptor, as returned by {@link + * #serialize()}. * @return The deserialized FlightDescriptor. * @throws IOException if the serialized form is invalid. */ @@ -175,6 +176,4 @@ public boolean equals(Object obj) { } return true; } - - } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightEndpoint.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightEndpoint.java index 41ead8e1fcd..ee6cf790893 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightEndpoint.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightEndpoint.java @@ -17,6 +17,9 @@ package org.apache.arrow.flight; +import com.google.protobuf.ByteString; +import com.google.protobuf.Timestamp; +import com.google.protobuf.util.Timestamps; import java.io.IOException; import java.net.URISyntaxException; import java.nio.ByteBuffer; @@ -28,16 +31,9 @@ import java.util.List; import java.util.Objects; import java.util.Optional; - import org.apache.arrow.flight.impl.Flight; -import com.google.protobuf.ByteString; -import com.google.protobuf.Timestamp; -import com.google.protobuf.util.Timestamps; - -/** - * POJO to convert to/from the underlying protobuf FlightEndpoint. - */ +/** POJO to convert to/from the underlying protobuf FlightEndpoint. */ public class FlightEndpoint { private final List locations; private final Ticket ticket; @@ -48,10 +44,10 @@ public class FlightEndpoint { * Constructs a new endpoint with no expiration time. * * @param ticket A ticket that describe the key of a data stream. - * @param locations The possible locations the stream can be retrieved from. + * @param locations The possible locations the stream can be retrieved from. */ public FlightEndpoint(Ticket ticket, Location... locations) { - this(ticket, /*expirationTime*/null, locations); + this(ticket, /*expirationTime*/ null, locations); } /** @@ -59,16 +55,19 @@ public FlightEndpoint(Ticket ticket, Location... locations) { * * @param ticket A ticket that describe the key of a data stream. * @param expirationTime (optional) When this endpoint expires. - * @param locations The possible locations the stream can be retrieved from. + * @param locations The possible locations the stream can be retrieved from. */ public FlightEndpoint(Ticket ticket, Instant expirationTime, Location... locations) { - this(ticket, expirationTime, null, Collections.unmodifiableList(new ArrayList<>(Arrays.asList(locations)))); + this( + ticket, + expirationTime, + null, + Collections.unmodifiableList(new ArrayList<>(Arrays.asList(locations)))); } - /** - * Private constructor with all parameters. Should only be called by Builder. - */ - private FlightEndpoint(Ticket ticket, Instant expirationTime, byte[] appMetadata, List locations) { + /** Private constructor with all parameters. Should only be called by Builder. */ + private FlightEndpoint( + Ticket ticket, Instant expirationTime, byte[] appMetadata, List locations) { Objects.requireNonNull(ticket); this.locations = locations; this.expirationTime = expirationTime; @@ -76,17 +75,16 @@ private FlightEndpoint(Ticket ticket, Instant expirationTime, byte[] appMetadata this.appMetadata = appMetadata; } - /** - * Constructs from the protocol buffer representation. - */ + /** Constructs from the protocol buffer representation. */ FlightEndpoint(Flight.FlightEndpoint flt) throws URISyntaxException { this.locations = new ArrayList<>(); for (final Flight.Location location : flt.getLocationList()) { this.locations.add(new Location(location.getUri())); } if (flt.hasExpirationTime()) { - this.expirationTime = Instant.ofEpochSecond( - flt.getExpirationTime().getSeconds(), Timestamps.toNanos(flt.getExpirationTime())); + this.expirationTime = + Instant.ofEpochSecond( + flt.getExpirationTime().getSeconds(), Timestamps.toNanos(flt.getExpirationTime())); } else { this.expirationTime = null; } @@ -110,12 +108,10 @@ public byte[] getAppMetadata() { return appMetadata; } - /** - * Converts to the protocol buffer representation. - */ + /** Converts to the protocol buffer representation. */ Flight.FlightEndpoint toProtocol() { - Flight.FlightEndpoint.Builder b = Flight.FlightEndpoint.newBuilder() - .setTicket(ticket.toProtocol()); + Flight.FlightEndpoint.Builder b = + Flight.FlightEndpoint.newBuilder().setTicket(ticket.toProtocol()); for (Location l : locations) { b.addLocation(l.toProtocol()); @@ -139,7 +135,8 @@ Flight.FlightEndpoint toProtocol() { /** * Get the serialized form of this protocol message. * - *

Intended to help interoperability by allowing non-Flight services to still return Flight types. + *

Intended to help interoperability by allowing non-Flight services to still return Flight + * types. */ public ByteBuffer serialize() { return ByteBuffer.wrap(toProtocol().toByteArray()); @@ -148,14 +145,16 @@ public ByteBuffer serialize() { /** * Parse the serialized form of this protocol message. * - *

Intended to help interoperability by allowing Flight clients to obtain stream info from non-Flight services. + *

Intended to help interoperability by allowing Flight clients to obtain stream info from + * non-Flight services. * * @param serialized The serialized form of the message, as returned by {@link #serialize()}. * @return The deserialized message. * @throws IOException if the serialized form is invalid. * @throws URISyntaxException if the serialized form contains an unsupported URI format. */ - public static FlightEndpoint deserialize(ByteBuffer serialized) throws IOException, URISyntaxException { + public static FlightEndpoint deserialize(ByteBuffer serialized) + throws IOException, URISyntaxException { return new FlightEndpoint(Flight.FlightEndpoint.parseFrom(serialized)); } @@ -168,10 +167,10 @@ public boolean equals(Object o) { return false; } FlightEndpoint that = (FlightEndpoint) o; - return locations.equals(that.locations) && - ticket.equals(that.ticket) && - Objects.equals(expirationTime, that.expirationTime) && - Arrays.equals(appMetadata, that.appMetadata); + return locations.equals(that.locations) + && ticket.equals(that.ticket) + && Objects.equals(expirationTime, that.expirationTime) + && Arrays.equals(appMetadata, that.appMetadata); } @Override @@ -181,27 +180,29 @@ public int hashCode() { @Override public String toString() { - return "FlightEndpoint{" + - "locations=" + locations + - ", ticket=" + ticket + - ", expirationTime=" + (expirationTime == null ? "(none)" : expirationTime.toString()) + - ", appMetadata=" + (appMetadata == null ? "(none)" : Base64.getEncoder().encodeToString(appMetadata)) + - '}'; + return "FlightEndpoint{" + + "locations=" + + locations + + ", ticket=" + + ticket + + ", expirationTime=" + + (expirationTime == null ? "(none)" : expirationTime.toString()) + + ", appMetadata=" + + (appMetadata == null ? "(none)" : Base64.getEncoder().encodeToString(appMetadata)) + + '}'; } /** * Create a builder for FlightEndpoint. * * @param ticket A ticket that describe the key of a data stream. - * @param locations The possible locations the stream can be retrieved from. + * @param locations The possible locations the stream can be retrieved from. */ public static Builder builder(Ticket ticket, Location... locations) { return new Builder(ticket, locations); } - /** - * Builder for FlightEndpoint. - */ + /** Builder for FlightEndpoint. */ public static final class Builder { private final Ticket ticket; private final List locations; @@ -233,9 +234,7 @@ public Builder setAppMetadata(byte[] appMetadata) { return this; } - /** - * Build FlightEndpoint object. - */ + /** Build FlightEndpoint object. */ public FlightEndpoint build() { return new FlightEndpoint(ticket, expirationTime, appMetadata, locations); } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightGrpcUtils.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightGrpcUtils.java index eb5e492b4cd..7796a9e74ba 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightGrpcUtils.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightGrpcUtils.java @@ -17,28 +17,22 @@ package org.apache.arrow.flight; -import java.util.Collections; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.TimeUnit; - -import org.apache.arrow.flight.auth.ServerAuthHandler; -import org.apache.arrow.memory.BufferAllocator; -import org.apache.arrow.util.VisibleForTesting; - import io.grpc.BindableService; import io.grpc.CallOptions; import io.grpc.ClientCall; import io.grpc.ConnectivityState; import io.grpc.ManagedChannel; import io.grpc.MethodDescriptor; +import java.util.Collections; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; +import org.apache.arrow.flight.auth.ServerAuthHandler; +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.util.VisibleForTesting; -/** - * Exposes Flight GRPC service & client. - */ +/** Exposes Flight GRPC service & client. */ public class FlightGrpcUtils { - /** - * Proxy class for ManagedChannel that makes closure a no-op. - */ + /** Proxy class for ManagedChannel that makes closure a no-op. */ @VisibleForTesting static class NonClosingProxyManagedChannel extends ManagedChannel { private final ManagedChannel channel; @@ -107,9 +101,12 @@ public ConnectivityState getState(boolean requestConnection) { @Override public void notifyWhenStateChanged(ConnectivityState source, Runnable callback) { - // The proxy has no insight into the underlying channel state changes, so we'll have to leak the abstraction - // a bit here and simply pass to the underlying channel, even though it will never transition to shutdown via - // the proxy. This should be fine, since it's mainly targeted at the FlightClient and there's no getter for + // The proxy has no insight into the underlying channel state changes, so we'll have to leak + // the abstraction + // a bit here and simply pass to the underlying channel, even though it will never transition + // to shutdown via + // the proxy. This should be fine, since it's mainly targeted at the FlightClient and there's + // no getter for // the channel. this.channel.notifyWhenStateChanged(source, callback); } @@ -129,33 +126,42 @@ private FlightGrpcUtils() {} /** * Creates a Flight service. + * * @param allocator Memory allocator * @param producer Specifies the service api * @param authHandler Authentication handler * @param executor Executor service * @return FlightBindingService */ - public static BindableService createFlightService(BufferAllocator allocator, FlightProducer producer, - ServerAuthHandler authHandler, ExecutorService executor) { + public static BindableService createFlightService( + BufferAllocator allocator, + FlightProducer producer, + ServerAuthHandler authHandler, + ExecutorService executor) { return new FlightBindingService(allocator, producer, authHandler, executor); } /** * Creates a Flight client. - * @param incomingAllocator Memory allocator + * + * @param incomingAllocator Memory allocator * @param channel provides a connection to a gRPC server. */ - public static FlightClient createFlightClient(BufferAllocator incomingAllocator, ManagedChannel channel) { + public static FlightClient createFlightClient( + BufferAllocator incomingAllocator, ManagedChannel channel) { return new FlightClient(incomingAllocator, channel, Collections.emptyList()); } /** * Creates a Flight client. - * @param incomingAllocator Memory allocator - * @param channel provides a connection to a gRPC server. Will not be closed on closure of the returned FlightClient. + * + * @param incomingAllocator Memory allocator + * @param channel provides a connection to a gRPC server. Will not be closed on closure of the + * returned FlightClient. */ public static FlightClient createFlightClientWithSharedChannel( BufferAllocator incomingAllocator, ManagedChannel channel) { - return new FlightClient(incomingAllocator, new NonClosingProxyManagedChannel(channel), Collections.emptyList()); + return new FlightClient( + incomingAllocator, new NonClosingProxyManagedChannel(channel), Collections.emptyList()); } } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightInfo.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightInfo.java index 39e5f5e3a3e..788abdeb416 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightInfo.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightInfo.java @@ -17,6 +17,8 @@ package org.apache.arrow.flight; +import com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream; +import com.google.protobuf.ByteString; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URISyntaxException; @@ -30,7 +32,6 @@ import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; - import org.apache.arrow.flight.impl.Flight; import org.apache.arrow.vector.ipc.ReadChannel; import org.apache.arrow.vector.ipc.WriteChannel; @@ -39,12 +40,7 @@ import org.apache.arrow.vector.types.pojo.Schema; import org.apache.arrow.vector.validate.MetadataV4UnionChecker; -import com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream; -import com.google.protobuf.ByteString; - -/** - * A POJO representation of a FlightInfo, metadata associated with a set of data records. - */ +/** A POJO representation of a FlightInfo, metadata associated with a set of data records. */ public class FlightInfo { private final Schema schema; private final FlightDescriptor descriptor; @@ -64,7 +60,11 @@ public class FlightInfo { * @param bytes The number of bytes in the flight * @param records The number of records in the flight. */ - public FlightInfo(Schema schema, FlightDescriptor descriptor, List endpoints, long bytes, + public FlightInfo( + Schema schema, + FlightDescriptor descriptor, + List endpoints, + long bytes, long records) { this(schema, descriptor, endpoints, bytes, records, /*ordered*/ false, IpcOption.DEFAULT); } @@ -79,8 +79,13 @@ public FlightInfo(Schema schema, FlightDescriptor descriptor, List endpoints, long bytes, - long records, IpcOption option) { + public FlightInfo( + Schema schema, + FlightDescriptor descriptor, + List endpoints, + long bytes, + long records, + IpcOption option) { this(schema, descriptor, endpoints, bytes, records, /*ordered*/ false, option); } @@ -95,8 +100,14 @@ public FlightInfo(Schema schema, FlightDescriptor descriptor, List endpoints, long bytes, - long records, boolean ordered, IpcOption option) { + public FlightInfo( + Schema schema, + FlightDescriptor descriptor, + List endpoints, + long bytes, + long records, + boolean ordered, + IpcOption option) { this(schema, descriptor, endpoints, bytes, records, ordered, option, null); } @@ -112,8 +123,15 @@ public FlightInfo(Schema schema, FlightDescriptor descriptor, List endpoints, long bytes, - long records, boolean ordered, IpcOption option, byte[] appMetadata) { + public FlightInfo( + Schema schema, + FlightDescriptor descriptor, + List endpoints, + long bytes, + long records, + boolean ordered, + IpcOption option, + byte[] appMetadata) { Objects.requireNonNull(descriptor); Objects.requireNonNull(endpoints); if (schema != null) { @@ -129,18 +147,15 @@ public FlightInfo(Schema schema, FlightDescriptor descriptor, List 0 ? - MessageSerializer.deserializeSchema( - new ReadChannel( - Channels.newChannel( - new ByteBufferBackedInputStream(schemaBuf)))) - : null; + schema = + pbFlightInfo.getSchema().size() > 0 + ? MessageSerializer.deserializeSchema( + new ReadChannel(Channels.newChannel(new ByteBufferBackedInputStream(schemaBuf)))) + : null; } catch (IOException e) { throw new RuntimeException(e); } @@ -152,7 +167,10 @@ public FlightInfo(Schema schema, FlightDescriptor descriptor, List getSchemaOptional() { /** * Returns the schema, or an empty schema if no schema is present. + * * @deprecated Deprecated. Use {@link #getSchemaOptional()} instead. */ @Deprecated @@ -193,12 +212,12 @@ public byte[] getAppMetadata() { return appMetadata; } - /** - * Converts to the protocol buffer representation. - */ + /** Converts to the protocol buffer representation. */ Flight.FlightInfo toProtocol() { - Flight.FlightInfo.Builder builder = Flight.FlightInfo.newBuilder() - .addAllEndpoint(endpoints.stream().map(t -> t.toProtocol()).collect(Collectors.toList())) + Flight.FlightInfo.Builder builder = + Flight.FlightInfo.newBuilder() + .addAllEndpoint( + endpoints.stream().map(t -> t.toProtocol()).collect(Collectors.toList())) .setFlightDescriptor(descriptor.toProtocol()) .setTotalBytes(FlightInfo.this.bytes) .setTotalRecords(records) @@ -206,10 +225,7 @@ Flight.FlightInfo toProtocol() { if (schema != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { - MessageSerializer.serialize( - new WriteChannel(Channels.newChannel(baos)), - schema, - option); + MessageSerializer.serialize(new WriteChannel(Channels.newChannel(baos)), schema, option); builder.setSchema(ByteString.copyFrom(baos.toByteArray())); } catch (IOException e) { throw new RuntimeException(e); @@ -224,7 +240,8 @@ Flight.FlightInfo toProtocol() { /** * Get the serialized form of this protocol message. * - *

Intended to help interoperability by allowing non-Flight services to still return Flight types. + *

Intended to help interoperability by allowing non-Flight services to still return Flight + * types. */ public ByteBuffer serialize() { return ByteBuffer.wrap(toProtocol().toByteArray()); @@ -233,14 +250,16 @@ public ByteBuffer serialize() { /** * Parse the serialized form of this protocol message. * - *

Intended to help interoperability by allowing Flight clients to obtain stream info from non-Flight services. + *

Intended to help interoperability by allowing Flight clients to obtain stream info from + * non-Flight services. * * @param serialized The serialized form of the FlightInfo, as returned by {@link #serialize()}. * @return The deserialized FlightInfo. * @throws IOException if the serialized form is invalid. * @throws URISyntaxException if the serialized form contains an unsupported URI format. */ - public static FlightInfo deserialize(ByteBuffer serialized) throws IOException, URISyntaxException { + public static FlightInfo deserialize(ByteBuffer serialized) + throws IOException, URISyntaxException { return new FlightInfo(Flight.FlightInfo.parseFrom(serialized)); } @@ -253,47 +272,54 @@ public boolean equals(Object o) { return false; } FlightInfo that = (FlightInfo) o; - return bytes == that.bytes && - records == that.records && - schema.equals(that.schema) && - descriptor.equals(that.descriptor) && - endpoints.equals(that.endpoints) && - ordered == that.ordered && - Arrays.equals(appMetadata, that.appMetadata); + return bytes == that.bytes + && records == that.records + && schema.equals(that.schema) + && descriptor.equals(that.descriptor) + && endpoints.equals(that.endpoints) + && ordered == that.ordered + && Arrays.equals(appMetadata, that.appMetadata); } @Override public int hashCode() { - return Objects.hash(schema, descriptor, endpoints, bytes, records, ordered, Arrays.hashCode(appMetadata)); + return Objects.hash( + schema, descriptor, endpoints, bytes, records, ordered, Arrays.hashCode(appMetadata)); } @Override public String toString() { - return "FlightInfo{" + - "schema=" + schema + - ", descriptor=" + descriptor + - ", endpoints=" + endpoints + - ", bytes=" + bytes + - ", records=" + records + - ", ordered=" + ordered + - ", appMetadata=" + (appMetadata == null ? "(none)" : Base64.getEncoder().encodeToString(appMetadata)) + - '}'; + return "FlightInfo{" + + "schema=" + + schema + + ", descriptor=" + + descriptor + + ", endpoints=" + + endpoints + + ", bytes=" + + bytes + + ", records=" + + records + + ", ordered=" + + ordered + + ", appMetadata=" + + (appMetadata == null ? "(none)" : Base64.getEncoder().encodeToString(appMetadata)) + + '}'; } /** * Create a builder for FlightInfo. * - * @param schema The schema of the Flight + * @param schema The schema of the Flight * @param descriptor An identifier for the Flight. - * @param endpoints A list of endpoints that have the flight available. + * @param endpoints A list of endpoints that have the flight available. */ - public static Builder builder(Schema schema, FlightDescriptor descriptor, List endpoints) { + public static Builder builder( + Schema schema, FlightDescriptor descriptor, List endpoints) { return new Builder(schema, descriptor, endpoints); } - /** - * Builder for FlightInfo. - */ + /** Builder for FlightInfo. */ public static final class Builder { private final Schema schema; private final FlightDescriptor descriptor; @@ -360,11 +386,10 @@ public Builder setAppMetadata(byte[] appMetadata) { return this; } - /** - * Build FlightInfo object. - */ + /** Build FlightInfo object. */ public FlightInfo build() { - return new FlightInfo(schema, descriptor, endpoints, bytes, records, ordered, option, appMetadata); + return new FlightInfo( + schema, descriptor, endpoints, bytes, records, ordered, option, appMetadata); } } } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightMethod.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightMethod.java index b1331a8edd6..f848857309a 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightMethod.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightMethod.java @@ -19,9 +19,7 @@ import org.apache.arrow.flight.impl.FlightServiceGrpc; -/** - * All the RPC methods available in Flight. - */ +/** All the RPC methods available in Flight. */ public enum FlightMethod { HANDSHAKE, LIST_FLIGHTS, diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightProducer.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightProducer.java index cdc29ae5de4..9e0132f2c25 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightProducer.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightProducer.java @@ -19,9 +19,7 @@ import java.util.Map; -/** - * API to Implement an Arrow Flight producer. - */ +/** API to Implement an Arrow Flight producer. */ public interface FlightProducer { /** @@ -40,8 +38,7 @@ public interface FlightProducer { * @param criteria Application-defined criteria for filtering streams. * @param listener An interface for sending data back to the client. */ - void listFlights(CallContext context, Criteria criteria, - StreamListener listener); + void listFlights(CallContext context, Criteria criteria, StreamListener listener); /** * Get information about a particular data stream. @@ -55,9 +52,9 @@ void listFlights(CallContext context, Criteria criteria, /** * Begin or get an update on execution of a long-running query. * - *

If the descriptor would begin a query, the server should return a response immediately to not - * block the client. Otherwise, the server should not return an update until progress is made to - * not spam the client with inactionable updates. + *

If the descriptor would begin a query, the server should return a response immediately to + * not block the client. Otherwise, the server should not return an update until progress is made + * to not spam the client with inactionable updates. * * @param context Per-call context. * @param descriptor The descriptor identifying the data stream. @@ -77,26 +74,28 @@ default PollInfo pollFlightInfo(CallContext context, FlightDescriptor descriptor */ default SchemaResult getSchema(CallContext context, FlightDescriptor descriptor) { FlightInfo info = getFlightInfo(context, descriptor); - return new SchemaResult(info - .getSchemaOptional() - .orElseThrow(() -> - CallStatus - .INVALID_ARGUMENT - .withDescription("No schema is present in FlightInfo").toRuntimeException())); + return new SchemaResult( + info.getSchemaOptional() + .orElseThrow( + () -> + CallStatus.INVALID_ARGUMENT + .withDescription("No schema is present in FlightInfo") + .toRuntimeException())); } - /** * Accept uploaded data for a particular stream. * * @param context Per-call context. * @param flightStream The data stream being uploaded. */ - Runnable acceptPut(CallContext context, - FlightStream flightStream, StreamListener ackStream); + Runnable acceptPut( + CallContext context, FlightStream flightStream, StreamListener ackStream); default void doExchange(CallContext context, FlightStream reader, ServerStreamListener writer) { - throw CallStatus.UNIMPLEMENTED.withDescription("DoExchange is unimplemented").toRuntimeException(); + throw CallStatus.UNIMPLEMENTED + .withDescription("DoExchange is unimplemented") + .toRuntimeException(); } /** @@ -106,32 +105,29 @@ default void doExchange(CallContext context, FlightStream reader, ServerStreamLi * @param action Client-supplied parameters. * @param listener A stream of responses. */ - void doAction(CallContext context, Action action, - StreamListener listener); + void doAction(CallContext context, Action action, StreamListener listener); /** * List available application-defined RPCs. + * * @param context Per-call context. * @param listener An interface for sending data back to the client. */ void listActions(CallContext context, StreamListener listener); - /** - * An interface for sending Arrow data back to a client. - */ + /** An interface for sending Arrow data back to a client. */ interface ServerStreamListener extends OutboundStreamListener { - /** - * Check whether the call has been cancelled. If so, stop sending data. - */ + /** Check whether the call has been cancelled. If so, stop sending data. */ boolean isCancelled(); /** - * Set a callback for when the client cancels a call, i.e. {@link #isCancelled()} has become true. + * Set a callback for when the client cancels a call, i.e. {@link #isCancelled()} has become + * true. * - *

Note that this callback may only be called some time after {@link #isCancelled()} becomes true, and may never - * be called if all executor threads on the server are busy, or the RPC method body is implemented in a blocking - * fashion. + *

Note that this callback may only be called some time after {@link #isCancelled()} becomes + * true, and may never be called if all executor threads on the server are busy, or the RPC + * method body is implemented in a blocking fashion. */ void setOnCancelHandler(Runnable handler); } @@ -143,9 +139,7 @@ interface ServerStreamListener extends OutboundStreamListener { */ interface StreamListener { - /** - * Send the next value to the client. - */ + /** Send the next value to the client. */ void onNext(T val); /** @@ -155,16 +149,11 @@ interface StreamListener { */ void onError(Throwable t); - /** - * Indicate that the transmission is finished. - */ + /** Indicate that the transmission is finished. */ void onCompleted(); - } - /** - * Call-specific context. - */ + /** Call-specific context. */ interface CallContext { /** The identity of the authenticated peer. May be the empty string if unknown. */ String peerIdentity(); diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightRuntimeException.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightRuntimeException.java index 76d3349a2c3..d5d1e2b2627 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightRuntimeException.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightRuntimeException.java @@ -20,15 +20,13 @@ /** * An exception raised from a Flight RPC. * - *

In service implementations, raising an instance of this exception will provide clients with a more detailed - * message and error code. + *

In service implementations, raising an instance of this exception will provide clients with a + * more detailed message and error code. */ public class FlightRuntimeException extends RuntimeException { private final CallStatus status; - /** - * Create a new exception from the given status. - */ + /** Create a new exception from the given status. */ FlightRuntimeException(CallStatus status) { super(status.description(), status.cause()); this.status = status; diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightServer.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightServer.java index d873f7d2828..2e50c1e9052 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightServer.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightServer.java @@ -17,6 +17,16 @@ package org.apache.arrow.flight; +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import io.grpc.Server; +import io.grpc.ServerInterceptors; +import io.grpc.netty.GrpcSslContexts; +import io.grpc.netty.NettyServerBuilder; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.ServerChannel; +import io.netty.handler.ssl.ClientAuth; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslContextBuilder; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -34,9 +44,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; - import javax.net.ssl.SSLException; - import org.apache.arrow.flight.auth.ServerAuthHandler; import org.apache.arrow.flight.auth.ServerAuthInterceptor; import org.apache.arrow.flight.auth2.Auth2Constants; @@ -48,33 +56,21 @@ import org.apache.arrow.util.Preconditions; import org.apache.arrow.util.VisibleForTesting; -import com.google.common.util.concurrent.ThreadFactoryBuilder; - -import io.grpc.Server; -import io.grpc.ServerInterceptors; -import io.grpc.netty.GrpcSslContexts; -import io.grpc.netty.NettyServerBuilder; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.ServerChannel; -import io.netty.handler.ssl.ClientAuth; -import io.netty.handler.ssl.SslContext; -import io.netty.handler.ssl.SslContextBuilder; - - /** * Generic server of flight data that is customized via construction with delegate classes for the - * actual logic. The server currently uses GRPC as its transport mechanism. + * actual logic. The server currently uses GRPC as its transport mechanism. */ public class FlightServer implements AutoCloseable { - private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(FlightServer.class); + private static final org.slf4j.Logger logger = + org.slf4j.LoggerFactory.getLogger(FlightServer.class); private final Location location; private final Server server; - // The executor used by the gRPC server. We don't use it here, but we do need to clean it up with the server. + // The executor used by the gRPC server. We don't use it here, but we do need to clean it up with + // the server. // May be null, if a user-supplied executor was provided (as we do not want to clean that up) - @VisibleForTesting - final ExecutorService grpcExecutor; + @VisibleForTesting final ExecutorService grpcExecutor; /** The maximum size of an individual gRPC message. This effectively disables the limit. */ static final int MAX_GRPC_MESSAGE_SIZE = Integer.MAX_VALUE; @@ -103,8 +99,15 @@ public Location getLocation() { // If the server was bound to port 0, replace the port in the location with the real port. final URI uri = location.getUri(); try { - return new Location(new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), getPort(), - uri.getPath(), uri.getQuery(), uri.getFragment())); + return new Location( + new URI( + uri.getScheme(), + uri.getUserInfo(), + uri.getHost(), + getPort(), + uri.getPath(), + uri.getQuery(), + uri.getFragment())); } catch (URISyntaxException e) { // We don't expect this to happen throw new RuntimeException(e); @@ -128,9 +131,11 @@ public void shutdown() { /** * Wait for the server to shut down with a timeout. + * * @return true if the server shut down successfully. */ - public boolean awaitTermination(final long timeout, final TimeUnit unit) throws InterruptedException { + public boolean awaitTermination(final long timeout, final TimeUnit unit) + throws InterruptedException { return server.awaitTermination(timeout, unit); } @@ -165,7 +170,8 @@ public static Builder builder() { } /** Create a builder for a Flight server. */ - public static Builder builder(BufferAllocator allocator, Location location, FlightProducer producer) { + public static Builder builder( + BufferAllocator allocator, Location location, FlightProducer producer) { return new Builder(allocator, location, producer); } @@ -204,7 +210,8 @@ public static final class Builder { public FlightServer build() { // Add the auth middleware if applicable. if (headerAuthenticator != CallHeaderAuthenticator.NO_OP) { - this.middleware(FlightServerMiddleware.Key.of(Auth2Constants.AUTHORIZATION_HEADER), + this.middleware( + FlightServerMiddleware.Key.of(Auth2Constants.AUTHORIZATION_HEADER), new ServerCallHeaderAuthMiddleware.Factory(headerAuthenticator)); } @@ -212,58 +219,69 @@ public FlightServer build() { final NettyServerBuilder builder; switch (location.getUri().getScheme()) { - case LocationSchemes.GRPC_DOMAIN_SOCKET: { - // The implementation is platform-specific, so we have to find the classes at runtime - builder = NettyServerBuilder.forAddress(location.toSocketAddress()); - try { + case LocationSchemes.GRPC_DOMAIN_SOCKET: + { + // The implementation is platform-specific, so we have to find the classes at runtime + builder = NettyServerBuilder.forAddress(location.toSocketAddress()); try { - // Linux - builder.channelType(Class - .forName("io.netty.channel.epoll.EpollServerDomainSocketChannel") - .asSubclass(ServerChannel.class)); - final EventLoopGroup elg = Class.forName("io.netty.channel.epoll.EpollEventLoopGroup") - .asSubclass(EventLoopGroup.class).getConstructor().newInstance(); - builder.bossEventLoopGroup(elg).workerEventLoopGroup(elg); - } catch (ClassNotFoundException e) { - // BSD - builder.channelType( - Class.forName("io.netty.channel.kqueue.KQueueServerDomainSocketChannel") - .asSubclass(ServerChannel.class)); - final EventLoopGroup elg = Class.forName("io.netty.channel.kqueue.KQueueEventLoopGroup") - .asSubclass(EventLoopGroup.class).getConstructor().newInstance(); - builder.bossEventLoopGroup(elg).workerEventLoopGroup(elg); + try { + // Linux + builder.channelType( + Class.forName("io.netty.channel.epoll.EpollServerDomainSocketChannel") + .asSubclass(ServerChannel.class)); + final EventLoopGroup elg = + Class.forName("io.netty.channel.epoll.EpollEventLoopGroup") + .asSubclass(EventLoopGroup.class) + .getConstructor() + .newInstance(); + builder.bossEventLoopGroup(elg).workerEventLoopGroup(elg); + } catch (ClassNotFoundException e) { + // BSD + builder.channelType( + Class.forName("io.netty.channel.kqueue.KQueueServerDomainSocketChannel") + .asSubclass(ServerChannel.class)); + final EventLoopGroup elg = + Class.forName("io.netty.channel.kqueue.KQueueEventLoopGroup") + .asSubclass(EventLoopGroup.class) + .getConstructor() + .newInstance(); + builder.bossEventLoopGroup(elg).workerEventLoopGroup(elg); + } + } catch (ClassNotFoundException + | InstantiationException + | IllegalAccessException + | NoSuchMethodException + | InvocationTargetException e) { + throw new UnsupportedOperationException( + "Could not find suitable Netty native transport implementation for domain socket address."); } - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | - InvocationTargetException e) { - throw new UnsupportedOperationException( - "Could not find suitable Netty native transport implementation for domain socket address."); + break; } - break; - } case LocationSchemes.GRPC: - case LocationSchemes.GRPC_INSECURE: { - builder = NettyServerBuilder.forAddress(location.toSocketAddress()); - break; - } - case LocationSchemes.GRPC_TLS: { - if (certChain == null) { - throw new IllegalArgumentException("Must provide a certificate and key to serve gRPC over TLS"); + case LocationSchemes.GRPC_INSECURE: + { + builder = NettyServerBuilder.forAddress(location.toSocketAddress()); + break; + } + case LocationSchemes.GRPC_TLS: + { + if (certChain == null) { + throw new IllegalArgumentException( + "Must provide a certificate and key to serve gRPC over TLS"); + } + builder = NettyServerBuilder.forAddress(location.toSocketAddress()); + break; } - builder = NettyServerBuilder.forAddress(location.toSocketAddress()); - break; - } default: - throw new IllegalArgumentException("Scheme is not supported: " + location.getUri().getScheme()); + throw new IllegalArgumentException( + "Scheme is not supported: " + location.getUri().getScheme()); } if (certChain != null) { - SslContextBuilder sslContextBuilder = GrpcSslContexts - .forServer(certChain, key); + SslContextBuilder sslContextBuilder = GrpcSslContexts.forServer(certChain, key); if (mTlsCACert != null) { - sslContextBuilder - .clientAuth(ClientAuth.REQUIRE) - .trustManager(mTlsCACert); + sslContextBuilder.clientAuth(ClientAuth.REQUIRE).trustManager(mTlsCACert); } try { sslContext = sslContextBuilder.build(); @@ -280,56 +298,70 @@ public FlightServer build() { // Share one executor between the gRPC service, DoPut, and Handshake final ExecutorService exec; - // We only want to have FlightServer close the gRPC executor if we created it here. We should not close + // We only want to have FlightServer close the gRPC executor if we created it here. We should + // not close // user-supplied executors. final ExecutorService grpcExecutor; if (executor != null) { exec = executor; grpcExecutor = null; } else { - exec = Executors.newCachedThreadPool( - // Name threads for better debuggability - new ThreadFactoryBuilder().setNameFormat("flight-server-default-executor-%d").build()); + exec = + Executors.newCachedThreadPool( + // Name threads for better debuggability + new ThreadFactoryBuilder() + .setNameFormat("flight-server-default-executor-%d") + .build()); grpcExecutor = exec; } - final FlightBindingService flightService = new FlightBindingService(allocator, producer, authHandler, exec); + final FlightBindingService flightService = + new FlightBindingService(allocator, producer, authHandler, exec); builder .executor(exec) .maxInboundMessageSize(maxInboundMessageSize) .addService( - ServerInterceptors.intercept( - flightService, - new ServerAuthInterceptor(authHandler))); + ServerInterceptors.intercept(flightService, new ServerAuthInterceptor(authHandler))); - // Allow hooking into the gRPC builder. This is not guaranteed to be available on all Arrow versions or + // Allow hooking into the gRPC builder. This is not guaranteed to be available on all Arrow + // versions or // Flight implementations. - builderOptions.computeIfPresent("grpc.builderConsumer", (key, builderConsumer) -> { - final Consumer consumer = (Consumer) builderConsumer; - consumer.accept(builder); - return null; - }); + builderOptions.computeIfPresent( + "grpc.builderConsumer", + (key, builderConsumer) -> { + final Consumer consumer = + (Consumer) builderConsumer; + consumer.accept(builder); + return null; + }); // Allow explicitly setting some Netty-specific options - builderOptions.computeIfPresent("netty.channelType", (key, channelType) -> { - builder.channelType((Class) channelType); - return null; - }); - builderOptions.computeIfPresent("netty.bossEventLoopGroup", (key, elg) -> { - builder.bossEventLoopGroup((EventLoopGroup) elg); - return null; - }); - builderOptions.computeIfPresent("netty.workerEventLoopGroup", (key, elg) -> { - builder.workerEventLoopGroup((EventLoopGroup) elg); - return null; - }); + builderOptions.computeIfPresent( + "netty.channelType", + (key, channelType) -> { + builder.channelType((Class) channelType); + return null; + }); + builderOptions.computeIfPresent( + "netty.bossEventLoopGroup", + (key, elg) -> { + builder.bossEventLoopGroup((EventLoopGroup) elg); + return null; + }); + builderOptions.computeIfPresent( + "netty.workerEventLoopGroup", + (key, elg) -> { + builder.workerEventLoopGroup((EventLoopGroup) elg); + return null; + }); builder.intercept(new ServerInterceptorAdapter(interceptors)); return new FlightServer(location, builder.build(), grpcExecutor); } /** - * Set the maximum size of a message. Defaults to "unlimited", depending on the underlying transport. + * Set the maximum size of a message. Defaults to "unlimited", depending on the underlying + * transport. */ public Builder maxInboundMessageSize(int maxMessageSize) { this.maxInboundMessageSize = maxMessageSize; @@ -337,8 +369,9 @@ public Builder maxInboundMessageSize(int maxMessageSize) { } /** - * A small utility function to ensure that InputStream attributes. - * are closed if they are not null + * A small utility function to ensure that InputStream attributes. are closed if they are not + * null + * * @param stream The InputStream to close (if it is not null). */ private void closeInputStreamIfNotNull(InputStream stream) { @@ -352,8 +385,8 @@ private void closeInputStreamIfNotNull(InputStream stream) { } /** - * A small utility function to ensure that the certChain attribute - * is closed if it is not null. It then sets the attribute to null. + * A small utility function to ensure that the certChain attribute is closed if it is not null. + * It then sets the attribute to null. */ private void closeCertChain() { closeInputStreamIfNotNull(certChain); @@ -361,8 +394,8 @@ private void closeCertChain() { } /** - * A small utility function to ensure that the key attribute - * is closed if it is not null. It then sets the attribute to null. + * A small utility function to ensure that the key attribute is closed if it is not null. It + * then sets the attribute to null. */ private void closeKey() { closeInputStreamIfNotNull(key); @@ -370,8 +403,8 @@ private void closeKey() { } /** - * A small utility function to ensure that the mTlsCACert attribute - * is closed if it is not null. It then sets the attribute to null. + * A small utility function to ensure that the mTlsCACert attribute is closed if it is not null. + * It then sets the attribute to null. */ private void closeMTlsCACert() { closeInputStreamIfNotNull(mTlsCACert); @@ -380,6 +413,7 @@ private void closeMTlsCACert() { /** * Enable TLS on the server. + * * @param certChain The certificate chain to use. * @param key The private key to use. */ @@ -395,6 +429,7 @@ public Builder useTls(final File certChain, final File key) throws IOException { /** * Enable Client Verification via mTLS on the server. + * * @param mTlsCACert The CA certificate to use for verifying clients. */ public Builder useMTlsClientVerification(final File mTlsCACert) throws IOException { @@ -405,6 +440,7 @@ public Builder useMTlsClientVerification(final File mTlsCACert) throws IOExcepti /** * Enable TLS on the server. + * * @param certChain The certificate chain to use. * @param key The private key to use. */ @@ -420,6 +456,7 @@ public Builder useTls(final InputStream certChain, final InputStream key) throws /** * Enable mTLS on the server. + * * @param mTlsCACert The CA certificate to use for verifying clients. */ public Builder useMTlsClientVerification(final InputStream mTlsCACert) throws IOException { @@ -431,33 +468,27 @@ public Builder useMTlsClientVerification(final InputStream mTlsCACert) throws IO /** * Set the executor used by the server. * - *

Flight will NOT take ownership of the executor. The application must clean it up if one is provided. (If not - * provided, Flight will use a default executor which it will clean up.) + *

Flight will NOT take ownership of the executor. The application must clean it up if one is + * provided. (If not provided, Flight will use a default executor which it will clean up.) */ public Builder executor(ExecutorService executor) { this.executor = executor; return this; } - /** - * Set the authentication handler. - */ + /** Set the authentication handler. */ public Builder authHandler(ServerAuthHandler authHandler) { this.authHandler = authHandler; return this; } - /** - * Set the header-based authentication mechanism. - */ + /** Set the header-based authentication mechanism. */ public Builder headerAuthenticator(CallHeaderAuthenticator headerAuthenticator) { this.headerAuthenticator = headerAuthenticator; return this; } - /** - * Provide a transport-specific option. Not guaranteed to have any effect. - */ + /** Provide a transport-specific option. Not guaranteed to have any effect. */ public Builder transportHint(final String key, Object option) { builderOptions.put(key, option); return this; @@ -466,14 +497,15 @@ public Builder transportHint(final String key, Object option) { /** * Add a Flight middleware component to inspect and modify requests to this service. * - * @param key An identifier for this middleware component. Service implementations can retrieve the middleware - * instance for the current call using {@link org.apache.arrow.flight.FlightProducer.CallContext}. + * @param key An identifier for this middleware component. Service implementations can retrieve + * the middleware instance for the current call using {@link + * org.apache.arrow.flight.FlightProducer.CallContext}. * @param factory A factory for the middleware. * @param The middleware type. * @throws IllegalArgumentException if the key already exists */ - public Builder middleware(final FlightServerMiddleware.Key key, - final FlightServerMiddleware.Factory factory) { + public Builder middleware( + final FlightServerMiddleware.Key key, final FlightServerMiddleware.Factory factory) { if (interceptorKeys.contains(key.key)) { throw new IllegalArgumentException("Key already exists: " + key.key); } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightServerMiddleware.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightServerMiddleware.java index 9bc8bbfe739..789784a70b2 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightServerMiddleware.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightServerMiddleware.java @@ -24,18 +24,20 @@ * *

Middleware are instantiated per-call. * - *

Methods are not guaranteed to be called on any particular thread, relative to the thread that Flight requests are - * executed on. Do not depend on thread-local storage; instead, use state on the middleware instance. Service - * implementations may communicate with middleware implementations through - * {@link org.apache.arrow.flight.FlightProducer.CallContext#getMiddleware(Key)}. Methods on the middleware instance - * are non-reentrant, that is, a particular RPC will not make multiple concurrent calls to methods on a single - * middleware instance. However, methods on the factory instance are expected to be thread-safe, and if the factory - * instance returns the same middleware object more than once, then that middleware object must be thread-safe. + *

Methods are not guaranteed to be called on any particular thread, relative to the thread that + * Flight requests are executed on. Do not depend on thread-local storage; instead, use state on the + * middleware instance. Service implementations may communicate with middleware implementations + * through {@link org.apache.arrow.flight.FlightProducer.CallContext#getMiddleware(Key)}. Methods on + * the middleware instance are non-reentrant, that is, a particular RPC will not make multiple + * concurrent calls to methods on a single middleware instance. However, methods on the factory + * instance are expected to be thread-safe, and if the factory instance returns the same middleware + * object more than once, then that middleware object must be thread-safe. */ public interface FlightServerMiddleware { /** * A factory for Flight server middleware. + * * @param The middleware type. */ interface Factory { @@ -45,18 +47,20 @@ interface Factory { * @param info Details about the call. * @param incomingHeaders A mutable set of request headers. * @param context Context about the current request. - * - * @throws FlightRuntimeException if the middleware wants to reject the call with the given status + * @throws FlightRuntimeException if the middleware wants to reject the call with the given + * status */ T onCallStarted(CallInfo info, CallHeaders incomingHeaders, RequestContext context); } /** - * A key for Flight server middleware. On a server, middleware instances are identified by this key. + * A key for Flight server middleware. On a server, middleware instances are identified by this + * key. * *

Keys use reference equality, so instances should be shared. * - * @param The middleware class stored in this key. This provides a compile-time check when retrieving instances. + * @param The middleware class stored in this key. This provides a compile-time check when + * retrieving instances. */ class Key { final String key; @@ -65,9 +69,7 @@ class Key { this.key = Objects.requireNonNull(key, "Key must not be null."); } - /** - * Create a new key for the given type. - */ + /** Create a new key for the given type. */ public static Key of(String key) { return new Key<>(key); } @@ -76,13 +78,14 @@ public static Key of(String key) { /** * Callback for when the underlying transport is about to send response headers. * - * @param outgoingHeaders A mutable set of response headers. These can be manipulated to send different headers to the - * client. + * @param outgoingHeaders A mutable set of response headers. These can be manipulated to send + * different headers to the client. */ void onBeforeSendingHeaders(CallHeaders outgoingHeaders); /** * Callback for when the underlying transport has completed a call. + * * @param status Whether the call completed successfully or not. */ void onCallCompleted(CallStatus status); @@ -90,9 +93,10 @@ public static Key of(String key) { /** * Callback for when an RPC method implementation throws an uncaught exception. * - *

May be called multiple times, and may be called before or after {@link #onCallCompleted(CallStatus)}. - * Generally, an uncaught exception will end the call with a error {@link CallStatus}, and will be reported to {@link - * #onCallCompleted(CallStatus)}, but not necessarily this method. + *

May be called multiple times, and may be called before or after {@link + * #onCallCompleted(CallStatus)}. Generally, an uncaught exception will end the call with a error + * {@link CallStatus}, and will be reported to {@link #onCallCompleted(CallStatus)}, but not + * necessarily this method. * * @param err The exception that was thrown. */ diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightService.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightService.java index f55b47d2a94..05abe0e881e 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightService.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightService.java @@ -17,15 +17,16 @@ package org.apache.arrow.flight; +import com.google.common.base.Strings; +import io.grpc.stub.ServerCallStreamObserver; +import io.grpc.stub.StreamObserver; import java.util.Collections; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.function.BooleanSupplier; import java.util.function.Consumer; - import org.apache.arrow.flight.FlightProducer.ServerStreamListener; -import org.apache.arrow.flight.FlightServerMiddleware.Key; import org.apache.arrow.flight.auth.AuthConstants; import org.apache.arrow.flight.auth.ServerAuthHandler; import org.apache.arrow.flight.auth.ServerAuthWrapper; @@ -41,14 +42,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Strings; - -import io.grpc.stub.ServerCallStreamObserver; -import io.grpc.stub.StreamObserver; - -/** - * GRPC service implementation for a flight server. - */ +/** GRPC service implementation for a flight server. */ class FlightService extends FlightServiceImplBase { private static final Logger logger = LoggerFactory.getLogger(FlightService.class); @@ -59,7 +53,10 @@ class FlightService extends FlightServiceImplBase { private final ServerAuthHandler authHandler; private final ExecutorService executors; - FlightService(BufferAllocator allocator, FlightProducer producer, ServerAuthHandler authHandler, + FlightService( + BufferAllocator allocator, + FlightProducer producer, + ServerAuthHandler authHandler, ExecutorService executors) { this.allocator = allocator; this.producer = producer; @@ -84,16 +81,19 @@ private CallContext makeContext(ServerCallStreamObserver responseObserver) { } @Override - public StreamObserver handshake(StreamObserver responseObserver) { + public StreamObserver handshake( + StreamObserver responseObserver) { // This method is not meaningful with the auth2 interfaces. Authentication would already // have happened by header/middleware with the auth2 classes. return ServerAuthWrapper.wrapHandshake(authHandler, responseObserver, executors); } @Override - public void listFlights(Flight.Criteria criteria, StreamObserver responseObserver) { - final StreamPipe listener = StreamPipe - .wrap(responseObserver, FlightInfo::toProtocol, this::handleExceptionWithMiddleware); + public void listFlights( + Flight.Criteria criteria, StreamObserver responseObserver) { + final StreamPipe listener = + StreamPipe.wrap( + responseObserver, FlightInfo::toProtocol, this::handleExceptionWithMiddleware); try { final CallContext context = makeContext((ServerCallStreamObserver) responseObserver); producer.listFlights(context, new Criteria(criteria), listener); @@ -103,11 +103,13 @@ public void listFlights(Flight.Criteria criteria, StreamObserver responseObserverSimple) { + public void doGetCustom( + Flight.Ticket ticket, StreamObserver responseObserverSimple) { final ServerCallStreamObserver responseObserver = (ServerCallStreamObserver) responseObserverSimple; - final GetListener listener = new GetListener(responseObserver, this::handleExceptionWithMiddleware); + final GetListener listener = + new GetListener(responseObserver, this::handleExceptionWithMiddleware); try { producer.getStream(makeContext(responseObserver), new Ticket(ticket), listener); } catch (Exception ex) { @@ -118,8 +120,8 @@ public void doGetCustom(Flight.Ticket ticket, StreamObserver respo @Override public void doAction(Flight.Action request, StreamObserver responseObserver) { - final StreamPipe listener = StreamPipe - .wrap(responseObserver, Result::toProtocol, this::handleExceptionWithMiddleware); + final StreamPipe listener = + StreamPipe.wrap(responseObserver, Result::toProtocol, this::handleExceptionWithMiddleware); try { final CallContext context = makeContext((ServerCallStreamObserver) responseObserver); producer.doAction(context, new Action(request), listener); @@ -130,9 +132,11 @@ public void doAction(Flight.Action request, StreamObserver respon } @Override - public void listActions(Flight.Empty request, StreamObserver responseObserver) { - final StreamPipe listener = StreamPipe - .wrap(responseObserver, ActionType::toProtocol, this::handleExceptionWithMiddleware); + public void listActions( + Flight.Empty request, StreamObserver responseObserver) { + final StreamPipe listener = + StreamPipe.wrap( + responseObserver, ActionType::toProtocol, this::handleExceptionWithMiddleware); try { final CallContext context = makeContext((ServerCallStreamObserver) responseObserver); producer.listActions(context, listener); @@ -142,14 +146,16 @@ public void listActions(Flight.Empty request, StreamObserver // Do NOT call StreamPipe#onCompleted, as the FlightProducer implementation may be asynchronous } - private static class GetListener extends OutboundStreamListenerImpl implements ServerStreamListener { + private static class GetListener extends OutboundStreamListenerImpl + implements ServerStreamListener { private final ServerCallStreamObserver serverCallResponseObserver; private final Consumer errorHandler; private Runnable onCancelHandler = null; private Runnable onReadyHandler = null; private boolean completed; - public GetListener(ServerCallStreamObserver responseObserver, Consumer errorHandler) { + public GetListener( + ServerCallStreamObserver responseObserver, Consumer errorHandler) { super(null, responseObserver); this.errorHandler = errorHandler; this.completed = false; @@ -213,45 +219,54 @@ public void completed() { } } - public StreamObserver doPutCustom(final StreamObserver responseObserverSimple) { + public StreamObserver doPutCustom( + final StreamObserver responseObserverSimple) { ServerCallStreamObserver responseObserver = (ServerCallStreamObserver) responseObserverSimple; responseObserver.disableAutoInboundFlowControl(); responseObserver.request(1); - final StreamPipe ackStream = StreamPipe - .wrap(responseObserver, PutResult::toProtocol, this::handleExceptionWithMiddleware); - final FlightStream fs = new FlightStream( - allocator, - PENDING_REQUESTS, - /* server-upload streams are not cancellable */null, - responseObserver::request); + final StreamPipe ackStream = + StreamPipe.wrap( + responseObserver, PutResult::toProtocol, this::handleExceptionWithMiddleware); + final FlightStream fs = + new FlightStream( + allocator, + PENDING_REQUESTS, + /* server-upload streams are not cancellable */ null, + responseObserver::request); // When the ackStream is completed, the FlightStream will be closed with it ackStream.setAutoCloseable(fs); final StreamObserver observer = fs.asObserver(); - Future unused = executors.submit(() -> { - try { - producer.acceptPut(makeContext(responseObserver), fs, ackStream).run(); - } catch (Throwable ex) { - ackStream.onError(ex); - } finally { - // ARROW-6136: Close the stream if and only if acceptPut hasn't closed it itself - // We don't do this for other streams since the implementation may be asynchronous - ackStream.ensureCompleted(); - } - }); + Future unused = + executors.submit( + () -> { + try { + producer.acceptPut(makeContext(responseObserver), fs, ackStream).run(); + } catch (Throwable ex) { + ackStream.onError(ex); + } finally { + // ARROW-6136: Close the stream if and only if acceptPut hasn't closed it itself + // We don't do this for other streams since the implementation may be asynchronous + ackStream.ensureCompleted(); + } + }); return observer; } @Override - public void getFlightInfo(Flight.FlightDescriptor request, StreamObserver responseObserver) { + public void getFlightInfo( + Flight.FlightDescriptor request, StreamObserver responseObserver) { final FlightInfo info; try { - info = producer - .getFlightInfo(makeContext((ServerCallStreamObserver) responseObserver), new FlightDescriptor(request)); + info = + producer.getFlightInfo( + makeContext((ServerCallStreamObserver) responseObserver), + new FlightDescriptor(request)); } catch (Exception ex) { - // Don't capture exceptions from onNext or onCompleted with this block - because then we can't call onError + // Don't capture exceptions from onNext or onCompleted with this block - because then we can't + // call onError responseObserver.onError(StatusUtils.toGrpcException(ex)); return; } @@ -260,13 +275,17 @@ public void getFlightInfo(Flight.FlightDescriptor request, StreamObserver responseObserver) { + public void pollFlightInfo( + Flight.FlightDescriptor request, StreamObserver responseObserver) { final PollInfo info; try { - info = producer - .pollFlightInfo(makeContext((ServerCallStreamObserver) responseObserver), new FlightDescriptor(request)); + info = + producer.pollFlightInfo( + makeContext((ServerCallStreamObserver) responseObserver), + new FlightDescriptor(request)); } catch (Exception ex) { - // Don't capture exceptions from onNext or onCompleted with this block - because then we can't call onError + // Don't capture exceptions from onNext or onCompleted with this block - because then we can't + // call onError responseObserver.onError(StatusUtils.toGrpcException(ex)); return; } @@ -274,12 +293,10 @@ public void pollFlightInfo(Flight.FlightDescriptor request, StreamObserver, FlightServerMiddleware> middleware = ServerInterceptorAdapter - .SERVER_MIDDLEWARE_KEY.get(); + final Map, FlightServerMiddleware> middleware = + ServerInterceptorAdapter.SERVER_MIDDLEWARE_KEY.get(); if (middleware == null || middleware.isEmpty()) { logger.error("Uncaught exception in Flight method body", t); return; @@ -288,11 +305,13 @@ private void handleExceptionWithMiddleware(Throwable t) { } @Override - public void getSchema(Flight.FlightDescriptor request, StreamObserver responseObserver) { + public void getSchema( + Flight.FlightDescriptor request, StreamObserver responseObserver) { try { - SchemaResult result = producer - .getSchema(makeContext((ServerCallStreamObserver) responseObserver), - new FlightDescriptor(request)); + SchemaResult result = + producer.getSchema( + makeContext((ServerCallStreamObserver) responseObserver), + new FlightDescriptor(request)); responseObserver.onNext(result.toProtocol()); responseObserver.onCompleted(); } catch (Exception ex) { @@ -300,30 +319,33 @@ public void getSchema(Flight.FlightDescriptor request, StreamObserver responseObserver, Consumer errorHandler) { + public ExchangeListener( + ServerCallStreamObserver responseObserver, Consumer errorHandler) { super(responseObserver, errorHandler); this.resource = null; - super.setOnCancelHandler(() -> { - try { - if (onCancelHandler != null) { - onCancelHandler.run(); - } - } finally { - cleanup(); - } - }); + super.setOnCancelHandler( + () -> { + try { + if (onCancelHandler != null) { + onCancelHandler.run(); + } + } finally { + cleanup(); + } + }); } private void cleanup() { if (closed) { - // Prevent double-free. gRPC will call the OnCancelHandler even on a normal call end, which means that + // Prevent double-free. gRPC will call the OnCancelHandler even on a normal call end, which + // means that // we'll double-free without this guard. return; } @@ -362,41 +384,44 @@ public void setOnCancelHandler(Runnable handler) { } } - public StreamObserver doExchangeCustom(StreamObserver responseObserverSimple) { + public StreamObserver doExchangeCustom( + StreamObserver responseObserverSimple) { final ServerCallStreamObserver responseObserver = (ServerCallStreamObserver) responseObserverSimple; - final ExchangeListener listener = new ExchangeListener( - responseObserver, - this::handleExceptionWithMiddleware); - final FlightStream fs = new FlightStream( - allocator, - PENDING_REQUESTS, - /* server-upload streams are not cancellable */null, - responseObserver::request); + final ExchangeListener listener = + new ExchangeListener(responseObserver, this::handleExceptionWithMiddleware); + final FlightStream fs = + new FlightStream( + allocator, + PENDING_REQUESTS, + /* server-upload streams are not cancellable */ null, + responseObserver::request); // When service completes the call, this cleans up the FlightStream listener.resource = fs; responseObserver.disableAutoInboundFlowControl(); responseObserver.request(1); final StreamObserver observer = fs.asObserver(); try { - Future unused = executors.submit(() -> { - try { - producer.doExchange(makeContext(responseObserver), fs, listener); - } catch (Exception ex) { - listener.error(ex); - } - // We do not clean up or close anything here, to allow long-running asynchronous implementations. - // It is the service's responsibility to call completed() or error(), which will then clean up the FlightStream. - }); + Future unused = + executors.submit( + () -> { + try { + producer.doExchange(makeContext(responseObserver), fs, listener); + } catch (Exception ex) { + listener.error(ex); + } + // We do not clean up or close anything here, to allow long-running asynchronous + // implementations. + // It is the service's responsibility to call completed() or error(), which will + // then clean up the FlightStream. + }); } catch (Exception ex) { listener.error(ex); } return observer; } - /** - * Call context for the service. - */ + /** Call context for the service. */ static class CallContext implements FlightProducer.CallContext { private final String peerIdentity; @@ -419,8 +444,8 @@ public boolean isCancelled() { @Override public T getMiddleware(FlightServerMiddleware.Key key) { - final Map, FlightServerMiddleware> middleware = ServerInterceptorAdapter - .SERVER_MIDDLEWARE_KEY.get(); + final Map, FlightServerMiddleware> middleware = + ServerInterceptorAdapter.SERVER_MIDDLEWARE_KEY.get(); if (middleware == null) { return null; } @@ -428,7 +453,8 @@ public T getMiddleware(FlightServerMiddleware if (m == null) { return null; } - @SuppressWarnings("unchecked") final T result = (T) m; + @SuppressWarnings("unchecked") + final T result = (T) m; return result; } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightStatusCode.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightStatusCode.java index 3d96877ba02..75005ccd82e 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightStatusCode.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightStatusCode.java @@ -17,65 +17,41 @@ package org.apache.arrow.flight; -/** - * A status code describing the result of a Flight call. - */ +/** A status code describing the result of a Flight call. */ public enum FlightStatusCode { - /** - * The call completed successfully. Generally clients will not see this, but middleware may. - */ + /** The call completed successfully. Generally clients will not see this, but middleware may. */ OK, /** - * An unknown error occurred. This may also be the result of an implementation error on the server-side; by default, - * unhandled server exceptions result in this code. + * An unknown error occurred. This may also be the result of an implementation error on the + * server-side; by default, unhandled server exceptions result in this code. */ UNKNOWN, - /** - * An internal/implementation error occurred. - */ + /** An internal/implementation error occurred. */ INTERNAL, - /** - * One or more of the given arguments was invalid. - */ + /** One or more of the given arguments was invalid. */ INVALID_ARGUMENT, - /** - * The operation timed out. - */ + /** The operation timed out. */ TIMED_OUT, - /** - * The operation describes a resource that does not exist. - */ + /** The operation describes a resource that does not exist. */ NOT_FOUND, - /** - * The operation creates a resource that already exists. - */ + /** The operation creates a resource that already exists. */ ALREADY_EXISTS, - /** - * The operation was cancelled. - */ + /** The operation was cancelled. */ CANCELLED, - /** - * The client was not authenticated. - */ + /** The client was not authenticated. */ UNAUTHENTICATED, - /** - * The client did not have permission to make the call. - */ + /** The client did not have permission to make the call. */ UNAUTHORIZED, - /** - * The requested operation is not implemented. - */ + /** The requested operation is not implemented. */ UNIMPLEMENTED, /** - * The server cannot currently handle the request. This should be used for retriable requests, i.e. the server - * should send this code only if it has not done any work. + * The server cannot currently handle the request. This should be used for retriable requests, + * i.e. the server should send this code only if it has not done any work. */ UNAVAILABLE, ; - /** - * Create a blank {@link CallStatus} with this code. - */ + /** Create a blank {@link CallStatus} with this code. */ public CallStatus toStatus() { return new CallStatus(this); } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightStream.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightStream.java index 7a5a941603a..90e6775be8a 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightStream.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightStream.java @@ -17,6 +17,8 @@ package org.apache.arrow.flight; +import com.google.common.util.concurrent.SettableFuture; +import io.grpc.stub.StreamObserver; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -28,7 +30,6 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicInteger; - import org.apache.arrow.flight.ArrowMessage.HeaderType; import org.apache.arrow.flight.grpc.StatusUtils; import org.apache.arrow.memory.ArrowBuf; @@ -48,27 +49,19 @@ import org.apache.arrow.vector.util.DictionaryUtility; import org.apache.arrow.vector.validate.MetadataV4UnionChecker; -import com.google.common.util.concurrent.SettableFuture; - -import io.grpc.stub.StreamObserver; - -/** - * An adaptor between protobuf streams and flight data streams. - */ +/** An adaptor between protobuf streams and flight data streams. */ public class FlightStream implements AutoCloseable { // Use AutoCloseable sentinel objects to simplify logic in #close - private final AutoCloseable DONE = new AutoCloseable() { - @Override - public void close() throws Exception { - - } - }; - private final AutoCloseable DONE_EX = new AutoCloseable() { - @Override - public void close() throws Exception { - - } - }; + private final AutoCloseable DONE = + new AutoCloseable() { + @Override + public void close() throws Exception {} + }; + private final AutoCloseable DONE_EX = + new AutoCloseable() { + @Override + public void close() throws Exception {} + }; private final BufferAllocator allocator; private final Cancellable cancellable; @@ -78,10 +71,12 @@ public void close() throws Exception { private final int pendingTarget; private final Requestor requestor; // The completion flags. - // This flag is only updated as the user iterates through the data, i.e. it tracks whether the user has read all the + // This flag is only updated as the user iterates through the data, i.e. it tracks whether the + // user has read all the // data and closed the stream final CompletableFuture completed; - // This flag is immediately updated when gRPC signals that the server has ended the call. This is used to make sure + // This flag is immediately updated when gRPC signals that the server has ended the call. This is + // used to make sure // we don't block forever trying to write to a server that has rejected a call. final CompletableFuture cancelled; @@ -91,18 +86,18 @@ public void close() throws Exception { private volatile VectorLoader loader; private volatile Throwable ex; private volatile ArrowBuf applicationMetadata = null; - @VisibleForTesting - volatile MetadataVersion metadataVersion = null; + @VisibleForTesting volatile MetadataVersion metadataVersion = null; /** * Constructs a new instance. * - * @param allocator The allocator to use for creating/reallocating buffers for Vectors. + * @param allocator The allocator to use for creating/reallocating buffers for Vectors. * @param pendingTarget Target number of messages to receive. * @param cancellable Used to cancel mid-stream requests. * @param requestor A callback to determine how many pending items there are. */ - public FlightStream(BufferAllocator allocator, int pendingTarget, Cancellable cancellable, Requestor requestor) { + public FlightStream( + BufferAllocator allocator, int pendingTarget, Cancellable cancellable, Requestor requestor) { Objects.requireNonNull(allocator); Objects.requireNonNull(requestor); this.allocator = allocator; @@ -114,9 +109,7 @@ public FlightStream(BufferAllocator allocator, int pendingTarget, Cancellable ca this.cancelled = new CompletableFuture<>(); } - /** - * Get the schema for this stream. Blocks until the schema is available. - */ + /** Get the schema for this stream. Blocks until the schema is available. */ public Schema getSchema() { return getRoot().getSchema(); } @@ -124,9 +117,9 @@ public Schema getSchema() { /** * Get the provider for dictionaries in this stream. * - *

Does NOT retain a reference to the underlying dictionaries. Dictionaries may be updated as the stream is read. - * This method is intended for stream processing, where the application code will not retain references to values - * after the stream is closed. + *

Does NOT retain a reference to the underlying dictionaries. Dictionaries may be updated as + * the stream is read. This method is intended for stream processing, where the application code + * will not retain references to values after the stream is closed. * * @throws IllegalStateException if {@link #takeDictionaryOwnership()} was called * @see #takeDictionaryOwnership() @@ -139,10 +132,11 @@ public DictionaryProvider getDictionaryProvider() { } /** - * Get an owned reference to the dictionaries in this stream. Should be called after finishing reading the stream, - * but before closing. + * Get an owned reference to the dictionaries in this stream. Should be called after finishing + * reading the stream, but before closing. * - *

If called, the client is responsible for closing the dictionaries in this provider. Can only be called once. + *

If called, the client is responsible for closing the dictionaries in this provider. Can only + * be called once. * * @return The dictionary provider for the stream. * @throws IllegalStateException if called more than once. @@ -158,8 +152,8 @@ public DictionaryProvider takeDictionaryOwnership() { } /** - * Get the descriptor for this stream. Only applicable on the server side of a DoPut operation. Will block until the - * client sends the descriptor. + * Get the descriptor for this stream. Only applicable on the server side of a DoPut operation. + * Will block until the client sends the descriptor. */ public FlightDescriptor getDescriptor() { // This blocks until the first message from the client is received. @@ -169,38 +163,46 @@ public FlightDescriptor getDescriptor() { Thread.currentThread().interrupt(); throw CallStatus.INTERNAL.withCause(e).withDescription("Interrupted").toRuntimeException(); } catch (ExecutionException e) { - throw CallStatus.INTERNAL.withCause(e).withDescription("Error getting descriptor").toRuntimeException(); + throw CallStatus.INTERNAL + .withCause(e) + .withDescription("Error getting descriptor") + .toRuntimeException(); } } /** * Closes the stream (freeing any existing resources). * - *

If the stream isn't complete and is cancellable, this method will cancel and drain the stream first. + *

If the stream isn't complete and is cancellable, this method will cancel and drain the + * stream first. */ @Override public void close() throws Exception { final List closeables = new ArrayList<>(); Throwable suppressor = null; if (cancellable != null) { - // Client-side stream. Cancel the call, to help ensure gRPC doesn't deliver a message after close() ends. - // On the server side, we can't rely on draining the stream , because this gRPC bug means the completion callback + // Client-side stream. Cancel the call, to help ensure gRPC doesn't deliver a message after + // close() ends. + // On the server side, we can't rely on draining the stream , because this gRPC bug means the + // completion callback // may never run https://github.com/grpc/grpc-java/issues/5882 try { synchronized (cancellable) { if (!cancelled.isDone()) { // Only cancel if the call is not done on the gRPC side - cancellable.cancel("Stream closed before end", /* no exception to report */null); + cancellable.cancel("Stream closed before end", /* no exception to report */ null); } } // Drain the stream without the lock (as next() implicitly needs the lock) - while (next()) { } + while (next()) {} } catch (FlightRuntimeException e) { suppressor = e; } } - // Perform these operations under a lock. This way the observer can't enqueue new messages while we're in the - // middle of cleanup. This should only be a concern for server-side streams since client-side streams are drained + // Perform these operations under a lock. This way the observer can't enqueue new messages while + // we're in the + // middle of cleanup. This should only be a concern for server-side streams since client-side + // streams are drained // by the lambda above. synchronized (completed) { try { @@ -210,7 +212,9 @@ public void close() throws Exception { closeables.add(applicationMetadata); closeables.addAll(queue); if (dictionaries != null) { - dictionaries.getDictionaryIds().forEach(id -> closeables.add(dictionaries.lookup(id).getVector())); + dictionaries + .getDictionaryIds() + .forEach(id -> closeables.add(dictionaries.lookup(id).getVector())); } if (suppressor != null) { AutoCloseables.close(suppressor, closeables); @@ -220,7 +224,8 @@ public void close() throws Exception { // Remove any metadata after closing to prevent negative refcnt applicationMetadata = null; } finally { - // The value of this CompletableFuture is meaningless, only whether it's completed (or has an exception) + // The value of this CompletableFuture is meaningless, only whether it's completed (or has + // an exception) // No-op if already complete completed.complete(null); } @@ -229,6 +234,7 @@ public void close() throws Exception { /** * Blocking request to load next item into list. + * * @return Whether or not more data was found. */ public boolean next() { @@ -243,7 +249,8 @@ public boolean next() { Object data = queue.take(); if (DONE == data) { queue.put(DONE); - // Other code ignores the value of this CompletableFuture, only whether it's completed (or has an exception) + // Other code ignores the value of this CompletableFuture, only whether it's completed (or + // has an exception) completed.complete(null); return false; } else if (DONE_EX == data) { @@ -276,7 +283,8 @@ public boolean next() { try (ArrowDictionaryBatch arb = msg.asDictionaryBatch()) { final long id = arb.getDictionaryId(); if (dictionaries == null) { - throw new IllegalStateException("Dictionary ownership was claimed by the application."); + throw new IllegalStateException( + "Dictionary ownership was claimed by the application."); } final Dictionary dictionary = dictionaries.lookup(id); if (dictionary == null) { @@ -284,14 +292,18 @@ public boolean next() { } final FieldVector vector = dictionary.getVector(); - final VectorSchemaRoot dictionaryRoot = new VectorSchemaRoot(Collections.singletonList(vector.getField()), - Collections.singletonList(vector), 0); + final VectorSchemaRoot dictionaryRoot = + new VectorSchemaRoot( + Collections.singletonList(vector.getField()), + Collections.singletonList(vector), + 0); final VectorLoader dictionaryLoader = new VectorLoader(dictionaryRoot); dictionaryLoader.load(arb.getDictionary()); } return next(); } else { - throw new UnsupportedOperationException("Message type is unsupported: " + msg.getMessageType()); + throw new UnsupportedOperationException( + "Message type is unsupported: " + msg.getMessageType()); } return true; } @@ -321,18 +333,22 @@ private void checkMetadataVersion(ArrowMessage msg) { if (msg.asSchemaMessage() == null) { return; } - MetadataVersion receivedVersion = MetadataVersion.fromFlatbufID(msg.asSchemaMessage().getMessage().version()); + MetadataVersion receivedVersion = + MetadataVersion.fromFlatbufID(msg.asSchemaMessage().getMessage().version()); if (this.metadataVersion != receivedVersion) { - throw new IllegalStateException("Metadata version mismatch: stream started as " + - this.metadataVersion + " but got message with version " + receivedVersion); + throw new IllegalStateException( + "Metadata version mismatch: stream started as " + + this.metadataVersion + + " but got message with version " + + receivedVersion); } } /** * Get the current vector data from the stream. * - *

The data in the root may change at any time. Clients should NOT modify the root, but instead unload the data - * into their own root. + *

The data in the root may change at any time. Clients should NOT modify the root, but instead + * unload the data into their own root. * * @throws FlightRuntimeException if there was an error reading the schema from the stream. */ @@ -349,7 +365,7 @@ public VectorSchemaRoot getRoot() { /** * Check if there is a root (i.e. whether the other end has started sending data). * - * Updated by calls to {@link #next()}. + *

Updated by calls to {@link #next()}. * * @return true if and only if the other end has started sending data. */ @@ -358,9 +374,10 @@ public boolean hasRoot() { } /** - * Get the most recent metadata sent from the server. This may be cleared by calls to {@link #next()} if the server - * sends a message without metadata. This does NOT take ownership of the buffer - call retain() to create a reference - * if you need the buffer after a call to {@link #next()}. + * Get the most recent metadata sent from the server. This may be cleared by calls to {@link + * #next()} if the server sends a message without metadata. This does NOT take ownership of the + * buffer - call retain() to create a reference if you need the buffer after a call to {@link + * #next()}. * * @return the application metadata. May be null. */ @@ -395,68 +412,75 @@ private void enqueue(AutoCloseable message) { @Override public void onNext(ArrowMessage msg) { - // Operations here have to be under a lock so that we don't add a message to the queue while in the middle of + // Operations here have to be under a lock so that we don't add a message to the queue while + // in the middle of // close(). requestOutstanding(); switch (msg.getMessageType()) { - case NONE: { - // No IPC message - pure metadata or descriptor - if (msg.getDescriptor() != null) { - descriptor.set(new FlightDescriptor(msg.getDescriptor())); - } - if (msg.getApplicationMetadata() != null) { - enqueue(msg); - } - break; - } - case SCHEMA: { - Schema schema = msg.asSchema(); - - // if there is app metadata in the schema message, make sure - // that we don't leak it. - ArrowBuf meta = msg.getApplicationMetadata(); - if (meta != null) { - meta.close(); - } - - final List fields = new ArrayList<>(); - final Map dictionaryMap = new HashMap<>(); - for (final Field originalField : schema.getFields()) { - final Field updatedField = DictionaryUtility.toMemoryFormat(originalField, allocator, dictionaryMap); - fields.add(updatedField); - } - for (final Map.Entry entry : dictionaryMap.entrySet()) { - dictionaries.put(entry.getValue()); - } - schema = new Schema(fields, schema.getCustomMetadata()); - metadataVersion = MetadataVersion.fromFlatbufID(msg.asSchemaMessage().getMessage().version()); - try { - MetadataV4UnionChecker.checkRead(schema, metadataVersion); - } catch (IOException e) { - ex = e; - enqueue(DONE_EX); + case NONE: + { + // No IPC message - pure metadata or descriptor + if (msg.getDescriptor() != null) { + descriptor.set(new FlightDescriptor(msg.getDescriptor())); + } + if (msg.getApplicationMetadata() != null) { + enqueue(msg); + } break; } + case SCHEMA: + { + Schema schema = msg.asSchema(); + + // if there is app metadata in the schema message, make sure + // that we don't leak it. + ArrowBuf meta = msg.getApplicationMetadata(); + if (meta != null) { + meta.close(); + } - synchronized (completed) { - if (!completed.isDone()) { - fulfilledRoot = VectorSchemaRoot.create(schema, allocator); - loader = new VectorLoader(fulfilledRoot); - if (msg.getDescriptor() != null) { - descriptor.set(new FlightDescriptor(msg.getDescriptor())); + final List fields = new ArrayList<>(); + final Map dictionaryMap = new HashMap<>(); + for (final Field originalField : schema.getFields()) { + final Field updatedField = + DictionaryUtility.toMemoryFormat(originalField, allocator, dictionaryMap); + fields.add(updatedField); + } + for (final Map.Entry entry : dictionaryMap.entrySet()) { + dictionaries.put(entry.getValue()); + } + schema = new Schema(fields, schema.getCustomMetadata()); + metadataVersion = + MetadataVersion.fromFlatbufID(msg.asSchemaMessage().getMessage().version()); + try { + MetadataV4UnionChecker.checkRead(schema, metadataVersion); + } catch (IOException e) { + ex = e; + enqueue(DONE_EX); + break; + } + + synchronized (completed) { + if (!completed.isDone()) { + fulfilledRoot = VectorSchemaRoot.create(schema, allocator); + loader = new VectorLoader(fulfilledRoot); + if (msg.getDescriptor() != null) { + descriptor.set(new FlightDescriptor(msg.getDescriptor())); + } + root.set(fulfilledRoot); } - root.set(fulfilledRoot); } + break; } - break; - } case RECORD_BATCH: case DICTIONARY_BATCH: enqueue(msg); break; case TENSOR: default: - ex = new UnsupportedOperationException("Unable to handle message of type: " + msg.getMessageType()); + ex = + new UnsupportedOperationException( + "Unable to handle message of type: " + msg.getMessageType()); enqueue(DONE_EX); } } @@ -480,13 +504,14 @@ public void onCompleted() { /** * Cancels sending the stream to a client. * - *

Callers should drain the stream (with {@link #next()}) to ensure all messages sent before cancellation are - * received and to wait for the underlying transport to acknowledge cancellation. + *

Callers should drain the stream (with {@link #next()}) to ensure all messages sent before + * cancellation are received and to wait for the underlying transport to acknowledge cancellation. */ public void cancel(String message, Throwable exception) { if (cancellable == null) { - throw new UnsupportedOperationException("Streams cannot be cancelled that are produced by client. " + - "Instead, server should reject incoming messages."); + throw new UnsupportedOperationException( + "Streams cannot be cancelled that are produced by client. " + + "Instead, server should reject incoming messages."); } cancellable.cancel(message, exception); // Do not mark the stream as completed, as gRPC may still be delivering messages. @@ -496,22 +521,16 @@ StreamObserver asObserver() { return new Observer(); } - /** - * Provides a callback to cancel a process that is in progress. - */ + /** Provides a callback to cancel a process that is in progress. */ @FunctionalInterface public interface Cancellable { void cancel(String message, Throwable exception); } - /** - * Provides a interface to request more items from a stream producer. - */ + /** Provides a interface to request more items from a stream producer. */ @FunctionalInterface public interface Requestor { - /** - * Requests count more messages from the instance of this object. - */ + /** Requests count more messages from the instance of this object. */ void request(int count); } } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/HeaderCallOption.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/HeaderCallOption.java index 1a04ca3d085..d1a790fcb19 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/HeaderCallOption.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/HeaderCallOption.java @@ -21,17 +21,15 @@ import io.grpc.stub.AbstractStub; import io.grpc.stub.MetadataUtils; -/** - * Method option for supplying headers to method calls. - */ +/** Method option for supplying headers to method calls. */ public class HeaderCallOption implements CallOptions.GrpcCallOption { private final Metadata propertiesMetadata = new Metadata(); /** * Header property constructor. * - * @param headers the headers that should be sent across. If a header is a string, it should only be valid ASCII - * characters. Binary headers should end in "-bin". + * @param headers the headers that should be sent across. If a header is a string, it should only + * be valid ASCII characters. Binary headers should end in "-bin". */ public HeaderCallOption(CallHeaders headers) { for (String key : headers.keys()) { diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Location.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Location.java index fe192aa0c3f..7a68a265dc9 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Location.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Location.java @@ -23,7 +23,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.Objects; - import org.apache.arrow.flight.impl.Flight; /** A URI where a Flight stream is available. */ @@ -64,31 +63,36 @@ public SocketAddress toSocketAddress() { switch (uri.getScheme()) { case LocationSchemes.GRPC: case LocationSchemes.GRPC_TLS: - case LocationSchemes.GRPC_INSECURE: { - return new InetSocketAddress(uri.getHost(), uri.getPort()); - } - - case LocationSchemes.GRPC_DOMAIN_SOCKET: { - try { - // This dependency is not available on non-Unix platforms. - return Class.forName("io.netty.channel.unix.DomainSocketAddress").asSubclass(SocketAddress.class) - .getConstructor(String.class) - .newInstance(uri.getPath()); - } catch (InstantiationException | ClassNotFoundException | InvocationTargetException | - NoSuchMethodException | IllegalAccessException e) { - return null; + case LocationSchemes.GRPC_INSECURE: + { + return new InetSocketAddress(uri.getHost(), uri.getPort()); + } + + case LocationSchemes.GRPC_DOMAIN_SOCKET: + { + try { + // This dependency is not available on non-Unix platforms. + return Class.forName("io.netty.channel.unix.DomainSocketAddress") + .asSubclass(SocketAddress.class) + .getConstructor(String.class) + .newInstance(uri.getPath()); + } catch (InstantiationException + | ClassNotFoundException + | InvocationTargetException + | NoSuchMethodException + | IllegalAccessException e) { + return null; + } } - } - default: { - return null; - } + default: + { + return null; + } } } - /** - * Convert this Location into its protocol-level representation. - */ + /** Convert this Location into its protocol-level representation. */ Flight.Location toProtocol() { return Flight.Location.newBuilder().setUri(uri.toString()).build(); } @@ -100,7 +104,8 @@ Flight.Location toProtocol() { */ public static Location forGrpcInsecure(String host, int port) { try { - return new Location(new URI(LocationSchemes.GRPC_INSECURE, null, host, port, null, null, null)); + return new Location( + new URI(LocationSchemes.GRPC_INSECURE, null, host, port, null, null, null)); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } @@ -134,9 +139,7 @@ public static Location forGrpcDomainSocket(String path) { @Override public String toString() { - return "Location{" + - "uri=" + uri + - '}'; + return "Location{" + "uri=" + uri + '}'; } @Override diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/LocationSchemes.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/LocationSchemes.java index 872e5b1c22d..57ed0b50abe 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/LocationSchemes.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/LocationSchemes.java @@ -17,9 +17,7 @@ package org.apache.arrow.flight; -/** - * Constants representing well-known URI schemes for Flight services. - */ +/** Constants representing well-known URI schemes for Flight services. */ public final class LocationSchemes { public static final String GRPC = "grpc"; public static final String GRPC_INSECURE = "grpc+tcp"; diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/NoOpFlightProducer.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/NoOpFlightProducer.java index d1432f514d8..d878559e0e6 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/NoOpFlightProducer.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/NoOpFlightProducer.java @@ -17,45 +17,42 @@ package org.apache.arrow.flight; -/** - * A {@link FlightProducer} that throws on all operations. - */ +/** A {@link FlightProducer} that throws on all operations. */ public class NoOpFlightProducer implements FlightProducer { @Override - public void getStream(CallContext context, Ticket ticket, - ServerStreamListener listener) { - listener.error(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void getStream(CallContext context, Ticket ticket, ServerStreamListener listener) { + listener.error( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public void listFlights(CallContext context, Criteria criteria, - StreamListener listener) { - listener.onError(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void listFlights( + CallContext context, Criteria criteria, StreamListener listener) { + listener.onError( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public FlightInfo getFlightInfo(CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfo(CallContext context, FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public Runnable acceptPut(CallContext context, - FlightStream flightStream, StreamListener ackStream) { + public Runnable acceptPut( + CallContext context, FlightStream flightStream, StreamListener ackStream) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public void doAction(CallContext context, Action action, - StreamListener listener) { - listener.onError(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void doAction(CallContext context, Action action, StreamListener listener) { + listener.onError( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public void listActions(CallContext context, - StreamListener listener) { - listener.onError(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void listActions(CallContext context, StreamListener listener) { + listener.onError( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } - } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/NoOpStreamListener.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/NoOpStreamListener.java index e06af1a1026..1c8ab9423eb 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/NoOpStreamListener.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/NoOpStreamListener.java @@ -21,6 +21,7 @@ /** * A {@link StreamListener} that does nothing for all callbacks. + * * @param The type of the callback object. */ public class NoOpStreamListener implements StreamListener { @@ -28,18 +29,15 @@ public class NoOpStreamListener implements StreamListener { /** Ignores the value received. */ @Override - public void onNext(T val) { - } + public void onNext(T val) {} /** Ignores the error received. */ @Override - public void onError(Throwable t) { - } + public void onError(Throwable t) {} /** Ignores the stream completion event. */ @Override - public void onCompleted() { - } + public void onCompleted() {} @SuppressWarnings("unchecked") public static StreamListener getInstance() { diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/OutboundStreamListener.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/OutboundStreamListener.java index e80fb41c672..2b6b1861d73 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/OutboundStreamListener.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/OutboundStreamListener.java @@ -22,28 +22,26 @@ import org.apache.arrow.vector.dictionary.DictionaryProvider; import org.apache.arrow.vector.ipc.message.IpcOption; -/** - * An interface for writing data to a peer, client or server. - */ +/** An interface for writing data to a peer, client or server. */ public interface OutboundStreamListener { /** * A hint indicating whether the client is ready to receive data without excessive buffering. * - *

Writers should poll this flag before sending data to respect backpressure from the client and - * avoid sending data faster than the client can handle. Ignoring this flag may mean that the server - * will start consuming excessive amounts of memory, as it may buffer messages in memory. + *

Writers should poll this flag before sending data to respect backpressure from the client + * and avoid sending data faster than the client can handle. Ignoring this flag may mean that the + * server will start consuming excessive amounts of memory, as it may buffer messages in memory. */ boolean isReady(); /** - * Set a callback for when the listener is ready for new calls to putNext(), i.e. {@link #isReady()} - * has become true. + * Set a callback for when the listener is ready for new calls to putNext(), i.e. {@link + * #isReady()} has become true. * - *

Note that this callback may only be called some time after {@link #isReady()} becomes true, and may never - * be called if all executor threads on the server are busy, or the RPC method body is implemented in a blocking - * fashion. Note that isReady() must still be checked after the callback is run as it may have been run - * spuriously. + *

Note that this callback may only be called some time after {@link #isReady()} becomes true, + * and may never be called if all executor threads on the server are busy, or the RPC method body + * is implemented in a blocking fashion. Note that isReady() must still be checked after the + * callback is run as it may have been run spuriously. */ default void setOnReadyHandler(Runnable handler) { throw new UnsupportedOperationException("Not yet implemented."); @@ -78,13 +76,17 @@ default void start(VectorSchemaRoot root, DictionaryProvider dictionaries) { * Send the current contents of the associated {@link VectorSchemaRoot}. * *

This will not necessarily block until the message is actually sent; it may buffer messages - * in memory. Use {@link #isReady()} to check if there is backpressure and avoid excessive buffering. + * in memory. Use {@link #isReady()} to check if there is backpressure and avoid excessive + * buffering. */ void putNext(); /** - * Send the current contents of the associated {@link VectorSchemaRoot} alongside application-defined metadata. - * @param metadata The metadata to send. Ownership of the buffer is transferred to the Flight implementation. + * Send the current contents of the associated {@link VectorSchemaRoot} alongside + * application-defined metadata. + * + * @param metadata The metadata to send. Ownership of the buffer is transferred to the Flight + * implementation. */ void putNext(ArrowBuf metadata); @@ -96,28 +98,29 @@ default void start(VectorSchemaRoot root, DictionaryProvider dictionaries) { void putMetadata(ArrowBuf metadata); /** - * Indicate an error to the client. Terminates the stream; do not call {@link #completed()} afterwards. + * Indicate an error to the client. Terminates the stream; do not call {@link #completed()} + * afterwards. */ void error(Throwable ex); - /** - * Indicate that transmission is finished. - */ + /** Indicate that transmission is finished. */ void completed(); /** * Toggle whether to use the zero-copy write optimization. * - *

By default or when disabled, Arrow may copy data into a buffer for the underlying implementation to - * send. When enabled, Arrow will instead try to directly enqueue the Arrow buffer for sending. Not all - * implementations support this optimization, so even if enabled, you may not see a difference. + *

By default or when disabled, Arrow may copy data into a buffer for the underlying + * implementation to send. When enabled, Arrow will instead try to directly enqueue the Arrow + * buffer for sending. Not all implementations support this optimization, so even if enabled, you + * may not see a difference. * - *

In this mode, buffers must not be reused after they are written with {@link #putNext()}. For example, - * you would have to call {@link VectorSchemaRoot#allocateNew()} after every call to {@link #putNext()}. - * Hence, this is not enabled by default. + *

In this mode, buffers must not be reused after they are written with {@link #putNext()}. For + * example, you would have to call {@link VectorSchemaRoot#allocateNew()} after every call to + * {@link #putNext()}. Hence, this is not enabled by default. * - *

The default value can be toggled globally by setting the JVM property arrow.flight.enable_zero_copy_write - * or the environment variable ARROW_FLIGHT_ENABLE_ZERO_COPY_WRITE. + *

The default value can be toggled globally by setting the JVM property + * arrow.flight.enable_zero_copy_write or the environment variable + * ARROW_FLIGHT_ENABLE_ZERO_COPY_WRITE. */ default void setUseZeroCopy(boolean enabled) {} } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/OutboundStreamListenerImpl.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/OutboundStreamListenerImpl.java index 8c1cfde3ae9..a1db422c75e 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/OutboundStreamListenerImpl.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/OutboundStreamListenerImpl.java @@ -17,6 +17,7 @@ package org.apache.arrow.flight; +import io.grpc.stub.CallStreamObserver; import org.apache.arrow.flight.grpc.StatusUtils; import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.util.Preconditions; @@ -25,11 +26,7 @@ import org.apache.arrow.vector.dictionary.DictionaryProvider; import org.apache.arrow.vector.ipc.message.IpcOption; -import io.grpc.stub.CallStreamObserver; - -/** - * A base class for writing Arrow data to a Flight stream. - */ +/** A base class for writing Arrow data to a Flight stream. */ abstract class OutboundStreamListenerImpl implements OutboundStreamListener { private final FlightDescriptor descriptor; // nullable protected final CallStreamObserver responseObserver; @@ -37,7 +34,8 @@ abstract class OutboundStreamListenerImpl implements OutboundStreamListener { protected IpcOption option; // null until stream started protected boolean tryZeroCopy = ArrowMessage.ENABLE_ZERO_COPY_WRITE; - OutboundStreamListenerImpl(FlightDescriptor descriptor, CallStreamObserver responseObserver) { + OutboundStreamListenerImpl( + FlightDescriptor descriptor, CallStreamObserver responseObserver) { Preconditions.checkNotNull(responseObserver, "responseObserver must be provided"); this.descriptor = descriptor; this.responseObserver = responseObserver; @@ -58,13 +56,15 @@ public void setOnReadyHandler(Runnable handler) { public void start(VectorSchemaRoot root, DictionaryProvider dictionaries, IpcOption option) { this.option = option; try { - DictionaryUtils.generateSchemaMessages(root.getSchema(), descriptor, dictionaries, option, - responseObserver::onNext); + DictionaryUtils.generateSchemaMessages( + root.getSchema(), descriptor, dictionaries, option, responseObserver::onNext); } catch (RuntimeException e) { - // Propagate runtime exceptions, like those raised when trying to write unions with V4 metadata + // Propagate runtime exceptions, like those raised when trying to write unions with V4 + // metadata throw e; } catch (Exception e) { - // Only happens if closing buffers somehow fails - indicates application is an unknown state so propagate + // Only happens if closing buffers somehow fails - indicates application is an unknown state + // so propagate // the exception throw new RuntimeException("Could not generate and send all schema messages", e); } @@ -87,19 +87,27 @@ public void putNext() { @Override public void putNext(ArrowBuf metadata) { if (unloader == null) { - throw CallStatus.INTERNAL.withDescription("Stream was not started, call start()").toRuntimeException(); + throw CallStatus.INTERNAL + .withDescription("Stream was not started, call start()") + .toRuntimeException(); } waitUntilStreamReady(); - // close is a no-op if the message has been written to gRPC, otherwise frees the associated buffers - // in some code paths (e.g. if the call is cancelled), gRPC does not write the message, so we need to clean up - // ourselves. Normally, writing the ArrowMessage will transfer ownership of the data to gRPC/Netty. - try (final ArrowMessage message = new ArrowMessage(unloader.getRecordBatch(), metadata, tryZeroCopy, option)) { + // close is a no-op if the message has been written to gRPC, otherwise frees the associated + // buffers + // in some code paths (e.g. if the call is cancelled), gRPC does not write the message, so we + // need to clean up + // ourselves. Normally, writing the ArrowMessage will transfer ownership of the data to + // gRPC/Netty. + try (final ArrowMessage message = + new ArrowMessage(unloader.getRecordBatch(), metadata, tryZeroCopy, option)) { responseObserver.onNext(message); } catch (Exception e) { // This exception comes from ArrowMessage#close, not responseObserver#onNext. - // Generally this should not happen - ArrowMessage's implementation only closes non-throwing things. - // The user can't reasonably do anything about this, but if something does throw, we shouldn't let + // Generally this should not happen - ArrowMessage's implementation only closes non-throwing + // things. + // The user can't reasonably do anything about this, but if something does throw, we shouldn't + // let // execution continue since other state (e.g. allocators) may be in an odd state. throw new RuntimeException("Could not free ArrowMessage", e); } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/PollInfo.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/PollInfo.java index 59150d8814c..76c423bdc98 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/PollInfo.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/PollInfo.java @@ -17,21 +17,17 @@ package org.apache.arrow.flight; +import com.google.protobuf.Timestamp; +import com.google.protobuf.util.Timestamps; import java.io.IOException; import java.net.URISyntaxException; import java.nio.ByteBuffer; import java.time.Instant; import java.util.Objects; import java.util.Optional; - import org.apache.arrow.flight.impl.Flight; -import com.google.protobuf.Timestamp; -import com.google.protobuf.util.Timestamps; - -/** - * A POJO representation of the execution of a long-running query. - */ +/** A POJO representation of the execution of a long-running query. */ public class PollInfo { private final FlightInfo flightInfo; private final FlightDescriptor flightDescriptor; @@ -42,11 +38,17 @@ public class PollInfo { * Create a new PollInfo. * * @param flightInfo The FlightInfo (must not be null). - * @param flightDescriptor The descriptor used to poll for more information; null if and only if query is finished. + * @param flightDescriptor The descriptor used to poll for more information; null if and only if + * query is finished. * @param progress Optional progress info in [0.0, 1.0]. - * @param expirationTime An expiration time, after which the server may no longer recognize the descriptor. + * @param expirationTime An expiration time, after which the server may no longer recognize the + * descriptor. */ - public PollInfo(FlightInfo flightInfo, FlightDescriptor flightDescriptor, Double progress, Instant expirationTime) { + public PollInfo( + FlightInfo flightInfo, + FlightDescriptor flightDescriptor, + Double progress, + Instant expirationTime) { this.flightInfo = Objects.requireNonNull(flightInfo); this.flightDescriptor = flightDescriptor; this.progress = progress; @@ -55,19 +57,22 @@ public PollInfo(FlightInfo flightInfo, FlightDescriptor flightDescriptor, Double PollInfo(Flight.PollInfo flt) throws URISyntaxException { this.flightInfo = new FlightInfo(flt.getInfo()); - this.flightDescriptor = flt.hasFlightDescriptor() ? new FlightDescriptor(flt.getFlightDescriptor()) : null; + this.flightDescriptor = + flt.hasFlightDescriptor() ? new FlightDescriptor(flt.getFlightDescriptor()) : null; this.progress = flt.hasProgress() ? flt.getProgress() : null; - this.expirationTime = flt.hasExpirationTime() ? - Instant.ofEpochSecond(flt.getExpirationTime().getSeconds(), Timestamps.toNanos(flt.getExpirationTime())) : - null; + this.expirationTime = + flt.hasExpirationTime() + ? Instant.ofEpochSecond( + flt.getExpirationTime().getSeconds(), Timestamps.toNanos(flt.getExpirationTime())) + : null; } /** * The FlightInfo describing the result set of the execution of a query. * - *

This is always present and always contains all endpoints for the query execution so far, - * not just new endpoints that completed execution since the last call to - * {@link FlightClient#pollInfo(FlightDescriptor, CallOption...)}. + *

This is always present and always contains all endpoints for the query execution so far, not + * just new endpoints that completed execution since the last call to {@link + * FlightClient#pollInfo(FlightDescriptor, CallOption...)}. */ public FlightInfo getFlightInfo() { return flightInfo; @@ -76,7 +81,7 @@ public FlightInfo getFlightInfo() { /** * The FlightDescriptor that should be used to get further updates on this query. * - *

It is present if and only if the query is still running. If present, it should be passed to + *

It is present if and only if the query is still running. If present, it should be passed to * {@link FlightClient#pollInfo(FlightDescriptor, CallOption...)} to get an update. */ public Optional getFlightDescriptor() { @@ -86,7 +91,8 @@ public Optional getFlightDescriptor() { /** * The progress of the query. * - *

If present, should be a value in [0.0, 1.0]. It is not necessarily monotonic or non-decreasing. + *

If present, should be a value in [0.0, 1.0]. It is not necessarily monotonic or + * non-decreasing. */ public Optional getProgress() { return Optional.ofNullable(progress); @@ -95,8 +101,8 @@ public Optional getProgress() { /** * The expiration time of the query execution. * - *

After this passes, the server may not recognize the descriptor anymore and the client will not - * be able to track the query anymore. + *

After this passes, the server may not recognize the descriptor anymore and the client will + * not be able to track the query anymore. */ public Optional getExpirationTime() { return Optional.ofNullable(expirationTime); @@ -139,10 +145,10 @@ public boolean equals(Object o) { return false; } PollInfo pollInfo = (PollInfo) o; - return Objects.equals(getFlightInfo(), pollInfo.getFlightInfo()) && - Objects.equals(getFlightDescriptor(), pollInfo.getFlightDescriptor()) && - Objects.equals(getProgress(), pollInfo.getProgress()) && - Objects.equals(getExpirationTime(), pollInfo.getExpirationTime()); + return Objects.equals(getFlightInfo(), pollInfo.getFlightInfo()) + && Objects.equals(getFlightDescriptor(), pollInfo.getFlightDescriptor()) + && Objects.equals(getProgress(), pollInfo.getProgress()) + && Objects.equals(getExpirationTime(), pollInfo.getExpirationTime()); } @Override @@ -152,11 +158,15 @@ public int hashCode() { @Override public String toString() { - return "PollInfo{" + - "flightInfo=" + flightInfo + - ", flightDescriptor=" + flightDescriptor + - ", progress=" + progress + - ", expirationTime=" + expirationTime + - '}'; + return "PollInfo{" + + "flightInfo=" + + flightInfo + + ", flightDescriptor=" + + flightDescriptor + + ", progress=" + + progress + + ", expirationTime=" + + expirationTime + + '}'; } } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/PutResult.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/PutResult.java index 862401312b4..d7ba0e422bc 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/PutResult.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/PutResult.java @@ -17,13 +17,12 @@ package org.apache.arrow.flight; +import com.google.protobuf.ByteString; import org.apache.arrow.flight.impl.Flight; import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.ReferenceManager; -import com.google.protobuf.ByteString; - /** * A message from the server during a DoPut operation. * @@ -57,8 +56,8 @@ public static PutResult empty() { /** * Get the metadata in this message. May be null. * - *

Ownership of the {@link ArrowBuf} is retained by this object. Call {@link ReferenceManager#retain()} to preserve - * a reference. + *

Ownership of the {@link ArrowBuf} is retained by this object. Call {@link + * ReferenceManager#retain()} to preserve a reference. */ public ArrowBuf getApplicationMetadata() { return applicationMetadata; @@ -68,22 +67,28 @@ Flight.PutResult toProtocol() { if (applicationMetadata == null) { return Flight.PutResult.getDefaultInstance(); } - return Flight.PutResult.newBuilder().setAppMetadata(ByteString.copyFrom(applicationMetadata.nioBuffer())).build(); + return Flight.PutResult.newBuilder() + .setAppMetadata(ByteString.copyFrom(applicationMetadata.nioBuffer())) + .build(); } /** * Construct a PutResult from a Protobuf message. * - * @param allocator The allocator to use for allocating application metadata memory. The result object owns the - * allocated buffer, if any. + * @param allocator The allocator to use for allocating application metadata memory. The result + * object owns the allocated buffer, if any. * @param message The gRPC/Protobuf message. */ static PutResult fromProtocol(BufferAllocator allocator, Flight.PutResult message) { final ArrowBuf buf = allocator.buffer(message.getAppMetadata().size()); - message.getAppMetadata().asReadOnlyByteBufferList().forEach(bb -> { - buf.setBytes(buf.writerIndex(), bb); - buf.writerIndex(buf.writerIndex() + bb.limit()); - }); + message + .getAppMetadata() + .asReadOnlyByteBufferList() + .forEach( + bb -> { + buf.setBytes(buf.writerIndex(), bb); + buf.writerIndex(buf.writerIndex() + bb.limit()); + }); return new PutResult(buf); } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/RenewFlightEndpointRequest.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/RenewFlightEndpointRequest.java index ea233ff5ab0..d5ee50d29cf 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/RenewFlightEndpointRequest.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/RenewFlightEndpointRequest.java @@ -21,7 +21,6 @@ import java.net.URISyntaxException; import java.nio.ByteBuffer; import java.util.Objects; - import org.apache.arrow.flight.impl.Flight; /** A request to extend the expiration time of a FlightEndpoint. */ @@ -49,7 +48,8 @@ Flight.RenewFlightEndpointRequest toProtocol() { /** * Get the serialized form of this protocol message. * - *

Intended to help interoperability by allowing non-Flight services to still return Flight types. + *

Intended to help interoperability by allowing non-Flight services to still return Flight + * types. */ public ByteBuffer serialize() { return ByteBuffer.wrap(toProtocol().toByteArray()); @@ -58,13 +58,15 @@ public ByteBuffer serialize() { /** * Parse the serialized form of this protocol message. * - *

Intended to help interoperability by allowing Flight clients to obtain stream info from non-Flight services. + *

Intended to help interoperability by allowing Flight clients to obtain stream info from + * non-Flight services. * * @param serialized The serialized form of the message, as returned by {@link #serialize()}. * @return The deserialized message. * @throws IOException if the serialized form is invalid. */ - public static RenewFlightEndpointRequest deserialize(ByteBuffer serialized) throws IOException, URISyntaxException { + public static RenewFlightEndpointRequest deserialize(ByteBuffer serialized) + throws IOException, URISyntaxException { return new RenewFlightEndpointRequest(Flight.RenewFlightEndpointRequest.parseFrom(serialized)); } } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/RequestContext.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/RequestContext.java index 5117d05c2d2..f126f9113cb 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/RequestContext.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/RequestContext.java @@ -19,12 +19,11 @@ import java.util.Set; -/** - * Tracks variables about the current request. - */ +/** Tracks variables about the current request. */ public interface RequestContext { /** * Register a variable and a value. + * * @param key the variable name. * @param value the value. */ @@ -32,6 +31,7 @@ public interface RequestContext { /** * Retrieve a registered variable. + * * @param key the variable name. * @return the value, or null if not found. */ @@ -39,12 +39,14 @@ public interface RequestContext { /** * Retrieves the keys that have been registered to this context. + * * @return the keys used in this context. */ Set keySet(); /** * Deletes a registered variable. + * * @return the value associated with the deleted variable, or null if the key doesn't exist. */ String remove(String key); diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Result.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Result.java index 5d6ce485d65..1cfc18b8b69 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Result.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Result.java @@ -17,9 +17,8 @@ package org.apache.arrow.flight; -import org.apache.arrow.flight.impl.Flight; - import com.google.protobuf.ByteString; +import org.apache.arrow.flight.impl.Flight; /** * Opaque result returned after executing an action. @@ -43,8 +42,6 @@ public byte[] getBody() { } Flight.Result toProtocol() { - return Flight.Result.newBuilder() - .setBody(ByteString.copyFrom(body)) - .build(); + return Flight.Result.newBuilder().setBody(ByteString.copyFrom(body)).build(); } } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/SchemaResult.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/SchemaResult.java index 8becb85b8d3..c4a1d59e91d 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/SchemaResult.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/SchemaResult.java @@ -17,12 +17,14 @@ package org.apache.arrow.flight; +import com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream; +import com.google.common.collect.ImmutableList; +import com.google.protobuf.ByteString; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.util.Objects; - import org.apache.arrow.flight.impl.Flight; import org.apache.arrow.vector.ipc.ReadChannel; import org.apache.arrow.vector.ipc.WriteChannel; @@ -31,10 +33,6 @@ import org.apache.arrow.vector.types.pojo.Schema; import org.apache.arrow.vector.validate.MetadataV4UnionChecker; -import com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream; -import com.google.common.collect.ImmutableList; -import com.google.protobuf.ByteString; - /** * Opaque result returned after executing a getSchema request. * @@ -49,9 +47,7 @@ public SchemaResult(Schema schema) { this(schema, IpcOption.DEFAULT); } - /** - * Create a schema result with specific IPC options for serialization. - */ + /** Create a schema result with specific IPC options for serialization. */ public SchemaResult(Schema schema, IpcOption option) { Objects.requireNonNull(schema); MetadataV4UnionChecker.checkForUnion(schema.getFields().iterator(), option.metadataVersion); @@ -63,9 +59,7 @@ public Schema getSchema() { return schema; } - /** - * Converts to the protocol buffer representation. - */ + /** Converts to the protocol buffer representation. */ Flight.SchemaResult toProtocol() { // Encode schema in a Message payload ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -75,20 +69,18 @@ Flight.SchemaResult toProtocol() { throw new RuntimeException(e); } return Flight.SchemaResult.newBuilder() - .setSchema(ByteString.copyFrom(baos.toByteArray())) - .build(); - + .setSchema(ByteString.copyFrom(baos.toByteArray())) + .build(); } - /** - * Converts from the protocol buffer representation. - */ + /** Converts from the protocol buffer representation. */ static SchemaResult fromProtocol(Flight.SchemaResult pbSchemaResult) { try { final ByteBuffer schemaBuf = pbSchemaResult.getSchema().asReadOnlyByteBuffer(); - Schema schema = pbSchemaResult.getSchema().size() > 0 ? - MessageSerializer.deserializeSchema( - new ReadChannel(Channels.newChannel(new ByteBufferBackedInputStream(schemaBuf)))) + Schema schema = + pbSchemaResult.getSchema().size() > 0 + ? MessageSerializer.deserializeSchema( + new ReadChannel(Channels.newChannel(new ByteBufferBackedInputStream(schemaBuf)))) : new Schema(ImmutableList.of()); return new SchemaResult(schema); } catch (IOException e) { diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ServerHeaderMiddleware.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ServerHeaderMiddleware.java index 527c3128c65..c070f9ec59c 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ServerHeaderMiddleware.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/ServerHeaderMiddleware.java @@ -17,23 +17,16 @@ package org.apache.arrow.flight; -/** - * Middleware that's used to extract and pass headers to the server during requests. - */ +/** Middleware that's used to extract and pass headers to the server during requests. */ public class ServerHeaderMiddleware implements FlightServerMiddleware { - /** - * Factory for accessing ServerHeaderMiddleware. - */ + /** Factory for accessing ServerHeaderMiddleware. */ public static class Factory implements FlightServerMiddleware.Factory { - /** - * Construct a factory for receiving call headers. - */ - public Factory() { - } + /** Construct a factory for receiving call headers. */ + public Factory() {} @Override - public ServerHeaderMiddleware onCallStarted(CallInfo callInfo, CallHeaders incomingHeaders, - RequestContext context) { + public ServerHeaderMiddleware onCallStarted( + CallInfo callInfo, CallHeaders incomingHeaders, RequestContext context) { return new ServerHeaderMiddleware(incomingHeaders); } } @@ -44,22 +37,17 @@ private ServerHeaderMiddleware(CallHeaders incomingHeaders) { this.headers = incomingHeaders; } - /** - * Retrieve the headers for this call. - */ + /** Retrieve the headers for this call. */ public CallHeaders headers() { return headers; } @Override - public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) { - } + public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) {} @Override - public void onCallCompleted(CallStatus status) { - } + public void onCallCompleted(CallStatus status) {} @Override - public void onCallErrored(Throwable err) { - } + public void onCallErrored(Throwable err) {} } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/StreamPipe.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/StreamPipe.java index d506914d588..9559e899619 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/StreamPipe.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/StreamPipe.java @@ -17,18 +17,16 @@ package org.apache.arrow.flight; +import io.grpc.stub.StreamObserver; import java.util.function.Consumer; import java.util.function.Function; - import org.apache.arrow.flight.FlightProducer.StreamListener; import org.apache.arrow.flight.grpc.StatusUtils; import org.apache.arrow.util.AutoCloseables; -import io.grpc.stub.StreamObserver; - /** * Shim listener to avoid exposing GRPC internals. - + * * @param From Type * @param To Type */ @@ -45,17 +43,19 @@ class StreamPipe implements StreamListener { * * @param delegate The {@link StreamObserver} to wrap. * @param func The transformation function. - * @param errorHandler A handler for uncaught exceptions (e.g. if something tries to double-close this stream). + * @param errorHandler A handler for uncaught exceptions (e.g. if something tries to double-close + * this stream). * @param The source type. * @param The output type. * @return A wrapped listener. */ - public static StreamPipe wrap(StreamObserver delegate, Function func, - Consumer errorHandler) { + public static StreamPipe wrap( + StreamObserver delegate, Function func, Consumer errorHandler) { return new StreamPipe<>(delegate, func, errorHandler); } - public StreamPipe(StreamObserver delegate, Function func, Consumer errorHandler) { + public StreamPipe( + StreamObserver delegate, Function func, Consumer errorHandler) { super(); this.delegate = delegate; this.mapFunction = func; @@ -107,9 +107,7 @@ public void onCompleted() { } } - /** - * Ensure this stream has been completed. - */ + /** Ensure this stream has been completed. */ void ensureCompleted() { if (!closed) { onCompleted(); diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/SyncPutListener.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/SyncPutListener.java index 730cf4924b3..7121d7f6111 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/SyncPutListener.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/SyncPutListener.java @@ -21,13 +21,12 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; - import org.apache.arrow.flight.grpc.StatusUtils; import org.apache.arrow.memory.ArrowBuf; /** - * A listener for server-sent application metadata messages during a Flight DoPut. This class wraps the messages in a - * synchronous interface. + * A listener for server-sent application metadata messages during a Flight DoPut. This class wraps + * the messages in a synchronous interface. */ public final class SyncPutListener implements FlightClient.PutListener, AutoCloseable { @@ -55,8 +54,8 @@ private PutResult unwrap(Object queueItem) throws InterruptedException, Executio /** * Get the next message from the server, blocking until it is available. * - * @return The next message, or null if the server is done sending messages. The caller assumes ownership of the - * metadata and must remember to close it. + * @return The next message, or null if the server is done sending messages. The caller assumes + * ownership of the metadata and must remember to close it. * @throws InterruptedException if interrupted while waiting. * @throws ExecutionException if the server sent an error, or if there was an internal error. */ @@ -65,14 +64,17 @@ public PutResult read() throws InterruptedException, ExecutionException { } /** - * Get the next message from the server, blocking for the specified amount of time until it is available. + * Get the next message from the server, blocking for the specified amount of time until it is + * available. * - * @return The next message, or null if the server is done sending messages or no message arrived before the timeout. - * The caller assumes ownership of the metadata and must remember to close it. + * @return The next message, or null if the server is done sending messages or no message arrived + * before the timeout. The caller assumes ownership of the metadata and must remember to close + * it. * @throws InterruptedException if interrupted while waiting. * @throws ExecutionException if the server sent an error, or if there was an internal error. */ - public PutResult poll(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException { + public PutResult poll(long timeout, TimeUnit unit) + throws InterruptedException, ExecutionException { return unwrap(queue.poll(timeout, unit)); } @@ -108,11 +110,12 @@ public void onCompleted() { @Override public void close() { - queue.forEach(o -> { - if (o instanceof PutResult) { - ((PutResult) o).close(); - } - }); + queue.forEach( + o -> { + if (o instanceof PutResult) { + ((PutResult) o).close(); + } + }); } @Override diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Ticket.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Ticket.java index eb2f4af70d7..79e00facbf4 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Ticket.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/Ticket.java @@ -17,17 +17,13 @@ package org.apache.arrow.flight; +import com.google.protobuf.ByteString; import java.io.IOException; import java.nio.ByteBuffer; import java.util.Arrays; - import org.apache.arrow.flight.impl.Flight; -import com.google.protobuf.ByteString; - -/** - * Endpoint for a particular stream. - */ +/** Endpoint for a particular stream. */ public class Ticket { private final byte[] bytes; @@ -45,15 +41,14 @@ public byte[] getBytes() { } Flight.Ticket toProtocol() { - return Flight.Ticket.newBuilder() - .setTicket(ByteString.copyFrom(bytes)) - .build(); + return Flight.Ticket.newBuilder().setTicket(ByteString.copyFrom(bytes)).build(); } /** * Get the serialized form of this protocol message. * - *

Intended to help interoperability by allowing non-Flight services to still return Flight types. + *

Intended to help interoperability by allowing non-Flight services to still return Flight + * types. */ public ByteBuffer serialize() { return ByteBuffer.wrap(toProtocol().toByteArray()); @@ -62,7 +57,8 @@ public ByteBuffer serialize() { /** * Parse the serialized form of this protocol message. * - *

Intended to help interoperability by allowing Flight clients to obtain stream info from non-Flight services. + *

Intended to help interoperability by allowing Flight clients to obtain stream info from + * non-Flight services. * * @param serialized The serialized form of the Ticket, as returned by {@link #serialize()}. * @return The deserialized Ticket. @@ -97,6 +93,4 @@ public boolean equals(Object obj) { } return true; } - - } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/AuthConstants.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/AuthConstants.java index e3ccdc626d7..1eafa4086f3 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/AuthConstants.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/AuthConstants.java @@ -17,35 +17,36 @@ package org.apache.arrow.flight.auth; -import org.apache.arrow.flight.FlightConstants; - import io.grpc.Context; import io.grpc.Metadata; import io.grpc.Metadata.BinaryMarshaller; import io.grpc.MethodDescriptor; +import org.apache.arrow.flight.FlightConstants; -/** - * Constants used in authorization of flight connections. - */ +/** Constants used in authorization of flight connections. */ public final class AuthConstants { - public static final String HANDSHAKE_DESCRIPTOR_NAME = MethodDescriptor - .generateFullMethodName(FlightConstants.SERVICE, "Handshake"); + public static final String HANDSHAKE_DESCRIPTOR_NAME = + MethodDescriptor.generateFullMethodName(FlightConstants.SERVICE, "Handshake"); public static final String TOKEN_NAME = "Auth-Token-bin"; - public static final Metadata.Key TOKEN_KEY = Metadata.Key.of(TOKEN_NAME, new BinaryMarshaller() { - - @Override - public byte[] toBytes(byte[] value) { - return value; - } - - @Override - public byte[] parseBytes(byte[] serialized) { - return serialized; - } - }); - - public static final Context.Key PEER_IDENTITY_KEY = Context.keyWithDefault("arrow-flight-peer-identity", ""); + public static final Metadata.Key TOKEN_KEY = + Metadata.Key.of( + TOKEN_NAME, + new BinaryMarshaller() { + + @Override + public byte[] toBytes(byte[] value) { + return value; + } + + @Override + public byte[] parseBytes(byte[] serialized) { + return serialized; + } + }); + + public static final Context.Key PEER_IDENTITY_KEY = + Context.keyWithDefault("arrow-flight-peer-identity", ""); private AuthConstants() {} } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/BasicClientAuthHandler.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/BasicClientAuthHandler.java index c6dca97fb6b..ccf4b7eed24 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/BasicClientAuthHandler.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/BasicClientAuthHandler.java @@ -18,12 +18,9 @@ package org.apache.arrow.flight.auth; import java.util.Iterator; - import org.apache.arrow.flight.impl.Flight.BasicAuth; -/** - * A client auth handler that supports username and password. - */ +/** A client auth handler that supports username and password. */ public class BasicClientAuthHandler implements ClientAuthHandler { private final String name; @@ -54,5 +51,4 @@ public void authenticate(ClientAuthSender outgoing, Iterator incoming) { public byte[] getCallToken() { return token; } - } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/BasicServerAuthHandler.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/BasicServerAuthHandler.java index 34e3efc0d11..37d671be3ce 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/BasicServerAuthHandler.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/BasicServerAuthHandler.java @@ -17,18 +17,14 @@ package org.apache.arrow.flight.auth; +import com.google.protobuf.InvalidProtocolBufferException; import java.util.Iterator; import java.util.Optional; - import org.apache.arrow.flight.impl.Flight.BasicAuth; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.protobuf.InvalidProtocolBufferException; - -/** - * A ServerAuthHandler for username/password authentication. - */ +/** A ServerAuthHandler for username/password authentication. */ public class BasicServerAuthHandler implements ServerAuthHandler { private static final Logger logger = LoggerFactory.getLogger(BasicServerAuthHandler.class); @@ -39,15 +35,12 @@ public BasicServerAuthHandler(BasicAuthValidator authValidator) { this.authValidator = authValidator; } - /** - * Interface that this handler delegates for determining if credentials are valid. - */ + /** Interface that this handler delegates for determining if credentials are valid. */ public interface BasicAuthValidator { byte[] getToken(String username, String password) throws Exception; Optional isValid(byte[] token); - } @Override diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthHandler.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthHandler.java index af7da86e009..e83914d0890 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthHandler.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthHandler.java @@ -18,45 +18,35 @@ package org.apache.arrow.flight.auth; import java.util.Iterator; - import org.apache.arrow.flight.FlightClient; /** * Implement authentication for Flight on the client side. * * @deprecated As of 14.0.0. This implements a stateful "login" flow that does not play well with - * distributed or stateless systems. It will not be removed, but should not be used. Instead - * see {@link FlightClient#authenticateBasicToken(String, String)}. + * distributed or stateless systems. It will not be removed, but should not be used. Instead see + * {@link FlightClient#authenticateBasicToken(String, String)}. */ @Deprecated public interface ClientAuthHandler { /** * Handle the initial handshake with the server. + * * @param outgoing A channel to send data to the server. * @param incoming An iterator of incoming data from the server. */ void authenticate(ClientAuthSender outgoing, Iterator incoming); - /** - * Get the per-call authentication token. - */ + /** Get the per-call authentication token. */ byte[] getCallToken(); - /** - * A communication channel to the server during initial connection. - */ + /** A communication channel to the server during initial connection. */ interface ClientAuthSender { - /** - * Send the server a message. - */ + /** Send the server a message. */ void send(byte[] payload); - /** - * Signal an error to the server and abort the authentication attempt. - */ + /** Signal an error to the server and abort the authentication attempt. */ void onError(Throwable cause); - } - } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthInterceptor.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthInterceptor.java index 3d28b7ba7e7..df3ea2200a2 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthInterceptor.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthInterceptor.java @@ -25,9 +25,7 @@ import io.grpc.Metadata; import io.grpc.MethodDescriptor; -/** - * GRPC client intercepter that handles authentication with the server. - */ +/** GRPC client intercepter that handles authentication with the server. */ public class ClientAuthInterceptor implements ClientInterceptor { private volatile ClientAuthHandler authHandler = null; @@ -35,16 +33,15 @@ public void setAuthHandler(ClientAuthHandler authHandler) { this.authHandler = authHandler; } - public ClientAuthInterceptor() { - } + public ClientAuthInterceptor() {} public boolean hasAuthHandler() { return authHandler != null; } @Override - public ClientCall interceptCall(MethodDescriptor methodDescriptor, - CallOptions callOptions, Channel next) { + public ClientCall interceptCall( + MethodDescriptor methodDescriptor, CallOptions callOptions, Channel next) { ClientCall call = next.newCall(methodDescriptor, callOptions); // once we have an auth header, add that to the calls. @@ -55,7 +52,8 @@ public ClientCall interceptCall(MethodDescriptor extends SimpleForwardingClientCall { + private final class HeaderAttachingClientCall + extends SimpleForwardingClientCall { private HeaderAttachingClientCall(ClientCall call) { super(call); @@ -69,5 +67,4 @@ public void start(Listener responseListener, Metadata headers) { super.start(responseListener, headers); } } - } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthWrapper.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthWrapper.java index e86dc163c5f..d0aa790ad32 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthWrapper.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ClientAuthWrapper.java @@ -17,29 +17,24 @@ package org.apache.arrow.flight.auth; +import com.google.protobuf.ByteString; +import io.grpc.StatusRuntimeException; +import io.grpc.stub.StreamObserver; import java.util.Iterator; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.LinkedBlockingQueue; - import org.apache.arrow.flight.auth.ClientAuthHandler.ClientAuthSender; import org.apache.arrow.flight.grpc.StatusUtils; import org.apache.arrow.flight.impl.Flight.HandshakeRequest; import org.apache.arrow.flight.impl.Flight.HandshakeResponse; import org.apache.arrow.flight.impl.FlightServiceGrpc.FlightServiceStub; -import com.google.protobuf.ByteString; - -import io.grpc.StatusRuntimeException; -import io.grpc.stub.StreamObserver; - -/** - * Utility class for performing authorization over using a GRPC stub. - */ +/** Utility class for performing authorization over using a GRPC stub. */ public class ClientAuthWrapper { /** - * Do client auth for a client. The stub will be authenticated after this method returns. + * Do client auth for a client. The stub will be authenticated after this method returns. * * @param authHandler The handler to use. * @param stub The service stub. @@ -87,43 +82,46 @@ public void onNext(HandshakeResponse value) { } } - private Iterator iter = new Iterator() { - - @Override - public byte[] next() { - while (!completed.isDone() || !messages.isEmpty()) { - byte[] bytes = messages.poll(); - if (bytes == null) { - // busy wait. - continue; - } else { - return bytes; - } - } + private Iterator iter = + new Iterator() { + + @Override + public byte[] next() { + while (!completed.isDone() || !messages.isEmpty()) { + byte[] bytes = messages.poll(); + if (bytes == null) { + // busy wait. + continue; + } else { + return bytes; + } + } - if (completed.isCompletedExceptionally()) { - // Preserve prior exception behavior - // TODO: with ARROW-5681, throw an appropriate Flight exception if gRPC raised an exception - try { - completed.get(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } catch (ExecutionException e) { - if (e.getCause() instanceof StatusRuntimeException) { - throw (StatusRuntimeException) e.getCause(); + if (completed.isCompletedExceptionally()) { + // Preserve prior exception behavior + // TODO: with ARROW-5681, throw an appropriate Flight exception if gRPC raised an + // exception + try { + completed.get(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } catch (ExecutionException e) { + if (e.getCause() instanceof StatusRuntimeException) { + throw (StatusRuntimeException) e.getCause(); + } + throw new RuntimeException(e); + } } - throw new RuntimeException(e); - } - } - throw new IllegalStateException("You attempted to retrieve messages after there were none."); - } + throw new IllegalStateException( + "You attempted to retrieve messages after there were none."); + } - @Override - public boolean hasNext() { - return !messages.isEmpty(); - } - }; + @Override + public boolean hasNext() { + return !messages.isEmpty(); + } + }; @Override public void onError(Throwable t) { @@ -137,9 +135,8 @@ private class AuthSender implements ClientAuthSender { @Override public void send(byte[] payload) { try { - responseObserver.onNext(HandshakeRequest.newBuilder() - .setPayload(ByteString.copyFrom(payload)) - .build()); + responseObserver.onNext( + HandshakeRequest.newBuilder().setPayload(ByteString.copyFrom(payload)).build()); } catch (StatusRuntimeException sre) { throw StatusUtils.fromGrpcRuntimeException(sre); } @@ -150,7 +147,6 @@ public void onError(Throwable cause) { this.errored = true; responseObserver.onError(StatusUtils.toGrpcException(cause)); } - } @Override @@ -158,5 +154,4 @@ public void onCompleted() { completed.complete(true); } } - } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthHandler.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthHandler.java index 378027c9287..3da433c25c0 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthHandler.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthHandler.java @@ -19,7 +19,6 @@ import java.util.Iterator; import java.util.Optional; - import org.apache.arrow.flight.FlightServer; import org.apache.arrow.flight.auth2.CallHeaderAuthenticator; @@ -28,8 +27,8 @@ * * @deprecated As of 14.0.0. This implements a stateful "login" flow that does not play well with * distributed or stateless systems. It will not be removed, but should not be used. Instead, - * see {@link FlightServer.Builder#headerAuthenticator(CallHeaderAuthenticator)} - * and {@link CallHeaderAuthenticator}. + * see {@link FlightServer.Builder#headerAuthenticator(CallHeaderAuthenticator)} and {@link + * CallHeaderAuthenticator}. */ @Deprecated public interface ServerAuthHandler { @@ -37,8 +36,8 @@ public interface ServerAuthHandler { /** * Validate the client token provided on each call. * - * @return An empty optional if the client is not authenticated; the peer identity otherwise (may be the empty - * string). + * @return An empty optional if the client is not authenticated; the peer identity otherwise (may + * be the empty string). */ Optional isValid(byte[] token); @@ -52,30 +51,27 @@ public interface ServerAuthHandler { boolean authenticate(ServerAuthSender outgoing, Iterator incoming); /** - * Interface for a server implementations to send back authentication messages - * back to the client. + * Interface for a server implementations to send back authentication messages back to the client. */ interface ServerAuthSender { void send(byte[] payload); void onError(Throwable cause); - } - /** - * An auth handler that does nothing. - */ - ServerAuthHandler NO_OP = new ServerAuthHandler() { + /** An auth handler that does nothing. */ + ServerAuthHandler NO_OP = + new ServerAuthHandler() { - @Override - public Optional isValid(byte[] token) { - return Optional.of(""); - } + @Override + public Optional isValid(byte[] token) { + return Optional.of(""); + } - @Override - public boolean authenticate(ServerAuthSender outgoing, Iterator incoming) { - return true; - } - }; + @Override + public boolean authenticate(ServerAuthSender outgoing, Iterator incoming) { + return true; + } + }; } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthInterceptor.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthInterceptor.java index 5bff3784e17..c64d84502b9 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthInterceptor.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthInterceptor.java @@ -17,11 +17,6 @@ package org.apache.arrow.flight.auth; -import java.util.Optional; - -import org.apache.arrow.flight.FlightRuntimeException; -import org.apache.arrow.flight.grpc.StatusUtils; - import io.grpc.Context; import io.grpc.Contexts; import io.grpc.Metadata; @@ -31,10 +26,11 @@ import io.grpc.ServerInterceptor; import io.grpc.Status; import io.grpc.StatusRuntimeException; +import java.util.Optional; +import org.apache.arrow.flight.FlightRuntimeException; +import org.apache.arrow.flight.grpc.StatusUtils; -/** - * GRPC Interceptor for performing authentication. - */ +/** GRPC Interceptor for performing authentication. */ public class ServerAuthInterceptor implements ServerInterceptor { private final ServerAuthHandler authHandler; @@ -44,9 +40,11 @@ public ServerAuthInterceptor(ServerAuthHandler authHandler) { } @Override - public Listener interceptCall(ServerCall call, Metadata headers, - ServerCallHandler next) { - if (!call.getMethodDescriptor().getFullMethodName().equals(AuthConstants.HANDSHAKE_DESCRIPTOR_NAME)) { + public Listener interceptCall( + ServerCall call, Metadata headers, ServerCallHandler next) { + if (!call.getMethodDescriptor() + .getFullMethodName() + .equals(AuthConstants.HANDSHAKE_DESCRIPTOR_NAME)) { final Optional peerIdentity; // Allow customizing the response code by throwing FlightRuntimeException @@ -64,12 +62,17 @@ public Listener interceptCall(ServerCall call, if (!peerIdentity.isPresent()) { // Send back a description along with the status code - call.close(Status.UNAUTHENTICATED - .withDescription("Unauthenticated (invalid or missing auth token)"), new Metadata()); + call.close( + Status.UNAUTHENTICATED.withDescription( + "Unauthenticated (invalid or missing auth token)"), + new Metadata()); return new NoopServerCallListener<>(); } - return Contexts.interceptCall(Context.current().withValue(AuthConstants.PEER_IDENTITY_KEY, peerIdentity.get()), - call, headers, next); + return Contexts.interceptCall( + Context.current().withValue(AuthConstants.PEER_IDENTITY_KEY, peerIdentity.get()), + call, + headers, + next); } return next.startCall(call, headers); @@ -80,6 +83,5 @@ private Optional isValid(Metadata headers) { return authHandler.isValid(token); } - private static class NoopServerCallListener extends ServerCall.Listener { - } + private static class NoopServerCallListener extends ServerCall.Listener {} } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthWrapper.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthWrapper.java index ad1a36a935f..2e26b8b6cbf 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthWrapper.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth/ServerAuthWrapper.java @@ -17,11 +17,12 @@ package org.apache.arrow.flight.auth; +import com.google.protobuf.ByteString; +import io.grpc.stub.StreamObserver; import java.util.Iterator; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; - import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.auth.ServerAuthHandler.ServerAuthSender; import org.apache.arrow.flight.grpc.StatusUtils; @@ -30,13 +31,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.protobuf.ByteString; - -import io.grpc.stub.StreamObserver; - -/** - * Contains utility methods for integrating authorization into a GRPC stream. - */ +/** Contains utility methods for integrating authorization into a GRPC stream. */ public class ServerAuthWrapper { private static final Logger LOGGER = LoggerFactory.getLogger(ServerAuthWrapper.class); @@ -48,24 +43,28 @@ public class ServerAuthWrapper { * @param executors ExecutorService * @return AuthObserver */ - public static StreamObserver wrapHandshake(ServerAuthHandler authHandler, - StreamObserver responseObserver, ExecutorService executors) { + public static StreamObserver wrapHandshake( + ServerAuthHandler authHandler, + StreamObserver responseObserver, + ExecutorService executors) { // stream started. AuthObserver observer = new AuthObserver(responseObserver); - final Runnable r = () -> { - try { - if (authHandler.authenticate(observer.sender, observer.iter)) { - responseObserver.onCompleted(); - return; - } - - responseObserver.onError(StatusUtils.toGrpcException(CallStatus.UNAUTHENTICATED.toRuntimeException())); - } catch (Exception ex) { - LOGGER.error("Error during authentication", ex); - responseObserver.onError(StatusUtils.toGrpcException(ex)); - } - }; + final Runnable r = + () -> { + try { + if (authHandler.authenticate(observer.sender, observer.iter)) { + responseObserver.onCompleted(); + return; + } + + responseObserver.onError( + StatusUtils.toGrpcException(CallStatus.UNAUTHENTICATED.toRuntimeException())); + } catch (Exception ex) { + LOGGER.error("Error during authentication", ex); + responseObserver.onError(StatusUtils.toGrpcException(ex)); + } + }; observer.future = executors.submit(r); return observer; } @@ -91,31 +90,34 @@ public void onNext(HandshakeRequest value) { } } - private Iterator iter = new Iterator() { - - @Override - public byte[] next() { - while (!completed || !messages.isEmpty()) { - byte[] bytes = messages.poll(); - if (bytes == null) { - //busy wait. - continue; + private Iterator iter = + new Iterator() { + + @Override + public byte[] next() { + while (!completed || !messages.isEmpty()) { + byte[] bytes = messages.poll(); + if (bytes == null) { + // busy wait. + continue; + } + return bytes; + } + throw new IllegalStateException("Requesting more messages than client sent."); } - return bytes; - } - throw new IllegalStateException("Requesting more messages than client sent."); - } - @Override - public boolean hasNext() { - return !messages.isEmpty(); - } - }; + @Override + public boolean hasNext() { + return !messages.isEmpty(); + } + }; @Override public void onError(Throwable t) { completed = true; - while (future == null) {/* busy wait */} + while (future == null) { + /* busy wait */ + } future.cancel(true); } @@ -128,17 +130,14 @@ private class AuthSender implements ServerAuthSender { @Override public void send(byte[] payload) { - responseObserver.onNext(HandshakeResponse.newBuilder() - .setPayload(ByteString.copyFrom(payload)) - .build()); + responseObserver.onNext( + HandshakeResponse.newBuilder().setPayload(ByteString.copyFrom(payload)).build()); } @Override public void onError(Throwable cause) { responseObserver.onError(StatusUtils.toGrpcException(cause)); } - } } - } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/Auth2Constants.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/Auth2Constants.java index 624d7d5ff7c..a83e5343bca 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/Auth2Constants.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/Auth2Constants.java @@ -17,15 +17,12 @@ package org.apache.arrow.flight.auth2; -/** - * Constants used in authorization of flight connections. - */ +/** Constants used in authorization of flight connections. */ public final class Auth2Constants { public static final String PEER_IDENTITY_KEY = "arrow-flight-peer-identity"; public static final String BEARER_PREFIX = "Bearer "; public static final String BASIC_PREFIX = "Basic "; public static final String AUTHORIZATION_HEADER = "Authorization"; - private Auth2Constants() { - } + private Auth2Constants() {} } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/AuthUtilities.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/AuthUtilities.java index c73b7cf1a3a..d90abd3665c 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/AuthUtilities.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/AuthUtilities.java @@ -19,18 +19,14 @@ import org.apache.arrow.flight.CallHeaders; -/** - * Utility class for completing the auth process. - */ +/** Utility class for completing the auth process. */ public final class AuthUtilities { - private AuthUtilities() { - - } + private AuthUtilities() {} /** * Helper method for retrieving a value from the Authorization header. * - * @param headers The headers to inspect. + * @param headers The headers to inspect. * @param valuePrefix The prefix within the value portion of the header to extract away. * @return The header value. */ @@ -43,5 +39,4 @@ public static String getValueFromAuthHeader(CallHeaders headers, String valuePre } return null; } - } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BasicAuthCredentialWriter.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BasicAuthCredentialWriter.java index 698287e880b..9fca33dc952 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BasicAuthCredentialWriter.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BasicAuthCredentialWriter.java @@ -20,12 +20,9 @@ import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.function.Consumer; - import org.apache.arrow.flight.CallHeaders; -/** - * Client credentials that use a username and password. - */ +/** Client credentials that use a username and password. */ public final class BasicAuthCredentialWriter implements Consumer { private final String name; @@ -38,7 +35,11 @@ public BasicAuthCredentialWriter(String name, String password) { @Override public void accept(CallHeaders outputHeaders) { - outputHeaders.insert(Auth2Constants.AUTHORIZATION_HEADER, Auth2Constants.BASIC_PREFIX + - Base64.getEncoder().encodeToString(String.format("%s:%s", name, password).getBytes(StandardCharsets.UTF_8))); + outputHeaders.insert( + Auth2Constants.AUTHORIZATION_HEADER, + Auth2Constants.BASIC_PREFIX + + Base64.getEncoder() + .encodeToString( + String.format("%s:%s", name, password).getBytes(StandardCharsets.UTF_8))); } } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BasicCallHeaderAuthenticator.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BasicCallHeaderAuthenticator.java index fff7b46909a..07a6789faaf 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BasicCallHeaderAuthenticator.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BasicCallHeaderAuthenticator.java @@ -20,16 +20,13 @@ import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.Base64; - import org.apache.arrow.flight.CallHeaders; import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.FlightRuntimeException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * A ServerAuthHandler for username/password authentication. - */ +/** A ServerAuthHandler for username/password authentication. */ public class BasicCallHeaderAuthenticator implements CallHeaderAuthenticator { private static final Logger logger = LoggerFactory.getLogger(BasicCallHeaderAuthenticator.class); @@ -43,13 +40,14 @@ public BasicCallHeaderAuthenticator(CredentialValidator authValidator) { @Override public AuthResult authenticate(CallHeaders incomingHeaders) { try { - final String authEncoded = AuthUtilities.getValueFromAuthHeader( - incomingHeaders, Auth2Constants.BASIC_PREFIX); + final String authEncoded = + AuthUtilities.getValueFromAuthHeader(incomingHeaders, Auth2Constants.BASIC_PREFIX); if (authEncoded == null) { throw CallStatus.UNAUTHENTICATED.toRuntimeException(); } // The value has the format Base64(:) - final String authDecoded = new String(Base64.getDecoder().decode(authEncoded), StandardCharsets.UTF_8); + final String authDecoded = + new String(Base64.getDecoder().decode(authEncoded), StandardCharsets.UTF_8); final int colonPos = authDecoded.indexOf(':'); if (colonPos == -1) { throw CallStatus.UNAUTHENTICATED.toRuntimeException(); @@ -59,21 +57,21 @@ public AuthResult authenticate(CallHeaders incomingHeaders) { final String password = authDecoded.substring(colonPos + 1); return authValidator.validate(user, password); } catch (UnsupportedEncodingException ex) { - // Note: Intentionally discarding the exception cause when reporting back to the client for security purposes. + // Note: Intentionally discarding the exception cause when reporting back to the client for + // security purposes. logger.error("Authentication failed due to missing encoding.", ex); throw CallStatus.INTERNAL.toRuntimeException(); } catch (FlightRuntimeException ex) { throw ex; } catch (Exception ex) { - // Note: Intentionally discarding the exception cause when reporting back to the client for security purposes. + // Note: Intentionally discarding the exception cause when reporting back to the client for + // security purposes. logger.error("Authentication failed.", ex); throw CallStatus.UNAUTHENTICATED.toRuntimeException(); } } - /** - * Interface that this handler delegates to for validating the incoming headers. - */ + /** Interface that this handler delegates to for validating the incoming headers. */ public interface CredentialValidator { /** * Validate the supplied credentials (username/password) and return the peer identity. diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BearerCredentialWriter.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BearerCredentialWriter.java index 715ee502b68..90a01ad36f7 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BearerCredentialWriter.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BearerCredentialWriter.java @@ -18,12 +18,9 @@ package org.apache.arrow.flight.auth2; import java.util.function.Consumer; - import org.apache.arrow.flight.CallHeaders; -/** - * Client credentials that use a bearer token. - */ +/** Client credentials that use a bearer token. */ public final class BearerCredentialWriter implements Consumer { private final String bearer; @@ -34,6 +31,7 @@ public BearerCredentialWriter(String bearer) { @Override public void accept(CallHeaders outputHeaders) { - outputHeaders.insert(Auth2Constants.AUTHORIZATION_HEADER, Auth2Constants.BEARER_PREFIX + bearer); + outputHeaders.insert( + Auth2Constants.AUTHORIZATION_HEADER, Auth2Constants.BEARER_PREFIX + bearer); } } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BearerTokenAuthenticator.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BearerTokenAuthenticator.java index 5eb5863e792..e131b6c7faf 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BearerTokenAuthenticator.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/BearerTokenAuthenticator.java @@ -40,22 +40,25 @@ public AuthResult authenticate(CallHeaders incomingHeaders) { } // Delegate to the basic auth handler to do the validation. - final CallHeaderAuthenticator.AuthResult result = initialAuthenticator.authenticate(incomingHeaders); + final CallHeaderAuthenticator.AuthResult result = + initialAuthenticator.authenticate(incomingHeaders); return getAuthResultWithBearerToken(result); } /** * Callback to run when the initial authenticator succeeds. + * * @param authResult A successful initial authentication result. - * @return an alternate AuthResult based on the original AuthResult that will write a bearer token to output headers. + * @return an alternate AuthResult based on the original AuthResult that will write a bearer token + * to output headers. */ protected abstract AuthResult getAuthResultWithBearerToken(AuthResult authResult); /** * Validate the bearer token. + * * @param bearerToken The bearer token to validate. * @return A successful AuthResult if validation succeeded. */ protected abstract AuthResult validateBearer(String bearerToken); - } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/CallHeaderAuthenticator.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/CallHeaderAuthenticator.java index 87e60f1fa81..789f592f8fb 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/CallHeaderAuthenticator.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/CallHeaderAuthenticator.java @@ -23,33 +23,38 @@ /** * Interface for Server side authentication handlers. * - * A CallHeaderAuthenticator is used by {@link ServerCallHeaderAuthMiddleware} to validate headers sent by a Flight - * client for authentication purposes. The headers validated do not necessarily have to be Authorization headers. + *

A CallHeaderAuthenticator is used by {@link ServerCallHeaderAuthMiddleware} to validate + * headers sent by a Flight client for authentication purposes. The headers validated do not + * necessarily have to be Authorization headers. * - * The workflow is that the FlightServer will intercept headers on a request, validate the headers, and - * either send back an UNAUTHENTICATED error, or succeed and potentially send back additional headers to the client. + *

The workflow is that the FlightServer will intercept headers on a request, validate the + * headers, and either send back an UNAUTHENTICATED error, or succeed and potentially send back + * additional headers to the client. * - * Implementations of CallHeaderAuthenticator should take care not to provide leak confidential details (such as - * indicating if usernames are valid or not) for security reasons when reporting errors back to clients. + *

Implementations of CallHeaderAuthenticator should take care not to provide leak confidential + * details (such as indicating if usernames are valid or not) for security reasons when reporting + * errors back to clients. * - * Example CallHeaderAuthenticators provided include: - * The {@link BasicCallHeaderAuthenticator} will authenticate basic HTTP credentials. + *

Example CallHeaderAuthenticators provided include: The {@link BasicCallHeaderAuthenticator} + * will authenticate basic HTTP credentials. * - * The {@link BearerTokenAuthenticator} will authenticate basic HTTP credentials initially, then also send back a - * bearer token that the client can use for subsequent requests. The {@link GeneratedBearerTokenAuthenticator} will - * provide internally generated bearer tokens and maintain a cache of them. + *

The {@link BearerTokenAuthenticator} will authenticate basic HTTP credentials initially, then + * also send back a bearer token that the client can use for subsequent requests. The {@link + * GeneratedBearerTokenAuthenticator} will provide internally generated bearer tokens and maintain a + * cache of them. */ public interface CallHeaderAuthenticator { /** * Encapsulates the result of the {@link CallHeaderAuthenticator} analysis of headers. * - * This includes the identity of the incoming user and any outbound headers to send as a response to the client. + *

This includes the identity of the incoming user and any outbound headers to send as a + * response to the client. */ interface AuthResult { /** - * The peer identity that was determined by the handshake process based on the - * authentication credentials supplied by the client. + * The peer identity that was determined by the handshake process based on the authentication + * credentials supplied by the client. * * @return The peer identity. */ @@ -57,11 +62,10 @@ interface AuthResult { /** * Appends a header to the outgoing call headers. + * * @param outgoingHeaders The outgoing headers. */ - default void appendToOutgoingHeaders(CallHeaders outgoingHeaders) { - - } + default void appendToOutgoingHeaders(CallHeaders outgoingHeaders) {} } /** @@ -74,13 +78,12 @@ default void appendToOutgoingHeaders(CallHeaders outgoingHeaders) { */ AuthResult authenticate(CallHeaders incomingHeaders); - /** - * An auth handler that does nothing. - */ - CallHeaderAuthenticator NO_OP = new CallHeaderAuthenticator() { - @Override - public AuthResult authenticate(CallHeaders incomingHeaders) { - return () -> ""; - } - }; + /** An auth handler that does nothing. */ + CallHeaderAuthenticator NO_OP = + new CallHeaderAuthenticator() { + @Override + public AuthResult authenticate(CallHeaders incomingHeaders) { + return () -> ""; + } + }; } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientBearerHeaderHandler.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientBearerHeaderHandler.java index 45bdb6d959f..4ca80a0d716 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientBearerHeaderHandler.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientBearerHeaderHandler.java @@ -20,14 +20,14 @@ import org.apache.arrow.flight.CallHeaders; import org.apache.arrow.flight.grpc.CredentialCallOption; -/** - * A client header handler that parses the incoming headers for a bearer token. - */ +/** A client header handler that parses the incoming headers for a bearer token. */ public class ClientBearerHeaderHandler implements ClientHeaderHandler { @Override - public CredentialCallOption getCredentialCallOptionFromIncomingHeaders(CallHeaders incomingHeaders) { - final String bearerValue = AuthUtilities.getValueFromAuthHeader(incomingHeaders, Auth2Constants.BEARER_PREFIX); + public CredentialCallOption getCredentialCallOptionFromIncomingHeaders( + CallHeaders incomingHeaders) { + final String bearerValue = + AuthUtilities.getValueFromAuthHeader(incomingHeaders, Auth2Constants.BEARER_PREFIX); if (bearerValue != null) { return new CredentialCallOption(new BearerCredentialWriter(bearerValue)); } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientHandshakeWrapper.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientHandshakeWrapper.java index c84739d2e34..be570d24b68 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientHandshakeWrapper.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientHandshakeWrapper.java @@ -17,9 +17,10 @@ package org.apache.arrow.flight.auth2; +import io.grpc.StatusRuntimeException; +import io.grpc.stub.StreamObserver; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; - import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.FlightRuntimeException; import org.apache.arrow.flight.grpc.StatusUtils; @@ -27,17 +28,13 @@ import org.apache.arrow.flight.impl.Flight.HandshakeResponse; import org.apache.arrow.flight.impl.FlightServiceGrpc.FlightServiceStub; -import io.grpc.StatusRuntimeException; -import io.grpc.stub.StreamObserver; - -/** - * Utility class for executing a handshake with a FlightServer. - */ +/** Utility class for executing a handshake with a FlightServer. */ public class ClientHandshakeWrapper { - private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ClientHandshakeWrapper.class); + private static final org.slf4j.Logger logger = + org.slf4j.LoggerFactory.getLogger(ClientHandshakeWrapper.class); /** - * Do handshake for a client. The stub will be authenticated after this method returns. + * Do handshake for a client. The stub will be authenticated after this method returns. * * @param stub The service stub. */ @@ -83,8 +80,7 @@ public HandshakeObserver() { } @Override - public void onNext(HandshakeResponse value) { - } + public void onNext(HandshakeResponse value) {} @Override public void onError(Throwable t) { @@ -96,5 +92,4 @@ public void onCompleted() { completed.complete(true); } } - } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientHeaderHandler.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientHeaderHandler.java index 514189f9b13..175a71bffe3 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientHeaderHandler.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientHeaderHandler.java @@ -20,24 +20,23 @@ import org.apache.arrow.flight.CallHeaders; import org.apache.arrow.flight.grpc.CredentialCallOption; -/** - * Interface for client side header parsing and conversion to CredentialCallOption. - */ +/** Interface for client side header parsing and conversion to CredentialCallOption. */ public interface ClientHeaderHandler { /** * Parses the incoming headers and converts them into a CredentialCallOption. + * * @param incomingHeaders Incoming headers to parse. * @return An instance of CredentialCallOption. */ CredentialCallOption getCredentialCallOptionFromIncomingHeaders(CallHeaders incomingHeaders); - - /** - * An client header handler that does nothing. - */ - ClientHeaderHandler NO_OP = new ClientHeaderHandler() { - @Override - public CredentialCallOption getCredentialCallOptionFromIncomingHeaders(CallHeaders incomingHeaders) { - return null; - } - }; + + /** An client header handler that does nothing. */ + ClientHeaderHandler NO_OP = + new ClientHeaderHandler() { + @Override + public CredentialCallOption getCredentialCallOptionFromIncomingHeaders( + CallHeaders incomingHeaders) { + return null; + } + }; } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientIncomingAuthHeaderMiddleware.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientIncomingAuthHeaderMiddleware.java index 7bb55d145d1..165f44f8794 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientIncomingAuthHeaderMiddleware.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ClientIncomingAuthHeaderMiddleware.java @@ -23,22 +23,20 @@ import org.apache.arrow.flight.FlightClientMiddleware; import org.apache.arrow.flight.grpc.CredentialCallOption; -/** - * Middleware for capturing bearer tokens sent back from the Flight server. - */ +/** Middleware for capturing bearer tokens sent back from the Flight server. */ public class ClientIncomingAuthHeaderMiddleware implements FlightClientMiddleware { private final Factory factory; - /** - * Factory used within FlightClient. - */ + /** Factory used within FlightClient. */ public static class Factory implements FlightClientMiddleware.Factory { private final ClientHeaderHandler headerHandler; private CredentialCallOption credentialCallOption = null; /** * Construct a factory with the given header handler. - * @param headerHandler The header handler that will be used for handling incoming headers from the flight server. + * + * @param headerHandler The header handler that will be used for handling incoming headers from + * the flight server. */ public Factory(ClientHeaderHandler headerHandler) { this.headerHandler = headerHandler; @@ -63,16 +61,14 @@ private ClientIncomingAuthHeaderMiddleware(Factory factory) { } @Override - public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) { - } + public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) {} @Override public void onHeadersReceived(CallHeaders incomingHeaders) { factory.setCredentialCallOption( - factory.headerHandler.getCredentialCallOptionFromIncomingHeaders(incomingHeaders)); + factory.headerHandler.getCredentialCallOptionFromIncomingHeaders(incomingHeaders)); } @Override - public void onCallCompleted(CallStatus status) { - } + public void onCallCompleted(CallStatus status) {} } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/GeneratedBearerTokenAuthenticator.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/GeneratedBearerTokenAuthenticator.java index 8b312b6b77a..8d73d385ff2 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/GeneratedBearerTokenAuthenticator.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/GeneratedBearerTokenAuthenticator.java @@ -17,29 +17,25 @@ package org.apache.arrow.flight.auth2; +import com.google.common.base.Strings; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import io.grpc.Metadata; import java.nio.ByteBuffer; import java.util.Base64; import java.util.UUID; import java.util.concurrent.TimeUnit; - import org.apache.arrow.flight.CallHeaders; import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.grpc.MetadataAdapter; -import com.google.common.base.Strings; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; - -import io.grpc.Metadata; - -/** - * Generates and caches bearer tokens from user credentials. - */ +/** Generates and caches bearer tokens from user credentials. */ public class GeneratedBearerTokenAuthenticator extends BearerTokenAuthenticator { private final Cache bearerToIdentityCache; /** * Generate bearer tokens for the given basic call authenticator. + * * @param authenticator The authenticator to initial validate inputs with. */ public GeneratedBearerTokenAuthenticator(CallHeaderAuthenticator authenticator) { @@ -48,20 +44,25 @@ public GeneratedBearerTokenAuthenticator(CallHeaderAuthenticator authenticator) /** * Generate bearer tokens for the given basic call authenticator. + * * @param authenticator The authenticator to initial validate inputs with. * @param timeoutMinutes The time before tokens expire after being accessed. */ - public GeneratedBearerTokenAuthenticator(CallHeaderAuthenticator authenticator, int timeoutMinutes) { - this(authenticator, CacheBuilder.newBuilder().expireAfterAccess(timeoutMinutes, TimeUnit.MINUTES)); + public GeneratedBearerTokenAuthenticator( + CallHeaderAuthenticator authenticator, int timeoutMinutes) { + this( + authenticator, + CacheBuilder.newBuilder().expireAfterAccess(timeoutMinutes, TimeUnit.MINUTES)); } /** * Generate bearer tokens for the given basic call authenticator. + * * @param authenticator The authenticator to initial validate inputs with. * @param cacheBuilder The configuration of the cache of bearer tokens. */ - public GeneratedBearerTokenAuthenticator(CallHeaderAuthenticator authenticator, - CacheBuilder cacheBuilder) { + public GeneratedBearerTokenAuthenticator( + CallHeaderAuthenticator authenticator, CacheBuilder cacheBuilder) { super(authenticator); bearerToIdentityCache = cacheBuilder.build(); } @@ -81,8 +82,11 @@ public String getPeerIdentity() { @Override public void appendToOutgoingHeaders(CallHeaders outgoingHeaders) { - if (null == AuthUtilities.getValueFromAuthHeader(outgoingHeaders, Auth2Constants.BEARER_PREFIX)) { - outgoingHeaders.insert(Auth2Constants.AUTHORIZATION_HEADER, Auth2Constants.BEARER_PREFIX + bearerToken); + if (null + == AuthUtilities.getValueFromAuthHeader( + outgoingHeaders, Auth2Constants.BEARER_PREFIX)) { + outgoingHeaders.insert( + Auth2Constants.AUTHORIZATION_HEADER, Auth2Constants.BEARER_PREFIX + bearerToken); } } }; @@ -96,7 +100,7 @@ protected AuthResult getAuthResultWithBearerToken(AuthResult authResult) { final CallHeaders dummyHeaders = new MetadataAdapter(new Metadata()); authResult.appendToOutgoingHeaders(dummyHeaders); String bearerToken = - AuthUtilities.getValueFromAuthHeader(dummyHeaders, Auth2Constants.BEARER_PREFIX); + AuthUtilities.getValueFromAuthHeader(dummyHeaders, Auth2Constants.BEARER_PREFIX); final AuthResult authResultWithBearerToken; if (Strings.isNullOrEmpty(bearerToken)) { // Generate a new bearer token and return an AuthResult that can write it. @@ -106,18 +110,20 @@ protected AuthResult getAuthResultWithBearerToken(AuthResult authResult) { byteBuffer.putLong(uuid.getLeastSignificantBits()); final String newToken = Base64.getEncoder().encodeToString(byteBuffer.array()); bearerToken = newToken; - authResultWithBearerToken = new AuthResult() { - @Override - public String getPeerIdentity() { - return authResult.getPeerIdentity(); - } + authResultWithBearerToken = + new AuthResult() { + @Override + public String getPeerIdentity() { + return authResult.getPeerIdentity(); + } - @Override - public void appendToOutgoingHeaders(CallHeaders outgoingHeaders) { - authResult.appendToOutgoingHeaders(outgoingHeaders); - outgoingHeaders.insert(Auth2Constants.AUTHORIZATION_HEADER, Auth2Constants.BEARER_PREFIX + newToken); - } - }; + @Override + public void appendToOutgoingHeaders(CallHeaders outgoingHeaders) { + authResult.appendToOutgoingHeaders(outgoingHeaders); + outgoingHeaders.insert( + Auth2Constants.AUTHORIZATION_HEADER, Auth2Constants.BEARER_PREFIX + newToken); + } + }; } else { // Use the bearer token supplied by the original auth result. authResultWithBearerToken = authResult; diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ServerCallHeaderAuthMiddleware.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ServerCallHeaderAuthMiddleware.java index 9bfa7381844..727b04dad6a 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ServerCallHeaderAuthMiddleware.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/auth2/ServerCallHeaderAuthMiddleware.java @@ -26,18 +26,18 @@ import org.apache.arrow.flight.RequestContext; /** - * Middleware that's used to validate credentials during the handshake and verify - * the bearer token in subsequent requests. + * Middleware that's used to validate credentials during the handshake and verify the bearer token + * in subsequent requests. */ public class ServerCallHeaderAuthMiddleware implements FlightServerMiddleware { - /** - * Factory for accessing ServerAuthMiddleware. - */ - public static class Factory implements FlightServerMiddleware.Factory { + /** Factory for accessing ServerAuthMiddleware. */ + public static class Factory + implements FlightServerMiddleware.Factory { private final CallHeaderAuthenticator authHandler; /** * Construct a factory with the given auth handler. + * * @param authHandler The auth handler what will be used for authenticating requests. */ public Factory(CallHeaderAuthenticator authHandler) { @@ -45,8 +45,8 @@ public Factory(CallHeaderAuthenticator authHandler) { } @Override - public ServerCallHeaderAuthMiddleware onCallStarted(CallInfo callInfo, CallHeaders incomingHeaders, - RequestContext context) { + public ServerCallHeaderAuthMiddleware onCallStarted( + CallInfo callInfo, CallHeaders incomingHeaders, RequestContext context) { final AuthResult result = authHandler.authenticate(incomingHeaders); context.put(Auth2Constants.PEER_IDENTITY_KEY, result.getPeerIdentity()); return new ServerCallHeaderAuthMiddleware(result); @@ -65,10 +65,8 @@ public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) { } @Override - public void onCallCompleted(CallStatus status) { - } + public void onCallCompleted(CallStatus status) {} @Override - public void onCallErrored(Throwable err) { - } + public void onCallErrored(Throwable err) {} } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/client/ClientCookieMiddleware.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/client/ClientCookieMiddleware.java index 56f24e1019c..cf71df97f5b 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/client/ClientCookieMiddleware.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/client/ClientCookieMiddleware.java @@ -23,7 +23,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.stream.Collectors; - import org.apache.arrow.flight.CallHeaders; import org.apache.arrow.flight.CallInfo; import org.apache.arrow.flight.CallStatus; @@ -33,13 +32,12 @@ import org.slf4j.LoggerFactory; /** - * A client middleware for receiving and sending cookie information. - * Note that this class will not persist permanent cookies beyond the lifetime - * of this session. + * A client middleware for receiving and sending cookie information. Note that this class will not + * persist permanent cookies beyond the lifetime of this session. * - * This middleware will automatically remove cookies that have expired. - * Note: Negative max-age values currently do not get marked as expired due to - * a JDK issue. Use max-age=0 to explicitly remove an existing cookie. + *

This middleware will automatically remove cookies that have expired. Note: Negative + * max-age values currently do not get marked as expired due to a JDK issue. Use max-age=0 to + * explicitly remove an existing cookie. */ public class ClientCookieMiddleware implements FlightClientMiddleware { private static final Logger LOGGER = LoggerFactory.getLogger(ClientCookieMiddleware.class); @@ -54,9 +52,7 @@ public class ClientCookieMiddleware implements FlightClientMiddleware { this.factory = factory; } - /** - * Factory used within FlightClient. - */ + /** Factory used within FlightClient. */ public static class Factory implements FlightClientMiddleware.Factory { // Use a map to track the most recent version of a cookie from the server. // Note that cookie names are case-sensitive (but header names aren't). @@ -74,21 +70,24 @@ private void updateCookies(Iterable newCookieHeaderValues) { // with a different value and the client will use the new value in future requests. // The server can also update a cookie to have an Expiry in the past or negative age // to signal that the client should stop using the cookie immediately. - newCookieHeaderValues.forEach(headerValue -> { - try { - final List parsedCookies = HttpCookie.parse(headerValue); - parsedCookies.forEach(parsedCookie -> { - final String cookieNameLc = parsedCookie.getName().toLowerCase(Locale.ENGLISH); - if (parsedCookie.hasExpired()) { - cookies.remove(cookieNameLc); - } else { - cookies.put(parsedCookie.getName().toLowerCase(Locale.ENGLISH), parsedCookie); + newCookieHeaderValues.forEach( + headerValue -> { + try { + final List parsedCookies = HttpCookie.parse(headerValue); + parsedCookies.forEach( + parsedCookie -> { + final String cookieNameLc = parsedCookie.getName().toLowerCase(Locale.ENGLISH); + if (parsedCookie.hasExpired()) { + cookies.remove(cookieNameLc); + } else { + cookies.put(parsedCookie.getName().toLowerCase(Locale.ENGLISH), parsedCookie); + } + }); + } catch (IllegalArgumentException ex) { + LOGGER.warn( + "Skipping incorrectly formatted Set-Cookie header with value '{}'.", headerValue); } }); - } catch (IllegalArgumentException ex) { - LOGGER.warn("Skipping incorrectly formatted Set-Cookie header with value '{}'.", headerValue); - } - }); } } @@ -109,13 +108,9 @@ public void onHeadersReceived(CallHeaders incomingHeaders) { } @Override - public void onCallCompleted(CallStatus status) { - - } + public void onCallCompleted(CallStatus status) {} - /** - * Discards expired cookies and returns the valid cookies as a String delimited by ';'. - */ + /** Discards expired cookies and returns the valid cookies as a String delimited by ';'. */ @VisibleForTesting String getValidCookiesAsString() { // Discard expired cookies. diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/AddWritableBuffer.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/AddWritableBuffer.java index 4a99ab22842..5493c22ce46 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/AddWritableBuffer.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/AddWritableBuffer.java @@ -17,6 +17,7 @@ package org.apache.arrow.flight.grpc; +import io.netty.buffer.ByteBuf; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Constructor; @@ -25,8 +26,6 @@ import java.lang.reflect.Method; import java.util.List; -import io.netty.buffer.ByteBuf; - /** * Allow a user to add a ByteBuf based InputStream directly into GRPC WritableBuffer to avoid an * extra copy. This could be solved in GRPC by adding a ByteBufListable interface on InputStream and @@ -41,7 +40,6 @@ public class AddWritableBuffer { private static final Class bufChainOut; static { - Constructor tmpConstruct = null; Field tmpBufferList = null; Field tmpCurrent = null; @@ -54,7 +52,8 @@ public class AddWritableBuffer { Constructor tmpConstruct2 = nwb.getDeclaredConstructor(ByteBuf.class); tmpConstruct2.setAccessible(true); - Class tmpBufChainOut2 = Class.forName("io.grpc.internal.MessageFramer$BufferChainOutputStream"); + Class tmpBufChainOut2 = + Class.forName("io.grpc.internal.MessageFramer$BufferChainOutputStream"); Field tmpBufferList2 = tmpBufChainOut2.getDeclaredField("bufferList"); tmpBufferList2.setAccessible(true); @@ -72,7 +71,8 @@ public class AddWritableBuffer { tmpBufChainOut = tmpBufChainOut2; } catch (Exception ex) { - new RuntimeException("Failed to initialize AddWritableBuffer, falling back to slow path", ex).printStackTrace(); + new RuntimeException("Failed to initialize AddWritableBuffer, falling back to slow path", ex) + .printStackTrace(); } bufConstruct = tmpConstruct; @@ -80,18 +80,22 @@ public class AddWritableBuffer { current = tmpCurrent; listAdd = tmpListAdd; bufChainOut = tmpBufChainOut; - } /** - * Add the provided ByteBuf to the gRPC BufferChainOutputStream if possible, else copy the buffer to the stream. + * Add the provided ByteBuf to the gRPC BufferChainOutputStream if possible, else copy the buffer + * to the stream. + * * @param buf The buffer to add. * @param stream The Candidate OutputStream to add to. - * @param tryZeroCopy If true, try to zero-copy append the buffer to the stream. This may not succeed. + * @param tryZeroCopy If true, try to zero-copy append the buffer to the stream. This may not + * succeed. * @return True if buffer was zero-copy added to the stream. False if the buffer was copied. - * @throws IOException if the fast path is not enabled and there was an error copying the buffer to the stream. + * @throws IOException if the fast path is not enabled and there was an error copying the buffer + * to the stream. */ - public static boolean add(ByteBuf buf, OutputStream stream, boolean tryZeroCopy) throws IOException { + public static boolean add(ByteBuf buf, OutputStream stream, boolean tryZeroCopy) + throws IOException { if (!tryZeroCopy || !tryAddBuffer(buf, stream)) { buf.getBytes(0, stream, buf.readableBytes()); return false; @@ -120,7 +124,10 @@ private static boolean tryAddBuffer(ByteBuf buf, OutputStream stream) throws IOE listAdd.invoke(list, obj); current.set(stream, obj); return true; - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) { + } catch (IllegalAccessException + | IllegalArgumentException + | InvocationTargetException + | InstantiationException e) { e.printStackTrace(); return false; } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/CallCredentialAdapter.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/CallCredentialAdapter.java index 285ddb9ba6e..2c496986bab 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/CallCredentialAdapter.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/CallCredentialAdapter.java @@ -17,17 +17,13 @@ package org.apache.arrow.flight.grpc; +import io.grpc.CallCredentials; +import io.grpc.Metadata; import java.util.concurrent.Executor; import java.util.function.Consumer; - import org.apache.arrow.flight.CallHeaders; -import io.grpc.CallCredentials; -import io.grpc.Metadata; - -/** - * Adapter class to utilize a CredentialWriter to implement Grpc CallCredentials. - */ +/** Adapter class to utilize a CredentialWriter to implement Grpc CallCredentials. */ public class CallCredentialAdapter extends CallCredentials { private final Consumer credentialWriter; @@ -37,13 +33,14 @@ public CallCredentialAdapter(Consumer credentialWriter) { } @Override - public void applyRequestMetadata(RequestInfo requestInfo, Executor executor, MetadataApplier metadataApplier) { - executor.execute(() -> - { - final Metadata headers = new Metadata(); - credentialWriter.accept(new MetadataAdapter(headers)); - metadataApplier.apply(headers); - }); + public void applyRequestMetadata( + RequestInfo requestInfo, Executor executor, MetadataApplier metadataApplier) { + executor.execute( + () -> { + final Metadata headers = new Metadata(); + credentialWriter.accept(new MetadataAdapter(headers)); + metadataApplier.apply(headers); + }); } @Override diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ClientInterceptorAdapter.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ClientInterceptorAdapter.java index db27aa481ec..69c7cd8a705 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ClientInterceptorAdapter.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ClientInterceptorAdapter.java @@ -17,16 +17,6 @@ package org.apache.arrow.flight.grpc; -import java.util.ArrayList; -import java.util.List; - -import org.apache.arrow.flight.CallInfo; -import org.apache.arrow.flight.CallStatus; -import org.apache.arrow.flight.FlightClientMiddleware; -import org.apache.arrow.flight.FlightMethod; -import org.apache.arrow.flight.FlightRuntimeException; -import org.apache.arrow.flight.FlightStatusCode; - import io.grpc.CallOptions; import io.grpc.Channel; import io.grpc.ClientCall; @@ -37,11 +27,20 @@ import io.grpc.MethodDescriptor; import io.grpc.Status; import io.grpc.StatusRuntimeException; +import java.util.ArrayList; +import java.util.List; +import org.apache.arrow.flight.CallInfo; +import org.apache.arrow.flight.CallStatus; +import org.apache.arrow.flight.FlightClientMiddleware; +import org.apache.arrow.flight.FlightMethod; +import org.apache.arrow.flight.FlightRuntimeException; +import org.apache.arrow.flight.FlightStatusCode; /** * An adapter between Flight client middleware and gRPC interceptors. * - *

This is implemented as a single gRPC interceptor that runs all Flight client middleware sequentially. + *

This is implemented as a single gRPC interceptor that runs all Flight client middleware + * sequentially. */ public class ClientInterceptorAdapter implements ClientInterceptor { @@ -52,8 +51,8 @@ public ClientInterceptorAdapter(List factories) } @Override - public ClientCall interceptCall(MethodDescriptor method, - CallOptions callOptions, Channel next) { + public ClientCall interceptCall( + MethodDescriptor method, CallOptions callOptions, Channel next) { final List middleware = new ArrayList<>(); final CallInfo info = new CallInfo(FlightMethod.fromProtocol(method.getFullMethodName())); @@ -73,15 +72,17 @@ public ClientCall interceptCall(MethodDescriptor extends SimpleForwardingClientCallListener { + private static class FlightClientCallListener + extends SimpleForwardingClientCallListener { private final List middleware; boolean receivedHeaders; - public FlightClientCallListener(ClientCall.Listener responseListener, - List middleware) { + public FlightClientCallListener( + ClientCall.Listener responseListener, List middleware) { super(responseListener); this.middleware = middleware; receivedHeaders = false; @@ -103,8 +104,10 @@ public void onHeaders(Metadata headers) { public void onClose(Status status, Metadata trailers) { try { if (!receivedHeaders) { - // gRPC doesn't always send response headers if the call errors or completes immediately, but instead - // consolidates them with the trailers. If we never got headers, assume this happened and run the header + // gRPC doesn't always send response headers if the call errors or completes immediately, + // but instead + // consolidates them with the trailers. If we never got headers, assume this happened and + // run the header // callback with the trailers. final MetadataAdapter adapter = new MetadataAdapter(trailers); middleware.forEach(m -> m.onHeadersReceived(adapter)); @@ -121,11 +124,13 @@ public void onClose(Status status, Metadata trailers) { /** * The gRPC ClientCall which hooks into the gRPC request cycle and injects our ClientCallListener. */ - private static class FlightClientCall extends SimpleForwardingClientCall { + private static class FlightClientCall + extends SimpleForwardingClientCall { private final List middleware; - public FlightClientCall(ClientCall clientCall, List middleware) { + public FlightClientCall( + ClientCall clientCall, List middleware) { super(clientCall); this.middleware = middleware; } @@ -140,7 +145,8 @@ public void start(Listener responseListener, Metadata headers) { @Override public void cancel(String message, Throwable cause) { - final CallStatus flightStatus = new CallStatus(FlightStatusCode.CANCELLED, cause, message, null); + final CallStatus flightStatus = + new CallStatus(FlightStatusCode.CANCELLED, cause, message, null); middleware.forEach(m -> m.onCallCompleted(flightStatus)); super.cancel(message, cause); } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ContextPropagatingExecutorService.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ContextPropagatingExecutorService.java index 8f6bb6db29b..85a935fe3b9 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ContextPropagatingExecutorService.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ContextPropagatingExecutorService.java @@ -17,6 +17,7 @@ package org.apache.arrow.flight.grpc; +import io.grpc.Context; import java.util.Collection; import java.util.List; import java.util.concurrent.Callable; @@ -27,14 +28,12 @@ import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; -import io.grpc.Context; - /** * An {@link ExecutorService} that propagates the {@link Context}. * - *

Context is used to propagate per-call state, like the authenticated user, between threads (as gRPC makes no - * guarantees about what thread things execute on). This wrapper makes it easy to preserve this when using an Executor. - * The Context itself is immutable, so it is thread-safe. + *

Context is used to propagate per-call state, like the authenticated user, between threads (as + * gRPC makes no guarantees about what thread things execute on). This wrapper makes it easy to + * preserve this when using an Executor. The Context itself is immutable, so it is thread-safe. */ public class ContextPropagatingExecutorService implements ExecutorService { @@ -89,25 +88,32 @@ public Future submit(Runnable task) { } @Override - public List> invokeAll(Collection> tasks) throws InterruptedException { - return delegate.invokeAll(tasks.stream().map(Context.current()::wrap).collect(Collectors.toList())); + public List> invokeAll(Collection> tasks) + throws InterruptedException { + return delegate.invokeAll( + tasks.stream().map(Context.current()::wrap).collect(Collectors.toList())); } @Override - public List> invokeAll(Collection> tasks, long timeout, - TimeUnit unit) throws InterruptedException { - return delegate.invokeAll(tasks.stream().map(Context.current()::wrap).collect(Collectors.toList()), timeout, unit); + public List> invokeAll( + Collection> tasks, long timeout, TimeUnit unit) + throws InterruptedException { + return delegate.invokeAll( + tasks.stream().map(Context.current()::wrap).collect(Collectors.toList()), timeout, unit); } @Override - public T invokeAny(Collection> tasks) throws InterruptedException, ExecutionException { - return delegate.invokeAny(tasks.stream().map(Context.current()::wrap).collect(Collectors.toList())); + public T invokeAny(Collection> tasks) + throws InterruptedException, ExecutionException { + return delegate.invokeAny( + tasks.stream().map(Context.current()::wrap).collect(Collectors.toList())); } @Override public T invokeAny(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { - return delegate.invokeAny(tasks.stream().map(Context.current()::wrap).collect(Collectors.toList()), timeout, unit); + return delegate.invokeAny( + tasks.stream().map(Context.current()::wrap).collect(Collectors.toList()), timeout, unit); } @Override diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/CredentialCallOption.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/CredentialCallOption.java index 3bde7a83511..c8bfcd5c938 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/CredentialCallOption.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/CredentialCallOption.java @@ -17,16 +17,12 @@ package org.apache.arrow.flight.grpc; +import io.grpc.stub.AbstractStub; import java.util.function.Consumer; - import org.apache.arrow.flight.CallHeaders; import org.apache.arrow.flight.CallOptions; -import io.grpc.stub.AbstractStub; - -/** - * Method option for supplying credentials to method calls. - */ +/** Method option for supplying credentials to method calls. */ public class CredentialCallOption implements CallOptions.GrpcCallOption { private final Consumer credentialWriter; diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/GetReadableBuffer.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/GetReadableBuffer.java index 82cfd7f39c9..f9a29564186 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/GetReadableBuffer.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/GetReadableBuffer.java @@ -17,17 +17,14 @@ package org.apache.arrow.flight.grpc; +import com.google.common.base.Throwables; +import com.google.common.io.ByteStreams; +import io.grpc.internal.ReadableBuffer; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; - import org.apache.arrow.memory.ArrowBuf; -import com.google.common.base.Throwables; -import com.google.common.io.ByteStreams; - -import io.grpc.internal.ReadableBuffer; - /** * Enable access to ReadableBuffer directly to copy data from a BufferInputStream into a target * ByteBuffer/ByteBuf. @@ -51,7 +48,8 @@ public class GetReadableBuffer { tmpField = f; tmpClazz = clazz; } catch (Exception e) { - new RuntimeException("Failed to initialize GetReadableBuffer, falling back to slow path", e).printStackTrace(); + new RuntimeException("Failed to initialize GetReadableBuffer, falling back to slow path", e) + .printStackTrace(); } READABLE_BUFFER = tmpField; BUFFER_INPUT_STREAM = tmpClazz; @@ -60,8 +58,8 @@ public class GetReadableBuffer { /** * Extracts the ReadableBuffer for the given input stream. * - * @param is Must be an instance of io.grpc.internal.ReadableBuffers$BufferInputStream or - * null will be returned. + * @param is Must be an instance of io.grpc.internal.ReadableBuffers$BufferInputStream or null + * will be returned. */ public static ReadableBuffer getReadableBuffer(InputStream is) { @@ -78,14 +76,17 @@ public static ReadableBuffer getReadableBuffer(InputStream is) { /** * Helper method to read a gRPC-provided InputStream into an ArrowBuf. + * * @param stream The stream to read from. Should be an instance of {@link #BUFFER_INPUT_STREAM}. * @param buf The buffer to read into. * @param size The number of bytes to read. - * @param fastPath Whether to enable the fast path (i.e. detect whether the stream is a {@link #BUFFER_INPUT_STREAM}). + * @param fastPath Whether to enable the fast path (i.e. detect whether the stream is a {@link + * #BUFFER_INPUT_STREAM}). * @throws IOException if there is an error reading form the stream */ - public static void readIntoBuffer(final InputStream stream, final ArrowBuf buf, final int size, - final boolean fastPath) throws IOException { + public static void readIntoBuffer( + final InputStream stream, final ArrowBuf buf, final int size, final boolean fastPath) + throws IOException { ReadableBuffer readableBuffer = fastPath ? getReadableBuffer(stream) : null; if (readableBuffer != null) { readableBuffer.readBytes(buf.nioBuffer(0, size)); diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/MetadataAdapter.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/MetadataAdapter.java index 25e4fb0197f..3d64c8b6483 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/MetadataAdapter.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/MetadataAdapter.java @@ -17,21 +17,19 @@ package org.apache.arrow.flight.grpc; +import io.grpc.Metadata; import java.nio.charset.StandardCharsets; import java.util.HashSet; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.StreamSupport; - import org.apache.arrow.flight.CallHeaders; -import io.grpc.Metadata; -import io.grpc.Metadata.Key; - /** * A mutable adapter between the gRPC Metadata object and the Flight headers interface. * - *

This allows us to present the headers (metadata) from gRPC without copying to/from our own object. + *

This allows us to present the headers (metadata) from gRPC without copying to/from our own + * object. */ public class MetadataAdapter implements CallHeaders { @@ -65,7 +63,8 @@ public Iterable getAllByte(String key) { return this.metadata.getAll(Metadata.Key.of(key, Metadata.BINARY_BYTE_MARSHALLER)); } return StreamSupport.stream(getAll(key).spliterator(), false) - .map(String::getBytes).collect(Collectors.toList()); + .map(String::getBytes) + .collect(Collectors.toList()); } @Override diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/RequestContextAdapter.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/RequestContextAdapter.java index 9be4d12b90e..039af40c987 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/RequestContextAdapter.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/RequestContextAdapter.java @@ -17,26 +17,22 @@ package org.apache.arrow.flight.grpc; +import io.grpc.Context; import java.util.HashMap; import java.util.Set; - import org.apache.arrow.flight.RequestContext; -import io.grpc.Context; - - -/** - * Adapter for holding key value pairs. - */ +/** Adapter for holding key value pairs. */ public class RequestContextAdapter implements RequestContext { public static final Context.Key REQUEST_CONTEXT_KEY = - Context.key("arrow-flight-request-context"); + Context.key("arrow-flight-request-context"); private final HashMap map = new HashMap<>(); @Override public void put(String key, String value) { if (map.putIfAbsent(key, value) != null) { - throw new IllegalArgumentException("Duplicate write to a RequestContext at key " + key + " not allowed."); + throw new IllegalArgumentException( + "Duplicate write to a RequestContext at key " + key + " not allowed."); } } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ServerInterceptorAdapter.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ServerInterceptorAdapter.java index 70c667df560..89b895181a1 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ServerInterceptorAdapter.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/ServerInterceptorAdapter.java @@ -17,12 +17,20 @@ package org.apache.arrow.flight.grpc; +import io.grpc.Context; +import io.grpc.Contexts; +import io.grpc.ForwardingServerCall.SimpleForwardingServerCall; +import io.grpc.Metadata; +import io.grpc.ServerCall; +import io.grpc.ServerCall.Listener; +import io.grpc.ServerCallHandler; +import io.grpc.ServerInterceptor; +import io.grpc.Status; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; - import org.apache.arrow.flight.CallInfo; import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.FlightConstants; @@ -30,24 +38,14 @@ import org.apache.arrow.flight.FlightProducer.CallContext; import org.apache.arrow.flight.FlightRuntimeException; import org.apache.arrow.flight.FlightServerMiddleware; -import org.apache.arrow.flight.FlightServerMiddleware.Factory; import org.apache.arrow.flight.FlightServerMiddleware.Key; -import io.grpc.Context; -import io.grpc.Contexts; -import io.grpc.ForwardingServerCall.SimpleForwardingServerCall; -import io.grpc.Metadata; -import io.grpc.ServerCall; -import io.grpc.ServerCall.Listener; -import io.grpc.ServerCallHandler; -import io.grpc.ServerInterceptor; -import io.grpc.Status; - /** * An adapter between Flight middleware and a gRPC interceptor. * - *

This is implemented as a single gRPC interceptor that runs all Flight server middleware sequentially. Flight - * middleware instances are stored in the gRPC Context so their state is accessible later. + *

This is implemented as a single gRPC interceptor that runs all Flight server middleware + * sequentially. Flight middleware instances are stored in the gRPC Context so their state is + * accessible later. */ public class ServerInterceptorAdapter implements ServerInterceptor { @@ -61,7 +59,8 @@ public static class KeyFactory { private final FlightServerMiddleware.Key key; private final FlightServerMiddleware.Factory factory; - public KeyFactory(FlightServerMiddleware.Key key, FlightServerMiddleware.Factory factory) { + public KeyFactory( + FlightServerMiddleware.Key key, FlightServerMiddleware.Factory factory) { this.key = key; this.factory = factory; } @@ -70,10 +69,12 @@ public KeyFactory(FlightServerMiddleware.Key key, FlightServerMiddleware.Fact /** * The {@link Context.Key} that stores the Flight middleware active for a particular call. * - *

Applications should not use this directly. Instead, see {@link CallContext#getMiddleware(Key)}. + *

Applications should not use this directly. Instead, see {@link + * CallContext#getMiddleware(Key)}. */ - public static final Context.Key, FlightServerMiddleware>> SERVER_MIDDLEWARE_KEY = - Context.key("arrow.flight.server_middleware"); + public static final Context.Key, FlightServerMiddleware>> + SERVER_MIDDLEWARE_KEY = Context.key("arrow.flight.server_middleware"); + private final List> factories; public ServerInterceptorAdapter(List> factories) { @@ -81,16 +82,18 @@ public ServerInterceptorAdapter(List> factories) { } @Override - public Listener interceptCall(ServerCall call, Metadata headers, - ServerCallHandler next) { + public Listener interceptCall( + ServerCall call, Metadata headers, ServerCallHandler next) { if (!FlightConstants.SERVICE.equals(call.getMethodDescriptor().getServiceName())) { return Contexts.interceptCall(Context.current(), call, headers, next); } - - final CallInfo info = new CallInfo(FlightMethod.fromProtocol(call.getMethodDescriptor().getFullMethodName())); + + final CallInfo info = + new CallInfo(FlightMethod.fromProtocol(call.getMethodDescriptor().getFullMethodName())); final List middleware = new ArrayList<>(); // Use LinkedHashMap to preserve insertion order - final Map, FlightServerMiddleware> middlewareMap = new LinkedHashMap<>(); + final Map, FlightServerMiddleware> middlewareMap = + new LinkedHashMap<>(); final MetadataAdapter headerAdapter = new MetadataAdapter(headers); final RequestContextAdapter requestContextAdapter = new RequestContextAdapter(); for (final KeyFactory factory : factories) { @@ -106,45 +109,50 @@ public Listener interceptCall(ServerCall call, middlewareMap.put(factory.key, m); } - // Inject the middleware into the context so RPC method implementations can communicate with middleware instances - final Context contextWithMiddlewareAndRequestsOptions = Context.current() - .withValue(SERVER_MIDDLEWARE_KEY, Collections.unmodifiableMap(middlewareMap)) - .withValue(RequestContextAdapter.REQUEST_CONTEXT_KEY, requestContextAdapter); - - final SimpleForwardingServerCall forwardingServerCall = new SimpleForwardingServerCall( - call) { - boolean sentHeaders = false; - - @Override - public void sendHeaders(Metadata headers) { - sentHeaders = true; - try { - final MetadataAdapter headerAdapter = new MetadataAdapter(headers); - middleware.forEach(m -> m.onBeforeSendingHeaders(headerAdapter)); - } finally { - // Make sure to always call the gRPC callback to avoid interrupting the gRPC request cycle - super.sendHeaders(headers); - } - } - - @Override - public void close(Status status, Metadata trailers) { - try { - if (!sentHeaders) { - // gRPC doesn't always send response headers if the call errors or completes immediately - final MetadataAdapter headerAdapter = new MetadataAdapter(trailers); - middleware.forEach(m -> m.onBeforeSendingHeaders(headerAdapter)); + // Inject the middleware into the context so RPC method implementations can communicate with + // middleware instances + final Context contextWithMiddlewareAndRequestsOptions = + Context.current() + .withValue(SERVER_MIDDLEWARE_KEY, Collections.unmodifiableMap(middlewareMap)) + .withValue(RequestContextAdapter.REQUEST_CONTEXT_KEY, requestContextAdapter); + + final SimpleForwardingServerCall forwardingServerCall = + new SimpleForwardingServerCall(call) { + boolean sentHeaders = false; + + @Override + public void sendHeaders(Metadata headers) { + sentHeaders = true; + try { + final MetadataAdapter headerAdapter = new MetadataAdapter(headers); + middleware.forEach(m -> m.onBeforeSendingHeaders(headerAdapter)); + } finally { + // Make sure to always call the gRPC callback to avoid interrupting the gRPC request + // cycle + super.sendHeaders(headers); + } } - } finally { - // Make sure to always call the gRPC callback to avoid interrupting the gRPC request cycle - super.close(status, trailers); - } - - final CallStatus flightStatus = StatusUtils.fromGrpcStatus(status); - middleware.forEach(m -> m.onCallCompleted(flightStatus)); - } - }; - return Contexts.interceptCall(contextWithMiddlewareAndRequestsOptions, forwardingServerCall, headers, next); + @Override + public void close(Status status, Metadata trailers) { + try { + if (!sentHeaders) { + // gRPC doesn't always send response headers if the call errors or completes + // immediately + final MetadataAdapter headerAdapter = new MetadataAdapter(trailers); + middleware.forEach(m -> m.onBeforeSendingHeaders(headerAdapter)); + } + } finally { + // Make sure to always call the gRPC callback to avoid interrupting the gRPC request + // cycle + super.close(status, trailers); + } + + final CallStatus flightStatus = StatusUtils.fromGrpcStatus(status); + middleware.forEach(m -> m.onCallCompleted(flightStatus)); + } + }; + return Contexts.interceptCall( + contextWithMiddlewareAndRequestsOptions, forwardingServerCall, headers, next); } } diff --git a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/StatusUtils.java b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/StatusUtils.java index 7f0dcf2da3f..739073b0977 100644 --- a/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/StatusUtils.java +++ b/java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/StatusUtils.java @@ -17,23 +17,21 @@ package org.apache.arrow.flight.grpc; +import io.grpc.InternalMetadata; +import io.grpc.Metadata; +import io.grpc.Status; +import io.grpc.Status.Code; +import io.grpc.StatusException; +import io.grpc.StatusRuntimeException; import java.nio.charset.StandardCharsets; import java.util.Iterator; import java.util.Objects; import java.util.function.Function; - import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.ErrorFlightMetadata; import org.apache.arrow.flight.FlightRuntimeException; import org.apache.arrow.flight.FlightStatusCode; -import io.grpc.InternalMetadata; -import io.grpc.Metadata; -import io.grpc.Status; -import io.grpc.Status.Code; -import io.grpc.StatusException; -import io.grpc.StatusRuntimeException; - /** * Utilities to adapt gRPC and Flight status objects. * @@ -45,9 +43,7 @@ private StatusUtils() { throw new AssertionError("Do not instantiate this class."); } - /** - * Convert from a Flight status code to a gRPC status code. - */ + /** Convert from a Flight status code to a gRPC status code. */ public static Status.Code toGrpcStatusCode(FlightStatusCode code) { switch (code) { case OK: @@ -79,9 +75,7 @@ public static Status.Code toGrpcStatusCode(FlightStatusCode code) { } } - /** - * Convert from a gRPC status code to a Flight status code. - */ + /** Convert from a gRPC status code to a Flight status code. */ public static FlightStatusCode fromGrpcStatusCode(Status.Code code) { switch (code) { case OK: @@ -136,28 +130,29 @@ static Metadata.Key keyOfAscii(String name) { /** Convert from a gRPC Status & trailers to a Flight status. */ public static CallStatus fromGrpcStatusAndTrailers(Status status, Metadata trailers) { - // gRPC may not always have trailers - this happens when the server internally generates an error, which is rare, + // gRPC may not always have trailers - this happens when the server internally generates an + // error, which is rare, // but can happen. final ErrorFlightMetadata errorMetadata = trailers == null ? null : parseTrailers(trailers); return new CallStatus( - fromGrpcStatusCode(status.getCode()), - status.getCause(), - status.getDescription(), - errorMetadata); + fromGrpcStatusCode(status.getCode()), + status.getCause(), + status.getDescription(), + errorMetadata); } /** Convert from a gRPC status to a Flight status. */ public static CallStatus fromGrpcStatus(Status status) { return new CallStatus( - fromGrpcStatusCode(status.getCode()), - status.getCause(), - status.getDescription(), - null); + fromGrpcStatusCode(status.getCode()), status.getCause(), status.getDescription(), null); } /** Convert from a Flight status to a gRPC status. */ public static Status toGrpcStatus(CallStatus status) { - return toGrpcStatusCode(status.code()).toStatus().withDescription(status.description()).withCause(status.cause()); + return toGrpcStatusCode(status.code()) + .toStatus() + .withDescription(status.description()) + .withCause(status.cause()); } /** Convert from a gRPC exception to a Flight exception. */ @@ -172,15 +167,15 @@ private static ErrorFlightMetadata parseTrailers(Metadata trailers) { if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) { metadata.insert(key, trailers.get(keyOfBinary(key))); } else { - metadata.insert(key, Objects.requireNonNull(trailers.get(keyOfAscii(key))).getBytes(StandardCharsets.UTF_8)); + metadata.insert( + key, + Objects.requireNonNull(trailers.get(keyOfAscii(key))).getBytes(StandardCharsets.UTF_8)); } } return metadata; } - /** - * Convert arbitrary exceptions to a {@link FlightRuntimeException}. - */ + /** Convert arbitrary exceptions to a {@link FlightRuntimeException}. */ public static FlightRuntimeException fromThrowable(Throwable t) { if (t instanceof StatusRuntimeException) { return fromGrpcRuntimeException((StatusRuntimeException) t); @@ -193,8 +188,8 @@ public static FlightRuntimeException fromThrowable(Throwable t) { /** * Convert arbitrary exceptions to a {@link StatusRuntimeException} or {@link StatusException}. * - *

Such exceptions can be passed to {@link io.grpc.stub.StreamObserver#onError(Throwable)} and will give the client - * a reasonable error message. + *

Such exceptions can be passed to {@link io.grpc.stub.StreamObserver#onError(Throwable)} and + * will give the client a reasonable error message. */ public static Throwable toGrpcException(Throwable ex) { if (ex instanceof StatusRuntimeException) { @@ -209,7 +204,9 @@ public static Throwable toGrpcException(Throwable ex) { } return toGrpcStatus(fre.status()).asRuntimeException(); } - return Status.INTERNAL.withCause(ex).withDescription("There was an error servicing your request.") + return Status.INTERNAL + .withCause(ex) + .withDescription("There was an error servicing your request.") .asRuntimeException(); } @@ -226,11 +223,11 @@ private static Metadata toGrpcMetadata(ErrorFlightMetadata metadata) { } /** - * Maps a transformation function to the elements of an iterator, while wrapping exceptions in {@link - * FlightRuntimeException}. + * Maps a transformation function to the elements of an iterator, while wrapping exceptions in + * {@link FlightRuntimeException}. */ - public static Iterator wrapIterator(Iterator fromIterator, - Function transformer) { + public static Iterator wrapIterator( + Iterator fromIterator, Function transformer) { Objects.requireNonNull(fromIterator); Objects.requireNonNull(transformer); return new Iterator() { diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/FlightTestUtil.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/FlightTestUtil.java index 393fa086775..1b51439cce4 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/FlightTestUtil.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/FlightTestUtil.java @@ -23,14 +23,11 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.List; - import org.apache.arrow.vector.test.util.ArrowTestDataUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.function.Executable; -/** - * Utility methods and constants for testing flight servers. - */ +/** Utility methods and constants for testing flight servers. */ public class FlightTestUtil { public static final String LOCALHOST = "localhost"; @@ -47,9 +44,12 @@ static List exampleTlsCerts() { final Path root = getFlightTestDataRoot(); final Path cert0Pem = root.resolve("cert0.pem"); if (!Files.exists(cert0Pem)) { - throw new RuntimeException(cert0Pem + " doesn't exist. Make sure submodules are initialized (see https://arrow.apache.org/docs/dev/developers/java/building.html#building)"); + throw new RuntimeException( + cert0Pem + + " doesn't exist. Make sure submodules are initialized (see https://arrow.apache.org/docs/dev/developers/java/building.html#building)"); } - return Arrays.asList(new CertKeyPair(cert0Pem.toFile(), root.resolve("cert0.pkcs1").toFile()), + return Arrays.asList( + new CertKeyPair(cert0Pem.toFile(), root.resolve("cert0.pkcs1").toFile()), new CertKeyPair(root.resolve("cert1.pem").toFile(), root.resolve("cert1.pkcs1").toFile())); } @@ -57,7 +57,10 @@ static boolean isEpollAvailable() { try { Class epoll = Class.forName("io.netty.channel.epoll.Epoll"); return (Boolean) epoll.getMethod("isAvailable").invoke(null); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + } catch (ClassNotFoundException + | NoSuchMethodException + | IllegalAccessException + | InvocationTargetException e) { return false; } } @@ -66,7 +69,10 @@ static boolean isKqueueAvailable() { try { Class kqueue = Class.forName("io.netty.channel.kqueue.KQueue"); return (Boolean) kqueue.getMethod("isAvailable").invoke(null); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + } catch (ClassNotFoundException + | NoSuchMethodException + | IllegalAccessException + | InvocationTargetException e) { return false; } } @@ -77,6 +83,7 @@ static boolean isNativeTransportAvailable() { /** * Assert that the given runnable fails with a Flight exception of the given code. + * * @param code The expected Flight status code. * @param r The code to run. * @return The thrown status. @@ -98,6 +105,5 @@ public CertKeyPair(File cert, File key) { } } - private FlightTestUtil() { - } + private FlightTestUtil() {} } diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestApplicationMetadata.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestApplicationMetadata.java index 77c039afd87..024790cd998 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestApplicationMetadata.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestApplicationMetadata.java @@ -26,7 +26,6 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; - import org.apache.arrow.flight.FlightClient.PutListener; import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; @@ -40,9 +39,7 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -/** - * Tests for application-specific metadata support in Flight. - */ +/** Tests for application-specific metadata support in Flight. */ public class TestApplicationMetadata { // The command used to trigger the test for ARROW-6136. @@ -50,171 +47,186 @@ public class TestApplicationMetadata { // The expected error message. private static final String MESSAGE_ARROW_6136 = "The stream should not be double-closed."; - /** - * Ensure that a client can read the metadata sent from the server. - */ + /** Ensure that a client can read the metadata sent from the server. */ @Test // This test is consistently flaky on CI, unfortunately. @Disabled public void retrieveMetadata() { - test((allocator, client) -> { - try (final FlightStream stream = client.getStream(new Ticket(new byte[0]))) { - byte i = 0; - while (stream.next()) { - final IntVector vector = (IntVector) stream.getRoot().getVector("a"); - Assertions.assertEquals(1, vector.getValueCount()); - Assertions.assertEquals(10, vector.get(0)); - Assertions.assertEquals(i, stream.getLatestMetadata().getByte(0)); - i++; - } - } catch (Exception e) { - throw new RuntimeException(e); - } - }); + test( + (allocator, client) -> { + try (final FlightStream stream = client.getStream(new Ticket(new byte[0]))) { + byte i = 0; + while (stream.next()) { + final IntVector vector = (IntVector) stream.getRoot().getVector("a"); + Assertions.assertEquals(1, vector.getValueCount()); + Assertions.assertEquals(10, vector.get(0)); + Assertions.assertEquals(i, stream.getLatestMetadata().getByte(0)); + i++; + } + } catch (Exception e) { + throw new RuntimeException(e); + } + }); } - /** ARROW-6136: make sure that the Flight implementation doesn't double-close the server-to-client stream. */ + /** + * ARROW-6136: make sure that the Flight implementation doesn't double-close the server-to-client + * stream. + */ @Test public void arrow6136() { final Schema schema = new Schema(Collections.emptyList()); - test((allocator, client) -> { - try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { - final FlightDescriptor descriptor = FlightDescriptor.command(COMMAND_ARROW_6136); + test( + (allocator, client) -> { + try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final FlightDescriptor descriptor = FlightDescriptor.command(COMMAND_ARROW_6136); - final PutListener listener = new SyncPutListener(); - final FlightClient.ClientStreamListener writer = client.startPut(descriptor, root, listener); - // Must attempt to retrieve the result to get any server-side errors. - final CallStatus status = FlightTestUtil.assertCode(FlightStatusCode.INTERNAL, writer::getResult); - Assertions.assertEquals(MESSAGE_ARROW_6136, status.description()); - } catch (Exception e) { - throw new RuntimeException(e); - } - }); + final PutListener listener = new SyncPutListener(); + final FlightClient.ClientStreamListener writer = + client.startPut(descriptor, root, listener); + // Must attempt to retrieve the result to get any server-side errors. + final CallStatus status = + FlightTestUtil.assertCode(FlightStatusCode.INTERNAL, writer::getResult); + Assertions.assertEquals(MESSAGE_ARROW_6136, status.description()); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); } - /** - * Ensure that a client can send metadata to the server. - */ + /** Ensure that a client can send metadata to the server. */ @Test @Disabled public void uploadMetadataAsync() { - final Schema schema = new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); - test((allocator, client) -> { - try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { - final FlightDescriptor descriptor = FlightDescriptor.path("test"); + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); + test( + (allocator, client) -> { + try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final FlightDescriptor descriptor = FlightDescriptor.path("test"); - final PutListener listener = new AsyncPutListener() { - int counter = 0; + final PutListener listener = + new AsyncPutListener() { + int counter = 0; - @Override - public void onNext(PutResult val) { - Assertions.assertNotNull(val); - Assertions.assertEquals(counter, val.getApplicationMetadata().getByte(0)); - counter++; - } - }; - final FlightClient.ClientStreamListener writer = client.startPut(descriptor, root, listener); + @Override + public void onNext(PutResult val) { + Assertions.assertNotNull(val); + Assertions.assertEquals(counter, val.getApplicationMetadata().getByte(0)); + counter++; + } + }; + final FlightClient.ClientStreamListener writer = + client.startPut(descriptor, root, listener); - root.allocateNew(); - for (byte i = 0; i < 10; i++) { - final IntVector vector = (IntVector) root.getVector("a"); - final ArrowBuf metadata = allocator.buffer(1); - metadata.writeByte(i); - vector.set(0, 10); - vector.setValueCount(1); - root.setRowCount(1); - writer.putNext(metadata); - } - writer.completed(); - // Must attempt to retrieve the result to get any server-side errors. - writer.getResult(); - } catch (Exception e) { - throw new RuntimeException(e); - } - }); + root.allocateNew(); + for (byte i = 0; i < 10; i++) { + final IntVector vector = (IntVector) root.getVector("a"); + final ArrowBuf metadata = allocator.buffer(1); + metadata.writeByte(i); + vector.set(0, 10); + vector.setValueCount(1); + root.setRowCount(1); + writer.putNext(metadata); + } + writer.completed(); + // Must attempt to retrieve the result to get any server-side errors. + writer.getResult(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); } - /** - * Ensure that a client can send metadata to the server. Uses the synchronous API. - */ + /** Ensure that a client can send metadata to the server. Uses the synchronous API. */ @Test @Disabled public void uploadMetadataSync() { - final Schema schema = new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); - test((allocator, client) -> { - try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); - final SyncPutListener listener = new SyncPutListener()) { - final FlightDescriptor descriptor = FlightDescriptor.path("test"); - final FlightClient.ClientStreamListener writer = client.startPut(descriptor, root, listener); + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); + test( + (allocator, client) -> { + try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); + final SyncPutListener listener = new SyncPutListener()) { + final FlightDescriptor descriptor = FlightDescriptor.path("test"); + final FlightClient.ClientStreamListener writer = + client.startPut(descriptor, root, listener); - root.allocateNew(); - for (byte i = 0; i < 10; i++) { - final IntVector vector = (IntVector) root.getVector("a"); - final ArrowBuf metadata = allocator.buffer(1); - metadata.writeByte(i); - vector.set(0, 10); - vector.setValueCount(1); - root.setRowCount(1); - writer.putNext(metadata); - try (final PutResult message = listener.poll(5000, TimeUnit.SECONDS)) { - Assertions.assertNotNull(message); - Assertions.assertEquals(i, message.getApplicationMetadata().getByte(0)); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); + root.allocateNew(); + for (byte i = 0; i < 10; i++) { + final IntVector vector = (IntVector) root.getVector("a"); + final ArrowBuf metadata = allocator.buffer(1); + metadata.writeByte(i); + vector.set(0, 10); + vector.setValueCount(1); + root.setRowCount(1); + writer.putNext(metadata); + try (final PutResult message = listener.poll(5000, TimeUnit.SECONDS)) { + Assertions.assertNotNull(message); + Assertions.assertEquals(i, message.getApplicationMetadata().getByte(0)); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + } + writer.completed(); + // Must attempt to retrieve the result to get any server-side errors. + writer.getResult(); } - } - writer.completed(); - // Must attempt to retrieve the result to get any server-side errors. - writer.getResult(); - } - }); + }); } - /** - * Make sure that a {@link SyncPutListener} properly reclaims memory if ignored. - */ + /** Make sure that a {@link SyncPutListener} properly reclaims memory if ignored. */ @Test @Disabled public void syncMemoryReclaimed() { - final Schema schema = new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); - test((allocator, client) -> { - try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); - final SyncPutListener listener = new SyncPutListener()) { - final FlightDescriptor descriptor = FlightDescriptor.path("test"); - final FlightClient.ClientStreamListener writer = client.startPut(descriptor, root, listener); + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); + test( + (allocator, client) -> { + try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); + final SyncPutListener listener = new SyncPutListener()) { + final FlightDescriptor descriptor = FlightDescriptor.path("test"); + final FlightClient.ClientStreamListener writer = + client.startPut(descriptor, root, listener); - root.allocateNew(); - for (byte i = 0; i < 10; i++) { - final IntVector vector = (IntVector) root.getVector("a"); - final ArrowBuf metadata = allocator.buffer(1); - metadata.writeByte(i); - vector.set(0, 10); - vector.setValueCount(1); - root.setRowCount(1); - writer.putNext(metadata); - } - writer.completed(); - // Must attempt to retrieve the result to get any server-side errors. - writer.getResult(); - } - }); + root.allocateNew(); + for (byte i = 0; i < 10; i++) { + final IntVector vector = (IntVector) root.getVector("a"); + final ArrowBuf metadata = allocator.buffer(1); + metadata.writeByte(i); + vector.set(0, 10); + vector.setValueCount(1); + root.setRowCount(1); + writer.putNext(metadata); + } + writer.completed(); + // Must attempt to retrieve the result to get any server-side errors. + writer.getResult(); + } + }); } /** - * ARROW-9221: Flight copies metadata from the byte buffer of a Protobuf ByteString, - * which is in big-endian by default, thus mangling metadata. + * ARROW-9221: Flight copies metadata from the byte buffer of a Protobuf ByteString, which is in + * big-endian by default, thus mangling metadata. */ @Test public void testMetadataEndianness() throws Exception { try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - final BufferAllocator serverAllocator = allocator.newChildAllocator("flight-server", 0, Long.MAX_VALUE); - final FlightServer server = FlightServer.builder(serverAllocator, forGrpcInsecure(LOCALHOST, 0), - new EndianFlightProducer(serverAllocator)).build().start(); - final FlightClient client = FlightClient.builder(allocator, server.getLocation()).build()) { + final BufferAllocator serverAllocator = + allocator.newChildAllocator("flight-server", 0, Long.MAX_VALUE); + final FlightServer server = + FlightServer.builder( + serverAllocator, + forGrpcInsecure(LOCALHOST, 0), + new EndianFlightProducer(serverAllocator)) + .build() + .start(); + final FlightClient client = FlightClient.builder(allocator, server.getLocation()).build()) { final Schema schema = new Schema(Collections.emptyList()); final FlightDescriptor descriptor = FlightDescriptor.command(new byte[0]); try (final SyncPutListener reader = new SyncPutListener(); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { final FlightClient.ClientStreamListener writer = client.startPut(descriptor, root, reader); writer.completed(); try (final PutResult metadata = reader.read()) { @@ -230,18 +242,19 @@ public void testMetadataEndianness() throws Exception { private void test(BiConsumer fun) { try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - final FlightServer s = FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), - new MetadataFlightProducer(allocator)).build().start(); - final FlightClient client = FlightClient.builder(allocator, s.getLocation()).build()) { + final FlightServer s = + FlightServer.builder( + allocator, forGrpcInsecure(LOCALHOST, 0), new MetadataFlightProducer(allocator)) + .build() + .start(); + final FlightClient client = FlightClient.builder(allocator, s.getLocation()).build()) { fun.accept(allocator, client); } catch (Exception e) { throw new RuntimeException(e); } } - /** - * A FlightProducer that always produces a fixed data stream with metadata on the side. - */ + /** A FlightProducer that always produces a fixed data stream with metadata on the side. */ private static class MetadataFlightProducer extends NoOpFlightProducer { private final BufferAllocator allocator; @@ -252,7 +265,8 @@ public MetadataFlightProducer(BufferAllocator allocator) { @Override public void getStream(CallContext context, Ticket ticket, ServerStreamListener listener) { - final Schema schema = new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); try (VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { root.allocateNew(); listener.start(root); @@ -270,12 +284,13 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l } @Override - public Runnable acceptPut(CallContext context, FlightStream stream, StreamListener ackStream) { + public Runnable acceptPut( + CallContext context, FlightStream stream, StreamListener ackStream) { return () -> { // Wait for the descriptor to be sent stream.getRoot(); - if (stream.getDescriptor().isCommand() && - Arrays.equals(stream.getDescriptor().getCommand(), COMMAND_ARROW_6136)) { + if (stream.getDescriptor().isCommand() + && Arrays.equals(stream.getDescriptor().getCommand(), COMMAND_ARROW_6136)) { // ARROW-6136: Try closing the stream ackStream.onError( CallStatus.INTERNAL.withDescription(MESSAGE_ARROW_6136).toRuntimeException()); @@ -286,16 +301,22 @@ public Runnable acceptPut(CallContext context, FlightStream stream, StreamListen while (stream.next()) { final ArrowBuf metadata = stream.getLatestMetadata(); if (current != metadata.getByte(0)) { - ackStream.onError(CallStatus.INVALID_ARGUMENT.withDescription(String - .format("Metadata does not match expected value; got %d but expected %d.", metadata.getByte(0), - current)).toRuntimeException()); + ackStream.onError( + CallStatus.INVALID_ARGUMENT + .withDescription( + String.format( + "Metadata does not match expected value; got %d but expected %d.", + metadata.getByte(0), current)) + .toRuntimeException()); return; } ackStream.onNext(PutResult.metadata(metadata)); current++; } if (current != 10) { - throw CallStatus.INVALID_ARGUMENT.withDescription("Wrong number of messages sent.").toRuntimeException(); + throw CallStatus.INVALID_ARGUMENT + .withDescription("Wrong number of messages sent.") + .toRuntimeException(); } } catch (Exception e) { throw CallStatus.INTERNAL.withCause(e).withDescription(e.toString()).toRuntimeException(); @@ -313,7 +334,8 @@ private EndianFlightProducer(BufferAllocator allocator) { } @Override - public Runnable acceptPut(CallContext context, FlightStream flightStream, StreamListener ackStream) { + public Runnable acceptPut( + CallContext context, FlightStream flightStream, StreamListener ackStream) { return () -> { while (flightStream.next()) { // Ignore any data diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestAuth.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestAuth.java index 1a38922010a..aaec82a710c 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestAuth.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestAuth.java @@ -22,7 +22,6 @@ import java.util.Iterator; import java.util.Optional; - import org.apache.arrow.flight.auth.ClientAuthHandler; import org.apache.arrow.flight.auth.ServerAuthHandler; import org.apache.arrow.memory.BufferAllocator; @@ -35,51 +34,64 @@ public class TestAuth { /** An auth handler that does not send messages should not block the server forever. */ @Test public void noMessages() throws Exception { - Assertions.assertThrows(RuntimeException.class, () -> { - try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - final FlightServer s = FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), - new NoOpFlightProducer()).authHandler( - new OneshotAuthHandler()).build().start(); - final FlightClient client = FlightClient.builder(allocator, s.getLocation()).build()) { - client.authenticate(new ClientAuthHandler() { - @Override - public void authenticate(ClientAuthSender outgoing, Iterator incoming) { - } + Assertions.assertThrows( + RuntimeException.class, + () -> { + try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); + final FlightServer s = + FlightServer.builder( + allocator, forGrpcInsecure(LOCALHOST, 0), new NoOpFlightProducer()) + .authHandler(new OneshotAuthHandler()) + .build() + .start(); + final FlightClient client = + FlightClient.builder(allocator, s.getLocation()).build()) { + client.authenticate( + new ClientAuthHandler() { + @Override + public void authenticate(ClientAuthSender outgoing, Iterator incoming) {} - @Override - public byte[] getCallToken() { - return new byte[0]; + @Override + public byte[] getCallToken() { + return new byte[0]; + } + }); } }); - } - }); } /** An auth handler that sends an error should not block the server forever. */ @Test public void clientError() throws Exception { - Assertions.assertThrows(RuntimeException.class, () -> { - try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - final FlightServer s = FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), - new NoOpFlightProducer()).authHandler( - new OneshotAuthHandler()).build().start(); - final FlightClient client = FlightClient.builder(allocator, s.getLocation()).build()) { - client.authenticate(new ClientAuthHandler() { - @Override - public void authenticate(ClientAuthSender outgoing, Iterator incoming) { - outgoing.send(new byte[0]); - // Ensure the server-side runs - incoming.next(); - outgoing.onError(new RuntimeException("test")); - } + Assertions.assertThrows( + RuntimeException.class, + () -> { + try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); + final FlightServer s = + FlightServer.builder( + allocator, forGrpcInsecure(LOCALHOST, 0), new NoOpFlightProducer()) + .authHandler(new OneshotAuthHandler()) + .build() + .start(); + final FlightClient client = + FlightClient.builder(allocator, s.getLocation()).build()) { + client.authenticate( + new ClientAuthHandler() { + @Override + public void authenticate(ClientAuthSender outgoing, Iterator incoming) { + outgoing.send(new byte[0]); + // Ensure the server-side runs + incoming.next(); + outgoing.onError(new RuntimeException("test")); + } - @Override - public byte[] getCallToken() { - return new byte[0]; + @Override + public byte[] getCallToken() { + return new byte[0]; + } + }); } }); - } - }); } private static class OneshotAuthHandler implements ServerAuthHandler { diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestBackPressure.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestBackPressure.java index 596debcf89d..e0692330beb 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestBackPressure.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestBackPressure.java @@ -20,12 +20,12 @@ import static org.apache.arrow.flight.FlightTestUtil.LOCALHOST; import static org.apache.arrow.flight.Location.forGrpcInsecure; +import com.google.common.collect.ImmutableList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicLong; import java.util.function.Function; - import org.apache.arrow.flight.perf.PerformanceTestServer; import org.apache.arrow.flight.perf.TestPerf; import org.apache.arrow.memory.BufferAllocator; @@ -38,34 +38,29 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import com.google.common.collect.ImmutableList; - public class TestBackPressure { private static final int BATCH_SIZE = 4095; - /** - * Make sure that failing to consume one stream doesn't block other streams. - */ + /** Make sure that failing to consume one stream doesn't block other streams. */ @Disabled @Test public void ensureIndependentSteams() throws Exception { ensureIndependentSteams((b) -> (location -> new PerformanceTestServer(b, location))); } - /** - * Make sure that failing to consume one stream doesn't block other streams. - */ + /** Make sure that failing to consume one stream doesn't block other streams. */ @Disabled @Test public void ensureIndependentSteamsWithCallbacks() throws Exception { - ensureIndependentSteams((b) -> (location -> new PerformanceTestServer(b, location, - new BackpressureStrategy.CallbackBackpressureStrategy(), true))); + ensureIndependentSteams( + (b) -> + (location -> + new PerformanceTestServer( + b, location, new BackpressureStrategy.CallbackBackpressureStrategy(), true))); } - /** - * Test to make sure stream doesn't go faster than the consumer is consuming. - */ + /** Test to make sure stream doesn't go faster than the consumer is consuming. */ @Disabled @Test public void ensureWaitUntilProceed() throws Exception { @@ -73,8 +68,8 @@ public void ensureWaitUntilProceed() throws Exception { } /** - * Test to make sure stream doesn't go faster than the consumer is consuming using a callback-based - * backpressure strategy. + * Test to make sure stream doesn't go faster than the consumer is consuming using a + * callback-based backpressure strategy. */ @Disabled @Test @@ -82,25 +77,31 @@ public void ensureWaitUntilProceedWithCallbacks() throws Exception { ensureWaitUntilProceed(new RecordingCallbackBackpressureStrategy(), true); } - /** - * Make sure that failing to consume one stream doesn't block other streams. - */ - private static void ensureIndependentSteams(Function> - serverConstructor) throws Exception { - try ( - final BufferAllocator a = new RootAllocator(Long.MAX_VALUE); - final PerformanceTestServer server = serverConstructor.apply(a).apply(forGrpcInsecure(LOCALHOST, 0)).start(); - final FlightClient client = FlightClient.builder(a, server.getLocation()).build() - ) { - try (FlightStream fs1 = client.getStream(client.getInfo( - TestPerf.getPerfFlightDescriptor(110L * BATCH_SIZE, BATCH_SIZE, 1)) - .getEndpoints().get(0).getTicket())) { + /** Make sure that failing to consume one stream doesn't block other streams. */ + private static void ensureIndependentSteams( + Function> serverConstructor) + throws Exception { + try (final BufferAllocator a = new RootAllocator(Long.MAX_VALUE); + final PerformanceTestServer server = + serverConstructor.apply(a).apply(forGrpcInsecure(LOCALHOST, 0)).start(); + final FlightClient client = FlightClient.builder(a, server.getLocation()).build()) { + try (FlightStream fs1 = + client.getStream( + client + .getInfo(TestPerf.getPerfFlightDescriptor(110L * BATCH_SIZE, BATCH_SIZE, 1)) + .getEndpoints() + .get(0) + .getTicket())) { consume(fs1, 10); // stop consuming fs1 but make sure we can consume a large amount of fs2. - try (FlightStream fs2 = client.getStream(client.getInfo( - TestPerf.getPerfFlightDescriptor(200L * BATCH_SIZE, BATCH_SIZE, 1)) - .getEndpoints().get(0).getTicket())) { + try (FlightStream fs2 = + client.getStream( + client + .getInfo(TestPerf.getPerfFlightDescriptor(200L * BATCH_SIZE, BATCH_SIZE, 1)) + .getEndpoints() + .get(0) + .getTicket())) { consume(fs2, 100); consume(fs1, 100); @@ -113,65 +114,67 @@ private static void ensureIndependentSteams(Function { - int batches = 0; - final Schema pojoSchema = new Schema(ImmutableList.of(Field.nullable("a", MinorType.BIGINT.getType()))); - try (VectorSchemaRoot root = VectorSchemaRoot.create(pojoSchema, allocator)) { - listener.start(root); - while (true) { - bpStrategy.waitForListener(0); - if (batches > 100) { - root.clear(); - listener.completed(); - return; - } - - root.allocateNew(); - root.setRowCount(4095); - listener.putNext(); - batches++; + final FlightProducer producer = + new NoOpFlightProducer() { + + @Override + public void getStream( + CallContext context, Ticket ticket, ServerStreamListener listener) { + bpStrategy.register(listener); + final Runnable loadData = + () -> { + int batches = 0; + final Schema pojoSchema = + new Schema( + ImmutableList.of(Field.nullable("a", MinorType.BIGINT.getType()))); + try (VectorSchemaRoot root = VectorSchemaRoot.create(pojoSchema, allocator)) { + listener.start(root); + while (true) { + bpStrategy.waitForListener(0); + if (batches > 100) { + root.clear(); + listener.completed(); + return; + } + + root.allocateNew(); + root.setRowCount(4095); + listener.putNext(); + batches++; + } + } + }; + + if (!isNonBlocking) { + loadData.run(); + } else { + final ExecutorService service = Executors.newSingleThreadExecutor(); + Future unused = service.submit(loadData); + service.shutdown(); } } }; - if (!isNonBlocking) { - loadData.run(); - } else { - final ExecutorService service = Executors.newSingleThreadExecutor(); - Future unused = service.submit(loadData); - service.shutdown(); - } - } - }; - - - try ( - BufferAllocator serverAllocator = allocator.newChildAllocator("server", 0, Long.MAX_VALUE); - FlightServer server = FlightServer.builder(serverAllocator, forGrpcInsecure(LOCALHOST, 0), producer) - .build().start(); - BufferAllocator clientAllocator = allocator.newChildAllocator("client", 0, Long.MAX_VALUE); + try (BufferAllocator serverAllocator = + allocator.newChildAllocator("server", 0, Long.MAX_VALUE); + FlightServer server = + FlightServer.builder(serverAllocator, forGrpcInsecure(LOCALHOST, 0), producer) + .build() + .start(); + BufferAllocator clientAllocator = + allocator.newChildAllocator("client", 0, Long.MAX_VALUE); FlightClient client = - FlightClient - .builder(clientAllocator, server.getLocation()) - .build(); - FlightStream stream = client.getStream(new Ticket(new byte[1])) - ) { + FlightClient.builder(clientAllocator, server.getLocation()).build(); + FlightStream stream = client.getStream(new Ticket(new byte[1]))) { VectorSchemaRoot root = stream.getRoot(); root.clear(); Thread.sleep(wait); @@ -183,11 +186,7 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l bpStrategy.getSleepTime() > expected, String.format( "Expected a sleep of at least %dms but only slept for %d", - expected, - bpStrategy.getSleepTime() - ) - ); - + expected, bpStrategy.getSleepTime())); } } } @@ -210,15 +209,18 @@ private static void consume(FlightStream stream, int batches) { private interface SleepTimeRecordingBackpressureStrategy extends BackpressureStrategy { /** * Returns the total time spent waiting on the listener to be ready. + * * @return the total time spent waiting on the listener to be ready. */ long getSleepTime(); } /** - * Implementation of a backpressure strategy that polls on isReady and records amount of time spent in Thread.sleep(). + * Implementation of a backpressure strategy that polls on isReady and records amount of time + * spent in Thread.sleep(). */ - private static class PollingBackpressureStrategy implements SleepTimeRecordingBackpressureStrategy { + private static class PollingBackpressureStrategy + implements SleepTimeRecordingBackpressureStrategy { private final AtomicLong sleepTime = new AtomicLong(0); private FlightProducer.ServerStreamListener listener; @@ -247,10 +249,11 @@ public WaitResult waitForListener(long timeout) { } /** - * Implementation of a backpressure strategy that uses callbacks to detect changes in client readiness state - * and records spent time waiting. + * Implementation of a backpressure strategy that uses callbacks to detect changes in client + * readiness state and records spent time waiting. */ - private static class RecordingCallbackBackpressureStrategy extends BackpressureStrategy.CallbackBackpressureStrategy + private static class RecordingCallbackBackpressureStrategy + extends BackpressureStrategy.CallbackBackpressureStrategy implements SleepTimeRecordingBackpressureStrategy { private final AtomicLong sleepTime = new AtomicLong(0); diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestBasicOperation.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestBasicOperation.java index ae520ee9b99..16c55f6804e 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestBasicOperation.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestBasicOperation.java @@ -17,10 +17,12 @@ package org.apache.arrow.flight; - import static org.apache.arrow.flight.FlightTestUtil.LOCALHOST; import static org.apache.arrow.flight.Location.forGrpcInsecure; +import com.google.common.base.Charsets; +import com.google.protobuf.ByteString; +import io.grpc.MethodDescriptor; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -39,7 +41,6 @@ import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Consumer; - import org.apache.arrow.flight.FlightClient.ClientStreamListener; import org.apache.arrow.flight.impl.Flight; import org.apache.arrow.flight.impl.Flight.FlightDescriptor.DescriptorType; @@ -63,14 +64,7 @@ import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.OS; -import com.google.common.base.Charsets; -import com.google.protobuf.ByteString; - -import io.grpc.MethodDescriptor; - -/** - * Test the operations of a basic flight service. - */ +/** Test the operations of a basic flight service. */ public class TestBasicOperation { @Test @@ -79,9 +73,7 @@ public void fastPathDefaults() { Assertions.assertFalse(ArrowMessage.ENABLE_ZERO_COPY_WRITE); } - /** - * ARROW-6017: we should be able to construct locations for unknown schemes. - */ + /** ARROW-6017: we should be able to construct locations for unknown schemes. */ @Test public void unknownScheme() throws URISyntaxException { final Location location = new Location("s3://unknown"); @@ -90,19 +82,22 @@ public void unknownScheme() throws URISyntaxException { @Test public void unknownSchemeRemote() throws Exception { - test(c -> { - try { - final FlightInfo info = c.getInfo(FlightDescriptor.path("test")); - Assertions.assertEquals(new URI("https://example.com"), info.getEndpoints().get(0).getLocations().get(0).getUri()); - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } - }); + test( + c -> { + try { + final FlightInfo info = c.getInfo(FlightDescriptor.path("test")); + Assertions.assertEquals( + new URI("https://example.com"), + info.getEndpoints().get(0).getLocations().get(0).getUri()); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + }); } @Test public void roundTripTicket() throws Exception { - final Ticket ticket = new Ticket(new byte[]{0, 1, 2, 3, 4, 5}); + final Ticket ticket = new Ticket(new byte[] {0, 1, 2, 3, 4, 5}); Assertions.assertEquals(ticket, Ticket.deserialize(ticket.serialize())); } @@ -110,31 +105,55 @@ public void roundTripTicket() throws Exception { public void roundTripInfo() throws Exception { final Map metadata = new HashMap<>(); metadata.put("foo", "bar"); - final Schema schema = new Schema(Arrays.asList( - Field.nullable("a", new ArrowType.Int(32, true)), - Field.nullable("b", new ArrowType.FixedSizeBinary(32)) - ), metadata); - final FlightInfo info1 = FlightInfo.builder(schema, FlightDescriptor.path(), Collections.emptyList()) - .setAppMetadata("foo".getBytes(StandardCharsets.UTF_8)).build(); - final FlightInfo info2 = new FlightInfo(schema, FlightDescriptor.command(new byte[2]), - Collections.singletonList( - FlightEndpoint.builder(new Ticket(new byte[10]), Location.forGrpcDomainSocket("/tmp/test.sock")) - .setAppMetadata("bar".getBytes(StandardCharsets.UTF_8)).build() - ), 200, 500); - final FlightInfo info3 = new FlightInfo(schema, FlightDescriptor.path("a", "b"), - Arrays.asList(new FlightEndpoint( - new Ticket(new byte[10]), Location.forGrpcDomainSocket("/tmp/test.sock")), - new FlightEndpoint( - new Ticket(new byte[10]), Location.forGrpcDomainSocket("/tmp/test.sock"), - forGrpcInsecure("localhost", 50051)) - ), 200, 500); - final FlightInfo info4 = new FlightInfo(schema, FlightDescriptor.path("a", "b"), - Arrays.asList(new FlightEndpoint( - new Ticket(new byte[10]), Location.forGrpcDomainSocket("/tmp/test.sock")), - new FlightEndpoint( - new Ticket(new byte[10]), Location.forGrpcDomainSocket("/tmp/test.sock"), - forGrpcInsecure("localhost", 50051)) - ), 200, 500, /*ordered*/ true, IpcOption.DEFAULT); + final Schema schema = + new Schema( + Arrays.asList( + Field.nullable("a", new ArrowType.Int(32, true)), + Field.nullable("b", new ArrowType.FixedSizeBinary(32))), + metadata); + final FlightInfo info1 = + FlightInfo.builder(schema, FlightDescriptor.path(), Collections.emptyList()) + .setAppMetadata("foo".getBytes(StandardCharsets.UTF_8)) + .build(); + final FlightInfo info2 = + new FlightInfo( + schema, + FlightDescriptor.command(new byte[2]), + Collections.singletonList( + FlightEndpoint.builder( + new Ticket(new byte[10]), Location.forGrpcDomainSocket("/tmp/test.sock")) + .setAppMetadata("bar".getBytes(StandardCharsets.UTF_8)) + .build()), + 200, + 500); + final FlightInfo info3 = + new FlightInfo( + schema, + FlightDescriptor.path("a", "b"), + Arrays.asList( + new FlightEndpoint( + new Ticket(new byte[10]), Location.forGrpcDomainSocket("/tmp/test.sock")), + new FlightEndpoint( + new Ticket(new byte[10]), + Location.forGrpcDomainSocket("/tmp/test.sock"), + forGrpcInsecure("localhost", 50051))), + 200, + 500); + final FlightInfo info4 = + new FlightInfo( + schema, + FlightDescriptor.path("a", "b"), + Arrays.asList( + new FlightEndpoint( + new Ticket(new byte[10]), Location.forGrpcDomainSocket("/tmp/test.sock")), + new FlightEndpoint( + new Ticket(new byte[10]), + Location.forGrpcDomainSocket("/tmp/test.sock"), + forGrpcInsecure("localhost", 50051))), + 200, + 500, /*ordered*/ + true, + IpcOption.DEFAULT); Assertions.assertEquals(info1, FlightInfo.deserialize(info1.serialize())); Assertions.assertEquals(info2, FlightInfo.deserialize(info2.serialize())); @@ -151,7 +170,8 @@ public void roundTripInfo() throws Exception { @Test public void roundTripDescriptor() throws Exception { - final FlightDescriptor cmd = FlightDescriptor.command("test command".getBytes(StandardCharsets.UTF_8)); + final FlightDescriptor cmd = + FlightDescriptor.command("test command".getBytes(StandardCharsets.UTF_8)); Assertions.assertEquals(cmd, FlightDescriptor.deserialize(cmd.serialize())); final FlightDescriptor path = FlightDescriptor.path("foo", "bar", "test.arrow"); Assertions.assertEquals(path, FlightDescriptor.deserialize(path.serialize())); @@ -159,156 +179,172 @@ public void roundTripDescriptor() throws Exception { @Test public void getDescriptors() throws Exception { - test(c -> { - int count = 0; - for (FlightInfo unused : c.listFlights(Criteria.ALL)) { - count += 1; - } - Assertions.assertEquals(1, count); - }); + test( + c -> { + int count = 0; + for (FlightInfo unused : c.listFlights(Criteria.ALL)) { + count += 1; + } + Assertions.assertEquals(1, count); + }); } @Test public void getDescriptorsWithCriteria() throws Exception { - test(c -> { - int count = 0; - for (FlightInfo unused : c.listFlights(new Criteria(new byte[]{1}))) { + test( + c -> { + int count = 0; + for (FlightInfo unused : c.listFlights(new Criteria(new byte[] {1}))) { - count += 1; - } - Assertions.assertEquals(0, count); - }); + count += 1; + } + Assertions.assertEquals(0, count); + }); } @Test public void getDescriptor() throws Exception { - test(c -> { - System.out.println(c.getInfo(FlightDescriptor.path("hello")).getDescriptor()); - }); + test( + c -> { + System.out.println(c.getInfo(FlightDescriptor.path("hello")).getDescriptor()); + }); } @Test public void getSchema() throws Exception { - test(c -> { - System.out.println(c.getSchema(FlightDescriptor.path("hello")).getSchema()); - }); + test( + c -> { + System.out.println(c.getSchema(FlightDescriptor.path("hello")).getSchema()); + }); } - @Test public void listActions() throws Exception { - test(c -> { - for (ActionType at : c.listActions()) { - System.out.println(at.getType()); - } - }); + test( + c -> { + for (ActionType at : c.listActions()) { + System.out.println(at.getType()); + } + }); } @Test public void doAction() throws Exception { - test(c -> { - Iterator stream = c.doAction(new Action("hello")); - - Assertions.assertTrue(stream.hasNext()); - Result r = stream.next(); - Assertions.assertArrayEquals("world".getBytes(Charsets.UTF_8), r.getBody()); - }); - test(c -> { - Iterator stream = c.doAction(new Action("hellooo")); - - Assertions.assertTrue(stream.hasNext()); - Result r = stream.next(); - Assertions.assertArrayEquals("world".getBytes(Charsets.UTF_8), r.getBody()); - - Assertions.assertTrue(stream.hasNext()); - r = stream.next(); - Assertions.assertArrayEquals("!".getBytes(Charsets.UTF_8), r.getBody()); - Assertions.assertFalse(stream.hasNext()); - }); + test( + c -> { + Iterator stream = c.doAction(new Action("hello")); + + Assertions.assertTrue(stream.hasNext()); + Result r = stream.next(); + Assertions.assertArrayEquals("world".getBytes(Charsets.UTF_8), r.getBody()); + }); + test( + c -> { + Iterator stream = c.doAction(new Action("hellooo")); + + Assertions.assertTrue(stream.hasNext()); + Result r = stream.next(); + Assertions.assertArrayEquals("world".getBytes(Charsets.UTF_8), r.getBody()); + + Assertions.assertTrue(stream.hasNext()); + r = stream.next(); + Assertions.assertArrayEquals("!".getBytes(Charsets.UTF_8), r.getBody()); + Assertions.assertFalse(stream.hasNext()); + }); } @Test public void putStream() throws Exception { - test((c, a) -> { - final int size = 10; - - IntVector iv = new IntVector("c1", a); - - try (VectorSchemaRoot root = VectorSchemaRoot.of(iv)) { - ClientStreamListener listener = c - .startPut(FlightDescriptor.path("hello"), root, new AsyncPutListener()); - - //batch 1 - root.allocateNew(); - for (int i = 0; i < size; i++) { - iv.set(i, i); - } - iv.setValueCount(size); - root.setRowCount(size); - listener.putNext(); - - // batch 2 - - root.allocateNew(); - for (int i = 0; i < size; i++) { - iv.set(i, i + size); - } - iv.setValueCount(size); - root.setRowCount(size); - listener.putNext(); - root.clear(); - listener.completed(); - - // wait for ack to avoid memory leaks. - listener.getResult(); - } - }); + test( + (c, a) -> { + final int size = 10; + + IntVector iv = new IntVector("c1", a); + + try (VectorSchemaRoot root = VectorSchemaRoot.of(iv)) { + ClientStreamListener listener = + c.startPut(FlightDescriptor.path("hello"), root, new AsyncPutListener()); + + // batch 1 + root.allocateNew(); + for (int i = 0; i < size; i++) { + iv.set(i, i); + } + iv.setValueCount(size); + root.setRowCount(size); + listener.putNext(); + + // batch 2 + + root.allocateNew(); + for (int i = 0; i < size; i++) { + iv.set(i, i + size); + } + iv.setValueCount(size); + root.setRowCount(size); + listener.putNext(); + root.clear(); + listener.completed(); + + // wait for ack to avoid memory leaks. + listener.getResult(); + } + }); } @Test public void propagateErrors() throws Exception { - test(client -> { - FlightTestUtil.assertCode(FlightStatusCode.UNIMPLEMENTED, () -> { - client.doAction(new Action("invalid-action")).forEachRemaining(action -> Assertions.fail()); - }); - }); + test( + client -> { + FlightTestUtil.assertCode( + FlightStatusCode.UNIMPLEMENTED, + () -> { + client + .doAction(new Action("invalid-action")) + .forEachRemaining(action -> Assertions.fail()); + }); + }); } @Test public void getStream() throws Exception { - test(c -> { - try (final FlightStream stream = c.getStream(new Ticket(new byte[0]))) { - VectorSchemaRoot root = stream.getRoot(); - IntVector iv = (IntVector) root.getVector("c1"); - int value = 0; - while (stream.next()) { - for (int i = 0; i < root.getRowCount(); i++) { - Assertions.assertEquals(value, iv.get(i)); - value++; + test( + c -> { + try (final FlightStream stream = c.getStream(new Ticket(new byte[0]))) { + VectorSchemaRoot root = stream.getRoot(); + IntVector iv = (IntVector) root.getVector("c1"); + int value = 0; + while (stream.next()) { + for (int i = 0; i < root.getRowCount(); i++) { + Assertions.assertEquals(value, iv.get(i)); + value++; + } + } + } catch (Exception e) { + throw new RuntimeException(e); } - } - } catch (Exception e) { - throw new RuntimeException(e); - } - }); + }); } /** Ensure the client is configured to accept large messages. */ @Test - @DisabledOnOs(value = {OS.WINDOWS}, disabledReason = "https://github.com/apache/arrow/issues/33237: flaky test") + @DisabledOnOs( + value = {OS.WINDOWS}, + disabledReason = "https://github.com/apache/arrow/issues/33237: flaky test") public void getStreamLargeBatch() throws Exception { - test(c -> { - try (final FlightStream stream = c.getStream(new Ticket(Producer.TICKET_LARGE_BATCH))) { - Assertions.assertEquals(128, stream.getRoot().getFieldVectors().size()); - Assertions.assertTrue(stream.next()); - Assertions.assertEquals(65536, stream.getRoot().getRowCount()); - Assertions.assertTrue(stream.next()); - Assertions.assertEquals(65536, stream.getRoot().getRowCount()); - Assertions.assertFalse(stream.next()); - } catch (Exception e) { - throw new RuntimeException(e); - } - }); + test( + c -> { + try (final FlightStream stream = c.getStream(new Ticket(Producer.TICKET_LARGE_BATCH))) { + Assertions.assertEquals(128, stream.getRoot().getFieldVectors().size()); + Assertions.assertTrue(stream.next()); + Assertions.assertEquals(65536, stream.getRoot().getRowCount()); + Assertions.assertTrue(stream.next()); + Assertions.assertEquals(65536, stream.getRoot().getRowCount()); + Assertions.assertFalse(stream.next()); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); } /** Ensure the server is configured to accept large messages. */ @@ -323,36 +359,37 @@ public void startPutLargeBatch() throws Exception { } vectors.add(vector); } - test(c -> { - try (final VectorSchemaRoot root = new VectorSchemaRoot(vectors)) { - root.setRowCount(65536); - final ClientStreamListener stream = c.startPut(FlightDescriptor.path(""), root, new SyncPutListener()); - stream.putNext(); - stream.putNext(); - stream.completed(); - stream.getResult(); - } catch (Exception e) { - throw new RuntimeException(e); - } - }); + test( + c -> { + try (final VectorSchemaRoot root = new VectorSchemaRoot(vectors)) { + root.setRowCount(65536); + final ClientStreamListener stream = + c.startPut(FlightDescriptor.path(""), root, new SyncPutListener()); + stream.putNext(); + stream.putNext(); + stream.completed(); + stream.getResult(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); } } private void test(Consumer consumer) throws Exception { - test((c, a) -> { - consumer.accept(c); - }); + test( + (c, a) -> { + consumer.accept(c); + }); } private void test(BiConsumer consumer) throws Exception { - try ( - BufferAllocator a = new RootAllocator(Long.MAX_VALUE); + try (BufferAllocator a = new RootAllocator(Long.MAX_VALUE); Producer producer = new Producer(a); - FlightServer s = FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), producer).build().start()) { + FlightServer s = + FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), producer).build().start()) { - try ( - FlightClient c = FlightClient.builder(a, s.getLocation()).build() - ) { + try (FlightClient c = FlightClient.builder(a, s.getLocation()).build()) { try (BufferAllocator testAllocator = a.newChildAllocator("testcase", 0, Long.MAX_VALUE)) { consumer.accept(c, testAllocator); } @@ -362,7 +399,8 @@ private void test(BiConsumer consumer) throws Exc /** Helper method to convert an ArrowMessage into a Protobuf message. */ private Flight.FlightData arrowMessageToProtobuf( - MethodDescriptor.Marshaller marshaller, ArrowMessage message) throws IOException { + MethodDescriptor.Marshaller marshaller, ArrowMessage message) + throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final InputStream serialized = marshaller.stream(message)) { final byte[] buf = new byte[1024]; @@ -378,35 +416,44 @@ private Flight.FlightData arrowMessageToProtobuf( return Flight.FlightData.parseFrom(serializedMessage); } - /** ARROW-10962: accept FlightData messages generated by Protobuf (which can omit empty fields). */ + /** + * ARROW-10962: accept FlightData messages generated by Protobuf (which can omit empty fields). + */ @Test public void testProtobufRecordBatchCompatibility() throws Exception { - final Schema schema = new Schema(Collections.singletonList(Field.nullable("foo", new ArrowType.Int(32, true)))); + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("foo", new ArrowType.Int(32, true)))); try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { final VectorUnloader unloader = new VectorUnloader(root); root.setRowCount(0); - final MethodDescriptor.Marshaller marshaller = ArrowMessage.createMarshaller(allocator); - try (final ArrowMessage message = new ArrowMessage( - unloader.getRecordBatch(), /* appMetadata */ null, /* tryZeroCopy */ false, IpcOption.DEFAULT)) { + final MethodDescriptor.Marshaller marshaller = + ArrowMessage.createMarshaller(allocator); + try (final ArrowMessage message = + new ArrowMessage( + unloader.getRecordBatch(), /* appMetadata */ + null, /* tryZeroCopy */ + false, + IpcOption.DEFAULT)) { Assertions.assertEquals(ArrowMessage.HeaderType.RECORD_BATCH, message.getMessageType()); - // Should have at least one empty body buffer (there may be multiple for e.g. data and validity) + // Should have at least one empty body buffer (there may be multiple for e.g. data and + // validity) Iterator iterator = message.getBufs().iterator(); Assertions.assertTrue(iterator.hasNext()); while (iterator.hasNext()) { Assertions.assertEquals(0, iterator.next().capacity()); } - final Flight.FlightData protobufData = arrowMessageToProtobuf(marshaller, message) - .toBuilder() - .clearDataBody() - .build(); + final Flight.FlightData protobufData = + arrowMessageToProtobuf(marshaller, message).toBuilder().clearDataBody().build(); Assertions.assertEquals(0, protobufData.getDataBody().size()); - ArrowMessage parsedMessage = marshaller.parse(new ByteArrayInputStream(protobufData.toByteArray())); + ArrowMessage parsedMessage = + marshaller.parse(new ByteArrayInputStream(protobufData.toByteArray())); // Should have an empty body buffer Iterator parsedIterator = parsedMessage.getBufs().iterator(); Assertions.assertTrue(parsedIterator.hasNext()); Assertions.assertEquals(0, parsedIterator.next().capacity()); - // Should have only one (the parser synthesizes exactly one); in the case of empty buffers, this is equivalent + // Should have only one (the parser synthesizes exactly one); in the case of empty buffers, + // this is equivalent Assertions.assertFalse(parsedIterator.hasNext()); // Should not throw final ArrowRecordBatch rb = parsedMessage.asRecordBatch(); @@ -415,23 +462,28 @@ public void testProtobufRecordBatchCompatibility() throws Exception { } } - /** ARROW-10962: accept FlightData messages generated by Protobuf (which can omit empty fields). */ + /** + * ARROW-10962: accept FlightData messages generated by Protobuf (which can omit empty fields). + */ @Test public void testProtobufSchemaCompatibility() throws Exception { - final Schema schema = new Schema(Collections.singletonList(Field.nullable("foo", new ArrowType.Int(32, true)))); + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("foo", new ArrowType.Int(32, true)))); try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE)) { - final MethodDescriptor.Marshaller marshaller = ArrowMessage.createMarshaller(allocator); + final MethodDescriptor.Marshaller marshaller = + ArrowMessage.createMarshaller(allocator); Flight.FlightDescriptor descriptor = FlightDescriptor.command(new byte[0]).toProtocol(); try (final ArrowMessage message = new ArrowMessage(descriptor, schema, IpcOption.DEFAULT)) { Assertions.assertEquals(ArrowMessage.HeaderType.SCHEMA, message.getMessageType()); // Should have no body buffers Assertions.assertFalse(message.getBufs().iterator().hasNext()); - final Flight.FlightData protobufData = arrowMessageToProtobuf(marshaller, message) - .toBuilder() - .setDataBody(ByteString.EMPTY) - .build(); + final Flight.FlightData protobufData = + arrowMessageToProtobuf(marshaller, message).toBuilder() + .setDataBody(ByteString.EMPTY) + .build(); Assertions.assertEquals(0, protobufData.getDataBody().size()); - final ArrowMessage parsedMessage = marshaller.parse(new ByteArrayInputStream(protobufData.toByteArray())); + final ArrowMessage parsedMessage = + marshaller.parse(new ByteArrayInputStream(protobufData.toByteArray())); // Should have no body buffers Assertions.assertFalse(parsedMessage.getBufs().iterator().hasNext()); // Should not throw @@ -453,14 +505,12 @@ public void testGrpcInsecureLocation() throws Exception { public void testGrpcTlsLocation() throws Exception { Location location = Location.forGrpcTls(LOCALHOST, 9000); Assertions.assertEquals( - new URI(LocationSchemes.GRPC_TLS, null, LOCALHOST, 9000, null, null, null), - location.getUri()); + new URI(LocationSchemes.GRPC_TLS, null, LOCALHOST, 9000, null, null, null), + location.getUri()); Assertions.assertEquals(new InetSocketAddress(LOCALHOST, 9000), location.toSocketAddress()); } - /** - * An example FlightProducer for test purposes. - */ + /** An example FlightProducer for test purposes. */ public static class Producer implements FlightProducer, AutoCloseable { static final byte[] TICKET_LARGE_BATCH = "large-batch".getBytes(StandardCharsets.UTF_8); @@ -472,18 +522,20 @@ public Producer(BufferAllocator allocator) { } @Override - public void listFlights(CallContext context, Criteria criteria, - StreamListener listener) { + public void listFlights( + CallContext context, Criteria criteria, StreamListener listener) { if (criteria.getExpression().length > 0) { // Don't send anything if criteria are set listener.onCompleted(); } - Flight.FlightInfo getInfo = Flight.FlightInfo.newBuilder() - .setFlightDescriptor(Flight.FlightDescriptor.newBuilder() - .setType(DescriptorType.CMD) - .setCmd(ByteString.copyFrom("cool thing", Charsets.UTF_8))) - .build(); + Flight.FlightInfo getInfo = + Flight.FlightInfo.newBuilder() + .setFlightDescriptor( + Flight.FlightDescriptor.newBuilder() + .setType(DescriptorType.CMD) + .setCmd(ByteString.copyFrom("cool thing", Charsets.UTF_8))) + .build(); try { listener.onNext(new FlightInfo(getInfo)); } catch (URISyntaxException e) { @@ -494,7 +546,8 @@ public void listFlights(CallContext context, Criteria criteria, } @Override - public Runnable acceptPut(CallContext context, FlightStream flightStream, StreamListener ackStream) { + public Runnable acceptPut( + CallContext context, FlightStream flightStream, StreamListener ackStream) { return () -> { while (flightStream.next()) { // Drain the stream @@ -514,7 +567,7 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l VectorSchemaRoot root = VectorSchemaRoot.of(iv); listener.start(root); - //batch 1 + // batch 1 root.allocateNew(); for (int i = 0; i < size; i++) { iv.set(i, i); @@ -560,27 +613,29 @@ public void close() throws Exception { } @Override - public FlightInfo getFlightInfo(CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfo(CallContext context, FlightDescriptor descriptor) { try { - Flight.FlightInfo getInfo = Flight.FlightInfo.newBuilder() - .setSchema(schemaToByteString(new Schema(Collections.emptyList()))) - .setFlightDescriptor(Flight.FlightDescriptor.newBuilder() - .setType(DescriptorType.CMD) - .setCmd(ByteString.copyFrom("cool thing", Charsets.UTF_8))) - .addEndpoint( - Flight.FlightEndpoint.newBuilder().addLocation(new Location("https://example.com").toProtocol())) - .build(); + Flight.FlightInfo getInfo = + Flight.FlightInfo.newBuilder() + .setSchema(schemaToByteString(new Schema(Collections.emptyList()))) + .setFlightDescriptor( + Flight.FlightDescriptor.newBuilder() + .setType(DescriptorType.CMD) + .setCmd(ByteString.copyFrom("cool thing", Charsets.UTF_8))) + .addEndpoint( + Flight.FlightEndpoint.newBuilder() + .addLocation(new Location("https://example.com").toProtocol())) + .build(); return new FlightInfo(getInfo); } catch (URISyntaxException e) { throw new RuntimeException(e); } } - private static ByteString schemaToByteString(Schema schema) - { + private static ByteString schemaToByteString(Schema schema) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { - MessageSerializer.serialize(new WriteChannel(Channels.newChannel(baos)), schema, IpcOption.DEFAULT); + MessageSerializer.serialize( + new WriteChannel(Channels.newChannel(baos)), schema, IpcOption.DEFAULT); return ByteString.copyFrom(baos.toByteArray()); } catch (IOException e) { throw new RuntimeException(e); @@ -588,36 +643,35 @@ private static ByteString schemaToByteString(Schema schema) } @Override - public void doAction(CallContext context, Action action, - StreamListener listener) { + public void doAction(CallContext context, Action action, StreamListener listener) { switch (action.getType()) { - case "hello": { - listener.onNext(new Result("world".getBytes(Charsets.UTF_8))); - listener.onCompleted(); - break; - } - case "hellooo": { - listener.onNext(new Result("world".getBytes(Charsets.UTF_8))); - listener.onNext(new Result("!".getBytes(Charsets.UTF_8))); - listener.onCompleted(); - break; - } + case "hello": + { + listener.onNext(new Result("world".getBytes(Charsets.UTF_8))); + listener.onCompleted(); + break; + } + case "hellooo": + { + listener.onNext(new Result("world".getBytes(Charsets.UTF_8))); + listener.onNext(new Result("!".getBytes(Charsets.UTF_8))); + listener.onCompleted(); + break; + } default: - listener.onError(CallStatus.UNIMPLEMENTED.withDescription("Action not implemented: " + action.getType()) - .toRuntimeException()); + listener.onError( + CallStatus.UNIMPLEMENTED + .withDescription("Action not implemented: " + action.getType()) + .toRuntimeException()); } } @Override - public void listActions(CallContext context, - StreamListener listener) { + public void listActions(CallContext context, StreamListener listener) { listener.onNext(new ActionType("get", "")); listener.onNext(new ActionType("put", "")); listener.onNext(new ActionType("hello", "")); listener.onCompleted(); } - } - - } diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestCallOptions.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestCallOptions.java index 41df36c8633..628cacb00cc 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestCallOptions.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestCallOptions.java @@ -20,6 +20,7 @@ import static org.apache.arrow.flight.FlightTestUtil.LOCALHOST; import static org.apache.arrow.flight.Location.forGrpcInsecure; +import io.grpc.Metadata; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.time.Duration; @@ -27,47 +28,52 @@ import java.util.Iterator; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import io.grpc.Metadata; - public class TestCallOptions { @Test @Disabled public void timeoutFires() { // Ignored due to CI flakiness - test((client) -> { - Instant start = Instant.now(); - Iterator results = client.doAction(new Action("hang"), CallOptions.timeout(1, TimeUnit.SECONDS)); - try { - results.next(); - Assertions.fail("Call should have failed"); - } catch (RuntimeException e) { - Assertions.assertTrue(e.getMessage().contains("deadline exceeded"), e.getMessage()); - } - Instant end = Instant.now(); - Assertions.assertTrue(Duration.between(start, end).toMillis() < 1500, "Call took over 1500 ms despite timeout"); - }); + test( + (client) -> { + Instant start = Instant.now(); + Iterator results = + client.doAction(new Action("hang"), CallOptions.timeout(1, TimeUnit.SECONDS)); + try { + results.next(); + Assertions.fail("Call should have failed"); + } catch (RuntimeException e) { + Assertions.assertTrue(e.getMessage().contains("deadline exceeded"), e.getMessage()); + } + Instant end = Instant.now(); + Assertions.assertTrue( + Duration.between(start, end).toMillis() < 1500, + "Call took over 1500 ms despite timeout"); + }); } @Test @Disabled public void underTimeout() { // Ignored due to CI flakiness - test((client) -> { - Instant start = Instant.now(); - // This shouldn't fail and it should complete within the timeout - Iterator results = client.doAction(new Action("fast"), CallOptions.timeout(2, TimeUnit.SECONDS)); - Assertions.assertArrayEquals(new byte[]{42, 42}, results.next().getBody()); - Instant end = Instant.now(); - Assertions.assertTrue(Duration.between(start, end).toMillis() < 2500, "Call took over 2500 ms despite timeout"); - }); + test( + (client) -> { + Instant start = Instant.now(); + // This shouldn't fail and it should complete within the timeout + Iterator results = + client.doAction(new Action("fast"), CallOptions.timeout(2, TimeUnit.SECONDS)); + Assertions.assertArrayEquals(new byte[] {42, 42}, results.next().getBody()); + Instant end = Instant.now(); + Assertions.assertTrue( + Duration.between(start, end).toMillis() < 2500, + "Call took over 2500 ms despite timeout"); + }); } @Test @@ -102,12 +108,13 @@ public void mixedProperties() { } private void testHeaders(CallHeaders headers) { - try ( - BufferAllocator a = new RootAllocator(Long.MAX_VALUE); + try (BufferAllocator a = new RootAllocator(Long.MAX_VALUE); HeaderProducer producer = new HeaderProducer(); - FlightServer s = FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), producer).build().start(); + FlightServer s = + FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), producer).build().start(); FlightClient client = FlightClient.builder(a, s.getLocation()).build()) { - Assertions.assertFalse(client.doAction(new Action(""), new HeaderCallOption(headers)).hasNext()); + Assertions.assertFalse( + client.doAction(new Action(""), new HeaderCallOption(headers)).hasNext()); final CallHeaders incomingHeaders = producer.headers(); for (String key : headers.keys()) { if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) { @@ -122,10 +129,10 @@ private void testHeaders(CallHeaders headers) { } void test(Consumer testFn) { - try ( - BufferAllocator a = new RootAllocator(Long.MAX_VALUE); + try (BufferAllocator a = new RootAllocator(Long.MAX_VALUE); Producer producer = new Producer(); - FlightServer s = FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), producer).build().start(); + FlightServer s = + FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), producer).build().start(); FlightClient client = FlightClient.builder(a, s.getLocation()).build()) { testFn.accept(client); } catch (InterruptedException | IOException e) { @@ -137,8 +144,7 @@ static class HeaderProducer extends NoOpFlightProducer implements AutoCloseable CallHeaders headers; @Override - public void close() { - } + public void close() {} public CallHeaders headers() { return headers; @@ -153,39 +159,40 @@ public void doAction(CallContext context, Action action, StreamListener static class Producer extends NoOpFlightProducer implements AutoCloseable { - Producer() { - } + Producer() {} @Override - public void close() { - } + public void close() {} @Override public void doAction(CallContext context, Action action, StreamListener listener) { switch (action.getType()) { - case "hang": { - try { - Thread.sleep(25000); - } catch (InterruptedException e) { - throw new RuntimeException(e); + case "hang": + { + try { + Thread.sleep(25000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + listener.onNext(new Result(new byte[] {})); + listener.onCompleted(); + return; } - listener.onNext(new Result(new byte[]{})); - listener.onCompleted(); - return; - } - case "fast": { - try { - Thread.sleep(500); - } catch (InterruptedException e) { - throw new RuntimeException(e); + case "fast": + { + try { + Thread.sleep(500); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + listener.onNext(new Result(new byte[] {42, 42})); + listener.onCompleted(); + return; + } + default: + { + throw new UnsupportedOperationException(action.getType()); } - listener.onNext(new Result(new byte[]{42, 42})); - listener.onCompleted(); - return; - } - default: { - throw new UnsupportedOperationException(action.getType()); - } } } } diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestClientMiddleware.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestClientMiddleware.java index bcff54bd7f6..026fda87b29 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestClientMiddleware.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestClientMiddleware.java @@ -28,61 +28,69 @@ import java.util.List; import java.util.Map; import java.util.function.BiConsumer; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -/** - * A basic test of client middleware using a simplified OpenTracing-like example. - */ +/** A basic test of client middleware using a simplified OpenTracing-like example. */ public class TestClientMiddleware { /** - * Test that a client middleware can fail a call before it starts by throwing a {@link FlightRuntimeException}. + * Test that a client middleware can fail a call before it starts by throwing a {@link + * FlightRuntimeException}. */ @Test public void clientMiddleware_failCallBeforeSending() { - test(new NoOpFlightProducer(), null, Collections.singletonList(new CallRejector.Factory()), + test( + new NoOpFlightProducer(), + null, + Collections.singletonList(new CallRejector.Factory()), (allocator, client) -> { FlightTestUtil.assertCode(FlightStatusCode.UNAVAILABLE, client::listActions); }); } /** - * Test an OpenTracing-like scenario where client and server middleware work together to propagate a request ID - * without explicit intervention from the service implementation. + * Test an OpenTracing-like scenario where client and server middleware work together to propagate + * a request ID without explicit intervention from the service implementation. */ @Test public void middleware_propagateHeader() { final Context context = new Context("span id"); - test(new NoOpFlightProducer(), + test( + new NoOpFlightProducer(), new TestServerMiddleware.ServerMiddlewarePair<>( FlightServerMiddleware.Key.of("test"), new ServerSpanInjector.Factory()), Collections.singletonList(new ClientSpanInjector.Factory(context)), (allocator, client) -> { - FlightTestUtil.assertCode(FlightStatusCode.UNIMPLEMENTED, () -> client.listActions().forEach(actionType -> { - })); + FlightTestUtil.assertCode( + FlightStatusCode.UNIMPLEMENTED, () -> client.listActions().forEach(actionType -> {})); }); Assertions.assertEquals(context.outgoingSpanId, context.incomingSpanId); Assertions.assertNotNull(context.finalStatus); Assertions.assertEquals(FlightStatusCode.UNIMPLEMENTED, context.finalStatus.code()); } - /** Ensure both server and client can send and receive multi-valued headers (both binary and text values). */ + /** + * Ensure both server and client can send and receive multi-valued headers (both binary and text + * values). + */ @Test public void testMultiValuedHeaders() { - final MultiHeaderClientMiddlewareFactory clientFactory = new MultiHeaderClientMiddlewareFactory(); - test(new NoOpFlightProducer(), + final MultiHeaderClientMiddlewareFactory clientFactory = + new MultiHeaderClientMiddlewareFactory(); + test( + new NoOpFlightProducer(), new TestServerMiddleware.ServerMiddlewarePair<>( FlightServerMiddleware.Key.of("test"), new MultiHeaderServerMiddlewareFactory()), Collections.singletonList(clientFactory), (allocator, client) -> { - FlightTestUtil.assertCode(FlightStatusCode.UNIMPLEMENTED, () -> client.listActions().forEach(actionType -> { - })); + FlightTestUtil.assertCode( + FlightStatusCode.UNIMPLEMENTED, () -> client.listActions().forEach(actionType -> {})); }); - // The server echoes the headers we send back to us, so ensure all the ones we sent are present with the correct + // The server echoes the headers we send back to us, so ensure all the ones we sent are present + // with the correct // values in the correct order. for (final Map.Entry> entry : EXPECTED_BINARY_HEADERS.entrySet()) { // Compare header values entry-by-entry because byte arrays don't compare via equals @@ -90,7 +98,8 @@ public void testMultiValuedHeaders() { Assertions.assertNotNull(receivedValues, "Missing for header: " + entry.getKey()); Assertions.assertEquals( entry.getValue().size(), - receivedValues.size(), "Missing or wrong value for header: " + entry.getKey()); + receivedValues.size(), + "Missing or wrong value for header: " + entry.getKey()); for (int i = 0; i < entry.getValue().size(); i++) { Assertions.assertArrayEquals(entry.getValue().get(i), receivedValues.get(i)); } @@ -99,12 +108,12 @@ public void testMultiValuedHeaders() { Assertions.assertEquals( entry.getValue(), clientFactory.lastTextHeaders.get(entry.getKey()), - "Missing or wrong value for header: " + entry.getKey() - ); + "Missing or wrong value for header: " + entry.getKey()); } } - private static void test(FlightProducer producer, + private static void test( + FlightProducer producer, TestServerMiddleware.ServerMiddlewarePair serverMiddleware, List clientMiddleware, BiConsumer body) { @@ -119,8 +128,7 @@ private static void test(FlightProducer produ FlightClient.Builder clientBuilder = FlightClient.builder(allocator, server.getLocation()); clientMiddleware.forEach(clientBuilder::intercept); try (final FlightServer ignored = server; - final FlightClient client = clientBuilder.build() - ) { + final FlightClient client = clientBuilder.build()) { body.accept(allocator, client); } } catch (InterruptedException | IOException e) { @@ -129,8 +137,8 @@ private static void test(FlightProducer produ } /** - * A server middleware component that reads a request ID from incoming headers and sends the request ID back on - * outgoing headers. + * A server middleware component that reads a request ID from incoming headers and sends the + * request ID back on outgoing headers. */ static class ServerSpanInjector implements FlightServerMiddleware { @@ -146,27 +154,24 @@ public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) { } @Override - public void onCallCompleted(CallStatus status) { - - } + public void onCallCompleted(CallStatus status) {} @Override - public void onCallErrored(Throwable err) { - - } + public void onCallErrored(Throwable err) {} static class Factory implements FlightServerMiddleware.Factory { @Override - public ServerSpanInjector onCallStarted(CallInfo info, CallHeaders incomingHeaders, RequestContext context) { + public ServerSpanInjector onCallStarted( + CallInfo info, CallHeaders incomingHeaders, RequestContext context) { return new ServerSpanInjector(incomingHeaders.get("x-span")); } } } /** - * A client middleware component that, given a mock OpenTracing-like "request context", sends the request ID in the - * context on outgoing headers and reads it from incoming headers. + * A client middleware component that, given a mock OpenTracing-like "request context", sends the + * request ID in the context on outgoing headers and reads it from incoming headers. */ static class ClientSpanInjector implements FlightClientMiddleware { @@ -206,9 +211,7 @@ public FlightClientMiddleware onCallStarted(CallInfo info) { } } - /** - * A mock OpenTracing-like "request context". - */ + /** A mock OpenTracing-like "request context". */ static class Context { final String outgoingSpanId; @@ -220,22 +223,17 @@ static class Context { } } - /** - * A client middleware that fails outgoing calls. - */ + /** A client middleware that fails outgoing calls. */ static class CallRejector implements FlightClientMiddleware { @Override - public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) { - } + public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) {} @Override - public void onHeadersReceived(CallHeaders incomingHeaders) { - } + public void onHeadersReceived(CallHeaders incomingHeaders) {} @Override - public void onCallCompleted(CallStatus status) { - } + public void onCallCompleted(CallStatus status) {} static class Factory implements FlightClientMiddleware.Factory { @@ -251,36 +249,41 @@ public FlightClientMiddleware onCallStarted(CallInfo info) { static Map> EXPECTED_TEXT_HEADERS = new HashMap>(); static { - EXPECTED_BINARY_HEADERS.put("x-binary-bin", Arrays.asList(new byte[] {0}, new byte[]{1})); + EXPECTED_BINARY_HEADERS.put("x-binary-bin", Arrays.asList(new byte[] {0}, new byte[] {1})); EXPECTED_TEXT_HEADERS.put("x-text", Arrays.asList("foo", "bar")); } - static class MultiHeaderServerMiddlewareFactory implements - FlightServerMiddleware.Factory { + static class MultiHeaderServerMiddlewareFactory + implements FlightServerMiddleware.Factory { @Override - public MultiHeaderServerMiddleware onCallStarted(CallInfo info, CallHeaders incomingHeaders, - RequestContext context) { - // Echo the headers back to the client. Copy values out of CallHeaders since the underlying gRPC metadata + public MultiHeaderServerMiddleware onCallStarted( + CallInfo info, CallHeaders incomingHeaders, RequestContext context) { + // Echo the headers back to the client. Copy values out of CallHeaders since the underlying + // gRPC metadata // object isn't safe to use after this function returns. Map> binaryHeaders = new HashMap<>(); Map> textHeaders = new HashMap<>(); for (final String key : incomingHeaders.keys()) { if (key.endsWith("-bin")) { - binaryHeaders.compute(key, (ignored, values) -> { - if (values == null) { - values = new ArrayList<>(); - } - incomingHeaders.getAllByte(key).forEach(values::add); - return values; - }); + binaryHeaders.compute( + key, + (ignored, values) -> { + if (values == null) { + values = new ArrayList<>(); + } + incomingHeaders.getAllByte(key).forEach(values::add); + return values; + }); } else { - textHeaders.compute(key, (ignored, values) -> { - if (values == null) { - values = new ArrayList<>(); - } - incomingHeaders.getAll(key).forEach(values::add); - return values; - }); + textHeaders.compute( + key, + (ignored, values) -> { + if (values == null) { + values = new ArrayList<>(); + } + incomingHeaders.getAll(key).forEach(values::add); + return values; + }); } } return new MultiHeaderServerMiddleware(binaryHeaders, textHeaders); @@ -291,15 +294,18 @@ static class MultiHeaderServerMiddleware implements FlightServerMiddleware { private final Map> binaryHeaders; private final Map> textHeaders; - MultiHeaderServerMiddleware(Map> binaryHeaders, Map> textHeaders) { + MultiHeaderServerMiddleware( + Map> binaryHeaders, Map> textHeaders) { this.binaryHeaders = binaryHeaders; this.textHeaders = textHeaders; } @Override public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) { - binaryHeaders.forEach((key, values) -> values.forEach(value -> outgoingHeaders.insert(key, value))); - textHeaders.forEach((key, values) -> values.forEach(value -> outgoingHeaders.insert(key, value))); + binaryHeaders.forEach( + (key, values) -> values.forEach(value -> outgoingHeaders.insert(key, value))); + textHeaders.forEach( + (key, values) -> values.forEach(value -> outgoingHeaders.insert(key, value))); } @Override @@ -342,17 +348,20 @@ public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) { public void onHeadersReceived(CallHeaders incomingHeaders) { factory.lastBinaryHeaders = new HashMap<>(); factory.lastTextHeaders = new HashMap<>(); - incomingHeaders.keys().forEach(header -> { - if (header.endsWith("-bin")) { - final List values = new ArrayList<>(); - incomingHeaders.getAllByte(header).forEach(values::add); - factory.lastBinaryHeaders.put(header, values); - } else { - final List values = new ArrayList<>(); - incomingHeaders.getAll(header).forEach(values::add); - factory.lastTextHeaders.put(header, values); - } - }); + incomingHeaders + .keys() + .forEach( + header -> { + if (header.endsWith("-bin")) { + final List values = new ArrayList<>(); + incomingHeaders.getAllByte(header).forEach(values::add); + factory.lastBinaryHeaders.put(header, values); + } else { + final List values = new ArrayList<>(); + incomingHeaders.getAll(header).forEach(values::add); + factory.lastTextHeaders.put(header, values); + } + }); } @Override diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestDictionaryUtils.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestDictionaryUtils.java index 40930131e0c..29d9100ec8b 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestDictionaryUtils.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestDictionaryUtils.java @@ -21,8 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertSame; +import com.google.common.collect.ImmutableList; import java.util.TreeSet; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.VarCharVector; @@ -35,11 +35,7 @@ import org.apache.arrow.vector.types.pojo.Schema; import org.junit.jupiter.api.Test; -import com.google.common.collect.ImmutableList; - -/** - * Test cases for {@link DictionaryUtils}. - */ +/** Test cases for {@link DictionaryUtils}. */ public class TestDictionaryUtils { @Test @@ -47,9 +43,9 @@ public void testReuseSchema() { FieldType varcharType = new FieldType(true, new ArrowType.Utf8(), null); FieldType intType = new FieldType(true, new ArrowType.Int(32, true), null); - ImmutableList build = ImmutableList.of( - new Field("stringCol", varcharType, null), - new Field("intCol", intType, null)); + ImmutableList build = + ImmutableList.of( + new Field("stringCol", varcharType, null), new Field("intCol", intType, null)); Schema schema = new Schema(build); Schema newSchema = DictionaryUtils.generateSchema(schema, null, new TreeSet<>()); @@ -62,18 +58,19 @@ public void testReuseSchema() { public void testCreateSchema() { try (BufferAllocator allocator = new RootAllocator(1024)) { DictionaryEncoding dictionaryEncoding = - new DictionaryEncoding(0, true, new ArrowType.Int(8, true)); + new DictionaryEncoding(0, true, new ArrowType.Int(8, true)); VarCharVector dictVec = new VarCharVector("dict vector", allocator); Dictionary dictionary = new Dictionary(dictVec, dictionaryEncoding); DictionaryProvider dictProvider = new DictionaryProvider.MapDictionaryProvider(dictionary); TreeSet dictionaryUsed = new TreeSet<>(); - FieldType encodedVarcharType = new FieldType(true, new ArrowType.Int(8, true), dictionaryEncoding); + FieldType encodedVarcharType = + new FieldType(true, new ArrowType.Int(8, true), dictionaryEncoding); FieldType intType = new FieldType(true, new ArrowType.Int(32, true), null); - ImmutableList build = ImmutableList.of( - new Field("stringCol", encodedVarcharType, null), - new Field("intCol", intType, null)); + ImmutableList build = + ImmutableList.of( + new Field("stringCol", encodedVarcharType, null), new Field("intCol", intType, null)); Schema schema = new Schema(build); Schema newSchema = DictionaryUtils.generateSchema(schema, dictProvider, dictionaryUsed); diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestDoExchange.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestDoExchange.java index b70353df8e9..e27007928f5 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestDoExchange.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestDoExchange.java @@ -28,7 +28,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.stream.IntStream; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -67,7 +66,8 @@ public void setUp() throws Exception { final Location serverLocation = Location.forGrpcInsecure(FlightTestUtil.LOCALHOST, 0); server = FlightServer.builder(allocator, serverLocation, new Producer(allocator)).build(); server.start(); - final Location clientLocation = Location.forGrpcInsecure(FlightTestUtil.LOCALHOST, server.getPort()); + final Location clientLocation = + Location.forGrpcInsecure(FlightTestUtil.LOCALHOST, server.getPort()); client = FlightClient.builder(allocator, clientLocation).build(); } @@ -81,10 +81,11 @@ public void tearDown() throws Exception { public void testDoExchangeOnlyMetadata() throws Exception { // Send a particular descriptor to the server and check for a particular response pattern. try (final FlightClient.ExchangeReaderWriter stream = - client.doExchange(FlightDescriptor.command(EXCHANGE_METADATA_ONLY))) { + client.doExchange(FlightDescriptor.command(EXCHANGE_METADATA_ONLY))) { final FlightStream reader = stream.getReader(); - // Server starts by sending a message without data (hence no VectorSchemaRoot should be present) + // Server starts by sending a message without data (hence no VectorSchemaRoot should be + // present) assertTrue(reader.next()); assertFalse(reader.hasRoot()); assertEquals(42, reader.getLatestMetadata().getInt(0)); @@ -109,7 +110,7 @@ public void testDoExchangeOnlyMetadata() throws Exception { @Test public void testDoExchangeDoGet() throws Exception { try (final FlightClient.ExchangeReaderWriter stream = - client.doExchange(FlightDescriptor.command(EXCHANGE_DO_GET))) { + client.doExchange(FlightDescriptor.command(EXCHANGE_DO_GET))) { final FlightStream reader = stream.getReader(); VectorSchemaRoot root = reader.getRoot(); IntVector iv = (IntVector) root.getVector("a"); @@ -128,31 +129,35 @@ public void testDoExchangeDoGet() throws Exception { /** Emulate a DoPut with a DoExchange. */ @Test public void testDoExchangeDoPut() throws Exception { - final Schema schema = new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); try (final FlightClient.ExchangeReaderWriter stream = - client.doExchange(FlightDescriptor.command(EXCHANGE_DO_PUT)); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + client.doExchange(FlightDescriptor.command(EXCHANGE_DO_PUT)); + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { IntVector iv = (IntVector) root.getVector("a"); iv.allocateNew(); stream.getWriter().start(root); int counter = 0; for (int i = 0; i < 10; i++) { - ValueVectorDataPopulator.setVector(iv, IntStream.range(0, i).boxed().toArray(Integer[]::new)); + ValueVectorDataPopulator.setVector( + iv, IntStream.range(0, i).boxed().toArray(Integer[]::new)); root.setRowCount(i); counter += i; stream.getWriter().putNext(); assertTrue(stream.getReader().next()); assertFalse(stream.getReader().hasRoot()); - // For each write, the server sends back a metadata message containing the index of the last written batch + // For each write, the server sends back a metadata message containing the index of the last + // written batch final ArrowBuf metadata = stream.getReader().getLatestMetadata(); assertEquals(counter, metadata.getInt(0)); } stream.getWriter().completed(); while (stream.getReader().next()) { - // Drain the stream. Otherwise closing the stream sends a CANCEL which seriously screws with the server. + // Drain the stream. Otherwise closing the stream sends a CANCEL which seriously screws with + // the server. // CANCEL -> runs onCancel handler -> closes the FlightStream early } } @@ -161,9 +166,11 @@ public void testDoExchangeDoPut() throws Exception { /** Test a DoExchange that echoes the client message. */ @Test public void testDoExchangeEcho() throws Exception { - final Schema schema = new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); - try (final FlightClient.ExchangeReaderWriter stream = client.doExchange(FlightDescriptor.command(EXCHANGE_ECHO)); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); + try (final FlightClient.ExchangeReaderWriter stream = + client.doExchange(FlightDescriptor.command(EXCHANGE_ECHO)); + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { final FlightStream reader = stream.getReader(); // First try writing metadata without starting the Arrow data stream @@ -208,12 +215,15 @@ public void testDoExchangeEcho() throws Exception { /** Write some data, have it transformed, then read it back. */ @Test public void testTransform() throws Exception { - final Schema schema = new Schema(Arrays.asList( - Field.nullable("a", new ArrowType.Int(32, true)), - Field.nullable("b", new ArrowType.Int(32, true)))); + final Schema schema = + new Schema( + Arrays.asList( + Field.nullable("a", new ArrowType.Int(32, true)), + Field.nullable("b", new ArrowType.Int(32, true)))); try (final FlightClient.ExchangeReaderWriter stream = - client.doExchange(FlightDescriptor.command(EXCHANGE_TRANSFORM))) { - // Write ten batches of data to the stream, where batch N contains N rows of data (N in [0, 10)) + client.doExchange(FlightDescriptor.command(EXCHANGE_TRANSFORM))) { + // Write ten batches of data to the stream, where batch N contains N rows of data (N in [0, + // 10)) final FlightStream reader = stream.getReader(); final FlightClient.ClientStreamListener writer = stream.getWriter(); try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { @@ -221,7 +231,8 @@ public void testTransform() throws Exception { for (int batchIndex = 0; batchIndex < 10; batchIndex++) { for (final FieldVector rawVec : root.getFieldVectors()) { final IntVector vec = (IntVector) rawVec; - ValueVectorDataPopulator.setVector(vec, IntStream.range(0, batchIndex).boxed().toArray(Integer[]::new)); + ValueVectorDataPopulator.setVector( + vec, IntStream.range(0, batchIndex).boxed().toArray(Integer[]::new)); } root.setRowCount(batchIndex); writer.putNext(); @@ -255,12 +266,15 @@ public void testTransform() throws Exception { @Test public void testTransformZeroCopy() throws Exception { final int rowsPerBatch = 4096; - final Schema schema = new Schema(Arrays.asList( - Field.nullable("a", new ArrowType.Int(32, true)), - Field.nullable("b", new ArrowType.Int(32, true)))); + final Schema schema = + new Schema( + Arrays.asList( + Field.nullable("a", new ArrowType.Int(32, true)), + Field.nullable("b", new ArrowType.Int(32, true)))); try (final FlightClient.ExchangeReaderWriter stream = - client.doExchange(FlightDescriptor.command(EXCHANGE_TRANSFORM))) { - // Write ten batches of data to the stream, where batch N contains 1024 rows of data (N in [0, 10)) + client.doExchange(FlightDescriptor.command(EXCHANGE_TRANSFORM))) { + // Write ten batches of data to the stream, where batch N contains 1024 rows of data (N in [0, + // 10)) final FlightStream reader = stream.getReader(); final FlightClient.ClientStreamListener writer = stream.getWriter(); try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { @@ -311,7 +325,7 @@ public void testTransformZeroCopy() throws Exception { @Test public void testServerCancel() throws Exception { try (final FlightClient.ExchangeReaderWriter stream = - client.doExchange(FlightDescriptor.command(EXCHANGE_CANCEL))) { + client.doExchange(FlightDescriptor.command(EXCHANGE_CANCEL))) { final FlightStream reader = stream.getReader(); final FlightClient.ClientStreamListener writer = stream.getWriter(); @@ -319,10 +333,14 @@ public void testServerCancel() throws Exception { assertEquals(FlightStatusCode.CANCELLED, fre.status().code()); assertEquals("expected", fre.status().description()); - // Before, this would hang forever, because the writer checks if the stream is ready and not cancelled. - // However, the cancellation flag (was) only updated by reading, and the stream is never ready once the call ends. - // The test looks weird since normally, an application shouldn't try to write after the read fails. However, - // an application that isn't reading data wouldn't notice, and would instead get stuck on the write. + // Before, this would hang forever, because the writer checks if the stream is ready and not + // cancelled. + // However, the cancellation flag (was) only updated by reading, and the stream is never ready + // once the call ends. + // The test looks weird since normally, an application shouldn't try to write after the read + // fails. However, + // an application that isn't reading data wouldn't notice, and would instead get stuck on the + // write. // Here, we read first to avoid a race condition in the test itself. writer.putMetadata(allocator.getEmpty()); } @@ -332,7 +350,7 @@ public void testServerCancel() throws Exception { @Test public void testServerCancelLeak() throws Exception { try (final FlightClient.ExchangeReaderWriter stream = - client.doExchange(FlightDescriptor.command(EXCHANGE_CANCEL))) { + client.doExchange(FlightDescriptor.command(EXCHANGE_CANCEL))) { final FlightStream reader = stream.getReader(); final FlightClient.ClientStreamListener writer = stream.getWriter(); try (final VectorSchemaRoot root = VectorSchemaRoot.create(Producer.SCHEMA, allocator)) { @@ -358,7 +376,7 @@ public void testServerCancelLeak() throws Exception { @Disabled public void testClientCancel() throws Exception { try (final FlightClient.ExchangeReaderWriter stream = - client.doExchange(FlightDescriptor.command(EXCHANGE_DO_GET))) { + client.doExchange(FlightDescriptor.command(EXCHANGE_DO_GET))) { final FlightStream reader = stream.getReader(); reader.cancel("", null); // Cancel should be idempotent @@ -369,9 +387,11 @@ public void testClientCancel() throws Exception { /** Test a DoExchange error handling. */ @Test public void testDoExchangeError() throws Exception { - final Schema schema = new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); - try (final FlightClient.ExchangeReaderWriter stream = client.doExchange(FlightDescriptor.command(EXCHANGE_ERROR)); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); + try (final FlightClient.ExchangeReaderWriter stream = + client.doExchange(FlightDescriptor.command(EXCHANGE_ERROR)); + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { final FlightStream reader = stream.getReader(); // Write data and check that it gets echoed back. @@ -392,7 +412,8 @@ public void testDoExchangeError() throws Exception { stream.getWriter().completed(); // Must call reader.next() to get any errors after exchange, will return false if no error - final FlightRuntimeException fre = assertThrows(FlightRuntimeException.class, stream::getResult); + final FlightRuntimeException fre = + assertThrows(FlightRuntimeException.class, stream::getResult); assertEquals("error completing exchange", fre.status().description()); } } @@ -401,11 +422,13 @@ public void testDoExchangeError() throws Exception { @Test public void testClientClose() throws Exception { try (final FlightClient.ExchangeReaderWriter stream = - client.doExchange(FlightDescriptor.command(EXCHANGE_DO_GET))) { + client.doExchange(FlightDescriptor.command(EXCHANGE_DO_GET))) { assertEquals(Producer.SCHEMA, stream.getReader().getSchema()); } - // Intentionally leak the allocator in this test. gRPC has a bug where it does not wait for all calls to complete - // when shutting down the server, so this test will fail otherwise because it closes the allocator while the + // Intentionally leak the allocator in this test. gRPC has a bug where it does not wait for all + // calls to complete + // when shutting down the server, so this test will fail otherwise because it closes the + // allocator while the // server-side call still has memory allocated. // TODO(ARROW-9586): fix this once we track outstanding RPCs outside of gRPC. // https://stackoverflow.com/questions/46716024/ @@ -418,10 +441,11 @@ public void testClientClose() throws Exception { public void testCloseWithMetadata() throws Exception { // Send a particular descriptor to the server and check for a particular response pattern. try (final FlightClient.ExchangeReaderWriter stream = - client.doExchange(FlightDescriptor.command(EXCHANGE_METADATA_ONLY))) { + client.doExchange(FlightDescriptor.command(EXCHANGE_METADATA_ONLY))) { final FlightStream reader = stream.getReader(); - // Server starts by sending a message without data (hence no VectorSchemaRoot should be present) + // Server starts by sending a message without data (hence no VectorSchemaRoot should be + // present) assertTrue(reader.next()); assertFalse(reader.hasRoot()); assertEquals(42, reader.getLatestMetadata().getInt(0)); @@ -440,14 +464,15 @@ public void testCloseWithMetadata() throws Exception { stream.getWriter().completed(); stream.getResult(); - // Not necessary to close reader here, but check closing twice doesn't lead to negative refcnt from metadata + // Not necessary to close reader here, but check closing twice doesn't lead to negative refcnt + // from metadata stream.getReader().close(); } } static class Producer extends NoOpFlightProducer { - static final Schema SCHEMA = new Schema( - Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); + static final Schema SCHEMA = + new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); private final BufferAllocator allocator; Producer(BufferAllocator allocator) { @@ -471,12 +496,16 @@ public void doExchange(CallContext context, FlightStream reader, ServerStreamLis } else if (Arrays.equals(reader.getDescriptor().getCommand(), EXCHANGE_ERROR)) { error(context, reader, writer); } else { - writer.error(CallStatus.UNIMPLEMENTED.withDescription("Command not implemented").toRuntimeException()); + writer.error( + CallStatus.UNIMPLEMENTED + .withDescription("Command not implemented") + .toRuntimeException()); } } /** Emulate DoGet. */ - private void doGet(CallContext unusedContext, FlightStream unusedReader, ServerStreamListener writer) { + private void doGet( + CallContext unusedContext, FlightStream unusedReader, ServerStreamListener writer) { try (VectorSchemaRoot root = VectorSchemaRoot.create(SCHEMA, allocator)) { writer.start(root); root.allocateNew(); @@ -493,11 +522,15 @@ private void doGet(CallContext unusedContext, FlightStream unusedReader, ServerS } /** Emulate DoPut. */ - private void doPut(CallContext unusedContext, FlightStream reader, ServerStreamListener writer) { + private void doPut( + CallContext unusedContext, FlightStream reader, ServerStreamListener writer) { int counter = 0; while (reader.next()) { if (!reader.hasRoot()) { - writer.error(CallStatus.INVALID_ARGUMENT.withDescription("Message has no data").toRuntimeException()); + writer.error( + CallStatus.INVALID_ARGUMENT + .withDescription("Message has no data") + .toRuntimeException()); return; } counter += reader.getRoot().getRowCount(); @@ -510,7 +543,8 @@ private void doPut(CallContext unusedContext, FlightStream reader, ServerStreamL } /** Exchange metadata without ever exchanging data. */ - private void metadataOnly(CallContext unusedContext, FlightStream reader, ServerStreamListener writer) { + private void metadataOnly( + CallContext unusedContext, FlightStream reader, ServerStreamListener writer) { final ArrowBuf buf = allocator.buffer(4); buf.writeInt(42); writer.putMetadata(buf); @@ -555,16 +589,23 @@ private void echo(CallContext unusedContext, FlightStream reader, ServerStreamLi } /** Accept a set of messages, then return some result. */ - private void transform(CallContext unusedContext, FlightStream reader, ServerStreamListener writer) { + private void transform( + CallContext unusedContext, FlightStream reader, ServerStreamListener writer) { final Schema schema = reader.getSchema(); for (final Field field : schema.getFields()) { if (!(field.getType() instanceof ArrowType.Int)) { - writer.error(CallStatus.INVALID_ARGUMENT.withDescription("Invalid type: " + field).toRuntimeException()); + writer.error( + CallStatus.INVALID_ARGUMENT + .withDescription("Invalid type: " + field) + .toRuntimeException()); return; } final ArrowType.Int intType = (ArrowType.Int) field.getType(); if (!intType.getIsSigned() || intType.getBitWidth() != 32) { - writer.error(CallStatus.INVALID_ARGUMENT.withDescription("Must be i32: " + field).toRuntimeException()); + writer.error( + CallStatus.INVALID_ARGUMENT + .withDescription("Must be i32: " + field) + .toRuntimeException()); return; } } @@ -597,11 +638,13 @@ private void transform(CallContext unusedContext, FlightStream reader, ServerStr } /** Immediately cancel the call. */ - private void cancel(CallContext unusedContext, FlightStream unusedReader, ServerStreamListener writer) { + private void cancel( + CallContext unusedContext, FlightStream unusedReader, ServerStreamListener writer) { writer.error(CallStatus.CANCELLED.withDescription("expected").toRuntimeException()); } - private void error(CallContext unusedContext, FlightStream reader, ServerStreamListener writer) { + private void error( + CallContext unusedContext, FlightStream reader, ServerStreamListener writer) { VectorSchemaRoot root = null; VectorLoader loader = null; while (reader.next()) { @@ -623,7 +666,8 @@ private void error(CallContext unusedContext, FlightStream reader, ServerStreamL } // An error occurs before completing the writer - writer.error(CallStatus.INTERNAL.withDescription("error completing exchange").toRuntimeException()); + writer.error( + CallStatus.INTERNAL.withDescription("error completing exchange").toRuntimeException()); } } } diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestErrorMetadata.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestErrorMetadata.java index 4ec73014662..374659fb366 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestErrorMetadata.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestErrorMetadata.java @@ -20,44 +20,48 @@ import static org.apache.arrow.flight.FlightTestUtil.LOCALHOST; import static org.apache.arrow.flight.Location.forGrpcInsecure; -import java.nio.charset.StandardCharsets; - -import org.apache.arrow.flight.perf.impl.PerfOuterClass; -import org.apache.arrow.memory.BufferAllocator; -import org.apache.arrow.memory.RootAllocator; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - import com.google.protobuf.Any; import com.google.protobuf.InvalidProtocolBufferException; import com.google.rpc.Status; - import io.grpc.Metadata; import io.grpc.StatusRuntimeException; import io.grpc.protobuf.ProtoUtils; import io.grpc.protobuf.StatusProto; +import java.nio.charset.StandardCharsets; +import org.apache.arrow.flight.perf.impl.PerfOuterClass; +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.RootAllocator; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; public class TestErrorMetadata { private static final Metadata.BinaryMarshaller marshaller = - ProtoUtils.metadataMarshaller(Status.getDefaultInstance()); + ProtoUtils.metadataMarshaller(Status.getDefaultInstance()); /** Ensure metadata attached to a gRPC error is propagated. */ @Test public void testGrpcMetadata() throws Exception { - PerfOuterClass.Perf perf = PerfOuterClass.Perf.newBuilder() - .setStreamCount(12) - .setRecordsPerBatch(1000) - .setRecordsPerStream(1000000L) - .build(); + PerfOuterClass.Perf perf = + PerfOuterClass.Perf.newBuilder() + .setStreamCount(12) + .setRecordsPerBatch(1000) + .setRecordsPerStream(1000000L) + .build(); StatusRuntimeExceptionProducer producer = new StatusRuntimeExceptionProducer(perf); try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - final FlightServer s = FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), producer).build() - .start(); - final FlightClient client = FlightClient.builder(allocator, s.getLocation()).build()) { - final CallStatus flightStatus = FlightTestUtil.assertCode(FlightStatusCode.CANCELLED, () -> { - FlightStream stream = client.getStream(new Ticket("abs".getBytes(StandardCharsets.UTF_8))); - stream.next(); - }); + final FlightServer s = + FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), producer) + .build() + .start(); + final FlightClient client = FlightClient.builder(allocator, s.getLocation()).build()) { + final CallStatus flightStatus = + FlightTestUtil.assertCode( + FlightStatusCode.CANCELLED, + () -> { + FlightStream stream = + client.getStream(new Ticket("abs".getBytes(StandardCharsets.UTF_8))); + stream.next(); + }); PerfOuterClass.Perf newPerf = null; ErrorFlightMetadata metadata = flightStatus.metadata(); Assertions.assertNotNull(metadata); @@ -82,25 +86,33 @@ public void testGrpcMetadata() throws Exception { @Test public void testFlightMetadata() throws Exception { try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - final FlightServer s = FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), new CallStatusProducer()) - .build().start(); - final FlightClient client = FlightClient.builder(allocator, s.getLocation()).build()) { - CallStatus flightStatus = FlightTestUtil.assertCode(FlightStatusCode.INVALID_ARGUMENT, () -> { - FlightStream stream = client.getStream(new Ticket(new byte[0])); - stream.next(); - }); + final FlightServer s = + FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), new CallStatusProducer()) + .build() + .start(); + final FlightClient client = FlightClient.builder(allocator, s.getLocation()).build()) { + CallStatus flightStatus = + FlightTestUtil.assertCode( + FlightStatusCode.INVALID_ARGUMENT, + () -> { + FlightStream stream = client.getStream(new Ticket(new byte[0])); + stream.next(); + }); ErrorFlightMetadata metadata = flightStatus.metadata(); Assertions.assertNotNull(metadata); Assertions.assertEquals("foo", metadata.get("x-foo")); - Assertions.assertArrayEquals(new byte[]{1}, metadata.getByte("x-bar-bin")); - - flightStatus = FlightTestUtil.assertCode(FlightStatusCode.INVALID_ARGUMENT, () -> { - client.getInfo(FlightDescriptor.command(new byte[0])); - }); + Assertions.assertArrayEquals(new byte[] {1}, metadata.getByte("x-bar-bin")); + + flightStatus = + FlightTestUtil.assertCode( + FlightStatusCode.INVALID_ARGUMENT, + () -> { + client.getInfo(FlightDescriptor.command(new byte[0])); + }); metadata = flightStatus.metadata(); Assertions.assertNotNull(metadata); Assertions.assertEquals("foo", metadata.get("x-foo")); - Assertions.assertArrayEquals(new byte[]{1}, metadata.getByte("x-bar-bin")); + Assertions.assertArrayEquals(new byte[] {1}, metadata.getByte("x-bar-bin")); } } @@ -113,11 +125,13 @@ private StatusRuntimeExceptionProducer(PerfOuterClass.Perf perf) { @Override public void getStream(CallContext context, Ticket ticket, ServerStreamListener listener) { - StatusRuntimeException sre = StatusProto.toStatusRuntimeException(Status.newBuilder() - .setCode(1) - .setMessage("Testing 1 2 3") - .addDetails(Any.pack(perf, "arrow/meta/types")) - .build()); + StatusRuntimeException sre = + StatusProto.toStatusRuntimeException( + Status.newBuilder() + .setCode(1) + .setMessage("Testing 1 2 3") + .addDetails(Any.pack(perf, "arrow/meta/types")) + .build()); listener.error(sre); } } @@ -128,17 +142,24 @@ private static class CallStatusProducer extends NoOpFlightProducer { CallStatusProducer() { this.metadata = new ErrorFlightMetadata(); metadata.insert("x-foo", "foo"); - metadata.insert("x-bar-bin", new byte[]{1}); + metadata.insert("x-bar-bin", new byte[] {1}); } @Override public void getStream(CallContext context, Ticket ticket, ServerStreamListener listener) { - listener.error(CallStatus.INVALID_ARGUMENT.withDescription("Failed").withMetadata(metadata).toRuntimeException()); + listener.error( + CallStatus.INVALID_ARGUMENT + .withDescription("Failed") + .withMetadata(metadata) + .toRuntimeException()); } @Override public FlightInfo getFlightInfo(CallContext context, FlightDescriptor descriptor) { - throw CallStatus.INVALID_ARGUMENT.withDescription("Failed").withMetadata(metadata).toRuntimeException(); + throw CallStatus.INVALID_ARGUMENT + .withDescription("Failed") + .withMetadata(metadata) + .toRuntimeException(); } } } diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestFlightClient.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestFlightClient.java index db05592ab37..bf3d450e698 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestFlightClient.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestFlightClient.java @@ -23,7 +23,6 @@ import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; - import org.apache.arrow.flight.FlightClient.ClientStreamListener; import org.apache.arrow.flight.TestBasicOperation.Producer; import org.apache.arrow.memory.BufferAllocator; @@ -48,22 +47,27 @@ import org.junit.jupiter.api.Test; public class TestFlightClient { - /** - * ARROW-5063: make sure two clients to the same location can be closed independently. - */ + /** ARROW-5063: make sure two clients to the same location can be closed independently. */ @Test public void independentShutdown() throws Exception { try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - final FlightServer server = FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), - new Producer(allocator)).build().start()) { - final Schema schema = new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); - try (final FlightClient client1 = FlightClient.builder(allocator, server.getLocation()).build(); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final FlightServer server = + FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), new Producer(allocator)) + .build() + .start()) { + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("a", new ArrowType.Int(32, true)))); + try (final FlightClient client1 = + FlightClient.builder(allocator, server.getLocation()).build(); + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { // Use startPut as this ensures the RPC won't finish until we want it to - final ClientStreamListener listener = client1.startPut(FlightDescriptor.path("test"), root, - new AsyncPutListener()); - try (final FlightClient client2 = FlightClient.builder(allocator, server.getLocation()).build()) { - client2.listActions().forEach(actionType -> Assertions.assertNotNull(actionType.getType())); + final ClientStreamListener listener = + client1.startPut(FlightDescriptor.path("test"), root, new AsyncPutListener()); + try (final FlightClient client2 = + FlightClient.builder(allocator, server.getLocation()).build()) { + client2 + .listActions() + .forEach(actionType -> Assertions.assertNotNull(actionType.getType())); } listener.completed(); listener.getResult(); @@ -77,45 +81,65 @@ public void independentShutdown() throws Exception { @Disabled // Unfortunately this test is flaky in CI. @Test public void freeDictionaries() throws Exception { - final Schema expectedSchema = new Schema(Collections - .singletonList(new Field("encoded", - new FieldType(true, new ArrowType.Int(32, true), new DictionaryEncoding(1L, false, null)), null))); + final Schema expectedSchema = + new Schema( + Collections.singletonList( + new Field( + "encoded", + new FieldType( + true, new ArrowType.Int(32, true), new DictionaryEncoding(1L, false, null)), + null))); try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - final BufferAllocator serverAllocator = allocator.newChildAllocator("flight-server", 0, Integer.MAX_VALUE); - final FlightServer server = FlightServer.builder(serverAllocator, forGrpcInsecure(LOCALHOST, 0), - new DictionaryProducer(serverAllocator)).build().start()) { - try (final FlightClient client = FlightClient.builder(allocator, server.getLocation()).build()) { + final BufferAllocator serverAllocator = + allocator.newChildAllocator("flight-server", 0, Integer.MAX_VALUE); + final FlightServer server = + FlightServer.builder( + serverAllocator, + forGrpcInsecure(LOCALHOST, 0), + new DictionaryProducer(serverAllocator)) + .build() + .start()) { + try (final FlightClient client = + FlightClient.builder(allocator, server.getLocation()).build()) { try (final FlightStream stream = client.getStream(new Ticket(new byte[0]))) { Assertions.assertTrue(stream.next()); Assertions.assertNotNull(stream.getDictionaryProvider().lookup(1)); final VectorSchemaRoot root = stream.getRoot(); Assertions.assertEquals(expectedSchema, root.getSchema()); Assertions.assertEquals(6, root.getVector("encoded").getValueCount()); - try (final ValueVector decoded = DictionaryEncoder - .decode(root.getVector("encoded"), stream.getDictionaryProvider().lookup(1))) { + try (final ValueVector decoded = + DictionaryEncoder.decode( + root.getVector("encoded"), stream.getDictionaryProvider().lookup(1))) { Assertions.assertFalse(decoded.isNull(1)); Assertions.assertTrue(decoded instanceof VarCharVector); - Assertions.assertArrayEquals("one".getBytes(StandardCharsets.UTF_8), ((VarCharVector) decoded).get(1)); + Assertions.assertArrayEquals( + "one".getBytes(StandardCharsets.UTF_8), ((VarCharVector) decoded).get(1)); } Assertions.assertFalse(stream.next()); } - // Closing stream fails if it doesn't free dictionaries; closing dictionaries fails (refcount goes negative) + // Closing stream fails if it doesn't free dictionaries; closing dictionaries fails + // (refcount goes negative) // if reference isn't retained in ArrowMessage } } } - /** - * ARROW-5978: make sure that dictionary ownership can't be claimed twice. - */ + /** ARROW-5978: make sure that dictionary ownership can't be claimed twice. */ @Disabled // Unfortunately this test is flaky in CI. @Test public void ownDictionaries() throws Exception { try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - final BufferAllocator serverAllocator = allocator.newChildAllocator("flight-server", 0, Integer.MAX_VALUE); - final FlightServer server = FlightServer.builder(serverAllocator, forGrpcInsecure(LOCALHOST, 0), - new DictionaryProducer(serverAllocator)).build().start()) { - try (final FlightClient client = FlightClient.builder(allocator, server.getLocation()).build()) { + final BufferAllocator serverAllocator = + allocator.newChildAllocator("flight-server", 0, Integer.MAX_VALUE); + final FlightServer server = + FlightServer.builder( + serverAllocator, + forGrpcInsecure(LOCALHOST, 0), + new DictionaryProducer(serverAllocator)) + .build() + .start()) { + try (final FlightClient client = + FlightClient.builder(allocator, server.getLocation()).build()) { try (final FlightStream stream = client.getStream(new Ticket(new byte[0]))) { Assertions.assertTrue(stream.next()); Assertions.assertFalse(stream.next()); @@ -128,18 +152,22 @@ public void ownDictionaries() throws Exception { } } - /** - * ARROW-5978: make sure that dictionaries can be used after closing the stream. - */ + /** ARROW-5978: make sure that dictionaries can be used after closing the stream. */ @Disabled // Unfortunately this test is flaky in CI. @Test public void useDictionariesAfterClose() throws Exception { try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - final BufferAllocator serverAllocator = allocator.newChildAllocator("flight-server", 0, Integer.MAX_VALUE); - final FlightServer server = FlightServer.builder(serverAllocator, forGrpcInsecure(LOCALHOST, 0), - new DictionaryProducer(serverAllocator)) - .build().start()) { - try (final FlightClient client = FlightClient.builder(allocator, server.getLocation()).build()) { + final BufferAllocator serverAllocator = + allocator.newChildAllocator("flight-server", 0, Integer.MAX_VALUE); + final FlightServer server = + FlightServer.builder( + serverAllocator, + forGrpcInsecure(LOCALHOST, 0), + new DictionaryProducer(serverAllocator)) + .build() + .start()) { + try (final FlightClient client = + FlightClient.builder(allocator, server.getLocation()).build()) { final VectorSchemaRoot root; final DictionaryProvider provider; try (final FlightStream stream = client.getStream(new Ticket(new byte[0]))) { @@ -153,11 +181,12 @@ public void useDictionariesAfterClose() throws Exception { } provider = stream.takeDictionaryOwnership(); } - try (final ValueVector decoded = DictionaryEncoder - .decode(root.getVector("encoded"), provider.lookup(1))) { + try (final ValueVector decoded = + DictionaryEncoder.decode(root.getVector("encoded"), provider.lookup(1))) { Assertions.assertFalse(decoded.isNull(1)); Assertions.assertTrue(decoded instanceof VarCharVector); - Assertions.assertArrayEquals("one".getBytes(StandardCharsets.UTF_8), ((VarCharVector) decoded).get(1)); + Assertions.assertArrayEquals( + "one".getBytes(StandardCharsets.UTF_8), ((VarCharVector) decoded).get(1)); } root.close(); DictionaryUtils.closeDictionaries(root.getSchema(), provider); @@ -179,7 +208,8 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l final byte[] one = "one".getBytes(StandardCharsets.UTF_8); final byte[] two = "two".getBytes(StandardCharsets.UTF_8); try (final VarCharVector dictionaryVector = newVarCharVector("dictionary", allocator)) { - final DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); + final DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); dictionaryVector.allocateNew(512, 3); dictionaryVector.setSafe(0, zero, 0, zero.length); @@ -187,7 +217,8 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l dictionaryVector.setSafe(2, two, 0, two.length); dictionaryVector.setValueCount(3); - final Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); + final Dictionary dictionary = + new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); provider.put(dictionary); final FieldVector encodedVector; @@ -204,7 +235,8 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l final List fields = Collections.singletonList(encodedVector.getField()); final List vectors = Collections.singletonList(encodedVector); - try (final VectorSchemaRoot root = new VectorSchemaRoot(fields, vectors, encodedVector.getValueCount())) { + try (final VectorSchemaRoot root = + new VectorSchemaRoot(fields, vectors, encodedVector.getValueCount())) { listener.start(root, provider); listener.putNext(); listener.completed(); diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestFlightGrpcUtils.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestFlightGrpcUtils.java index 2569d2ac2b3..9a7c1af303d 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestFlightGrpcUtils.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestFlightGrpcUtils.java @@ -19,10 +19,17 @@ import static org.junit.jupiter.api.Assertions.assertThrows; +import com.google.protobuf.Empty; +import io.grpc.BindableService; +import io.grpc.ConnectivityState; +import io.grpc.ManagedChannel; +import io.grpc.Server; +import io.grpc.inprocess.InProcessChannelBuilder; +import io.grpc.inprocess.InProcessServerBuilder; +import io.grpc.stub.StreamObserver; import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; - import org.apache.arrow.flight.auth.ServerAuthHandler; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -31,19 +38,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import com.google.protobuf.Empty; - -import io.grpc.BindableService; -import io.grpc.ConnectivityState; -import io.grpc.ManagedChannel; -import io.grpc.Server; -import io.grpc.inprocess.InProcessChannelBuilder; -import io.grpc.inprocess.InProcessServerBuilder; -import io.grpc.stub.StreamObserver; - -/** - * Unit test which adds 2 services to same server end point. - */ +/** Unit test which adds 2 services to same server end point. */ public class TestFlightGrpcUtils { private Server server; private BufferAllocator allocator; @@ -51,21 +46,22 @@ public class TestFlightGrpcUtils { @BeforeEach public void setup() throws IOException { - //Defines flight service + // Defines flight service allocator = new RootAllocator(Integer.MAX_VALUE); final NoOpFlightProducer producer = new NoOpFlightProducer(); final ServerAuthHandler authHandler = ServerAuthHandler.NO_OP; final ExecutorService exec = Executors.newCachedThreadPool(); - final BindableService flightBindingService = FlightGrpcUtils.createFlightService(allocator, producer, - authHandler, exec); + final BindableService flightBindingService = + FlightGrpcUtils.createFlightService(allocator, producer, authHandler, exec); - //initializes server with 2 services - FlightBindingService & TestService + // initializes server with 2 services - FlightBindingService & TestService serverName = InProcessServerBuilder.generateName(); - server = InProcessServerBuilder.forName(serverName) - .directExecutor() - .addService(flightBindingService) - .addService(new TestServiceAdapter()) - .build(); + server = + InProcessServerBuilder.forName(serverName) + .directExecutor() + .addService(flightBindingService) + .addService(new TestServiceAdapter()) + .build(); server.start(); } @@ -75,39 +71,45 @@ public void cleanup() { } /** - * This test checks if multiple gRPC services can be added to the same - * server endpoint and if they can be used by different clients via the same channel. + * This test checks if multiple gRPC services can be added to the same server endpoint and if they + * can be used by different clients via the same channel. + * * @throws IOException If server fails to start. */ @Test public void testMultipleGrpcServices() throws IOException { - //Initializes channel so that multiple clients can communicate with server - final ManagedChannel managedChannel = InProcessChannelBuilder.forName(serverName) - .directExecutor() - .build(); + // Initializes channel so that multiple clients can communicate with server + final ManagedChannel managedChannel = + InProcessChannelBuilder.forName(serverName).directExecutor().build(); - //Defines flight client and calls service method. Since we use a NoOpFlightProducer we expect the service - //to throw a RunTimeException + // Defines flight client and calls service method. Since we use a NoOpFlightProducer we expect + // the service + // to throw a RunTimeException final FlightClient flightClient = FlightGrpcUtils.createFlightClient(allocator, managedChannel); final Iterable actionTypes = flightClient.listActions(); - assertThrows(FlightRuntimeException.class, () -> actionTypes.forEach( - actionType -> System.out.println(actionType.toString()))); - - //Define Test client as a blocking stub and call test method which correctly returns an empty protobuf object - final TestServiceGrpc.TestServiceBlockingStub blockingStub = TestServiceGrpc.newBlockingStub(managedChannel); - Assertions.assertEquals(Empty.newBuilder().build(), blockingStub.test(Empty.newBuilder().build())); + assertThrows( + FlightRuntimeException.class, + () -> actionTypes.forEach(actionType -> System.out.println(actionType.toString()))); + + // Define Test client as a blocking stub and call test method which correctly returns an empty + // protobuf object + final TestServiceGrpc.TestServiceBlockingStub blockingStub = + TestServiceGrpc.newBlockingStub(managedChannel); + Assertions.assertEquals( + Empty.newBuilder().build(), blockingStub.test(Empty.newBuilder().build())); } @Test public void testShutdown() throws IOException, InterruptedException { - //Initializes channel so that multiple clients can communicate with server - final ManagedChannel managedChannel = InProcessChannelBuilder.forName(serverName) - .directExecutor() - .build(); + // Initializes channel so that multiple clients can communicate with server + final ManagedChannel managedChannel = + InProcessChannelBuilder.forName(serverName).directExecutor().build(); - //Defines flight client and calls service method. Since we use a NoOpFlightProducer we expect the service - //to throw a RunTimeException - final FlightClient flightClient = FlightGrpcUtils.createFlightClientWithSharedChannel(allocator, managedChannel); + // Defines flight client and calls service method. Since we use a NoOpFlightProducer we expect + // the service + // to throw a RunTimeException + final FlightClient flightClient = + FlightGrpcUtils.createFlightClientWithSharedChannel(allocator, managedChannel); // Should be a no-op. flightClient.close(); @@ -119,10 +121,9 @@ public void testShutdown() throws IOException, InterruptedException { @Test public void testProxyChannel() throws IOException, InterruptedException { - //Initializes channel so that multiple clients can communicate with server - final ManagedChannel managedChannel = InProcessChannelBuilder.forName(serverName) - .directExecutor() - .build(); + // Initializes channel so that multiple clients can communicate with server + final ManagedChannel managedChannel = + InProcessChannelBuilder.forName(serverName).directExecutor().build(); final FlightGrpcUtils.NonClosingProxyManagedChannel proxyChannel = new FlightGrpcUtils.NonClosingProxyManagedChannel(managedChannel); @@ -148,10 +149,9 @@ public void testProxyChannel() throws IOException, InterruptedException { @Test public void testProxyChannelWithClosedChannel() throws IOException, InterruptedException { - //Initializes channel so that multiple clients can communicate with server - final ManagedChannel managedChannel = InProcessChannelBuilder.forName(serverName) - .directExecutor() - .build(); + // Initializes channel so that multiple clients can communicate with server + final ManagedChannel managedChannel = + InProcessChannelBuilder.forName(serverName).directExecutor().build(); final FlightGrpcUtils.NonClosingProxyManagedChannel proxyChannel = new FlightGrpcUtils.NonClosingProxyManagedChannel(managedChannel); @@ -173,13 +173,12 @@ public void testProxyChannelWithClosedChannel() throws IOException, InterruptedE Assertions.assertEquals(ConnectivityState.SHUTDOWN, managedChannel.getState(false)); } - /** - * Private class used for testing purposes that overrides service behavior. - */ + /** Private class used for testing purposes that overrides service behavior. */ private static class TestServiceAdapter extends TestServiceGrpc.TestServiceImplBase { /** * gRPC service that receives an empty object & returns and empty protobuf object. + * * @param request google.protobuf.Empty * @param responseObserver google.protobuf.Empty */ @@ -190,4 +189,3 @@ public void test(Empty request, StreamObserver responseObserver) { } } } - diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestFlightService.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestFlightService.java index de1b7750da3..5ea0f52bfc2 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestFlightService.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestFlightService.java @@ -21,10 +21,10 @@ import static org.apache.arrow.flight.Location.forGrpcInsecure; import static org.junit.jupiter.api.Assertions.fail; +import io.grpc.stub.ServerCallStreamObserver; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.Optional; - import org.apache.arrow.flight.impl.Flight; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -36,8 +36,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import io.grpc.stub.ServerCallStreamObserver; - public class TestFlightService { private BufferAllocator allocator; @@ -54,76 +52,63 @@ public void cleanup() throws Exception { @Test public void testFlightServiceWithNoAuthHandlerOrInterceptors() { - // This test is for ARROW-10491. There was a bug where FlightService would try to access the RequestContext, - // but the RequestContext was getting set to null because no interceptors were active to initialize it + // This test is for ARROW-10491. There was a bug where FlightService would try to access the + // RequestContext, + // but the RequestContext was getting set to null because no interceptors were active to + // initialize it // when using FlightService directly rather than starting up a FlightServer. // Arrange - final FlightProducer producer = new NoOpFlightProducer() { - @Override - public void getStream(CallContext context, Ticket ticket, - ServerStreamListener listener) { - listener.completed(); - } - }; + final FlightProducer producer = + new NoOpFlightProducer() { + @Override + public void getStream(CallContext context, Ticket ticket, ServerStreamListener listener) { + listener.completed(); + } + }; // This response observer notifies that the test failed if onError() is called. - final ServerCallStreamObserver observer = new ServerCallStreamObserver() { - @Override - public boolean isCancelled() { - return false; - } - - @Override - public void setOnCancelHandler(Runnable runnable) { - - } - - @Override - public void setCompression(String s) { - - } - - @Override - public boolean isReady() { - return false; - } - - @Override - public void setOnReadyHandler(Runnable runnable) { - - } - - @Override - public void disableAutoInboundFlowControl() { + final ServerCallStreamObserver observer = + new ServerCallStreamObserver() { + @Override + public boolean isCancelled() { + return false; + } - } + @Override + public void setOnCancelHandler(Runnable runnable) {} - @Override - public void request(int i) { + @Override + public void setCompression(String s) {} - } + @Override + public boolean isReady() { + return false; + } - @Override - public void setMessageCompression(boolean b) { + @Override + public void setOnReadyHandler(Runnable runnable) {} - } + @Override + public void disableAutoInboundFlowControl() {} - @Override - public void onNext(ArrowMessage arrowMessage) { + @Override + public void request(int i) {} - } + @Override + public void setMessageCompression(boolean b) {} - @Override - public void onError(Throwable throwable) { - fail(throwable); - } + @Override + public void onNext(ArrowMessage arrowMessage) {} - @Override - public void onCompleted() { + @Override + public void onError(Throwable throwable) { + fail(throwable); + } - } - }; + @Override + public void onCompleted() {} + }; final FlightService flightService = new FlightService(allocator, producer, null, null); // Act @@ -133,28 +118,37 @@ public void onCompleted() { } @Test - public void supportsNullSchemas() throws Exception - { - final FlightProducer producer = new NoOpFlightProducer() { - @Override - public FlightInfo getFlightInfo(CallContext context, - FlightDescriptor descriptor) { - return new FlightInfo(null, descriptor, Collections.emptyList(), - 0, 0, false, IpcOption.DEFAULT, "foo".getBytes(StandardCharsets.UTF_8)); - } - }; + public void supportsNullSchemas() throws Exception { + final FlightProducer producer = + new NoOpFlightProducer() { + @Override + public FlightInfo getFlightInfo(CallContext context, FlightDescriptor descriptor) { + return new FlightInfo( + null, + descriptor, + Collections.emptyList(), + 0, + 0, + false, + IpcOption.DEFAULT, + "foo".getBytes(StandardCharsets.UTF_8)); + } + }; try (final FlightServer s = - FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), producer).build().start(); - final FlightClient client = FlightClient.builder(allocator, s.getLocation()).build()) { + FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), producer) + .build() + .start(); + final FlightClient client = FlightClient.builder(allocator, s.getLocation()).build()) { FlightInfo flightInfo = client.getInfo(FlightDescriptor.path("test")); Assertions.assertEquals(Optional.empty(), flightInfo.getSchemaOptional()); Assertions.assertEquals(new Schema(Collections.emptyList()), flightInfo.getSchema()); - Assertions.assertArrayEquals(flightInfo.getAppMetadata(), "foo".getBytes(StandardCharsets.UTF_8)); + Assertions.assertArrayEquals( + flightInfo.getAppMetadata(), "foo".getBytes(StandardCharsets.UTF_8)); - Exception e = Assertions.assertThrows( - FlightRuntimeException.class, - () -> client.getSchema(FlightDescriptor.path("test"))); + Exception e = + Assertions.assertThrows( + FlightRuntimeException.class, () -> client.getSchema(FlightDescriptor.path("test"))); Assertions.assertEquals("No schema is present in FlightInfo", e.getMessage()); } } diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestLargeMessage.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestLargeMessage.java index 430dc29a7d0..2f02a25e64d 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestLargeMessage.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestLargeMessage.java @@ -24,7 +24,6 @@ import java.util.Arrays; import java.util.List; import java.util.stream.Stream; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.IntVector; @@ -37,17 +36,16 @@ import org.junit.jupiter.api.Test; public class TestLargeMessage { - /** - * Make sure a Flight client accepts large message payloads by default. - */ + /** Make sure a Flight client accepts large message payloads by default. */ @Test public void getLargeMessage() throws Exception { try (final BufferAllocator a = new RootAllocator(Long.MAX_VALUE); - final Producer producer = new Producer(a); - final FlightServer s = FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), producer).build().start()) { + final Producer producer = new Producer(a); + final FlightServer s = + FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), producer).build().start()) { try (FlightClient client = FlightClient.builder(a, s.getLocation()).build()) { - try (FlightStream stream = client.getStream(new Ticket(new byte[]{})); + try (FlightStream stream = client.getStream(new Ticket(new byte[] {})); VectorSchemaRoot root = stream.getRoot()) { while (stream.next()) { for (final Field field : root.getSchema().getFields()) { @@ -64,19 +62,18 @@ public void getLargeMessage() throws Exception { } } - /** - * Make sure a Flight server accepts large message payloads by default. - */ + /** Make sure a Flight server accepts large message payloads by default. */ @Test public void putLargeMessage() throws Exception { try (final BufferAllocator a = new RootAllocator(Long.MAX_VALUE); - final Producer producer = new Producer(a); - final FlightServer s = FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), producer).build().start()) { + final Producer producer = new Producer(a); + final FlightServer s = + FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), producer).build().start()) { try (FlightClient client = FlightClient.builder(a, s.getLocation()).build(); - BufferAllocator testAllocator = a.newChildAllocator("testcase", 0, Long.MAX_VALUE); - VectorSchemaRoot root = generateData(testAllocator)) { - final FlightClient.ClientStreamListener listener = client.startPut(FlightDescriptor.path("hello"), root, - new AsyncPutListener()); + BufferAllocator testAllocator = a.newChildAllocator("testcase", 0, Long.MAX_VALUE); + VectorSchemaRoot root = generateData(testAllocator)) { + final FlightClient.ClientStreamListener listener = + client.startPut(FlightDescriptor.path("hello"), root, new AsyncPutListener()); listener.putNext(); listener.completed(); listener.getResult(); @@ -86,10 +83,13 @@ public void putLargeMessage() throws Exception { private static VectorSchemaRoot generateData(BufferAllocator allocator) { final int size = 128 * 1024; - final List fieldNames = Arrays.asList("c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10"); - final Stream fields = fieldNames - .stream() - .map(fieldName -> new Field(fieldName, FieldType.nullable(new ArrowType.Int(32, true)), null)); + final List fieldNames = + Arrays.asList("c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10"); + final Stream fields = + fieldNames.stream() + .map( + fieldName -> + new Field(fieldName, FieldType.nullable(new ArrowType.Int(32, true)), null)); final Schema schema = new Schema(fields.collect(toImmutableList()), null); final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); @@ -113,8 +113,7 @@ private static class Producer implements FlightProducer, AutoCloseable { } @Override - public void getStream(CallContext context, Ticket ticket, - ServerStreamListener listener) { + public void getStream(CallContext context, Ticket ticket, ServerStreamListener listener) { try (VectorSchemaRoot root = generateData(allocator)) { listener.start(root); listener.putNext(); @@ -123,19 +122,17 @@ public void getStream(CallContext context, Ticket ticket, } @Override - public void listFlights(CallContext context, Criteria criteria, - StreamListener listener) { - - } + public void listFlights( + CallContext context, Criteria criteria, StreamListener listener) {} @Override - public FlightInfo getFlightInfo(CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfo(CallContext context, FlightDescriptor descriptor) { return null; } @Override - public Runnable acceptPut(CallContext context, FlightStream flightStream, StreamListener ackStream) { + public Runnable acceptPut( + CallContext context, FlightStream flightStream, StreamListener ackStream) { return () -> { try (VectorSchemaRoot root = flightStream.getRoot()) { while (flightStream.next()) { @@ -146,16 +143,12 @@ public Runnable acceptPut(CallContext context, FlightStream flightStream, Stream } @Override - public void doAction(CallContext context, Action action, - StreamListener listener) { + public void doAction(CallContext context, Action action, StreamListener listener) { listener.onCompleted(); } @Override - public void listActions(CallContext context, - StreamListener listener) { - - } + public void listActions(CallContext context, StreamListener listener) {} @Override public void close() throws Exception { diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestLeak.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestLeak.java index 1e4d2470c6e..22e2dd9c381 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestLeak.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestLeak.java @@ -23,7 +23,6 @@ import java.util.Arrays; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.Float8Vector; @@ -34,50 +33,53 @@ import org.apache.arrow.vector.types.pojo.Schema; import org.junit.jupiter.api.Test; -/** - * Tests for scenarios where Flight could leak memory. - */ +/** Tests for scenarios where Flight could leak memory. */ public class TestLeak { private static final int ROWS = 2048; private static Schema getSchema() { - return new Schema(Arrays.asList( - Field.nullable("0", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), - Field.nullable("1", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), - Field.nullable("2", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), - Field.nullable("3", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), - Field.nullable("4", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), - Field.nullable("5", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), - Field.nullable("6", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), - Field.nullable("7", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), - Field.nullable("8", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), - Field.nullable("9", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), - Field.nullable("10", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)) - )); + return new Schema( + Arrays.asList( + Field.nullable("0", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), + Field.nullable("1", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), + Field.nullable("2", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), + Field.nullable("3", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), + Field.nullable("4", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), + Field.nullable("5", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), + Field.nullable("6", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), + Field.nullable("7", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), + Field.nullable("8", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), + Field.nullable("9", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), + Field.nullable("10", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)))); } /** * Ensure that if the client cancels, the server does not leak memory. * - *

In gRPC, canceling the stream from the client sends an event to the server. Once processed, gRPC will start - * silently rejecting messages sent by the server. However, Flight depends on gRPC processing these messages in order - * to free the associated memory. + *

In gRPC, canceling the stream from the client sends an event to the server. Once processed, + * gRPC will start silently rejecting messages sent by the server. However, Flight depends on gRPC + * processing these messages in order to free the associated memory. */ @Test public void testCancelingDoGetDoesNotLeak() throws Exception { final CountDownLatch callFinished = new CountDownLatch(1); try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - final FlightServer s = FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), - new LeakFlightProducer(allocator, callFinished)) - .build().start(); - final FlightClient client = FlightClient.builder(allocator, s.getLocation()).build()) { + final FlightServer s = + FlightServer.builder( + allocator, + forGrpcInsecure(LOCALHOST, 0), + new LeakFlightProducer(allocator, callFinished)) + .build() + .start(); + final FlightClient client = FlightClient.builder(allocator, s.getLocation()).build()) { final FlightStream stream = client.getStream(new Ticket(new byte[0])); stream.getRoot(); stream.cancel("Cancel", null); - // Wait for the call to finish. (Closing the allocator while a call is ongoing is a guaranteed leak.) + // Wait for the call to finish. (Closing the allocator while a call is ongoing is a guaranteed + // leak.) callFinished.await(60, TimeUnit.SECONDS); s.shutdown(); @@ -89,15 +91,20 @@ public void testCancelingDoGetDoesNotLeak() throws Exception { public void testCancelingDoPutDoesNotBlock() throws Exception { final CountDownLatch callFinished = new CountDownLatch(1); try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - final FlightServer s = FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), - new LeakFlightProducer(allocator, callFinished)) - .build().start(); - final FlightClient client = FlightClient.builder(allocator, s.getLocation()).build()) { + final FlightServer s = + FlightServer.builder( + allocator, + forGrpcInsecure(LOCALHOST, 0), + new LeakFlightProducer(allocator, callFinished)) + .build() + .start(); + final FlightClient client = FlightClient.builder(allocator, s.getLocation()).build()) { try (final VectorSchemaRoot root = VectorSchemaRoot.create(getSchema(), allocator)) { final FlightDescriptor descriptor = FlightDescriptor.command(new byte[0]); final SyncPutListener listener = new SyncPutListener(); - final FlightClient.ClientStreamListener stream = client.startPut(descriptor, root, listener); + final FlightClient.ClientStreamListener stream = + client.startPut(descriptor, root, listener); // Wait for the server to cancel callFinished.await(60, TimeUnit.SECONDS); @@ -109,7 +116,8 @@ public void testCancelingDoPutDoesNotBlock() throws Exception { } } root.setRowCount(ROWS); - // Unlike DoGet, this method fairly reliably will write the message to the stream, so even without the fix + // Unlike DoGet, this method fairly reliably will write the message to the stream, so even + // without the fix // for ARROW-7343, this won't leak memory. // However, it will block if FlightClient doesn't check for cancellation. stream.putNext(); @@ -121,9 +129,7 @@ public void testCancelingDoPutDoesNotBlock() throws Exception { } } - /** - * A FlightProducer that always produces a fixed data stream with metadata on the side. - */ + /** A FlightProducer that always produces a fixed data stream with metadata on the side. */ private static class LeakFlightProducer extends NoOpFlightProducer { private final BufferAllocator allocator; @@ -143,35 +149,38 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l // We can't poll listener#isCancelled since gRPC has two distinct "is cancelled" flags. // TODO: should we continue leaking gRPC semantics? Can we even avoid this? - listener.setOnCancelHandler(() -> { - try { - for (int col = 0; col < 11; col++) { - final Float8Vector vector = (Float8Vector) root.getVector(Integer.toString(col)); - vector.allocateNew(); - for (int row = 0; row < ROWS; row++) { - vector.setSafe(row, 10.); + listener.setOnCancelHandler( + () -> { + try { + for (int col = 0; col < 11; col++) { + final Float8Vector vector = (Float8Vector) root.getVector(Integer.toString(col)); + vector.allocateNew(); + for (int row = 0; row < ROWS; row++) { + vector.setSafe(row, 10.); + } + } + root.setRowCount(ROWS); + // Once the call is "really cancelled" (setOnCancelListener has run/is running), this + // call is actually a + // no-op on the gRPC side and will leak the ArrowMessage unless Flight checks for + // this. + listener.putNext(); + listener.completed(); + } finally { + try { + root.close(); + childAllocator.close(); + } finally { + // Don't let the test hang if we throw above + callFinished.countDown(); + } } - } - root.setRowCount(ROWS); - // Once the call is "really cancelled" (setOnCancelListener has run/is running), this call is actually a - // no-op on the gRPC side and will leak the ArrowMessage unless Flight checks for this. - listener.putNext(); - listener.completed(); - } finally { - try { - root.close(); - childAllocator.close(); - } finally { - // Don't let the test hang if we throw above - callFinished.countDown(); - } - } - }); + }); } @Override - public Runnable acceptPut(CallContext context, - FlightStream flightStream, StreamListener ackStream) { + public Runnable acceptPut( + CallContext context, FlightStream flightStream, StreamListener ackStream) { return () -> { flightStream.getRoot(); ackStream.onError(CallStatus.CANCELLED.withDescription("CANCELLED").toRuntimeException()); diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestMetadataVersion.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestMetadataVersion.java index 0d3f7d4ff84..79e7b8229e4 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestMetadataVersion.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestMetadataVersion.java @@ -26,7 +26,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.Optional; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.IntVector; @@ -41,9 +40,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -/** - * Test clients/servers with different metadata versions. - */ +/** Test clients/servers with different metadata versions. */ public class TestMetadataVersion { private static BufferAllocator allocator; private static Schema schema; @@ -54,9 +51,12 @@ public class TestMetadataVersion { @BeforeAll public static void setUpClass() { allocator = new RootAllocator(Integer.MAX_VALUE); - schema = new Schema(Collections.singletonList(Field.nullable("foo", new ArrowType.Int(32, true)))); - unionSchema = new Schema( - Collections.singletonList(Field.nullable("union", new ArrowType.Union(UnionMode.Dense, new int[]{0})))); + schema = + new Schema(Collections.singletonList(Field.nullable("foo", new ArrowType.Int(32, true)))); + unionSchema = + new Schema( + Collections.singletonList( + Field.nullable("union", new ArrowType.Union(UnionMode.Dense, new int[] {0})))); // avoid writing legacy ipc format by default optionV4 = new IpcOption(false, MetadataVersion.V4); @@ -71,7 +71,7 @@ public static void tearDownClass() { @Test public void testGetFlightInfoV4() throws Exception { try (final FlightServer server = startServer(optionV4); - final FlightClient client = connect(server)) { + final FlightClient client = connect(server)) { final FlightInfo result = client.getInfo(FlightDescriptor.command(new byte[0])); assertEquals(Optional.of(schema), result.getSchemaOptional()); } @@ -80,7 +80,7 @@ public void testGetFlightInfoV4() throws Exception { @Test public void testGetSchemaV4() throws Exception { try (final FlightServer server = startServer(optionV4); - final FlightClient client = connect(server)) { + final FlightClient client = connect(server)) { final SchemaResult result = client.getSchema(FlightDescriptor.command(new byte[0])); assertEquals(schema, result.getSchema()); } @@ -89,32 +89,43 @@ public void testGetSchemaV4() throws Exception { @Test public void testUnionCheck() throws Exception { assertThrows(IllegalArgumentException.class, () -> new SchemaResult(unionSchema, optionV4)); - assertThrows(IllegalArgumentException.class, () -> - new FlightInfo(unionSchema, FlightDescriptor.command(new byte[0]), Collections.emptyList(), -1, -1, optionV4)); + assertThrows( + IllegalArgumentException.class, + () -> + new FlightInfo( + unionSchema, + FlightDescriptor.command(new byte[0]), + Collections.emptyList(), + -1, + -1, + optionV4)); try (final FlightServer server = startServer(optionV4); - final FlightClient client = connect(server); - final FlightStream stream = client.getStream(new Ticket("union".getBytes(StandardCharsets.UTF_8)))) { + final FlightClient client = connect(server); + final FlightStream stream = + client.getStream(new Ticket("union".getBytes(StandardCharsets.UTF_8)))) { final FlightRuntimeException err = assertThrows(FlightRuntimeException.class, stream::next); - assertTrue(err.getMessage().contains("Cannot write union with V4 metadata"), err.getMessage()); + assertTrue( + err.getMessage().contains("Cannot write union with V4 metadata"), err.getMessage()); } try (final FlightServer server = startServer(optionV4); - final FlightClient client = connect(server); - final VectorSchemaRoot root = VectorSchemaRoot.create(unionSchema, allocator)) { + final FlightClient client = connect(server); + final VectorSchemaRoot root = VectorSchemaRoot.create(unionSchema, allocator)) { final FlightDescriptor descriptor = FlightDescriptor.command(new byte[0]); final SyncPutListener reader = new SyncPutListener(); final FlightClient.ClientStreamListener listener = client.startPut(descriptor, reader); - final IllegalArgumentException err = assertThrows(IllegalArgumentException.class, - () -> listener.start(root, null, optionV4)); - assertTrue(err.getMessage().contains("Cannot write union with V4 metadata"), err.getMessage()); + final IllegalArgumentException err = + assertThrows(IllegalArgumentException.class, () -> listener.start(root, null, optionV4)); + assertTrue( + err.getMessage().contains("Cannot write union with V4 metadata"), err.getMessage()); } } @Test public void testPutV4() throws Exception { try (final FlightServer server = startServer(optionV4); - final FlightClient client = connect(server); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final FlightClient client = connect(server); + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { generateData(root); final FlightDescriptor descriptor = FlightDescriptor.command(new byte[0]); final SyncPutListener reader = new SyncPutListener(); @@ -129,8 +140,8 @@ public void testPutV4() throws Exception { @Test public void testGetV4() throws Exception { try (final FlightServer server = startServer(optionV4); - final FlightClient client = connect(server); - final FlightStream stream = client.getStream(new Ticket(new byte[0]))) { + final FlightClient client = connect(server); + final FlightStream stream = client.getStream(new Ticket(new byte[0]))) { assertTrue(stream.next()); assertEquals(optionV4.metadataVersion, stream.metadataVersion); validateRoot(stream.getRoot()); @@ -141,9 +152,10 @@ public void testGetV4() throws Exception { @Test public void testExchangeV4ToV5() throws Exception { try (final FlightServer server = startServer(optionV5); - final FlightClient client = connect(server); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); - final FlightClient.ExchangeReaderWriter stream = client.doExchange(FlightDescriptor.command(new byte[0]))) { + final FlightClient client = connect(server); + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); + final FlightClient.ExchangeReaderWriter stream = + client.doExchange(FlightDescriptor.command(new byte[0]))) { stream.getWriter().start(root, null, optionV4); generateData(root); stream.getWriter().putNext(); @@ -158,9 +170,10 @@ public void testExchangeV4ToV5() throws Exception { @Test public void testExchangeV5ToV4() throws Exception { try (final FlightServer server = startServer(optionV4); - final FlightClient client = connect(server); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); - final FlightClient.ExchangeReaderWriter stream = client.doExchange(FlightDescriptor.command(new byte[0]))) { + final FlightClient client = connect(server); + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); + final FlightClient.ExchangeReaderWriter stream = + client.doExchange(FlightDescriptor.command(new byte[0]))) { stream.getWriter().start(root, null, optionV5); generateData(root); stream.getWriter().putNext(); @@ -175,9 +188,10 @@ public void testExchangeV5ToV4() throws Exception { @Test public void testExchangeV4ToV4() throws Exception { try (final FlightServer server = startServer(optionV4); - final FlightClient client = connect(server); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); - final FlightClient.ExchangeReaderWriter stream = client.doExchange(FlightDescriptor.command(new byte[0]))) { + final FlightClient client = connect(server); + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); + final FlightClient.ExchangeReaderWriter stream = + client.doExchange(FlightDescriptor.command(new byte[0]))) { stream.getWriter().start(root, null, optionV4); generateData(root); stream.getWriter().putNext(); @@ -245,10 +259,17 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l try (final VectorSchemaRoot root = VectorSchemaRoot.create(unionSchema, allocator)) { listener.start(root, null, option); } catch (IllegalArgumentException e) { - listener.error(CallStatus.INTERNAL.withCause(e).withDescription(e.getMessage()).toRuntimeException()); + listener.error( + CallStatus.INTERNAL + .withCause(e) + .withDescription(e.getMessage()) + .toRuntimeException()); return; } - listener.error(CallStatus.INTERNAL.withDescription("Expected exception not raised").toRuntimeException()); + listener.error( + CallStatus.INTERNAL + .withDescription("Expected exception not raised") + .toRuntimeException()); return; } try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { @@ -260,7 +281,8 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l } @Override - public Runnable acceptPut(CallContext context, FlightStream flightStream, StreamListener ackStream) { + public Runnable acceptPut( + CallContext context, FlightStream flightStream, StreamListener ackStream) { return () -> { try { assertTrue(flightStream.next()); @@ -269,17 +291,19 @@ public Runnable acceptPut(CallContext context, FlightStream flightStream, Stream } catch (AssertionError err) { // gRPC doesn't propagate stack traces across the wire. err.printStackTrace(); - ackStream.onError(CallStatus.INVALID_ARGUMENT - .withCause(err) - .withDescription("Server assertion failed: " + err) - .toRuntimeException()); + ackStream.onError( + CallStatus.INVALID_ARGUMENT + .withCause(err) + .withDescription("Server assertion failed: " + err) + .toRuntimeException()); return; } catch (RuntimeException err) { err.printStackTrace(); - ackStream.onError(CallStatus.INTERNAL - .withCause(err) - .withDescription("Server assertion failed: " + err) - .toRuntimeException()); + ackStream.onError( + CallStatus.INTERNAL + .withCause(err) + .withDescription("Server assertion failed: " + err) + .toRuntimeException()); return; } ackStream.onCompleted(); @@ -296,17 +320,19 @@ public void doExchange(CallContext context, FlightStream reader, ServerStreamLis } catch (AssertionError err) { // gRPC doesn't propagate stack traces across the wire. err.printStackTrace(); - writer.error(CallStatus.INVALID_ARGUMENT - .withCause(err) - .withDescription("Server assertion failed: " + err) - .toRuntimeException()); + writer.error( + CallStatus.INVALID_ARGUMENT + .withCause(err) + .withDescription("Server assertion failed: " + err) + .toRuntimeException()); return; } catch (RuntimeException err) { err.printStackTrace(); - writer.error(CallStatus.INTERNAL - .withCause(err) - .withDescription("Server assertion failed: " + err) - .toRuntimeException()); + writer.error( + CallStatus.INTERNAL + .withCause(err) + .withDescription("Server assertion failed: " + err) + .toRuntimeException()); return; } diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerMiddleware.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerMiddleware.java index 3bc8f2f90a6..9f749d8bad7 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerMiddleware.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerMiddleware.java @@ -25,10 +25,7 @@ import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.function.BiConsumer; - import org.apache.arrow.flight.FlightClient.ClientStreamListener; -import org.apache.arrow.flight.FlightServerMiddleware.Factory; -import org.apache.arrow.flight.FlightServerMiddleware.Key; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.VectorSchemaRoot; @@ -38,44 +35,48 @@ public class TestServerMiddleware { - /** - * Make sure errors in DoPut are intercepted. - */ + /** Make sure errors in DoPut are intercepted. */ @Test public void doPutErrors() { test( new ErrorProducer(new RuntimeException("test")), (allocator, client) -> { final FlightDescriptor descriptor = FlightDescriptor.path("test"); - try (final VectorSchemaRoot root = VectorSchemaRoot.create(new Schema(Collections.emptyList()), allocator)) { - final ClientStreamListener listener = client.startPut(descriptor, root, new SyncPutListener()); + try (final VectorSchemaRoot root = + VectorSchemaRoot.create(new Schema(Collections.emptyList()), allocator)) { + final ClientStreamListener listener = + client.startPut(descriptor, root, new SyncPutListener()); listener.completed(); FlightTestUtil.assertCode(FlightStatusCode.INTERNAL, listener::getResult); } - }, (recorder) -> { + }, + (recorder) -> { final CallStatus status = recorder.statusFuture.get(); Assertions.assertNotNull(status); Assertions.assertNotNull(status.cause()); Assertions.assertEquals(FlightStatusCode.INTERNAL, status.code()); }); - // Check the status after server shutdown (to make sure gRPC finishes pending calls on the server side) + // Check the status after server shutdown (to make sure gRPC finishes pending calls on the + // server side) } - /** - * Make sure custom error codes in DoPut are intercepted. - */ + /** Make sure custom error codes in DoPut are intercepted. */ @Test public void doPutCustomCode() { test( - new ErrorProducer(CallStatus.UNAVAILABLE.withDescription("description").toRuntimeException()), + new ErrorProducer( + CallStatus.UNAVAILABLE.withDescription("description").toRuntimeException()), (allocator, client) -> { final FlightDescriptor descriptor = FlightDescriptor.path("test"); - try (final VectorSchemaRoot root = VectorSchemaRoot.create(new Schema(Collections.emptyList()), allocator)) { - final ClientStreamListener listener = client.startPut(descriptor, root, new SyncPutListener()); + try (final VectorSchemaRoot root = + VectorSchemaRoot.create(new Schema(Collections.emptyList()), allocator)) { + final ClientStreamListener listener = + client.startPut(descriptor, root, new SyncPutListener()); listener.completed(); FlightTestUtil.assertCode(FlightStatusCode.UNAVAILABLE, listener::getResult); } - }, (recorder) -> { + }, + (recorder) -> { final CallStatus status = recorder.statusFuture.get(); Assertions.assertNotNull(status); Assertions.assertNull(status.cause()); @@ -84,20 +85,22 @@ public void doPutCustomCode() { }); } - /** - * Make sure uncaught exceptions in DoPut are intercepted. - */ + /** Make sure uncaught exceptions in DoPut are intercepted. */ @Test public void doPutUncaught() { - test(new ServerErrorProducer(new RuntimeException("test")), + test( + new ServerErrorProducer(new RuntimeException("test")), (allocator, client) -> { final FlightDescriptor descriptor = FlightDescriptor.path("test"); - try (final VectorSchemaRoot root = VectorSchemaRoot.create(new Schema(Collections.emptyList()), allocator)) { - final ClientStreamListener listener = client.startPut(descriptor, root, new SyncPutListener()); + try (final VectorSchemaRoot root = + VectorSchemaRoot.create(new Schema(Collections.emptyList()), allocator)) { + final ClientStreamListener listener = + client.startPut(descriptor, root, new SyncPutListener()); listener.completed(); listener.getResult(); } - }, (recorder) -> { + }, + (recorder) -> { final CallStatus status = recorder.statusFuture.get(); final Throwable err = recorder.errFuture.get(); Assertions.assertNotNull(status); @@ -110,9 +113,11 @@ public void doPutUncaught() { @Test public void listFlightsUncaught() { - test(new ServerErrorProducer(new RuntimeException("test")), - (allocator, client) -> client.listFlights(new Criteria(new byte[0])).forEach((action) -> { - }), (recorder) -> { + test( + new ServerErrorProducer(new RuntimeException("test")), + (allocator, client) -> + client.listFlights(new Criteria(new byte[0])).forEach((action) -> {}), + (recorder) -> { final CallStatus status = recorder.statusFuture.get(); final Throwable err = recorder.errFuture.get(); Assertions.assertNotNull(status); @@ -125,9 +130,10 @@ public void listFlightsUncaught() { @Test public void doActionUncaught() { - test(new ServerErrorProducer(new RuntimeException("test")), - (allocator, client) -> client.doAction(new Action("test")).forEachRemaining(result -> { - }), (recorder) -> { + test( + new ServerErrorProducer(new RuntimeException("test")), + (allocator, client) -> client.doAction(new Action("test")).forEachRemaining(result -> {}), + (recorder) -> { final CallStatus status = recorder.statusFuture.get(); final Throwable err = recorder.errFuture.get(); Assertions.assertNotNull(status); @@ -140,9 +146,10 @@ public void doActionUncaught() { @Test public void listActionsUncaught() { - test(new ServerErrorProducer(new RuntimeException("test")), - (allocator, client) -> client.listActions().forEach(result -> { - }), (recorder) -> { + test( + new ServerErrorProducer(new RuntimeException("test")), + (allocator, client) -> client.listActions().forEach(result -> {}), + (recorder) -> { final CallStatus status = recorder.statusFuture.get(); final Throwable err = recorder.errFuture.get(); Assertions.assertNotNull(status); @@ -155,29 +162,34 @@ public void listActionsUncaught() { @Test public void getFlightInfoUncaught() { - test(new ServerErrorProducer(new RuntimeException("test")), + test( + new ServerErrorProducer(new RuntimeException("test")), (allocator, client) -> { - FlightTestUtil.assertCode(FlightStatusCode.INTERNAL, () -> client.getInfo(FlightDescriptor.path("test"))); - }, (recorder) -> { + FlightTestUtil.assertCode( + FlightStatusCode.INTERNAL, () -> client.getInfo(FlightDescriptor.path("test"))); + }, + (recorder) -> { final CallStatus status = recorder.statusFuture.get(); Assertions.assertNotNull(status); Assertions.assertEquals(FlightStatusCode.INTERNAL, status.code()); Assertions.assertNotNull(status.cause()); - Assertions.assertEquals(new RuntimeException("test").getMessage(), status.cause().getMessage()); + Assertions.assertEquals( + new RuntimeException("test").getMessage(), status.cause().getMessage()); }); } @Test public void doGetUncaught() { - test(new ServerErrorProducer(new RuntimeException("test")), + test( + new ServerErrorProducer(new RuntimeException("test")), (allocator, client) -> { try (final FlightStream stream = client.getStream(new Ticket(new byte[0]))) { - while (stream.next()) { - } + while (stream.next()) {} } catch (Exception e) { Assertions.fail(e.toString()); } - }, (recorder) -> { + }, + (recorder) -> { final CallStatus status = recorder.statusFuture.get(); final Throwable err = recorder.errFuture.get(); Assertions.assertNotNull(status); @@ -188,17 +200,14 @@ public void doGetUncaught() { }); } - /** - * A middleware that records the last error on any call. - */ + /** A middleware that records the last error on any call. */ static class ErrorRecorder implements FlightServerMiddleware { CompletableFuture statusFuture = new CompletableFuture<>(); CompletableFuture errFuture = new CompletableFuture<>(); @Override - public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) { - } + public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) {} @Override public void onCallCompleted(CallStatus status) { @@ -215,15 +224,14 @@ static class Factory implements FlightServerMiddleware.Factory { ErrorRecorder instance = new ErrorRecorder(); @Override - public ErrorRecorder onCallStarted(CallInfo info, CallHeaders incomingHeaders, RequestContext context) { + public ErrorRecorder onCallStarted( + CallInfo info, CallHeaders incomingHeaders, RequestContext context) { return instance; } } } - /** - * A producer that throws the given exception on a call. - */ + /** A producer that throws the given exception on a call. */ static class ErrorProducer extends NoOpFlightProducer { final RuntimeException error; @@ -233,18 +241,19 @@ static class ErrorProducer extends NoOpFlightProducer { } @Override - public Runnable acceptPut(CallContext context, FlightStream flightStream, StreamListener ackStream) { + public Runnable acceptPut( + CallContext context, FlightStream flightStream, StreamListener ackStream) { return () -> { // Drain queue to avoid FlightStream#close cancelling the call - while (flightStream.next()) { - } + while (flightStream.next()) {} throw error; }; } } /** - * A producer that throws the given exception on a call, but only after sending a success to the client. + * A producer that throws the given exception on a call, but only after sending a success to the + * client. */ static class ServerErrorProducer extends NoOpFlightProducer { @@ -257,7 +266,8 @@ static class ServerErrorProducer extends NoOpFlightProducer { @Override public void getStream(CallContext context, Ticket ticket, ServerStreamListener listener) { try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - final VectorSchemaRoot root = VectorSchemaRoot.create(new Schema(Collections.emptyList()), allocator)) { + final VectorSchemaRoot root = + VectorSchemaRoot.create(new Schema(Collections.emptyList()), allocator)) { listener.start(root); listener.completed(); } @@ -265,7 +275,8 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l } @Override - public void listFlights(CallContext context, Criteria criteria, StreamListener listener) { + public void listFlights( + CallContext context, Criteria criteria, StreamListener listener) { listener.onCompleted(); throw error; } @@ -276,10 +287,10 @@ public FlightInfo getFlightInfo(CallContext context, FlightDescriptor descriptor } @Override - public Runnable acceptPut(CallContext context, FlightStream flightStream, StreamListener ackStream) { + public Runnable acceptPut( + CallContext context, FlightStream flightStream, StreamListener ackStream) { return () -> { - while (flightStream.next()) { - } + while (flightStream.next()) {} ackStream.onCompleted(); throw error; }; @@ -303,7 +314,8 @@ static class ServerMiddlewarePair { final FlightServerMiddleware.Key key; final FlightServerMiddleware.Factory factory; - ServerMiddlewarePair(FlightServerMiddleware.Key key, FlightServerMiddleware.Factory factory) { + ServerMiddlewarePair( + FlightServerMiddleware.Key key, FlightServerMiddleware.Factory factory) { this.key = key; this.factory = factory; } @@ -317,15 +329,18 @@ static class ServerMiddlewarePair { * @param body A function to run as the body of the test. * @param The middleware type. */ - static void test(FlightProducer producer, List> middleware, + static void test( + FlightProducer producer, + List> middleware, BiConsumer body) { try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE)) { - final FlightServer.Builder builder = FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), producer); + final FlightServer.Builder builder = + FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), producer); middleware.forEach(pair -> builder.middleware(pair.key, pair.factory)); final FlightServer server = builder.build().start(); try (final FlightServer ignored = server; - final FlightClient client = FlightClient.builder(allocator, server.getLocation()).build() - ) { + final FlightClient client = + FlightClient.builder(allocator, server.getLocation()).build()) { body.accept(allocator, client); } } catch (InterruptedException | IOException e) { @@ -333,19 +348,25 @@ static void test(FlightProducer producer, Lis } } - static void test(FlightProducer producer, BiConsumer body, + static void test( + FlightProducer producer, + BiConsumer body, ErrorConsumer verify) { final ErrorRecorder.Factory factory = new ErrorRecorder.Factory(); - final List> middleware = Collections - .singletonList(new ServerMiddlewarePair<>(FlightServerMiddleware.Key.of("m"), factory)); - test(producer, middleware, (allocator, client) -> { - body.accept(allocator, client); - try { - verify.accept(factory.instance); - } catch (Exception e) { - throw new RuntimeException(e); - } - }); + final List> middleware = + Collections.singletonList( + new ServerMiddlewarePair<>(FlightServerMiddleware.Key.of("m"), factory)); + test( + producer, + middleware, + (allocator, client) -> { + body.accept(allocator, client); + try { + verify.accept(factory.instance); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); } @FunctionalInterface diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerOptions.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerOptions.java index f66b9e7fd26..5588627d5cd 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerOptions.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestServerOptions.java @@ -22,6 +22,15 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import io.grpc.Channel; +import io.grpc.MethodDescriptor; +import io.grpc.ServerServiceDefinition; +import io.grpc.health.v1.HealthCheckRequest; +import io.grpc.health.v1.HealthCheckResponse; +import io.grpc.health.v1.HealthGrpc; +import io.grpc.netty.NettyChannelBuilder; +import io.grpc.netty.NettyServerBuilder; +import io.grpc.protobuf.services.HealthStatusManager; import java.io.File; import java.util.HashMap; import java.util.Map; @@ -29,7 +38,6 @@ import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; - import org.apache.arrow.flight.TestBasicOperation.Producer; import org.apache.arrow.flight.auth.ServerAuthHandler; import org.apache.arrow.flight.impl.FlightServiceGrpc; @@ -41,16 +49,6 @@ import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Test; -import io.grpc.Channel; -import io.grpc.MethodDescriptor; -import io.grpc.ServerServiceDefinition; -import io.grpc.health.v1.HealthCheckRequest; -import io.grpc.health.v1.HealthCheckResponse; -import io.grpc.health.v1.HealthGrpc; -import io.grpc.netty.NettyChannelBuilder; -import io.grpc.netty.NettyServerBuilder; -import io.grpc.protobuf.services.HealthStatusManager; - public class TestServerOptions { @Test @@ -58,46 +56,46 @@ public void builderConsumer() throws Exception { final AtomicBoolean consumerCalled = new AtomicBoolean(); final Consumer consumer = (builder) -> consumerCalled.set(true); - try ( - BufferAllocator a = new RootAllocator(Long.MAX_VALUE); + try (BufferAllocator a = new RootAllocator(Long.MAX_VALUE); Producer producer = new Producer(a); - FlightServer s = FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), producer) - .transportHint("grpc.builderConsumer", consumer).build().start() - ) { + FlightServer s = + FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), producer) + .transportHint("grpc.builderConsumer", consumer) + .build() + .start()) { Assertions.assertTrue(consumerCalled.get()); } } /** - * Make sure that if Flight supplies a default executor to gRPC, then it is closed along with the server. + * Make sure that if Flight supplies a default executor to gRPC, then it is closed along with the + * server. */ @Test public void defaultExecutorClosed() throws Exception { final ExecutorService executor; - try ( - BufferAllocator a = new RootAllocator(Long.MAX_VALUE); - FlightServer server = FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), new NoOpFlightProducer()) - .build().start() - ) { + try (BufferAllocator a = new RootAllocator(Long.MAX_VALUE); + FlightServer server = + FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), new NoOpFlightProducer()) + .build() + .start()) { assertNotNull(server.grpcExecutor); executor = server.grpcExecutor; } Assertions.assertTrue(executor.isShutdown()); } - /** - * Make sure that if the user provides an executor to gRPC, then Flight does not close it. - */ + /** Make sure that if the user provides an executor to gRPC, then Flight does not close it. */ @Test public void suppliedExecutorNotClosed() throws Exception { final ExecutorService executor = Executors.newSingleThreadExecutor(); try { - try ( - BufferAllocator a = new RootAllocator(Long.MAX_VALUE); - FlightServer server = FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), new NoOpFlightProducer()) - .executor(executor) - .build().start() - ) { + try (BufferAllocator a = new RootAllocator(Long.MAX_VALUE); + FlightServer server = + FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), new NoOpFlightProducer()) + .executor(executor) + .build() + .start()) { Assertions.assertNull(server.grpcExecutor); } Assertions.assertFalse(executor.isShutdown()); @@ -108,18 +106,20 @@ public void suppliedExecutorNotClosed() throws Exception { @Test public void domainSocket() throws Exception { - Assumptions.assumeTrue(FlightTestUtil.isNativeTransportAvailable(), "We have a native transport available"); + Assumptions.assumeTrue( + FlightTestUtil.isNativeTransportAvailable(), "We have a native transport available"); final File domainSocket = File.createTempFile("flight-unit-test-", ".sock"); Assertions.assertTrue(domainSocket.delete()); - // Domain socket paths have a platform-dependent limit. Set a conservative limit and skip the test if the temporary - // file name is too long. (We do not assume a particular platform-dependent temporary directory path.) - Assumptions.assumeTrue(domainSocket.getAbsolutePath().length() < 100, "The domain socket path is not too long"); + // Domain socket paths have a platform-dependent limit. Set a conservative limit and skip the + // test if the temporary + // file name is too long. (We do not assume a particular platform-dependent temporary directory + // path.) + Assumptions.assumeTrue( + domainSocket.getAbsolutePath().length() < 100, "The domain socket path is not too long"); final Location location = Location.forGrpcDomainSocket(domainSocket.getAbsolutePath()); - try ( - BufferAllocator a = new RootAllocator(Long.MAX_VALUE); + try (BufferAllocator a = new RootAllocator(Long.MAX_VALUE); Producer producer = new Producer(a); - FlightServer s = FlightServer.builder(a, location, producer).build().start(); - ) { + FlightServer s = FlightServer.builder(a, location, producer).build().start(); ) { try (FlightClient c = FlightClient.builder(a, location).build()) { try (FlightStream stream = c.getStream(new Ticket(new byte[0]))) { VectorSchemaRoot root = stream.getRoot(); @@ -141,30 +141,45 @@ public void checkReflectionMetadata() { // This metadata is needed for gRPC reflection to work. final ExecutorService executorService = Executors.newSingleThreadExecutor(); try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE)) { - final FlightBindingService service = new FlightBindingService(allocator, new NoOpFlightProducer(), - ServerAuthHandler.NO_OP, executorService); + final FlightBindingService service = + new FlightBindingService( + allocator, new NoOpFlightProducer(), ServerAuthHandler.NO_OP, executorService); final ServerServiceDefinition definition = service.bindService(); - assertEquals(FlightServiceGrpc.getServiceDescriptor().getSchemaDescriptor(), + assertEquals( + FlightServiceGrpc.getServiceDescriptor().getSchemaDescriptor(), definition.getServiceDescriptor().getSchemaDescriptor()); final Map> definedMethods = new HashMap<>(); final Map> serviceMethods = new HashMap<>(); - // Make sure that the reflection metadata object is identical across all the places where it's accessible - definition.getMethods().forEach( - method -> definedMethods.put(method.getMethodDescriptor().getFullMethodName(), method.getMethodDescriptor())); - definition.getServiceDescriptor().getMethods().forEach( - method -> serviceMethods.put(method.getFullMethodName(), method)); - - for (final MethodDescriptor descriptor : FlightServiceGrpc.getServiceDescriptor().getMethods()) { + // Make sure that the reflection metadata object is identical across all the places where it's + // accessible + definition + .getMethods() + .forEach( + method -> + definedMethods.put( + method.getMethodDescriptor().getFullMethodName(), + method.getMethodDescriptor())); + definition + .getServiceDescriptor() + .getMethods() + .forEach(method -> serviceMethods.put(method.getFullMethodName(), method)); + + for (final MethodDescriptor descriptor : + FlightServiceGrpc.getServiceDescriptor().getMethods()) { final String methodName = descriptor.getFullMethodName(); - Assertions.assertTrue(definedMethods.containsKey(methodName), + Assertions.assertTrue( + definedMethods.containsKey(methodName), "Method is missing from ServerServiceDefinition: " + methodName); - Assertions.assertTrue(definedMethods.containsKey(methodName), + Assertions.assertTrue( + definedMethods.containsKey(methodName), "Method is missing from ServiceDescriptor: " + methodName); - assertEquals(descriptor.getSchemaDescriptor(), definedMethods.get(methodName).getSchemaDescriptor()); - assertEquals(descriptor.getSchemaDescriptor(), serviceMethods.get(methodName).getSchemaDescriptor()); + assertEquals( + descriptor.getSchemaDescriptor(), definedMethods.get(methodName).getSchemaDescriptor()); + assertEquals( + descriptor.getSchemaDescriptor(), serviceMethods.get(methodName).getSchemaDescriptor()); } } finally { executorService.shutdown(); @@ -178,20 +193,22 @@ public void checkReflectionMetadata() { @Test public void addHealthCheckService() throws Exception { final HealthStatusManager statusManager = new HealthStatusManager(); - final Consumer consumer = (builder) -> { - builder.addService(statusManager.getHealthService()); - }; + final Consumer consumer = + (builder) -> { + builder.addService(statusManager.getHealthService()); + }; final Location location = forGrpcInsecure(LOCALHOST, 5555); - try ( - BufferAllocator a = new RootAllocator(Long.MAX_VALUE); + try (BufferAllocator a = new RootAllocator(Long.MAX_VALUE); Producer producer = new Producer(a); - FlightServer s = FlightServer.builder(a, location, producer) - .transportHint("grpc.builderConsumer", consumer).build().start(); - ) { - Channel channel = NettyChannelBuilder.forAddress(location.toSocketAddress()).usePlaintext().build(); - HealthCheckResponse response = HealthGrpc - .newBlockingStub(channel) - .check(HealthCheckRequest.getDefaultInstance()); + FlightServer s = + FlightServer.builder(a, location, producer) + .transportHint("grpc.builderConsumer", consumer) + .build() + .start(); ) { + Channel channel = + NettyChannelBuilder.forAddress(location.toSocketAddress()).usePlaintext().build(); + HealthCheckResponse response = + HealthGrpc.newBlockingStub(channel).check(HealthCheckRequest.getDefaultInstance()); assertEquals(response.getStatus(), HealthCheckResponse.ServingStatus.SERVING); } diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestTls.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestTls.java index 75bc5f6e615..2f2951ae168 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestTls.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/TestTls.java @@ -26,94 +26,92 @@ import java.nio.charset.StandardCharsets; import java.util.Iterator; import java.util.function.Consumer; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -/** - * Tests for TLS in Flight. - */ +/** Tests for TLS in Flight. */ public class TestTls { - /** - * Test a basic request over TLS. - */ + /** Test a basic request over TLS. */ @Test public void connectTls() { - test((builder) -> { - try (final InputStream roots = new FileInputStream(FlightTestUtil.exampleTlsRootCert().toFile()); - final FlightClient client = builder.trustedCertificates(roots).build()) { - final Iterator responses = client.doAction(new Action("hello-world")); - final byte[] response = responses.next().getBody(); - Assertions.assertEquals("Hello, world!", new String(response, StandardCharsets.UTF_8)); - Assertions.assertFalse(responses.hasNext()); - } catch (InterruptedException | IOException e) { - throw new RuntimeException(e); - } - }); + test( + (builder) -> { + try (final InputStream roots = + new FileInputStream(FlightTestUtil.exampleTlsRootCert().toFile()); + final FlightClient client = builder.trustedCertificates(roots).build()) { + final Iterator responses = client.doAction(new Action("hello-world")); + final byte[] response = responses.next().getBody(); + Assertions.assertEquals("Hello, world!", new String(response, StandardCharsets.UTF_8)); + Assertions.assertFalse(responses.hasNext()); + } catch (InterruptedException | IOException e) { + throw new RuntimeException(e); + } + }); } - /** - * Make sure that connections are rejected when the root certificate isn't trusted. - */ + /** Make sure that connections are rejected when the root certificate isn't trusted. */ @Test public void rejectInvalidCert() { - test((builder) -> { - try (final FlightClient client = builder.build()) { - final Iterator responses = client.doAction(new Action("hello-world")); - FlightTestUtil.assertCode(FlightStatusCode.UNAVAILABLE, () -> responses.next().getBody()); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - }); + test( + (builder) -> { + try (final FlightClient client = builder.build()) { + final Iterator responses = client.doAction(new Action("hello-world")); + FlightTestUtil.assertCode( + FlightStatusCode.UNAVAILABLE, () -> responses.next().getBody()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + }); } - /** - * Make sure that connections are rejected when the hostname doesn't match. - */ + /** Make sure that connections are rejected when the hostname doesn't match. */ @Test public void rejectHostname() { - test((builder) -> { - try (final InputStream roots = new FileInputStream(FlightTestUtil.exampleTlsRootCert().toFile()); - final FlightClient client = builder.trustedCertificates(roots).overrideHostname("fakehostname") - .build()) { - final Iterator responses = client.doAction(new Action("hello-world")); - FlightTestUtil.assertCode(FlightStatusCode.UNAVAILABLE, () -> responses.next().getBody()); - } catch (InterruptedException | IOException e) { - throw new RuntimeException(e); - } - }); + test( + (builder) -> { + try (final InputStream roots = + new FileInputStream(FlightTestUtil.exampleTlsRootCert().toFile()); + final FlightClient client = + builder.trustedCertificates(roots).overrideHostname("fakehostname").build()) { + final Iterator responses = client.doAction(new Action("hello-world")); + FlightTestUtil.assertCode( + FlightStatusCode.UNAVAILABLE, () -> responses.next().getBody()); + } catch (InterruptedException | IOException e) { + throw new RuntimeException(e); + } + }); } - /** - * Test a basic request over TLS. - */ + /** Test a basic request over TLS. */ @Test public void connectTlsDisableServerVerification() { - test((builder) -> { - try (final FlightClient client = builder.verifyServer(false).build()) { - final Iterator responses = client.doAction(new Action("hello-world")); - final byte[] response = responses.next().getBody(); - Assertions.assertEquals("Hello, world!", new String(response, StandardCharsets.UTF_8)); - Assertions.assertFalse(responses.hasNext()); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - }); + test( + (builder) -> { + try (final FlightClient client = builder.verifyServer(false).build()) { + final Iterator responses = client.doAction(new Action("hello-world")); + final byte[] response = responses.next().getBody(); + Assertions.assertEquals("Hello, world!", new String(response, StandardCharsets.UTF_8)); + Assertions.assertFalse(responses.hasNext()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + }); } void test(Consumer testFn) { final FlightTestUtil.CertKeyPair certKey = FlightTestUtil.exampleTlsCerts().get(0); - try ( - BufferAllocator a = new RootAllocator(Long.MAX_VALUE); + try (BufferAllocator a = new RootAllocator(Long.MAX_VALUE); Producer producer = new Producer(); - FlightServer s = FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), producer) - .useTls(certKey.cert, certKey.key) - .build().start()) { - final FlightClient.Builder builder = FlightClient.builder(a, Location.forGrpcTls(FlightTestUtil.LOCALHOST, - s.getPort())); + FlightServer s = + FlightServer.builder(a, forGrpcInsecure(LOCALHOST, 0), producer) + .useTls(certKey.cert, certKey.key) + .build() + .start()) { + final FlightClient.Builder builder = + FlightClient.builder(a, Location.forGrpcTls(FlightTestUtil.LOCALHOST, s.getPort())); testFn.accept(builder); } catch (InterruptedException | IOException e) { throw new RuntimeException(e); @@ -129,12 +127,13 @@ public void doAction(CallContext context, Action action, StreamListener listener.onCompleted(); return; } - listener - .onError(CallStatus.UNIMPLEMENTED.withDescription("Invalid action " + action.getType()).toRuntimeException()); + listener.onError( + CallStatus.UNIMPLEMENTED + .withDescription("Invalid action " + action.getType()) + .toRuntimeException()); } @Override - public void close() { - } + public void close() {} } } diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/auth/TestBasicAuth.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/auth/TestBasicAuth.java index 176277866b7..d972d027f45 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/auth/TestBasicAuth.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/auth/TestBasicAuth.java @@ -20,11 +20,11 @@ import static org.apache.arrow.flight.FlightTestUtil.LOCALHOST; import static org.apache.arrow.flight.Location.forGrpcInsecure; +import com.google.common.collect.ImmutableList; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Optional; - import org.apache.arrow.flight.Criteria; import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightInfo; @@ -48,8 +48,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import com.google.common.collect.ImmutableList; - public class TestBasicAuth { private static final String USERNAME = "flight"; @@ -79,20 +77,26 @@ public void asyncCall() throws Exception { @Test public void invalidAuth() { - FlightTestUtil.assertCode(FlightStatusCode.UNAUTHENTICATED, () -> { - client.authenticateBasic(USERNAME, "WRONG"); - }); - - FlightTestUtil.assertCode(FlightStatusCode.UNAUTHENTICATED, () -> { - client.listFlights(Criteria.ALL).forEach(action -> Assertions.fail()); - }); + FlightTestUtil.assertCode( + FlightStatusCode.UNAUTHENTICATED, + () -> { + client.authenticateBasic(USERNAME, "WRONG"); + }); + + FlightTestUtil.assertCode( + FlightStatusCode.UNAUTHENTICATED, + () -> { + client.listFlights(Criteria.ALL).forEach(action -> Assertions.fail()); + }); } @Test public void didntAuth() { - FlightTestUtil.assertCode(FlightStatusCode.UNAUTHENTICATED, () -> { - client.listFlights(Criteria.ALL).forEach(action -> Assertions.fail()); - }); + FlightTestUtil.assertCode( + FlightStatusCode.UNAUTHENTICATED, + () -> { + client.listFlights(Criteria.ALL).forEach(action -> Assertions.fail()); + }); } @BeforeEach @@ -103,56 +107,65 @@ public void testSetup() throws IOException { @BeforeAll public static void setup() throws IOException { allocator = new RootAllocator(Long.MAX_VALUE); - final BasicServerAuthHandler.BasicAuthValidator validator = new BasicServerAuthHandler.BasicAuthValidator() { - - @Override - public Optional isValid(byte[] token) { - if (Arrays.equals(token, VALID_TOKEN)) { - return Optional.of(USERNAME); - } - return Optional.empty(); - } - - @Override - public byte[] getToken(String username, String password) { - if (USERNAME.equals(username) && PASSWORD.equals(password)) { - return VALID_TOKEN; - } else { - throw new IllegalArgumentException("invalid credentials"); - } - } - }; + final BasicServerAuthHandler.BasicAuthValidator validator = + new BasicServerAuthHandler.BasicAuthValidator() { - server = FlightServer.builder( - allocator, forGrpcInsecure(LOCALHOST, 0), - new NoOpFlightProducer() { @Override - public void listFlights(CallContext context, Criteria criteria, - StreamListener listener) { - if (!context.peerIdentity().equals(USERNAME)) { - listener.onError(new IllegalArgumentException("Invalid username")); - return; + public Optional isValid(byte[] token) { + if (Arrays.equals(token, VALID_TOKEN)) { + return Optional.of(USERNAME); } - listener.onCompleted(); + return Optional.empty(); } @Override - public void getStream(CallContext context, Ticket ticket, ServerStreamListener listener) { - if (!context.peerIdentity().equals(USERNAME)) { - listener.error(new IllegalArgumentException("Invalid username")); - return; - } - final Schema pojoSchema = new Schema(ImmutableList.of(Field.nullable("a", - Types.MinorType.BIGINT.getType()))); - try (VectorSchemaRoot root = VectorSchemaRoot.create(pojoSchema, allocator)) { - listener.start(root); - root.allocateNew(); - root.setRowCount(4095); - listener.putNext(); - listener.completed(); + public byte[] getToken(String username, String password) { + if (USERNAME.equals(username) && PASSWORD.equals(password)) { + return VALID_TOKEN; + } else { + throw new IllegalArgumentException("invalid credentials"); } } - }).authHandler(new BasicServerAuthHandler(validator)).build().start(); + }; + + server = + FlightServer.builder( + allocator, + forGrpcInsecure(LOCALHOST, 0), + new NoOpFlightProducer() { + @Override + public void listFlights( + CallContext context, Criteria criteria, StreamListener listener) { + if (!context.peerIdentity().equals(USERNAME)) { + listener.onError(new IllegalArgumentException("Invalid username")); + return; + } + listener.onCompleted(); + } + + @Override + public void getStream( + CallContext context, Ticket ticket, ServerStreamListener listener) { + if (!context.peerIdentity().equals(USERNAME)) { + listener.error(new IllegalArgumentException("Invalid username")); + return; + } + final Schema pojoSchema = + new Schema( + ImmutableList.of( + Field.nullable("a", Types.MinorType.BIGINT.getType()))); + try (VectorSchemaRoot root = VectorSchemaRoot.create(pojoSchema, allocator)) { + listener.start(root); + root.allocateNew(); + root.setRowCount(4095); + listener.putNext(); + listener.completed(); + } + } + }) + .authHandler(new BasicServerAuthHandler(validator)) + .build() + .start(); } @AfterEach @@ -167,5 +180,4 @@ public static void shutdown() throws Exception { allocator.getChildAllocators().forEach(BufferAllocator::close); AutoCloseables.close(allocator); } - } diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/auth2/TestBasicAuth2.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/auth2/TestBasicAuth2.java index cadd67d3ed2..366cfaf3756 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/auth2/TestBasicAuth2.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/auth2/TestBasicAuth2.java @@ -20,8 +20,9 @@ import static org.apache.arrow.flight.FlightTestUtil.LOCALHOST; import static org.apache.arrow.flight.Location.forGrpcInsecure; +import com.google.common.base.Strings; +import com.google.common.collect.ImmutableList; import java.io.IOException; - import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.Criteria; import org.apache.arrow.flight.FlightClient; @@ -46,9 +47,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import com.google.common.base.Strings; -import com.google.common.collect.ImmutableList; - public class TestBasicAuth2 { private static final String USERNAME_1 = "flight1"; @@ -70,9 +68,10 @@ public static void setup() throws Exception { private static FlightProducer getFlightProducer() { return new NoOpFlightProducer() { @Override - public void listFlights(CallContext context, Criteria criteria, - StreamListener listener) { - if (!context.peerIdentity().equals(USERNAME_1) && !context.peerIdentity().equals(USERNAME_2)) { + public void listFlights( + CallContext context, Criteria criteria, StreamListener listener) { + if (!context.peerIdentity().equals(USERNAME_1) + && !context.peerIdentity().equals(USERNAME_2)) { listener.onError(new IllegalArgumentException("Invalid username")); return; } @@ -81,12 +80,13 @@ public void listFlights(CallContext context, Criteria criteria, @Override public void getStream(CallContext context, Ticket ticket, ServerStreamListener listener) { - if (!context.peerIdentity().equals(USERNAME_1) && !context.peerIdentity().equals(USERNAME_2)) { + if (!context.peerIdentity().equals(USERNAME_1) + && !context.peerIdentity().equals(USERNAME_2)) { listener.error(new IllegalArgumentException("Invalid username")); return; } - final Schema pojoSchema = new Schema(ImmutableList.of(Field.nullable("a", - Types.MinorType.BIGINT.getType()))); + final Schema pojoSchema = + new Schema(ImmutableList.of(Field.nullable("a", Types.MinorType.BIGINT.getType()))); try (VectorSchemaRoot root = VectorSchemaRoot.create(pojoSchema, allocator)) { listener.start(root); root.allocateNew(); @@ -100,13 +100,14 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l private static void startServerAndClient() throws IOException { final FlightProducer flightProducer = getFlightProducer(); - server = FlightServer - .builder(allocator, forGrpcInsecure(LOCALHOST, 0), flightProducer) - .headerAuthenticator(new GeneratedBearerTokenAuthenticator( - new BasicCallHeaderAuthenticator(TestBasicAuth2::validate))) - .build().start(); - client = FlightClient.builder(allocator, server.getLocation()) - .build(); + server = + FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), flightProducer) + .headerAuthenticator( + new GeneratedBearerTokenAuthenticator( + new BasicCallHeaderAuthenticator(TestBasicAuth2::validate))) + .build() + .start(); + client = FlightClient.builder(allocator, server.getLocation()).build(); } @AfterAll @@ -122,13 +123,14 @@ public static void shutdown() throws Exception { } private void startClient2() throws IOException { - client2 = FlightClient.builder(allocator, server.getLocation()) - .build(); + client2 = FlightClient.builder(allocator, server.getLocation()).build(); } private static CallHeaderAuthenticator.AuthResult validate(String username, String password) { if (Strings.isNullOrEmpty(username)) { - throw CallStatus.UNAUTHENTICATED.withDescription("Credentials not supplied.").toRuntimeException(); + throw CallStatus.UNAUTHENTICATED + .withDescription("Credentials not supplied.") + .toRuntimeException(); } final String identity; if (USERNAME_1.equals(username) && PASSWORD_1.equals(password)) { @@ -136,7 +138,9 @@ private static CallHeaderAuthenticator.AuthResult validate(String username, Stri } else if (USERNAME_2.equals(username) && PASSWORD_2.equals(password)) { identity = USERNAME_2; } else { - throw CallStatus.UNAUTHENTICATED.withDescription("Username or password is invalid.").toRuntimeException(); + throw CallStatus.UNAUTHENTICATED + .withDescription("Username or password is invalid.") + .toRuntimeException(); } return () -> identity; } @@ -147,21 +151,23 @@ public void validAuthWithBearerAuthServer() throws IOException { } @Test - public void validAuthWithMultipleClientsWithSameCredentialsWithBearerAuthServer() throws IOException { + public void validAuthWithMultipleClientsWithSameCredentialsWithBearerAuthServer() + throws IOException { startClient2(); testValidAuthWithMultipleClientsWithSameCredentials(client, client2); } @Test - public void validAuthWithMultipleClientsWithDifferentCredentialsWithBearerAuthServer() throws IOException { + public void validAuthWithMultipleClientsWithDifferentCredentialsWithBearerAuthServer() + throws IOException { startClient2(); testValidAuthWithMultipleClientsWithDifferentCredentials(client, client2); } @Test public void asyncCall() throws Exception { - final CredentialCallOption bearerToken = client - .authenticateBasicToken(USERNAME_1, PASSWORD_1).get(); + final CredentialCallOption bearerToken = + client.authenticateBasicToken(USERNAME_1, PASSWORD_1).get(); client.listFlights(Criteria.ALL, bearerToken); try (final FlightStream s = client.getStream(new Ticket(new byte[1]), bearerToken)) { while (s.next()) { @@ -181,54 +187,52 @@ public void didntAuthWithBearerAuthServer() throws IOException { } private void testValidAuth(FlightClient client) { - final CredentialCallOption bearerToken = client - .authenticateBasicToken(USERNAME_1, PASSWORD_1).get(); - Assertions.assertTrue(ImmutableList.copyOf(client - .listFlights(Criteria.ALL, bearerToken)) - .isEmpty()); + final CredentialCallOption bearerToken = + client.authenticateBasicToken(USERNAME_1, PASSWORD_1).get(); + Assertions.assertTrue( + ImmutableList.copyOf(client.listFlights(Criteria.ALL, bearerToken)).isEmpty()); } private void testValidAuthWithMultipleClientsWithSameCredentials( - FlightClient client1, FlightClient client2) { - final CredentialCallOption bearerToken1 = client1 - .authenticateBasicToken(USERNAME_1, PASSWORD_1).get(); - final CredentialCallOption bearerToken2 = client2 - .authenticateBasicToken(USERNAME_1, PASSWORD_1).get(); - Assertions.assertTrue(ImmutableList.copyOf(client1 - .listFlights(Criteria.ALL, bearerToken1)) - .isEmpty()); - Assertions.assertTrue(ImmutableList.copyOf(client2 - .listFlights(Criteria.ALL, bearerToken2)) - .isEmpty()); + FlightClient client1, FlightClient client2) { + final CredentialCallOption bearerToken1 = + client1.authenticateBasicToken(USERNAME_1, PASSWORD_1).get(); + final CredentialCallOption bearerToken2 = + client2.authenticateBasicToken(USERNAME_1, PASSWORD_1).get(); + Assertions.assertTrue( + ImmutableList.copyOf(client1.listFlights(Criteria.ALL, bearerToken1)).isEmpty()); + Assertions.assertTrue( + ImmutableList.copyOf(client2.listFlights(Criteria.ALL, bearerToken2)).isEmpty()); } private void testValidAuthWithMultipleClientsWithDifferentCredentials( - FlightClient client1, FlightClient client2) { - final CredentialCallOption bearerToken1 = client1 - .authenticateBasicToken(USERNAME_1, PASSWORD_1).get(); - final CredentialCallOption bearerToken2 = client2 - .authenticateBasicToken(USERNAME_2, PASSWORD_2).get(); - Assertions.assertTrue(ImmutableList.copyOf(client1 - .listFlights(Criteria.ALL, bearerToken1)) - .isEmpty()); - Assertions.assertTrue(ImmutableList.copyOf(client2 - .listFlights(Criteria.ALL, bearerToken2)) - .isEmpty()); + FlightClient client1, FlightClient client2) { + final CredentialCallOption bearerToken1 = + client1.authenticateBasicToken(USERNAME_1, PASSWORD_1).get(); + final CredentialCallOption bearerToken2 = + client2.authenticateBasicToken(USERNAME_2, PASSWORD_2).get(); + Assertions.assertTrue( + ImmutableList.copyOf(client1.listFlights(Criteria.ALL, bearerToken1)).isEmpty()); + Assertions.assertTrue( + ImmutableList.copyOf(client2.listFlights(Criteria.ALL, bearerToken2)).isEmpty()); } private void testInvalidAuth(FlightClient client) { - FlightTestUtil.assertCode(FlightStatusCode.UNAUTHENTICATED, () -> - client.authenticateBasicToken(USERNAME_1, "WRONG")); + FlightTestUtil.assertCode( + FlightStatusCode.UNAUTHENTICATED, () -> client.authenticateBasicToken(USERNAME_1, "WRONG")); - FlightTestUtil.assertCode(FlightStatusCode.UNAUTHENTICATED, () -> - client.authenticateBasicToken(NO_USERNAME, PASSWORD_1)); + FlightTestUtil.assertCode( + FlightStatusCode.UNAUTHENTICATED, + () -> client.authenticateBasicToken(NO_USERNAME, PASSWORD_1)); - FlightTestUtil.assertCode(FlightStatusCode.UNAUTHENTICATED, () -> - client.listFlights(Criteria.ALL).forEach(action -> Assertions.fail())); + FlightTestUtil.assertCode( + FlightStatusCode.UNAUTHENTICATED, + () -> client.listFlights(Criteria.ALL).forEach(action -> Assertions.fail())); } private void didntAuth(FlightClient client) { - FlightTestUtil.assertCode(FlightStatusCode.UNAUTHENTICATED, () -> - client.listFlights(Criteria.ALL).forEach(action -> Assertions.fail())); + FlightTestUtil.assertCode( + FlightStatusCode.UNAUTHENTICATED, + () -> client.listFlights(Criteria.ALL).forEach(action -> Assertions.fail())); } } diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/client/CustomHeaderTest.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/client/CustomHeaderTest.java index 8fc7557c0be..63012e7aa48 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/client/CustomHeaderTest.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/client/CustomHeaderTest.java @@ -17,9 +17,9 @@ package org.apache.arrow.flight.client; +import com.google.common.collect.ImmutableMap; import java.util.HashMap; import java.util.Map; - import org.apache.arrow.flight.Action; import org.apache.arrow.flight.CallHeaders; import org.apache.arrow.flight.CallInfo; @@ -48,23 +48,18 @@ import org.junit.Before; import org.junit.Test; -import com.google.common.collect.ImmutableMap; - - -/** - * Tests to ensure custom headers are passed along to the server for each command. - */ +/** Tests to ensure custom headers are passed along to the server for each command. */ public class CustomHeaderTest { FlightServer server; FlightClient client; BufferAllocator allocator; TestCustomHeaderMiddleware.Factory headersMiddleware; HeaderCallOption headers; - Map testHeaders = ImmutableMap.of( + Map testHeaders = + ImmutableMap.of( "foo", "bar", "bar", "foo", - "answer", "42" - ); + "answer", "42"); @Before public void setUp() throws Exception { @@ -75,11 +70,13 @@ public void setUp() throws Exception { callHeaders.insert(entry.getKey(), entry.getValue()); } headers = new HeaderCallOption(callHeaders); - server = FlightServer.builder(allocator, - Location.forGrpcInsecure(FlightTestUtil.LOCALHOST, /*port*/ 0), - new NoOpFlightProducer()) - .middleware(FlightServerMiddleware.Key.of("customHeader"), headersMiddleware) - .build(); + server = + FlightServer.builder( + allocator, + Location.forGrpcInsecure(FlightTestUtil.LOCALHOST, /*port*/ 0), + new NoOpFlightProducer()) + .middleware(FlightServerMiddleware.Key.of("customHeader"), headersMiddleware) + .build(); server.start(); client = FlightClient.builder(allocator, server.getLocation()).build(); } @@ -94,7 +91,8 @@ public void tearDown() throws Exception { public void testHandshake() { try { client.handshake(headers); - } catch (Exception ignored) { } + } catch (Exception ignored) { + } assertHeadersMatch(FlightMethod.HANDSHAKE); } @@ -103,7 +101,8 @@ public void testHandshake() { public void testGetSchema() { try { client.getSchema(FlightDescriptor.command(new byte[0]), headers); - } catch (Exception ignored) { } + } catch (Exception ignored) { + } assertHeadersMatch(FlightMethod.GET_SCHEMA); } @@ -112,7 +111,8 @@ public void testGetSchema() { public void testGetFlightInfo() { try { client.getInfo(FlightDescriptor.command(new byte[0]), headers); - } catch (Exception ignored) { } + } catch (Exception ignored) { + } assertHeadersMatch(FlightMethod.GET_FLIGHT_INFO); } @@ -121,7 +121,8 @@ public void testGetFlightInfo() { public void testListActions() { try { client.listActions(headers).iterator().next(); - } catch (Exception ignored) { } + } catch (Exception ignored) { + } assertHeadersMatch(FlightMethod.LIST_ACTIONS); } @@ -129,8 +130,9 @@ public void testListActions() { @Test public void testListFlights() { try { - client.listFlights(new Criteria(new byte[]{1}), headers).iterator().next(); - } catch (Exception ignored) { } + client.listFlights(new Criteria(new byte[] {1}), headers).iterator().next(); + } catch (Exception ignored) { + } assertHeadersMatch(FlightMethod.LIST_FLIGHTS); } @@ -139,7 +141,8 @@ public void testListFlights() { public void testDoAction() { try { client.doAction(new Action("test"), headers).next(); - } catch (Exception ignored) { } + } catch (Exception ignored) { + } assertHeadersMatch(FlightMethod.DO_ACTION); } @@ -147,11 +150,11 @@ public void testDoAction() { @Test public void testStartPut() { try { - final ClientStreamListener listener = client.startPut(FlightDescriptor.command(new byte[0]), - new SyncPutListener(), - headers); + final ClientStreamListener listener = + client.startPut(FlightDescriptor.command(new byte[0]), new SyncPutListener(), headers); listener.getResult(); - } catch (Exception ignored) { } + } catch (Exception ignored) { + } assertHeadersMatch(FlightMethod.DO_PUT); } @@ -160,62 +163,54 @@ public void testStartPut() { public void testGetStream() { try (final FlightStream stream = client.getStream(new Ticket(new byte[0]), headers)) { stream.next(); - } catch (Exception ignored) { } + } catch (Exception ignored) { + } assertHeadersMatch(FlightMethod.DO_GET); } @Test public void testDoExchange() { - try (final FlightClient.ExchangeReaderWriter stream = client.doExchange( - FlightDescriptor.command(new byte[0]), - headers) - ) { + try (final FlightClient.ExchangeReaderWriter stream = + client.doExchange(FlightDescriptor.command(new byte[0]), headers)) { stream.getReader().next(); - } catch (Exception ignored) { } + } catch (Exception ignored) { + } assertHeadersMatch(FlightMethod.DO_EXCHANGE); } private void assertHeadersMatch(FlightMethod method) { for (Map.Entry entry : testHeaders.entrySet()) { - Assert.assertEquals(entry.getValue(), headersMiddleware.getCustomHeader(method, entry.getKey())); + Assert.assertEquals( + entry.getValue(), headersMiddleware.getCustomHeader(method, entry.getKey())); } } - /** - * A middleware used to test if customHeaders are being sent to the server properly. - */ + /** A middleware used to test if customHeaders are being sent to the server properly. */ static class TestCustomHeaderMiddleware implements FlightServerMiddleware { - public TestCustomHeaderMiddleware() { - } + public TestCustomHeaderMiddleware() {} @Override - public void onBeforeSendingHeaders(CallHeaders callHeaders) { - - } + public void onBeforeSendingHeaders(CallHeaders callHeaders) {} @Override - public void onCallCompleted(CallStatus callStatus) { - - } + public void onCallCompleted(CallStatus callStatus) {} @Override - public void onCallErrored(Throwable throwable) { - - } + public void onCallErrored(Throwable throwable) {} /** - * A factory for the middleware that keeps track of the received headers and provides a way - * to check those values for a given Flight Method. + * A factory for the middleware that keeps track of the received headers and provides a way to + * check those values for a given Flight Method. */ static class Factory implements FlightServerMiddleware.Factory { private final Map receivedCallHeaders = new HashMap<>(); @Override - public TestCustomHeaderMiddleware onCallStarted(CallInfo callInfo, CallHeaders callHeaders, - RequestContext requestContext) { + public TestCustomHeaderMiddleware onCallStarted( + CallInfo callInfo, CallHeaders callHeaders, RequestContext requestContext) { receivedCallHeaders.put(callInfo.method(), callHeaders); return new TestCustomHeaderMiddleware(); @@ -229,5 +224,5 @@ public String getCustomHeader(FlightMethod method, String key) { return headers.get(key); } } - } + } } diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/client/TestCookieHandling.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/client/TestCookieHandling.java index d34a3a2d3a5..eefa3b332c8 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/client/TestCookieHandling.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/client/TestCookieHandling.java @@ -21,7 +21,6 @@ import static org.apache.arrow.flight.Location.forGrpcInsecure; import java.io.IOException; - import org.apache.arrow.flight.CallHeaders; import org.apache.arrow.flight.CallInfo; import org.apache.arrow.flight.CallStatus; @@ -186,7 +185,8 @@ public void multipleCookiesWithSetCookie() { headersToSend.insert(SET_COOKIE_HEADER, "firstKey=firstVal"); headersToSend.insert(SET_COOKIE_HEADER, "secondKey=secondVal"); cookieMiddleware.onHeadersReceived(headersToSend); - Assertions.assertEquals("firstKey=firstVal; secondKey=secondVal", cookieMiddleware.getValidCookiesAsString()); + Assertions.assertEquals( + "firstKey=firstVal; secondKey=secondVal", cookieMiddleware.getValidCookiesAsString()); } @Test @@ -199,9 +199,7 @@ public void cookieStaysAfterMultipleRequestsEndToEnd() { Assertions.assertEquals("k=v", testFactory.clientCookieMiddleware.getValidCookiesAsString()); } - /** - * A server middleware component that injects SET_COOKIE_HEADER into the outgoing headers. - */ + /** A server middleware component that injects SET_COOKIE_HEADER into the outgoing headers. */ static class SetCookieHeaderInjector implements FlightServerMiddleware { private final Factory factory; @@ -217,21 +215,17 @@ public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) { } @Override - public void onCallCompleted(CallStatus status) { - - } + public void onCallCompleted(CallStatus status) {} @Override - public void onCallErrored(Throwable err) { - - } + public void onCallErrored(Throwable err) {} static class Factory implements FlightServerMiddleware.Factory { private boolean receivedCookieHeader = false; @Override - public SetCookieHeaderInjector onCallStarted(CallInfo info, CallHeaders incomingHeaders, - RequestContext context) { + public SetCookieHeaderInjector onCallStarted( + CallInfo info, CallHeaders incomingHeaders, RequestContext context) { receivedCookieHeader = null != incomingHeaders.get(COOKIE_HEADER); return new SetCookieHeaderInjector(this); } @@ -250,21 +244,23 @@ public ClientCookieMiddleware onCallStarted(CallInfo info) { } private void startServerAndClient() throws IOException { - final FlightProducer flightProducer = new NoOpFlightProducer() { - @Override - public void listFlights(CallContext context, Criteria criteria, - StreamListener listener) { - listener.onCompleted(); - } - }; - - this.server = FlightServer - .builder(allocator, forGrpcInsecure(LOCALHOST, 0), flightProducer) - .middleware(FlightServerMiddleware.Key.of("test"), new SetCookieHeaderInjector.Factory()) - .build().start(); - - this.client = FlightClient.builder(allocator, server.getLocation()) - .intercept(testFactory) - .build(); + final FlightProducer flightProducer = + new NoOpFlightProducer() { + @Override + public void listFlights( + CallContext context, Criteria criteria, StreamListener listener) { + listener.onCompleted(); + } + }; + + this.server = + FlightServer.builder(allocator, forGrpcInsecure(LOCALHOST, 0), flightProducer) + .middleware( + FlightServerMiddleware.Key.of("test"), new SetCookieHeaderInjector.Factory()) + .build() + .start(); + + this.client = + FlightClient.builder(allocator, server.getLocation()).intercept(testFactory).build(); } } diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/grpc/TestStatusUtils.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/grpc/TestStatusUtils.java index 9912a26ea34..209c3876e25 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/grpc/TestStatusUtils.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/grpc/TestStatusUtils.java @@ -17,14 +17,13 @@ package org.apache.arrow.flight.grpc; +import io.grpc.Metadata; +import io.grpc.Status; import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.FlightStatusCode; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import io.grpc.Metadata; -import io.grpc.Status; - public class TestStatusUtils { @Test diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/perf/PerformanceTestServer.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/perf/PerformanceTestServer.java index b1e83ea61ed..b47267f24b5 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/perf/PerformanceTestServer.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/perf/PerformanceTestServer.java @@ -17,13 +17,14 @@ package org.apache.arrow.flight.perf; +import com.google.common.collect.ImmutableList; +import com.google.protobuf.InvalidProtocolBufferException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; - import org.apache.arrow.flight.BackpressureStrategy; import org.apache.arrow.flight.FlightDescriptor; import org.apache.arrow.flight.FlightEndpoint; @@ -44,9 +45,6 @@ import org.apache.arrow.vector.types.pojo.Field; import org.apache.arrow.vector.types.pojo.Schema; -import com.google.common.collect.ImmutableList; -import com.google.protobuf.InvalidProtocolBufferException; - public class PerformanceTestServer implements AutoCloseable { private final FlightServer flightServer; @@ -55,26 +53,33 @@ public class PerformanceTestServer implements AutoCloseable { private final boolean isNonBlocking; public PerformanceTestServer(BufferAllocator incomingAllocator, Location location) { - this(incomingAllocator, location, new BackpressureStrategy() { - private FlightProducer.ServerStreamListener listener; - - @Override - public void register(FlightProducer.ServerStreamListener listener) { - this.listener = listener; - } + this( + incomingAllocator, + location, + new BackpressureStrategy() { + private FlightProducer.ServerStreamListener listener; + + @Override + public void register(FlightProducer.ServerStreamListener listener) { + this.listener = listener; + } - @Override - public WaitResult waitForListener(long timeout) { - while (!listener.isReady() && !listener.isCancelled()) { - // busy wait - } - return WaitResult.READY; - } - }, false); + @Override + public WaitResult waitForListener(long timeout) { + while (!listener.isReady() && !listener.isCancelled()) { + // busy wait + } + return WaitResult.READY; + } + }, + false); } - public PerformanceTestServer(BufferAllocator incomingAllocator, Location location, BackpressureStrategy bpStrategy, - boolean isNonBlocking) { + public PerformanceTestServer( + BufferAllocator incomingAllocator, + Location location, + BackpressureStrategy bpStrategy, + boolean isNonBlocking) { this.allocator = incomingAllocator.newChildAllocator("perf-server", 0, Long.MAX_VALUE); this.producer = new PerfProducer(bpStrategy); this.flightServer = FlightServer.builder(this.allocator, location, producer).build(); @@ -103,62 +108,60 @@ private PerfProducer(BackpressureStrategy bpStrategy) { } @Override - public void getStream(CallContext context, Ticket ticket, - ServerStreamListener listener) { + public void getStream(CallContext context, Ticket ticket, ServerStreamListener listener) { bpStrategy.register(listener); - final Runnable loadData = () -> { - Token token = null; - try { - token = Token.parseFrom(ticket.getBytes()); - } catch (InvalidProtocolBufferException e) { - throw new RuntimeException(e); - } - Perf perf = token.getDefinition(); - Schema schema = Schema.deserializeMessage(perf.getSchema().asReadOnlyByteBuffer()); - try ( - VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); - BigIntVector a = (BigIntVector) root.getVector("a") - ) { - listener.setUseZeroCopy(true); - listener.start(root); - root.allocateNew(); - - int current = 0; - long i = token.getStart(); - while (i < token.getEnd()) { - if (listener.isCancelled()) { - root.clear(); - return; - } - - if (TestPerf.VALIDATE) { - a.setSafe(current, i); + final Runnable loadData = + () -> { + Token token = null; + try { + token = Token.parseFrom(ticket.getBytes()); + } catch (InvalidProtocolBufferException e) { + throw new RuntimeException(e); } + Perf perf = token.getDefinition(); + Schema schema = Schema.deserializeMessage(perf.getSchema().asReadOnlyByteBuffer()); + try (VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); + BigIntVector a = (BigIntVector) root.getVector("a")) { + listener.setUseZeroCopy(true); + listener.start(root); + root.allocateNew(); - i++; - current++; - if (i % perf.getRecordsPerBatch() == 0) { - root.setRowCount(current); + int current = 0; + long i = token.getStart(); + while (i < token.getEnd()) { + if (listener.isCancelled()) { + root.clear(); + return; + } + + if (TestPerf.VALIDATE) { + a.setSafe(current, i); + } + + i++; + current++; + if (i % perf.getRecordsPerBatch() == 0) { + root.setRowCount(current); + + bpStrategy.waitForListener(0); + if (listener.isCancelled()) { + root.clear(); + return; + } + listener.putNext(); + current = 0; + root.allocateNew(); + } + } - bpStrategy.waitForListener(0); - if (listener.isCancelled()) { - root.clear(); - return; + // send last partial batch. + if (current != 0) { + root.setRowCount(current); + listener.putNext(); } - listener.putNext(); - current = 0; - root.allocateNew(); + listener.completed(); } - } - - // send last partial batch. - if (current != 0) { - root.setRowCount(current); - listener.putNext(); - } - listener.completed(); - } - }; + }; if (!isNonBlocking) { loadData.run(); @@ -170,23 +173,25 @@ public void getStream(CallContext context, Ticket ticket, } @Override - public FlightInfo getFlightInfo(CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfo(CallContext context, FlightDescriptor descriptor) { try { Preconditions.checkArgument(descriptor.isCommand()); Perf exec = Perf.parseFrom(descriptor.getCommand()); - final Schema pojoSchema = new Schema(ImmutableList.of( - Field.nullable("a", MinorType.BIGINT.getType()), - Field.nullable("b", MinorType.BIGINT.getType()), - Field.nullable("c", MinorType.BIGINT.getType()), - Field.nullable("d", MinorType.BIGINT.getType()) - )); - - Token token = Token.newBuilder().setDefinition(exec) - .setStart(0) - .setEnd(exec.getRecordsPerStream()) - .build(); + final Schema pojoSchema = + new Schema( + ImmutableList.of( + Field.nullable("a", MinorType.BIGINT.getType()), + Field.nullable("b", MinorType.BIGINT.getType()), + Field.nullable("c", MinorType.BIGINT.getType()), + Field.nullable("d", MinorType.BIGINT.getType()))); + + Token token = + Token.newBuilder() + .setDefinition(exec) + .setStart(0) + .setEnd(exec.getRecordsPerStream()) + .build(); final Ticket ticket = new Ticket(token.toByteArray()); List endpoints = new ArrayList<>(); @@ -194,7 +199,11 @@ public FlightInfo getFlightInfo(CallContext context, endpoints.add(new FlightEndpoint(ticket, getLocation())); } - return new FlightInfo(pojoSchema, descriptor, endpoints, -1, + return new FlightInfo( + pojoSchema, + descriptor, + endpoints, + -1, exec.getRecordsPerStream() * exec.getStreamCount()); } catch (InvalidProtocolBufferException e) { throw new RuntimeException(e); @@ -202,6 +211,3 @@ public FlightInfo getFlightInfo(CallContext context, } } } - - - diff --git a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/perf/TestPerf.java b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/perf/TestPerf.java index 290e82de36c..7048c6f16f3 100644 --- a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/perf/TestPerf.java +++ b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/perf/TestPerf.java @@ -20,13 +20,20 @@ import static org.apache.arrow.flight.FlightTestUtil.LOCALHOST; import static org.apache.arrow.flight.Location.forGrpcInsecure; +import com.google.common.base.MoreObjects; +import com.google.common.base.Stopwatch; +import com.google.common.collect.ImmutableList; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; +import com.google.protobuf.ByteString; import java.util.Arrays; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; - import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightDescriptor; import org.apache.arrow.flight.FlightInfo; @@ -43,38 +50,32 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import com.google.common.base.MoreObjects; -import com.google.common.base.Stopwatch; -import com.google.common.collect.ImmutableList; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.ListeningExecutorService; -import com.google.common.util.concurrent.MoreExecutors; -import com.google.protobuf.ByteString; - @Disabled public class TestPerf { public static final boolean VALIDATE = false; - public static FlightDescriptor getPerfFlightDescriptor(long recordCount, int recordsPerBatch, int streamCount) { - final Schema pojoSchema = new Schema(ImmutableList.of( - Field.nullable("a", MinorType.BIGINT.getType()), - Field.nullable("b", MinorType.BIGINT.getType()), - Field.nullable("c", MinorType.BIGINT.getType()), - Field.nullable("d", MinorType.BIGINT.getType()) - )); + public static FlightDescriptor getPerfFlightDescriptor( + long recordCount, int recordsPerBatch, int streamCount) { + final Schema pojoSchema = + new Schema( + ImmutableList.of( + Field.nullable("a", MinorType.BIGINT.getType()), + Field.nullable("b", MinorType.BIGINT.getType()), + Field.nullable("c", MinorType.BIGINT.getType()), + Field.nullable("d", MinorType.BIGINT.getType()))); byte[] bytes = pojoSchema.serializeAsMessage(); ByteString serializedSchema = ByteString.copyFrom(bytes); - return FlightDescriptor.command(Perf.newBuilder() - .setRecordsPerStream(recordCount) - .setRecordsPerBatch(recordsPerBatch) - .setSchema(serializedSchema) - .setStreamCount(streamCount) - .build() - .toByteArray()); + return FlightDescriptor.command( + Perf.newBuilder() + .setRecordsPerStream(recordCount) + .setRecordsPerBatch(recordsPerBatch) + .setSchema(serializedSchema) + .setStreamCount(streamCount) + .build() + .toByteArray()); } public static void main(String[] args) throws Exception { @@ -84,50 +85,55 @@ public static void main(String[] args) throws Exception { @Test public void throughput() throws Exception { final int numRuns = 10; - ListeningExecutorService pool = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(4)); - double [] throughPuts = new double[numRuns]; + ListeningExecutorService pool = + MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(4)); + double[] throughPuts = new double[numRuns]; for (int i = 0; i < numRuns; i++) { - try ( - final BufferAllocator a = new RootAllocator(Long.MAX_VALUE); - final PerformanceTestServer server = new PerformanceTestServer(a, forGrpcInsecure(LOCALHOST, 0)).start(); - final FlightClient client = FlightClient.builder(a, server.getLocation()).build(); - ) { + try (final BufferAllocator a = new RootAllocator(Long.MAX_VALUE); + final PerformanceTestServer server = + new PerformanceTestServer(a, forGrpcInsecure(LOCALHOST, 0)).start(); + final FlightClient client = FlightClient.builder(a, server.getLocation()).build(); ) { final FlightInfo info = client.getInfo(getPerfFlightDescriptor(50_000_000L, 4095, 2)); - List> results = info.getEndpoints() - .stream() - .map(t -> new Consumer(client, t.getTicket())) - .map(t -> pool.submit(t)) - .collect(Collectors.toList()); - - final Result r = Futures.whenAllSucceed(results).call(() -> { - Result res = new Result(); - for (ListenableFuture f : results) { - res.add(f.get()); - } - return res; - }, pool).get(); + List> results = + info.getEndpoints().stream() + .map(t -> new Consumer(client, t.getTicket())) + .map(t -> pool.submit(t)) + .collect(Collectors.toList()); + + final Result r = + Futures.whenAllSucceed(results) + .call( + () -> { + Result res = new Result(); + for (ListenableFuture f : results) { + res.add(f.get()); + } + return res; + }, + pool) + .get(); double seconds = r.nanos * 1.0d / 1000 / 1000 / 1000; throughPuts[i] = (r.bytes * 1.0d / 1024 / 1024) / seconds; System.out.printf( - "Transferred %d records totaling %s bytes at %f MiB/s. %f record/s. %f batch/s.%n", + "Transferred %d records totaling %s bytes at %f MiB/s. %f record/s. %f batch/s.%n", r.rows, r.bytes, throughPuts[i], (r.rows * 1.0d) / seconds, - (r.batches * 1.0d) / seconds - ); + (r.batches * 1.0d) / seconds); } } pool.shutdown(); System.out.println("Summary: "); double average = Arrays.stream(throughPuts).sum() / numRuns; - double sqrSum = Arrays.stream(throughPuts).map(val -> val - average).map(val -> val * val).sum(); + double sqrSum = + Arrays.stream(throughPuts).map(val -> val - average).map(val -> val * val).sum(); double stddev = Math.sqrt(sqrSum / numRuns); - System.out.printf("Average throughput: %f MiB/s, standard deviation: %f MiB/s%n", - average, stddev); + System.out.printf( + "Average throughput: %f MiB/s, standard deviation: %f MiB/s%n", average, stddev); } private static final class Consumer implements Callable { @@ -170,7 +176,6 @@ public Result call() throws Exception { } } } - } private static final class Result { diff --git a/java/flight/flight-core/src/test/resources/logback.xml b/java/flight/flight-core/src/test/resources/logback.xml index 444b2ed6d83..2ad65d23304 100644 --- a/java/flight/flight-core/src/test/resources/logback.xml +++ b/java/flight/flight-core/src/test/resources/logback.xml @@ -1,4 +1,4 @@ - + - - - 4.0.0 - - arrow-flight - org.apache.arrow - 16.0.0-SNAPSHOT - ../pom.xml - + + 4.0.0 + + org.apache.arrow + arrow-flight + 16.0.0-SNAPSHOT + ../pom.xml + - flight-integration-tests - Arrow Flight Integration Tests - Integration tests for Flight RPC. - jar + flight-integration-tests + jar + Arrow Flight Integration Tests + Integration tests for Flight RPC. - - - org.apache.arrow - arrow-vector - - - org.apache.arrow - arrow-memory-core - - - org.apache.arrow - flight-core - - - org.apache.arrow - flight-sql - - - com.google.protobuf - protobuf-java - - - commons-cli - commons-cli - 1.6.0 - - - org.slf4j - slf4j-api - - + + + org.apache.arrow + arrow-vector + + + org.apache.arrow + arrow-memory-core + + + org.apache.arrow + flight-core + + + org.apache.arrow + flight-sql + + + com.google.protobuf + protobuf-java + + + commons-cli + commons-cli + 1.6.0 + + + org.slf4j + slf4j-api + + - - - - maven-assembly-plugin - 3.0.0 - - - jar-with-dependencies - - - - - make-assembly - package - - single - - - - - - + + + + maven-assembly-plugin + 3.0.0 + + + jar-with-dependencies + + + + + make-assembly + + single + + package + + + + + diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/AppMetadataFlightInfoEndpointScenario.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/AppMetadataFlightInfoEndpointScenario.java index 3220bb5a2d2..31b90915433 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/AppMetadataFlightInfoEndpointScenario.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/AppMetadataFlightInfoEndpointScenario.java @@ -20,7 +20,6 @@ import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; - import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightDescriptor; import org.apache.arrow.flight.FlightEndpoint; @@ -43,11 +42,11 @@ public FlightProducer producer(BufferAllocator allocator, Location location) thr } @Override - public void buildServer(FlightServer.Builder builder) throws Exception { - } + public void buildServer(FlightServer.Builder builder) throws Exception {} @Override - public void client(BufferAllocator allocator, Location location, FlightClient client) throws Exception { + public void client(BufferAllocator allocator, Location location, FlightClient client) + throws Exception { byte[] cmd = "foobar".getBytes(StandardCharsets.UTF_8); FlightInfo info = client.getInfo(FlightDescriptor.command(cmd)); IntegrationAssertions.assertEquals(info.getAppMetadata(), cmd); @@ -60,17 +59,19 @@ static class AppMetadataFlightInfoEndpointProducer extends NoOpFlightProducer { @Override public FlightInfo getFlightInfo(CallContext context, FlightDescriptor descriptor) { byte[] cmd = descriptor.getCommand(); - - Schema schema = new Schema( - Collections.singletonList(Field.notNullable("number", Types.MinorType.UINT4.getType()))); - List endpoints = Collections.singletonList( - FlightEndpoint.builder( - new Ticket("".getBytes(StandardCharsets.UTF_8))).setAppMetadata(cmd).build()); + Schema schema = + new Schema( + Collections.singletonList( + Field.notNullable("number", Types.MinorType.UINT4.getType()))); + + List endpoints = + Collections.singletonList( + FlightEndpoint.builder(new Ticket("".getBytes(StandardCharsets.UTF_8))) + .setAppMetadata(cmd) + .build()); return FlightInfo.builder(schema, descriptor, endpoints).setAppMetadata(cmd).build(); } } } - - diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/AuthBasicProtoScenario.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/AuthBasicProtoScenario.java index 1c95d4d5593..891bde3e40d 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/AuthBasicProtoScenario.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/AuthBasicProtoScenario.java @@ -20,7 +20,6 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Optional; - import org.apache.arrow.flight.Action; import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.FlightClient; @@ -35,9 +34,7 @@ import org.apache.arrow.flight.auth.BasicServerAuthHandler; import org.apache.arrow.memory.BufferAllocator; -/** - * A scenario testing the built-in basic authentication Protobuf. - */ +/** A scenario testing the built-in basic authentication Protobuf. */ final class AuthBasicProtoScenario implements Scenario { static final String USERNAME = "arrow"; @@ -56,34 +53,40 @@ public void doAction(CallContext context, Action action, StreamListener @Override public void buildServer(FlightServer.Builder builder) { - builder.authHandler(new BasicServerAuthHandler(new BasicServerAuthHandler.BasicAuthValidator() { - @Override - public byte[] getToken(String username, String password) throws Exception { - if (!USERNAME.equals(username) || !PASSWORD.equals(password)) { - throw CallStatus.UNAUTHENTICATED.withDescription("Username or password is invalid.").toRuntimeException(); - } - return ("valid:" + username).getBytes(StandardCharsets.UTF_8); - } + builder.authHandler( + new BasicServerAuthHandler( + new BasicServerAuthHandler.BasicAuthValidator() { + @Override + public byte[] getToken(String username, String password) throws Exception { + if (!USERNAME.equals(username) || !PASSWORD.equals(password)) { + throw CallStatus.UNAUTHENTICATED + .withDescription("Username or password is invalid.") + .toRuntimeException(); + } + return ("valid:" + username).getBytes(StandardCharsets.UTF_8); + } - @Override - public Optional isValid(byte[] token) { - if (token != null) { - final String credential = new String(token, StandardCharsets.UTF_8); - if (credential.startsWith("valid:")) { - return Optional.of(credential.substring(6)); - } - } - return Optional.empty(); - } - })); + @Override + public Optional isValid(byte[] token) { + if (token != null) { + final String credential = new String(token, StandardCharsets.UTF_8); + if (credential.startsWith("valid:")) { + return Optional.of(credential.substring(6)); + } + } + return Optional.empty(); + } + })); } @Override public void client(BufferAllocator allocator, Location location, FlightClient client) { - final FlightRuntimeException e = IntegrationAssertions.assertThrows(FlightRuntimeException.class, () -> { - client.listActions().forEach(act -> { - }); - }); + final FlightRuntimeException e = + IntegrationAssertions.assertThrows( + FlightRuntimeException.class, + () -> { + client.listActions().forEach(act -> {}); + }); if (!FlightStatusCode.UNAUTHENTICATED.equals(e.status().code())) { throw new AssertionError("Expected UNAUTHENTICATED but found " + e.status().code(), e); } @@ -91,7 +94,8 @@ public void client(BufferAllocator allocator, Location location, FlightClient cl client.authenticate(new BasicClientAuthHandler(USERNAME, PASSWORD)); final Result result = client.doAction(new Action("")).next(); if (!USERNAME.equals(new String(result.getBody(), StandardCharsets.UTF_8))) { - throw new AssertionError("Expected " + USERNAME + " but got " + Arrays.toString(result.getBody())); + throw new AssertionError( + "Expected " + USERNAME + " but got " + Arrays.toString(result.getBody())); } } } diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeCancelFlightInfoScenario.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeCancelFlightInfoScenario.java index 244f4373b29..d6b128b1614 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeCancelFlightInfoScenario.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeCancelFlightInfoScenario.java @@ -18,7 +18,6 @@ package org.apache.arrow.flight.integration.tests; import java.nio.charset.StandardCharsets; - import org.apache.arrow.flight.CancelFlightInfoRequest; import org.apache.arrow.flight.CancelFlightInfoResult; import org.apache.arrow.flight.CancelStatus; @@ -41,24 +40,26 @@ public FlightProducer producer(BufferAllocator allocator, Location location) thr } @Override - public void buildServer(FlightServer.Builder builder) { - } + public void buildServer(FlightServer.Builder builder) {} @Override - public void client(BufferAllocator allocator, Location location, FlightClient client) throws Exception { - FlightInfo info = client.getInfo(FlightDescriptor.command("expiration".getBytes(StandardCharsets.UTF_8))); + public void client(BufferAllocator allocator, Location location, FlightClient client) + throws Exception { + FlightInfo info = + client.getInfo(FlightDescriptor.command("expiration".getBytes(StandardCharsets.UTF_8))); CancelFlightInfoRequest request = new CancelFlightInfoRequest(info); CancelFlightInfoResult result = client.cancelFlightInfo(request); IntegrationAssertions.assertEquals(CancelStatus.CANCELLED, result.getStatus()); // All requests should fail for (FlightEndpoint endpoint : info.getEndpoints()) { - IntegrationAssertions.assertThrows(FlightRuntimeException.class, () -> { - try (FlightStream stream = client.getStream(endpoint.getTicket())) { - while (stream.next()) { - } - } - }); + IntegrationAssertions.assertThrows( + FlightRuntimeException.class, + () -> { + try (FlightStream stream = client.getStream(endpoint.getTicket())) { + while (stream.next()) {} + } + }); } } } diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeDoGetScenario.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeDoGetScenario.java index 504836b334e..9c7a781ce06 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeDoGetScenario.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeDoGetScenario.java @@ -20,7 +20,6 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; - import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightDescriptor; import org.apache.arrow.flight.FlightEndpoint; @@ -45,22 +44,26 @@ public FlightProducer producer(BufferAllocator allocator, Location location) thr } @Override - public void buildServer(FlightServer.Builder builder) { - } + public void buildServer(FlightServer.Builder builder) {} @Override - public void client(BufferAllocator allocator, Location location, FlightClient client) throws Exception { - FlightInfo info = client.getInfo(FlightDescriptor.command("expiration_time".getBytes(StandardCharsets.UTF_8))); + public void client(BufferAllocator allocator, Location location, FlightClient client) + throws Exception { + FlightInfo info = + client.getInfo( + FlightDescriptor.command("expiration_time".getBytes(StandardCharsets.UTF_8))); List batches = new ArrayList<>(); try { for (FlightEndpoint endpoint : info.getEndpoints()) { if (batches.size() == 0) { - IntegrationAssertions.assertFalse("endpoints[0] must not have expiration time", + IntegrationAssertions.assertFalse( + "endpoints[0] must not have expiration time", endpoint.getExpirationTime().isPresent()); } else { - IntegrationAssertions.assertTrue("endpoints[" + batches.size() + "] must have expiration time", + IntegrationAssertions.assertTrue( + "endpoints[" + batches.size() + "] must have expiration time", endpoint.getExpirationTime().isPresent()); } try (FlightStream stream = client.getStream(endpoint.getTicket())) { @@ -72,7 +75,8 @@ public void client(BufferAllocator allocator, Location location, FlightClient cl // Check data IntegrationAssertions.assertEquals(3, batches.size()); - try (final VectorSchemaRoot root = VectorSchemaRoot.create(ExpirationTimeProducer.SCHEMA, allocator)) { + try (final VectorSchemaRoot root = + VectorSchemaRoot.create(ExpirationTimeProducer.SCHEMA, allocator)) { final VectorLoader loader = new VectorLoader(root); loader.load(batches.get(0)); diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeListActionsScenario.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeListActionsScenario.java index 26c4e7b604d..c618d01f291 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeListActionsScenario.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeListActionsScenario.java @@ -18,7 +18,6 @@ package org.apache.arrow.flight.integration.tests; import java.util.Iterator; - import org.apache.arrow.flight.ActionType; import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightConstants; @@ -35,19 +34,21 @@ public FlightProducer producer(BufferAllocator allocator, Location location) thr } @Override - public void buildServer(FlightServer.Builder builder) { - } + public void buildServer(FlightServer.Builder builder) {} @Override - public void client(BufferAllocator allocator, Location location, FlightClient client) throws Exception { + public void client(BufferAllocator allocator, Location location, FlightClient client) + throws Exception { Iterator actions = client.listActions().iterator(); IntegrationAssertions.assertTrue("Expected 2 actions", actions.hasNext()); ActionType action = actions.next(); - IntegrationAssertions.assertEquals(FlightConstants.CANCEL_FLIGHT_INFO.getType(), action.getType()); + IntegrationAssertions.assertEquals( + FlightConstants.CANCEL_FLIGHT_INFO.getType(), action.getType()); IntegrationAssertions.assertTrue("Expected 2 actions", actions.hasNext()); action = actions.next(); - IntegrationAssertions.assertEquals(FlightConstants.RENEW_FLIGHT_ENDPOINT.getType(), action.getType()); + IntegrationAssertions.assertEquals( + FlightConstants.RENEW_FLIGHT_ENDPOINT.getType(), action.getType()); IntegrationAssertions.assertFalse("Expected 2 actions", actions.hasNext()); } diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeProducer.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeProducer.java index 7838667e4d2..b33a8a49193 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeProducer.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeProducer.java @@ -26,7 +26,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; - import org.apache.arrow.flight.Action; import org.apache.arrow.flight.ActionType; import org.apache.arrow.flight.CallStatus; @@ -49,35 +48,32 @@ import org.apache.arrow.vector.types.pojo.Field; import org.apache.arrow.vector.types.pojo.Schema; -/** The server used for testing FlightEndpoint.expiration_time. - *

- * GetFlightInfo() returns a FlightInfo that has the following - * three FlightEndpoints: +/** + * The server used for testing FlightEndpoint.expiration_time. + * + *

GetFlightInfo() returns a FlightInfo that has the following three FlightEndpoints: * *

    - *
  1. No expiration time
  2. - *
  3. 5 seconds expiration time
  4. - *
  5. 6 seconds expiration time
  6. + *
  7. No expiration time + *
  8. 5 seconds expiration time + *
  9. 6 seconds expiration time *
* - * The client can't read data from the first endpoint multiple times - * but can read data from the second and third endpoints. The client - * can't re-read data from the second endpoint 5 seconds later. The - * client can't re-read data from the third endpoint 6 seconds - * later. - *

- * The client can cancel a returned FlightInfo by pre-defined - * CancelFlightInfo action. The client can't read data from endpoints - * even within 6 seconds after the action. - *

- * The client can extend the expiration time of a FlightEndpoint in - * a returned FlightInfo by pre-defined RenewFlightEndpoint - * action. The client can read data from endpoints multiple times + * The client can't read data from the first endpoint multiple times but can read data from the + * second and third endpoints. The client can't re-read data from the second endpoint 5 seconds + * later. The client can't re-read data from the third endpoint 6 seconds later. + * + *

The client can cancel a returned FlightInfo by pre-defined CancelFlightInfo action. The client + * can't read data from endpoints even within 6 seconds after the action. + * + *

The client can extend the expiration time of a FlightEndpoint in a returned FlightInfo by + * pre-defined RenewFlightEndpoint action. The client can read data from endpoints multiple times * within more 10 seconds after the action. */ final class ExpirationTimeProducer extends NoOpFlightProducer { - public static final Schema SCHEMA = new Schema( - Collections.singletonList(Field.notNullable("number", Types.MinorType.UINT4.getType()))); + public static final Schema SCHEMA = + new Schema( + Collections.singletonList(Field.notNullable("number", Types.MinorType.UINT4.getType()))); private final BufferAllocator allocator; private final List statuses; @@ -104,22 +100,28 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l int index = parseIndexFromTicket(ticket); EndpointStatus status = statuses.get(index); if (status.cancelled) { - listener.error(CallStatus.NOT_FOUND - .withDescription("Invalid flight: cancelled: " + - new String(ticket.getBytes(), StandardCharsets.UTF_8)) - .toRuntimeException()); + listener.error( + CallStatus.NOT_FOUND + .withDescription( + "Invalid flight: cancelled: " + + new String(ticket.getBytes(), StandardCharsets.UTF_8)) + .toRuntimeException()); return; } else if (status.expirationTime != null && Instant.now().isAfter(status.expirationTime)) { - listener.error(CallStatus.NOT_FOUND - .withDescription("Invalid flight: expired: " + - new String(ticket.getBytes(), StandardCharsets.UTF_8)) - .toRuntimeException()); + listener.error( + CallStatus.NOT_FOUND + .withDescription( + "Invalid flight: expired: " + + new String(ticket.getBytes(), StandardCharsets.UTF_8)) + .toRuntimeException()); return; } else if (status.expirationTime == null && status.numGets > 0) { - listener.error(CallStatus.NOT_FOUND - .withDescription("Invalid flight: can't read multiple times: " + - new String(ticket.getBytes(), StandardCharsets.UTF_8)) - .toRuntimeException()); + listener.error( + CallStatus.NOT_FOUND + .withDescription( + "Invalid flight: can't read multiple times: " + + new String(ticket.getBytes(), StandardCharsets.UTF_8)) + .toRuntimeException()); return; } status.numGets++; @@ -138,7 +140,8 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l public void doAction(CallContext context, Action action, StreamListener listener) { try { if (action.getType().equals(FlightConstants.CANCEL_FLIGHT_INFO.getType())) { - CancelFlightInfoRequest request = CancelFlightInfoRequest.deserialize(ByteBuffer.wrap(action.getBody())); + CancelFlightInfoRequest request = + CancelFlightInfoRequest.deserialize(ByteBuffer.wrap(action.getBody())); CancelStatus cancelStatus = CancelStatus.UNSPECIFIED; for (FlightEndpoint endpoint : request.getInfo().getEndpoints()) { int index = parseIndexFromTicket(endpoint.getTicket()); @@ -154,14 +157,16 @@ public void doAction(CallContext context, Action action, StreamListener } listener.onNext(new Result(new CancelFlightInfoResult(cancelStatus).serialize().array())); } else if (action.getType().equals(FlightConstants.RENEW_FLIGHT_ENDPOINT.getType())) { - RenewFlightEndpointRequest request = RenewFlightEndpointRequest.deserialize(ByteBuffer.wrap(action.getBody())); + RenewFlightEndpointRequest request = + RenewFlightEndpointRequest.deserialize(ByteBuffer.wrap(action.getBody())); FlightEndpoint endpoint = request.getFlightEndpoint(); int index = parseIndexFromTicket(endpoint.getTicket()); EndpointStatus status = statuses.get(index); if (status.cancelled) { - listener.onError(CallStatus.INVALID_ARGUMENT - .withDescription("Invalid flight: cancelled: " + index) - .toRuntimeException()); + listener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription("Invalid flight: cancelled: " + index) + .toRuntimeException()); return; } @@ -170,17 +175,20 @@ public void doAction(CallContext context, Action action, StreamListener Ticket ticket = new Ticket(ticketBody.getBytes(StandardCharsets.UTF_8)); Instant expiration = Instant.now().plus(10, ChronoUnit.SECONDS); status.expirationTime = expiration; - FlightEndpoint newEndpoint = new FlightEndpoint( - ticket, expiration, endpoint.getLocations().toArray(new Location[0])); + FlightEndpoint newEndpoint = + new FlightEndpoint( + ticket, expiration, endpoint.getLocations().toArray(new Location[0])); listener.onNext(new Result(newEndpoint.serialize().array())); } else { - listener.onError(CallStatus.INVALID_ARGUMENT - .withDescription("Unknown action: " + action.getType()) - .toRuntimeException()); + listener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription("Unknown action: " + action.getType()) + .toRuntimeException()); return; } } catch (IOException | URISyntaxException e) { - listener.onError(CallStatus.INTERNAL.withCause(e).withDescription(e.toString()).toRuntimeException()); + listener.onError( + CallStatus.INTERNAL.withCause(e).withDescription(e.toString()).toRuntimeException()); return; } listener.onCompleted(); @@ -194,7 +202,9 @@ public void listActions(CallContext context, StreamListener listener } private FlightEndpoint addEndpoint(String ticket, Instant expirationTime) { - Ticket flightTicket = new Ticket(String.format("%d: %s", statuses.size(), ticket).getBytes(StandardCharsets.UTF_8)); + Ticket flightTicket = + new Ticket( + String.format("%d: %s", statuses.size(), ticket).getBytes(StandardCharsets.UTF_8)); statuses.add(new EndpointStatus(expirationTime)); return new FlightEndpoint(flightTicket, expirationTime); } @@ -204,8 +214,8 @@ private int parseIndexFromTicket(Ticket ticket) { int index = contents.indexOf(':'); if (index == -1) { throw CallStatus.INVALID_ARGUMENT - .withDescription("Invalid ticket: " + - new String(ticket.getBytes(), StandardCharsets.UTF_8)) + .withDescription( + "Invalid ticket: " + new String(ticket.getBytes(), StandardCharsets.UTF_8)) .toRuntimeException(); } int endpointIndex = Integer.parseInt(contents.substring(0, index)); diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeRenewFlightEndpointScenario.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeRenewFlightEndpointScenario.java index 6f47e2965a2..b2448fdfd99 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeRenewFlightEndpointScenario.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/ExpirationTimeRenewFlightEndpointScenario.java @@ -19,7 +19,6 @@ import java.nio.charset.StandardCharsets; import java.time.Instant; - import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightDescriptor; import org.apache.arrow.flight.FlightEndpoint; @@ -38,12 +37,13 @@ public FlightProducer producer(BufferAllocator allocator, Location location) thr } @Override - public void buildServer(FlightServer.Builder builder) { - } + public void buildServer(FlightServer.Builder builder) {} @Override - public void client(BufferAllocator allocator, Location location, FlightClient client) throws Exception { - FlightInfo info = client.getInfo(FlightDescriptor.command("expiration".getBytes(StandardCharsets.UTF_8))); + public void client(BufferAllocator allocator, Location location, FlightClient client) + throws Exception { + FlightInfo info = + client.getInfo(FlightDescriptor.command("expiration".getBytes(StandardCharsets.UTF_8))); // Renew all endpoints with expiration time for (FlightEndpoint endpoint : info.getEndpoints()) { @@ -53,11 +53,12 @@ public void client(BufferAllocator allocator, Location location, FlightClient cl Instant expiration = endpoint.getExpirationTime().get(); FlightEndpoint renewed = client.renewFlightEndpoint(new RenewFlightEndpointRequest(endpoint)); - IntegrationAssertions.assertTrue("Renewed FlightEndpoint must have expiration time", + IntegrationAssertions.assertTrue( + "Renewed FlightEndpoint must have expiration time", renewed.getExpirationTime().isPresent()); - IntegrationAssertions.assertTrue("Renewed FlightEndpoint must have newer expiration time", + IntegrationAssertions.assertTrue( + "Renewed FlightEndpoint must have newer expiration time", renewed.getExpirationTime().get().isAfter(expiration)); - } } } diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/FlightSqlExtensionScenario.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/FlightSqlExtensionScenario.java index cd20ae4f46f..2abd49d8192 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/FlightSqlExtensionScenario.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/FlightSqlExtensionScenario.java @@ -19,7 +19,6 @@ import java.util.HashMap; import java.util.Map; - import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightInfo; import org.apache.arrow.flight.FlightStream; @@ -38,9 +37,9 @@ import org.apache.arrow.vector.types.pojo.Schema; /** - * Integration test scenario for validating Flight SQL specs across multiple implementations. - * This should ensure that RPC objects are being built and parsed correctly for multiple languages - * and that the Arrow schemas are returned as expected. + * Integration test scenario for validating Flight SQL specs across multiple implementations. This + * should ensure that RPC objects are being built and parsed correctly for multiple languages and + * that the Arrow schemas are returned as expected. */ public class FlightSqlExtensionScenario extends FlightSqlScenario { @Override @@ -61,7 +60,8 @@ private void validateMetadataRetrieval(FlightSqlClient sqlClient) throws Excepti Map infoValues = new HashMap<>(); try (FlightStream stream = sqlClient.getStream(ticket)) { Schema actualSchema = stream.getSchema(); - IntegrationAssertions.assertEquals(FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA, actualSchema); + IntegrationAssertions.assertEquals( + FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA, actualSchema); while (stream.next()) { UInt4Vector infoName = (UInt4Vector) stream.getRoot().getVector(0); @@ -76,9 +76,10 @@ private void validateMetadataRetrieval(FlightSqlClient sqlClient) throws Excepti byte typeId = value.getTypeId(i); switch (typeId) { case 0: // string - object = Preconditions.checkNotNull(value.getVarCharVector(typeId) - .getObject(value.getOffset(i))) - .toString(); + object = + Preconditions.checkNotNull( + value.getVarCharVector(typeId).getObject(value.getOffset(i))) + .toString(); break; case 1: // bool object = value.getBitVector(typeId).getObject(value.getOffset(i)); @@ -97,22 +98,25 @@ private void validateMetadataRetrieval(FlightSqlClient sqlClient) throws Excepti } } - IntegrationAssertions.assertEquals(Boolean.FALSE, - infoValues.get(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_SQL_VALUE)); - IntegrationAssertions.assertEquals(Boolean.TRUE, - infoValues.get(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_SUBSTRAIT_VALUE)); - IntegrationAssertions.assertEquals("min_version", + IntegrationAssertions.assertEquals( + Boolean.FALSE, infoValues.get(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_SQL_VALUE)); + IntegrationAssertions.assertEquals( + Boolean.TRUE, infoValues.get(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_SUBSTRAIT_VALUE)); + IntegrationAssertions.assertEquals( + "min_version", infoValues.get(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_SUBSTRAIT_MIN_VERSION_VALUE)); - IntegrationAssertions.assertEquals("max_version", + IntegrationAssertions.assertEquals( + "max_version", infoValues.get(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_SUBSTRAIT_MAX_VERSION_VALUE)); - IntegrationAssertions.assertEquals(FlightSql.SqlSupportedTransaction.SQL_SUPPORTED_TRANSACTION_SAVEPOINT_VALUE, + IntegrationAssertions.assertEquals( + FlightSql.SqlSupportedTransaction.SQL_SUPPORTED_TRANSACTION_SAVEPOINT_VALUE, infoValues.get(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_TRANSACTION_VALUE)); - IntegrationAssertions.assertEquals(Boolean.TRUE, - infoValues.get(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_CANCEL_VALUE)); - IntegrationAssertions.assertEquals(42, - infoValues.get(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_STATEMENT_TIMEOUT_VALUE)); - IntegrationAssertions.assertEquals(7, - infoValues.get(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_TRANSACTION_TIMEOUT_VALUE)); + IntegrationAssertions.assertEquals( + Boolean.TRUE, infoValues.get(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_CANCEL_VALUE)); + IntegrationAssertions.assertEquals( + 42, infoValues.get(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_STATEMENT_TIMEOUT_VALUE)); + IntegrationAssertions.assertEquals( + 7, infoValues.get(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_TRANSACTION_TIMEOUT_VALUE)); } private void validateStatementExecution(FlightSqlClient sqlClient) throws Exception { @@ -124,15 +128,15 @@ private void validateStatementExecution(FlightSqlClient sqlClient) throws Except IntegrationAssertions.assertEquals(CancelResult.CANCELLED, sqlClient.cancelQuery(info)); - IntegrationAssertions.assertEquals(sqlClient.executeSubstraitUpdate(SUBSTRAIT_PLAN), - UPDATE_STATEMENT_EXPECTED_ROWS); + IntegrationAssertions.assertEquals( + sqlClient.executeSubstraitUpdate(SUBSTRAIT_PLAN), UPDATE_STATEMENT_EXPECTED_ROWS); } - private void validatePreparedStatementExecution(BufferAllocator allocator, - FlightSqlClient sqlClient) throws Exception { + private void validatePreparedStatementExecution( + BufferAllocator allocator, FlightSqlClient sqlClient) throws Exception { try (FlightSqlClient.PreparedStatement preparedStatement = sqlClient.prepare(SUBSTRAIT_PLAN); - VectorSchemaRoot parameters = VectorSchemaRoot.create( - FlightSqlScenarioProducer.getQuerySchema(), allocator)) { + VectorSchemaRoot parameters = + VectorSchemaRoot.create(FlightSqlScenarioProducer.getQuerySchema(), allocator)) { parameters.setRowCount(1); preparedStatement.setParameters(parameters); validate(FlightSqlScenarioProducer.getQuerySchema(), preparedStatement.execute(), sqlClient); @@ -140,16 +144,18 @@ private void validatePreparedStatementExecution(BufferAllocator allocator, } try (FlightSqlClient.PreparedStatement preparedStatement = sqlClient.prepare(SUBSTRAIT_PLAN)) { - IntegrationAssertions.assertEquals(preparedStatement.executeUpdate(), - UPDATE_PREPARED_STATEMENT_EXPECTED_ROWS); + IntegrationAssertions.assertEquals( + preparedStatement.executeUpdate(), UPDATE_PREPARED_STATEMENT_EXPECTED_ROWS); } } - private void validateTransactions(BufferAllocator allocator, FlightSqlClient sqlClient) throws Exception { + private void validateTransactions(BufferAllocator allocator, FlightSqlClient sqlClient) + throws Exception { final FlightSqlClient.Transaction transaction = sqlClient.beginTransaction(); IntegrationAssertions.assertEquals(TRANSACTION_ID, transaction.getTransactionId()); - final FlightSqlClient.Savepoint savepoint = sqlClient.beginSavepoint(transaction, SAVEPOINT_NAME); + final FlightSqlClient.Savepoint savepoint = + sqlClient.beginSavepoint(transaction, SAVEPOINT_NAME); IntegrationAssertions.assertEquals(SAVEPOINT_ID, savepoint.getSavepointId()); FlightInfo info = sqlClient.execute("SELECT STATEMENT", transaction); @@ -164,47 +170,59 @@ private void validateTransactions(BufferAllocator allocator, FlightSqlClient sql schema = sqlClient.getExecuteSubstraitSchema(SUBSTRAIT_PLAN, transaction); validateSchema(FlightSqlScenarioProducer.getQueryWithTransactionSchema(), schema); - IntegrationAssertions.assertEquals(sqlClient.executeUpdate("UPDATE STATEMENT", transaction), + IntegrationAssertions.assertEquals( + sqlClient.executeUpdate("UPDATE STATEMENT", transaction), UPDATE_STATEMENT_WITH_TRANSACTION_EXPECTED_ROWS); - IntegrationAssertions.assertEquals(sqlClient.executeSubstraitUpdate(SUBSTRAIT_PLAN, transaction), + IntegrationAssertions.assertEquals( + sqlClient.executeSubstraitUpdate(SUBSTRAIT_PLAN, transaction), UPDATE_STATEMENT_WITH_TRANSACTION_EXPECTED_ROWS); - try (FlightSqlClient.PreparedStatement preparedStatement = sqlClient.prepare( - "SELECT PREPARED STATEMENT", transaction); - VectorSchemaRoot parameters = VectorSchemaRoot.create( - FlightSqlScenarioProducer.getQuerySchema(), allocator)) { + try (FlightSqlClient.PreparedStatement preparedStatement = + sqlClient.prepare("SELECT PREPARED STATEMENT", transaction); + VectorSchemaRoot parameters = + VectorSchemaRoot.create(FlightSqlScenarioProducer.getQuerySchema(), allocator)) { parameters.setRowCount(1); preparedStatement.setParameters(parameters); - validate(FlightSqlScenarioProducer.getQueryWithTransactionSchema(), preparedStatement.execute(), sqlClient); + validate( + FlightSqlScenarioProducer.getQueryWithTransactionSchema(), + preparedStatement.execute(), + sqlClient); schema = preparedStatement.fetchSchema(); validateSchema(FlightSqlScenarioProducer.getQueryWithTransactionSchema(), schema); } - try (FlightSqlClient.PreparedStatement preparedStatement = sqlClient.prepare(SUBSTRAIT_PLAN, transaction); - VectorSchemaRoot parameters = VectorSchemaRoot.create( - FlightSqlScenarioProducer.getQuerySchema(), allocator)) { + try (FlightSqlClient.PreparedStatement preparedStatement = + sqlClient.prepare(SUBSTRAIT_PLAN, transaction); + VectorSchemaRoot parameters = + VectorSchemaRoot.create(FlightSqlScenarioProducer.getQuerySchema(), allocator)) { parameters.setRowCount(1); preparedStatement.setParameters(parameters); - validate(FlightSqlScenarioProducer.getQueryWithTransactionSchema(), preparedStatement.execute(), sqlClient); + validate( + FlightSqlScenarioProducer.getQueryWithTransactionSchema(), + preparedStatement.execute(), + sqlClient); schema = preparedStatement.fetchSchema(); validateSchema(FlightSqlScenarioProducer.getQueryWithTransactionSchema(), schema); } try (FlightSqlClient.PreparedStatement preparedStatement = - sqlClient.prepare("UPDATE PREPARED STATEMENT", transaction)) { - IntegrationAssertions.assertEquals(preparedStatement.executeUpdate(), + sqlClient.prepare("UPDATE PREPARED STATEMENT", transaction)) { + IntegrationAssertions.assertEquals( + preparedStatement.executeUpdate(), UPDATE_PREPARED_STATEMENT_WITH_TRANSACTION_EXPECTED_ROWS); } try (FlightSqlClient.PreparedStatement preparedStatement = - sqlClient.prepare(SUBSTRAIT_PLAN, transaction)) { - IntegrationAssertions.assertEquals(preparedStatement.executeUpdate(), + sqlClient.prepare(SUBSTRAIT_PLAN, transaction)) { + IntegrationAssertions.assertEquals( + preparedStatement.executeUpdate(), UPDATE_PREPARED_STATEMENT_WITH_TRANSACTION_EXPECTED_ROWS); } sqlClient.rollback(savepoint); - final FlightSqlClient.Savepoint savepoint2 = sqlClient.beginSavepoint(transaction, SAVEPOINT_NAME); + final FlightSqlClient.Savepoint savepoint2 = + sqlClient.beginSavepoint(transaction, SAVEPOINT_NAME); IntegrationAssertions.assertEquals(SAVEPOINT_ID, savepoint2.getSavepointId()); sqlClient.release(savepoint); diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/FlightSqlScenario.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/FlightSqlScenario.java index 71f1f741d58..6332f7a484a 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/FlightSqlScenario.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/FlightSqlScenario.java @@ -19,7 +19,6 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; - import org.apache.arrow.flight.CallOption; import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightInfo; @@ -38,9 +37,9 @@ import org.apache.arrow.vector.types.pojo.Schema; /** - * Integration test scenario for validating Flight SQL specs across multiple implementations. - * This should ensure that RPC objects are being built and parsed correctly for multiple languages - * and that the Arrow schemas are returned as expected. + * Integration test scenario for validating Flight SQL specs across multiple implementations. This + * should ensure that RPC objects are being built and parsed correctly for multiple languages and + * that the Arrow schemas are returned as expected. */ public class FlightSqlScenario implements Scenario { public static final long UPDATE_STATEMENT_EXPECTED_ROWS = 10000L; @@ -61,9 +60,7 @@ public FlightProducer producer(BufferAllocator allocator, Location location) thr } @Override - public void buildServer(FlightServer.Builder builder) throws Exception { - - } + public void buildServer(FlightServer.Builder builder) throws Exception {} @Override public void client(BufferAllocator allocator, Location location, FlightClient client) @@ -78,72 +75,111 @@ public void client(BufferAllocator allocator, Location location, FlightClient cl private void validateMetadataRetrieval(FlightSqlClient sqlClient) throws Exception { final CallOption[] options = new CallOption[0]; - validate(FlightSqlProducer.Schemas.GET_CATALOGS_SCHEMA, sqlClient.getCatalogs(options), - sqlClient); - validateSchema(FlightSqlProducer.Schemas.GET_CATALOGS_SCHEMA, sqlClient.getCatalogsSchema(options)); + validate( + FlightSqlProducer.Schemas.GET_CATALOGS_SCHEMA, sqlClient.getCatalogs(options), sqlClient); + validateSchema( + FlightSqlProducer.Schemas.GET_CATALOGS_SCHEMA, sqlClient.getCatalogsSchema(options)); - validate(FlightSqlProducer.Schemas.GET_SCHEMAS_SCHEMA, + validate( + FlightSqlProducer.Schemas.GET_SCHEMAS_SCHEMA, sqlClient.getSchemas("catalog", "db_schema_filter_pattern", options), sqlClient); validateSchema(FlightSqlProducer.Schemas.GET_SCHEMAS_SCHEMA, sqlClient.getSchemasSchema()); - validate(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA, - sqlClient.getTables("catalog", "db_schema_filter_pattern", "table_filter_pattern", - Arrays.asList("table", "view"), true, options), sqlClient); - validateSchema(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA, - sqlClient.getTablesSchema(/*includeSchema*/true, options)); - validateSchema(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA_NO_SCHEMA, - sqlClient.getTablesSchema(/*includeSchema*/false, options)); - - validate(FlightSqlProducer.Schemas.GET_TABLE_TYPES_SCHEMA, sqlClient.getTableTypes(options), sqlClient); - validateSchema(FlightSqlProducer.Schemas.GET_TABLE_TYPES_SCHEMA, sqlClient.getTableTypesSchema(options)); + validate( + FlightSqlProducer.Schemas.GET_TABLES_SCHEMA, + sqlClient.getTables( + "catalog", + "db_schema_filter_pattern", + "table_filter_pattern", + Arrays.asList("table", "view"), + true, + options), + sqlClient); + validateSchema( + FlightSqlProducer.Schemas.GET_TABLES_SCHEMA, + sqlClient.getTablesSchema(/*includeSchema*/ true, options)); + validateSchema( + FlightSqlProducer.Schemas.GET_TABLES_SCHEMA_NO_SCHEMA, + sqlClient.getTablesSchema(/*includeSchema*/ false, options)); + + validate( + FlightSqlProducer.Schemas.GET_TABLE_TYPES_SCHEMA, + sqlClient.getTableTypes(options), + sqlClient); + validateSchema( + FlightSqlProducer.Schemas.GET_TABLE_TYPES_SCHEMA, sqlClient.getTableTypesSchema(options)); - validate(FlightSqlProducer.Schemas.GET_PRIMARY_KEYS_SCHEMA, + validate( + FlightSqlProducer.Schemas.GET_PRIMARY_KEYS_SCHEMA, sqlClient.getPrimaryKeys(TableRef.of("catalog", "db_schema", "table"), options), sqlClient); - validateSchema(FlightSqlProducer.Schemas.GET_PRIMARY_KEYS_SCHEMA, sqlClient.getPrimaryKeysSchema(options)); + validateSchema( + FlightSqlProducer.Schemas.GET_PRIMARY_KEYS_SCHEMA, sqlClient.getPrimaryKeysSchema(options)); - validate(FlightSqlProducer.Schemas.GET_EXPORTED_KEYS_SCHEMA, + validate( + FlightSqlProducer.Schemas.GET_EXPORTED_KEYS_SCHEMA, sqlClient.getExportedKeys(TableRef.of("catalog", "db_schema", "table"), options), sqlClient); - validateSchema(FlightSqlProducer.Schemas.GET_EXPORTED_KEYS_SCHEMA, sqlClient.getExportedKeysSchema(options)); + validateSchema( + FlightSqlProducer.Schemas.GET_EXPORTED_KEYS_SCHEMA, + sqlClient.getExportedKeysSchema(options)); - validate(FlightSqlProducer.Schemas.GET_IMPORTED_KEYS_SCHEMA, + validate( + FlightSqlProducer.Schemas.GET_IMPORTED_KEYS_SCHEMA, sqlClient.getImportedKeys(TableRef.of("catalog", "db_schema", "table"), options), sqlClient); - validateSchema(FlightSqlProducer.Schemas.GET_IMPORTED_KEYS_SCHEMA, sqlClient.getImportedKeysSchema(options)); - - validate(FlightSqlProducer.Schemas.GET_CROSS_REFERENCE_SCHEMA, - sqlClient.getCrossReference(TableRef.of("pk_catalog", "pk_db_schema", "pk_table"), - TableRef.of("fk_catalog", "fk_db_schema", "fk_table"), options), + validateSchema( + FlightSqlProducer.Schemas.GET_IMPORTED_KEYS_SCHEMA, + sqlClient.getImportedKeysSchema(options)); + + validate( + FlightSqlProducer.Schemas.GET_CROSS_REFERENCE_SCHEMA, + sqlClient.getCrossReference( + TableRef.of("pk_catalog", "pk_db_schema", "pk_table"), + TableRef.of("fk_catalog", "fk_db_schema", "fk_table"), + options), sqlClient); - validateSchema(FlightSqlProducer.Schemas.GET_CROSS_REFERENCE_SCHEMA, sqlClient.getCrossReferenceSchema(options)); - - validate(FlightSqlProducer.Schemas.GET_TYPE_INFO_SCHEMA, sqlClient.getXdbcTypeInfo(options), sqlClient); - validateSchema(FlightSqlProducer.Schemas.GET_TYPE_INFO_SCHEMA, sqlClient.getXdbcTypeInfoSchema(options)); + validateSchema( + FlightSqlProducer.Schemas.GET_CROSS_REFERENCE_SCHEMA, + sqlClient.getCrossReferenceSchema(options)); - validate(FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA, - sqlClient.getSqlInfo(new FlightSql.SqlInfo[] {FlightSql.SqlInfo.FLIGHT_SQL_SERVER_NAME, - FlightSql.SqlInfo.FLIGHT_SQL_SERVER_READ_ONLY}, options), sqlClient); - validateSchema(FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA, sqlClient.getSqlInfoSchema(options)); + validate( + FlightSqlProducer.Schemas.GET_TYPE_INFO_SCHEMA, + sqlClient.getXdbcTypeInfo(options), + sqlClient); + validateSchema( + FlightSqlProducer.Schemas.GET_TYPE_INFO_SCHEMA, sqlClient.getXdbcTypeInfoSchema(options)); + + validate( + FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA, + sqlClient.getSqlInfo( + new FlightSql.SqlInfo[] { + FlightSql.SqlInfo.FLIGHT_SQL_SERVER_NAME, + FlightSql.SqlInfo.FLIGHT_SQL_SERVER_READ_ONLY + }, + options), + sqlClient); + validateSchema( + FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA, sqlClient.getSqlInfoSchema(options)); } private void validateStatementExecution(FlightSqlClient sqlClient) throws Exception { FlightInfo info = sqlClient.execute("SELECT STATEMENT"); validate(FlightSqlScenarioProducer.getQuerySchema(), info, sqlClient); - validateSchema(FlightSqlScenarioProducer.getQuerySchema(), - sqlClient.getExecuteSchema("SELECT STATEMENT")); + validateSchema( + FlightSqlScenarioProducer.getQuerySchema(), sqlClient.getExecuteSchema("SELECT STATEMENT")); - IntegrationAssertions.assertEquals(sqlClient.executeUpdate("UPDATE STATEMENT"), - UPDATE_STATEMENT_EXPECTED_ROWS); + IntegrationAssertions.assertEquals( + sqlClient.executeUpdate("UPDATE STATEMENT"), UPDATE_STATEMENT_EXPECTED_ROWS); } - private void validatePreparedStatementExecution(BufferAllocator allocator, - FlightSqlClient sqlClient) throws Exception { - try (FlightSqlClient.PreparedStatement preparedStatement = sqlClient.prepare( - "SELECT PREPARED STATEMENT"); - VectorSchemaRoot parameters = VectorSchemaRoot.create( - FlightSqlScenarioProducer.getQuerySchema(), allocator)) { + private void validatePreparedStatementExecution( + BufferAllocator allocator, FlightSqlClient sqlClient) throws Exception { + try (FlightSqlClient.PreparedStatement preparedStatement = + sqlClient.prepare("SELECT PREPARED STATEMENT"); + VectorSchemaRoot parameters = + VectorSchemaRoot.create(FlightSqlScenarioProducer.getQuerySchema(), allocator)) { parameters.setRowCount(1); preparedStatement.setParameters(parameters); validate(FlightSqlScenarioProducer.getQuerySchema(), preparedStatement.execute(), sqlClient); @@ -151,14 +187,14 @@ private void validatePreparedStatementExecution(BufferAllocator allocator, } try (FlightSqlClient.PreparedStatement preparedStatement = - sqlClient.prepare("UPDATE PREPARED STATEMENT")) { - IntegrationAssertions.assertEquals(preparedStatement.executeUpdate(), - UPDATE_PREPARED_STATEMENT_EXPECTED_ROWS); + sqlClient.prepare("UPDATE PREPARED STATEMENT")) { + IntegrationAssertions.assertEquals( + preparedStatement.executeUpdate(), UPDATE_PREPARED_STATEMENT_EXPECTED_ROWS); } } - protected void validate(Schema expectedSchema, FlightInfo flightInfo, - FlightSqlClient sqlClient) throws Exception { + protected void validate(Schema expectedSchema, FlightInfo flightInfo, FlightSqlClient sqlClient) + throws Exception { Ticket ticket = flightInfo.getEndpoints().get(0).getTicket(); try (FlightStream stream = sqlClient.getStream(ticket)) { Schema actualSchema = stream.getSchema(); diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/FlightSqlScenarioProducer.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/FlightSqlScenarioProducer.java index 4ed9a3df0fc..5685ad6cacb 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/FlightSqlScenarioProducer.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/FlightSqlScenarioProducer.java @@ -17,10 +17,13 @@ package org.apache.arrow.flight.integration.tests; +import com.google.protobuf.Any; +import com.google.protobuf.ByteString; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.Message; import java.util.Arrays; import java.util.Collections; import java.util.List; - import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.Criteria; import org.apache.arrow.flight.FlightDescriptor; @@ -44,14 +47,7 @@ import org.apache.arrow.vector.types.pojo.FieldType; import org.apache.arrow.vector.types.pojo.Schema; -import com.google.protobuf.Any; -import com.google.protobuf.ByteString; -import com.google.protobuf.InvalidProtocolBufferException; -import com.google.protobuf.Message; - -/** - * Hardcoded Flight SQL producer used for cross-language integration tests. - */ +/** Hardcoded Flight SQL producer used for cross-language integration tests. */ public class FlightSqlScenarioProducer implements FlightSqlProducer { private final BufferAllocator allocator; @@ -60,157 +56,200 @@ public FlightSqlScenarioProducer(BufferAllocator allocator) { } /** - * Schema to be returned for mocking the statement/prepared statement results. - * Must be the same across all languages. + * Schema to be returned for mocking the statement/prepared statement results. Must be the same + * across all languages. */ static Schema getQuerySchema() { return new Schema( Collections.singletonList( - new Field("id", new FieldType(true, new ArrowType.Int(64, true), - null, new FlightSqlColumnMetadata.Builder() - .tableName("test") - .isAutoIncrement(true) - .isCaseSensitive(false) - .typeName("type_test") - .schemaName("schema_test") - .isSearchable(true) - .catalogName("catalog_test") - .precision(100) - .build().getMetadataMap()), null) - ) - ); + new Field( + "id", + new FieldType( + true, + new ArrowType.Int(64, true), + null, + new FlightSqlColumnMetadata.Builder() + .tableName("test") + .isAutoIncrement(true) + .isCaseSensitive(false) + .typeName("type_test") + .schemaName("schema_test") + .isSearchable(true) + .catalogName("catalog_test") + .precision(100) + .build() + .getMetadataMap()), + null))); } /** * The expected schema for queries with transactions. - *

- * Must be the same across all languages. + * + *

Must be the same across all languages. */ static Schema getQueryWithTransactionSchema() { return new Schema( Collections.singletonList( - new Field("pkey", new FieldType(true, new ArrowType.Int(32, true), - null, new FlightSqlColumnMetadata.Builder() - .tableName("test") - .isAutoIncrement(true) - .isCaseSensitive(false) - .typeName("type_test") - .schemaName("schema_test") - .isSearchable(true) - .catalogName("catalog_test") - .precision(100) - .build().getMetadataMap()), null) - ) - ); - } - - @Override - public void beginSavepoint(FlightSql.ActionBeginSavepointRequest request, CallContext context, - StreamListener listener) { + new Field( + "pkey", + new FieldType( + true, + new ArrowType.Int(32, true), + null, + new FlightSqlColumnMetadata.Builder() + .tableName("test") + .isAutoIncrement(true) + .isCaseSensitive(false) + .typeName("type_test") + .schemaName("schema_test") + .isSearchable(true) + .catalogName("catalog_test") + .precision(100) + .build() + .getMetadataMap()), + null))); + } + + @Override + public void beginSavepoint( + FlightSql.ActionBeginSavepointRequest request, + CallContext context, + StreamListener listener) { if (!request.getName().equals(FlightSqlScenario.SAVEPOINT_NAME)) { - listener.onError(CallStatus.INVALID_ARGUMENT - .withDescription(String.format("Expected name '%s', not '%s'", - FlightSqlScenario.SAVEPOINT_NAME, request.getName())) - .toRuntimeException()); + listener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription( + String.format( + "Expected name '%s', not '%s'", + FlightSqlScenario.SAVEPOINT_NAME, request.getName())) + .toRuntimeException()); return; } - if (!Arrays.equals(request.getTransactionId().toByteArray(), FlightSqlScenario.TRANSACTION_ID)) { - listener.onError(CallStatus.INVALID_ARGUMENT - .withDescription(String.format("Expected transaction ID '%s', not '%s'", - Arrays.toString(FlightSqlScenario.TRANSACTION_ID), - Arrays.toString(request.getTransactionId().toByteArray()))) - .toRuntimeException()); + if (!Arrays.equals( + request.getTransactionId().toByteArray(), FlightSqlScenario.TRANSACTION_ID)) { + listener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription( + String.format( + "Expected transaction ID '%s', not '%s'", + Arrays.toString(FlightSqlScenario.TRANSACTION_ID), + Arrays.toString(request.getTransactionId().toByteArray()))) + .toRuntimeException()); return; } - listener.onNext(FlightSql.ActionBeginSavepointResult.newBuilder() - .setSavepointId(ByteString.copyFrom(FlightSqlScenario.SAVEPOINT_ID)) - .build()); + listener.onNext( + FlightSql.ActionBeginSavepointResult.newBuilder() + .setSavepointId(ByteString.copyFrom(FlightSqlScenario.SAVEPOINT_ID)) + .build()); listener.onCompleted(); } @Override - public void beginTransaction(FlightSql.ActionBeginTransactionRequest request, CallContext context, - StreamListener listener) { - listener.onNext(FlightSql.ActionBeginTransactionResult.newBuilder() - .setTransactionId(ByteString.copyFrom(FlightSqlScenario.TRANSACTION_ID)) - .build()); + public void beginTransaction( + FlightSql.ActionBeginTransactionRequest request, + CallContext context, + StreamListener listener) { + listener.onNext( + FlightSql.ActionBeginTransactionResult.newBuilder() + .setTransactionId(ByteString.copyFrom(FlightSqlScenario.TRANSACTION_ID)) + .build()); listener.onCompleted(); } @Override - public void cancelQuery(FlightInfo info, CallContext context, StreamListener listener) { + public void cancelQuery( + FlightInfo info, CallContext context, StreamListener listener) { final String expectedTicket = "PLAN HANDLE"; if (info.getEndpoints().size() != 1) { - listener.onError(CallStatus.INVALID_ARGUMENT - .withDescription(String.format("Expected 1 endpoint, got %d", info.getEndpoints().size())) - .toRuntimeException()); + listener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription( + String.format("Expected 1 endpoint, got %d", info.getEndpoints().size())) + .toRuntimeException()); } final FlightEndpoint endpoint = info.getEndpoints().get(0); try { final Any any = Any.parseFrom(endpoint.getTicket().getBytes()); if (!any.is(FlightSql.TicketStatementQuery.class)) { - listener.onError(CallStatus.INVALID_ARGUMENT - .withDescription(String.format("Expected TicketStatementQuery, found '%s'", any.getTypeUrl())) - .toRuntimeException()); + listener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription( + String.format("Expected TicketStatementQuery, found '%s'", any.getTypeUrl())) + .toRuntimeException()); } - final FlightSql.TicketStatementQuery ticket = any.unpack(FlightSql.TicketStatementQuery.class); + final FlightSql.TicketStatementQuery ticket = + any.unpack(FlightSql.TicketStatementQuery.class); if (!ticket.getStatementHandle().toStringUtf8().equals(expectedTicket)) { - listener.onError(CallStatus.INVALID_ARGUMENT - .withDescription(String.format("Expected ticket '%s'", expectedTicket)) - .toRuntimeException()); + listener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription(String.format("Expected ticket '%s'", expectedTicket)) + .toRuntimeException()); } listener.onNext(CancelResult.CANCELLED); listener.onCompleted(); } catch (InvalidProtocolBufferException e) { - listener.onError(CallStatus.INVALID_ARGUMENT - .withDescription("Invalid Protobuf:" + e) - .withCause(e) - .toRuntimeException()); + listener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription("Invalid Protobuf:" + e) + .withCause(e) + .toRuntimeException()); } } @Override - public void createPreparedStatement(FlightSql.ActionCreatePreparedStatementRequest request, - CallContext context, StreamListener listener) { - IntegrationAssertions.assertTrue("Expect to be one of the two queries used on tests", - request.getQuery().equals("SELECT PREPARED STATEMENT") || - request.getQuery().equals("UPDATE PREPARED STATEMENT")); + public void createPreparedStatement( + FlightSql.ActionCreatePreparedStatementRequest request, + CallContext context, + StreamListener listener) { + IntegrationAssertions.assertTrue( + "Expect to be one of the two queries used on tests", + request.getQuery().equals("SELECT PREPARED STATEMENT") + || request.getQuery().equals("UPDATE PREPARED STATEMENT")); String text = request.getQuery(); if (!request.getTransactionId().isEmpty()) { text += " WITH TXN"; } text += " HANDLE"; - final FlightSql.ActionCreatePreparedStatementResult - result = FlightSql.ActionCreatePreparedStatementResult.newBuilder() - .setPreparedStatementHandle(ByteString.copyFromUtf8(text)) - .build(); + final FlightSql.ActionCreatePreparedStatementResult result = + FlightSql.ActionCreatePreparedStatementResult.newBuilder() + .setPreparedStatementHandle(ByteString.copyFromUtf8(text)) + .build(); listener.onNext(new Result(Any.pack(result).toByteArray())); listener.onCompleted(); } @Override - public void createPreparedSubstraitPlan(FlightSql.ActionCreatePreparedSubstraitPlanRequest request, - CallContext context, - StreamListener listener) { - if (!Arrays.equals(request.getPlan().getPlan().toByteArray(), FlightSqlScenario.SUBSTRAIT_PLAN_TEXT)) { - listener.onError(CallStatus.INVALID_ARGUMENT - .withDescription(String.format("Expected plan '%s', not '%s'", - Arrays.toString(FlightSqlScenario.SUBSTRAIT_PLAN_TEXT), - Arrays.toString(request.getPlan().getPlan().toByteArray()))) - .toRuntimeException()); + public void createPreparedSubstraitPlan( + FlightSql.ActionCreatePreparedSubstraitPlanRequest request, + CallContext context, + StreamListener listener) { + if (!Arrays.equals( + request.getPlan().getPlan().toByteArray(), FlightSqlScenario.SUBSTRAIT_PLAN_TEXT)) { + listener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription( + String.format( + "Expected plan '%s', not '%s'", + Arrays.toString(FlightSqlScenario.SUBSTRAIT_PLAN_TEXT), + Arrays.toString(request.getPlan().getPlan().toByteArray()))) + .toRuntimeException()); return; } if (!FlightSqlScenario.SUBSTRAIT_VERSION.equals(request.getPlan().getVersion())) { - listener.onError(CallStatus.INVALID_ARGUMENT - .withDescription(String.format("Expected version '%s', not '%s'", - FlightSqlScenario.SUBSTRAIT_VERSION, - request.getPlan().getVersion())) - .toRuntimeException()); + listener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription( + String.format( + "Expected version '%s', not '%s'", + FlightSqlScenario.SUBSTRAIT_VERSION, request.getPlan().getVersion())) + .toRuntimeException()); return; } - final String handle = request.getTransactionId().isEmpty() ? - "PREPARED PLAN HANDLE" : "PREPARED PLAN WITH TXN HANDLE"; + final String handle = + request.getTransactionId().isEmpty() + ? "PREPARED PLAN HANDLE" + : "PREPARED PLAN WITH TXN HANDLE"; final FlightSql.ActionCreatePreparedStatementResult result = FlightSql.ActionCreatePreparedStatementResult.newBuilder() .setPreparedStatementHandle(ByteString.copyFromUtf8(handle)) @@ -220,116 +259,141 @@ public void createPreparedSubstraitPlan(FlightSql.ActionCreatePreparedSubstraitP } @Override - public void closePreparedStatement(FlightSql.ActionClosePreparedStatementRequest request, - CallContext context, StreamListener listener) { + public void closePreparedStatement( + FlightSql.ActionClosePreparedStatementRequest request, + CallContext context, + StreamListener listener) { final String handle = request.getPreparedStatementHandle().toStringUtf8(); - IntegrationAssertions.assertTrue("Expect to be one of the queries used on tests", - handle.equals("SELECT PREPARED STATEMENT HANDLE") || - handle.equals("SELECT PREPARED STATEMENT WITH TXN HANDLE") || - handle.equals("UPDATE PREPARED STATEMENT HANDLE") || - handle.equals("UPDATE PREPARED STATEMENT WITH TXN HANDLE") || - handle.equals("PREPARED PLAN HANDLE") || - handle.equals("PREPARED PLAN WITH TXN HANDLE")); + IntegrationAssertions.assertTrue( + "Expect to be one of the queries used on tests", + handle.equals("SELECT PREPARED STATEMENT HANDLE") + || handle.equals("SELECT PREPARED STATEMENT WITH TXN HANDLE") + || handle.equals("UPDATE PREPARED STATEMENT HANDLE") + || handle.equals("UPDATE PREPARED STATEMENT WITH TXN HANDLE") + || handle.equals("PREPARED PLAN HANDLE") + || handle.equals("PREPARED PLAN WITH TXN HANDLE")); listener.onCompleted(); } @Override - public void endSavepoint(FlightSql.ActionEndSavepointRequest request, CallContext context, - StreamListener listener) { + public void endSavepoint( + FlightSql.ActionEndSavepointRequest request, + CallContext context, + StreamListener listener) { switch (request.getAction()) { case END_SAVEPOINT_RELEASE: case END_SAVEPOINT_ROLLBACK: - if (!Arrays.equals(request.getSavepointId().toByteArray(), FlightSqlScenario.SAVEPOINT_ID)) { - listener.onError(CallStatus.INVALID_ARGUMENT - .withDescription("Unexpected ID: " + Arrays.toString(request.getSavepointId().toByteArray())) - .toRuntimeException()); + if (!Arrays.equals( + request.getSavepointId().toByteArray(), FlightSqlScenario.SAVEPOINT_ID)) { + listener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription( + "Unexpected ID: " + Arrays.toString(request.getSavepointId().toByteArray())) + .toRuntimeException()); } break; case UNRECOGNIZED: - default: { - listener.onError(CallStatus.INVALID_ARGUMENT - .withDescription("Unknown action: " + request.getAction()) - .toRuntimeException()); - return; - } + default: + { + listener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription("Unknown action: " + request.getAction()) + .toRuntimeException()); + return; + } } listener.onCompleted(); } @Override - public void endTransaction(FlightSql.ActionEndTransactionRequest request, CallContext context, - StreamListener listener) { + public void endTransaction( + FlightSql.ActionEndTransactionRequest request, + CallContext context, + StreamListener listener) { switch (request.getAction()) { case END_TRANSACTION_COMMIT: case END_TRANSACTION_ROLLBACK: - if (!Arrays.equals(request.getTransactionId().toByteArray(), FlightSqlScenario.TRANSACTION_ID)) { - listener.onError(CallStatus.INVALID_ARGUMENT - .withDescription("Unexpected ID: " + Arrays.toString(request.getTransactionId().toByteArray())) - .toRuntimeException()); + if (!Arrays.equals( + request.getTransactionId().toByteArray(), FlightSqlScenario.TRANSACTION_ID)) { + listener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription( + "Unexpected ID: " + Arrays.toString(request.getTransactionId().toByteArray())) + .toRuntimeException()); } break; case UNRECOGNIZED: - default: { - listener.onError(CallStatus.INVALID_ARGUMENT - .withDescription("Unknown action: " + request.getAction()) - .toRuntimeException()); - return; - } + default: + { + listener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription("Unknown action: " + request.getAction()) + .toRuntimeException()); + return; + } } listener.onCompleted(); } @Override - public FlightInfo getFlightInfoStatement(FlightSql.CommandStatementQuery command, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoStatement( + FlightSql.CommandStatementQuery command, CallContext context, FlightDescriptor descriptor) { IntegrationAssertions.assertEquals(command.getQuery(), "SELECT STATEMENT"); if (command.getTransactionId().isEmpty()) { String handle = "SELECT STATEMENT HANDLE"; - FlightSql.TicketStatementQuery ticket = FlightSql.TicketStatementQuery.newBuilder() - .setStatementHandle(ByteString.copyFromUtf8(handle)) - .build(); + FlightSql.TicketStatementQuery ticket = + FlightSql.TicketStatementQuery.newBuilder() + .setStatementHandle(ByteString.copyFromUtf8(handle)) + .build(); return getFlightInfoForSchema(ticket, descriptor, getQuerySchema()); } else { String handle = "SELECT STATEMENT WITH TXN HANDLE"; - FlightSql.TicketStatementQuery ticket = FlightSql.TicketStatementQuery.newBuilder() - .setStatementHandle(ByteString.copyFromUtf8(handle)) - .build(); + FlightSql.TicketStatementQuery ticket = + FlightSql.TicketStatementQuery.newBuilder() + .setStatementHandle(ByteString.copyFromUtf8(handle)) + .build(); return getFlightInfoForSchema(ticket, descriptor, getQueryWithTransactionSchema()); } } @Override - public FlightInfo getFlightInfoSubstraitPlan(FlightSql.CommandStatementSubstraitPlan command, CallContext context, - FlightDescriptor descriptor) { - IntegrationAssertions.assertEquals(command.getPlan().getPlan().toByteArray(), - FlightSqlScenario.SUBSTRAIT_PLAN_TEXT); - IntegrationAssertions.assertEquals(command.getPlan().getVersion(), FlightSqlScenario.SUBSTRAIT_VERSION); - String handle = command.getTransactionId().isEmpty() ? - "PLAN HANDLE" : "PLAN WITH TXN HANDLE"; - FlightSql.TicketStatementQuery ticket = FlightSql.TicketStatementQuery.newBuilder() - .setStatementHandle(ByteString.copyFromUtf8(handle)) - .build(); + public FlightInfo getFlightInfoSubstraitPlan( + FlightSql.CommandStatementSubstraitPlan command, + CallContext context, + FlightDescriptor descriptor) { + IntegrationAssertions.assertEquals( + command.getPlan().getPlan().toByteArray(), FlightSqlScenario.SUBSTRAIT_PLAN_TEXT); + IntegrationAssertions.assertEquals( + command.getPlan().getVersion(), FlightSqlScenario.SUBSTRAIT_VERSION); + String handle = command.getTransactionId().isEmpty() ? "PLAN HANDLE" : "PLAN WITH TXN HANDLE"; + FlightSql.TicketStatementQuery ticket = + FlightSql.TicketStatementQuery.newBuilder() + .setStatementHandle(ByteString.copyFromUtf8(handle)) + .build(); return getFlightInfoForSchema(ticket, descriptor, getQuerySchema()); } @Override - public FlightInfo getFlightInfoPreparedStatement(FlightSql.CommandPreparedStatementQuery command, - CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoPreparedStatement( + FlightSql.CommandPreparedStatementQuery command, + CallContext context, + FlightDescriptor descriptor) { String handle = command.getPreparedStatementHandle().toStringUtf8(); - if (handle.equals("SELECT PREPARED STATEMENT HANDLE") || - handle.equals("PREPARED PLAN HANDLE")) { + if (handle.equals("SELECT PREPARED STATEMENT HANDLE") + || handle.equals("PREPARED PLAN HANDLE")) { return getFlightInfoForSchema(command, descriptor, getQuerySchema()); - } else if (handle.equals("SELECT PREPARED STATEMENT WITH TXN HANDLE") || - handle.equals("PREPARED PLAN WITH TXN HANDLE")) { + } else if (handle.equals("SELECT PREPARED STATEMENT WITH TXN HANDLE") + || handle.equals("PREPARED PLAN WITH TXN HANDLE")) { return getFlightInfoForSchema(command, descriptor, getQueryWithTransactionSchema()); } - throw CallStatus.INVALID_ARGUMENT.withDescription("Unknown handle: " + handle).toRuntimeException(); + throw CallStatus.INVALID_ARGUMENT + .withDescription("Unknown handle: " + handle) + .toRuntimeException(); } @Override - public SchemaResult getSchemaStatement(FlightSql.CommandStatementQuery command, - CallContext context, FlightDescriptor descriptor) { + public SchemaResult getSchemaStatement( + FlightSql.CommandStatementQuery command, CallContext context, FlightDescriptor descriptor) { IntegrationAssertions.assertEquals(command.getQuery(), "SELECT STATEMENT"); if (command.getTransactionId().isEmpty()) { return new SchemaResult(getQuerySchema()); @@ -338,34 +402,44 @@ public SchemaResult getSchemaStatement(FlightSql.CommandStatementQuery command, } @Override - public SchemaResult getSchemaPreparedStatement(FlightSql.CommandPreparedStatementQuery command, CallContext context, - FlightDescriptor descriptor) { + public SchemaResult getSchemaPreparedStatement( + FlightSql.CommandPreparedStatementQuery command, + CallContext context, + FlightDescriptor descriptor) { String handle = command.getPreparedStatementHandle().toStringUtf8(); - if (handle.equals("SELECT PREPARED STATEMENT HANDLE") || - handle.equals("PREPARED PLAN HANDLE")) { + if (handle.equals("SELECT PREPARED STATEMENT HANDLE") + || handle.equals("PREPARED PLAN HANDLE")) { return new SchemaResult(getQuerySchema()); - } else if (handle.equals("SELECT PREPARED STATEMENT WITH TXN HANDLE") || - handle.equals("PREPARED PLAN WITH TXN HANDLE")) { + } else if (handle.equals("SELECT PREPARED STATEMENT WITH TXN HANDLE") + || handle.equals("PREPARED PLAN WITH TXN HANDLE")) { return new SchemaResult(getQueryWithTransactionSchema()); } - throw CallStatus.INVALID_ARGUMENT.withDescription("Unknown handle: " + handle).toRuntimeException(); + throw CallStatus.INVALID_ARGUMENT + .withDescription("Unknown handle: " + handle) + .toRuntimeException(); } @Override - public SchemaResult getSchemaSubstraitPlan(FlightSql.CommandStatementSubstraitPlan command, CallContext context, - FlightDescriptor descriptor) { - if (!Arrays.equals(command.getPlan().getPlan().toByteArray(), FlightSqlScenario.SUBSTRAIT_PLAN_TEXT)) { + public SchemaResult getSchemaSubstraitPlan( + FlightSql.CommandStatementSubstraitPlan command, + CallContext context, + FlightDescriptor descriptor) { + if (!Arrays.equals( + command.getPlan().getPlan().toByteArray(), FlightSqlScenario.SUBSTRAIT_PLAN_TEXT)) { throw CallStatus.INVALID_ARGUMENT - .withDescription(String.format("Expected plan '%s', not '%s'", - Arrays.toString(FlightSqlScenario.SUBSTRAIT_PLAN_TEXT), - Arrays.toString(command.getPlan().getPlan().toByteArray()))) + .withDescription( + String.format( + "Expected plan '%s', not '%s'", + Arrays.toString(FlightSqlScenario.SUBSTRAIT_PLAN_TEXT), + Arrays.toString(command.getPlan().getPlan().toByteArray()))) .toRuntimeException(); } if (!FlightSqlScenario.SUBSTRAIT_VERSION.equals(command.getPlan().getVersion())) { throw CallStatus.INVALID_ARGUMENT - .withDescription(String.format("Expected version '%s', not '%s'", - FlightSqlScenario.SUBSTRAIT_VERSION, - command.getPlan().getVersion())) + .withDescription( + String.format( + "Expected version '%s', not '%s'", + FlightSqlScenario.SUBSTRAIT_VERSION, command.getPlan().getVersion())) .toRuntimeException(); } if (command.getTransactionId().isEmpty()) { @@ -375,31 +449,39 @@ public SchemaResult getSchemaSubstraitPlan(FlightSql.CommandStatementSubstraitPl } @Override - public void getStreamStatement(FlightSql.TicketStatementQuery ticket, CallContext context, - ServerStreamListener listener) { + public void getStreamStatement( + FlightSql.TicketStatementQuery ticket, CallContext context, ServerStreamListener listener) { final String handle = ticket.getStatementHandle().toStringUtf8(); if (handle.equals("SELECT STATEMENT HANDLE") || handle.equals("PLAN HANDLE")) { putEmptyBatchToStreamListener(listener, getQuerySchema()); - } else if (handle.equals("SELECT STATEMENT WITH TXN HANDLE") || handle.equals("PLAN WITH TXN HANDLE")) { + } else if (handle.equals("SELECT STATEMENT WITH TXN HANDLE") + || handle.equals("PLAN WITH TXN HANDLE")) { putEmptyBatchToStreamListener(listener, getQueryWithTransactionSchema()); } else { - listener.error(CallStatus.INVALID_ARGUMENT.withDescription("Unknown handle: " + handle).toRuntimeException()); + listener.error( + CallStatus.INVALID_ARGUMENT + .withDescription("Unknown handle: " + handle) + .toRuntimeException()); } } @Override - public void getStreamPreparedStatement(FlightSql.CommandPreparedStatementQuery command, - CallContext context, ServerStreamListener listener) { + public void getStreamPreparedStatement( + FlightSql.CommandPreparedStatementQuery command, + CallContext context, + ServerStreamListener listener) { String handle = command.getPreparedStatementHandle().toStringUtf8(); - if (handle.equals("SELECT PREPARED STATEMENT HANDLE") || handle.equals("PREPARED PLAN HANDLE")) { + if (handle.equals("SELECT PREPARED STATEMENT HANDLE") + || handle.equals("PREPARED PLAN HANDLE")) { putEmptyBatchToStreamListener(listener, getQuerySchema()); - } else if (handle.equals("SELECT PREPARED STATEMENT WITH TXN HANDLE") || - handle.equals("PREPARED PLAN WITH TXN HANDLE")) { + } else if (handle.equals("SELECT PREPARED STATEMENT WITH TXN HANDLE") + || handle.equals("PREPARED PLAN WITH TXN HANDLE")) { putEmptyBatchToStreamListener(listener, getQueryWithTransactionSchema()); } else { - listener.error(CallStatus.INVALID_ARGUMENT - .withDescription("Unknown handle: " + handle) - .toRuntimeException()); + listener.error( + CallStatus.INVALID_ARGUMENT + .withDescription("Unknown handle: " + handle) + .toRuntimeException()); } } @@ -417,113 +499,134 @@ private Runnable acceptPutReturnConstant(StreamListener ackStream, lo } @Override - public Runnable acceptPutStatement(FlightSql.CommandStatementUpdate command, CallContext context, - FlightStream flightStream, - StreamListener ackStream) { + public Runnable acceptPutStatement( + FlightSql.CommandStatementUpdate command, + CallContext context, + FlightStream flightStream, + StreamListener ackStream) { IntegrationAssertions.assertEquals(command.getQuery(), "UPDATE STATEMENT"); - return acceptPutReturnConstant(ackStream, - command.getTransactionId().isEmpty() ? FlightSqlScenario.UPDATE_STATEMENT_EXPECTED_ROWS : - FlightSqlScenario.UPDATE_STATEMENT_WITH_TRANSACTION_EXPECTED_ROWS); - } - - @Override - public Runnable acceptPutSubstraitPlan(FlightSql.CommandStatementSubstraitPlan command, CallContext context, - FlightStream flightStream, StreamListener ackStream) { - IntegrationAssertions.assertEquals(command.getPlan().getPlan().toByteArray(), - FlightSqlScenario.SUBSTRAIT_PLAN_TEXT); - IntegrationAssertions.assertEquals(command.getPlan().getVersion(), FlightSqlScenario.SUBSTRAIT_VERSION); - return acceptPutReturnConstant(ackStream, - command.getTransactionId().isEmpty() ? FlightSqlScenario.UPDATE_STATEMENT_EXPECTED_ROWS : - FlightSqlScenario.UPDATE_STATEMENT_WITH_TRANSACTION_EXPECTED_ROWS); - } - - @Override - public Runnable acceptPutPreparedStatementUpdate(FlightSql.CommandPreparedStatementUpdate command, - CallContext context, FlightStream flightStream, - StreamListener ackStream) { + return acceptPutReturnConstant( + ackStream, + command.getTransactionId().isEmpty() + ? FlightSqlScenario.UPDATE_STATEMENT_EXPECTED_ROWS + : FlightSqlScenario.UPDATE_STATEMENT_WITH_TRANSACTION_EXPECTED_ROWS); + } + + @Override + public Runnable acceptPutSubstraitPlan( + FlightSql.CommandStatementSubstraitPlan command, + CallContext context, + FlightStream flightStream, + StreamListener ackStream) { + IntegrationAssertions.assertEquals( + command.getPlan().getPlan().toByteArray(), FlightSqlScenario.SUBSTRAIT_PLAN_TEXT); + IntegrationAssertions.assertEquals( + command.getPlan().getVersion(), FlightSqlScenario.SUBSTRAIT_VERSION); + return acceptPutReturnConstant( + ackStream, + command.getTransactionId().isEmpty() + ? FlightSqlScenario.UPDATE_STATEMENT_EXPECTED_ROWS + : FlightSqlScenario.UPDATE_STATEMENT_WITH_TRANSACTION_EXPECTED_ROWS); + } + + @Override + public Runnable acceptPutPreparedStatementUpdate( + FlightSql.CommandPreparedStatementUpdate command, + CallContext context, + FlightStream flightStream, + StreamListener ackStream) { final String handle = command.getPreparedStatementHandle().toStringUtf8(); - if (handle.equals("UPDATE PREPARED STATEMENT HANDLE") || - handle.equals("PREPARED PLAN HANDLE")) { - return acceptPutReturnConstant(ackStream, FlightSqlScenario.UPDATE_PREPARED_STATEMENT_EXPECTED_ROWS); - } else if (handle.equals("UPDATE PREPARED STATEMENT WITH TXN HANDLE") || - handle.equals("PREPARED PLAN WITH TXN HANDLE")) { + if (handle.equals("UPDATE PREPARED STATEMENT HANDLE") + || handle.equals("PREPARED PLAN HANDLE")) { + return acceptPutReturnConstant( + ackStream, FlightSqlScenario.UPDATE_PREPARED_STATEMENT_EXPECTED_ROWS); + } else if (handle.equals("UPDATE PREPARED STATEMENT WITH TXN HANDLE") + || handle.equals("PREPARED PLAN WITH TXN HANDLE")) { return acceptPutReturnConstant( ackStream, FlightSqlScenario.UPDATE_PREPARED_STATEMENT_WITH_TRANSACTION_EXPECTED_ROWS); } return () -> { - ackStream.onError(CallStatus.INVALID_ARGUMENT - .withDescription("Unknown handle: " + handle) - .toRuntimeException()); + ackStream.onError( + CallStatus.INVALID_ARGUMENT + .withDescription("Unknown handle: " + handle) + .toRuntimeException()); }; } @Override - public Runnable acceptPutPreparedStatementQuery(FlightSql.CommandPreparedStatementQuery command, - CallContext context, FlightStream flightStream, - StreamListener ackStream) { + public Runnable acceptPutPreparedStatementQuery( + FlightSql.CommandPreparedStatementQuery command, + CallContext context, + FlightStream flightStream, + StreamListener ackStream) { final String handle = command.getPreparedStatementHandle().toStringUtf8(); - if (handle.equals("SELECT PREPARED STATEMENT HANDLE") || - handle.equals("SELECT PREPARED STATEMENT WITH TXN HANDLE") || - handle.equals("PREPARED PLAN HANDLE") || - handle.equals("PREPARED PLAN WITH TXN HANDLE")) { + if (handle.equals("SELECT PREPARED STATEMENT HANDLE") + || handle.equals("SELECT PREPARED STATEMENT WITH TXN HANDLE") + || handle.equals("PREPARED PLAN HANDLE") + || handle.equals("PREPARED PLAN WITH TXN HANDLE")) { IntegrationAssertions.assertEquals(getQuerySchema(), flightStream.getSchema()); return ackStream::onCompleted; } return () -> { - ackStream.onError(CallStatus.INVALID_ARGUMENT - .withDescription("Unknown handle: " + handle) - .toRuntimeException()); + ackStream.onError( + CallStatus.INVALID_ARGUMENT + .withDescription("Unknown handle: " + handle) + .toRuntimeException()); }; } @Override - public FlightInfo getFlightInfoSqlInfo(FlightSql.CommandGetSqlInfo request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoSqlInfo( + FlightSql.CommandGetSqlInfo request, CallContext context, FlightDescriptor descriptor) { if (request.getInfoCount() == 2) { // Integration test for the protocol messages - IntegrationAssertions.assertEquals(request.getInfo(0), - FlightSql.SqlInfo.FLIGHT_SQL_SERVER_NAME_VALUE); - IntegrationAssertions.assertEquals(request.getInfo(1), - FlightSql.SqlInfo.FLIGHT_SQL_SERVER_READ_ONLY_VALUE); + IntegrationAssertions.assertEquals( + request.getInfo(0), FlightSql.SqlInfo.FLIGHT_SQL_SERVER_NAME_VALUE); + IntegrationAssertions.assertEquals( + request.getInfo(1), FlightSql.SqlInfo.FLIGHT_SQL_SERVER_READ_ONLY_VALUE); } return getFlightInfoForSchema(request, descriptor, Schemas.GET_SQL_INFO_SCHEMA); } @Override - public void getStreamSqlInfo(FlightSql.CommandGetSqlInfo command, CallContext context, - ServerStreamListener listener) { + public void getStreamSqlInfo( + FlightSql.CommandGetSqlInfo command, CallContext context, ServerStreamListener listener) { if (command.getInfoCount() == 2) { // Integration test for the protocol messages putEmptyBatchToStreamListener(listener, Schemas.GET_SQL_INFO_SCHEMA); return; } - SqlInfoBuilder sqlInfoBuilder = new SqlInfoBuilder() - .withFlightSqlServerSql(false) - .withFlightSqlServerSubstrait(true) - .withFlightSqlServerSubstraitMinVersion("min_version") - .withFlightSqlServerSubstraitMaxVersion("max_version") - .withFlightSqlServerTransaction(FlightSql.SqlSupportedTransaction.SQL_SUPPORTED_TRANSACTION_SAVEPOINT) - .withFlightSqlServerCancel(true) - .withFlightSqlServerStatementTimeout(42) - .withFlightSqlServerTransactionTimeout(7); + SqlInfoBuilder sqlInfoBuilder = + new SqlInfoBuilder() + .withFlightSqlServerSql(false) + .withFlightSqlServerSubstrait(true) + .withFlightSqlServerSubstraitMinVersion("min_version") + .withFlightSqlServerSubstraitMaxVersion("max_version") + .withFlightSqlServerTransaction( + FlightSql.SqlSupportedTransaction.SQL_SUPPORTED_TRANSACTION_SAVEPOINT) + .withFlightSqlServerCancel(true) + .withFlightSqlServerStatementTimeout(42) + .withFlightSqlServerTransactionTimeout(7); sqlInfoBuilder.send(command.getInfoList(), listener); } @Override - public void getStreamTypeInfo(FlightSql.CommandGetXdbcTypeInfo request, - CallContext context, ServerStreamListener listener) { + public void getStreamTypeInfo( + FlightSql.CommandGetXdbcTypeInfo request, + CallContext context, + ServerStreamListener listener) { putEmptyBatchToStreamListener(listener, Schemas.GET_TYPE_INFO_SCHEMA); } @Override - public FlightInfo getFlightInfoTypeInfo(FlightSql.CommandGetXdbcTypeInfo request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoTypeInfo( + FlightSql.CommandGetXdbcTypeInfo request, CallContext context, FlightDescriptor descriptor) { return getFlightInfoForSchema(request, descriptor, Schemas.GET_TYPE_INFO_SCHEMA); } @Override - public FlightInfo getFlightInfoCatalogs(FlightSql.CommandGetCatalogs request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoCatalogs( + FlightSql.CommandGetCatalogs request, CallContext context, FlightDescriptor descriptor) { return getFlightInfoForSchema(request, descriptor, Schemas.GET_CATALOGS_SCHEMA); } @@ -541,27 +644,27 @@ public void getStreamCatalogs(CallContext context, ServerStreamListener listener } @Override - public FlightInfo getFlightInfoSchemas(FlightSql.CommandGetDbSchemas request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoSchemas( + FlightSql.CommandGetDbSchemas request, CallContext context, FlightDescriptor descriptor) { IntegrationAssertions.assertEquals(request.getCatalog(), "catalog"); - IntegrationAssertions.assertEquals(request.getDbSchemaFilterPattern(), - "db_schema_filter_pattern"); + IntegrationAssertions.assertEquals( + request.getDbSchemaFilterPattern(), "db_schema_filter_pattern"); return getFlightInfoForSchema(request, descriptor, Schemas.GET_SCHEMAS_SCHEMA); } @Override - public void getStreamSchemas(FlightSql.CommandGetDbSchemas command, CallContext context, - ServerStreamListener listener) { + public void getStreamSchemas( + FlightSql.CommandGetDbSchemas command, CallContext context, ServerStreamListener listener) { putEmptyBatchToStreamListener(listener, Schemas.GET_SCHEMAS_SCHEMA); } @Override - public FlightInfo getFlightInfoTables(FlightSql.CommandGetTables request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoTables( + FlightSql.CommandGetTables request, CallContext context, FlightDescriptor descriptor) { IntegrationAssertions.assertEquals(request.getCatalog(), "catalog"); - IntegrationAssertions.assertEquals(request.getDbSchemaFilterPattern(), - "db_schema_filter_pattern"); + IntegrationAssertions.assertEquals( + request.getDbSchemaFilterPattern(), "db_schema_filter_pattern"); IntegrationAssertions.assertEquals(request.getTableNameFilterPattern(), "table_filter_pattern"); IntegrationAssertions.assertEquals(request.getTableTypesCount(), 2); IntegrationAssertions.assertEquals(request.getTableTypes(0), "table"); @@ -571,14 +674,14 @@ public FlightInfo getFlightInfoTables(FlightSql.CommandGetTables request, CallCo } @Override - public void getStreamTables(FlightSql.CommandGetTables command, CallContext context, - ServerStreamListener listener) { + public void getStreamTables( + FlightSql.CommandGetTables command, CallContext context, ServerStreamListener listener) { putEmptyBatchToStreamListener(listener, Schemas.GET_TABLES_SCHEMA); } @Override - public FlightInfo getFlightInfoTableTypes(FlightSql.CommandGetTableTypes request, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoTableTypes( + FlightSql.CommandGetTableTypes request, CallContext context, FlightDescriptor descriptor) { return getFlightInfoForSchema(request, descriptor, Schemas.GET_TABLE_TYPES_SCHEMA); } @@ -588,8 +691,8 @@ public void getStreamTableTypes(CallContext context, ServerStreamListener listen } @Override - public FlightInfo getFlightInfoPrimaryKeys(FlightSql.CommandGetPrimaryKeys request, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoPrimaryKeys( + FlightSql.CommandGetPrimaryKeys request, CallContext context, FlightDescriptor descriptor) { IntegrationAssertions.assertEquals(request.getCatalog(), "catalog"); IntegrationAssertions.assertEquals(request.getDbSchema(), "db_schema"); IntegrationAssertions.assertEquals(request.getTable(), "table"); @@ -598,14 +701,14 @@ public FlightInfo getFlightInfoPrimaryKeys(FlightSql.CommandGetPrimaryKeys reque } @Override - public void getStreamPrimaryKeys(FlightSql.CommandGetPrimaryKeys command, CallContext context, - ServerStreamListener listener) { + public void getStreamPrimaryKeys( + FlightSql.CommandGetPrimaryKeys command, CallContext context, ServerStreamListener listener) { putEmptyBatchToStreamListener(listener, Schemas.GET_PRIMARY_KEYS_SCHEMA); } @Override - public FlightInfo getFlightInfoExportedKeys(FlightSql.CommandGetExportedKeys request, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoExportedKeys( + FlightSql.CommandGetExportedKeys request, CallContext context, FlightDescriptor descriptor) { IntegrationAssertions.assertEquals(request.getCatalog(), "catalog"); IntegrationAssertions.assertEquals(request.getDbSchema(), "db_schema"); IntegrationAssertions.assertEquals(request.getTable(), "table"); @@ -614,8 +717,8 @@ public FlightInfo getFlightInfoExportedKeys(FlightSql.CommandGetExportedKeys req } @Override - public FlightInfo getFlightInfoImportedKeys(FlightSql.CommandGetImportedKeys request, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoImportedKeys( + FlightSql.CommandGetImportedKeys request, CallContext context, FlightDescriptor descriptor) { IntegrationAssertions.assertEquals(request.getCatalog(), "catalog"); IntegrationAssertions.assertEquals(request.getDbSchema(), "db_schema"); IntegrationAssertions.assertEquals(request.getTable(), "table"); @@ -624,8 +727,10 @@ public FlightInfo getFlightInfoImportedKeys(FlightSql.CommandGetImportedKeys req } @Override - public FlightInfo getFlightInfoCrossReference(FlightSql.CommandGetCrossReference request, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoCrossReference( + FlightSql.CommandGetCrossReference request, + CallContext context, + FlightDescriptor descriptor) { IntegrationAssertions.assertEquals(request.getPkCatalog(), "pk_catalog"); IntegrationAssertions.assertEquals(request.getPkDbSchema(), "pk_db_schema"); IntegrationAssertions.assertEquals(request.getPkTable(), "pk_table"); @@ -637,37 +742,38 @@ public FlightInfo getFlightInfoCrossReference(FlightSql.CommandGetCrossReference } @Override - public void getStreamExportedKeys(FlightSql.CommandGetExportedKeys command, CallContext context, - ServerStreamListener listener) { + public void getStreamExportedKeys( + FlightSql.CommandGetExportedKeys command, + CallContext context, + ServerStreamListener listener) { putEmptyBatchToStreamListener(listener, Schemas.GET_EXPORTED_KEYS_SCHEMA); } @Override - public void getStreamImportedKeys(FlightSql.CommandGetImportedKeys command, CallContext context, - ServerStreamListener listener) { + public void getStreamImportedKeys( + FlightSql.CommandGetImportedKeys command, + CallContext context, + ServerStreamListener listener) { putEmptyBatchToStreamListener(listener, Schemas.GET_IMPORTED_KEYS_SCHEMA); } @Override - public void getStreamCrossReference(FlightSql.CommandGetCrossReference command, - CallContext context, ServerStreamListener listener) { + public void getStreamCrossReference( + FlightSql.CommandGetCrossReference command, + CallContext context, + ServerStreamListener listener) { putEmptyBatchToStreamListener(listener, Schemas.GET_CROSS_REFERENCE_SCHEMA); } @Override - public void close() throws Exception { - - } + public void close() throws Exception {} @Override - public void listFlights(CallContext context, Criteria criteria, - StreamListener listener) { - - } + public void listFlights( + CallContext context, Criteria criteria, StreamListener listener) {} - private FlightInfo getFlightInfoForSchema(final T request, - final FlightDescriptor descriptor, - final Schema schema) { + private FlightInfo getFlightInfoForSchema( + final T request, final FlightDescriptor descriptor, final Schema schema) { final Ticket ticket = new Ticket(Any.pack(request).toByteArray()); final List endpoints = Collections.singletonList(new FlightEndpoint(ticket)); diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationAssertions.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationAssertions.java index 036bcc3811b..8e97fd5057c 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationAssertions.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationAssertions.java @@ -21,13 +21,10 @@ import java.io.StringWriter; import java.util.Arrays; import java.util.Objects; - import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.FlightRuntimeException; -/** - * Utility methods to implement integration tests without using JUnit assertions. - */ +/** Utility methods to implement integration tests without using JUnit assertions. */ final class IntegrationAssertions { /** @@ -46,42 +43,36 @@ static T assertThrows(Class clazz, AssertThrows body) { if (clazz.isInstance(t)) { return (T) t; } - throw new AssertionError("Expected exception of class " + clazz + " but got " + t.getClass(), t); + throw new AssertionError( + "Expected exception of class " + clazz + " but got " + t.getClass(), t); } throw new AssertionError("Expected exception of class " + clazz + " but did not throw."); } - /** - * Assert that the two (non-array) objects are equal. - */ + /** Assert that the two (non-array) objects are equal. */ static void assertEquals(Object expected, Object actual) { if (!Objects.equals(expected, actual)) { throw new AssertionError("Expected:\n" + expected + "\nbut got:\n" + actual); } } - /** - * Assert that the two arrays are equal. - */ + /** Assert that the two arrays are equal. */ static void assertEquals(byte[] expected, byte[] actual) { if (!Arrays.equals(expected, actual)) { throw new AssertionError( - String.format("Expected:\n%s\nbut got:\n%s", Arrays.toString(expected), Arrays.toString(actual))); + String.format( + "Expected:\n%s\nbut got:\n%s", Arrays.toString(expected), Arrays.toString(actual))); } } - /** - * Assert that the value is false, using the given message as an error otherwise. - */ + /** Assert that the value is false, using the given message as an error otherwise. */ static void assertFalse(String message, boolean value) { if (value) { throw new AssertionError("Expected false: " + message); } } - /** - * Assert that the value is true, using the given message as an error otherwise. - */ + /** Assert that the value is true, using the given message as an error otherwise. */ static void assertTrue(String message, boolean value) { if (!value) { throw new AssertionError("Expected true: " + message); @@ -94,22 +85,18 @@ static void assertNotNull(Object actual) { } } - /** - * Convert a throwable into a FlightRuntimeException with error details, for debugging. - */ + /** Convert a throwable into a FlightRuntimeException with error details, for debugging. */ static FlightRuntimeException toFlightRuntimeException(Throwable t) { final StringWriter stringWriter = new StringWriter(); final PrintWriter writer = new PrintWriter(stringWriter); t.printStackTrace(writer); return CallStatus.UNKNOWN - .withCause(t) - .withDescription("Unknown error: " + t + "\n. Stack trace:\n" + stringWriter.toString()) - .toRuntimeException(); + .withCause(t) + .withDescription("Unknown error: " + t + "\n. Stack trace:\n" + stringWriter.toString()) + .toRuntimeException(); } - /** - * An interface used with {@link #assertThrows(Class, AssertThrows)}. - */ + /** An interface used with {@link #assertThrows(Class, AssertThrows)}. */ @FunctionalInterface interface AssertThrows { diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationProducer.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationProducer.java index aed3045accb..b2a3dedc197 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationProducer.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationProducer.java @@ -27,7 +27,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.stream.Collectors; - import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.FlightDescriptor; import org.apache.arrow.flight.FlightEndpoint; @@ -81,10 +80,14 @@ public void setLocation(Location location) { @Override public void getStream(CallContext context, Ticket ticket, ServerStreamListener listener) { try { - FlightDescriptor descriptor = FlightDescriptor.deserialize(ByteBuffer.wrap(ticket.getBytes())); + FlightDescriptor descriptor = + FlightDescriptor.deserialize(ByteBuffer.wrap(ticket.getBytes())); Dataset dataset = datasets.get(descriptor); if (dataset == null) { - listener.error(CallStatus.NOT_FOUND.withDescription("Unknown ticket: " + descriptor).toRuntimeException()); + listener.error( + CallStatus.NOT_FOUND + .withDescription("Unknown ticket: " + descriptor) + .toRuntimeException()); return; } dataset.streamTo(allocator, listener); @@ -97,14 +100,18 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l public FlightInfo getFlightInfo(CallContext context, FlightDescriptor descriptor) { Dataset h = datasets.get(descriptor); if (h == null) { - throw CallStatus.NOT_FOUND.withDescription("Unknown descriptor: " + descriptor).toRuntimeException(); + throw CallStatus.NOT_FOUND + .withDescription("Unknown descriptor: " + descriptor) + .toRuntimeException(); } return h.getFlightInfo(location); } @Override - public Runnable acceptPut(CallContext context, - final FlightStream flightStream, final StreamListener ackStream) { + public Runnable acceptPut( + CallContext context, + final FlightStream flightStream, + final StreamListener ackStream) { return () -> { List batches = new ArrayList<>(); try { @@ -115,7 +122,8 @@ public Runnable acceptPut(CallContext context, batches.add(unloader.getRecordBatch()); } // Closing the stream will release the dictionaries, take ownership - final Dataset dataset = new Dataset( + final Dataset dataset = + new Dataset( flightStream.getDescriptor(), flightStream.getSchema(), flightStream.takeDictionaryOwnership(), @@ -143,10 +151,11 @@ private static final class Dataset implements AutoCloseable { private final DictionaryProvider dictionaryProvider; private final List batches; - private Dataset(FlightDescriptor descriptor, - Schema schema, - DictionaryProvider dictionaryProvider, - List batches) { + private Dataset( + FlightDescriptor descriptor, + Schema schema, + DictionaryProvider dictionaryProvider, + List batches) { this.descriptor = descriptor; this.schema = schema; this.dictionaryProvider = dictionaryProvider; @@ -157,20 +166,20 @@ public FlightInfo getFlightInfo(Location location) { ByteBuffer serializedDescriptor = descriptor.serialize(); byte[] descriptorBytes = new byte[serializedDescriptor.remaining()]; serializedDescriptor.get(descriptorBytes); - final List endpoints = Collections.singletonList( - new FlightEndpoint(new Ticket(descriptorBytes), location)); + final List endpoints = + Collections.singletonList(new FlightEndpoint(new Ticket(descriptorBytes), location)); return new FlightInfo( - messageFormatSchema(), - descriptor, - endpoints, - batches.stream().mapToLong(ArrowRecordBatch::computeBodyLength).sum(), - batches.stream().mapToInt(ArrowRecordBatch::getLength).sum()); + messageFormatSchema(), + descriptor, + endpoints, + batches.stream().mapToLong(ArrowRecordBatch::computeBodyLength).sum(), + batches.stream().mapToInt(ArrowRecordBatch::getLength).sum()); } private Schema messageFormatSchema() { final Set dictionaryIdsUsed = new HashSet<>(); - final List messageFormatFields = schema.getFields() - .stream() + final List messageFormatFields = + schema.getFields().stream() .map(f -> DictionaryUtility.toMessageFormat(f, dictionaryProvider, dictionaryIdsUsed)) .collect(Collectors.toList()); return new Schema(messageFormatFields, schema.getCustomMetadata()); diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationTestClient.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationTestClient.java index 64b5882c0f5..60f7efcf068 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationTestClient.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationTestClient.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.List; - import org.apache.arrow.flight.AsyncPutListener; import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightDescriptor; @@ -49,11 +48,10 @@ import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; -/** - * A Flight client for integration testing. - */ +/** A Flight client for integration testing. */ class IntegrationTestClient { - private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(IntegrationTestClient.class); + private static final org.slf4j.Logger LOGGER = + org.slf4j.LoggerFactory.getLogger(IntegrationTestClient.class); private final Options options; private IntegrationTestClient() { @@ -92,10 +90,11 @@ private void run(String[] args) throws Exception { final Location defaultLocation = Location.forGrpcInsecure(host, port); try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - final FlightClient client = FlightClient.builder(allocator, defaultLocation).build()) { + final FlightClient client = FlightClient.builder(allocator, defaultLocation).build()) { if (cmd.hasOption("scenario")) { - Scenarios.getScenario(cmd.getOptionValue("scenario")).client(allocator, defaultLocation, client); + Scenarios.getScenario(cmd.getOptionValue("scenario")) + .client(allocator, defaultLocation, client); } else { final String inputPath = cmd.getOptionValue("j"); testStream(allocator, client, inputPath); @@ -110,23 +109,30 @@ private static void testStream(BufferAllocator allocator, FlightClient client, S // 1. Read data from JSON and upload to server. FlightDescriptor descriptor = FlightDescriptor.path(inputPath); try (JsonFileReader reader = new JsonFileReader(new File(inputPath), allocator); - VectorSchemaRoot root = VectorSchemaRoot.create(reader.start(), allocator)) { - FlightClient.ClientStreamListener stream = client.startPut(descriptor, root, reader, - new AsyncPutListener() { - int counter = 0; - - @Override - public void onNext(PutResult val) { - final byte[] metadataRaw = new byte[checkedCastToInt(val.getApplicationMetadata().readableBytes())]; - val.getApplicationMetadata().readBytes(metadataRaw); - final String metadata = new String(metadataRaw, StandardCharsets.UTF_8); - if (!Integer.toString(counter).equals(metadata)) { - throw new RuntimeException( - String.format("Invalid ACK from server. Expected '%d' but got '%s'.", counter, metadata)); - } - counter++; - } - }); + VectorSchemaRoot root = VectorSchemaRoot.create(reader.start(), allocator)) { + FlightClient.ClientStreamListener stream = + client.startPut( + descriptor, + root, + reader, + new AsyncPutListener() { + int counter = 0; + + @Override + public void onNext(PutResult val) { + final byte[] metadataRaw = + new byte[checkedCastToInt(val.getApplicationMetadata().readableBytes())]; + val.getApplicationMetadata().readBytes(metadataRaw); + final String metadata = new String(metadataRaw, StandardCharsets.UTF_8); + if (!Integer.toString(counter).equals(metadata)) { + throw new RuntimeException( + String.format( + "Invalid ACK from server. Expected '%d' but got '%s'.", + counter, metadata)); + } + counter++; + } + }); int counter = 0; while (reader.read(root)) { final byte[] rawMetadata = Integer.toString(counter).getBytes(StandardCharsets.UTF_8); @@ -168,11 +174,12 @@ public void onNext(PutResult val) { } } - private static void testTicket(BufferAllocator allocator, FlightClient readClient, Ticket ticket, String inputPath) { + private static void testTicket( + BufferAllocator allocator, FlightClient readClient, Ticket ticket, String inputPath) { try (FlightStream stream = readClient.getStream(ticket); - VectorSchemaRoot root = stream.getRoot(); - VectorSchemaRoot downloadedRoot = VectorSchemaRoot.create(root.getSchema(), allocator); - JsonFileReader reader = new JsonFileReader(new File(inputPath), allocator)) { + VectorSchemaRoot root = stream.getRoot(); + VectorSchemaRoot downloadedRoot = VectorSchemaRoot.create(root.getSchema(), allocator); + JsonFileReader reader = new JsonFileReader(new File(inputPath), allocator)) { VectorLoader loader = new VectorLoader(downloadedRoot); VectorUnloader unloader = new VectorUnloader(root); diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationTestServer.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationTestServer.java index 6e22eb1ec31..673da423c96 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationTestServer.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/IntegrationTestServer.java @@ -28,11 +28,10 @@ import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; -/** - * Flight server for integration testing. - */ +/** Flight server for integration testing. */ class IntegrationTestServer { - private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(IntegrationTestServer.class); + private static final org.slf4j.Logger LOGGER = + org.slf4j.LoggerFactory.getLogger(IntegrationTestServer.class); private final Options options; private IntegrationTestServer() { @@ -48,7 +47,8 @@ private void run(String[] args) throws Exception { final Location location = Location.forGrpcInsecure("localhost", port); final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - final FlightServer.Builder builder = FlightServer.builder().allocator(allocator).location(location); + final FlightServer.Builder builder = + FlightServer.builder().allocator(allocator).location(location); final FlightServer server; if (cmd.hasOption("scenario")) { @@ -64,14 +64,17 @@ private void run(String[] args) throws Exception { // Print out message for integration test script System.out.println("Server listening on localhost:" + server.getPort()); - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - try { - System.out.println("\nExiting..."); - AutoCloseables.close(server, allocator); - } catch (Exception e) { - e.printStackTrace(); - } - })); + Runtime.getRuntime() + .addShutdownHook( + new Thread( + () -> { + try { + System.out.println("\nExiting..."); + AutoCloseables.close(server, allocator); + } catch (Exception e) { + e.printStackTrace(); + } + })); server.awaitTermination(); } @@ -92,5 +95,4 @@ private static void fatalError(String message, Throwable e) { LOGGER.error(message, e); System.exit(1); } - } diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/MiddlewareScenario.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/MiddlewareScenario.java index c284a577c08..9e4f751ed37 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/MiddlewareScenario.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/MiddlewareScenario.java @@ -20,7 +20,6 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; - import org.apache.arrow.flight.CallHeaders; import org.apache.arrow.flight.CallInfo; import org.apache.arrow.flight.CallStatus; @@ -39,9 +38,9 @@ import org.apache.arrow.vector.types.pojo.Schema; /** - * Test an edge case in middleware: gRPC-Java consolidates headers and trailers if a call fails immediately. On the - * gRPC implementation side, we need to watch for this, or else we'll have a call with "no headers" if we only look - * for headers. + * Test an edge case in middleware: gRPC-Java consolidates headers and trailers if a call fails + * immediately. On the gRPC implementation side, we need to watch for this, or else we'll have a + * call with "no headers" if we only look for headers. */ final class MiddlewareScenario implements Scenario { @@ -56,7 +55,8 @@ public FlightProducer producer(BufferAllocator allocator, Location location) { public FlightInfo getFlightInfo(CallContext context, FlightDescriptor descriptor) { if (descriptor.isCommand()) { if (Arrays.equals(COMMAND_SUCCESS, descriptor.getCommand())) { - return new FlightInfo(new Schema(Collections.emptyList()), descriptor, Collections.emptyList(), -1, -1); + return new FlightInfo( + new Schema(Collections.emptyList()), descriptor, Collections.emptyList(), -1, -1); } } throw CallStatus.UNIMPLEMENTED.toRuntimeException(); @@ -66,22 +66,26 @@ public FlightInfo getFlightInfo(CallContext context, FlightDescriptor descriptor @Override public void buildServer(FlightServer.Builder builder) { - builder.middleware(FlightServerMiddleware.Key.of("test"), new InjectingServerMiddleware.Factory()); + builder.middleware( + FlightServerMiddleware.Key.of("test"), new InjectingServerMiddleware.Factory()); } @Override - public void client(BufferAllocator allocator, Location location, FlightClient ignored) throws Exception { + public void client(BufferAllocator allocator, Location location, FlightClient ignored) + throws Exception { final ExtractingClientMiddleware.Factory factory = new ExtractingClientMiddleware.Factory(); - try (final FlightClient client = FlightClient.builder(allocator, location).intercept(factory).build()) { + try (final FlightClient client = + FlightClient.builder(allocator, location).intercept(factory).build()) { // Should fail immediately - IntegrationAssertions.assertThrows(FlightRuntimeException.class, + IntegrationAssertions.assertThrows( + FlightRuntimeException.class, () -> client.getInfo(FlightDescriptor.command(new byte[0]))); if (!EXPECTED_HEADER_VALUE.equals(factory.extractedHeader)) { throw new AssertionError( - "Expected to extract the header value '" + - EXPECTED_HEADER_VALUE + - "', but found: " + - factory.extractedHeader); + "Expected to extract the header value '" + + EXPECTED_HEADER_VALUE + + "', but found: " + + factory.extractedHeader); } // Should not fail @@ -89,10 +93,10 @@ public void client(BufferAllocator allocator, Location location, FlightClient ig client.getInfo(FlightDescriptor.command(COMMAND_SUCCESS)); if (!EXPECTED_HEADER_VALUE.equals(factory.extractedHeader)) { throw new AssertionError( - "Expected to extract the header value '" + - EXPECTED_HEADER_VALUE + - "', but found: " + - factory.extractedHeader); + "Expected to extract the header value '" + + EXPECTED_HEADER_VALUE + + "', but found: " + + factory.extractedHeader); } } } @@ -112,19 +116,17 @@ public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) { } @Override - public void onCallCompleted(CallStatus status) { - } + public void onCallCompleted(CallStatus status) {} @Override - public void onCallErrored(Throwable err) { - } + public void onCallErrored(Throwable err) {} /** The factory for the server middleware. */ static class Factory implements FlightServerMiddleware.Factory { @Override - public InjectingServerMiddleware onCallStarted(CallInfo info, CallHeaders incomingHeaders, - RequestContext context) { + public InjectingServerMiddleware onCallStarted( + CallInfo info, CallHeaders incomingHeaders, RequestContext context) { String incoming = incomingHeaders.get(HEADER); return new InjectingServerMiddleware(incoming == null ? "" : incoming); } @@ -151,8 +153,7 @@ public void onHeadersReceived(CallHeaders incomingHeaders) { } @Override - public void onCallCompleted(CallStatus status) { - } + public void onCallCompleted(CallStatus status) {} /** The factory for the client middleware. */ static class Factory implements FlightClientMiddleware.Factory { diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/OrderedScenario.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/OrderedScenario.java index b8aa46fb567..711dbb93246 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/OrderedScenario.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/OrderedScenario.java @@ -22,7 +22,6 @@ import java.util.Collections; import java.util.List; import java.util.Objects; - import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightDescriptor; @@ -124,7 +123,8 @@ public void getStream(CallContext context, Ticket ticket, ServerStreamListener l listener.error( CallStatus.INVALID_ARGUMENT .withDescription( - "Could not find flight: " + new String(ticket.getBytes(), StandardCharsets.UTF_8)) + "Could not find flight: " + + new String(ticket.getBytes(), StandardCharsets.UTF_8)) .toRuntimeException()); return; } diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/PollFlightInfoProducer.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/PollFlightInfoProducer.java index 61312db56bc..6b3328e1bc7 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/PollFlightInfoProducer.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/PollFlightInfoProducer.java @@ -23,7 +23,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; - import org.apache.arrow.flight.FlightDescriptor; import org.apache.arrow.flight.FlightEndpoint; import org.apache.arrow.flight.FlightInfo; @@ -40,17 +39,22 @@ class PollFlightInfoProducer extends NoOpFlightProducer { @Override public PollInfo pollFlightInfo(CallContext context, FlightDescriptor descriptor) { - Schema schema = new Schema( - Collections.singletonList(Field.notNullable("number", Types.MinorType.UINT4.getType()))); - List endpoints = Collections.singletonList( - new FlightEndpoint( - new Ticket("long-running query".getBytes(StandardCharsets.UTF_8)))); - FlightInfo info = new FlightInfo(schema, descriptor, endpoints, -1, -1 ); + Schema schema = + new Schema( + Collections.singletonList( + Field.notNullable("number", Types.MinorType.UINT4.getType()))); + List endpoints = + Collections.singletonList( + new FlightEndpoint(new Ticket("long-running query".getBytes(StandardCharsets.UTF_8)))); + FlightInfo info = new FlightInfo(schema, descriptor, endpoints, -1, -1); if (descriptor.isCommand() && Arrays.equals(descriptor.getCommand(), POLL_DESCRIPTOR)) { return new PollInfo(info, null, 1.0, null); } else { return new PollInfo( - info, FlightDescriptor.command(POLL_DESCRIPTOR), 0.1, Instant.now().plus(10, ChronoUnit.SECONDS)); + info, + FlightDescriptor.command(POLL_DESCRIPTOR), + 0.1, + Instant.now().plus(10, ChronoUnit.SECONDS)); } } } diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/PollFlightInfoScenario.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/PollFlightInfoScenario.java index 1ffbe3c7262..107e881a73b 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/PollFlightInfoScenario.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/PollFlightInfoScenario.java @@ -19,7 +19,6 @@ import java.nio.charset.StandardCharsets; import java.util.Optional; - import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightDescriptor; import org.apache.arrow.flight.FlightProducer; @@ -36,27 +35,31 @@ public FlightProducer producer(BufferAllocator allocator, Location location) thr } @Override - public void buildServer(FlightServer.Builder builder) throws Exception { - } + public void buildServer(FlightServer.Builder builder) throws Exception {} @Override - public void client(BufferAllocator allocator, Location location, FlightClient client) throws Exception { - PollInfo info = client.pollInfo(FlightDescriptor.command("heavy query".getBytes(StandardCharsets.UTF_8))); + public void client(BufferAllocator allocator, Location location, FlightClient client) + throws Exception { + PollInfo info = + client.pollInfo(FlightDescriptor.command("heavy query".getBytes(StandardCharsets.UTF_8))); IntegrationAssertions.assertNotNull(info.getFlightInfo()); Optional progress = info.getProgress(); IntegrationAssertions.assertTrue("progress is missing", progress.isPresent()); - IntegrationAssertions.assertTrue("progress is invalid", progress.get() >= 0.0 && progress.get() <= 1.0); + IntegrationAssertions.assertTrue( + "progress is invalid", progress.get() >= 0.0 && progress.get() <= 1.0); IntegrationAssertions.assertTrue("expiration is missing", info.getExpirationTime().isPresent()); - IntegrationAssertions.assertTrue("descriptor is missing", - info.getFlightDescriptor().isPresent()); + IntegrationAssertions.assertTrue( + "descriptor is missing", info.getFlightDescriptor().isPresent()); info = client.pollInfo(info.getFlightDescriptor().get()); IntegrationAssertions.assertNotNull(info.getFlightInfo()); progress = info.getProgress(); IntegrationAssertions.assertTrue("progress is missing in finished query", progress.isPresent()); - IntegrationAssertions.assertTrue("progress isn't 1.0 in finished query", - Math.abs(progress.get() - 1.0) < Math.ulp(1.0)); - IntegrationAssertions.assertFalse("expiration is set in finished query", info.getExpirationTime().isPresent()); - IntegrationAssertions.assertFalse("descriptor is set in finished query", info.getFlightDescriptor().isPresent()); + IntegrationAssertions.assertTrue( + "progress isn't 1.0 in finished query", Math.abs(progress.get() - 1.0) < Math.ulp(1.0)); + IntegrationAssertions.assertFalse( + "expiration is set in finished query", info.getExpirationTime().isPresent()); + IntegrationAssertions.assertFalse( + "descriptor is set in finished query", info.getFlightDescriptor().isPresent()); } } diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/Scenario.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/Scenario.java index bcc657b765c..e697038c1b5 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/Scenario.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/Scenario.java @@ -23,23 +23,15 @@ import org.apache.arrow.flight.Location; import org.apache.arrow.memory.BufferAllocator; -/** - * A particular scenario in integration testing. - */ +/** A particular scenario in integration testing. */ interface Scenario { - /** - * Construct the FlightProducer for a server in this scenario. - */ + /** Construct the FlightProducer for a server in this scenario. */ FlightProducer producer(BufferAllocator allocator, Location location) throws Exception; - /** - * Set any other server options. - */ + /** Set any other server options. */ void buildServer(FlightServer.Builder builder) throws Exception; - /** - * Run as the client in the scenario. - */ + /** Run as the client in the scenario. */ void client(BufferAllocator allocator, Location location, FlightClient client) throws Exception; } diff --git a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/Scenarios.java b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/Scenarios.java index c61fd94a4d2..fd0d33b4055 100644 --- a/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/Scenarios.java +++ b/java/flight/flight-integration-tests/src/main/java/org/apache/arrow/flight/integration/tests/Scenarios.java @@ -21,16 +21,13 @@ import java.util.TreeMap; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; - import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightServer; import org.apache.arrow.flight.Location; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; -/** - * Scenarios for integration testing. - */ +/** Scenarios for integration testing. */ final class Scenarios { private static Scenarios INSTANCE; @@ -40,8 +37,10 @@ final class Scenarios { private Scenarios() { scenarios = new TreeMap<>(); scenarios.put("auth:basic_proto", AuthBasicProtoScenario::new); - scenarios.put("expiration_time:cancel_flight_info", ExpirationTimeCancelFlightInfoScenario::new); - scenarios.put("expiration_time:renew_flight_endpoint", ExpirationTimeRenewFlightEndpointScenario::new); + scenarios.put( + "expiration_time:cancel_flight_info", ExpirationTimeCancelFlightInfoScenario::new); + scenarios.put( + "expiration_time:renew_flight_endpoint", ExpirationTimeRenewFlightEndpointScenario::new); scenarios.put("expiration_time:do_get", ExpirationTimeDoGetScenario::new); scenarios.put("expiration_time:list_actions", ExpirationTimeListActionsScenario::new); scenarios.put("middleware", MiddlewareScenario::new); @@ -76,8 +75,8 @@ public static void main(String[] args) { System.out.println("Running test scenario: " + entry.getKey()); final Scenario scenario = entry.getValue().get(); try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE)) { - final FlightServer.Builder builder = FlightServer - .builder(allocator, location, scenario.producer(allocator, location)); + final FlightServer.Builder builder = + FlightServer.builder(allocator, location, scenario.producer(allocator, location)); scenario.buildServer(builder); try (final FlightServer server = builder.build()) { server.start(); diff --git a/java/flight/flight-integration-tests/src/test/java/org/apache/arrow/flight/integration/tests/IntegrationTest.java b/java/flight/flight-integration-tests/src/test/java/org/apache/arrow/flight/integration/tests/IntegrationTest.java index 477a56055cb..ca198396bca 100644 --- a/java/flight/flight-integration-tests/src/test/java/org/apache/arrow/flight/integration/tests/IntegrationTest.java +++ b/java/flight/flight-integration-tests/src/test/java/org/apache/arrow/flight/integration/tests/IntegrationTest.java @@ -24,9 +24,7 @@ import org.apache.arrow.memory.RootAllocator; import org.junit.jupiter.api.Test; -/** - * Run the integration test scenarios in-process. - */ +/** Run the integration test scenarios in-process. */ class IntegrationTest { @Test void authBasicProto() throws Exception { @@ -85,9 +83,10 @@ void appMetadataFlightInfoEndpoint() throws Exception { void testScenario(String scenarioName) throws Exception { try (final BufferAllocator allocator = new RootAllocator()) { - final FlightServer.Builder builder = FlightServer.builder() - .allocator(allocator) - .location(Location.forGrpcInsecure("0.0.0.0", 0)); + final FlightServer.Builder builder = + FlightServer.builder() + .allocator(allocator) + .location(Location.forGrpcInsecure("0.0.0.0", 0)); final Scenario scenario = Scenarios.getScenario(scenarioName); scenario.buildServer(builder); builder.producer(scenario.producer(allocator, Location.forGrpcInsecure("0.0.0.0", 0))); diff --git a/java/flight/flight-integration-tests/src/test/resources/logback.xml b/java/flight/flight-integration-tests/src/test/resources/logback.xml index 95fb0b37dc5..df3ce59828e 100644 --- a/java/flight/flight-integration-tests/src/test/resources/logback.xml +++ b/java/flight/flight-integration-tests/src/test/resources/logback.xml @@ -1,4 +1,4 @@ - + - diff --git a/java/flight/flight-sql-jdbc-core/pom.xml b/java/flight/flight-sql-jdbc-core/pom.xml index ce1f52e3967..613d65a55d0 100644 --- a/java/flight/flight-sql-jdbc-core/pom.xml +++ b/java/flight/flight-sql-jdbc-core/pom.xml @@ -1,176 +1,173 @@ - - - - arrow-flight - org.apache.arrow - 16.0.0-SNAPSHOT - ../pom.xml - - 4.0.0 - - flight-sql-jdbc-core - Arrow Flight SQL JDBC Driver Core - Core implementation of JDBC driver based on Arrow Flight SQL. - jar - https://arrow.apache.org - - - ${project.parent.groupId}:${project.parent.artifactId} - ${project.parent.version} - ${project.name} - ${project.version} - - - - - org.apache.arrow - flight-core - - - io.netty - netty-transport-native-kqueue - - - io.netty - netty-transport-native-epoll - - - - - - - org.apache.arrow - arrow-memory-core - - - - - org.apache.arrow - arrow-memory-netty - runtime - - - - - org.apache.arrow - arrow-vector - ${arrow.vector.classifier} - - - - com.google.guava - guava - - - - org.slf4j - slf4j-api - runtime - - - - com.google.protobuf - protobuf-java - - - - org.hamcrest - hamcrest-core - 1.3 - test - - - - commons-io - commons-io - 2.7 - test - - - - org.mockito - mockito-core - ${mockito.core.version} - test - - - - org.mockito - mockito-inline - ${mockito.inline.version} - test - - - - io.netty - netty-common - - - - org.apache.arrow - flight-sql - ${project.version} - - - - org.apache.calcite.avatica - avatica - 1.24.0 - - - - org.bouncycastle - bcpkix-jdk15on - 1.70 - - - - - - - src/main/resources - - - - - maven-surefire-plugin - - false - - ${project.basedir}/../../../testing/data - - - - - org.codehaus.mojo - properties-maven-plugin - 1.1.0 - - - write-project-properties-to-file - generate-resources - - write-project-properties - - - src/main/resources/properties/flight.properties - - - - - - + license agreements. See the NOTICE file distributed with this work for additional + information regarding copyright ownership. The ASF licenses this file to + You under the Apache License, Version 2.0 (the "License"); you may not use + this file except in compliance with the License. You may obtain a copy of + the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required + by applicable law or agreed to in writing, software distributed under the + License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS + OF ANY KIND, either express or implied. See the License for the specific + language governing permissions and limitations under the License. --> + + 4.0.0 + + org.apache.arrow + arrow-flight + 16.0.0-SNAPSHOT + ../pom.xml + + + flight-sql-jdbc-core + jar + Arrow Flight SQL JDBC Driver Core + Core implementation of JDBC driver based on Arrow Flight SQL. + https://arrow.apache.org + + + ${project.parent.groupId}:${project.parent.artifactId} + ${project.parent.version} + ${project.name} + ${project.version} + + + + + org.apache.arrow + flight-core + + + io.netty + netty-transport-native-kqueue + + + io.netty + netty-transport-native-epoll + + + + + + + org.apache.arrow + arrow-memory-core + + + + + org.apache.arrow + arrow-memory-netty + runtime + + + + + org.apache.arrow + arrow-vector + ${arrow.vector.classifier} + + + + com.google.guava + guava + + + + org.slf4j + slf4j-api + runtime + + + + com.google.protobuf + protobuf-java + + + + org.hamcrest + hamcrest-core + 1.3 + test + + + + commons-io + commons-io + 2.7 + test + + + + org.mockito + mockito-core + ${mockito.core.version} + test + + + + org.mockito + mockito-inline + ${mockito.inline.version} + test + + + + io.netty + netty-common + + + + org.apache.arrow + flight-sql + ${project.version} + + + + org.apache.calcite.avatica + avatica + 1.24.0 + + + + org.bouncycastle + bcpkix-jdk15on + 1.70 + + + + + + + src/main/resources + + + + + maven-surefire-plugin + + false + + ${project.basedir}/../../../testing/data + + + + + org.codehaus.mojo + properties-maven-plugin + 1.1.0 + + + write-project-properties-to-file + + write-project-properties + + generate-resources + + src/main/resources/properties/flight.properties + + + + + + diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowDatabaseMetadata.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowDatabaseMetadata.java index d68b8070e2b..d4f44c07b85 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowDatabaseMetadata.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowDatabaseMetadata.java @@ -35,6 +35,7 @@ import static java.sql.Types.VARCHAR; import static org.apache.arrow.flight.sql.util.SqlInfoOptionsUtils.doesBitmaskTranslateToEnum; +import com.google.protobuf.ProtocolMessageEnum; import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.channels.Channels; @@ -52,7 +53,6 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.regex.Pattern; import java.util.stream.Collectors; - import org.apache.arrow.driver.jdbc.utils.SqlTypes; import org.apache.arrow.driver.jdbc.utils.VectorSchemaRootTransformer; import org.apache.arrow.flight.FlightInfo; @@ -85,11 +85,7 @@ import org.apache.calcite.avatica.AvaticaConnection; import org.apache.calcite.avatica.AvaticaDatabaseMetaData; -import com.google.protobuf.ProtocolMessageEnum; - -/** - * Arrow Flight JDBC's implementation of {@link DatabaseMetaData}. - */ +/** Arrow Flight JDBC's implementation of {@link DatabaseMetaData}. */ public class ArrowDatabaseMetadata extends AvaticaDatabaseMetaData { private static final String JAVA_REGEX_SPECIALS = "[]()|^-+*?{}$\\."; private static final Charset CHARSET = StandardCharsets.UTF_8; @@ -118,33 +114,33 @@ public class ArrowDatabaseMetadata extends AvaticaDatabaseMetaData { static final int DECIMAL_DIGITS_TIME_MILLISECONDS = 3; static final int DECIMAL_DIGITS_TIME_MICROSECONDS = 6; static final int DECIMAL_DIGITS_TIME_NANOSECONDS = 9; - private static final Schema GET_COLUMNS_SCHEMA = new Schema( - Arrays.asList( - Field.nullable("TABLE_CAT", Types.MinorType.VARCHAR.getType()), - Field.nullable("TABLE_SCHEM", Types.MinorType.VARCHAR.getType()), - Field.notNullable("TABLE_NAME", Types.MinorType.VARCHAR.getType()), - Field.notNullable("COLUMN_NAME", Types.MinorType.VARCHAR.getType()), - Field.nullable("DATA_TYPE", Types.MinorType.INT.getType()), - Field.nullable("TYPE_NAME", Types.MinorType.VARCHAR.getType()), - Field.nullable("COLUMN_SIZE", Types.MinorType.INT.getType()), - Field.nullable("BUFFER_LENGTH", Types.MinorType.INT.getType()), - Field.nullable("DECIMAL_DIGITS", Types.MinorType.INT.getType()), - Field.nullable("NUM_PREC_RADIX", Types.MinorType.INT.getType()), - Field.notNullable("NULLABLE", Types.MinorType.INT.getType()), - Field.nullable("REMARKS", Types.MinorType.VARCHAR.getType()), - Field.nullable("COLUMN_DEF", Types.MinorType.VARCHAR.getType()), - Field.nullable("SQL_DATA_TYPE", Types.MinorType.INT.getType()), - Field.nullable("SQL_DATETIME_SUB", Types.MinorType.INT.getType()), - Field.notNullable("CHAR_OCTET_LENGTH", Types.MinorType.INT.getType()), - Field.notNullable("ORDINAL_POSITION", Types.MinorType.INT.getType()), - Field.notNullable("IS_NULLABLE", Types.MinorType.VARCHAR.getType()), - Field.nullable("SCOPE_CATALOG", Types.MinorType.VARCHAR.getType()), - Field.nullable("SCOPE_SCHEMA", Types.MinorType.VARCHAR.getType()), - Field.nullable("SCOPE_TABLE", Types.MinorType.VARCHAR.getType()), - Field.nullable("SOURCE_DATA_TYPE", Types.MinorType.SMALLINT.getType()), - Field.notNullable("IS_AUTOINCREMENT", Types.MinorType.VARCHAR.getType()), - Field.notNullable("IS_GENERATEDCOLUMN", Types.MinorType.VARCHAR.getType()) - )); + private static final Schema GET_COLUMNS_SCHEMA = + new Schema( + Arrays.asList( + Field.nullable("TABLE_CAT", Types.MinorType.VARCHAR.getType()), + Field.nullable("TABLE_SCHEM", Types.MinorType.VARCHAR.getType()), + Field.notNullable("TABLE_NAME", Types.MinorType.VARCHAR.getType()), + Field.notNullable("COLUMN_NAME", Types.MinorType.VARCHAR.getType()), + Field.nullable("DATA_TYPE", Types.MinorType.INT.getType()), + Field.nullable("TYPE_NAME", Types.MinorType.VARCHAR.getType()), + Field.nullable("COLUMN_SIZE", Types.MinorType.INT.getType()), + Field.nullable("BUFFER_LENGTH", Types.MinorType.INT.getType()), + Field.nullable("DECIMAL_DIGITS", Types.MinorType.INT.getType()), + Field.nullable("NUM_PREC_RADIX", Types.MinorType.INT.getType()), + Field.notNullable("NULLABLE", Types.MinorType.INT.getType()), + Field.nullable("REMARKS", Types.MinorType.VARCHAR.getType()), + Field.nullable("COLUMN_DEF", Types.MinorType.VARCHAR.getType()), + Field.nullable("SQL_DATA_TYPE", Types.MinorType.INT.getType()), + Field.nullable("SQL_DATETIME_SUB", Types.MinorType.INT.getType()), + Field.notNullable("CHAR_OCTET_LENGTH", Types.MinorType.INT.getType()), + Field.notNullable("ORDINAL_POSITION", Types.MinorType.INT.getType()), + Field.notNullable("IS_NULLABLE", Types.MinorType.VARCHAR.getType()), + Field.nullable("SCOPE_CATALOG", Types.MinorType.VARCHAR.getType()), + Field.nullable("SCOPE_SCHEMA", Types.MinorType.VARCHAR.getType()), + Field.nullable("SCOPE_TABLE", Types.MinorType.VARCHAR.getType()), + Field.nullable("SOURCE_DATA_TYPE", Types.MinorType.SMALLINT.getType()), + Field.notNullable("IS_AUTOINCREMENT", Types.MinorType.VARCHAR.getType()), + Field.notNullable("IS_GENERATEDCOLUMN", Types.MinorType.VARCHAR.getType()))); private final AtomicBoolean isCachePopulated = new AtomicBoolean(false); private final Map cachedSqlInfo = new EnumMap<>(SqlInfo.class); private static final Map sqlTypesToFlightEnumConvertTypes = new HashMap<>(); @@ -160,12 +156,12 @@ public class ArrowDatabaseMetadata extends AvaticaDatabaseMetaData { sqlTypesToFlightEnumConvertTypes.put(REAL, SqlSupportsConvert.SQL_CONVERT_REAL_VALUE); sqlTypesToFlightEnumConvertTypes.put(DECIMAL, SqlSupportsConvert.SQL_CONVERT_DECIMAL_VALUE); sqlTypesToFlightEnumConvertTypes.put(BINARY, SqlSupportsConvert.SQL_CONVERT_BINARY_VALUE); - sqlTypesToFlightEnumConvertTypes.put(LONGVARBINARY, - SqlSupportsConvert.SQL_CONVERT_LONGVARBINARY_VALUE); + sqlTypesToFlightEnumConvertTypes.put( + LONGVARBINARY, SqlSupportsConvert.SQL_CONVERT_LONGVARBINARY_VALUE); sqlTypesToFlightEnumConvertTypes.put(CHAR, SqlSupportsConvert.SQL_CONVERT_CHAR_VALUE); sqlTypesToFlightEnumConvertTypes.put(VARCHAR, SqlSupportsConvert.SQL_CONVERT_VARCHAR_VALUE); - sqlTypesToFlightEnumConvertTypes.put(LONGNVARCHAR, - SqlSupportsConvert.SQL_CONVERT_LONGVARCHAR_VALUE); + sqlTypesToFlightEnumConvertTypes.put( + LONGNVARCHAR, SqlSupportsConvert.SQL_CONVERT_LONGVARCHAR_VALUE); sqlTypesToFlightEnumConvertTypes.put(DATE, SqlSupportsConvert.SQL_CONVERT_DATE_VALUE); sqlTypesToFlightEnumConvertTypes.put(TIMESTAMP, SqlSupportsConvert.SQL_CONVERT_TIMESTAMP_VALUE); } @@ -266,20 +262,20 @@ public boolean supportsConvert(final int fromType, final int toType) throws SQLE @Override public boolean supportsTableCorrelationNames() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTS_TABLE_CORRELATION_NAMES, - Boolean.class); + return getSqlInfoAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTS_TABLE_CORRELATION_NAMES, Boolean.class); } @Override public boolean supportsDifferentTableCorrelationNames() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTS_DIFFERENT_TABLE_CORRELATION_NAMES, - Boolean.class); + return getSqlInfoAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTS_DIFFERENT_TABLE_CORRELATION_NAMES, Boolean.class); } @Override public boolean supportsExpressionsInOrderBy() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTS_EXPRESSIONS_IN_ORDER_BY, - Boolean.class); + return getSqlInfoAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTS_EXPRESSIONS_IN_ORDER_BY, Boolean.class); } @Override @@ -296,8 +292,8 @@ public boolean supportsGroupBy() throws SQLException { @Override public boolean supportsGroupByUnrelated() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_GROUP_BY, - SqlSupportedGroupBy.SQL_GROUP_BY_UNRELATED); + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_GROUP_BY, SqlSupportedGroupBy.SQL_GROUP_BY_UNRELATED); } @Override @@ -307,66 +303,75 @@ public boolean supportsLikeEscapeClause() throws SQLException { @Override public boolean supportsNonNullableColumns() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTS_NON_NULLABLE_COLUMNS, - Boolean.class); + return getSqlInfoAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTS_NON_NULLABLE_COLUMNS, Boolean.class); } @Override public boolean supportsMinimumSQLGrammar() throws SQLException { return checkEnumLevel( - Arrays.asList(getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_GRAMMAR, - SupportedSqlGrammar.SQL_EXTENDED_GRAMMAR), - getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_GRAMMAR, - SupportedSqlGrammar.SQL_CORE_GRAMMAR), - getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_GRAMMAR, - SupportedSqlGrammar.SQL_MINIMUM_GRAMMAR))); + Arrays.asList( + getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_GRAMMAR, SupportedSqlGrammar.SQL_EXTENDED_GRAMMAR), + getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_GRAMMAR, SupportedSqlGrammar.SQL_CORE_GRAMMAR), + getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_GRAMMAR, SupportedSqlGrammar.SQL_MINIMUM_GRAMMAR))); } @Override public boolean supportsCoreSQLGrammar() throws SQLException { return checkEnumLevel( - Arrays.asList(getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_GRAMMAR, - SupportedSqlGrammar.SQL_EXTENDED_GRAMMAR), - getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_GRAMMAR, - SupportedSqlGrammar.SQL_CORE_GRAMMAR))); + Arrays.asList( + getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_GRAMMAR, SupportedSqlGrammar.SQL_EXTENDED_GRAMMAR), + getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_GRAMMAR, SupportedSqlGrammar.SQL_CORE_GRAMMAR))); } @Override public boolean supportsExtendedSQLGrammar() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_GRAMMAR, - SupportedSqlGrammar.SQL_EXTENDED_GRAMMAR); + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_GRAMMAR, SupportedSqlGrammar.SQL_EXTENDED_GRAMMAR); } @Override public boolean supportsANSI92EntryLevelSQL() throws SQLException { return checkEnumLevel( - Arrays.asList(getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_ANSI92_SUPPORTED_LEVEL, + Arrays.asList( + getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_ANSI92_SUPPORTED_LEVEL, SupportedAnsi92SqlGrammarLevel.ANSI92_ENTRY_SQL), - getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_ANSI92_SUPPORTED_LEVEL, + getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_ANSI92_SUPPORTED_LEVEL, SupportedAnsi92SqlGrammarLevel.ANSI92_INTERMEDIATE_SQL), - getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_ANSI92_SUPPORTED_LEVEL, + getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_ANSI92_SUPPORTED_LEVEL, SupportedAnsi92SqlGrammarLevel.ANSI92_FULL_SQL))); } @Override public boolean supportsANSI92IntermediateSQL() throws SQLException { return checkEnumLevel( - Arrays.asList(getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_ANSI92_SUPPORTED_LEVEL, + Arrays.asList( + getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_ANSI92_SUPPORTED_LEVEL, SupportedAnsi92SqlGrammarLevel.ANSI92_ENTRY_SQL), - getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_ANSI92_SUPPORTED_LEVEL, + getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_ANSI92_SUPPORTED_LEVEL, SupportedAnsi92SqlGrammarLevel.ANSI92_INTERMEDIATE_SQL))); } @Override public boolean supportsANSI92FullSQL() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_ANSI92_SUPPORTED_LEVEL, - SupportedAnsi92SqlGrammarLevel.ANSI92_FULL_SQL); + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_ANSI92_SUPPORTED_LEVEL, SupportedAnsi92SqlGrammarLevel.ANSI92_FULL_SQL); } @Override public boolean supportsIntegrityEnhancementFacility() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTS_INTEGRITY_ENHANCEMENT_FACILITY, - Boolean.class); + return getSqlInfoAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTS_INTEGRITY_ENHANCEMENT_FACILITY, Boolean.class); } @Override @@ -378,14 +383,14 @@ public boolean supportsOuterJoins() throws SQLException { @Override public boolean supportsFullOuterJoins() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_OUTER_JOINS_SUPPORT_LEVEL, - SqlOuterJoinsSupportLevel.SQL_FULL_OUTER_JOINS); + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_OUTER_JOINS_SUPPORT_LEVEL, SqlOuterJoinsSupportLevel.SQL_FULL_OUTER_JOINS); } @Override public boolean supportsLimitedOuterJoins() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_OUTER_JOINS_SUPPORT_LEVEL, - SqlOuterJoinsSupportLevel.SQL_LIMITED_OUTER_JOINS); + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_OUTER_JOINS_SUPPORT_LEVEL, SqlOuterJoinsSupportLevel.SQL_LIMITED_OUTER_JOINS); } @Override @@ -410,43 +415,50 @@ public boolean isCatalogAtStart() throws SQLException { @Override public boolean supportsSchemasInProcedureCalls() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SCHEMAS_SUPPORTED_ACTIONS, + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SCHEMAS_SUPPORTED_ACTIONS, SqlSupportedElementActions.SQL_ELEMENT_IN_PROCEDURE_CALLS); } @Override public boolean supportsSchemasInIndexDefinitions() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SCHEMAS_SUPPORTED_ACTIONS, + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SCHEMAS_SUPPORTED_ACTIONS, SqlSupportedElementActions.SQL_ELEMENT_IN_INDEX_DEFINITIONS); } @Override public boolean supportsSchemasInPrivilegeDefinitions() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SCHEMAS_SUPPORTED_ACTIONS, + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SCHEMAS_SUPPORTED_ACTIONS, SqlSupportedElementActions.SQL_ELEMENT_IN_PRIVILEGE_DEFINITIONS); } @Override public boolean supportsCatalogsInIndexDefinitions() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_CATALOGS_SUPPORTED_ACTIONS, + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_CATALOGS_SUPPORTED_ACTIONS, SqlSupportedElementActions.SQL_ELEMENT_IN_INDEX_DEFINITIONS); } @Override public boolean supportsCatalogsInPrivilegeDefinitions() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_CATALOGS_SUPPORTED_ACTIONS, + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_CATALOGS_SUPPORTED_ACTIONS, SqlSupportedElementActions.SQL_ELEMENT_IN_PRIVILEGE_DEFINITIONS); } @Override public boolean supportsPositionedDelete() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_POSITIONED_COMMANDS, + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_POSITIONED_COMMANDS, SqlSupportedPositionedCommands.SQL_POSITIONED_DELETE); } @Override public boolean supportsPositionedUpdate() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_POSITIONED_COMMANDS, + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_POSITIONED_COMMANDS, SqlSupportedPositionedCommands.SQL_POSITIONED_UPDATE); } @@ -457,14 +469,14 @@ public boolean supportsResultSetType(final int type) throws SQLException { switch (type) { case ResultSet.TYPE_FORWARD_ONLY: - return doesBitmaskTranslateToEnum(SqlSupportedResultSetType.SQL_RESULT_SET_TYPE_FORWARD_ONLY, - bitmask); + return doesBitmaskTranslateToEnum( + SqlSupportedResultSetType.SQL_RESULT_SET_TYPE_FORWARD_ONLY, bitmask); case ResultSet.TYPE_SCROLL_INSENSITIVE: - return doesBitmaskTranslateToEnum(SqlSupportedResultSetType.SQL_RESULT_SET_TYPE_SCROLL_INSENSITIVE, - bitmask); + return doesBitmaskTranslateToEnum( + SqlSupportedResultSetType.SQL_RESULT_SET_TYPE_SCROLL_INSENSITIVE, bitmask); case ResultSet.TYPE_SCROLL_SENSITIVE: - return doesBitmaskTranslateToEnum(SqlSupportedResultSetType.SQL_RESULT_SET_TYPE_SCROLL_SENSITIVE, - bitmask); + return doesBitmaskTranslateToEnum( + SqlSupportedResultSetType.SQL_RESULT_SET_TYPE_SCROLL_SENSITIVE, bitmask); default: throw new SQLException( "Invalid result set type argument. The informed type is not defined in java.sql.ResultSet."); @@ -483,32 +495,32 @@ public boolean supportsStoredProcedures() throws SQLException { @Override public boolean supportsSubqueriesInComparisons() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_SUBQUERIES, - SqlSupportedSubqueries.SQL_SUBQUERIES_IN_COMPARISONS); + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_SUBQUERIES, SqlSupportedSubqueries.SQL_SUBQUERIES_IN_COMPARISONS); } @Override public boolean supportsSubqueriesInExists() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_SUBQUERIES, - SqlSupportedSubqueries.SQL_SUBQUERIES_IN_EXISTS); + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_SUBQUERIES, SqlSupportedSubqueries.SQL_SUBQUERIES_IN_EXISTS); } @Override public boolean supportsSubqueriesInIns() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_SUBQUERIES, - SqlSupportedSubqueries.SQL_SUBQUERIES_IN_INS); + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_SUBQUERIES, SqlSupportedSubqueries.SQL_SUBQUERIES_IN_INS); } @Override public boolean supportsSubqueriesInQuantifieds() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_SUBQUERIES, - SqlSupportedSubqueries.SQL_SUBQUERIES_IN_QUANTIFIEDS); + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_SUBQUERIES, SqlSupportedSubqueries.SQL_SUBQUERIES_IN_QUANTIFIEDS); } @Override public boolean supportsCorrelatedSubqueries() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_CORRELATED_SUBQUERIES_SUPPORTED, - Boolean.class); + return getSqlInfoAndCacheIfCacheIsEmpty( + SqlInfo.SQL_CORRELATED_SUBQUERIES_SUPPORTED, Boolean.class); } @Override @@ -520,56 +532,56 @@ public boolean supportsUnion() throws SQLException { @Override public boolean supportsUnionAll() throws SQLException { - return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_UNIONS, - SqlSupportedUnions.SQL_UNION_ALL); + return getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_UNIONS, SqlSupportedUnions.SQL_UNION_ALL); } @Override public int getMaxBinaryLiteralLength() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_BINARY_LITERAL_LENGTH, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_BINARY_LITERAL_LENGTH, Long.class) + .intValue(); } @Override public int getMaxCharLiteralLength() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_CHAR_LITERAL_LENGTH, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_CHAR_LITERAL_LENGTH, Long.class) + .intValue(); } @Override public int getMaxColumnNameLength() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_COLUMN_NAME_LENGTH, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_COLUMN_NAME_LENGTH, Long.class) + .intValue(); } @Override public int getMaxColumnsInGroupBy() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_COLUMNS_IN_GROUP_BY, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_COLUMNS_IN_GROUP_BY, Long.class) + .intValue(); } @Override public int getMaxColumnsInIndex() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_COLUMNS_IN_INDEX, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_COLUMNS_IN_INDEX, Long.class) + .intValue(); } @Override public int getMaxColumnsInOrderBy() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_COLUMNS_IN_ORDER_BY, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_COLUMNS_IN_ORDER_BY, Long.class) + .intValue(); } @Override public int getMaxColumnsInSelect() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_COLUMNS_IN_SELECT, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_COLUMNS_IN_SELECT, Long.class) + .intValue(); } @Override public int getMaxColumnsInTable() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_COLUMNS_IN_TABLE, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_COLUMNS_IN_TABLE, Long.class) + .intValue(); } @Override @@ -579,8 +591,8 @@ public int getMaxConnections() throws SQLException { @Override public int getMaxCursorNameLength() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_CURSOR_NAME_LENGTH, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_CURSOR_NAME_LENGTH, Long.class) + .intValue(); } @Override @@ -590,20 +602,20 @@ public int getMaxIndexLength() throws SQLException { @Override public int getMaxSchemaNameLength() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_DB_SCHEMA_NAME_LENGTH, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_DB_SCHEMA_NAME_LENGTH, Long.class) + .intValue(); } @Override public int getMaxProcedureNameLength() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_PROCEDURE_NAME_LENGTH, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_PROCEDURE_NAME_LENGTH, Long.class) + .intValue(); } @Override public int getMaxCatalogNameLength() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_CATALOG_NAME_LENGTH, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_CATALOG_NAME_LENGTH, Long.class) + .intValue(); } @Override @@ -618,8 +630,8 @@ public boolean doesMaxRowSizeIncludeBlobs() throws SQLException { @Override public int getMaxStatementLength() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_STATEMENT_LENGTH, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_STATEMENT_LENGTH, Long.class) + .intValue(); } @Override @@ -629,14 +641,14 @@ public int getMaxStatements() throws SQLException { @Override public int getMaxTableNameLength() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_TABLE_NAME_LENGTH, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_TABLE_NAME_LENGTH, Long.class) + .intValue(); } @Override public int getMaxTablesInSelect() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_TABLES_IN_SELECT, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_MAX_TABLES_IN_SELECT, Long.class) + .intValue(); } @Override @@ -646,8 +658,8 @@ public int getMaxUserNameLength() throws SQLException { @Override public int getDefaultTransactionIsolation() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_DEFAULT_TRANSACTION_ISOLATION, - Long.class).intValue(); + return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_DEFAULT_TRANSACTION_ISOLATION, Long.class) + .intValue(); } @Override @@ -658,24 +670,25 @@ public boolean supportsTransactions() throws SQLException { @Override public boolean supportsTransactionIsolationLevel(final int level) throws SQLException { final int bitmask = - getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_SUPPORTED_TRANSACTIONS_ISOLATION_LEVELS, - Integer.class); + getSqlInfoAndCacheIfCacheIsEmpty( + SqlInfo.SQL_SUPPORTED_TRANSACTIONS_ISOLATION_LEVELS, Integer.class); switch (level) { case Connection.TRANSACTION_NONE: - return doesBitmaskTranslateToEnum(SqlTransactionIsolationLevel.SQL_TRANSACTION_NONE, bitmask); + return doesBitmaskTranslateToEnum( + SqlTransactionIsolationLevel.SQL_TRANSACTION_NONE, bitmask); case Connection.TRANSACTION_READ_COMMITTED: - return doesBitmaskTranslateToEnum(SqlTransactionIsolationLevel.SQL_TRANSACTION_READ_COMMITTED, - bitmask); + return doesBitmaskTranslateToEnum( + SqlTransactionIsolationLevel.SQL_TRANSACTION_READ_COMMITTED, bitmask); case Connection.TRANSACTION_READ_UNCOMMITTED: - return doesBitmaskTranslateToEnum(SqlTransactionIsolationLevel.SQL_TRANSACTION_READ_UNCOMMITTED, - bitmask); + return doesBitmaskTranslateToEnum( + SqlTransactionIsolationLevel.SQL_TRANSACTION_READ_UNCOMMITTED, bitmask); case Connection.TRANSACTION_REPEATABLE_READ: - return doesBitmaskTranslateToEnum(SqlTransactionIsolationLevel.SQL_TRANSACTION_REPEATABLE_READ, - bitmask); + return doesBitmaskTranslateToEnum( + SqlTransactionIsolationLevel.SQL_TRANSACTION_REPEATABLE_READ, bitmask); case Connection.TRANSACTION_SERIALIZABLE: - return doesBitmaskTranslateToEnum(SqlTransactionIsolationLevel.SQL_TRANSACTION_SERIALIZABLE, - bitmask); + return doesBitmaskTranslateToEnum( + SqlTransactionIsolationLevel.SQL_TRANSACTION_SERIALIZABLE, bitmask); default: throw new SQLException( "Invalid transaction isolation level argument. The informed level is not defined in java.sql.Connection."); @@ -684,14 +697,14 @@ public boolean supportsTransactionIsolationLevel(final int level) throws SQLExce @Override public boolean dataDefinitionCausesTransactionCommit() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_DATA_DEFINITION_CAUSES_TRANSACTION_COMMIT, - Boolean.class); + return getSqlInfoAndCacheIfCacheIsEmpty( + SqlInfo.SQL_DATA_DEFINITION_CAUSES_TRANSACTION_COMMIT, Boolean.class); } @Override public boolean dataDefinitionIgnoredInTransactions() throws SQLException { - return getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_DATA_DEFINITIONS_IN_TRANSACTIONS_IGNORED, - Boolean.class); + return getSqlInfoAndCacheIfCacheIsEmpty( + SqlInfo.SQL_DATA_DEFINITIONS_IN_TRANSACTIONS_IGNORED, Boolean.class); } @Override @@ -725,24 +738,25 @@ public ArrowFlightConnection getConnection() throws SQLException { return (ArrowFlightConnection) super.getConnection(); } - private T getSqlInfoAndCacheIfCacheIsEmpty(final SqlInfo sqlInfoCommand, - final Class desiredType) - throws SQLException { + private T getSqlInfoAndCacheIfCacheIsEmpty( + final SqlInfo sqlInfoCommand, final Class desiredType) throws SQLException { final ArrowFlightConnection connection = getConnection(); if (!isCachePopulated.get()) { // Lock-and-populate the cache. Only issue the call to getSqlInfo() once, // populate the cache, then mark it as populated. - // Note that multiple callers from separate threads can see that the cache is not populated, but only - // one thread will try to populate the cache. Other threads will see the cache is non-empty when acquiring + // Note that multiple callers from separate threads can see that the cache is not populated, + // but only + // one thread will try to populate the cache. Other threads will see the cache is non-empty + // when acquiring // the lock on the cache and skip population. synchronized (cachedSqlInfo) { if (cachedSqlInfo.isEmpty()) { final FlightInfo sqlInfo = connection.getClientHandler().getSqlInfo(); try (final ResultSet resultSet = - ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo( - connection, sqlInfo, null)) { + ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo(connection, sqlInfo, null)) { while (resultSet.next()) { - cachedSqlInfo.put(SqlInfo.forNumber((Integer) resultSet.getObject("info_name")), + cachedSqlInfo.put( + SqlInfo.forNumber((Integer) resultSet.getObject("info_name")), resultSet.getObject("value")); } } @@ -758,9 +772,7 @@ private String convertListSqlInfoToString(final List sqlInfoList) { } private boolean getSqlInfoEnumOptionAndCacheIfCacheIsEmpty( - final SqlInfo sqlInfoCommand, - final ProtocolMessageEnum enumInstance - ) throws SQLException { + final SqlInfo sqlInfoCommand, final ProtocolMessageEnum enumInstance) throws SQLException { final int bitmask = getSqlInfoAndCacheIfCacheIsEmpty(sqlInfoCommand, Integer.class); return doesBitmaskTranslateToEnum(enumInstance, bitmask); } @@ -779,8 +791,8 @@ public ResultSet getCatalogs() throws SQLException { new VectorSchemaRootTransformer.Builder(Schemas.GET_CATALOGS_SCHEMA, allocator) .renameFieldVector("catalog_name", "TABLE_CAT") .build(); - return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo(connection, flightInfoCatalogs, - transformer); + return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo( + connection, flightInfoCatalogs, transformer); } @Override @@ -792,8 +804,8 @@ public ResultSet getImportedKeys(final String catalog, final String schema, fina final BufferAllocator allocator = connection.getBufferAllocator(); final VectorSchemaRootTransformer transformer = getForeignKeysTransformer(allocator); - return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo(connection, flightInfoImportedKeys, - transformer); + return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo( + connection, flightInfoImportedKeys, transformer); } @Override @@ -805,33 +817,43 @@ public ResultSet getExportedKeys(final String catalog, final String schema, fina final BufferAllocator allocator = connection.getBufferAllocator(); final VectorSchemaRootTransformer transformer = getForeignKeysTransformer(allocator); - return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo(connection, flightInfoExportedKeys, - transformer); + return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo( + connection, flightInfoExportedKeys, transformer); } @Override - public ResultSet getCrossReference(final String parentCatalog, final String parentSchema, - final String parentTable, - final String foreignCatalog, final String foreignSchema, - final String foreignTable) + public ResultSet getCrossReference( + final String parentCatalog, + final String parentSchema, + final String parentTable, + final String foreignCatalog, + final String foreignSchema, + final String foreignTable) throws SQLException { final ArrowFlightConnection connection = getConnection(); - final FlightInfo flightInfoCrossReference = connection.getClientHandler().getCrossReference( - parentCatalog, parentSchema, parentTable, foreignCatalog, foreignSchema, foreignTable); + final FlightInfo flightInfoCrossReference = + connection + .getClientHandler() + .getCrossReference( + parentCatalog, + parentSchema, + parentTable, + foreignCatalog, + foreignSchema, + foreignTable); final BufferAllocator allocator = connection.getBufferAllocator(); final VectorSchemaRootTransformer transformer = getForeignKeysTransformer(allocator); - return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo(connection, flightInfoCrossReference, - transformer); + return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo( + connection, flightInfoCrossReference, transformer); } /** - * Transformer used on getImportedKeys, getExportedKeys and getCrossReference methods, since - * all three share the same schema. + * Transformer used on getImportedKeys, getExportedKeys and getCrossReference methods, since all + * three share the same schema. */ private VectorSchemaRootTransformer getForeignKeysTransformer(final BufferAllocator allocator) { - return new VectorSchemaRootTransformer.Builder(Schemas.GET_IMPORTED_KEYS_SCHEMA, - allocator) + return new VectorSchemaRootTransformer.Builder(Schemas.GET_IMPORTED_KEYS_SCHEMA, allocator) .renameFieldVector("pk_catalog_name", "PKTABLE_CAT") .renameFieldVector("pk_db_schema_name", "PKTABLE_SCHEM") .renameFieldVector("pk_table_name", "PKTABLE_NAME") @@ -862,8 +884,8 @@ public ResultSet getSchemas(final String catalog, final String schemaPattern) .renameFieldVector("db_schema_name", "TABLE_SCHEM") .renameFieldVector("catalog_name", "TABLE_CATALOG") .build(); - return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo(connection, flightInfoSchemas, - transformer); + return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo( + connection, flightInfoSchemas, transformer); } @Override @@ -876,19 +898,22 @@ public ResultSet getTableTypes() throws SQLException { new VectorSchemaRootTransformer.Builder(Schemas.GET_TABLE_TYPES_SCHEMA, allocator) .renameFieldVector("table_type", "TABLE_TYPE") .build(); - return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo(connection, flightInfoTableTypes, - transformer); + return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo( + connection, flightInfoTableTypes, transformer); } @Override - public ResultSet getTables(final String catalog, final String schemaPattern, - final String tableNamePattern, - final String[] types) + public ResultSet getTables( + final String catalog, + final String schemaPattern, + final String tableNamePattern, + final String[] types) throws SQLException { final ArrowFlightConnection connection = getConnection(); final List typesList = types == null ? null : Arrays.asList(types); final FlightInfo flightInfoTables = - connection.getClientHandler() + connection + .getClientHandler() .getTables(catalog, schemaPattern, tableNamePattern, typesList, false); final BufferAllocator allocator = connection.getBufferAllocator(); @@ -905,8 +930,8 @@ public ResultSet getTables(final String catalog, final String schemaPattern, .addEmptyField("SELF_REFERENCING_COL_NAME", Types.MinorType.VARBINARY) .addEmptyField("REF_GENERATION", Types.MinorType.VARBINARY) .build(); - return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo(connection, flightInfoTables, - transformer); + return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo( + connection, flightInfoTables, transformer); } @Override @@ -926,18 +951,21 @@ public ResultSet getPrimaryKeys(final String catalog, final String schema, final .renameFieldVector("key_sequence", "KEY_SEQ") .renameFieldVector("key_name", "PK_NAME") .build(); - return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo(connection, flightInfoPrimaryKeys, - transformer); + return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo( + connection, flightInfoPrimaryKeys, transformer); } @Override - public ResultSet getColumns(final String catalog, final String schemaPattern, - final String tableNamePattern, - final String columnNamePattern) + public ResultSet getColumns( + final String catalog, + final String schemaPattern, + final String tableNamePattern, + final String columnNamePattern) throws SQLException { final ArrowFlightConnection connection = getConnection(); final FlightInfo flightInfoTables = - connection.getClientHandler() + connection + .getClientHandler() .getTables(catalog, schemaPattern, tableNamePattern, null, true); final BufferAllocator allocator = connection.getBufferAllocator(); @@ -945,7 +973,9 @@ public ResultSet getColumns(final String catalog, final String schemaPattern, final Pattern columnNamePat = columnNamePattern != null ? Pattern.compile(sqlToRegexLike(columnNamePattern)) : null; - return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo(connection, flightInfoTables, + return ArrowFlightJdbcFlightStreamResultSet.fromFlightInfo( + connection, + flightInfoTables, (originalRoot, transformedRoot) -> { int columnCounter = 0; if (transformedRoot == null) { @@ -971,18 +1001,25 @@ public ResultSet getColumns(final String catalog, final String schemaPattern, final Schema currentSchema; try { - currentSchema = MessageSerializer.deserializeSchema( - new ReadChannel(Channels.newChannel( - new ByteArrayInputStream(schemaVector.get(i))))); + currentSchema = + MessageSerializer.deserializeSchema( + new ReadChannel( + Channels.newChannel(new ByteArrayInputStream(schemaVector.get(i))))); } catch (final IOException e) { throw new IOException( String.format("Failed to deserialize schema for table %s", tableName), e); } final List tableColumns = currentSchema.getFields(); - columnCounter = setGetColumnsVectorSchemaRootFromFields(transformedRoot, columnCounter, - tableColumns, - catalogName, tableName, schemaName, columnNamePat); + columnCounter = + setGetColumnsVectorSchemaRootFromFields( + transformedRoot, + columnCounter, + tableColumns, + catalogName, + tableName, + schemaName, + columnNamePat); } transformedRoot.setRowCount(columnCounter); @@ -992,12 +1029,14 @@ public ResultSet getColumns(final String catalog, final String schemaPattern, }); } - private int setGetColumnsVectorSchemaRootFromFields(final VectorSchemaRoot currentRoot, - int insertIndex, - final List tableColumns, - final Text catalogName, - final Text tableName, final Text schemaName, - final Pattern columnNamePattern) { + private int setGetColumnsVectorSchemaRootFromFields( + final VectorSchemaRoot currentRoot, + int insertIndex, + final List tableColumns, + final Text catalogName, + final Text tableName, + final Text schemaName, + final Pattern columnNamePattern) { int ordinalIndex = 1; final int tableColumnsSize = tableColumns.size(); @@ -1013,12 +1052,15 @@ private int setGetColumnsVectorSchemaRootFromFields(final VectorSchemaRoot curre final IntVector nullableVector = (IntVector) currentRoot.getVector("NULLABLE"); final IntVector ordinalPositionVector = (IntVector) currentRoot.getVector("ORDINAL_POSITION"); final VarCharVector isNullableVector = (VarCharVector) currentRoot.getVector("IS_NULLABLE"); - final VarCharVector isAutoincrementVector = (VarCharVector) currentRoot.getVector("IS_AUTOINCREMENT"); - final VarCharVector isGeneratedColumnVector = (VarCharVector) currentRoot.getVector("IS_GENERATEDCOLUMN"); + final VarCharVector isAutoincrementVector = + (VarCharVector) currentRoot.getVector("IS_AUTOINCREMENT"); + final VarCharVector isGeneratedColumnVector = + (VarCharVector) currentRoot.getVector("IS_GENERATEDCOLUMN"); for (int i = 0; i < tableColumnsSize; i++, ordinalIndex++) { final Field field = tableColumns.get(i); - final FlightSqlColumnMetadata columnMetadata = new FlightSqlColumnMetadata(field.getMetadata()); + final FlightSqlColumnMetadata columnMetadata = + new FlightSqlColumnMetadata(field.getMetadata()); final String columnName = field.getName(); if (columnNamePattern != null && !columnNamePattern.matcher(columnName).matches()) { @@ -1043,13 +1085,15 @@ private int setGetColumnsVectorSchemaRootFromFields(final VectorSchemaRoot curre } dataTypeVector.setSafe(insertIndex, SqlTypes.getSqlTypeIdFromArrowType(fieldType)); - byte[] typeName = columnMetadata.getTypeName() != null ? - columnMetadata.getTypeName().getBytes(CHARSET) : - SqlTypes.getSqlTypeNameFromArrowType(fieldType).getBytes(CHARSET); + byte[] typeName = + columnMetadata.getTypeName() != null + ? columnMetadata.getTypeName().getBytes(CHARSET) + : SqlTypes.getSqlTypeNameFromArrowType(fieldType).getBytes(CHARSET); typeNameVector.setSafe(insertIndex, typeName); // We aren't setting COLUMN_SIZE for ROWID SQL Types, as there's no such Arrow type. - // We aren't setting COLUMN_SIZE nor DECIMAL_DIGITS for Float/Double as their precision and scale are variable. + // We aren't setting COLUMN_SIZE nor DECIMAL_DIGITS for Float/Double as their precision and + // scale are variable. if (fieldType instanceof ArrowType.Decimal) { numPrecRadixVector.setSafe(insertIndex, BASE10_RADIX); } else if (fieldType instanceof ArrowType.Int) { @@ -1142,7 +1186,8 @@ static Integer getDecimalDigits(final ArrowType fieldType) { static Integer getColumnSize(final ArrowType fieldType) { // We aren't setting COLUMN_SIZE for ROWID SQL Types, as there's no such Arrow type. - // We aren't setting COLUMN_SIZE nor DECIMAL_DIGITS for Float/Double as their precision and scale are variable. + // We aren't setting COLUMN_SIZE nor DECIMAL_DIGITS for Float/Double as their precision and + // scale are variable. if (fieldType instanceof ArrowType.Decimal) { final ArrowType.Decimal thisDecimal = (ArrowType.Decimal) fieldType; return thisDecimal.getPrecision(); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightConnection.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightConnection.java index ad19c616ff2..a70e7899d84 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightConnection.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightConnection.java @@ -19,11 +19,11 @@ import static org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.replaceSemiColons; +import io.netty.util.concurrent.DefaultThreadFactory; import java.sql.SQLException; import java.util.Properties; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; - import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler; import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl; import org.apache.arrow.flight.FlightClient; @@ -33,11 +33,7 @@ import org.apache.calcite.avatica.AvaticaConnection; import org.apache.calcite.avatica.AvaticaFactory; -import io.netty.util.concurrent.DefaultThreadFactory; - -/** - * Connection to the Arrow Flight server. - */ +/** Connection to the Arrow Flight server. */ public final class ArrowFlightConnection extends AvaticaConnection { private final BufferAllocator allocator; @@ -48,19 +44,22 @@ public final class ArrowFlightConnection extends AvaticaConnection { /** * Creates a new {@link ArrowFlightConnection}. * - * @param driver the {@link ArrowFlightJdbcDriver} to use. - * @param factory the {@link AvaticaFactory} to use. - * @param url the URL to use. - * @param properties the {@link Properties} to use. - * @param config the {@link ArrowFlightConnectionConfigImpl} to use. - * @param allocator the {@link BufferAllocator} to use. + * @param driver the {@link ArrowFlightJdbcDriver} to use. + * @param factory the {@link AvaticaFactory} to use. + * @param url the URL to use. + * @param properties the {@link Properties} to use. + * @param config the {@link ArrowFlightConnectionConfigImpl} to use. + * @param allocator the {@link BufferAllocator} to use. * @param clientHandler the {@link ArrowFlightSqlClientHandler} to use. */ - private ArrowFlightConnection(final ArrowFlightJdbcDriver driver, final AvaticaFactory factory, - final String url, final Properties properties, - final ArrowFlightConnectionConfigImpl config, - final BufferAllocator allocator, - final ArrowFlightSqlClientHandler clientHandler) { + private ArrowFlightConnection( + final ArrowFlightJdbcDriver driver, + final AvaticaFactory factory, + final String url, + final Properties properties, + final ArrowFlightConnectionConfigImpl config, + final BufferAllocator allocator, + final ArrowFlightSqlClientHandler clientHandler) { super(driver, factory, url, properties); this.config = Preconditions.checkNotNull(config, "Config cannot be null."); this.allocator = Preconditions.checkNotNull(allocator, "Allocator cannot be null."); @@ -70,28 +69,31 @@ private ArrowFlightConnection(final ArrowFlightJdbcDriver driver, final AvaticaF /** * Creates a new {@link ArrowFlightConnection} to a {@link FlightClient}. * - * @param driver the {@link ArrowFlightJdbcDriver} to use. - * @param factory the {@link AvaticaFactory} to use. - * @param url the URL to establish the connection to. + * @param driver the {@link ArrowFlightJdbcDriver} to use. + * @param factory the {@link AvaticaFactory} to use. + * @param url the URL to establish the connection to. * @param properties the {@link Properties} to use for this session. - * @param allocator the {@link BufferAllocator} to use. + * @param allocator the {@link BufferAllocator} to use. * @return a new {@link ArrowFlightConnection}. * @throws SQLException on error. */ - static ArrowFlightConnection createNewConnection(final ArrowFlightJdbcDriver driver, - final AvaticaFactory factory, - String url, final Properties properties, - final BufferAllocator allocator) + static ArrowFlightConnection createNewConnection( + final ArrowFlightJdbcDriver driver, + final AvaticaFactory factory, + String url, + final Properties properties, + final BufferAllocator allocator) throws SQLException { url = replaceSemiColons(url); final ArrowFlightConnectionConfigImpl config = new ArrowFlightConnectionConfigImpl(properties); final ArrowFlightSqlClientHandler clientHandler = createNewClientHandler(config, allocator); - return new ArrowFlightConnection(driver, factory, url, properties, config, allocator, clientHandler); + return new ArrowFlightConnection( + driver, factory, url, properties, config, allocator, clientHandler); } private static ArrowFlightSqlClientHandler createNewClientHandler( - final ArrowFlightConnectionConfigImpl config, - final BufferAllocator allocator) throws SQLException { + final ArrowFlightConnectionConfigImpl config, final BufferAllocator allocator) + throws SQLException { try { return new ArrowFlightSqlClientHandler.Builder() .withHost(config.getHost()) @@ -154,10 +156,11 @@ ArrowFlightSqlClientHandler getClientHandler() { * @return the {@link #executorService}. */ synchronized ExecutorService getExecutorService() { - return executorService = executorService == null ? - Executors.newFixedThreadPool(config.threadPoolSize(), - new DefaultThreadFactory(getClass().getSimpleName())) : - executorService; + return executorService = + executorService == null + ? Executors.newFixedThreadPool( + config.threadPoolSize(), new DefaultThreadFactory(getClass().getSimpleName())) + : executorService; } @Override diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightInfoStatement.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightInfoStatement.java index 8365c7bb57a..4f4cb6c9b12 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightInfoStatement.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightInfoStatement.java @@ -19,12 +19,9 @@ import java.sql.SQLException; import java.sql.Statement; - import org.apache.arrow.flight.FlightInfo; -/** - * A {@link Statement} that deals with {@link FlightInfo}. - */ +/** A {@link Statement} that deals with {@link FlightInfo}. */ public interface ArrowFlightInfoStatement extends Statement { @Override diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcArray.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcArray.java index ed67c97cf69..3d8f0ca56dd 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcArray.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcArray.java @@ -23,7 +23,6 @@ import java.sql.SQLFeatureNotSupportedException; import java.util.Arrays; import java.util.Map; - import org.apache.arrow.driver.jdbc.accessor.impl.complex.AbstractArrowFlightJdbcListVectorAccessor; import org.apache.arrow.driver.jdbc.utils.SqlTypes; import org.apache.arrow.memory.util.LargeMemoryUtil; @@ -46,9 +45,10 @@ public class ArrowFlightJdbcArray implements Array { private final long valuesCount; /** - * Instantiate an {@link Array} backed up by given {@link FieldVector}, limited by a start offset and values count. + * Instantiate an {@link Array} backed up by given {@link FieldVector}, limited by a start offset + * and values count. * - * @param dataVector underlying FieldVector, containing the Array items. + * @param dataVector underlying FieldVector, containing the Array items. * @param startOffset offset from FieldVector pointing to this Array's first value. * @param valuesCount how many items this Array contains. */ @@ -111,8 +111,8 @@ public Object getArray(long index, int count, Map> map) throws } checkBoundaries(index, count); - return getArrayNoBoundCheck(this.dataVector, - LargeMemoryUtil.checkedCastToInt(this.startOffset + index), count); + return getArrayNoBoundCheck( + this.dataVector, LargeMemoryUtil.checkedCastToInt(this.startOffset + index), count); } @Override @@ -134,12 +134,11 @@ public ResultSet getResultSet(long index, int count) throws SQLException { return getResultSet(index, count, null); } - private static ResultSet getResultSetNoBoundariesCheck(ValueVector dataVector, long start, - long count) - throws SQLException { + private static ResultSet getResultSetNoBoundariesCheck( + ValueVector dataVector, long start, long count) throws SQLException { TransferPair transferPair = dataVector.getTransferPair(dataVector.getAllocator()); - transferPair.splitAndTransfer(LargeMemoryUtil.checkedCastToInt(start), - LargeMemoryUtil.checkedCastToInt(count)); + transferPair.splitAndTransfer( + LargeMemoryUtil.checkedCastToInt(start), LargeMemoryUtil.checkedCastToInt(count)); FieldVector vectorSlice = (FieldVector) transferPair.getTo(); VectorSchemaRoot vectorSchemaRoot = VectorSchemaRoot.of(vectorSlice); @@ -154,14 +153,12 @@ public ResultSet getResultSet(long index, int count, Map> map) } checkBoundaries(index, count); - return getResultSetNoBoundariesCheck(this.dataVector, - LargeMemoryUtil.checkedCastToInt(this.startOffset + index), count); + return getResultSetNoBoundariesCheck( + this.dataVector, LargeMemoryUtil.checkedCastToInt(this.startOffset + index), count); } @Override - public void free() { - - } + public void free() {} @Override public String toString() { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcConnectionPoolDataSource.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcConnectionPoolDataSource.java index 46a1d3ff87c..3307d8d23d6 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcConnectionPoolDataSource.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcConnectionPoolDataSource.java @@ -23,17 +23,13 @@ import java.util.Queue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; - import javax.sql.ConnectionEvent; import javax.sql.ConnectionEventListener; import javax.sql.ConnectionPoolDataSource; import javax.sql.PooledConnection; - import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl; -/** - * {@link ConnectionPoolDataSource} implementation for Arrow Flight JDBC Driver. - */ +/** {@link ConnectionPoolDataSource} implementation for Arrow Flight JDBC Driver. */ public class ArrowFlightJdbcConnectionPoolDataSource extends ArrowFlightJdbcDataSource implements ConnectionPoolDataSource, ConnectionEventListener, AutoCloseable { private final Map> pool = @@ -43,10 +39,10 @@ public class ArrowFlightJdbcConnectionPoolDataSource extends ArrowFlightJdbcData * Instantiates a new DataSource. * * @param properties the properties - * @param config the config. + * @param config the config. */ - protected ArrowFlightJdbcConnectionPoolDataSource(final Properties properties, - final ArrowFlightConnectionConfigImpl config) { + protected ArrowFlightJdbcConnectionPoolDataSource( + final Properties properties, final ArrowFlightConnectionConfigImpl config) { super(properties, config); } @@ -58,8 +54,8 @@ protected ArrowFlightJdbcConnectionPoolDataSource(final Properties properties, */ public static ArrowFlightJdbcConnectionPoolDataSource createNewDataSource( final Properties properties) { - return new ArrowFlightJdbcConnectionPoolDataSource(properties, - new ArrowFlightConnectionConfigImpl(properties)); + return new ArrowFlightJdbcConnectionPoolDataSource( + properties, new ArrowFlightConnectionConfigImpl(properties)); } @Override @@ -84,8 +80,7 @@ public PooledConnection getPooledConnection(final String username, final String } private ArrowFlightJdbcPooledConnection createPooledConnection( - final ArrowFlightConnectionConfigImpl config) - throws SQLException { + final ArrowFlightConnectionConfigImpl config) throws SQLException { ArrowFlightJdbcPooledConnection pooledConnection = new ArrowFlightJdbcPooledConnection(getConnection(config.getUser(), config.getPassword())); pooledConnection.addConnectionEventListener(this); @@ -102,9 +97,7 @@ public void connectionClosed(ConnectionEvent connectionEvent) { } @Override - public void connectionErrorOccurred(ConnectionEvent connectionEvent) { - - } + public void connectionErrorOccurred(ConnectionEvent connectionEvent) {} @Override public void close() throws Exception { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcCursor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcCursor.java index 45c23e4d529..0982746b4d5 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcCursor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcCursor.java @@ -17,13 +17,11 @@ package org.apache.arrow.driver.jdbc; - import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.util.AutoCloseables; import org.apache.arrow.vector.FieldVector; @@ -34,9 +32,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * Arrow Flight Jdbc's Cursor class. - */ +/** Arrow Flight Jdbc's Cursor class. */ public class ArrowFlightJdbcCursor extends AbstractCursor { private static final Logger LOGGER; @@ -54,18 +50,20 @@ public ArrowFlightJdbcCursor(VectorSchemaRoot root) { } @Override - public List createAccessors(List columns, - Calendar localCalendar, - ArrayImpl.Factory factory) { + public List createAccessors( + List columns, Calendar localCalendar, ArrayImpl.Factory factory) { final List fieldVectors = root.getFieldVectors(); - return IntStream.range(0, fieldVectors.size()).mapToObj(root::getVector) + return IntStream.range(0, fieldVectors.size()) + .mapToObj(root::getVector) .map(this::createAccessor) .collect(Collectors.toCollection(() -> new ArrayList<>(fieldVectors.size()))); } private Accessor createAccessor(FieldVector vector) { - return ArrowFlightJdbcAccessorFactory.createAccessor(vector, this::getCurrentRow, + return ArrowFlightJdbcAccessorFactory.createAccessor( + vector, + this::getCurrentRow, (boolean wasNull) -> { // AbstractCursor creates a boolean array of length 1 to hold the wasNull value this.wasNull[0] = wasNull; @@ -73,8 +71,9 @@ private Accessor createAccessor(FieldVector vector) { } /** - * ArrowFlightJdbcAccessors do not use {@link AbstractCursor.Getter}, as it would box primitive types and cause - * performance issues. Each Accessor implementation works directly on Arrow Vectors. + * ArrowFlightJdbcAccessors do not use {@link AbstractCursor.Getter}, as it would box primitive + * types and cause performance issues. Each Accessor implementation works directly on Arrow + * Vectors. */ @Override protected Getter createGetter(int column) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcDataSource.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcDataSource.java index a57eeaa8304..3ba150ff630 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcDataSource.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcDataSource.java @@ -24,25 +24,19 @@ import java.sql.SQLFeatureNotSupportedException; import java.util.Properties; import java.util.logging.Logger; - import javax.sql.DataSource; - import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl; import org.apache.arrow.util.Preconditions; -/** - * {@link DataSource} implementation for Arrow Flight JDBC Driver. - */ +/** {@link DataSource} implementation for Arrow Flight JDBC Driver. */ public class ArrowFlightJdbcDataSource implements DataSource { private final Properties properties; private final ArrowFlightConnectionConfigImpl config; private PrintWriter logWriter; - /** - * Instantiates a new DataSource. - */ - protected ArrowFlightJdbcDataSource(final Properties properties, - final ArrowFlightConnectionConfigImpl config) { + /** Instantiates a new DataSource. */ + protected ArrowFlightJdbcDataSource( + final Properties properties, final ArrowFlightConnectionConfigImpl config) { this.properties = Preconditions.checkNotNull(properties); this.config = Preconditions.checkNotNull(config); } @@ -57,8 +51,8 @@ protected final ArrowFlightConnectionConfigImpl getConfig() { } /** - * Gets a copy of the {@link #properties} for this {@link ArrowFlightJdbcDataSource} with - * the provided {@code username} and {@code password}. + * Gets a copy of the {@link #properties} for this {@link ArrowFlightJdbcDataSource} with the + * provided {@code username} and {@code password}. * * @return the {@link Properties} for this data source. */ @@ -81,8 +75,8 @@ protected final Properties getProperties(final String username, final String pas * @return a new data source. */ public static ArrowFlightJdbcDataSource createNewDataSource(final Properties properties) { - return new ArrowFlightJdbcDataSource(properties, - new ArrowFlightConnectionConfigImpl(properties)); + return new ArrowFlightJdbcDataSource( + properties, new ArrowFlightConnectionConfigImpl(properties)); } @Override diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcDriver.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcDriver.java index 183e3d5c7b0..b407102052d 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcDriver.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcDriver.java @@ -31,7 +31,6 @@ import java.util.Objects; import java.util.Optional; import java.util.Properties; - import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty; import org.apache.arrow.driver.jdbc.utils.UrlParser; import org.apache.arrow.flight.FlightRuntimeException; @@ -43,14 +42,12 @@ import org.apache.calcite.avatica.Meta; import org.apache.calcite.avatica.UnregisteredDriver; - -/** - * JDBC driver for querying data from an Apache Arrow Flight server. - */ +/** JDBC driver for querying data from an Apache Arrow Flight server. */ public class ArrowFlightJdbcDriver extends UnregisteredDriver { private static final String CONNECT_STRING_PREFIX = "jdbc:arrow-flight-sql://"; private static final String CONNECT_STRING_PREFIX_DEPRECATED = "jdbc:arrow-flight://"; - private static final String CONNECTION_STRING_EXPECTED = "jdbc:arrow-flight-sql://[host][:port][?param1=value&...]"; + private static final String CONNECTION_STRING_EXPECTED = + "jdbc:arrow-flight-sql://[host][:port][?param1=value&...]"; private static DriverVersion version; static { @@ -84,11 +81,7 @@ public ArrowFlightConnection connect(final String url, final Properties info) try { return ArrowFlightConnection.createNewConnection( - this, - factory, - url, - lowerCasePropertyKeys(properties), - new RootAllocator(Long.MAX_VALUE)); + this, factory, url, lowerCasePropertyKeys(properties), new RootAllocator(Long.MAX_VALUE)); } catch (final FlightRuntimeException e) { throw new SQLException("Failed to connect.", e); } @@ -103,11 +96,14 @@ protected String getFactoryClassName(final JdbcVersion jdbcVersion) { @SuppressWarnings("StringSplitter") protected DriverVersion createDriverVersion() { if (version == null) { - final InputStream flightProperties = this.getClass().getResourceAsStream("/properties/flight.properties"); + final InputStream flightProperties = + this.getClass().getResourceAsStream("/properties/flight.properties"); if (flightProperties == null) { - throw new RuntimeException("Flight Properties not found. Ensure the JAR was built properly."); + throw new RuntimeException( + "Flight Properties not found. Ensure the JAR was built properly."); } - try (final Reader reader = new BufferedReader(new InputStreamReader(flightProperties, StandardCharsets.UTF_8))) { + try (final Reader reader = + new BufferedReader(new InputStreamReader(flightProperties, StandardCharsets.UTF_8))) { final Properties properties = new Properties(); properties.load(reader); @@ -119,22 +115,24 @@ protected DriverVersion createDriverVersion() { final int parentMinorVersion = Integer.parseInt(pVersion[1]); final String childName = properties.getProperty("org.apache.arrow.flight.jdbc-driver.name"); - final String childVersion = properties.getProperty("org.apache.arrow.flight.jdbc-driver.version"); + final String childVersion = + properties.getProperty("org.apache.arrow.flight.jdbc-driver.version"); final String[] cVersion = childVersion.split("\\."); final int childMajorVersion = Integer.parseInt(cVersion[0]); final int childMinorVersion = Integer.parseInt(cVersion[1]); - version = new DriverVersion( - childName, - childVersion, - parentName, - parentVersion, - true, - childMajorVersion, - childMinorVersion, - parentMajorVersion, - parentMinorVersion); + version = + new DriverVersion( + childName, + childVersion, + parentName, + parentVersion, + true, + childMajorVersion, + childMinorVersion, + parentMajorVersion, + parentMinorVersion); } catch (final IOException e) { throw new RuntimeException("Failed to load driver version.", e); } @@ -156,15 +154,16 @@ protected String getConnectStringPrefix() { @Override public boolean acceptsURL(final String url) { Preconditions.checkNotNull(url); - return url.startsWith(CONNECT_STRING_PREFIX) || url.startsWith(CONNECT_STRING_PREFIX_DEPRECATED); + return url.startsWith(CONNECT_STRING_PREFIX) + || url.startsWith(CONNECT_STRING_PREFIX_DEPRECATED); } /** - * Parses the provided url based on the format this driver accepts, retrieving - * arguments after the {@link #CONNECT_STRING_PREFIX}. - *

- * This method gets the args if the provided URL follows this pattern: - * {@code jdbc:arrow-flight-sql://:[/?key1=val1&key2=val2&(...)]} + * Parses the provided url based on the format this driver accepts, retrieving arguments after the + * {@link #CONNECT_STRING_PREFIX}. + * + *

This method gets the args if the provided URL follows this pattern: {@code + * jdbc:arrow-flight-sql://:[/?key1=val1&key2=val2&(...)]} * * * @@ -210,8 +209,7 @@ public boolean acceptsURL(final String url) { * @throws SQLException If an error occurs while trying to parse the URL. */ @VisibleForTesting // ArrowFlightJdbcDriverTest - Optional> getUrlsArgs(String url) - throws SQLException { + Optional> getUrlsArgs(String url) throws SQLException { /* * @@ -230,8 +228,9 @@ Optional> getUrlsArgs(String url) url = replaceSemiColons(url); if (!url.startsWith("jdbc:")) { - throw new SQLException("Connection string must start with 'jdbc:'. Expected format: " + - CONNECTION_STRING_EXPECTED); + throw new SQLException( + "Connection string must start with 'jdbc:'. Expected format: " + + CONNECTION_STRING_EXPECTED); } // It's necessary to use a string without "jdbc:" at the beginning to be parsed as a valid URL. @@ -245,15 +244,17 @@ Optional> getUrlsArgs(String url) throw new SQLException("Malformed/invalid URL!", e); } - if (!Objects.equals(uri.getScheme(), "arrow-flight") && - !Objects.equals(uri.getScheme(), "arrow-flight-sql")) { + if (!Objects.equals(uri.getScheme(), "arrow-flight") + && !Objects.equals(uri.getScheme(), "arrow-flight-sql")) { return Optional.empty(); } if (uri.getHost() == null) { - throw new SQLException("URL must have a host. Expected format: " + CONNECTION_STRING_EXPECTED); + throw new SQLException( + "URL must have a host. Expected format: " + CONNECTION_STRING_EXPECTED); } else if (uri.getPort() < 0) { - throw new SQLException("URL must have a port. Expected format: " + CONNECTION_STRING_EXPECTED); + throw new SQLException( + "URL must have a port. Expected format: " + CONNECTION_STRING_EXPECTED); } resultMap.put(ArrowFlightConnectionProperty.HOST.camelName(), uri.getHost()); // host resultMap.put(ArrowFlightConnectionProperty.PORT.camelName(), uri.getPort()); // port diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcFactory.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcFactory.java index 16bdede02d0..7e2352abeb1 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcFactory.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcFactory.java @@ -21,7 +21,6 @@ import java.sql.SQLException; import java.util.Properties; import java.util.TimeZone; - import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler; import org.apache.arrow.memory.RootAllocator; import org.apache.calcite.avatica.AvaticaConnection; @@ -33,9 +32,7 @@ import org.apache.calcite.avatica.QueryState; import org.apache.calcite.avatica.UnregisteredDriver; -/** - * Factory for the Arrow Flight JDBC Driver. - */ +/** Factory for the Arrow Flight JDBC Driver. */ public class ArrowFlightJdbcFactory implements AvaticaFactory { private final int major; private final int minor; @@ -51,16 +48,14 @@ private ArrowFlightJdbcFactory(final int major, final int minor) { } @Override - public AvaticaConnection newConnection(final UnregisteredDriver driver, - final AvaticaFactory factory, - final String url, - final Properties info) throws SQLException { + public AvaticaConnection newConnection( + final UnregisteredDriver driver, + final AvaticaFactory factory, + final String url, + final Properties info) + throws SQLException { return ArrowFlightConnection.createNewConnection( - (ArrowFlightJdbcDriver) driver, - factory, - url, - info, - new RootAllocator(Long.MAX_VALUE)); + (ArrowFlightJdbcDriver) driver, factory, url, info, new RootAllocator(Long.MAX_VALUE)); } @Override @@ -70,8 +65,12 @@ public AvaticaStatement newStatement( final int resultType, final int resultSetConcurrency, final int resultSetHoldability) { - return new ArrowFlightStatement((ArrowFlightConnection) connection, - handle, resultType, resultSetConcurrency, resultSetHoldability); + return new ArrowFlightStatement( + (ArrowFlightConnection) connection, + handle, + resultType, + resultSetConcurrency, + resultSetHoldability); } @Override @@ -81,27 +80,34 @@ public ArrowFlightPreparedStatement newPreparedStatement( final Meta.Signature signature, final int resultType, final int resultSetConcurrency, - final int resultSetHoldability) throws SQLException { + final int resultSetHoldability) + throws SQLException { final ArrowFlightConnection flightConnection = (ArrowFlightConnection) connection; ArrowFlightSqlClientHandler.PreparedStatement preparedStatement = flightConnection.getMeta().getPreparedStatement(statementHandle); return ArrowFlightPreparedStatement.newPreparedStatement( - flightConnection, preparedStatement, statementHandle, - signature, resultType, resultSetConcurrency, resultSetHoldability); + flightConnection, + preparedStatement, + statementHandle, + signature, + resultType, + resultSetConcurrency, + resultSetHoldability); } @Override - public ArrowFlightJdbcVectorSchemaRootResultSet newResultSet(final AvaticaStatement statement, - final QueryState state, - final Meta.Signature signature, - final TimeZone timeZone, - final Meta.Frame frame) + public ArrowFlightJdbcVectorSchemaRootResultSet newResultSet( + final AvaticaStatement statement, + final QueryState state, + final Meta.Signature signature, + final TimeZone timeZone, + final Meta.Frame frame) throws SQLException { final ResultSetMetaData metaData = newResultSetMetaData(statement, signature); - return new ArrowFlightJdbcFlightStreamResultSet(statement, state, signature, metaData, timeZone, - frame); + return new ArrowFlightJdbcFlightStreamResultSet( + statement, state, signature, metaData, timeZone, frame); } @Override @@ -111,10 +117,8 @@ public AvaticaSpecificDatabaseMetaData newDatabaseMetaData(final AvaticaConnecti @Override public ResultSetMetaData newResultSetMetaData( - final AvaticaStatement avaticaStatement, - final Meta.Signature signature) { - return new AvaticaResultSetMetaData(avaticaStatement, - null, signature); + final AvaticaStatement avaticaStatement, final Meta.Signature signature) { + return new AvaticaResultSetMetaData(avaticaStatement, null, signature); } @Override diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcFlightStreamResultSet.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcFlightStreamResultSet.java index f69681d77a5..f22e61a1e68 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcFlightStreamResultSet.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcFlightStreamResultSet.java @@ -25,7 +25,6 @@ import java.util.Optional; import java.util.TimeZone; import java.util.concurrent.TimeUnit; - import org.apache.arrow.driver.jdbc.client.CloseableEndpointStreamPair; import org.apache.arrow.driver.jdbc.utils.FlightEndpointDataQueue; import org.apache.arrow.driver.jdbc.utils.VectorSchemaRootTransformer; @@ -41,8 +40,8 @@ import org.apache.calcite.avatica.QueryState; /** - * {@link ResultSet} implementation for Arrow Flight used to access the results of multiple {@link FlightStream} - * objects. + * {@link ResultSet} implementation for Arrow Flight used to access the results of multiple {@link + * FlightStream} objects. */ public final class ArrowFlightJdbcFlightStreamResultSet extends ArrowFlightJdbcVectorSchemaRootResultSet { @@ -57,49 +56,50 @@ public final class ArrowFlightJdbcFlightStreamResultSet private Schema schema; - /** - * Public constructor used by ArrowFlightJdbcFactory. - */ - ArrowFlightJdbcFlightStreamResultSet(final AvaticaStatement statement, - final QueryState state, - final Meta.Signature signature, - final ResultSetMetaData resultSetMetaData, - final TimeZone timeZone, - final Meta.Frame firstFrame) throws SQLException { + /** Public constructor used by ArrowFlightJdbcFactory. */ + ArrowFlightJdbcFlightStreamResultSet( + final AvaticaStatement statement, + final QueryState state, + final Meta.Signature signature, + final ResultSetMetaData resultSetMetaData, + final TimeZone timeZone, + final Meta.Frame firstFrame) + throws SQLException { super(statement, state, signature, resultSetMetaData, timeZone, firstFrame); this.connection = (ArrowFlightConnection) statement.connection; this.flightInfo = ((ArrowFlightInfoStatement) statement).executeFlightInfoQuery(); } - /** - * Private constructor for fromFlightInfo. - */ - private ArrowFlightJdbcFlightStreamResultSet(final ArrowFlightConnection connection, - final QueryState state, - final Meta.Signature signature, - final ResultSetMetaData resultSetMetaData, - final TimeZone timeZone, - final Meta.Frame firstFrame, - final FlightInfo flightInfo - ) throws SQLException { + /** Private constructor for fromFlightInfo. */ + private ArrowFlightJdbcFlightStreamResultSet( + final ArrowFlightConnection connection, + final QueryState state, + final Meta.Signature signature, + final ResultSetMetaData resultSetMetaData, + final TimeZone timeZone, + final Meta.Frame firstFrame, + final FlightInfo flightInfo) + throws SQLException { super(null, state, signature, resultSetMetaData, timeZone, firstFrame); this.connection = connection; this.flightInfo = flightInfo; } /** - * Create a {@link ResultSet} which pulls data from given {@link FlightInfo}. This is used to fetch result sets - * from DatabaseMetadata calls and skips the Avatica factory. + * Create a {@link ResultSet} which pulls data from given {@link FlightInfo}. This is used to + * fetch result sets from DatabaseMetadata calls and skips the Avatica factory. * - * @param connection The connection linked to the returned ResultSet. - * @param flightInfo The FlightInfo from which data will be iterated by the returned ResultSet. - * @param transformer Optional transformer for processing VectorSchemaRoot before access from ResultSet + * @param connection The connection linked to the returned ResultSet. + * @param flightInfo The FlightInfo from which data will be iterated by the returned ResultSet. + * @param transformer Optional transformer for processing VectorSchemaRoot before access from + * ResultSet * @return A ResultSet which pulls data from given FlightInfo. */ static ArrowFlightJdbcFlightStreamResultSet fromFlightInfo( final ArrowFlightConnection connection, final FlightInfo flightInfo, - final VectorSchemaRootTransformer transformer) throws SQLException { + final VectorSchemaRootTransformer transformer) + throws SQLException { // Similar to how org.apache.calcite.avatica.util.ArrayFactoryImpl does final TimeZone timeZone = TimeZone.getDefault(); @@ -110,8 +110,8 @@ static ArrowFlightJdbcFlightStreamResultSet fromFlightInfo( final AvaticaResultSetMetaData resultSetMetaData = new AvaticaResultSetMetaData(null, null, signature); final ArrowFlightJdbcFlightStreamResultSet resultSet = - new ArrowFlightJdbcFlightStreamResultSet(connection, state, signature, resultSetMetaData, - timeZone, null, flightInfo); + new ArrowFlightJdbcFlightStreamResultSet( + connection, state, signature, resultSetMetaData, timeZone, null, flightInfo); resultSet.transformer = transformer; @@ -167,9 +167,7 @@ private void populateDataForCurrentFlightStream() throws SQLException { populateData(currentVectorSchemaRoot, schema); } - /** - * Expose appMetadata associated with the underlying FlightInfo for this ResultSet. - */ + /** Expose appMetadata associated with the underlying FlightInfo for this ResultSet. */ public byte[] getAppMetadata() { return flightInfo.getAppMetadata(); } @@ -253,11 +251,13 @@ public synchronized void close() { } } - private CloseableEndpointStreamPair getNextEndpointStream(final boolean canTimeout) throws SQLException { + private CloseableEndpointStreamPair getNextEndpointStream(final boolean canTimeout) + throws SQLException { if (canTimeout) { final int statementTimeout = statement != null ? statement.getQueryTimeout() : 0; - return statementTimeout != 0 ? - flightEndpointDataQueue.next(statementTimeout, TimeUnit.SECONDS) : flightEndpointDataQueue.next(); + return statementTimeout != 0 + ? flightEndpointDataQueue.next(statementTimeout, TimeUnit.SECONDS) + : flightEndpointDataQueue.next(); } else { return flightEndpointDataQueue.next(); } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcPooledConnection.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcPooledConnection.java index 96a2d9dda1d..ad8846e218c 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcPooledConnection.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcPooledConnection.java @@ -23,17 +23,13 @@ import java.util.HashSet; import java.util.Properties; import java.util.Set; - import javax.sql.ConnectionEvent; import javax.sql.ConnectionEventListener; import javax.sql.PooledConnection; import javax.sql.StatementEventListener; - import org.apache.arrow.driver.jdbc.utils.ConnectionWrapper; -/** - * {@link PooledConnection} implementation for Arrow Flight JDBC Driver. - */ +/** {@link PooledConnection} implementation for Arrow Flight JDBC Driver. */ public class ArrowFlightJdbcPooledConnection implements PooledConnection { private final ArrowFlightConnection connection; diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcTime.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcTime.java index 109048bc05c..7fd9747b3a9 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcTime.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcTime.java @@ -19,19 +19,18 @@ import static org.apache.calcite.avatica.util.DateTimeUtils.MILLIS_PER_DAY; +import com.google.common.collect.ImmutableList; import java.sql.Time; import java.time.LocalTime; import java.time.temporal.ChronoField; import java.util.List; import java.util.Objects; import java.util.concurrent.TimeUnit; - import org.apache.arrow.util.VisibleForTesting; -import com.google.common.collect.ImmutableList; - /** - * Wrapper class for Time objects to include the milliseconds part in ISO 8601 format in this#toString. + * Wrapper class for Time objects to include the milliseconds part in ISO 8601 format in + * this#toString. */ public class ArrowFlightJdbcTime extends Time { private static final List LEADING_ZEROES = ImmutableList.of("", "0", "00"); @@ -54,7 +53,8 @@ public ArrowFlightJdbcTime(final long milliseconds) { @VisibleForTesting ArrowFlightJdbcTime(final LocalTime time) { - // Although the constructor is deprecated, this is the exact same code as Time#valueOf(LocalTime) + // Although the constructor is deprecated, this is the exact same code as + // Time#valueOf(LocalTime) super(time.getHour(), time.getMinute(), time.getSecond()); millisReprValue = time.get(ChronoField.MILLI_OF_SECOND); } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcVectorSchemaRootResultSet.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcVectorSchemaRootResultSet.java index 626ae95bc5b..a8155adcc96 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcVectorSchemaRootResultSet.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcVectorSchemaRootResultSet.java @@ -25,7 +25,6 @@ import java.util.Objects; import java.util.Set; import java.util.TimeZone; - import org.apache.arrow.driver.jdbc.utils.ConvertUtils; import org.apache.arrow.util.AutoCloseables; import org.apache.arrow.vector.VectorSchemaRoot; @@ -41,19 +40,20 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * {@link ResultSet} implementation used to access a {@link VectorSchemaRoot}. - */ +/** {@link ResultSet} implementation used to access a {@link VectorSchemaRoot}. */ public class ArrowFlightJdbcVectorSchemaRootResultSet extends AvaticaResultSet { private static final Logger LOGGER = LoggerFactory.getLogger(ArrowFlightJdbcVectorSchemaRootResultSet.class); VectorSchemaRoot vectorSchemaRoot; - ArrowFlightJdbcVectorSchemaRootResultSet(final AvaticaStatement statement, final QueryState state, - final Signature signature, - final ResultSetMetaData resultSetMetaData, - final TimeZone timeZone, final Frame firstFrame) + ArrowFlightJdbcVectorSchemaRootResultSet( + final AvaticaStatement statement, + final QueryState state, + final Signature signature, + final ResultSetMetaData resultSetMetaData, + final TimeZone timeZone, + final Frame firstFrame) throws SQLException { super(statement, state, signature, resultSetMetaData, timeZone, firstFrame); } @@ -65,8 +65,7 @@ public class ArrowFlightJdbcVectorSchemaRootResultSet extends AvaticaResultSet { * @return a ResultSet which accesses the given VectorSchemaRoot */ public static ArrowFlightJdbcVectorSchemaRootResultSet fromVectorSchemaRoot( - final VectorSchemaRoot vectorSchemaRoot) - throws SQLException { + final VectorSchemaRoot vectorSchemaRoot) throws SQLException { // Similar to how org.apache.calcite.avatica.util.ArrayFactoryImpl does final TimeZone timeZone = TimeZone.getDefault(); @@ -76,10 +75,9 @@ public static ArrowFlightJdbcVectorSchemaRootResultSet fromVectorSchemaRoot( final AvaticaResultSetMetaData resultSetMetaData = new AvaticaResultSetMetaData(null, null, signature); - final ArrowFlightJdbcVectorSchemaRootResultSet - resultSet = - new ArrowFlightJdbcVectorSchemaRootResultSet(null, state, signature, resultSetMetaData, - timeZone, null); + final ArrowFlightJdbcVectorSchemaRootResultSet resultSet = + new ArrowFlightJdbcVectorSchemaRootResultSet( + null, state, signature, resultSetMetaData, timeZone, null); resultSet.populateData(vectorSchemaRoot); return resultSet; @@ -96,7 +94,8 @@ void populateData(final VectorSchemaRoot vectorSchemaRoot) { void populateData(final VectorSchemaRoot vectorSchemaRoot, final Schema schema) { Schema currentSchema = schema == null ? vectorSchemaRoot.getSchema() : schema; - final List columns = ConvertUtils.convertArrowFieldsToColumnMetaDataList(currentSchema.getFields()); + final List columns = + ConvertUtils.convertArrowFieldsToColumnMetaDataList(currentSchema.getFields()); signature.columns.clear(); signature.columns.addAll(columns); @@ -138,9 +137,11 @@ public void close() { } } exceptions.parallelStream().forEach(e -> LOGGER.error(e.getMessage(), e)); - exceptions.stream().findAny().ifPresent(e -> { - throw new RuntimeException(e); - }); + exceptions.stream() + .findAny() + .ifPresent( + e -> { + throw new RuntimeException(e); + }); } - } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightMetaImpl.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightMetaImpl.java index d25f03ac27b..2ba39cf0773 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightMetaImpl.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightMetaImpl.java @@ -25,7 +25,6 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; - import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler.PreparedStatement; import org.apache.arrow.driver.jdbc.utils.AvaticaParameterBinder; import org.apache.arrow.driver.jdbc.utils.ConvertUtils; @@ -39,14 +38,13 @@ import org.apache.calcite.avatica.QueryState; import org.apache.calcite.avatica.remote.TypedValue; -/** - * Metadata handler for Arrow Flight. - */ +/** Metadata handler for Arrow Flight. */ public class ArrowFlightMetaImpl extends MetaImpl { private final Map statementHandlePreparedStatementMap; /** * Constructs a {@link MetaImpl} object specific for Arrow Flight. + * * @param connection A {@link AvaticaConnection}. */ public ArrowFlightMetaImpl(final AvaticaConnection connection) { @@ -55,14 +53,16 @@ public ArrowFlightMetaImpl(final AvaticaConnection connection) { setDefaultConnectionProperties(); } - /** - * Construct a signature. - */ + /** Construct a signature. */ static Signature newSignature(final String sql, Schema resultSetSchema, Schema parameterSchema) { - List columnMetaData = resultSetSchema == null ? - new ArrayList<>() : ConvertUtils.convertArrowFieldsToColumnMetaDataList(resultSetSchema.getFields()); - List parameters = parameterSchema == null ? - new ArrayList<>() : ConvertUtils.convertArrowFieldsToAvaticaParameters(parameterSchema.getFields()); + List columnMetaData = + resultSetSchema == null + ? new ArrayList<>() + : ConvertUtils.convertArrowFieldsToColumnMetaDataList(resultSetSchema.getFields()); + List parameters = + parameterSchema == null + ? new ArrayList<>() + : ConvertUtils.convertArrowFieldsToAvaticaParameters(parameterSchema.getFields()); return new Signature( columnMetaData, @@ -70,15 +70,15 @@ static Signature newSignature(final String sql, Schema resultSetSchema, Schema p parameters, Collections.emptyMap(), null, // unnecessary, as SQL requests use ArrowFlightJdbcCursor - StatementType.SELECT - ); + StatementType.SELECT); } @Override public void closeStatement(final StatementHandle statementHandle) { PreparedStatement preparedStatement = statementHandlePreparedStatementMap.remove(new StatementHandleKey(statementHandle)); - // Testing if the prepared statement was created because the statement can be not created until this moment + // Testing if the prepared statement was created because the statement can be not created until + // this moment if (preparedStatement != null) { preparedStatement.close(); } @@ -90,54 +90,64 @@ public void commit(final ConnectionHandle connectionHandle) { } @Override - public ExecuteResult execute(final StatementHandle statementHandle, - final List typedValues, final long maxRowCount) { - Preconditions.checkArgument(connection.id.equals(statementHandle.connectionId), - "Connection IDs are not consistent"); + public ExecuteResult execute( + final StatementHandle statementHandle, + final List typedValues, + final long maxRowCount) { + Preconditions.checkArgument( + connection.id.equals(statementHandle.connectionId), "Connection IDs are not consistent"); PreparedStatement preparedStatement = getPreparedStatement(statementHandle); if (preparedStatement == null) { throw new IllegalStateException("Prepared statement not found: " + statementHandle); } - - new AvaticaParameterBinder(preparedStatement, ((ArrowFlightConnection) connection).getBufferAllocator()) - .bind(typedValues); + new AvaticaParameterBinder( + preparedStatement, ((ArrowFlightConnection) connection).getBufferAllocator()) + .bind(typedValues); if (statementHandle.signature == null) { // Update query long updatedCount = preparedStatement.executeUpdate(); - return new ExecuteResult(Collections.singletonList(MetaResultSet.count(statementHandle.connectionId, - statementHandle.id, updatedCount))); + return new ExecuteResult( + Collections.singletonList( + MetaResultSet.count(statementHandle.connectionId, statementHandle.id, updatedCount))); } else { // TODO Why is maxRowCount ignored? return new ExecuteResult( - Collections.singletonList(MetaResultSet.create( - statementHandle.connectionId, statementHandle.id, - true, statementHandle.signature, null))); + Collections.singletonList( + MetaResultSet.create( + statementHandle.connectionId, + statementHandle.id, + true, + statementHandle.signature, + null))); } } @Override - public ExecuteResult execute(final StatementHandle statementHandle, - final List typedValues, final int maxRowsInFirstFrame) { + public ExecuteResult execute( + final StatementHandle statementHandle, + final List typedValues, + final int maxRowsInFirstFrame) { return execute(statementHandle, typedValues, (long) maxRowsInFirstFrame); } @Override - public ExecuteBatchResult executeBatch(final StatementHandle statementHandle, - final List> parameterValuesList) + public ExecuteBatchResult executeBatch( + final StatementHandle statementHandle, final List> parameterValuesList) throws IllegalStateException { - Preconditions.checkArgument(connection.id.equals(statementHandle.connectionId), - "Connection IDs are not consistent"); + Preconditions.checkArgument( + connection.id.equals(statementHandle.connectionId), "Connection IDs are not consistent"); PreparedStatement preparedStatement = getPreparedStatement(statementHandle); if (preparedStatement == null) { throw new IllegalStateException("Prepared statement not found: " + statementHandle); } - final AvaticaParameterBinder binder = new AvaticaParameterBinder(preparedStatement, - ((ArrowFlightConnection) connection).getBufferAllocator()); + final AvaticaParameterBinder binder = + new AvaticaParameterBinder( + preparedStatement, ((ArrowFlightConnection) connection).getBufferAllocator()); for (int i = 0; i < parameterValuesList.size(); i++) { binder.bind(parameterValuesList.get(i), i); } @@ -148,49 +158,53 @@ public ExecuteBatchResult executeBatch(final StatementHandle statementHandle, } @Override - public Frame fetch(final StatementHandle statementHandle, final long offset, - final int fetchMaxRowCount) { + public Frame fetch( + final StatementHandle statementHandle, final long offset, final int fetchMaxRowCount) { /* * ArrowFlightMetaImpl does not use frames. * Instead, we have accessors that contain a VectorSchemaRoot with * the results. */ throw AvaticaConnection.HELPER.wrap( - String.format("%s does not use frames.", this), - AvaticaConnection.HELPER.unsupported()); + String.format("%s does not use frames.", this), AvaticaConnection.HELPER.unsupported()); } private PreparedStatement prepareForHandle(final String query, StatementHandle handle) { final PreparedStatement preparedStatement = ((ArrowFlightConnection) connection).getClientHandler().prepare(query); - handle.signature = newSignature(query, preparedStatement.getDataSetSchema(), - preparedStatement.getParameterSchema()); + handle.signature = + newSignature( + query, preparedStatement.getDataSetSchema(), preparedStatement.getParameterSchema()); statementHandlePreparedStatementMap.put(new StatementHandleKey(handle), preparedStatement); return preparedStatement; } @Override - public StatementHandle prepare(final ConnectionHandle connectionHandle, - final String query, final long maxRowCount) { + public StatementHandle prepare( + final ConnectionHandle connectionHandle, final String query, final long maxRowCount) { final StatementHandle handle = super.createStatement(connectionHandle); prepareForHandle(query, handle); return handle; } @Override - public ExecuteResult prepareAndExecute(final StatementHandle statementHandle, - final String query, final long maxRowCount, - final PrepareCallback prepareCallback) + public ExecuteResult prepareAndExecute( + final StatementHandle statementHandle, + final String query, + final long maxRowCount, + final PrepareCallback prepareCallback) throws NoSuchStatementException { return prepareAndExecute( statementHandle, query, maxRowCount, -1 /* Not used */, prepareCallback); } @Override - public ExecuteResult prepareAndExecute(final StatementHandle handle, - final String query, final long maxRowCount, - final int maxRowsInFirstFrame, - final PrepareCallback callback) + public ExecuteResult prepareAndExecute( + final StatementHandle handle, + final String query, + final long maxRowCount, + final int maxRowsInFirstFrame, + final PrepareCallback callback) throws NoSuchStatementException { try { PreparedStatement preparedStatement = prepareForHandle(query, handle); @@ -203,11 +217,12 @@ public ExecuteResult prepareAndExecute(final StatementHandle handle, callback.assign(handle.signature, null, updateCount); } callback.execute(); - final MetaResultSet metaResultSet = MetaResultSet.create(handle.connectionId, handle.id, - false, handle.signature, null); + final MetaResultSet metaResultSet = + MetaResultSet.create(handle.connectionId, handle.id, false, handle.signature, null); return new ExecuteResult(Collections.singletonList(metaResultSet)); } catch (SQLTimeoutException e) { - // So far AvaticaStatement(executeInternal) only handles NoSuchStatement and Runtime Exceptions. + // So far AvaticaStatement(executeInternal) only handles NoSuchStatement and Runtime + // Exceptions. throw new RuntimeException(e); } catch (SQLException e) { throw new NoSuchStatementException(handle); @@ -228,8 +243,8 @@ public void rollback(final ConnectionHandle connectionHandle) { } @Override - public boolean syncResults(final StatementHandle statementHandle, - final QueryState queryState, final long offset) + public boolean syncResults( + final StatementHandle statementHandle, final QueryState queryState, final long offset) throws NoSuchStatementException { // TODO Fill this stub. return false; @@ -237,7 +252,8 @@ public boolean syncResults(final StatementHandle statementHandle, void setDefaultConnectionProperties() { // TODO Double-check this. - connProps.setDirty(false) + connProps + .setDirty(false) .setAutoCommit(true) .setReadOnly(true) .setCatalog(null) @@ -249,7 +265,8 @@ PreparedStatement getPreparedStatement(StatementHandle statementHandle) { return statementHandlePreparedStatementMap.get(new StatementHandleKey(statementHandle)); } - // Helper used to look up prepared statement instances later. Avatica doesn't give us the signature in + // Helper used to look up prepared statement instances later. Avatica doesn't give us the + // signature in // an UPDATE code path so we can't directly use StatementHandle as a map key. private static final class StatementHandleKey { public final String connectionId; diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatement.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatement.java index 7203f02daa9..6d823934eb0 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatement.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatement.java @@ -19,7 +19,6 @@ import java.sql.PreparedStatement; import java.sql.SQLException; - import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler; import org.apache.arrow.flight.FlightInfo; import org.apache.arrow.util.Preconditions; @@ -27,36 +26,42 @@ import org.apache.calcite.avatica.Meta.Signature; import org.apache.calcite.avatica.Meta.StatementHandle; - -/** - * Arrow Flight JBCS's implementation {@link PreparedStatement}. - */ +/** Arrow Flight JBCS's implementation {@link PreparedStatement}. */ public class ArrowFlightPreparedStatement extends AvaticaPreparedStatement implements ArrowFlightInfoStatement { private final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement; - private ArrowFlightPreparedStatement(final ArrowFlightConnection connection, - final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement, - final StatementHandle handle, - final Signature signature, final int resultSetType, - final int resultSetConcurrency, - final int resultSetHoldability) + private ArrowFlightPreparedStatement( + final ArrowFlightConnection connection, + final ArrowFlightSqlClientHandler.PreparedStatement preparedStatement, + final StatementHandle handle, + final Signature signature, + final int resultSetType, + final int resultSetConcurrency, + final int resultSetHoldability) throws SQLException { super(connection, handle, signature, resultSetType, resultSetConcurrency, resultSetHoldability); this.preparedStatement = Preconditions.checkNotNull(preparedStatement); } - static ArrowFlightPreparedStatement newPreparedStatement(final ArrowFlightConnection connection, + static ArrowFlightPreparedStatement newPreparedStatement( + final ArrowFlightConnection connection, final ArrowFlightSqlClientHandler.PreparedStatement preparedStmt, final StatementHandle statementHandle, final Signature signature, final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability) throws SQLException { + final int resultSetHoldability) + throws SQLException { return new ArrowFlightPreparedStatement( - connection, preparedStmt, statementHandle, - signature, resultSetType, resultSetConcurrency, resultSetHoldability); + connection, + preparedStmt, + statementHandle, + signature, + resultSetType, + resultSetConcurrency, + resultSetHoldability); } @Override diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightStatement.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightStatement.java index 5bc7c2ab9b4..4a267fd3a77 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightStatement.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightStatement.java @@ -18,7 +18,6 @@ package org.apache.arrow.driver.jdbc; import java.sql.SQLException; - import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler.PreparedStatement; import org.apache.arrow.driver.jdbc.utils.ConvertUtils; import org.apache.arrow.flight.FlightInfo; @@ -27,14 +26,15 @@ import org.apache.calcite.avatica.Meta; import org.apache.calcite.avatica.Meta.StatementHandle; -/** - * A SQL statement for querying data from an Arrow Flight server. - */ +/** A SQL statement for querying data from an Arrow Flight server. */ public class ArrowFlightStatement extends AvaticaStatement implements ArrowFlightInfoStatement { - ArrowFlightStatement(final ArrowFlightConnection connection, - final StatementHandle handle, final int resultSetType, - final int resultSetConcurrency, final int resultSetHoldability) { + ArrowFlightStatement( + final ArrowFlightConnection connection, + final StatementHandle handle, + final int resultSetType, + final int resultSetConcurrency, + final int resultSetHoldability) { super(connection, handle, resultSetType, resultSetConcurrency, resultSetHoldability); } @@ -45,14 +45,16 @@ public ArrowFlightConnection getConnection() throws SQLException { @Override public FlightInfo executeFlightInfoQuery() throws SQLException { - final PreparedStatement preparedStatement = getConnection().getMeta().getPreparedStatement(handle); + final PreparedStatement preparedStatement = + getConnection().getMeta().getPreparedStatement(handle); final Meta.Signature signature = getSignature(); if (signature == null) { return null; } final Schema resultSetSchema = preparedStatement.getDataSetSchema(); - signature.columns.addAll(ConvertUtils.convertArrowFieldsToColumnMetaDataList(resultSetSchema.getFields())); + signature.columns.addAll( + ConvertUtils.convertArrowFieldsToColumnMetaDataList(resultSetSchema.getFields())); setSignature(signature); return preparedStatement.executeQuery(); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessor.java index 3821ee1dc87..47dcb3375dd 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessor.java @@ -38,9 +38,7 @@ import java.util.Map; import java.util.function.IntSupplier; -/** - * Base Jdbc Accessor. - */ +/** Base Jdbc Accessor. */ public abstract class ArrowFlightJdbcAccessor implements Accessor { private final IntSupplier currentRowSupplier; @@ -48,8 +46,9 @@ public abstract class ArrowFlightJdbcAccessor implements Accessor { protected boolean wasNull; protected ArrowFlightJdbcAccessorFactory.WasNullConsumer wasNullConsumer; - protected ArrowFlightJdbcAccessor(final IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer wasNullConsumer) { + protected ArrowFlightJdbcAccessor( + final IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer wasNullConsumer) { this.currentRowSupplier = currentRowSupplier; this.wasNullConsumer = wasNullConsumer; } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorFactory.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorFactory.java index 813b40a8070..0cb9e5b7d3c 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorFactory.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorFactory.java @@ -18,7 +18,6 @@ package org.apache.arrow.driver.jdbc.accessor; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.impl.ArrowFlightJdbcNullVectorAccessor; import org.apache.arrow.driver.jdbc.accessor.impl.binary.ArrowFlightJdbcBinaryVectorAccessor; import org.apache.arrow.driver.jdbc.accessor.impl.calendar.ArrowFlightJdbcDateVectorAccessor; @@ -77,136 +76,132 @@ import org.apache.arrow.vector.complex.StructVector; import org.apache.arrow.vector.complex.UnionVector; -/** - * Factory to instantiate the accessors. - */ +/** Factory to instantiate the accessors. */ public class ArrowFlightJdbcAccessorFactory { /** * Create an accessor according to its type. * - * @param vector an instance of an arrow vector. + * @param vector an instance of an arrow vector. * @param getCurrentRow a supplier to check which row is being accessed. * @return an instance of one of the accessors. */ - public static ArrowFlightJdbcAccessor createAccessor(ValueVector vector, - IntSupplier getCurrentRow, - WasNullConsumer setCursorWasNull) { + public static ArrowFlightJdbcAccessor createAccessor( + ValueVector vector, IntSupplier getCurrentRow, WasNullConsumer setCursorWasNull) { if (vector instanceof UInt1Vector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((UInt1Vector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcBaseIntVectorAccessor( + (UInt1Vector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof UInt2Vector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((UInt2Vector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcBaseIntVectorAccessor( + (UInt2Vector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof UInt4Vector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((UInt4Vector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcBaseIntVectorAccessor( + (UInt4Vector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof UInt8Vector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((UInt8Vector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcBaseIntVectorAccessor( + (UInt8Vector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof TinyIntVector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((TinyIntVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcBaseIntVectorAccessor( + (TinyIntVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof SmallIntVector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((SmallIntVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcBaseIntVectorAccessor( + (SmallIntVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof IntVector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((IntVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcBaseIntVectorAccessor( + (IntVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof BigIntVector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((BigIntVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcBaseIntVectorAccessor( + (BigIntVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof Float4Vector) { - return new ArrowFlightJdbcFloat4VectorAccessor((Float4Vector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcFloat4VectorAccessor( + (Float4Vector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof Float8Vector) { - return new ArrowFlightJdbcFloat8VectorAccessor((Float8Vector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcFloat8VectorAccessor( + (Float8Vector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof BitVector) { - return new ArrowFlightJdbcBitVectorAccessor((BitVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcBitVectorAccessor( + (BitVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof DecimalVector) { - return new ArrowFlightJdbcDecimalVectorAccessor((DecimalVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcDecimalVectorAccessor( + (DecimalVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof Decimal256Vector) { - return new ArrowFlightJdbcDecimalVectorAccessor((Decimal256Vector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcDecimalVectorAccessor( + (Decimal256Vector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof VarBinaryVector) { - return new ArrowFlightJdbcBinaryVectorAccessor((VarBinaryVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcBinaryVectorAccessor( + (VarBinaryVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof LargeVarBinaryVector) { - return new ArrowFlightJdbcBinaryVectorAccessor((LargeVarBinaryVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcBinaryVectorAccessor( + (LargeVarBinaryVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof FixedSizeBinaryVector) { - return new ArrowFlightJdbcBinaryVectorAccessor((FixedSizeBinaryVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcBinaryVectorAccessor( + (FixedSizeBinaryVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof TimeStampVector) { - return new ArrowFlightJdbcTimeStampVectorAccessor((TimeStampVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcTimeStampVectorAccessor( + (TimeStampVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof TimeNanoVector) { - return new ArrowFlightJdbcTimeVectorAccessor((TimeNanoVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcTimeVectorAccessor( + (TimeNanoVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof TimeMicroVector) { - return new ArrowFlightJdbcTimeVectorAccessor((TimeMicroVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcTimeVectorAccessor( + (TimeMicroVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof TimeMilliVector) { - return new ArrowFlightJdbcTimeVectorAccessor((TimeMilliVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcTimeVectorAccessor( + (TimeMilliVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof TimeSecVector) { - return new ArrowFlightJdbcTimeVectorAccessor((TimeSecVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcTimeVectorAccessor( + (TimeSecVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof DateDayVector) { - return new ArrowFlightJdbcDateVectorAccessor(((DateDayVector) vector), getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcDateVectorAccessor( + ((DateDayVector) vector), getCurrentRow, setCursorWasNull); } else if (vector instanceof DateMilliVector) { - return new ArrowFlightJdbcDateVectorAccessor(((DateMilliVector) vector), getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcDateVectorAccessor( + ((DateMilliVector) vector), getCurrentRow, setCursorWasNull); } else if (vector instanceof VarCharVector) { - return new ArrowFlightJdbcVarCharVectorAccessor((VarCharVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcVarCharVectorAccessor( + (VarCharVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof LargeVarCharVector) { - return new ArrowFlightJdbcVarCharVectorAccessor((LargeVarCharVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcVarCharVectorAccessor( + (LargeVarCharVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof DurationVector) { - return new ArrowFlightJdbcDurationVectorAccessor((DurationVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcDurationVectorAccessor( + (DurationVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof IntervalDayVector) { - return new ArrowFlightJdbcIntervalVectorAccessor(((IntervalDayVector) vector), getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcIntervalVectorAccessor( + ((IntervalDayVector) vector), getCurrentRow, setCursorWasNull); } else if (vector instanceof IntervalYearVector) { - return new ArrowFlightJdbcIntervalVectorAccessor(((IntervalYearVector) vector), getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcIntervalVectorAccessor( + ((IntervalYearVector) vector), getCurrentRow, setCursorWasNull); } else if (vector instanceof StructVector) { - return new ArrowFlightJdbcStructVectorAccessor((StructVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcStructVectorAccessor( + (StructVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof MapVector) { - return new ArrowFlightJdbcMapVectorAccessor((MapVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcMapVectorAccessor( + (MapVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof ListVector) { - return new ArrowFlightJdbcListVectorAccessor((ListVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcListVectorAccessor( + (ListVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof LargeListVector) { - return new ArrowFlightJdbcLargeListVectorAccessor((LargeListVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcLargeListVectorAccessor( + (LargeListVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof FixedSizeListVector) { - return new ArrowFlightJdbcFixedSizeListVectorAccessor((FixedSizeListVector) vector, - getCurrentRow, setCursorWasNull); + return new ArrowFlightJdbcFixedSizeListVectorAccessor( + (FixedSizeListVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof UnionVector) { - return new ArrowFlightJdbcUnionVectorAccessor((UnionVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcUnionVectorAccessor( + (UnionVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof DenseUnionVector) { - return new ArrowFlightJdbcDenseUnionVectorAccessor((DenseUnionVector) vector, getCurrentRow, - setCursorWasNull); + return new ArrowFlightJdbcDenseUnionVectorAccessor( + (DenseUnionVector) vector, getCurrentRow, setCursorWasNull); } else if (vector instanceof NullVector || vector == null) { return new ArrowFlightJdbcNullVectorAccessor(setCursorWasNull); } - throw new UnsupportedOperationException("Unsupported vector type: " + vector.getClass().getName()); + throw new UnsupportedOperationException( + "Unsupported vector type: " + vector.getClass().getName()); } - /** - * Functional interface used to propagate that the value accessed was null or not. - */ + /** Functional interface used to propagate that the value accessed was null or not. */ @FunctionalInterface public interface WasNullConsumer { void setWasNull(boolean wasNull); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/ArrowFlightJdbcNullVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/ArrowFlightJdbcNullVectorAccessor.java index f40a5797293..b1ef1d4d02b 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/ArrowFlightJdbcNullVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/ArrowFlightJdbcNullVectorAccessor.java @@ -21,9 +21,7 @@ import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.NullVector; -/** - * Accessor for the Arrow type {@link NullVector}. - */ +/** Accessor for the Arrow type {@link NullVector}. */ public class ArrowFlightJdbcNullVectorAccessor extends ArrowFlightJdbcAccessor { public ArrowFlightJdbcNullVectorAccessor( ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/binary/ArrowFlightJdbcBinaryVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/binary/ArrowFlightJdbcBinaryVectorAccessor.java index c50d7349721..df7854575bf 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/binary/ArrowFlightJdbcBinaryVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/binary/ArrowFlightJdbcBinaryVectorAccessor.java @@ -23,7 +23,6 @@ import java.io.Reader; import java.nio.charset.StandardCharsets; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.FixedSizeBinaryVector; @@ -31,8 +30,8 @@ import org.apache.arrow.vector.VarBinaryVector; /** - * Accessor for the Arrow types: {@link FixedSizeBinaryVector}, {@link VarBinaryVector} - * and {@link LargeVarBinaryVector}. + * Accessor for the Arrow types: {@link FixedSizeBinaryVector}, {@link VarBinaryVector} and {@link + * LargeVarBinaryVector}. */ public class ArrowFlightJdbcBinaryVectorAccessor extends ArrowFlightJdbcAccessor { @@ -42,26 +41,31 @@ private interface ByteArrayGetter { private final ByteArrayGetter getter; - public ArrowFlightJdbcBinaryVectorAccessor(FixedSizeBinaryVector vector, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcBinaryVectorAccessor( + FixedSizeBinaryVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { this(vector::get, currentRowSupplier, setCursorWasNull); } - public ArrowFlightJdbcBinaryVectorAccessor(VarBinaryVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcBinaryVectorAccessor( + VarBinaryVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { this(vector::get, currentRowSupplier, setCursorWasNull); } - public ArrowFlightJdbcBinaryVectorAccessor(LargeVarBinaryVector vector, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcBinaryVectorAccessor( + LargeVarBinaryVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { this(vector::get, currentRowSupplier, setCursorWasNull); } - private ArrowFlightJdbcBinaryVectorAccessor(ByteArrayGetter getter, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + private ArrowFlightJdbcBinaryVectorAccessor( + ByteArrayGetter getter, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.getter = getter; } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDateVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDateVectorAccessor.java index f6c14a47f52..f5a2ae93316 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDateVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDateVectorAccessor.java @@ -29,7 +29,6 @@ import java.util.Calendar; import java.util.concurrent.TimeUnit; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.driver.jdbc.utils.DateTimeUtils; @@ -37,9 +36,7 @@ import org.apache.arrow.vector.DateMilliVector; import org.apache.arrow.vector.ValueVector; -/** - * Accessor for the Arrow types: {@link DateDayVector} and {@link DateMilliVector}. - */ +/** Accessor for the Arrow types: {@link DateDayVector} and {@link DateMilliVector}. */ public class ArrowFlightJdbcDateVectorAccessor extends ArrowFlightJdbcAccessor { private final Getter getter; @@ -49,12 +46,14 @@ public class ArrowFlightJdbcDateVectorAccessor extends ArrowFlightJdbcAccessor { /** * Instantiate an accessor for a {@link DateDayVector}. * - * @param vector an instance of a DateDayVector. + * @param vector an instance of a DateDayVector. * @param currentRowSupplier the supplier to track the lines. - * @param setCursorWasNull the consumer to set if value was null. + * @param setCursorWasNull the consumer to set if value was null. */ - public ArrowFlightJdbcDateVectorAccessor(DateDayVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcDateVectorAccessor( + DateDayVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.holder = new Holder(); this.getter = createGetter(vector); @@ -64,11 +63,13 @@ public ArrowFlightJdbcDateVectorAccessor(DateDayVector vector, IntSupplier curre /** * Instantiate an accessor for a {@link DateMilliVector}. * - * @param vector an instance of a DateMilliVector. + * @param vector an instance of a DateMilliVector. * @param currentRowSupplier the supplier to track the lines. */ - public ArrowFlightJdbcDateVectorAccessor(DateMilliVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcDateVectorAccessor( + DateMilliVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.holder = new Holder(); this.getter = createGetter(vector); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDateVectorGetter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDateVectorGetter.java index ea545851a3a..0c97605e9be 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDateVectorGetter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDateVectorGetter.java @@ -22,9 +22,7 @@ import org.apache.arrow.vector.holders.NullableDateDayHolder; import org.apache.arrow.vector.holders.NullableDateMilliHolder; -/** - * Auxiliary class used to unify data access on TimeStampVectors. - */ +/** Auxiliary class used to unify data access on TimeStampVectors. */ final class ArrowFlightJdbcDateVectorGetter { private ArrowFlightJdbcDateVectorGetter() { @@ -32,16 +30,15 @@ private ArrowFlightJdbcDateVectorGetter() { } /** - * Auxiliary class meant to unify Date*Vector#get implementations with different classes of ValueHolders. + * Auxiliary class meant to unify Date*Vector#get implementations with different classes of + * ValueHolders. */ static class Holder { int isSet; // Tells if value is set; 0 = not set, 1 = set long value; // Holds actual value in its respective timeunit } - /** - * Functional interface used to unify Date*Vector#get implementations. - */ + /** Functional interface used to unify Date*Vector#get implementations. */ @FunctionalInterface interface Getter { void get(int index, Holder holder); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDurationVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDurationVectorAccessor.java index 22a0e6f8923..0690afb0e00 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDurationVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDurationVectorAccessor.java @@ -19,21 +19,19 @@ import java.time.Duration; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.DurationVector; -/** - * Accessor for the Arrow type {@link DurationVector}. - */ +/** Accessor for the Arrow type {@link DurationVector}. */ public class ArrowFlightJdbcDurationVectorAccessor extends ArrowFlightJdbcAccessor { private final DurationVector vector; - public ArrowFlightJdbcDurationVectorAccessor(DurationVector vector, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcDurationVectorAccessor( + DurationVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.vector = vector; } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcIntervalVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcIntervalVectorAccessor.java index 21d1c15712c..27c08f333bb 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcIntervalVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcIntervalVectorAccessor.java @@ -25,7 +25,6 @@ import java.time.Duration; import java.time.Period; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.BaseFixedWidthVector; @@ -34,9 +33,7 @@ import org.apache.arrow.vector.holders.NullableIntervalDayHolder; import org.apache.arrow.vector.holders.NullableIntervalYearHolder; -/** - * Accessor for the Arrow type {@link IntervalDayVector}. - */ +/** Accessor for the Arrow type {@link IntervalDayVector}. */ public class ArrowFlightJdbcIntervalVectorAccessor extends ArrowFlightJdbcAccessor { private final BaseFixedWidthVector vector; @@ -46,53 +43,57 @@ public class ArrowFlightJdbcIntervalVectorAccessor extends ArrowFlightJdbcAccess /** * Instantiate an accessor for a {@link IntervalDayVector}. * - * @param vector an instance of a IntervalDayVector. + * @param vector an instance of a IntervalDayVector. * @param currentRowSupplier the supplier to track the rows. - * @param setCursorWasNull the consumer to set if value was null. + * @param setCursorWasNull the consumer to set if value was null. */ - public ArrowFlightJdbcIntervalVectorAccessor(IntervalDayVector vector, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcIntervalVectorAccessor( + IntervalDayVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.vector = vector; - stringGetter = (index) -> { - final NullableIntervalDayHolder holder = new NullableIntervalDayHolder(); - vector.get(index, holder); - if (holder.isSet == 0) { - return null; - } else { - final int days = holder.days; - final int millis = holder.milliseconds; - return formatIntervalDay(Duration.ofDays(days).plusMillis(millis)); - } - }; + stringGetter = + (index) -> { + final NullableIntervalDayHolder holder = new NullableIntervalDayHolder(); + vector.get(index, holder); + if (holder.isSet == 0) { + return null; + } else { + final int days = holder.days; + final int millis = holder.milliseconds; + return formatIntervalDay(Duration.ofDays(days).plusMillis(millis)); + } + }; objectClass = java.time.Duration.class; } /** * Instantiate an accessor for a {@link IntervalYearVector}. * - * @param vector an instance of a IntervalYearVector. + * @param vector an instance of a IntervalYearVector. * @param currentRowSupplier the supplier to track the rows. - * @param setCursorWasNull the consumer to set if value was null. + * @param setCursorWasNull the consumer to set if value was null. */ - public ArrowFlightJdbcIntervalVectorAccessor(IntervalYearVector vector, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcIntervalVectorAccessor( + IntervalYearVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.vector = vector; - stringGetter = (index) -> { - final NullableIntervalYearHolder holder = new NullableIntervalYearHolder(); - vector.get(index, holder); - if (holder.isSet == 0) { - return null; - } else { - final int interval = holder.value; - final int years = (interval / yearsToMonths); - final int months = (interval % yearsToMonths); - return formatIntervalYear(Period.ofYears(years).plusMonths(months)); - } - }; + stringGetter = + (index) -> { + final NullableIntervalYearHolder holder = new NullableIntervalYearHolder(); + vector.get(index, holder); + if (holder.isSet == 0) { + return null; + } else { + final int interval = holder.value; + final int years = (interval / yearsToMonths); + final int months = (interval % yearsToMonths); + return formatIntervalYear(Period.ofYears(years).plusMonths(months)); + } + }; objectClass = java.time.Period.class; } @@ -117,9 +118,7 @@ public Object getObject() { return object; } - /** - * Functional interface used to unify Interval*Vector#getAsStringBuilder implementations. - */ + /** Functional interface used to unify Interval*Vector#getAsStringBuilder implementations. */ @FunctionalInterface interface StringGetter { String get(int index); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessor.java index a23883baf1e..d60d320e4b2 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessor.java @@ -30,16 +30,13 @@ import java.util.TimeZone; import java.util.concurrent.TimeUnit; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.TimeStampVector; import org.apache.arrow.vector.types.pojo.ArrowType; import org.apache.arrow.vector.util.DateUtility; -/** - * Accessor for the Arrow types extending from {@link TimeStampVector}. - */ +/** Accessor for the Arrow types extending from {@link TimeStampVector}. */ public class ArrowFlightJdbcTimeStampVectorAccessor extends ArrowFlightJdbcAccessor { private final TimeZone timeZone; @@ -48,19 +45,16 @@ public class ArrowFlightJdbcTimeStampVectorAccessor extends ArrowFlightJdbcAcces private final LongToLocalDateTime longToLocalDateTime; private final Holder holder; - /** - * Functional interface used to convert a number (in any time resolution) to LocalDateTime. - */ + /** Functional interface used to convert a number (in any time resolution) to LocalDateTime. */ interface LongToLocalDateTime { LocalDateTime fromLong(long value); } - /** - * Instantiate a ArrowFlightJdbcTimeStampVectorAccessor for given vector. - */ - public ArrowFlightJdbcTimeStampVectorAccessor(TimeStampVector vector, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + /** Instantiate a ArrowFlightJdbcTimeStampVectorAccessor for given vector. */ + public ArrowFlightJdbcTimeStampVectorAccessor( + TimeStampVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.holder = new Holder(); this.getter = createGetter(vector); @@ -95,8 +89,9 @@ private LocalDateTime getLocalDateTime(Calendar calendar) { if (calendar != null) { TimeZone timeZone = calendar.getTimeZone(); long millis = this.timeUnit.toMillis(value); - localDateTime = localDateTime - .minus(timeZone.getOffset(millis) - this.timeZone.getOffset(millis), ChronoUnit.MILLIS); + localDateTime = + localDateTime.minus( + timeZone.getOffset(millis) - this.timeZone.getOffset(millis), ChronoUnit.MILLIS); } return localDateTime; } @@ -149,8 +144,8 @@ protected static TimeUnit getTimeUnitForVector(TimeStampVector vector) { } } - protected static LongToLocalDateTime getLongToLocalDateTimeForVector(TimeStampVector vector, - TimeZone timeZone) { + protected static LongToLocalDateTime getLongToLocalDateTimeForVector( + TimeStampVector vector, TimeZone timeZone) { String timeZoneID = timeZone.getID(); ArrowType.Timestamp arrowType = @@ -164,8 +159,9 @@ protected static LongToLocalDateTime getLongToLocalDateTimeForVector(TimeStampVe case MILLISECOND: return milliseconds -> DateUtility.getLocalDateTimeFromEpochMilli(milliseconds, timeZoneID); case SECOND: - return seconds -> DateUtility.getLocalDateTimeFromEpochMilli( - TimeUnit.SECONDS.toMillis(seconds), timeZoneID); + return seconds -> + DateUtility.getLocalDateTimeFromEpochMilli( + TimeUnit.SECONDS.toMillis(seconds), timeZoneID); default: throw new UnsupportedOperationException("Invalid Arrow time unit"); } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorGetter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorGetter.java index 03fb35face7..8bdc5856bc9 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorGetter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorGetter.java @@ -35,9 +35,7 @@ import org.apache.arrow.vector.holders.NullableTimeStampSecHolder; import org.apache.arrow.vector.holders.NullableTimeStampSecTZHolder; -/** - * Auxiliary class used to unify data access on TimeStampVectors. - */ +/** Auxiliary class used to unify data access on TimeStampVectors. */ final class ArrowFlightJdbcTimeStampVectorGetter { private ArrowFlightJdbcTimeStampVectorGetter() { @@ -45,16 +43,15 @@ private ArrowFlightJdbcTimeStampVectorGetter() { } /** - * Auxiliary class meant to unify TimeStamp*Vector#get implementations with different classes of ValueHolders. + * Auxiliary class meant to unify TimeStamp*Vector#get implementations with different classes of + * ValueHolders. */ static class Holder { int isSet; // Tells if value is set; 0 = not set, 1 = set long value; // Holds actual value in its respective timeunit } - /** - * Functional interface used to unify TimeStamp*Vector#get implementations. - */ + /** Functional interface used to unify TimeStamp*Vector#get implementations. */ @FunctionalInterface interface Getter { void get(int index, Holder holder); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeVectorAccessor.java index 6c2173d5e56..81fecfaf184 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeVectorAccessor.java @@ -26,7 +26,6 @@ import java.util.Calendar; import java.util.concurrent.TimeUnit; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.ArrowFlightJdbcTime; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; @@ -38,8 +37,8 @@ import org.apache.arrow.vector.ValueVector; /** - * Accessor for the Arrow types: {@link TimeNanoVector}, {@link TimeMicroVector}, {@link TimeMilliVector} - * and {@link TimeSecVector}. + * Accessor for the Arrow types: {@link TimeNanoVector}, {@link TimeMicroVector}, {@link + * TimeMilliVector} and {@link TimeSecVector}. */ public class ArrowFlightJdbcTimeVectorAccessor extends ArrowFlightJdbcAccessor { @@ -50,12 +49,14 @@ public class ArrowFlightJdbcTimeVectorAccessor extends ArrowFlightJdbcAccessor { /** * Instantiate an accessor for a {@link TimeNanoVector}. * - * @param vector an instance of a TimeNanoVector. + * @param vector an instance of a TimeNanoVector. * @param currentRowSupplier the supplier to track the lines. - * @param setCursorWasNull the consumer to set if value was null. + * @param setCursorWasNull the consumer to set if value was null. */ - public ArrowFlightJdbcTimeVectorAccessor(TimeNanoVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcTimeVectorAccessor( + TimeNanoVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.holder = new Holder(); this.getter = createGetter(vector); @@ -65,12 +66,14 @@ public ArrowFlightJdbcTimeVectorAccessor(TimeNanoVector vector, IntSupplier curr /** * Instantiate an accessor for a {@link TimeMicroVector}. * - * @param vector an instance of a TimeMicroVector. + * @param vector an instance of a TimeMicroVector. * @param currentRowSupplier the supplier to track the lines. - * @param setCursorWasNull the consumer to set if value was null. + * @param setCursorWasNull the consumer to set if value was null. */ - public ArrowFlightJdbcTimeVectorAccessor(TimeMicroVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcTimeVectorAccessor( + TimeMicroVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.holder = new Holder(); this.getter = createGetter(vector); @@ -80,11 +83,13 @@ public ArrowFlightJdbcTimeVectorAccessor(TimeMicroVector vector, IntSupplier cur /** * Instantiate an accessor for a {@link TimeMilliVector}. * - * @param vector an instance of a TimeMilliVector. + * @param vector an instance of a TimeMilliVector. * @param currentRowSupplier the supplier to track the lines. */ - public ArrowFlightJdbcTimeVectorAccessor(TimeMilliVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcTimeVectorAccessor( + TimeMilliVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.holder = new Holder(); this.getter = createGetter(vector); @@ -94,11 +99,13 @@ public ArrowFlightJdbcTimeVectorAccessor(TimeMilliVector vector, IntSupplier cur /** * Instantiate an accessor for a {@link TimeSecVector}. * - * @param vector an instance of a TimeSecVector. + * @param vector an instance of a TimeSecVector. * @param currentRowSupplier the supplier to track the lines. */ - public ArrowFlightJdbcTimeVectorAccessor(TimeSecVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcTimeVectorAccessor( + TimeSecVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.holder = new Holder(); this.getter = createGetter(vector); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeVectorGetter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeVectorGetter.java index fb254c69401..754749369c2 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeVectorGetter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeVectorGetter.java @@ -26,9 +26,7 @@ import org.apache.arrow.vector.holders.NullableTimeNanoHolder; import org.apache.arrow.vector.holders.NullableTimeSecHolder; -/** - * Auxiliary class used to unify data access on Time*Vectors. - */ +/** Auxiliary class used to unify data access on Time*Vectors. */ final class ArrowFlightJdbcTimeVectorGetter { private ArrowFlightJdbcTimeVectorGetter() { @@ -36,16 +34,15 @@ private ArrowFlightJdbcTimeVectorGetter() { } /** - * Auxiliary class meant to unify TimeStamp*Vector#get implementations with different classes of ValueHolders. + * Auxiliary class meant to unify TimeStamp*Vector#get implementations with different classes of + * ValueHolders. */ static class Holder { int isSet; // Tells if value is set; 0 = not set, 1 = set long value; // Holds actual value in its respective timeunit } - /** - * Functional interface used to unify TimeStamp*Vector#get implementations. - */ + /** Functional interface used to unify TimeStamp*Vector#get implementations. */ @FunctionalInterface interface Getter { void get(int index, Holder holder); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcListVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcListVectorAccessor.java index d3338608f83..dd9e2fc5d93 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcListVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcListVectorAccessor.java @@ -20,7 +20,6 @@ import java.sql.Array; import java.util.List; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.ArrowFlightJdbcArray; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; @@ -30,12 +29,14 @@ import org.apache.arrow.vector.complex.ListVector; /** - * Base Accessor for the Arrow types {@link ListVector}, {@link LargeListVector} and {@link FixedSizeListVector}. + * Base Accessor for the Arrow types {@link ListVector}, {@link LargeListVector} and {@link + * FixedSizeListVector}. */ public abstract class AbstractArrowFlightJdbcListVectorAccessor extends ArrowFlightJdbcAccessor { - protected AbstractArrowFlightJdbcListVectorAccessor(IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + protected AbstractArrowFlightJdbcListVectorAccessor( + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); } @@ -70,4 +71,3 @@ public final Array getArray() { return new ArrowFlightJdbcArray(dataVector, startOffset, valuesCount); } } - diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessor.java index 0465765f183..62980af3239 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessor.java @@ -35,7 +35,6 @@ import java.util.Calendar; import java.util.Map; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.driver.jdbc.accessor.impl.ArrowFlightJdbcNullVectorAccessor; @@ -43,22 +42,20 @@ import org.apache.arrow.vector.complex.DenseUnionVector; import org.apache.arrow.vector.complex.UnionVector; -/** - * Base accessor for {@link UnionVector} and {@link DenseUnionVector}. - */ +/** Base accessor for {@link UnionVector} and {@link DenseUnionVector}. */ public abstract class AbstractArrowFlightJdbcUnionVectorAccessor extends ArrowFlightJdbcAccessor { /** - * Array of accessors for each type contained in UnionVector. - * Index corresponds to UnionVector and DenseUnionVector typeIds which are both limited to 128. + * Array of accessors for each type contained in UnionVector. Index corresponds to UnionVector and + * DenseUnionVector typeIds which are both limited to 128. */ private final ArrowFlightJdbcAccessor[] accessors = new ArrowFlightJdbcAccessor[128]; private final ArrowFlightJdbcNullVectorAccessor nullAccessor = - new ArrowFlightJdbcNullVectorAccessor((boolean wasNull) -> { - }); + new ArrowFlightJdbcNullVectorAccessor((boolean wasNull) -> {}); - protected AbstractArrowFlightJdbcUnionVectorAccessor(IntSupplier currentRowSupplier, + protected AbstractArrowFlightJdbcUnionVectorAccessor( + IntSupplier currentRowSupplier, ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcDenseUnionVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcDenseUnionVectorAccessor.java index ba5b83ade63..74c1754cfbf 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcDenseUnionVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcDenseUnionVectorAccessor.java @@ -18,15 +18,12 @@ package org.apache.arrow.driver.jdbc.accessor.impl.complex; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.ValueVector; import org.apache.arrow.vector.complex.DenseUnionVector; -/** - * Accessor for the Arrow type {@link DenseUnionVector}. - */ +/** Accessor for the Arrow type {@link DenseUnionVector}. */ public class ArrowFlightJdbcDenseUnionVectorAccessor extends AbstractArrowFlightJdbcUnionVectorAccessor { @@ -35,22 +32,22 @@ public class ArrowFlightJdbcDenseUnionVectorAccessor /** * Instantiate an accessor for a {@link DenseUnionVector}. * - * @param vector an instance of a DenseUnionVector. + * @param vector an instance of a DenseUnionVector. * @param currentRowSupplier the supplier to track the rows. - * @param setCursorWasNull the consumer to set if value was null. + * @param setCursorWasNull the consumer to set if value was null. */ - public ArrowFlightJdbcDenseUnionVectorAccessor(DenseUnionVector vector, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcDenseUnionVectorAccessor( + DenseUnionVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.vector = vector; } @Override protected ArrowFlightJdbcAccessor createAccessorForVector(ValueVector vector) { - return ArrowFlightJdbcAccessorFactory.createAccessor(vector, - () -> this.vector.getOffset(this.getCurrentRow()), (boolean wasNull) -> { - }); + return ArrowFlightJdbcAccessorFactory.createAccessor( + vector, () -> this.vector.getOffset(this.getCurrentRow()), (boolean wasNull) -> {}); } @Override diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcFixedSizeListVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcFixedSizeListVectorAccessor.java index 7bdd3abfd0c..b55cc3bec19 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcFixedSizeListVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcFixedSizeListVectorAccessor.java @@ -19,22 +19,20 @@ import java.util.List; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.complex.FixedSizeListVector; -/** - * Accessor for the Arrow type {@link FixedSizeListVector}. - */ +/** Accessor for the Arrow type {@link FixedSizeListVector}. */ public class ArrowFlightJdbcFixedSizeListVectorAccessor extends AbstractArrowFlightJdbcListVectorAccessor { private final FixedSizeListVector vector; - public ArrowFlightJdbcFixedSizeListVectorAccessor(FixedSizeListVector vector, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcFixedSizeListVectorAccessor( + FixedSizeListVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.vector = vector; } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcLargeListVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcLargeListVectorAccessor.java index f7608bb06e5..f88ccf35465 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcLargeListVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcLargeListVectorAccessor.java @@ -19,22 +19,20 @@ import java.util.List; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.complex.LargeListVector; -/** - * Accessor for the Arrow type {@link LargeListVector}. - */ +/** Accessor for the Arrow type {@link LargeListVector}. */ public class ArrowFlightJdbcLargeListVectorAccessor extends AbstractArrowFlightJdbcListVectorAccessor { private final LargeListVector vector; - public ArrowFlightJdbcLargeListVectorAccessor(LargeListVector vector, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcLargeListVectorAccessor( + LargeListVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.vector = vector; } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcListVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcListVectorAccessor.java index a329a344073..daa7ed82767 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcListVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcListVectorAccessor.java @@ -19,21 +19,20 @@ import java.util.List; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.complex.BaseRepeatedValueVector; import org.apache.arrow.vector.complex.ListVector; -/** - * Accessor for the Arrow type {@link ListVector}. - */ +/** Accessor for the Arrow type {@link ListVector}. */ public class ArrowFlightJdbcListVectorAccessor extends AbstractArrowFlightJdbcListVectorAccessor { private final ListVector vector; - public ArrowFlightJdbcListVectorAccessor(ListVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcListVectorAccessor( + ListVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.vector = vector; } @@ -45,7 +44,8 @@ protected long getStartOffset(int index) { @Override protected long getEndOffset(int index) { - return vector.getOffsetBuffer() + return vector + .getOffsetBuffer() .getInt((long) (index + 1) * BaseRepeatedValueVector.OFFSET_WIDTH); } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcMapVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcMapVectorAccessor.java index bf1225b33de..b2476cbc021 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcMapVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcMapVectorAccessor.java @@ -19,7 +19,6 @@ import java.util.Map; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.complex.BaseRepeatedValueVector; @@ -27,15 +26,15 @@ import org.apache.arrow.vector.complex.impl.UnionMapReader; import org.apache.arrow.vector.util.JsonStringHashMap; -/** - * Accessor for the Arrow type {@link MapVector}. - */ +/** Accessor for the Arrow type {@link MapVector}. */ public class ArrowFlightJdbcMapVectorAccessor extends AbstractArrowFlightJdbcListVectorAccessor { private final MapVector vector; - public ArrowFlightJdbcMapVectorAccessor(MapVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcMapVectorAccessor( + MapVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.vector = vector; } @@ -76,7 +75,8 @@ protected long getStartOffset(int index) { @Override protected long getEndOffset(int index) { - return vector.getOffsetBuffer() + return vector + .getOffsetBuffer() .getInt((long) (index + 1) * BaseRepeatedValueVector.OFFSET_WIDTH); } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcStructVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcStructVectorAccessor.java index 8a7ac117113..755d927a95b 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcStructVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcStructVectorAccessor.java @@ -22,21 +22,20 @@ import java.util.Map; import java.util.function.IntSupplier; import java.util.stream.Collectors; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.complex.StructVector; import org.apache.calcite.avatica.util.StructImpl; -/** - * Accessor for the Arrow type {@link StructVector}. - */ +/** Accessor for the Arrow type {@link StructVector}. */ public class ArrowFlightJdbcStructVectorAccessor extends ArrowFlightJdbcAccessor { private final StructVector vector; - public ArrowFlightJdbcStructVectorAccessor(StructVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcStructVectorAccessor( + StructVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.vector = vector; } @@ -65,10 +64,10 @@ public Struct getStruct() { return null; } - List attributes = vector.getChildrenFromFields() - .stream() - .map(vector -> vector.getObject(currentRow)) - .collect(Collectors.toList()); + List attributes = + vector.getChildrenFromFields().stream() + .map(vector -> vector.getObject(currentRow)) + .collect(Collectors.toList()); return new StructImpl(attributes); } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcUnionVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcUnionVectorAccessor.java index 5b5a0a472d5..39fb232b525 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcUnionVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcUnionVectorAccessor.java @@ -18,15 +18,12 @@ package org.apache.arrow.driver.jdbc.accessor.impl.complex; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.ValueVector; import org.apache.arrow.vector.complex.UnionVector; -/** - * Accessor for the Arrow type {@link UnionVector}. - */ +/** Accessor for the Arrow type {@link UnionVector}. */ public class ArrowFlightJdbcUnionVectorAccessor extends AbstractArrowFlightJdbcUnionVectorAccessor { private final UnionVector vector; @@ -34,21 +31,22 @@ public class ArrowFlightJdbcUnionVectorAccessor extends AbstractArrowFlightJdbcU /** * Instantiate an accessor for a {@link UnionVector}. * - * @param vector an instance of a UnionVector. + * @param vector an instance of a UnionVector. * @param currentRowSupplier the supplier to track the rows. - * @param setCursorWasNull the consumer to set if value was null. + * @param setCursorWasNull the consumer to set if value was null. */ - public ArrowFlightJdbcUnionVectorAccessor(UnionVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcUnionVectorAccessor( + UnionVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.vector = vector; } @Override protected ArrowFlightJdbcAccessor createAccessorForVector(ValueVector vector) { - return ArrowFlightJdbcAccessorFactory.createAccessor(vector, this::getCurrentRow, - (boolean wasNull) -> { - }); + return ArrowFlightJdbcAccessorFactory.createAccessor( + vector, this::getCurrentRow, (boolean wasNull) -> {}); } @Override diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBaseIntVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBaseIntVectorAccessor.java index 8d2fe1cc703..299fc97c27f 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBaseIntVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBaseIntVectorAccessor.java @@ -23,7 +23,6 @@ import java.math.BigDecimal; import java.math.RoundingMode; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.driver.jdbc.accessor.impl.numeric.ArrowFlightJdbcNumericGetter.NumericHolder; @@ -49,48 +48,66 @@ public class ArrowFlightJdbcBaseIntVectorAccessor extends ArrowFlightJdbcAccesso private final Getter getter; private final NumericHolder holder; - public ArrowFlightJdbcBaseIntVectorAccessor(UInt1Vector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcBaseIntVectorAccessor( + UInt1Vector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { this(vector, currentRowSupplier, true, setCursorWasNull); } - public ArrowFlightJdbcBaseIntVectorAccessor(UInt2Vector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcBaseIntVectorAccessor( + UInt2Vector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { this(vector, currentRowSupplier, true, setCursorWasNull); } - public ArrowFlightJdbcBaseIntVectorAccessor(UInt4Vector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcBaseIntVectorAccessor( + UInt4Vector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { this(vector, currentRowSupplier, true, setCursorWasNull); } - public ArrowFlightJdbcBaseIntVectorAccessor(UInt8Vector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcBaseIntVectorAccessor( + UInt8Vector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { this(vector, currentRowSupplier, true, setCursorWasNull); } - public ArrowFlightJdbcBaseIntVectorAccessor(TinyIntVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcBaseIntVectorAccessor( + TinyIntVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { this(vector, currentRowSupplier, false, setCursorWasNull); } - public ArrowFlightJdbcBaseIntVectorAccessor(SmallIntVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcBaseIntVectorAccessor( + SmallIntVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { this(vector, currentRowSupplier, false, setCursorWasNull); } - public ArrowFlightJdbcBaseIntVectorAccessor(IntVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcBaseIntVectorAccessor( + IntVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { this(vector, currentRowSupplier, false, setCursorWasNull); } - public ArrowFlightJdbcBaseIntVectorAccessor(BigIntVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcBaseIntVectorAccessor( + BigIntVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { this(vector, currentRowSupplier, false, setCursorWasNull); } - private ArrowFlightJdbcBaseIntVectorAccessor(BaseIntVector vector, IntSupplier currentRowSupplier, - boolean isUnsigned, + private ArrowFlightJdbcBaseIntVectorAccessor( + BaseIntVector vector, + IntSupplier currentRowSupplier, + boolean isUnsigned, ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.type = vector.getMinorType(); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBitVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBitVectorAccessor.java index 67d98c2e698..8d380816656 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBitVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBitVectorAccessor.java @@ -19,15 +19,12 @@ import java.math.BigDecimal; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.BitVector; import org.apache.arrow.vector.holders.NullableBitHolder; -/** - * Accessor for the arrow {@link BitVector}. - */ +/** Accessor for the arrow {@link BitVector}. */ public class ArrowFlightJdbcBitVectorAccessor extends ArrowFlightJdbcAccessor { private final BitVector vector; @@ -36,12 +33,14 @@ public class ArrowFlightJdbcBitVectorAccessor extends ArrowFlightJdbcAccessor { /** * Constructor for the BitVectorAccessor. * - * @param vector an instance of a {@link BitVector}. + * @param vector an instance of a {@link BitVector}. * @param currentRowSupplier a supplier to check which row is being accessed. - * @param setCursorWasNull the consumer to set if value was null. + * @param setCursorWasNull the consumer to set if value was null. */ - public ArrowFlightJdbcBitVectorAccessor(BitVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcBitVectorAccessor( + BitVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.vector = vector; this.holder = new NullableBitHolder(); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcDecimalVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcDecimalVectorAccessor.java index 0f7d618a609..5e1e17ad9b5 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcDecimalVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcDecimalVectorAccessor.java @@ -20,36 +20,34 @@ import java.math.BigDecimal; import java.math.RoundingMode; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.Decimal256Vector; import org.apache.arrow.vector.DecimalVector; -/** - * Accessor for {@link DecimalVector} and {@link Decimal256Vector}. - */ +/** Accessor for {@link DecimalVector} and {@link Decimal256Vector}. */ public class ArrowFlightJdbcDecimalVectorAccessor extends ArrowFlightJdbcAccessor { private final Getter getter; - /** - * Functional interface used to unify Decimal*Vector#getObject implementations. - */ + /** Functional interface used to unify Decimal*Vector#getObject implementations. */ @FunctionalInterface interface Getter { BigDecimal getObject(int index); } - public ArrowFlightJdbcDecimalVectorAccessor(DecimalVector vector, IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcDecimalVectorAccessor( + DecimalVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.getter = vector::getObject; } - public ArrowFlightJdbcDecimalVectorAccessor(Decimal256Vector vector, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcDecimalVectorAccessor( + Decimal256Vector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.getter = vector::getObject; } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat4VectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat4VectorAccessor.java index cbf2d36ff80..4dd9065af86 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat4VectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat4VectorAccessor.java @@ -21,15 +21,12 @@ import java.math.RoundingMode; import java.sql.SQLException; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.Float4Vector; import org.apache.arrow.vector.holders.NullableFloat4Holder; -/** - * Accessor for the Float4Vector. - */ +/** Accessor for the Float4Vector. */ public class ArrowFlightJdbcFloat4VectorAccessor extends ArrowFlightJdbcAccessor { private final Float4Vector vector; @@ -38,13 +35,14 @@ public class ArrowFlightJdbcFloat4VectorAccessor extends ArrowFlightJdbcAccessor /** * Instantiate a accessor for the {@link Float4Vector}. * - * @param vector an instance of a Float4Vector. + * @param vector an instance of a Float4Vector. * @param currentRowSupplier the supplier to track the lines. - * @param setCursorWasNull the consumer to set if value was null. + * @param setCursorWasNull the consumer to set if value was null. */ - public ArrowFlightJdbcFloat4VectorAccessor(Float4Vector vector, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcFloat4VectorAccessor( + Float4Vector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.holder = new NullableFloat4Holder(); this.vector = vector; diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat8VectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat8VectorAccessor.java index dc5542ffc58..44538f011d4 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat8VectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat8VectorAccessor.java @@ -21,15 +21,12 @@ import java.math.RoundingMode; import java.sql.SQLException; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.vector.Float8Vector; import org.apache.arrow.vector.holders.NullableFloat8Holder; -/** - * Accessor for the Float8Vector. - */ +/** Accessor for the Float8Vector. */ public class ArrowFlightJdbcFloat8VectorAccessor extends ArrowFlightJdbcAccessor { private final Float8Vector vector; @@ -38,13 +35,14 @@ public class ArrowFlightJdbcFloat8VectorAccessor extends ArrowFlightJdbcAccessor /** * Instantiate a accessor for the {@link Float8Vector}. * - * @param vector an instance of a Float8Vector. + * @param vector an instance of a Float8Vector. * @param currentRowSupplier the supplier to track the lines. - * @param setCursorWasNull the consumer to set if value was null. + * @param setCursorWasNull the consumer to set if value was null. */ - public ArrowFlightJdbcFloat8VectorAccessor(Float8Vector vector, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcFloat8VectorAccessor( + Float8Vector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.holder = new NullableFloat8Holder(); this.vector = vector; diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcNumericGetter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcNumericGetter.java index cc802a0089d..4052d1efad3 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcNumericGetter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcNumericGetter.java @@ -15,7 +15,6 @@ * limitations under the License. */ - package org.apache.arrow.driver.jdbc.accessor.impl.numeric; import org.apache.arrow.vector.BaseIntVector; @@ -36,29 +35,22 @@ import org.apache.arrow.vector.holders.NullableUInt4Holder; import org.apache.arrow.vector.holders.NullableUInt8Holder; -/** - * A custom getter for values from the {@link BaseIntVector}. - */ +/** A custom getter for values from the {@link BaseIntVector}. */ class ArrowFlightJdbcNumericGetter { - /** - * A holder for values from the {@link BaseIntVector}. - */ + /** A holder for values from the {@link BaseIntVector}. */ static class NumericHolder { int isSet; // Tells if value is set; 0 = not set, 1 = set long value; // Holds actual value } - /** - * Functional interface for a getter to baseInt values. - */ + /** Functional interface for a getter to baseInt values. */ @FunctionalInterface interface Getter { void get(int index, NumericHolder holder); } /** - * Main class that will check the type of the vector to create - * a specific getter. + * Main class that will check the type of the vector to create a specific getter. * * @param vector an instance of the {@link BaseIntVector} * @return a getter. diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessor.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessor.java index d4075bbb75e..7eef1078196 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessor.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessor.java @@ -31,7 +31,6 @@ import java.sql.Timestamp; import java.util.Calendar; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.driver.jdbc.utils.DateTimeUtils; @@ -39,14 +38,10 @@ import org.apache.arrow.vector.VarCharVector; import org.apache.arrow.vector.util.Text; -/** - * Accessor for the Arrow types: {@link VarCharVector} and {@link LargeVarCharVector}. - */ +/** Accessor for the Arrow types: {@link VarCharVector} and {@link LargeVarCharVector}. */ public class ArrowFlightJdbcVarCharVectorAccessor extends ArrowFlightJdbcAccessor { - /** - * Functional interface to help integrating VarCharVector and LargeVarCharVector. - */ + /** Functional interface to help integrating VarCharVector and LargeVarCharVector. */ @FunctionalInterface interface Getter { byte[] get(int index); @@ -54,21 +49,24 @@ interface Getter { private final Getter getter; - public ArrowFlightJdbcVarCharVectorAccessor(VarCharVector vector, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcVarCharVectorAccessor( + VarCharVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { this(vector::get, currentRowSupplier, setCursorWasNull); } - public ArrowFlightJdbcVarCharVectorAccessor(LargeVarCharVector vector, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + public ArrowFlightJdbcVarCharVectorAccessor( + LargeVarCharVector vector, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { this(vector::get, currentRowSupplier, setCursorWasNull); } - ArrowFlightJdbcVarCharVectorAccessor(Getter getter, - IntSupplier currentRowSupplier, - ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { + ArrowFlightJdbcVarCharVectorAccessor( + Getter getter, + IntSupplier currentRowSupplier, + ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); this.getter = getter; } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java index 234820bd418..5ca14c8dc4f 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java @@ -27,7 +27,6 @@ import java.util.HashSet; import java.util.List; import java.util.Set; - import org.apache.arrow.driver.jdbc.client.utils.ClientAuthenticationUtils; import org.apache.arrow.flight.CallOption; import org.apache.arrow.flight.FlightClient; @@ -56,9 +55,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * A {@link FlightSqlClient} handler. - */ +/** A {@link FlightSqlClient} handler. */ public final class ArrowFlightSqlClientHandler implements AutoCloseable { private static final Logger LOGGER = LoggerFactory.getLogger(ArrowFlightSqlClientHandler.class); @@ -66,9 +63,10 @@ public final class ArrowFlightSqlClientHandler implements AutoCloseable { private final Set options = new HashSet<>(); private final Builder builder; - ArrowFlightSqlClientHandler(final FlightSqlClient sqlClient, - final Builder builder, - final Collection credentialOptions) { + ArrowFlightSqlClientHandler( + final FlightSqlClient sqlClient, + final Builder builder, + final Collection credentialOptions) { this.options.addAll(builder.options); this.options.addAll(credentialOptions); this.sqlClient = Preconditions.checkNotNull(sqlClient); @@ -76,15 +74,15 @@ public final class ArrowFlightSqlClientHandler implements AutoCloseable { } /** - * Creates a new {@link ArrowFlightSqlClientHandler} from the provided {@code client} and {@code options}. + * Creates a new {@link ArrowFlightSqlClientHandler} from the provided {@code client} and {@code + * options}. * - * @param client the {@link FlightClient} to manage under a {@link FlightSqlClient} wrapper. + * @param client the {@link FlightClient} to manage under a {@link FlightSqlClient} wrapper. * @param options the {@link CallOption}s to persist in between subsequent client calls. * @return a new {@link ArrowFlightSqlClientHandler}. */ - public static ArrowFlightSqlClientHandler createNewHandler(final FlightClient client, - final Builder builder, - final Collection options) { + public static ArrowFlightSqlClientHandler createNewHandler( + final FlightClient client, final Builder builder, final Collection options) { return new ArrowFlightSqlClientHandler(new FlightSqlClient(client), builder, options); } @@ -98,13 +96,14 @@ private CallOption[] getOptions() { } /** - * Makes an RPC "getStream" request based on the provided {@link FlightInfo} - * object. Retrieves the result of the query previously prepared with "getInfo." + * Makes an RPC "getStream" request based on the provided {@link FlightInfo} object. Retrieves the + * result of the query previously prepared with "getInfo." * * @param flightInfo The {@link FlightInfo} instance from which to fetch results. * @return a {@code FlightStream} of results. */ - public List getStreams(final FlightInfo flightInfo) throws SQLException { + public List getStreams(final FlightInfo flightInfo) + throws SQLException { final ArrayList endpoints = new ArrayList<>(flightInfo.getEndpoints().size()); @@ -112,28 +111,36 @@ public List getStreams(final FlightInfo flightInfo) for (FlightEndpoint endpoint : flightInfo.getEndpoints()) { if (endpoint.getLocations().isEmpty()) { // Create a stream using the current client only and do not close the client at the end. - endpoints.add(new CloseableEndpointStreamPair( - sqlClient.getStream(endpoint.getTicket(), getOptions()), null)); + endpoints.add( + new CloseableEndpointStreamPair( + sqlClient.getStream(endpoint.getTicket(), getOptions()), null)); } else { // Clone the builder and then set the new endpoint on it. - // GH-38573: This code currently only tries the first Location and treats a failure as fatal. + // GH-38573: This code currently only tries the first Location and treats a failure as + // fatal. // This should be changed to try other Locations that are available. - - // GH-38574: Currently a new FlightClient will be made for each partition that returns a non-empty Location - // then disposed of. It may be better to cache clients because a server may report the same Locations. - // It would also be good to identify when the reported location is the same as the original connection's + + // GH-38574: Currently a new FlightClient will be made for each partition that returns a + // non-empty Location + // then disposed of. It may be better to cache clients because a server may report the + // same Locations. + // It would also be good to identify when the reported location is the same as the + // original connection's // Location and skip creating a FlightClient in that scenario. final URI endpointUri = endpoint.getLocations().get(0).getUri(); - final Builder builderForEndpoint = new Builder(ArrowFlightSqlClientHandler.this.builder) - .withHost(endpointUri.getHost()) - .withPort(endpointUri.getPort()) - .withEncryption(endpointUri.getScheme().equals(LocationSchemes.GRPC_TLS)); + final Builder builderForEndpoint = + new Builder(ArrowFlightSqlClientHandler.this.builder) + .withHost(endpointUri.getHost()) + .withPort(endpointUri.getPort()) + .withEncryption(endpointUri.getScheme().equals(LocationSchemes.GRPC_TLS)); final ArrowFlightSqlClientHandler endpointHandler = builderForEndpoint.build(); try { - endpoints.add(new CloseableEndpointStreamPair( - endpointHandler.sqlClient.getStream(endpoint.getTicket(), - endpointHandler.getOptions()), endpointHandler.sqlClient)); + endpoints.add( + new CloseableEndpointStreamPair( + endpointHandler.sqlClient.getStream( + endpoint.getTicket(), endpointHandler.getOptions()), + endpointHandler.sqlClient)); } catch (Exception ex) { AutoCloseables.close(endpointHandler); throw ex; @@ -156,8 +163,7 @@ public List getStreams(final FlightInfo flightInfo) } /** - * Makes an RPC "getInfo" request based on the provided {@code query} - * object. + * Makes an RPC "getInfo" request based on the provided {@code query} object. * * @param query The query. * @return a {@code FlightStream} of results. @@ -175,9 +181,7 @@ public void close() throws SQLException { } } - /** - * A prepared statement handler. - */ + /** A prepared statement handler. */ public interface PreparedStatement extends AutoCloseable { /** * Executes this {@link PreparedStatement}. @@ -268,9 +272,9 @@ public void close() { preparedStatement.close(getOptions()); } catch (FlightRuntimeException fre) { // ARROW-17785: suppress exceptions caused by flaky gRPC layer - if (fre.status().code().equals(FlightStatusCode.UNAVAILABLE) || - (fre.status().code().equals(FlightStatusCode.INTERNAL) && - fre.getMessage().contains("Connection closed after GOAWAY"))) { + if (fre.status().code().equals(FlightStatusCode.UNAVAILABLE) + || (fre.status().code().equals(FlightStatusCode.INTERNAL) + && fre.getMessage().contains("Connection closed after GOAWAY"))) { LOGGER.warn("Supressed error closing PreparedStatement", fre); return; } @@ -293,12 +297,12 @@ public FlightInfo getCatalogs() { * Makes an RPC "getImportedKeys" request based on the provided info. * * @param catalog The catalog name. Must match the catalog name as it is stored in the database. - * Retrieves those without a catalog. Null means that the catalog name should not be used to - * narrow the search. - * @param schema The schema name. Must match the schema name as it is stored in the database. - * "" retrieves those without a schema. Null means that the schema name should not be used to narrow - * the search. - * @param table The table name. Must match the table name as it is stored in the database. + * Retrieves those without a catalog. Null means that the catalog name should not be used to + * narrow the search. + * @param schema The schema name. Must match the schema name as it is stored in the database. "" + * retrieves those without a schema. Null means that the schema name should not be used to + * narrow the search. + * @param table The table name. Must match the table name as it is stored in the database. * @return a {@code FlightStream} of results. */ public FlightInfo getImportedKeys(final String catalog, final String schema, final String table) { @@ -309,12 +313,12 @@ public FlightInfo getImportedKeys(final String catalog, final String schema, fin * Makes an RPC "getExportedKeys" request based on the provided info. * * @param catalog The catalog name. Must match the catalog name as it is stored in the database. - * Retrieves those without a catalog. Null means that the catalog name should not be used to - * narrow the search. - * @param schema The schema name. Must match the schema name as it is stored in the database. - * "" retrieves those without a schema. Null means that the schema name should not be used to narrow - * the search. - * @param table The table name. Must match the table name as it is stored in the database. + * Retrieves those without a catalog. Null means that the catalog name should not be used to + * narrow the search. + * @param schema The schema name. Must match the schema name as it is stored in the database. "" + * retrieves those without a schema. Null means that the schema name should not be used to + * narrow the search. + * @param table The table name. Must match the table name as it is stored in the database. * @return a {@code FlightStream} of results. */ public FlightInfo getExportedKeys(final String catalog, final String schema, final String table) { @@ -324,11 +328,11 @@ public FlightInfo getExportedKeys(final String catalog, final String schema, fin /** * Makes an RPC "getSchemas" request based on the provided info. * - * @param catalog The catalog name. Must match the catalog name as it is stored in the database. - * Retrieves those without a catalog. Null means that the catalog name should not be used to - * narrow the search. - * @param schemaPattern The schema name pattern. Must match the schema name as it is stored in the database. - * Null means that schema name should not be used to narrow down the search. + * @param catalog The catalog name. Must match the catalog name as it is stored in the database. + * Retrieves those without a catalog. Null means that the catalog name should not be used to + * narrow the search. + * @param schemaPattern The schema name pattern. Must match the schema name as it is stored in the + * database. Null means that schema name should not be used to narrow down the search. * @return a {@code FlightStream} of results. */ public FlightInfo getSchemas(final String catalog, final String schemaPattern) { @@ -347,24 +351,28 @@ public FlightInfo getTableTypes() { /** * Makes an RPC "getTables" request based on the provided info. * - * @param catalog The catalog name. Must match the catalog name as it is stored in the database. - * Retrieves those without a catalog. Null means that the catalog name should not be used to - * narrow the search. - * @param schemaPattern The schema name pattern. Must match the schema name as it is stored in the database. - * "" retrieves those without a schema. Null means that the schema name should not be used to - * narrow the search. - * @param tableNamePattern The table name pattern. Must match the table name as it is stored in the database. - * @param types The list of table types, which must be from the list of table types to include. - * Null returns all types. - * @param includeSchema Whether to include schema. + * @param catalog The catalog name. Must match the catalog name as it is stored in the database. + * Retrieves those without a catalog. Null means that the catalog name should not be used to + * narrow the search. + * @param schemaPattern The schema name pattern. Must match the schema name as it is stored in the + * database. "" retrieves those without a schema. Null means that the schema name should not + * be used to narrow the search. + * @param tableNamePattern The table name pattern. Must match the table name as it is stored in + * the database. + * @param types The list of table types, which must be from the list of table types to include. + * Null returns all types. + * @param includeSchema Whether to include schema. * @return a {@code FlightStream} of results. */ - public FlightInfo getTables(final String catalog, final String schemaPattern, - final String tableNamePattern, - final List types, final boolean includeSchema) { - - return sqlClient.getTables(catalog, schemaPattern, tableNamePattern, types, includeSchema, - getOptions()); + public FlightInfo getTables( + final String catalog, + final String schemaPattern, + final String tableNamePattern, + final List types, + final boolean includeSchema) { + + return sqlClient.getTables( + catalog, schemaPattern, tableNamePattern, types, includeSchema, getOptions()); } /** @@ -380,12 +388,12 @@ public FlightInfo getSqlInfo(SqlInfo... info) { * Makes an RPC "getPrimaryKeys" request based on the provided info. * * @param catalog The catalog name; must match the catalog name as it is stored in the database. - * "" retrieves those without a catalog. - * Null means that the catalog name should not be used to narrow the search. - * @param schema The schema name; must match the schema name as it is stored in the database. - * "" retrieves those without a schema. Null means that the schema name should not be used to narrow - * the search. - * @param table The table name. Must match the table name as it is stored in the database. + * "" retrieves those without a catalog. Null means that the catalog name should not be used + * to narrow the search. + * @param schema The schema name; must match the schema name as it is stored in the database. "" + * retrieves those without a schema. Null means that the schema name should not be used to + * narrow the search. + * @param table The table name. Must match the table name as it is stored in the database. * @return a {@code FlightStream} of results. */ public FlightInfo getPrimaryKeys(final String catalog, final String schema, final String table) { @@ -396,90 +404,80 @@ public FlightInfo getPrimaryKeys(final String catalog, final String schema, fina * Makes an RPC "getCrossReference" request based on the provided info. * * @param pkCatalog The catalog name. Must match the catalog name as it is stored in the database. - * Retrieves those without a catalog. Null means that the catalog name should not be used to - * narrow the search. - * @param pkSchema The schema name. Must match the schema name as it is stored in the database. - * "" retrieves those without a schema. Null means that the schema name should not be used to narrow - * the search. - * @param pkTable The table name. Must match the table name as it is stored in the database. + * Retrieves those without a catalog. Null means that the catalog name should not be used to + * narrow the search. + * @param pkSchema The schema name. Must match the schema name as it is stored in the database. "" + * retrieves those without a schema. Null means that the schema name should not be used to + * narrow the search. + * @param pkTable The table name. Must match the table name as it is stored in the database. * @param fkCatalog The catalog name. Must match the catalog name as it is stored in the database. - * Retrieves those without a catalog. Null means that the catalog name should not be used to - * narrow the search. - * @param fkSchema The schema name. Must match the schema name as it is stored in the database. - * "" retrieves those without a schema. Null means that the schema name should not be used to narrow - * the search. - * @param fkTable The table name. Must match the table name as it is stored in the database. + * Retrieves those without a catalog. Null means that the catalog name should not be used to + * narrow the search. + * @param fkSchema The schema name. Must match the schema name as it is stored in the database. "" + * retrieves those without a schema. Null means that the schema name should not be used to + * narrow the search. + * @param fkTable The table name. Must match the table name as it is stored in the database. * @return a {@code FlightStream} of results. */ - public FlightInfo getCrossReference(String pkCatalog, String pkSchema, String pkTable, - String fkCatalog, String fkSchema, String fkTable) { - return sqlClient.getCrossReference(TableRef.of(pkCatalog, pkSchema, pkTable), + public FlightInfo getCrossReference( + String pkCatalog, + String pkSchema, + String pkTable, + String fkCatalog, + String fkSchema, + String fkTable) { + return sqlClient.getCrossReference( + TableRef.of(pkCatalog, pkSchema, pkTable), TableRef.of(fkCatalog, fkSchema, fkTable), getOptions()); } - /** - * Builder for {@link ArrowFlightSqlClientHandler}. - */ + /** Builder for {@link ArrowFlightSqlClientHandler}. */ public static final class Builder { private final Set middlewareFactories = new HashSet<>(); private final Set options = new HashSet<>(); private String host; private int port; - @VisibleForTesting - String username; + @VisibleForTesting String username; - @VisibleForTesting - String password; + @VisibleForTesting String password; - @VisibleForTesting - String trustStorePath; + @VisibleForTesting String trustStorePath; - @VisibleForTesting - String trustStorePassword; + @VisibleForTesting String trustStorePassword; - @VisibleForTesting - String token; + @VisibleForTesting String token; - @VisibleForTesting - boolean useEncryption = true; + @VisibleForTesting boolean useEncryption = true; - @VisibleForTesting - boolean disableCertificateVerification; + @VisibleForTesting boolean disableCertificateVerification; - @VisibleForTesting - boolean useSystemTrustStore = true; + @VisibleForTesting boolean useSystemTrustStore = true; - @VisibleForTesting - String tlsRootCertificatesPath; + @VisibleForTesting String tlsRootCertificatesPath; - @VisibleForTesting - String clientCertificatePath; + @VisibleForTesting String clientCertificatePath; - @VisibleForTesting - String clientKeyPath; + @VisibleForTesting String clientKeyPath; - @VisibleForTesting - private BufferAllocator allocator; + @VisibleForTesting private BufferAllocator allocator; - @VisibleForTesting - boolean retainCookies = true; + @VisibleForTesting boolean retainCookies = true; - @VisibleForTesting - boolean retainAuth = true; + @VisibleForTesting boolean retainAuth = true; - // These two middleware are for internal use within build() and should not be exposed by builder APIs. + // These two middleware are for internal use within build() and should not be exposed by builder + // APIs. // Note that these middleware may not necessarily be registered. @VisibleForTesting - ClientIncomingAuthHeaderMiddleware.Factory authFactory - = new ClientIncomingAuthHeaderMiddleware.Factory(new ClientBearerHeaderHandler()); + ClientIncomingAuthHeaderMiddleware.Factory authFactory = + new ClientIncomingAuthHeaderMiddleware.Factory(new ClientBearerHeaderHandler()); @VisibleForTesting ClientCookieMiddleware.Factory cookieFactory = new ClientCookieMiddleware.Factory(); - public Builder() { - } + public Builder() {} /** * Copies the builder. @@ -597,7 +595,8 @@ public Builder withEncryption(final boolean useEncryption) { * @param disableCertificateVerification whether to disable certificate verification. * @return this instance. */ - public Builder withDisableCertificateVerification(final boolean disableCertificateVerification) { + public Builder withDisableCertificateVerification( + final boolean disableCertificateVerification) { this.disableCertificateVerification = disableCertificateVerification; return this; } @@ -614,8 +613,8 @@ public Builder withSystemTrustStore(final boolean useSystemTrustStore) { } /** - * Sets the TLS root certificate path as an alternative to using the System - * or other Trust Store. The path must contain a valid PEM file. + * Sets the TLS root certificate path as an alternative to using the System or other Trust + * Store. The path must contain a valid PEM file. * * @param tlsRootCertificatesPath the TLS root certificate path (if TLS is required). * @return this instance. @@ -646,12 +645,12 @@ public Builder withClientKey(final String clientKeyPath) { this.clientKeyPath = clientKeyPath; return this; } - + /** * Sets the token used in the token authentication. * * @param token the token value. - * @return this builder instance. + * @return this builder instance. */ public Builder withToken(final String token) { this.token = token; @@ -665,15 +664,16 @@ public Builder withToken(final String token) { * @return this instance. */ public Builder withBufferAllocator(final BufferAllocator allocator) { - this.allocator = allocator - .newChildAllocator("ArrowFlightSqlClientHandler", 0, allocator.getLimit()); + this.allocator = + allocator.newChildAllocator("ArrowFlightSqlClientHandler", 0, allocator.getLimit()); return this; } /** * Indicates if cookies should be re-used by connections spawned for getStreams() calls. + * * @param retainCookies The flag indicating if cookies should be re-used. - * @return this builder instance. + * @return this builder instance. */ public Builder withRetainCookies(boolean retainCookies) { this.retainCookies = retainCookies; @@ -681,11 +681,11 @@ public Builder withRetainCookies(boolean retainCookies) { } /** - * Indicates if bearer tokens negotiated should be re-used by connections - * spawned for getStreams() calls. + * Indicates if bearer tokens negotiated should be re-used by connections spawned for + * getStreams() calls. * * @param retainAuth The flag indicating if auth tokens should be re-used. - * @return this builder instance. + * @return this builder instance. */ public Builder withRetainAuth(boolean retainAuth) { this.retainAuth = retainAuth; @@ -693,7 +693,8 @@ public Builder withRetainAuth(boolean retainAuth) { } /** - * Adds the provided {@code factories} to the list of {@link #middlewareFactories} of this handler. + * Adds the provided {@code factories} to the list of {@link #middlewareFactories} of this + * handler. * * @param factories the factories to add. * @return this instance. @@ -703,7 +704,8 @@ public Builder withMiddlewareFactories(final FlightClientMiddleware.Factory... f } /** - * Adds the provided {@code factories} to the list of {@link #middlewareFactories} of this handler. + * Adds the provided {@code factories} to the list of {@link #middlewareFactories} of this + * handler. * * @param factories the factories to add. * @return this instance. @@ -742,13 +744,16 @@ public Builder withCallOptions(final Collection options) { * @throws SQLException on error. */ public ArrowFlightSqlClientHandler build() throws SQLException { - // Copy middleware so that the build method doesn't change the state of the builder fields itself. - Set buildTimeMiddlewareFactories = new HashSet<>(this.middlewareFactories); + // Copy middleware so that the build method doesn't change the state of the builder fields + // itself. + Set buildTimeMiddlewareFactories = + new HashSet<>(this.middlewareFactories); FlightClient client = null; boolean isUsingUserPasswordAuth = username != null && token == null; try { - // Token should take priority since some apps pass in a username/password even when a token is provided + // Token should take priority since some apps pass in a username/password even when a token + // is provided if (isUsingUserPasswordAuth) { buildTimeMiddlewareFactories.add(authFactory); } @@ -771,13 +776,15 @@ public ArrowFlightSqlClientHandler build() throws SQLException { } else { if (tlsRootCertificatesPath != null) { clientBuilder.trustedCertificates( - ClientAuthenticationUtils.getTlsRootCertificatesStream(tlsRootCertificatesPath)); + ClientAuthenticationUtils.getTlsRootCertificatesStream(tlsRootCertificatesPath)); } else if (useSystemTrustStore) { clientBuilder.trustedCertificates( - ClientAuthenticationUtils.getCertificateInputStreamFromSystem(trustStorePassword)); + ClientAuthenticationUtils.getCertificateInputStreamFromSystem( + trustStorePassword)); } else if (trustStorePath != null) { clientBuilder.trustedCertificates( - ClientAuthenticationUtils.getCertificateStream(trustStorePath, trustStorePassword)); + ClientAuthenticationUtils.getCertificateStream( + trustStorePath, trustStorePassword)); } } @@ -792,7 +799,8 @@ public ArrowFlightSqlClientHandler build() throws SQLException { final ArrayList credentialOptions = new ArrayList<>(); if (isUsingUserPasswordAuth) { // If the authFactory has already been used for a handshake, use the existing token. - // This can occur if the authFactory is being re-used for a new connection spawned for getStream(). + // This can occur if the authFactory is being re-used for a new connection spawned for + // getStream(). if (authFactory.getCredentialCallOption() != null) { credentialOptions.add(authFactory.getCredentialCallOption()); } else { @@ -804,12 +812,16 @@ public ArrowFlightSqlClientHandler build() throws SQLException { } else if (token != null) { credentialOptions.add( ClientAuthenticationUtils.getAuthenticate( - client, new CredentialCallOption(new BearerCredentialWriter(token)), options.toArray( - new CallOption[0]))); + client, + new CredentialCallOption(new BearerCredentialWriter(token)), + options.toArray(new CallOption[0]))); } return ArrowFlightSqlClientHandler.createNewHandler(client, this, credentialOptions); - } catch (final IllegalArgumentException | GeneralSecurityException | IOException | FlightRuntimeException e) { + } catch (final IllegalArgumentException + | GeneralSecurityException + | IOException + | FlightRuntimeException e) { final SQLException originalException = new SQLException(e); if (client != null) { try { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/CloseableEndpointStreamPair.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/CloseableEndpointStreamPair.java index 6c37a5b0c62..85fc6299fa8 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/CloseableEndpointStreamPair.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/CloseableEndpointStreamPair.java @@ -22,9 +22,7 @@ import org.apache.arrow.util.AutoCloseables; import org.apache.arrow.util.Preconditions; -/** - * Represents a connection to a {@link org.apache.arrow.flight.FlightEndpoint}. - */ +/** Represents a connection to a {@link org.apache.arrow.flight.FlightEndpoint}. */ public class CloseableEndpointStreamPair implements AutoCloseable { private final FlightStream stream; diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtils.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtils.java index ffb0048181c..dd84aa1f274 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtils.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtils.java @@ -36,7 +36,6 @@ import java.util.Collection; import java.util.Enumeration; import java.util.List; - import org.apache.arrow.flight.CallOption; import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.auth2.BasicAuthCredentialWriter; @@ -46,9 +45,7 @@ import org.apache.arrow.util.VisibleForTesting; import org.bouncycastle.openssl.jcajce.JcaPEMWriter; -/** - * Utils for {@link FlightClientHandler} authentication. - */ +/** Utils for {@link FlightClientHandler} authentication. */ public final class ClientAuthenticationUtils { private ClientAuthenticationUtils() { @@ -58,14 +55,15 @@ private ClientAuthenticationUtils() { /** * Gets the {@link CredentialCallOption} for the provided authentication info. * - * @param client the client. - * @param credential the credential as CallOptions. - * @param options the {@link CallOption}s to use. + * @param client the client. + * @param credential the credential as CallOptions. + * @param options the {@link CallOption}s to use. * @return the credential call option. */ - public static CredentialCallOption getAuthenticate(final FlightClient client, - final CredentialCallOption credential, - final CallOption... options) { + public static CredentialCallOption getAuthenticate( + final FlightClient client, + final CredentialCallOption credential, + final CallOption... options) { final List theseOptions = new ArrayList<>(); theseOptions.add(credential); @@ -78,27 +76,32 @@ public static CredentialCallOption getAuthenticate(final FlightClient client, /** * Gets the {@link CredentialCallOption} for the provided authentication info. * - * @param client the client. + * @param client the client. * @param username the username. * @param password the password. - * @param factory the {@link ClientIncomingAuthHeaderMiddleware.Factory} to use. - * @param options the {@link CallOption}s to use. + * @param factory the {@link ClientIncomingAuthHeaderMiddleware.Factory} to use. + * @param options the {@link CallOption}s to use. * @return the credential call option. */ - public static CredentialCallOption getAuthenticate(final FlightClient client, - final String username, final String password, - final ClientIncomingAuthHeaderMiddleware.Factory factory, - final CallOption... options) { - - return getAuthenticate(client, + public static CredentialCallOption getAuthenticate( + final FlightClient client, + final String username, + final String password, + final ClientIncomingAuthHeaderMiddleware.Factory factory, + final CallOption... options) { + + return getAuthenticate( + client, new CredentialCallOption(new BasicAuthCredentialWriter(username, password)), - factory, options); + factory, + options); } - private static CredentialCallOption getAuthenticate(final FlightClient client, - final CredentialCallOption token, - final ClientIncomingAuthHeaderMiddleware.Factory factory, - final CallOption... options) { + private static CredentialCallOption getAuthenticate( + final FlightClient client, + final CredentialCallOption token, + final ClientIncomingAuthHeaderMiddleware.Factory factory, + final CallOption... options) { final List theseOptions = new ArrayList<>(); theseOptions.add(token); theseOptions.addAll(Arrays.asList(options)); @@ -148,17 +151,16 @@ public static boolean isMac() { } /** - * It gets the trusted certificate based on the operating system and loads all the certificate into a - * {@link InputStream}. + * It gets the trusted certificate based on the operating system and loads all the certificate + * into a {@link InputStream}. * * @return An input stream with all the certificates. - * - * @throws KeyStoreException if a key store could not be loaded. - * @throws CertificateException if a certificate could not be found. - * @throws IOException if it fails reading the file. + * @throws KeyStoreException if a key store could not be loaded. + * @throws CertificateException if a certificate could not be found. + * @throws IOException if it fails reading the file. */ - public static InputStream getCertificateInputStreamFromSystem(String password) throws KeyStoreException, - CertificateException, IOException, NoSuchAlgorithmException { + public static InputStream getCertificateInputStreamFromSystem(String password) + throws KeyStoreException, CertificateException, IOException, NoSuchAlgorithmException { List keyStoreList = new ArrayList<>(); if (isWindows()) { @@ -201,36 +203,33 @@ static void getCertificatesInputStream(KeyStore keyStore, JcaPEMWriter pemWriter static InputStream getCertificatesInputStream(Collection keyStores) throws IOException, KeyStoreException { try (final StringWriter writer = new StringWriter(); - final JcaPEMWriter pemWriter = new JcaPEMWriter(writer)) { + final JcaPEMWriter pemWriter = new JcaPEMWriter(writer)) { for (KeyStore keyStore : keyStores) { getCertificatesInputStream(keyStore, pemWriter); } - return new ByteArrayInputStream( - writer.toString().getBytes(StandardCharsets.UTF_8)); + return new ByteArrayInputStream(writer.toString().getBytes(StandardCharsets.UTF_8)); } } /** - * Generates an {@link InputStream} that contains certificates for a private - * key. + * Generates an {@link InputStream} that contains certificates for a private key. * * @param keyStorePath The path of the KeyStore. * @param keyStorePass The password of the KeyStore. * @return a new {code InputStream} containing the certificates. * @throws GeneralSecurityException on error. - * @throws IOException on error. + * @throws IOException on error. */ - public static InputStream getCertificateStream(final String keyStorePath, - final String keyStorePass) + public static InputStream getCertificateStream( + final String keyStorePath, final String keyStorePass) throws GeneralSecurityException, IOException { Preconditions.checkNotNull(keyStorePath, "KeyStore path cannot be null!"); Preconditions.checkNotNull(keyStorePass, "KeyStorePass cannot be null!"); final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); - try (final InputStream keyStoreStream = Files - .newInputStream(Paths.get(keyStorePath))) { + try (final InputStream keyStoreStream = Files.newInputStream(Paths.get(keyStorePath))) { keyStore.load(keyStoreStream, keyStorePass.toCharArray()); } @@ -238,54 +237,51 @@ public static InputStream getCertificateStream(final String keyStorePath, } /** - * Generates an {@link InputStream} that contains certificates for path-based - * TLS Root Certificates. + * Generates an {@link InputStream} that contains certificates for path-based TLS Root + * Certificates. * * @param tlsRootsCertificatesPath The path of the TLS Root Certificates. * @return a new {code InputStream} containing the certificates. * @throws GeneralSecurityException on error. - * @throws IOException on error. + * @throws IOException on error. */ public static InputStream getTlsRootCertificatesStream(final String tlsRootsCertificatesPath) - throws GeneralSecurityException, IOException { - Preconditions.checkNotNull(tlsRootsCertificatesPath, "TLS Root certificates path cannot be null!"); + throws GeneralSecurityException, IOException { + Preconditions.checkNotNull( + tlsRootsCertificatesPath, "TLS Root certificates path cannot be null!"); - return Files - .newInputStream(Paths.get(tlsRootsCertificatesPath)); + return Files.newInputStream(Paths.get(tlsRootsCertificatesPath)); } /** - * Generates an {@link InputStream} that contains certificates for a path-based - * mTLS Client Certificate. + * Generates an {@link InputStream} that contains certificates for a path-based mTLS Client + * Certificate. * * @param clientCertificatePath The path of the mTLS Client Certificate. * @return a new {code InputStream} containing the certificates. * @throws GeneralSecurityException on error. - * @throws IOException on error. + * @throws IOException on error. */ public static InputStream getClientCertificateStream(final String clientCertificatePath) throws GeneralSecurityException, IOException { Preconditions.checkNotNull(clientCertificatePath, "Client certificate path cannot be null!"); - return Files - .newInputStream(Paths.get(clientCertificatePath)); + return Files.newInputStream(Paths.get(clientCertificatePath)); } /** - * Generates an {@link InputStream} that contains certificates for a path-based - * mTLS Client Key. + * Generates an {@link InputStream} that contains certificates for a path-based mTLS Client Key. * * @param clientKeyPath The path of the mTLS Client Key. * @return a new {code InputStream} containing the certificates. * @throws GeneralSecurityException on error. - * @throws IOException on error. + * @throws IOException on error. */ public static InputStream getClientKeyStream(final String clientKeyPath) - throws GeneralSecurityException, IOException { + throws GeneralSecurityException, IOException { Preconditions.checkNotNull(clientKeyPath, "Client key path cannot be null!"); - return Files - .newInputStream(Paths.get(clientKeyPath)); + return Files.newInputStream(Paths.get(clientKeyPath)); } private static InputStream getSingleCertificateInputStream(KeyStore keyStore) @@ -302,16 +298,14 @@ private static InputStream getSingleCertificateInputStream(KeyStore keyStore) throw new CertificateException("Keystore did not have a certificate."); } - private static InputStream toInputStream(final Certificate certificate) - throws IOException { + private static InputStream toInputStream(final Certificate certificate) throws IOException { try (final StringWriter writer = new StringWriter(); - final JcaPEMWriter pemWriter = new JcaPEMWriter(writer)) { + final JcaPEMWriter pemWriter = new JcaPEMWriter(writer)) { pemWriter.writeObject(certificate); pemWriter.flush(); - return new ByteArrayInputStream( - writer.toString().getBytes(StandardCharsets.UTF_8)); + return new ByteArrayInputStream(writer.toString().getBytes(StandardCharsets.UTF_8)); } } } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/BaseAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/BaseAvaticaParameterConverter.java index f5cf8358b7a..8b5b1460a38 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/BaseAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/BaseAvaticaParameterConverter.java @@ -34,7 +34,8 @@ protected AvaticaParameter createParameter(Field field, boolean signed) { final ArrowType arrowType = field.getType(); final String typeName = arrowType.toString(); final int precision = 0; // Would have to know about the actual number - final int scale = 0; // According to https://www.postgresql.org/docs/current/datatype-numeric.html + final int scale = + 0; // According to https://www.postgresql.org/docs/current/datatype-numeric.html final int jdbcType = SqlTypes.getSqlTypeIdFromArrowType(arrowType); final String className = SqlType.valueOf(jdbcType).clazz.getCanonicalName(); return new AvaticaParameter(signed, precision, scale, jdbcType, typeName, className, name); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/BinaryAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/BinaryAvaticaParameterConverter.java index d244848955e..0da0d6f69d9 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/BinaryAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/BinaryAvaticaParameterConverter.java @@ -24,13 +24,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for Binary Arrow types. - */ +/** AvaticaParameterConverter for Binary Arrow types. */ public class BinaryAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public BinaryAvaticaParameterConverter(ArrowType.Binary type) { - } + public BinaryAvaticaParameterConverter(ArrowType.Binary type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/BoolAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/BoolAvaticaParameterConverter.java index 6725154d03c..7df7f7e68f6 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/BoolAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/BoolAvaticaParameterConverter.java @@ -24,13 +24,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for Bool Arrow types. - */ +/** AvaticaParameterConverter for Bool Arrow types. */ public class BoolAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public BoolAvaticaParameterConverter(ArrowType.Bool type) { - } + public BoolAvaticaParameterConverter(ArrowType.Bool type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/DateAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/DateAvaticaParameterConverter.java index 0da1dabe437..d51f66b8efa 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/DateAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/DateAvaticaParameterConverter.java @@ -25,13 +25,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for Date Arrow types. - */ +/** AvaticaParameterConverter for Date Arrow types. */ public class DateAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public DateAvaticaParameterConverter(ArrowType.Date type) { - } + public DateAvaticaParameterConverter(ArrowType.Date type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/DecimalAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/DecimalAvaticaParameterConverter.java index fad43e2e06a..7e10fe13b67 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/DecimalAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/DecimalAvaticaParameterConverter.java @@ -18,7 +18,6 @@ package org.apache.arrow.driver.jdbc.converter.impl; import java.math.BigDecimal; - import org.apache.arrow.vector.DecimalVector; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.types.pojo.ArrowType; @@ -26,13 +25,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for Decimal Arrow types. - */ +/** AvaticaParameterConverter for Decimal Arrow types. */ public class DecimalAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public DecimalAvaticaParameterConverter(ArrowType.Decimal type) { - } + public DecimalAvaticaParameterConverter(ArrowType.Decimal type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/DurationAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/DurationAvaticaParameterConverter.java index 89f2fc1d5c1..0b42899e1ba 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/DurationAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/DurationAvaticaParameterConverter.java @@ -23,13 +23,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for Duration Arrow types. - */ +/** AvaticaParameterConverter for Duration Arrow types. */ public class DurationAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public DurationAvaticaParameterConverter(ArrowType.Duration type) { - } + public DurationAvaticaParameterConverter(ArrowType.Duration type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/FixedSizeBinaryAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/FixedSizeBinaryAvaticaParameterConverter.java index a90434f695a..08e6804d4a8 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/FixedSizeBinaryAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/FixedSizeBinaryAvaticaParameterConverter.java @@ -24,13 +24,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for FixedSizeBinary Arrow types. - */ +/** AvaticaParameterConverter for FixedSizeBinary Arrow types. */ public class FixedSizeBinaryAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public FixedSizeBinaryAvaticaParameterConverter(ArrowType.FixedSizeBinary type) { - } + public FixedSizeBinaryAvaticaParameterConverter(ArrowType.FixedSizeBinary type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/FixedSizeListAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/FixedSizeListAvaticaParameterConverter.java index 1525bcaaf51..ca774d68551 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/FixedSizeListAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/FixedSizeListAvaticaParameterConverter.java @@ -18,7 +18,6 @@ package org.apache.arrow.driver.jdbc.converter.impl; import java.util.List; - import org.apache.arrow.driver.jdbc.utils.AvaticaParameterBinder; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.complex.FixedSizeListVector; @@ -27,13 +26,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for FixedSizeList Arrow types. - */ +/** AvaticaParameterConverter for FixedSizeList Arrow types. */ public class FixedSizeListAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public FixedSizeListAvaticaParameterConverter(ArrowType.FixedSizeList type) { - } + public FixedSizeListAvaticaParameterConverter(ArrowType.FixedSizeList type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { @@ -47,9 +43,11 @@ public boolean bindParameter(FieldVector vector, TypedValue typedValue, int inde if (arraySize != maxArraySize) { if (!childVector.getField().isNullable()) { - throw new UnsupportedOperationException("Each array must contain " + maxArraySize + " elements"); + throw new UnsupportedOperationException( + "Each array must contain " + maxArraySize + " elements"); } else if (arraySize > maxArraySize) { - throw new UnsupportedOperationException("Each array must contain at most " + maxArraySize + " elements"); + throw new UnsupportedOperationException( + "Each array must contain at most " + maxArraySize + " elements"); } } @@ -64,9 +62,12 @@ public boolean bindParameter(FieldVector vector, TypedValue typedValue, int inde throw new UnsupportedOperationException("Can't set null on non-nullable child list"); } } else { - childVector.getField().getType().accept( + childVector + .getField() + .getType() + .accept( new AvaticaParameterBinder.BinderVisitor( - childVector, TypedValue.ofSerial(typedValue.componentType, val), childIndex)); + childVector, TypedValue.ofSerial(typedValue.componentType, val), childIndex)); } } listVector.setValueCount(index + 1); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/FloatingPointAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/FloatingPointAvaticaParameterConverter.java index 9f305a2b6f2..2113c3058d0 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/FloatingPointAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/FloatingPointAvaticaParameterConverter.java @@ -25,13 +25,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for FloatingPoint Arrow types. - */ +/** AvaticaParameterConverter for FloatingPoint Arrow types. */ public class FloatingPointAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public FloatingPointAvaticaParameterConverter(ArrowType.FloatingPoint type) { - } + public FloatingPointAvaticaParameterConverter(ArrowType.FloatingPoint type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/IntAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/IntAvaticaParameterConverter.java index 6684e8d32c9..96b8aa87265 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/IntAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/IntAvaticaParameterConverter.java @@ -31,9 +31,7 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for Int Arrow types. - */ +/** AvaticaParameterConverter for Int Arrow types. */ public class IntAvaticaParameterConverter extends BaseAvaticaParameterConverter { private final ArrowType.Int type; diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/IntervalAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/IntervalAvaticaParameterConverter.java index 724275d5109..a9e2db955e4 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/IntervalAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/IntervalAvaticaParameterConverter.java @@ -23,13 +23,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for Interval Arrow types. - */ +/** AvaticaParameterConverter for Interval Arrow types. */ public class IntervalAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public IntervalAvaticaParameterConverter(ArrowType.Interval type) { - } + public IntervalAvaticaParameterConverter(ArrowType.Interval type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/LargeBinaryAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/LargeBinaryAvaticaParameterConverter.java index 133ec2072d5..8648aa6e0ca 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/LargeBinaryAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/LargeBinaryAvaticaParameterConverter.java @@ -24,13 +24,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for LargeBinary Arrow types. - */ +/** AvaticaParameterConverter for LargeBinary Arrow types. */ public class LargeBinaryAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public LargeBinaryAvaticaParameterConverter(ArrowType.LargeBinary type) { - } + public LargeBinaryAvaticaParameterConverter(ArrowType.LargeBinary type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/LargeListAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/LargeListAvaticaParameterConverter.java index a20747693e3..bb3ab674483 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/LargeListAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/LargeListAvaticaParameterConverter.java @@ -18,7 +18,6 @@ package org.apache.arrow.driver.jdbc.converter.impl; import java.util.List; - import org.apache.arrow.driver.jdbc.utils.AvaticaParameterBinder; import org.apache.arrow.memory.util.LargeMemoryUtil; import org.apache.arrow.vector.FieldVector; @@ -28,13 +27,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for LargeList Arrow types. - */ +/** AvaticaParameterConverter for LargeList Arrow types. */ public class LargeListAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public LargeListAvaticaParameterConverter(ArrowType.LargeList type) { - } + public LargeListAvaticaParameterConverter(ArrowType.LargeList type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { @@ -55,9 +51,12 @@ public boolean bindParameter(FieldVector vector, TypedValue typedValue, int inde throw new UnsupportedOperationException("Can't set null on non-nullable child list"); } } else { - childVector.getField().getType().accept( + childVector + .getField() + .getType() + .accept( new AvaticaParameterBinder.BinderVisitor( - childVector, TypedValue.ofSerial(typedValue.componentType, val), childIndex)); + childVector, TypedValue.ofSerial(typedValue.componentType, val), childIndex)); } } listVector.endValue(index, values.size()); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/LargeUtf8AvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/LargeUtf8AvaticaParameterConverter.java index d412ab007ac..e4050c16b97 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/LargeUtf8AvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/LargeUtf8AvaticaParameterConverter.java @@ -25,13 +25,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for LargeUtf8 Arrow types. - */ +/** AvaticaParameterConverter for LargeUtf8 Arrow types. */ public class LargeUtf8AvaticaParameterConverter extends BaseAvaticaParameterConverter { - public LargeUtf8AvaticaParameterConverter(ArrowType.LargeUtf8 type) { - } + public LargeUtf8AvaticaParameterConverter(ArrowType.LargeUtf8 type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/ListAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/ListAvaticaParameterConverter.java index f6cb9f3be2a..6645fdcfd3c 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/ListAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/ListAvaticaParameterConverter.java @@ -18,7 +18,6 @@ package org.apache.arrow.driver.jdbc.converter.impl; import java.util.List; - import org.apache.arrow.driver.jdbc.utils.AvaticaParameterBinder; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.complex.ListVector; @@ -27,13 +26,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for List Arrow types. - */ +/** AvaticaParameterConverter for List Arrow types. */ public class ListAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public ListAvaticaParameterConverter(ArrowType.List type) { - } + public ListAvaticaParameterConverter(ArrowType.List type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { @@ -54,9 +50,12 @@ public boolean bindParameter(FieldVector vector, TypedValue typedValue, int inde throw new UnsupportedOperationException("Can't set null on non-nullable child list"); } } else { - childVector.getField().getType().accept( + childVector + .getField() + .getType() + .accept( new AvaticaParameterBinder.BinderVisitor( - childVector, TypedValue.ofSerial(typedValue.componentType, val), childIndex)); + childVector, TypedValue.ofSerial(typedValue.componentType, val), childIndex)); } } listVector.endValue(index, values.size()); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/MapAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/MapAvaticaParameterConverter.java index feac3794d22..c0e1d984606 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/MapAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/MapAvaticaParameterConverter.java @@ -23,13 +23,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for Map Arrow types. - */ +/** AvaticaParameterConverter for Map Arrow types. */ public class MapAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public MapAvaticaParameterConverter(ArrowType.Map type) { - } + public MapAvaticaParameterConverter(ArrowType.Map type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/NullAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/NullAvaticaParameterConverter.java index e2c184fb11a..f41ac055752 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/NullAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/NullAvaticaParameterConverter.java @@ -24,19 +24,18 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for Null Arrow types. - */ +/** AvaticaParameterConverter for Null Arrow types. */ public class NullAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public NullAvaticaParameterConverter(ArrowType.Null type) { - } + public NullAvaticaParameterConverter(ArrowType.Null type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { Object value = typedValue.toLocal(); if (vector instanceof NullVector) { - if (value != null) { throw new RuntimeException("Can't set non-null value on NullVector"); } + if (value != null) { + throw new RuntimeException("Can't set non-null value on NullVector"); + } vector.setNull(index); } return false; diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/StructAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/StructAvaticaParameterConverter.java index 5dfe923cb51..8bcc365d493 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/StructAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/StructAvaticaParameterConverter.java @@ -23,13 +23,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for Struct Arrow types. - */ +/** AvaticaParameterConverter for Struct Arrow types. */ public class StructAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public StructAvaticaParameterConverter(ArrowType.Struct type) { - } + public StructAvaticaParameterConverter(ArrowType.Struct type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimeAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimeAvaticaParameterConverter.java index c6b79537fd4..80887d2f897 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimeAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimeAvaticaParameterConverter.java @@ -27,13 +27,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for Time Arrow types. - */ +/** AvaticaParameterConverter for Time Arrow types. */ public class TimeAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public TimeAvaticaParameterConverter(ArrowType.Time type) { - } + public TimeAvaticaParameterConverter(ArrowType.Time type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverter.java index eb3316a1bdb..7e0edb27023 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverter.java @@ -31,13 +31,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for Timestamp Arrow types. - */ +/** AvaticaParameterConverter for Timestamp Arrow types. */ public class TimestampAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public TimestampAvaticaParameterConverter(ArrowType.Timestamp type) { - } + public TimestampAvaticaParameterConverter(ArrowType.Timestamp type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/UnionAvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/UnionAvaticaParameterConverter.java index 6b171e68557..eefd42216d7 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/UnionAvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/UnionAvaticaParameterConverter.java @@ -23,13 +23,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for Union Arrow types. - */ +/** AvaticaParameterConverter for Union Arrow types. */ public class UnionAvaticaParameterConverter extends BaseAvaticaParameterConverter { - public UnionAvaticaParameterConverter(ArrowType.Union type) { - } + public UnionAvaticaParameterConverter(ArrowType.Union type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/Utf8AvaticaParameterConverter.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/Utf8AvaticaParameterConverter.java index 9223e5361d2..4ef67ea5e89 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/Utf8AvaticaParameterConverter.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/Utf8AvaticaParameterConverter.java @@ -25,13 +25,10 @@ import org.apache.calcite.avatica.AvaticaParameter; import org.apache.calcite.avatica.remote.TypedValue; -/** - * AvaticaParameterConverter for Utf8 Arrow types. - */ +/** AvaticaParameterConverter for Utf8 Arrow types. */ public class Utf8AvaticaParameterConverter extends BaseAvaticaParameterConverter { - public Utf8AvaticaParameterConverter(ArrowType.Utf8 type) { - } + public Utf8AvaticaParameterConverter(ArrowType.Utf8 type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImpl.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImpl.java index e95cf00bc7a..aca7da0e144 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImpl.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImpl.java @@ -22,7 +22,6 @@ import java.util.Map; import java.util.Objects; import java.util.Properties; - import org.apache.arrow.driver.jdbc.ArrowFlightConnection; import org.apache.arrow.flight.CallHeaders; import org.apache.arrow.flight.CallOption; @@ -33,10 +32,7 @@ import org.apache.calcite.avatica.ConnectionConfigImpl; import org.apache.calcite.avatica.ConnectionProperty; - -/** - * A {@link ConnectionConfig} for the {@link ArrowFlightConnection}. - */ +/** A {@link ConnectionConfig} for the {@link ArrowFlightConnection}. */ public final class ArrowFlightConnectionConfigImpl extends ConnectionConfigImpl { public ArrowFlightConnectionConfigImpl(final Properties properties) { super(properties); @@ -78,7 +74,6 @@ public String getPassword() { return ArrowFlightConnectionProperty.PASSWORD.getString(properties); } - public String getToken() { return ArrowFlightConnectionProperty.TOKEN.getString(properties); } @@ -145,16 +140,16 @@ public int threadPoolSize() { } /** - * Indicates if sub-connections created for stream retrieval - * should reuse cookies from the main connection. + * Indicates if sub-connections created for stream retrieval should reuse cookies from the main + * connection. */ public boolean retainCookies() { return ArrowFlightConnectionProperty.RETAIN_COOKIES.getBoolean(properties); } /** - * Indicates if sub-connections created for stream retrieval - * should reuse bearer tokens created from the main connection. + * Indicates if sub-connections created for stream retrieval should reuse bearer tokens created + * from the main connection. */ public boolean retainAuth() { return ArrowFlightConnectionProperty.RETAIN_AUTH.getBoolean(properties); @@ -184,16 +179,15 @@ public Map getHeaderAttributes() { (key, val) -> { // For built-in properties before adding new headers if (Arrays.stream(builtInProperties) - .noneMatch(builtInProperty -> builtInProperty.camelName.equalsIgnoreCase(key.toString()))) { + .noneMatch( + builtInProperty -> builtInProperty.camelName.equalsIgnoreCase(key.toString()))) { headers.put(key.toString(), val.toString()); } }); return headers; } - /** - * Custom {@link ConnectionProperty} for the {@link ArrowFlightConnectionConfigImpl}. - */ + /** Custom {@link ConnectionProperty} for the {@link ArrowFlightConnectionConfigImpl}. */ public enum ArrowFlightConnectionProperty implements ConnectionProperty { HOST("host", null, Type.STRING, true), PORT("port", null, Type.NUMBER, true), @@ -217,8 +211,11 @@ public enum ArrowFlightConnectionProperty implements ConnectionProperty { private final Type type; private final boolean required; - ArrowFlightConnectionProperty(final String camelName, final Object defaultValue, - final Type type, final boolean required) { + ArrowFlightConnectionProperty( + final String camelName, + final Object defaultValue, + final Type type, + final boolean required) { this.camelName = Preconditions.checkNotNull(camelName); this.defaultValue = defaultValue; this.type = Preconditions.checkNotNull(type); @@ -239,7 +236,8 @@ public Object get(final Properties properties) { } if (required) { if (value == null) { - throw new IllegalStateException(String.format("Required property not provided: <%s>.", this)); + throw new IllegalStateException( + String.format("Required property not provided: <%s>.", this)); } return value; } else { diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/AvaticaParameterBinder.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/AvaticaParameterBinder.java index b2bd8e745ec..313be75566f 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/AvaticaParameterBinder.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/AvaticaParameterBinder.java @@ -18,7 +18,6 @@ package org.apache.arrow.driver.jdbc.utils; import java.util.List; - import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler.PreparedStatement; import org.apache.arrow.driver.jdbc.converter.impl.BinaryAvaticaParameterConverter; import org.apache.arrow.driver.jdbc.converter.impl.BoolAvaticaParameterConverter; @@ -48,22 +47,25 @@ import org.apache.calcite.avatica.remote.TypedValue; /** - * Convert Avatica PreparedStatement parameters from a list of TypedValue to Arrow and bind them to the - * VectorSchemaRoot representing the PreparedStatement parameters. - *

- * NOTE: Make sure to close the parameters VectorSchemaRoot once we're done with them. + * Convert Avatica PreparedStatement parameters from a list of TypedValue to Arrow and bind them to + * the VectorSchemaRoot representing the PreparedStatement parameters. + * + *

NOTE: Make sure to close the parameters VectorSchemaRoot once we're done with them. */ public class AvaticaParameterBinder { private final PreparedStatement preparedStatement; private final VectorSchemaRoot parameters; - public AvaticaParameterBinder(PreparedStatement preparedStatement, BufferAllocator bufferAllocator) { - this.parameters = VectorSchemaRoot.create(preparedStatement.getParameterSchema(), bufferAllocator); + public AvaticaParameterBinder( + PreparedStatement preparedStatement, BufferAllocator bufferAllocator) { + this.parameters = + VectorSchemaRoot.create(preparedStatement.getParameterSchema(), bufferAllocator); this.preparedStatement = preparedStatement; } /** * Bind the given Avatica values to the prepared statement. + * * @param typedValues The parameter values. */ public void bind(List typedValues) { @@ -72,15 +74,16 @@ public void bind(List typedValues) { /** * Bind the given Avatica values to the prepared statement at the given index. + * * @param typedValues The parameter values. * @param index index for parameter. */ public void bind(List typedValues, int index) { if (preparedStatement.getParameterSchema().getFields().size() != typedValues.size()) { throw new IllegalStateException( - String.format("Prepared statement has %s parameters, but only received %s", - preparedStatement.getParameterSchema().getFields().size(), - typedValues.size())); + String.format( + "Prepared statement has %s parameters, but only received %s", + preparedStatement.getParameterSchema().getFields().size(), typedValues.size())); } for (int i = 0; i < typedValues.size(); i++) { @@ -96,9 +99,9 @@ public void bind(List typedValues, int index) { /** * Bind a TypedValue to the given index on the FieldVector. * - * @param vector FieldVector to bind to. + * @param vector FieldVector to bind to. * @param typedValue TypedValue to bind to the vector. - * @param index Vector index to bind the value at. + * @param index Vector index to bind the value at. */ private void bind(FieldVector vector, TypedValue typedValue, int index) { try { @@ -108,19 +111,24 @@ private void bind(FieldVector vector, TypedValue typedValue, int index) { } else { throw new UnsupportedOperationException("Can't set null on non-nullable parameter"); } - } else if (!vector.getField().getType().accept(new BinderVisitor(vector, typedValue, index))) { + } else if (!vector + .getField() + .getType() + .accept(new BinderVisitor(vector, typedValue, index))) { throw new UnsupportedOperationException( - String.format("Binding to vector type %s is not yet supported", vector.getClass())); + String.format("Binding to vector type %s is not yet supported", vector.getClass())); } } catch (ClassCastException e) { throw new UnsupportedOperationException( - String.format("Binding value of type %s is not yet supported for expected Arrow type %s", - typedValue.type, vector.getField().getType())); + String.format( + "Binding value of type %s is not yet supported for expected Arrow type %s", + typedValue.type, vector.getField().getType())); } } /** - * ArrowTypeVisitor that binds Avatica TypedValues to the given FieldVector at the specified index. + * ArrowTypeVisitor that binds Avatica TypedValues to the given FieldVector at the specified + * index. */ public static class BinderVisitor implements ArrowType.ArrowTypeVisitor { private final FieldVector vector; @@ -162,7 +170,8 @@ public Boolean visit(ArrowType.LargeList type) { @Override public Boolean visit(ArrowType.FixedSizeList type) { - return new FixedSizeListAvaticaParameterConverter(type).bindParameter(vector, typedValue, index); + return new FixedSizeListAvaticaParameterConverter(type) + .bindParameter(vector, typedValue, index); } @Override @@ -182,7 +191,8 @@ public Boolean visit(ArrowType.Int type) { @Override public Boolean visit(ArrowType.FloatingPoint type) { - return new FloatingPointAvaticaParameterConverter(type).bindParameter(vector, typedValue, index); + return new FloatingPointAvaticaParameterConverter(type) + .bindParameter(vector, typedValue, index); } @Override @@ -202,12 +212,14 @@ public Boolean visit(ArrowType.Binary type) { @Override public Boolean visit(ArrowType.LargeBinary type) { - return new LargeBinaryAvaticaParameterConverter(type).bindParameter(vector, typedValue, index); + return new LargeBinaryAvaticaParameterConverter(type) + .bindParameter(vector, typedValue, index); } @Override public Boolean visit(ArrowType.FixedSizeBinary type) { - return new FixedSizeBinaryAvaticaParameterConverter(type).bindParameter(vector, typedValue, index); + return new FixedSizeBinaryAvaticaParameterConverter(type) + .bindParameter(vector, typedValue, index); } @Override @@ -245,5 +257,4 @@ public Boolean visit(ArrowType.Duration type) { return new DurationAvaticaParameterConverter(type).bindParameter(vector, typedValue, index); } } - } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ConnectionWrapper.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ConnectionWrapper.java index c28071490ca..5f023f85c36 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ConnectionWrapper.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ConnectionWrapper.java @@ -37,7 +37,6 @@ import java.util.Map; import java.util.Properties; import java.util.concurrent.Executor; - import org.apache.arrow.driver.jdbc.ArrowFlightJdbcPooledConnection; /** @@ -162,15 +161,15 @@ public Statement createStatement(final int resultSetTypeId, final int resultSetC } @Override - public PreparedStatement prepareStatement(final String sqlQuery, final int resultSetTypeId, - final int resultSetConcurrencyId) + public PreparedStatement prepareStatement( + final String sqlQuery, final int resultSetTypeId, final int resultSetConcurrencyId) throws SQLException { return realConnection.prepareStatement(sqlQuery, resultSetTypeId, resultSetConcurrencyId); } @Override - public CallableStatement prepareCall(final String query, final int resultSetTypeId, - final int resultSetConcurrencyId) + public CallableStatement prepareCall( + final String query, final int resultSetTypeId, final int resultSetConcurrencyId) throws SQLException { return realConnection.prepareCall(query, resultSetTypeId, resultSetConcurrencyId); } @@ -216,29 +215,33 @@ public void releaseSavepoint(final Savepoint savepoint) throws SQLException { } @Override - public Statement createStatement(final int resultSetType, - final int resultSetConcurrency, - final int resultSetHoldability) throws SQLException { - return realConnection.createStatement(resultSetType, resultSetConcurrency, - resultSetHoldability); + public Statement createStatement( + final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) + throws SQLException { + return realConnection.createStatement( + resultSetType, resultSetConcurrency, resultSetHoldability); } @Override - public PreparedStatement prepareStatement(final String sqlQuery, - final int resultSetType, - final int resultSetConcurrency, - final int resultSetHoldability) throws SQLException { - return realConnection.prepareStatement(sqlQuery, resultSetType, resultSetConcurrency, - resultSetHoldability); + public PreparedStatement prepareStatement( + final String sqlQuery, + final int resultSetType, + final int resultSetConcurrency, + final int resultSetHoldability) + throws SQLException { + return realConnection.prepareStatement( + sqlQuery, resultSetType, resultSetConcurrency, resultSetHoldability); } @Override - public CallableStatement prepareCall(final String sqlQuery, - final int resultSetType, - final int resultSetConcurrency, - final int resultSetHoldability) throws SQLException { - return realConnection.prepareCall(sqlQuery, resultSetType, resultSetConcurrency, - resultSetHoldability); + public CallableStatement prepareCall( + final String sqlQuery, + final int resultSetType, + final int resultSetConcurrency, + final int resultSetHoldability) + throws SQLException { + return realConnection.prepareCall( + sqlQuery, resultSetType, resultSetConcurrency, resultSetHoldability); } @Override @@ -311,8 +314,7 @@ public Array createArrayOf(final String typeName, final Object[] elements) throw } @Override - public Struct createStruct(final String typeName, final Object[] attributes) - throws SQLException { + public Struct createStruct(final String typeName, final Object[] attributes) throws SQLException { return realConnection.createStruct(typeName, attributes); } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ConvertUtils.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ConvertUtils.java index 843fe0cb89d..8f8b50396e2 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ConvertUtils.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ConvertUtils.java @@ -21,7 +21,6 @@ import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; - import org.apache.arrow.driver.jdbc.converter.impl.BinaryAvaticaParameterConverter; import org.apache.arrow.driver.jdbc.converter.impl.BoolAvaticaParameterConverter; import org.apache.arrow.driver.jdbc.converter.impl.DateAvaticaParameterConverter; @@ -50,13 +49,10 @@ import org.apache.calcite.avatica.ColumnMetaData; import org.apache.calcite.avatica.proto.Common; -/** - * Convert objects between Arrow and Avatica. - */ +/** Convert objects between Arrow and Avatica. */ public final class ConvertUtils { - private ConvertUtils() { - } + private ConvertUtils() {} /** * Convert Fields To Column MetaData List functions. @@ -64,36 +60,42 @@ private ConvertUtils() { * @param fields list of {@link Field}. * @return list of {@link ColumnMetaData}. */ - public static List convertArrowFieldsToColumnMetaDataList(final List fields) { - return Stream.iterate(0, Math::incrementExact).limit(fields.size()) - .map(index -> { - final Field field = fields.get(index); - final ArrowType fieldType = field.getType(); - - final Common.ColumnMetaData.Builder builder = Common.ColumnMetaData.newBuilder() - .setOrdinal(index) - .setColumnName(field.getName()) - .setLabel(field.getName()); - - setOnColumnMetaDataBuilder(builder, field.getMetadata()); - - builder.setType(Common.AvaticaType.newBuilder() - .setId(SqlTypes.getSqlTypeIdFromArrowType(fieldType)) - .setName(SqlTypes.getSqlTypeNameFromArrowType(fieldType)) - .build()); - - return ColumnMetaData.fromProto(builder.build()); - }).collect(Collectors.toList()); + public static List convertArrowFieldsToColumnMetaDataList( + final List fields) { + return Stream.iterate(0, Math::incrementExact) + .limit(fields.size()) + .map( + index -> { + final Field field = fields.get(index); + final ArrowType fieldType = field.getType(); + + final Common.ColumnMetaData.Builder builder = + Common.ColumnMetaData.newBuilder() + .setOrdinal(index) + .setColumnName(field.getName()) + .setLabel(field.getName()); + + setOnColumnMetaDataBuilder(builder, field.getMetadata()); + + builder.setType( + Common.AvaticaType.newBuilder() + .setId(SqlTypes.getSqlTypeIdFromArrowType(fieldType)) + .setName(SqlTypes.getSqlTypeNameFromArrowType(fieldType)) + .build()); + + return ColumnMetaData.fromProto(builder.build()); + }) + .collect(Collectors.toList()); } /** * Set on Column MetaData Builder. * - * @param builder {@link Common.ColumnMetaData.Builder} + * @param builder {@link Common.ColumnMetaData.Builder} * @param metadataMap {@link Map} */ - public static void setOnColumnMetaDataBuilder(final Common.ColumnMetaData.Builder builder, - final Map metadataMap) { + public static void setOnColumnMetaDataBuilder( + final Common.ColumnMetaData.Builder builder, final Map metadataMap) { final FlightSqlColumnMetadata columnMetadata = new FlightSqlColumnMetadata(metadataMap); final String catalogName = columnMetadata.getCatalogName(); if (catalogName != null) { @@ -141,10 +143,11 @@ public static void setOnColumnMetaDataBuilder(final Common.ColumnMetaData.Builde * @param fields list of {@link Field}. * @return list of {@link AvaticaParameter}. */ - public static List convertArrowFieldsToAvaticaParameters(final List fields) { + public static List convertArrowFieldsToAvaticaParameters( + final List fields) { return fields.stream() - .map(field -> field.getType().accept(new ConverterVisitor(field))) - .collect(Collectors.toList()); + .map(field -> field.getType().accept(new ConverterVisitor(field))) + .collect(Collectors.toList()); } private static class ConverterVisitor implements ArrowType.ArrowTypeVisitor { @@ -167,25 +170,21 @@ public AvaticaParameter visit(ArrowType.Struct type) { @Override public AvaticaParameter visit(ArrowType.List type) { return new ListAvaticaParameterConverter(type).createParameter(field); - } @Override public AvaticaParameter visit(ArrowType.LargeList type) { return new LargeListAvaticaParameterConverter(type).createParameter(field); - } @Override public AvaticaParameter visit(ArrowType.FixedSizeList type) { return new FixedSizeListAvaticaParameterConverter(type).createParameter(field); - } @Override public AvaticaParameter visit(ArrowType.Union type) { return new UnionAvaticaParameterConverter(type).createParameter(field); - } @Override @@ -263,5 +262,4 @@ public AvaticaParameter visit(ArrowType.Duration type) { return new DurationAvaticaParameterConverter(type).createParameter(field); } } - } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/DateTimeUtils.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/DateTimeUtils.java index dd94a09256d..fe2f6ca788b 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/DateTimeUtils.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/DateTimeUtils.java @@ -27,17 +27,13 @@ import java.util.TimeZone; import java.util.concurrent.TimeUnit; -/** - * Datetime utility functions. - */ +/** Datetime utility functions. */ public class DateTimeUtils { private DateTimeUtils() { // Prevent instantiation. } - /** - * Subtracts given Calendar's TimeZone offset from epoch milliseconds. - */ + /** Subtracts given Calendar's TimeZone offset from epoch milliseconds. */ public static long applyCalendarOffset(long milliseconds, Calendar calendar) { if (calendar == null) { calendar = Calendar.getInstance(); @@ -53,7 +49,6 @@ public static long applyCalendarOffset(long milliseconds, Calendar calendar) { return milliseconds; } - /** * Converts Epoch millis to a {@link Timestamp} object. * @@ -70,7 +65,6 @@ public static Timestamp getTimestampValue(long millisWithCalendar) { return Timestamp.valueOf( LocalDateTime.of( LocalDate.ofEpochDay(millisWithCalendar / MILLIS_PER_DAY), - LocalTime.ofNanoOfDay(TimeUnit.MILLISECONDS.toNanos(milliseconds % MILLIS_PER_DAY))) - ); + LocalTime.ofNanoOfDay(TimeUnit.MILLISECONDS.toNanos(milliseconds % MILLIS_PER_DAY)))); } } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/FlightEndpointDataQueue.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/FlightEndpointDataQueue.java index 1198d89c40a..8d066e65ca2 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/FlightEndpointDataQueue.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/FlightEndpointDataQueue.java @@ -35,7 +35,6 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; - import org.apache.arrow.driver.jdbc.client.CloseableEndpointStreamPair; import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.FlightRuntimeException; @@ -46,27 +45,29 @@ /** * Auxiliary class used to handle consuming of multiple {@link FlightStream}. - *

- * The usage follows this routine: + * + *

The usage follows this routine: + * *

    - *
  1. Create a FlightStreamQueue;
  2. - *
  3. Call enqueue(FlightStream) for all streams to be consumed;
  4. - *
  5. Call next() to get a FlightStream that is ready to consume
  6. - *
  7. Consume the given FlightStream and add it back to the queue - call enqueue(FlightStream)
  8. - *
  9. Repeat from (3) until next() returns null.
  10. + *
  11. Create a FlightStreamQueue; + *
  12. Call enqueue(FlightStream) for all streams to be consumed; + *
  13. Call next() to get a FlightStream that is ready to consume + *
  14. Consume the given FlightStream and add it back to the queue - call + * enqueue(FlightStream) + *
  15. Repeat from (3) until next() returns null. *
*/ public class FlightEndpointDataQueue implements AutoCloseable { private static final Logger LOGGER = LoggerFactory.getLogger(FlightEndpointDataQueue.class); private final CompletionService completionService; private final Set> futures = synchronizedSet(new HashSet<>()); - private final Set endpointsToClose = synchronizedSet(new HashSet<>()); + private final Set endpointsToClose = + synchronizedSet(new HashSet<>()); private final AtomicBoolean closed = new AtomicBoolean(); - /** - * Instantiate a new FlightStreamQueue. - */ - protected FlightEndpointDataQueue(final CompletionService executorService) { + /** Instantiate a new FlightStreamQueue. */ + protected FlightEndpointDataQueue( + final CompletionService executorService) { completionService = checkNotNull(executorService); } @@ -89,15 +90,14 @@ public boolean isClosed() { return closed.get(); } - /** - * Auxiliary functional interface for getting ready-to-consume FlightStreams. - */ + /** Auxiliary functional interface for getting ready-to-consume FlightStreams. */ @FunctionalInterface interface EndpointStreamSupplier { Future get() throws SQLException; } - private CloseableEndpointStreamPair next(final EndpointStreamSupplier endpointStreamSupplier) throws SQLException { + private CloseableEndpointStreamPair next(final EndpointStreamSupplier endpointStreamSupplier) + throws SQLException { checkOpen(); while (!futures.isEmpty()) { final Future future = endpointStreamSupplier.get(); @@ -119,24 +119,26 @@ private CloseableEndpointStreamPair next(final EndpointStreamSupplier endpointSt * Blocking request with timeout to get the next ready FlightStream in queue. * * @param timeoutValue the amount of time to be waited - * @param timeoutUnit the timeoutValue time unit + * @param timeoutUnit the timeoutValue time unit * @return a FlightStream that is ready to consume or null if all FlightStreams are ended. */ public CloseableEndpointStreamPair next(final long timeoutValue, final TimeUnit timeoutUnit) throws SQLException { - return next(() -> { - try { - final Future future = completionService.poll(timeoutValue, timeoutUnit); - if (future != null) { - return future; - } - } catch (final InterruptedException e) { - throw new SQLTimeoutException("Query was interrupted", e); - } + return next( + () -> { + try { + final Future future = + completionService.poll(timeoutValue, timeoutUnit); + if (future != null) { + return future; + } + } catch (final InterruptedException e) { + throw new SQLTimeoutException("Query was interrupted", e); + } - throw new SQLTimeoutException( - String.format("Query timed out after %d %s", timeoutValue, timeoutUnit)); - }); + throw new SQLTimeoutException( + String.format("Query timed out after %d %s", timeoutValue, timeoutUnit)); + }); } /** @@ -145,50 +147,47 @@ public CloseableEndpointStreamPair next(final long timeoutValue, final TimeUnit * @return a FlightStream that is ready to consume or null if all FlightStreams are ended. */ public CloseableEndpointStreamPair next() throws SQLException { - return next(() -> { - try { - return completionService.take(); - } catch (final InterruptedException e) { - throw AvaticaConnection.HELPER.wrap(e.getMessage(), e); - } - }); + return next( + () -> { + try { + return completionService.take(); + } catch (final InterruptedException e) { + throw AvaticaConnection.HELPER.wrap(e.getMessage(), e); + } + }); } - /** - * Checks if this queue is open. - */ + /** Checks if this queue is open. */ public synchronized void checkOpen() { checkState(!isClosed(), format("%s closed", this.getClass().getSimpleName())); } - /** - * Readily adds given {@link FlightStream}s to the queue. - */ + /** Readily adds given {@link FlightStream}s to the queue. */ public void enqueue(final Collection endpointRequests) { endpointRequests.forEach(this::enqueue); } - /** - * Adds given {@link FlightStream} to the queue. - */ + /** Adds given {@link FlightStream} to the queue. */ public synchronized void enqueue(final CloseableEndpointStreamPair endpointRequest) { checkNotNull(endpointRequest); checkOpen(); endpointsToClose.add(endpointRequest); - futures.add(completionService.submit(() -> { - // `FlightStream#next` will block until new data can be read or stream is over. - while (endpointRequest.getStream().next()) { - if (endpointRequest.getStream().getRoot().getRowCount() > 0) { - return endpointRequest; - } - } - return null; - })); + futures.add( + completionService.submit( + () -> { + // `FlightStream#next` will block until new data can be read or stream is over. + while (endpointRequest.getStream().next()) { + if (endpointRequest.getStream().getRoot().getRowCount() > 0) { + return endpointRequest; + } + } + return null; + })); } private static boolean isCallStatusCancelled(final Exception e) { - return e.getCause() instanceof FlightRuntimeException && - ((FlightRuntimeException) e.getCause()).status().code() == CallStatus.CANCELLED.code(); + return e.getCause() instanceof FlightRuntimeException + && ((FlightRuntimeException) e.getCause()).status().code() == CallStatus.CANCELLED.code(); } @Override @@ -208,19 +207,20 @@ public synchronized void close() throws SQLException { exceptions.add(new SQLException(errorMsg, e)); } } - futures.forEach(future -> { - try { - // TODO: Consider adding a hardcoded timeout? - future.get(); - } catch (final InterruptedException | ExecutionException e) { - // Ignore if future is already cancelled - if (!isCallStatusCancelled(e)) { - final String errorMsg = "Failed consuming a future during close."; - LOGGER.error(errorMsg, e); - exceptions.add(new SQLException(errorMsg, e)); - } - } - }); + futures.forEach( + future -> { + try { + // TODO: Consider adding a hardcoded timeout? + future.get(); + } catch (final InterruptedException | ExecutionException e) { + // Ignore if future is already cancelled + if (!isCallStatusCancelled(e)) { + final String errorMsg = "Failed consuming a future during close."; + LOGGER.error(errorMsg, e); + exceptions.add(new SQLException(errorMsg, e)); + } + } + }); for (final CloseableEndpointStreamPair endpointToClose : endpointsToClose) { try { endpointToClose.close(); diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/IntervalStringUtils.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/IntervalStringUtils.java index fdf6c508d93..3757bb4eb48 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/IntervalStringUtils.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/IntervalStringUtils.java @@ -19,23 +19,20 @@ import java.time.Duration; import java.time.Period; - import org.apache.arrow.vector.util.DateUtility; /** - * Utility class to format periods similar to Oracle's representation - * of "INTERVAL * to *" data type. + * Utility class to format periods similar to Oracle's representation of "INTERVAL * to *" data + * type. */ public final class IntervalStringUtils { - /** - * Constructor Method of class. - */ - private IntervalStringUtils( ) {} + /** Constructor Method of class. */ + private IntervalStringUtils() {} /** - * Formats a period similar to Oracle INTERVAL YEAR TO MONTH data type
. - * For example, the string "+21-02" defines an interval of 21 years and 2 months. + * Formats a period similar to Oracle INTERVAL YEAR TO MONTH data type
+ * . For example, the string "+21-02" defines an interval of 21 years and 2 months. */ public static String formatIntervalYear(final Period p) { long months = p.toTotalMonths(); @@ -51,9 +48,9 @@ public static String formatIntervalYear(final Period p) { } /** - * Formats a period similar to Oracle INTERVAL DAY TO SECOND data type.
. - * For example, the string "-001 18:25:16.766" defines an interval of - * - 1 day 18 hours 25 minutes 16 seconds and 766 milliseconds. + * Formats a period similar to Oracle INTERVAL DAY TO SECOND data type.
+ * . For example, the string "-001 18:25:16.766" defines an interval of - 1 day 18 hours 25 + * minutes 16 seconds and 766 milliseconds. */ public static String formatIntervalDay(final Duration d) { long millis = d.toMillis(); @@ -76,6 +73,7 @@ public static String formatIntervalDay(final Duration d) { final int seconds = (int) (millis / DateUtility.secondsToMillis); millis = millis % DateUtility.secondsToMillis; - return String.format("%c%03d %02d:%02d:%02d.%03d", neg ? '-' : '+', days, hours, minutes, seconds, millis); + return String.format( + "%c%03d %02d:%02d:%02d.%03d", neg ? '-' : '+', days, hours, minutes, seconds, millis); } } diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/SqlTypes.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/SqlTypes.java index 85c3964303c..3af95b13973 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/SqlTypes.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/SqlTypes.java @@ -20,13 +20,10 @@ import java.sql.Types; import java.util.HashMap; import java.util.Map; - import org.apache.arrow.vector.types.FloatingPointPrecision; import org.apache.arrow.vector.types.pojo.ArrowType; -/** - * SQL Types utility functions. - */ +/** SQL Types utility functions. */ public class SqlTypes { private static final Map typeIdToName = new HashMap<>(); @@ -84,7 +81,6 @@ public static String getSqlTypeNameFromArrowType(ArrowType arrowType) { return typeIdToName.get(typeId); } - /** * Convert given {@link ArrowType} to its corresponding SQL type ID. * diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/UrlParser.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/UrlParser.java index 64255e2213a..c41f7404d04 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/UrlParser.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/UrlParser.java @@ -21,13 +21,10 @@ import java.net.URLDecoder; import java.util.HashMap; import java.util.Map; -/** - * URL Parser for extracting key values from a connection string. - */ +/** URL Parser for extracting key values from a connection string. */ public final class UrlParser { - private UrlParser() { - } + private UrlParser() {} /** * Parse URL key value parameters. @@ -45,8 +42,10 @@ public static Map parse(String url, String separator) { for (String keyValue : keyValues) { try { - int separatorKey = keyValue.indexOf("="); // Find the first equal sign to split key and value. - if (separatorKey != -1) { // Avoid crashes when not finding an equal sign in the property value. + int separatorKey = + keyValue.indexOf("="); // Find the first equal sign to split key and value. + if (separatorKey + != -1) { // Avoid crashes when not finding an equal sign in the property value. String key = keyValue.substring(0, separatorKey); key = URLDecoder.decode(key, "UTF-8"); String value = ""; diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/VectorSchemaRootTransformer.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/VectorSchemaRootTransformer.java index 52a1d7db791..e24bf9f50a4 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/VectorSchemaRootTransformer.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/VectorSchemaRootTransformer.java @@ -20,7 +20,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.BaseFixedWidthVector; import org.apache.arrow.vector.BaseVariableWidthVector; @@ -32,17 +31,13 @@ import org.apache.arrow.vector.types.pojo.FieldType; import org.apache.arrow.vector.types.pojo.Schema; -/** - * Converts Arrow's {@link VectorSchemaRoot} format to one JDBC would expect. - */ +/** Converts Arrow's {@link VectorSchemaRoot} format to one JDBC would expect. */ @FunctionalInterface public interface VectorSchemaRootTransformer { VectorSchemaRoot transform(VectorSchemaRoot originalRoot, VectorSchemaRoot transformedRoot) throws Exception; - /** - * Transformer's helper class; builds a new {@link VectorSchemaRoot}. - */ + /** Transformer's helper class; builds a new {@link VectorSchemaRoot}. */ class Builder { private final Schema schema; @@ -52,50 +47,57 @@ class Builder { public Builder(final Schema schema, final BufferAllocator bufferAllocator) { this.schema = schema; - this.bufferAllocator = bufferAllocator - .newChildAllocator("VectorSchemaRootTransformer", 0, bufferAllocator.getLimit()); + this.bufferAllocator = + bufferAllocator.newChildAllocator( + "VectorSchemaRootTransformer", 0, bufferAllocator.getLimit()); } /** - * Add task to transform a vector to a new vector renaming it. - * This also adds transformedVectorName to the transformed {@link VectorSchemaRoot} schema. + * Add task to transform a vector to a new vector renaming it. This also adds + * transformedVectorName to the transformed {@link VectorSchemaRoot} schema. * - * @param originalVectorName Name of the original vector to be transformed. + * @param originalVectorName Name of the original vector to be transformed. * @param transformedVectorName Name of the vector that is the result of the transformation. * @return a VectorSchemaRoot instance with a task to rename a field vector. */ - public Builder renameFieldVector(final String originalVectorName, - final String transformedVectorName) { - tasks.add((originalRoot, transformedRoot) -> { - final FieldVector originalVector = originalRoot.getVector(originalVectorName); - final FieldVector transformedVector = transformedRoot.getVector(transformedVectorName); - - final ArrowType originalType = originalVector.getField().getType(); - final ArrowType transformedType = transformedVector.getField().getType(); - if (!originalType.equals(transformedType)) { - throw new IllegalArgumentException(String.format( - "Cannot transfer vector with field type %s to %s", originalType, transformedType)); - } - - if (originalVector instanceof BaseVariableWidthVector) { - ((BaseVariableWidthVector) originalVector).transferTo( - ((BaseVariableWidthVector) transformedVector)); - } else if (originalVector instanceof BaseFixedWidthVector) { - ((BaseFixedWidthVector) originalVector).transferTo( - ((BaseFixedWidthVector) transformedVector)); - } else { - throw new IllegalStateException(String.format( - "Cannot transfer vector of type %s", originalVector.getClass())); - } - }); + public Builder renameFieldVector( + final String originalVectorName, final String transformedVectorName) { + tasks.add( + (originalRoot, transformedRoot) -> { + final FieldVector originalVector = originalRoot.getVector(originalVectorName); + final FieldVector transformedVector = transformedRoot.getVector(transformedVectorName); + + final ArrowType originalType = originalVector.getField().getType(); + final ArrowType transformedType = transformedVector.getField().getType(); + if (!originalType.equals(transformedType)) { + throw new IllegalArgumentException( + String.format( + "Cannot transfer vector with field type %s to %s", + originalType, transformedType)); + } + + if (originalVector instanceof BaseVariableWidthVector) { + ((BaseVariableWidthVector) originalVector) + .transferTo(((BaseVariableWidthVector) transformedVector)); + } else if (originalVector instanceof BaseFixedWidthVector) { + ((BaseFixedWidthVector) originalVector) + .transferTo(((BaseFixedWidthVector) transformedVector)); + } else { + throw new IllegalStateException( + String.format("Cannot transfer vector of type %s", originalVector.getClass())); + } + }); final Field originalField = schema.findField(originalVectorName); - newFields.add(new Field( - transformedVectorName, - new FieldType(originalField.isNullable(), originalField.getType(), - originalField.getDictionary(), originalField.getMetadata()), - originalField.getChildren()) - ); + newFields.add( + new Field( + transformedVectorName, + new FieldType( + originalField.isNullable(), + originalField.getType(), + originalField.getDictionary(), + originalField.getMetadata()), + originalField.getChildren())); return this; } @@ -144,7 +146,8 @@ public VectorSchemaRootTransformer build() { } /** - * Functional interface used to a task to transform a VectorSchemaRoot into a new VectorSchemaRoot. + * Functional interface used to a task to transform a VectorSchemaRoot into a new + * VectorSchemaRoot. */ @FunctionalInterface interface Task { diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowDatabaseMetadataTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowDatabaseMetadataTest.java index 0d930f4c44e..46bfe71975e 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowDatabaseMetadataTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowDatabaseMetadataTest.java @@ -33,6 +33,9 @@ import static org.apache.arrow.flight.sql.impl.FlightSql.SqlSupportsConvert.SQL_CONVERT_INTEGER_VALUE; import static org.hamcrest.CoreMatchers.is; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.protobuf.Message; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; @@ -45,7 +48,6 @@ import java.util.Map; import java.util.Objects; import java.util.function.Consumer; - import org.apache.arrow.driver.jdbc.utils.MockFlightSqlProducer; import org.apache.arrow.driver.jdbc.utils.ResultSetTestUtils; import org.apache.arrow.driver.jdbc.utils.ThrowableAssertionUtils; @@ -84,20 +86,16 @@ import org.junit.Test; import org.junit.rules.ErrorCollector; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.protobuf.Message; - -/** - * Class containing the tests from the {@link ArrowDatabaseMetadata}. - */ +/** Class containing the tests from the {@link ArrowDatabaseMetadata}. */ @SuppressWarnings("DoubleBraceInitialization") public class ArrowDatabaseMetadataTest { public static final boolean EXPECTED_MAX_ROW_SIZE_INCLUDES_BLOBS = false; private static final MockFlightSqlProducer FLIGHT_SQL_PRODUCER = new MockFlightSqlProducer(); + @ClassRule - public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE = FlightServerTestRule - .createStandardTestRule(FLIGHT_SQL_PRODUCER); + public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE = + FlightServerTestRule.createStandardTestRule(FLIGHT_SQL_PRODUCER); + private static final int ROW_COUNT = 10; private static final List> EXPECTED_GET_CATALOGS_RESULTS = range(0, ROW_COUNT) @@ -113,61 +111,85 @@ public class ArrowDatabaseMetadataTest { .collect(toList()); private static final List> EXPECTED_GET_TABLES_RESULTS = range(0, ROW_COUNT) - .mapToObj(i -> new Object[] { - format("catalog_name #%d", i), - format("db_schema_name #%d", i), - format("table_name #%d", i), - format("table_type #%d", i), - // TODO Add these fields to FlightSQL, as it's currently not possible to fetch them. - null, null, null, null, null, null}) + .mapToObj( + i -> + new Object[] { + format("catalog_name #%d", i), + format("db_schema_name #%d", i), + format("table_name #%d", i), + format("table_type #%d", i), + // TODO Add these fields to FlightSQL, as it's currently not possible to fetch + // them. + null, + null, + null, + null, + null, + null + }) .map(Arrays::asList) .collect(toList()); private static final List> EXPECTED_GET_SCHEMAS_RESULTS = range(0, ROW_COUNT) - .mapToObj(i -> new Object[] { - format("db_schema_name #%d", i), - format("catalog_name #%d", i)}) + .mapToObj( + i -> new Object[] {format("db_schema_name #%d", i), format("catalog_name #%d", i)}) .map(Arrays::asList) .collect(toList()); private static final List> EXPECTED_GET_EXPORTED_AND_IMPORTED_KEYS_RESULTS = range(0, ROW_COUNT) - .mapToObj(i -> new Object[] { - format("pk_catalog_name #%d", i), - format("pk_db_schema_name #%d", i), - format("pk_table_name #%d", i), - format("pk_column_name #%d", i), - format("fk_catalog_name #%d", i), - format("fk_db_schema_name #%d", i), - format("fk_table_name #%d", i), - format("fk_column_name #%d", i), - i, - format("fk_key_name #%d", i), - format("pk_key_name #%d", i), - (byte) i, - (byte) i, - // TODO Add this field to FlightSQL, as it's currently not possible to fetch it. - null}) + .mapToObj( + i -> + new Object[] { + format("pk_catalog_name #%d", i), + format("pk_db_schema_name #%d", i), + format("pk_table_name #%d", i), + format("pk_column_name #%d", i), + format("fk_catalog_name #%d", i), + format("fk_db_schema_name #%d", i), + format("fk_table_name #%d", i), + format("fk_column_name #%d", i), + i, + format("fk_key_name #%d", i), + format("pk_key_name #%d", i), + (byte) i, + (byte) i, + // TODO Add this field to FlightSQL, as it's currently not possible to fetch it. + null + }) .map(Arrays::asList) .collect(toList()); private static final List> EXPECTED_CROSS_REFERENCE_RESULTS = EXPECTED_GET_EXPORTED_AND_IMPORTED_KEYS_RESULTS; private static final List> EXPECTED_PRIMARY_KEYS_RESULTS = range(0, ROW_COUNT) - .mapToObj(i -> new Object[] { - format("catalog_name #%d", i), - format("db_schema_name #%d", i), - format("table_name #%d", i), - format("column_name #%d", i), - i, - format("key_name #%d", i)}) + .mapToObj( + i -> + new Object[] { + format("catalog_name #%d", i), + format("db_schema_name #%d", i), + format("table_name #%d", i), + format("column_name #%d", i), + i, + format("key_name #%d", i) + }) .map(Arrays::asList) .collect(toList()); - private static final List FIELDS_GET_IMPORTED_EXPORTED_KEYS = ImmutableList.of( - "PKTABLE_CAT", "PKTABLE_SCHEM", "PKTABLE_NAME", - "PKCOLUMN_NAME", "FKTABLE_CAT", "FKTABLE_SCHEM", - "FKTABLE_NAME", "FKCOLUMN_NAME", "KEY_SEQ", - "FK_NAME", "PK_NAME", "UPDATE_RULE", "DELETE_RULE", - "DEFERRABILITY"); + private static final List FIELDS_GET_IMPORTED_EXPORTED_KEYS = + ImmutableList.of( + "PKTABLE_CAT", + "PKTABLE_SCHEM", + "PKTABLE_NAME", + "PKCOLUMN_NAME", + "FKTABLE_CAT", + "FKTABLE_SCHEM", + "FKTABLE_NAME", + "FKCOLUMN_NAME", + "KEY_SEQ", + "FK_NAME", + "PK_NAME", + "UPDATE_RULE", + "DELETE_RULE", + "DEFERRABILITY"); private static final List FIELDS_GET_CROSS_REFERENCE = FIELDS_GET_IMPORTED_EXPORTED_KEYS; private static final String TARGET_TABLE = "TARGET_TABLE"; private static final String TARGET_FOREIGN_TABLE = "FOREIGN_TABLE"; @@ -229,8 +251,8 @@ public class ArrowDatabaseMetadataTest { private static final boolean EXPECTED_SUBQUERIES_IN_EXISTS = false; private static final boolean EXPECTED_SUBQUERIES_IN_INS = false; private static final boolean EXPECTED_SUBQUERIES_IN_QUANTIFIEDS = false; - private static final SqlSupportedSubqueries[] EXPECTED_SUPPORTED_SUBQUERIES = new SqlSupportedSubqueries[] - {SqlSupportedSubqueries.SQL_SUBQUERIES_IN_COMPARISONS}; + private static final SqlSupportedSubqueries[] EXPECTED_SUPPORTED_SUBQUERIES = + new SqlSupportedSubqueries[] {SqlSupportedSubqueries.SQL_SUBQUERIES_IN_COMPARISONS}; private static final boolean EXPECTED_CORRELATED_SUBQUERIES_SUPPORTED = true; private static final boolean EXPECTED_SUPPORTS_UNION = true; private static final boolean EXPECTED_SUPPORTS_UNION_ALL = true; @@ -277,30 +299,41 @@ public class ArrowDatabaseMetadataTest { List expectedGetColumnsColumnSize = Arrays.asList(5, 29, 10); List expectedGetColumnsDecimalDigits = Arrays.asList(2, 9, 0); List expectedGetColumnsIsNullable = Arrays.asList("YES", "YES", "NO"); - EXPECTED_GET_COLUMNS_RESULTS = range(0, ROW_COUNT * 3) - .mapToObj(i -> new Object[] { - format("catalog_name #%d", i / 3), - format("db_schema_name #%d", i / 3), - format("table_name%d", i / 3), - format("column_%d", (i % 3) + 1), - expectedGetColumnsDataTypes.get(i % 3), - expectedGetColumnsTypeName.get(i % 3), - expectedGetColumnsColumnSize.get(i % 3), - null, - expectedGetColumnsDecimalDigits.get(i % 3), - expectedGetColumnsRadix.get(i % 3), - !Objects.equals(expectedGetColumnsIsNullable.get(i % 3), "NO") ? 1 : 0, - null, null, null, null, null, - (i % 3) + 1, - expectedGetColumnsIsNullable.get(i % 3), - null, null, null, null, - "", ""}) - .map(Arrays::asList) - .collect(toList()); + EXPECTED_GET_COLUMNS_RESULTS = + range(0, ROW_COUNT * 3) + .mapToObj( + i -> + new Object[] { + format("catalog_name #%d", i / 3), + format("db_schema_name #%d", i / 3), + format("table_name%d", i / 3), + format("column_%d", (i % 3) + 1), + expectedGetColumnsDataTypes.get(i % 3), + expectedGetColumnsTypeName.get(i % 3), + expectedGetColumnsColumnSize.get(i % 3), + null, + expectedGetColumnsDecimalDigits.get(i % 3), + expectedGetColumnsRadix.get(i % 3), + !Objects.equals(expectedGetColumnsIsNullable.get(i % 3), "NO") ? 1 : 0, + null, + null, + null, + null, + null, + (i % 3) + 1, + expectedGetColumnsIsNullable.get(i % 3), + null, + null, + null, + null, + "", + "" + }) + .map(Arrays::asList) + .collect(toList()); } - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); public final ResultSetTestUtils resultSetTestUtils = new ResultSetTestUtils(collector); @BeforeClass @@ -308,140 +341,149 @@ public static void setUpBeforeClass() throws SQLException { connection = FLIGHT_SERVER_TEST_RULE.getConnection(false); final Message commandGetCatalogs = CommandGetCatalogs.getDefaultInstance(); - final Consumer commandGetCatalogsResultProducer = listener -> { - try (final BufferAllocator allocator = new RootAllocator(); - final VectorSchemaRoot root = VectorSchemaRoot.create(Schemas.GET_CATALOGS_SCHEMA, - allocator)) { - final VarCharVector catalogName = (VarCharVector) root.getVector("catalog_name"); - range(0, ROW_COUNT).forEach( - i -> catalogName.setSafe(i, new Text(format("catalog #%d", i)))); - root.setRowCount(ROW_COUNT); - listener.start(root); - listener.putNext(); - } catch (final Throwable throwable) { - listener.error(throwable); - } finally { - listener.completed(); - } - }; + final Consumer commandGetCatalogsResultProducer = + listener -> { + try (final BufferAllocator allocator = new RootAllocator(); + final VectorSchemaRoot root = + VectorSchemaRoot.create(Schemas.GET_CATALOGS_SCHEMA, allocator)) { + final VarCharVector catalogName = (VarCharVector) root.getVector("catalog_name"); + range(0, ROW_COUNT) + .forEach(i -> catalogName.setSafe(i, new Text(format("catalog #%d", i)))); + root.setRowCount(ROW_COUNT); + listener.start(root); + listener.putNext(); + } catch (final Throwable throwable) { + listener.error(throwable); + } finally { + listener.completed(); + } + }; FLIGHT_SQL_PRODUCER.addCatalogQuery(commandGetCatalogs, commandGetCatalogsResultProducer); final Message commandGetTableTypes = CommandGetTableTypes.getDefaultInstance(); - final Consumer commandGetTableTypesResultProducer = listener -> { - try (final BufferAllocator allocator = new RootAllocator(); - final VectorSchemaRoot root = VectorSchemaRoot.create(Schemas.GET_TABLE_TYPES_SCHEMA, - allocator)) { - final VarCharVector tableType = (VarCharVector) root.getVector("table_type"); - range(0, ROW_COUNT).forEach( - i -> tableType.setSafe(i, new Text(format("table_type #%d", i)))); - root.setRowCount(ROW_COUNT); - listener.start(root); - listener.putNext(); - } catch (final Throwable throwable) { - listener.error(throwable); - } finally { - listener.completed(); - } - }; + final Consumer commandGetTableTypesResultProducer = + listener -> { + try (final BufferAllocator allocator = new RootAllocator(); + final VectorSchemaRoot root = + VectorSchemaRoot.create(Schemas.GET_TABLE_TYPES_SCHEMA, allocator)) { + final VarCharVector tableType = (VarCharVector) root.getVector("table_type"); + range(0, ROW_COUNT) + .forEach(i -> tableType.setSafe(i, new Text(format("table_type #%d", i)))); + root.setRowCount(ROW_COUNT); + listener.start(root); + listener.putNext(); + } catch (final Throwable throwable) { + listener.error(throwable); + } finally { + listener.completed(); + } + }; FLIGHT_SQL_PRODUCER.addCatalogQuery(commandGetTableTypes, commandGetTableTypesResultProducer); final Message commandGetTables = CommandGetTables.getDefaultInstance(); - final Consumer commandGetTablesResultProducer = listener -> { - try (final BufferAllocator allocator = new RootAllocator(); - final VectorSchemaRoot root = VectorSchemaRoot.create( - Schemas.GET_TABLES_SCHEMA_NO_SCHEMA, allocator)) { - final VarCharVector catalogName = (VarCharVector) root.getVector("catalog_name"); - final VarCharVector schemaName = (VarCharVector) root.getVector("db_schema_name"); - final VarCharVector tableName = (VarCharVector) root.getVector("table_name"); - final VarCharVector tableType = (VarCharVector) root.getVector("table_type"); - range(0, ROW_COUNT) - .peek(i -> catalogName.setSafe(i, new Text(format("catalog_name #%d", i)))) - .peek(i -> schemaName.setSafe(i, new Text(format("db_schema_name #%d", i)))) - .peek(i -> tableName.setSafe(i, new Text(format("table_name #%d", i)))) - .forEach(i -> tableType.setSafe(i, new Text(format("table_type #%d", i)))); - root.setRowCount(ROW_COUNT); - listener.start(root); - listener.putNext(); - } catch (final Throwable throwable) { - listener.error(throwable); - } finally { - listener.completed(); - } - }; + final Consumer commandGetTablesResultProducer = + listener -> { + try (final BufferAllocator allocator = new RootAllocator(); + final VectorSchemaRoot root = + VectorSchemaRoot.create(Schemas.GET_TABLES_SCHEMA_NO_SCHEMA, allocator)) { + final VarCharVector catalogName = (VarCharVector) root.getVector("catalog_name"); + final VarCharVector schemaName = (VarCharVector) root.getVector("db_schema_name"); + final VarCharVector tableName = (VarCharVector) root.getVector("table_name"); + final VarCharVector tableType = (VarCharVector) root.getVector("table_type"); + range(0, ROW_COUNT) + .peek(i -> catalogName.setSafe(i, new Text(format("catalog_name #%d", i)))) + .peek(i -> schemaName.setSafe(i, new Text(format("db_schema_name #%d", i)))) + .peek(i -> tableName.setSafe(i, new Text(format("table_name #%d", i)))) + .forEach(i -> tableType.setSafe(i, new Text(format("table_type #%d", i)))); + root.setRowCount(ROW_COUNT); + listener.start(root); + listener.putNext(); + } catch (final Throwable throwable) { + listener.error(throwable); + } finally { + listener.completed(); + } + }; FLIGHT_SQL_PRODUCER.addCatalogQuery(commandGetTables, commandGetTablesResultProducer); - final Message commandGetTablesWithSchema = CommandGetTables.newBuilder() - .setIncludeSchema(true) - .build(); - final Consumer commandGetTablesWithSchemaResultProducer = listener -> { - try (final BufferAllocator allocator = new RootAllocator(); - final VectorSchemaRoot root = VectorSchemaRoot.create(Schemas.GET_TABLES_SCHEMA, - allocator)) { - final byte[] filledTableSchemaBytes = - copyFrom( - serializeSchema(new Schema(Arrays.asList( - Field.nullable("column_1", ArrowType.Decimal.createDecimal(5, 2, 128)), - Field.nullable("column_2", new ArrowType.Timestamp(TimeUnit.NANOSECOND, "UTC")), - Field.notNullable("column_3", Types.MinorType.INT.getType()))))) - .toByteArray(); - final VarCharVector catalogName = (VarCharVector) root.getVector("catalog_name"); - final VarCharVector schemaName = (VarCharVector) root.getVector("db_schema_name"); - final VarCharVector tableName = (VarCharVector) root.getVector("table_name"); - final VarCharVector tableType = (VarCharVector) root.getVector("table_type"); - final VarBinaryVector tableSchema = (VarBinaryVector) root.getVector("table_schema"); - range(0, ROW_COUNT) - .peek(i -> catalogName.setSafe(i, new Text(format("catalog_name #%d", i)))) - .peek(i -> schemaName.setSafe(i, new Text(format("db_schema_name #%d", i)))) - .peek(i -> tableName.setSafe(i, new Text(format("table_name%d", i)))) - .peek(i -> tableType.setSafe(i, new Text(format("table_type #%d", i)))) - .forEach(i -> tableSchema.setSafe(i, filledTableSchemaBytes)); - root.setRowCount(ROW_COUNT); - listener.start(root); - listener.putNext(); - } catch (final Throwable throwable) { - listener.error(throwable); - } finally { - listener.completed(); - } - }; - FLIGHT_SQL_PRODUCER.addCatalogQuery(commandGetTablesWithSchema, - commandGetTablesWithSchemaResultProducer); + final Message commandGetTablesWithSchema = + CommandGetTables.newBuilder().setIncludeSchema(true).build(); + final Consumer commandGetTablesWithSchemaResultProducer = + listener -> { + try (final BufferAllocator allocator = new RootAllocator(); + final VectorSchemaRoot root = + VectorSchemaRoot.create(Schemas.GET_TABLES_SCHEMA, allocator)) { + final byte[] filledTableSchemaBytes = + copyFrom( + serializeSchema( + new Schema( + Arrays.asList( + Field.nullable( + "column_1", ArrowType.Decimal.createDecimal(5, 2, 128)), + Field.nullable( + "column_2", + new ArrowType.Timestamp(TimeUnit.NANOSECOND, "UTC")), + Field.notNullable("column_3", Types.MinorType.INT.getType()))))) + .toByteArray(); + final VarCharVector catalogName = (VarCharVector) root.getVector("catalog_name"); + final VarCharVector schemaName = (VarCharVector) root.getVector("db_schema_name"); + final VarCharVector tableName = (VarCharVector) root.getVector("table_name"); + final VarCharVector tableType = (VarCharVector) root.getVector("table_type"); + final VarBinaryVector tableSchema = (VarBinaryVector) root.getVector("table_schema"); + range(0, ROW_COUNT) + .peek(i -> catalogName.setSafe(i, new Text(format("catalog_name #%d", i)))) + .peek(i -> schemaName.setSafe(i, new Text(format("db_schema_name #%d", i)))) + .peek(i -> tableName.setSafe(i, new Text(format("table_name%d", i)))) + .peek(i -> tableType.setSafe(i, new Text(format("table_type #%d", i)))) + .forEach(i -> tableSchema.setSafe(i, filledTableSchemaBytes)); + root.setRowCount(ROW_COUNT); + listener.start(root); + listener.putNext(); + } catch (final Throwable throwable) { + listener.error(throwable); + } finally { + listener.completed(); + } + }; + FLIGHT_SQL_PRODUCER.addCatalogQuery( + commandGetTablesWithSchema, commandGetTablesWithSchemaResultProducer); final Message commandGetDbSchemas = CommandGetDbSchemas.getDefaultInstance(); - final Consumer commandGetSchemasResultProducer = listener -> { - try (final BufferAllocator allocator = new RootAllocator(); - final VectorSchemaRoot root = VectorSchemaRoot.create(Schemas.GET_SCHEMAS_SCHEMA, - allocator)) { - final VarCharVector catalogName = (VarCharVector) root.getVector("catalog_name"); - final VarCharVector schemaName = (VarCharVector) root.getVector("db_schema_name"); - range(0, ROW_COUNT) - .peek(i -> catalogName.setSafe(i, new Text(format("catalog_name #%d", i)))) - .forEach(i -> schemaName.setSafe(i, new Text(format("db_schema_name #%d", i)))); - root.setRowCount(ROW_COUNT); - listener.start(root); - listener.putNext(); - } catch (final Throwable throwable) { - listener.error(throwable); - } finally { - listener.completed(); - } - }; + final Consumer commandGetSchemasResultProducer = + listener -> { + try (final BufferAllocator allocator = new RootAllocator(); + final VectorSchemaRoot root = + VectorSchemaRoot.create(Schemas.GET_SCHEMAS_SCHEMA, allocator)) { + final VarCharVector catalogName = (VarCharVector) root.getVector("catalog_name"); + final VarCharVector schemaName = (VarCharVector) root.getVector("db_schema_name"); + range(0, ROW_COUNT) + .peek(i -> catalogName.setSafe(i, new Text(format("catalog_name #%d", i)))) + .forEach(i -> schemaName.setSafe(i, new Text(format("db_schema_name #%d", i)))); + root.setRowCount(ROW_COUNT); + listener.start(root); + listener.putNext(); + } catch (final Throwable throwable) { + listener.error(throwable); + } finally { + listener.completed(); + } + }; FLIGHT_SQL_PRODUCER.addCatalogQuery(commandGetDbSchemas, commandGetSchemasResultProducer); final Message commandGetExportedKeys = CommandGetExportedKeys.newBuilder().setTable(TARGET_TABLE).build(); final Message commandGetImportedKeys = CommandGetImportedKeys.newBuilder().setTable(TARGET_TABLE).build(); - final Message commandGetCrossReference = CommandGetCrossReference.newBuilder() - .setPkTable(TARGET_TABLE) - .setFkTable(TARGET_FOREIGN_TABLE) - .build(); + final Message commandGetCrossReference = + CommandGetCrossReference.newBuilder() + .setPkTable(TARGET_TABLE) + .setFkTable(TARGET_FOREIGN_TABLE) + .build(); final Consumer commandGetExportedAndImportedKeysResultProducer = listener -> { try (final BufferAllocator allocator = new RootAllocator(); - final VectorSchemaRoot root = VectorSchemaRoot.create( - Schemas.GET_IMPORTED_KEYS_SCHEMA, - allocator)) { + final VectorSchemaRoot root = + VectorSchemaRoot.create(Schemas.GET_IMPORTED_KEYS_SCHEMA, allocator)) { final VarCharVector pkCatalogName = (VarCharVector) root.getVector("pk_catalog_name"); final VarCharVector pkSchemaName = (VarCharVector) root.getVector("pk_db_schema_name"); final VarCharVector pkTableName = (VarCharVector) root.getVector("pk_table_name"); @@ -478,44 +520,46 @@ public static void setUpBeforeClass() throws SQLException { listener.completed(); } }; - FLIGHT_SQL_PRODUCER.addCatalogQuery(commandGetExportedKeys, - commandGetExportedAndImportedKeysResultProducer); - FLIGHT_SQL_PRODUCER.addCatalogQuery(commandGetImportedKeys, - commandGetExportedAndImportedKeysResultProducer); - FLIGHT_SQL_PRODUCER.addCatalogQuery(commandGetCrossReference, - commandGetExportedAndImportedKeysResultProducer); + FLIGHT_SQL_PRODUCER.addCatalogQuery( + commandGetExportedKeys, commandGetExportedAndImportedKeysResultProducer); + FLIGHT_SQL_PRODUCER.addCatalogQuery( + commandGetImportedKeys, commandGetExportedAndImportedKeysResultProducer); + FLIGHT_SQL_PRODUCER.addCatalogQuery( + commandGetCrossReference, commandGetExportedAndImportedKeysResultProducer); final Message commandGetPrimaryKeys = CommandGetPrimaryKeys.newBuilder().setTable(TARGET_TABLE).build(); - final Consumer commandGetPrimaryKeysResultProducer = listener -> { - try (final BufferAllocator allocator = new RootAllocator(); - final VectorSchemaRoot root = VectorSchemaRoot.create(Schemas.GET_PRIMARY_KEYS_SCHEMA, - allocator)) { - final VarCharVector catalogName = (VarCharVector) root.getVector("catalog_name"); - final VarCharVector schemaName = (VarCharVector) root.getVector("db_schema_name"); - final VarCharVector tableName = (VarCharVector) root.getVector("table_name"); - final VarCharVector columnName = (VarCharVector) root.getVector("column_name"); - final IntVector keySequence = (IntVector) root.getVector("key_sequence"); - final VarCharVector keyName = (VarCharVector) root.getVector("key_name"); - range(0, ROW_COUNT) - .peek(i -> catalogName.setSafe(i, new Text(format("catalog_name #%d", i)))) - .peek(i -> schemaName.setSafe(i, new Text(format("db_schema_name #%d", i)))) - .peek(i -> tableName.setSafe(i, new Text(format("table_name #%d", i)))) - .peek(i -> columnName.setSafe(i, new Text(format("column_name #%d", i)))) - .peek(i -> keySequence.setSafe(i, i)) - .forEach(i -> keyName.setSafe(i, new Text(format("key_name #%d", i)))); - root.setRowCount(ROW_COUNT); - listener.start(root); - listener.putNext(); - } catch (final Throwable throwable) { - listener.error(throwable); - } finally { - listener.completed(); - } - }; + final Consumer commandGetPrimaryKeysResultProducer = + listener -> { + try (final BufferAllocator allocator = new RootAllocator(); + final VectorSchemaRoot root = + VectorSchemaRoot.create(Schemas.GET_PRIMARY_KEYS_SCHEMA, allocator)) { + final VarCharVector catalogName = (VarCharVector) root.getVector("catalog_name"); + final VarCharVector schemaName = (VarCharVector) root.getVector("db_schema_name"); + final VarCharVector tableName = (VarCharVector) root.getVector("table_name"); + final VarCharVector columnName = (VarCharVector) root.getVector("column_name"); + final IntVector keySequence = (IntVector) root.getVector("key_sequence"); + final VarCharVector keyName = (VarCharVector) root.getVector("key_name"); + range(0, ROW_COUNT) + .peek(i -> catalogName.setSafe(i, new Text(format("catalog_name #%d", i)))) + .peek(i -> schemaName.setSafe(i, new Text(format("db_schema_name #%d", i)))) + .peek(i -> tableName.setSafe(i, new Text(format("table_name #%d", i)))) + .peek(i -> columnName.setSafe(i, new Text(format("column_name #%d", i)))) + .peek(i -> keySequence.setSafe(i, i)) + .forEach(i -> keyName.setSafe(i, new Text(format("key_name #%d", i)))); + root.setRowCount(ROW_COUNT); + listener.start(root); + listener.putNext(); + } catch (final Throwable throwable) { + listener.error(throwable); + } finally { + listener.completed(); + } + }; FLIGHT_SQL_PRODUCER.addCatalogQuery(commandGetPrimaryKeys, commandGetPrimaryKeysResultProducer); - FLIGHT_SQL_PRODUCER.getSqlInfoBuilder() + FLIGHT_SQL_PRODUCER + .getSqlInfoBuilder() .withSqlOuterJoinSupportLevel(FlightSql.SqlOuterJoinsSupportLevel.SQL_FULL_OUTER_JOINS) .withFlightSqlServerName(EXPECTED_DATABASE_PRODUCT_NAME) .withFlightSqlServerVersion(EXPECTED_DATABASE_PRODUCT_VERSION) @@ -530,8 +574,10 @@ public static void setUpBeforeClass() throws SQLException { .withSqlExtraNameCharacters(EXPECTED_EXTRA_NAME_CHARACTERS) .withSqlSupportsColumnAliasing(EXPECTED_SUPPORTS_COLUMN_ALIASING) .withSqlNullPlusNullIsNull(EXPECTED_NULL_PLUS_NULL_IS_NULL) - .withSqlSupportsConvert(ImmutableMap.of(SQL_CONVERT_BIT_VALUE, - Arrays.asList(SQL_CONVERT_INTEGER_VALUE, SQL_CONVERT_BIGINT_VALUE))) + .withSqlSupportsConvert( + ImmutableMap.of( + SQL_CONVERT_BIT_VALUE, + Arrays.asList(SQL_CONVERT_INTEGER_VALUE, SQL_CONVERT_BIGINT_VALUE))) .withSqlSupportsTableCorrelationNames(EXPECTED_SUPPORTS_TABLE_CORRELATION_NAMES) .withSqlSupportsDifferentTableCorrelationNames( EXPECTED_SUPPORTS_DIFFERENT_TABLE_CORRELATION_NAMES) @@ -540,9 +586,11 @@ public static void setUpBeforeClass() throws SQLException { .withSqlSupportedGroupBy(FlightSql.SqlSupportedGroupBy.SQL_GROUP_BY_UNRELATED) .withSqlSupportsLikeEscapeClause(EXPECTED_SUPPORTS_LIKE_ESCAPE_CLAUSE) .withSqlSupportsNonNullableColumns(EXPECTED_NON_NULLABLE_COLUMNS) - .withSqlSupportedGrammar(FlightSql.SupportedSqlGrammar.SQL_CORE_GRAMMAR, + .withSqlSupportedGrammar( + FlightSql.SupportedSqlGrammar.SQL_CORE_GRAMMAR, FlightSql.SupportedSqlGrammar.SQL_MINIMUM_GRAMMAR) - .withSqlAnsi92SupportedLevel(FlightSql.SupportedAnsi92SqlGrammarLevel.ANSI92_ENTRY_SQL, + .withSqlAnsi92SupportedLevel( + FlightSql.SupportedAnsi92SqlGrammarLevel.ANSI92_ENTRY_SQL, FlightSql.SupportedAnsi92SqlGrammarLevel.ANSI92_INTERMEDIATE_SQL) .withSqlSupportsIntegrityEnhancementFacility( EXPECTED_SUPPORTS_INTEGRITY_ENHANCEMENT_FACILITY) @@ -607,7 +655,6 @@ public static void tearDown() throws Exception { AutoCloseables.close(connection, FLIGHT_SQL_PRODUCER); } - @Test public void testGetCatalogsCanBeAccessedByIndices() throws SQLException { try (final ResultSet resultSet = connection.getMetaData().getCatalogs()) { @@ -618,8 +665,8 @@ public void testGetCatalogsCanBeAccessedByIndices() throws SQLException { @Test public void testGetCatalogsCanBeAccessedByNames() throws SQLException { try (final ResultSet resultSet = connection.getMetaData().getCatalogs()) { - resultSetTestUtils.testData(resultSet, singletonList("TABLE_CAT"), - EXPECTED_GET_CATALOGS_RESULTS); + resultSetTestUtils.testData( + resultSet, singletonList("TABLE_CAT"), EXPECTED_GET_CATALOGS_RESULTS); } } @@ -633,8 +680,8 @@ public void testTableTypesCanBeAccessedByIndices() throws SQLException { @Test public void testTableTypesCanBeAccessedByNames() throws SQLException { try (final ResultSet resultSet = connection.getMetaData().getTableTypes()) { - resultSetTestUtils.testData(resultSet, singletonList("TABLE_TYPE"), - EXPECTED_GET_TABLE_TYPES_RESULTS); + resultSetTestUtils.testData( + resultSet, singletonList("TABLE_TYPE"), EXPECTED_GET_TABLE_TYPES_RESULTS); } } @@ -661,8 +708,7 @@ public void testGetTablesCanBeAccessedByNames() throws SQLException { "TYPE_NAME", "SELF_REFERENCING_COL_NAME", "REF_GENERATION"), - EXPECTED_GET_TABLES_RESULTS - ); + EXPECTED_GET_TABLES_RESULTS); } } @@ -676,59 +722,67 @@ public void testGetSchemasCanBeAccessedByIndices() throws SQLException { @Test public void testGetSchemasCanBeAccessedByNames() throws SQLException { try (final ResultSet resultSet = connection.getMetaData().getSchemas()) { - resultSetTestUtils.testData(resultSet, ImmutableList.of("TABLE_SCHEM", "TABLE_CATALOG"), + resultSetTestUtils.testData( + resultSet, + ImmutableList.of("TABLE_SCHEM", "TABLE_CATALOG"), EXPECTED_GET_SCHEMAS_RESULTS); } } @Test public void testGetExportedKeysCanBeAccessedByIndices() throws SQLException { - try (final ResultSet resultSet = connection.getMetaData() - .getExportedKeys(null, null, TARGET_TABLE)) { + try (final ResultSet resultSet = + connection.getMetaData().getExportedKeys(null, null, TARGET_TABLE)) { resultSetTestUtils.testData(resultSet, EXPECTED_GET_EXPORTED_AND_IMPORTED_KEYS_RESULTS); } } @Test public void testGetExportedKeysCanBeAccessedByNames() throws SQLException { - try (final ResultSet resultSet = connection.getMetaData() - .getExportedKeys(null, null, TARGET_TABLE)) { + try (final ResultSet resultSet = + connection.getMetaData().getExportedKeys(null, null, TARGET_TABLE)) { resultSetTestUtils.testData( - resultSet, FIELDS_GET_IMPORTED_EXPORTED_KEYS, + resultSet, + FIELDS_GET_IMPORTED_EXPORTED_KEYS, EXPECTED_GET_EXPORTED_AND_IMPORTED_KEYS_RESULTS); } } @Test public void testGetImportedKeysCanBeAccessedByIndices() throws SQLException { - try (final ResultSet resultSet = connection.getMetaData() - .getImportedKeys(null, null, TARGET_TABLE)) { + try (final ResultSet resultSet = + connection.getMetaData().getImportedKeys(null, null, TARGET_TABLE)) { resultSetTestUtils.testData(resultSet, EXPECTED_GET_EXPORTED_AND_IMPORTED_KEYS_RESULTS); } } @Test public void testGetImportedKeysCanBeAccessedByNames() throws SQLException { - try (final ResultSet resultSet = connection.getMetaData() - .getImportedKeys(null, null, TARGET_TABLE)) { + try (final ResultSet resultSet = + connection.getMetaData().getImportedKeys(null, null, TARGET_TABLE)) { resultSetTestUtils.testData( - resultSet, FIELDS_GET_IMPORTED_EXPORTED_KEYS, + resultSet, + FIELDS_GET_IMPORTED_EXPORTED_KEYS, EXPECTED_GET_EXPORTED_AND_IMPORTED_KEYS_RESULTS); } } @Test public void testGetCrossReferenceCanBeAccessedByIndices() throws SQLException { - try (final ResultSet resultSet = connection.getMetaData().getCrossReference(null, null, - TARGET_TABLE, null, null, TARGET_FOREIGN_TABLE)) { + try (final ResultSet resultSet = + connection + .getMetaData() + .getCrossReference(null, null, TARGET_TABLE, null, null, TARGET_FOREIGN_TABLE)) { resultSetTestUtils.testData(resultSet, EXPECTED_CROSS_REFERENCE_RESULTS); } } @Test public void testGetGetCrossReferenceCanBeAccessedByNames() throws SQLException { - try (final ResultSet resultSet = connection.getMetaData().getCrossReference(null, null, - TARGET_TABLE, null, null, TARGET_FOREIGN_TABLE)) { + try (final ResultSet resultSet = + connection + .getMetaData() + .getCrossReference(null, null, TARGET_TABLE, null, null, TARGET_FOREIGN_TABLE)) { resultSetTestUtils.testData( resultSet, FIELDS_GET_CROSS_REFERENCE, EXPECTED_CROSS_REFERENCE_RESULTS); } @@ -736,27 +790,21 @@ public void testGetGetCrossReferenceCanBeAccessedByNames() throws SQLException { @Test public void testPrimaryKeysCanBeAccessedByIndices() throws SQLException { - try (final ResultSet resultSet = connection.getMetaData() - .getPrimaryKeys(null, null, TARGET_TABLE)) { + try (final ResultSet resultSet = + connection.getMetaData().getPrimaryKeys(null, null, TARGET_TABLE)) { resultSetTestUtils.testData(resultSet, EXPECTED_PRIMARY_KEYS_RESULTS); } } @Test public void testPrimaryKeysCanBeAccessedByNames() throws SQLException { - try (final ResultSet resultSet = connection.getMetaData() - .getPrimaryKeys(null, null, TARGET_TABLE)) { + try (final ResultSet resultSet = + connection.getMetaData().getPrimaryKeys(null, null, TARGET_TABLE)) { resultSetTestUtils.testData( resultSet, ImmutableList.of( - "TABLE_CAT", - "TABLE_SCHEM", - "TABLE_NAME", - "COLUMN_NAME", - "KEY_SEQ", - "PK_NAME"), - EXPECTED_PRIMARY_KEYS_RESULTS - ); + "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "COLUMN_NAME", "KEY_SEQ", "PK_NAME"), + EXPECTED_PRIMARY_KEYS_RESULTS); } } @@ -769,14 +817,13 @@ public void testGetColumnsCanBeAccessedByIndices() throws SQLException { @Test public void testGetColumnsCanByIndicesFilteringColumnNames() throws SQLException { - try ( - final ResultSet resultSet = connection.getMetaData() - .getColumns(null, null, null, "column_1")) { - resultSetTestUtils.testData(resultSet, EXPECTED_GET_COLUMNS_RESULTS - .stream() - .filter(insideList -> Objects.equals(insideList.get(3), "column_1")) - .collect(toList()) - ); + try (final ResultSet resultSet = + connection.getMetaData().getColumns(null, null, null, "column_1")) { + resultSetTestUtils.testData( + resultSet, + EXPECTED_GET_COLUMNS_RESULTS.stream() + .filter(insideList -> Objects.equals(insideList.get(3), "column_1")) + .collect(toList())); } } @@ -784,8 +831,8 @@ public void testGetColumnsCanByIndicesFilteringColumnNames() throws SQLException public void testGetSqlInfo() throws SQLException { final DatabaseMetaData metaData = connection.getMetaData(); collector.checkThat(metaData.getDatabaseProductName(), is(EXPECTED_DATABASE_PRODUCT_NAME)); - collector.checkThat(metaData.getDatabaseProductVersion(), - is(EXPECTED_DATABASE_PRODUCT_VERSION)); + collector.checkThat( + metaData.getDatabaseProductVersion(), is(EXPECTED_DATABASE_PRODUCT_VERSION)); collector.checkThat(metaData.getIdentifierQuoteString(), is(EXPECTED_IDENTIFIER_QUOTE_STRING)); collector.checkThat(metaData.isReadOnly(), is(EXPECTED_IS_READ_ONLY)); collector.checkThat(metaData.getSQLKeywords(), is(EXPECTED_SQL_KEYWORDS)); @@ -798,29 +845,29 @@ public void testGetSqlInfo() throws SQLException { collector.checkThat(metaData.supportsConvert(), is(EXPECTED_SQL_SUPPORTS_CONVERT)); collector.checkThat(metaData.supportsConvert(BIT, INTEGER), is(EXPECTED_SQL_SUPPORTS_CONVERT)); collector.checkThat(metaData.supportsConvert(BIT, BIGINT), is(EXPECTED_SQL_SUPPORTS_CONVERT)); - collector.checkThat(metaData.supportsConvert(BIGINT, INTEGER), - is(EXPECTED_INVALID_SQL_SUPPORTS_CONVERT)); - collector.checkThat(metaData.supportsConvert(JAVA_OBJECT, INTEGER), - is(EXPECTED_INVALID_SQL_SUPPORTS_CONVERT)); - collector.checkThat(metaData.supportsTableCorrelationNames(), - is(EXPECTED_SUPPORTS_TABLE_CORRELATION_NAMES)); - collector.checkThat(metaData.supportsExpressionsInOrderBy(), - is(EXPECTED_EXPRESSIONS_IN_ORDER_BY)); - collector.checkThat(metaData.supportsOrderByUnrelated(), - is(EXPECTED_SUPPORTS_ORDER_BY_UNRELATED)); + collector.checkThat( + metaData.supportsConvert(BIGINT, INTEGER), is(EXPECTED_INVALID_SQL_SUPPORTS_CONVERT)); + collector.checkThat( + metaData.supportsConvert(JAVA_OBJECT, INTEGER), is(EXPECTED_INVALID_SQL_SUPPORTS_CONVERT)); + collector.checkThat( + metaData.supportsTableCorrelationNames(), is(EXPECTED_SUPPORTS_TABLE_CORRELATION_NAMES)); + collector.checkThat( + metaData.supportsExpressionsInOrderBy(), is(EXPECTED_EXPRESSIONS_IN_ORDER_BY)); + collector.checkThat( + metaData.supportsOrderByUnrelated(), is(EXPECTED_SUPPORTS_ORDER_BY_UNRELATED)); collector.checkThat(metaData.supportsGroupBy(), is(EXPECTED_SUPPORTS_GROUP_BY)); - collector.checkThat(metaData.supportsGroupByUnrelated(), - is(EXPECTED_SUPPORTS_GROUP_BY_UNRELATED)); - collector.checkThat(metaData.supportsLikeEscapeClause(), - is(EXPECTED_SUPPORTS_LIKE_ESCAPE_CLAUSE)); + collector.checkThat( + metaData.supportsGroupByUnrelated(), is(EXPECTED_SUPPORTS_GROUP_BY_UNRELATED)); + collector.checkThat( + metaData.supportsLikeEscapeClause(), is(EXPECTED_SUPPORTS_LIKE_ESCAPE_CLAUSE)); collector.checkThat(metaData.supportsNonNullableColumns(), is(EXPECTED_NON_NULLABLE_COLUMNS)); collector.checkThat(metaData.supportsMinimumSQLGrammar(), is(EXPECTED_MINIMUM_SQL_GRAMMAR)); collector.checkThat(metaData.supportsCoreSQLGrammar(), is(EXPECTED_CORE_SQL_GRAMMAR)); collector.checkThat(metaData.supportsExtendedSQLGrammar(), is(EXPECTED_EXTEND_SQL_GRAMMAR)); - collector.checkThat(metaData.supportsANSI92EntryLevelSQL(), - is(EXPECTED_ANSI92_ENTRY_LEVEL_SQL)); - collector.checkThat(metaData.supportsANSI92IntermediateSQL(), - is(EXPECTED_ANSI92_INTERMEDIATE_SQL)); + collector.checkThat( + metaData.supportsANSI92EntryLevelSQL(), is(EXPECTED_ANSI92_ENTRY_LEVEL_SQL)); + collector.checkThat( + metaData.supportsANSI92IntermediateSQL(), is(EXPECTED_ANSI92_INTERMEDIATE_SQL)); collector.checkThat(metaData.supportsANSI92FullSQL(), is(EXPECTED_ANSI92_FULL_SQL)); collector.checkThat(metaData.supportsOuterJoins(), is(EXPECTED_SUPPORTS_OUTER_JOINS)); collector.checkThat(metaData.supportsFullOuterJoins(), is(EXPECTED_SUPPORTS_FULL_OUTER_JOINS)); @@ -829,32 +876,33 @@ public void testGetSqlInfo() throws SQLException { collector.checkThat(metaData.getProcedureTerm(), is(EXPECTED_PROCEDURE_TERM)); collector.checkThat(metaData.getCatalogTerm(), is(EXPECTED_CATALOG_TERM)); collector.checkThat(metaData.isCatalogAtStart(), is(EXPECTED_CATALOG_AT_START)); - collector.checkThat(metaData.supportsSchemasInProcedureCalls(), - is(EXPECTED_SCHEMAS_IN_PROCEDURE_CALLS)); - collector.checkThat(metaData.supportsSchemasInIndexDefinitions(), - is(EXPECTED_SCHEMAS_IN_INDEX_DEFINITIONS)); - collector.checkThat(metaData.supportsCatalogsInIndexDefinitions(), - is(EXPECTED_CATALOGS_IN_INDEX_DEFINITIONS)); + collector.checkThat( + metaData.supportsSchemasInProcedureCalls(), is(EXPECTED_SCHEMAS_IN_PROCEDURE_CALLS)); + collector.checkThat( + metaData.supportsSchemasInIndexDefinitions(), is(EXPECTED_SCHEMAS_IN_INDEX_DEFINITIONS)); + collector.checkThat( + metaData.supportsCatalogsInIndexDefinitions(), is(EXPECTED_CATALOGS_IN_INDEX_DEFINITIONS)); collector.checkThat(metaData.supportsPositionedDelete(), is(EXPECTED_POSITIONED_DELETE)); collector.checkThat(metaData.supportsPositionedUpdate(), is(EXPECTED_POSITIONED_UPDATE)); - collector.checkThat(metaData.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY), + collector.checkThat( + metaData.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY), is(EXPECTED_TYPE_FORWARD_ONLY)); - collector.checkThat(metaData.supportsSelectForUpdate(), - is(EXPECTED_SELECT_FOR_UPDATE_SUPPORTED)); - collector.checkThat(metaData.supportsStoredProcedures(), - is(EXPECTED_STORED_PROCEDURES_SUPPORTED)); - collector.checkThat(metaData.supportsSubqueriesInComparisons(), - is(EXPECTED_SUBQUERIES_IN_COMPARISON)); + collector.checkThat( + metaData.supportsSelectForUpdate(), is(EXPECTED_SELECT_FOR_UPDATE_SUPPORTED)); + collector.checkThat( + metaData.supportsStoredProcedures(), is(EXPECTED_STORED_PROCEDURES_SUPPORTED)); + collector.checkThat( + metaData.supportsSubqueriesInComparisons(), is(EXPECTED_SUBQUERIES_IN_COMPARISON)); collector.checkThat(metaData.supportsSubqueriesInExists(), is(EXPECTED_SUBQUERIES_IN_EXISTS)); collector.checkThat(metaData.supportsSubqueriesInIns(), is(EXPECTED_SUBQUERIES_IN_INS)); - collector.checkThat(metaData.supportsSubqueriesInQuantifieds(), - is(EXPECTED_SUBQUERIES_IN_QUANTIFIEDS)); - collector.checkThat(metaData.supportsCorrelatedSubqueries(), - is(EXPECTED_CORRELATED_SUBQUERIES_SUPPORTED)); + collector.checkThat( + metaData.supportsSubqueriesInQuantifieds(), is(EXPECTED_SUBQUERIES_IN_QUANTIFIEDS)); + collector.checkThat( + metaData.supportsCorrelatedSubqueries(), is(EXPECTED_CORRELATED_SUBQUERIES_SUPPORTED)); collector.checkThat(metaData.supportsUnion(), is(EXPECTED_SUPPORTS_UNION)); collector.checkThat(metaData.supportsUnionAll(), is(EXPECTED_SUPPORTS_UNION_ALL)); - collector.checkThat(metaData.getMaxBinaryLiteralLength(), - is(EXPECTED_MAX_BINARY_LITERAL_LENGTH)); + collector.checkThat( + metaData.getMaxBinaryLiteralLength(), is(EXPECTED_MAX_BINARY_LITERAL_LENGTH)); collector.checkThat(metaData.getMaxCharLiteralLength(), is(EXPECTED_MAX_CHAR_LITERAL_LENGTH)); collector.checkThat(metaData.getMaxColumnsInGroupBy(), is(EXPECTED_MAX_COLUMNS_IN_GROUP_BY)); collector.checkThat(metaData.getMaxColumnsInIndex(), is(EXPECTED_MAX_COLUMNS_IN_INDEX)); @@ -864,35 +912,40 @@ public void testGetSqlInfo() throws SQLException { collector.checkThat(metaData.getMaxCursorNameLength(), is(EXPECTED_MAX_CURSOR_NAME_LENGTH)); collector.checkThat(metaData.getMaxIndexLength(), is(EXPECTED_MAX_INDEX_LENGTH)); collector.checkThat(metaData.getMaxSchemaNameLength(), is(EXPECTED_SCHEMA_NAME_LENGTH)); - collector.checkThat(metaData.getMaxProcedureNameLength(), - is(EXPECTED_MAX_PROCEDURE_NAME_LENGTH)); + collector.checkThat( + metaData.getMaxProcedureNameLength(), is(EXPECTED_MAX_PROCEDURE_NAME_LENGTH)); collector.checkThat(metaData.getMaxCatalogNameLength(), is(EXPECTED_MAX_CATALOG_NAME_LENGTH)); collector.checkThat(metaData.getMaxRowSize(), is(EXPECTED_MAX_ROW_SIZE)); - collector.checkThat(metaData.doesMaxRowSizeIncludeBlobs(), - is(EXPECTED_MAX_ROW_SIZE_INCLUDES_BLOBS)); + collector.checkThat( + metaData.doesMaxRowSizeIncludeBlobs(), is(EXPECTED_MAX_ROW_SIZE_INCLUDES_BLOBS)); collector.checkThat(metaData.getMaxStatementLength(), is(EXPECTED_MAX_STATEMENT_LENGTH)); collector.checkThat(metaData.getMaxStatements(), is(EXPECTED_MAX_STATEMENTS)); collector.checkThat(metaData.getMaxTableNameLength(), is(EXPECTED_MAX_TABLE_NAME_LENGTH)); collector.checkThat(metaData.getMaxTablesInSelect(), is(EXPECTED_MAX_TABLES_IN_SELECT)); collector.checkThat(metaData.getMaxUserNameLength(), is(EXPECTED_MAX_USERNAME_LENGTH)); - collector.checkThat(metaData.getDefaultTransactionIsolation(), - is(EXPECTED_DEFAULT_TRANSACTION_ISOLATION)); + collector.checkThat( + metaData.getDefaultTransactionIsolation(), is(EXPECTED_DEFAULT_TRANSACTION_ISOLATION)); collector.checkThat(metaData.supportsTransactions(), is(EXPECTED_TRANSACTIONS_SUPPORTED)); collector.checkThat(metaData.supportsBatchUpdates(), is(EXPECTED_BATCH_UPDATES_SUPPORTED)); collector.checkThat(metaData.supportsSavepoints(), is(EXPECTED_SAVEPOINTS_SUPPORTED)); - collector.checkThat(metaData.supportsNamedParameters(), - is(EXPECTED_NAMED_PARAMETERS_SUPPORTED)); + collector.checkThat( + metaData.supportsNamedParameters(), is(EXPECTED_NAMED_PARAMETERS_SUPPORTED)); collector.checkThat(metaData.locatorsUpdateCopy(), is(EXPECTED_LOCATORS_UPDATE_COPY)); - collector.checkThat(metaData.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE), + collector.checkThat( + metaData.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE), is(EXPECTED_TYPE_SCROLL_INSENSITIVE)); - collector.checkThat(metaData.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE), + collector.checkThat( + metaData.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE), is(EXPECTED_TYPE_SCROLL_SENSITIVE)); - collector.checkThat(metaData.supportsSchemasInPrivilegeDefinitions(), + collector.checkThat( + metaData.supportsSchemasInPrivilegeDefinitions(), is(EXPECTED_SCHEMAS_IN_PRIVILEGE_DEFINITIONS)); - collector.checkThat(metaData.supportsCatalogsInPrivilegeDefinitions(), + collector.checkThat( + metaData.supportsCatalogsInPrivilegeDefinitions(), is(EXPECTED_CATALOGS_IN_PRIVILEGE_DEFINITIONS)); - collector.checkThat(metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_NONE), + collector.checkThat( + metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_NONE), is(EXPECTED_TRANSACTION_NONE)); collector.checkThat( metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_COMMITTED), @@ -906,27 +959,35 @@ public void testGetSqlInfo() throws SQLException { collector.checkThat( metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_SERIALIZABLE), is(EXPECTED_TRANSACTION_SERIALIZABLE)); - collector.checkThat(metaData.dataDefinitionCausesTransactionCommit(), + collector.checkThat( + metaData.dataDefinitionCausesTransactionCommit(), is(EXPECTED_DATA_DEFINITION_CAUSES_TRANSACTION_COMMIT)); - collector.checkThat(metaData.dataDefinitionIgnoredInTransactions(), + collector.checkThat( + metaData.dataDefinitionIgnoredInTransactions(), is(EXPECTED_DATA_DEFINITIONS_IN_TRANSACTIONS_IGNORED)); - collector.checkThat(metaData.supportsStoredFunctionsUsingCallSyntax(), + collector.checkThat( + metaData.supportsStoredFunctionsUsingCallSyntax(), is(EXPECTED_STORED_FUNCTIONS_USING_CALL_SYNTAX_SUPPORTED)); - collector.checkThat(metaData.supportsIntegrityEnhancementFacility(), + collector.checkThat( + metaData.supportsIntegrityEnhancementFacility(), is(EXPECTED_SUPPORTS_INTEGRITY_ENHANCEMENT_FACILITY)); - collector.checkThat(metaData.supportsDifferentTableCorrelationNames(), + collector.checkThat( + metaData.supportsDifferentTableCorrelationNames(), is(EXPECTED_SUPPORTS_DIFFERENT_TABLE_CORRELATION_NAMES)); - ThrowableAssertionUtils.simpleAssertThrowableClass(SQLException.class, + ThrowableAssertionUtils.simpleAssertThrowableClass( + SQLException.class, () -> metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_SERIALIZABLE + 1)); - ThrowableAssertionUtils.simpleAssertThrowableClass(SQLException.class, + ThrowableAssertionUtils.simpleAssertThrowableClass( + SQLException.class, () -> metaData.supportsResultSetType(ResultSet.HOLD_CURSORS_OVER_COMMIT)); } @Test public void testGetColumnsCanBeAccessedByNames() throws SQLException { try (final ResultSet resultSet = connection.getMetaData().getColumns(null, null, null, null)) { - resultSetTestUtils.testData(resultSet, + resultSetTestUtils.testData( + resultSet, ImmutableList.of( "TABLE_CAT", "TABLE_SCHEM", @@ -952,8 +1013,7 @@ public void testGetColumnsCanBeAccessedByNames() throws SQLException { "SOURCE_DATA_TYPE", "IS_AUTOINCREMENT", "IS_GENERATEDCOLUMN"), - EXPECTED_GET_COLUMNS_RESULTS - ); + EXPECTED_GET_COLUMNS_RESULTS); } } @@ -961,27 +1021,28 @@ public void testGetColumnsCanBeAccessedByNames() throws SQLException { public void testGetProcedures() throws SQLException { try (final ResultSet resultSet = connection.getMetaData().getProcedures(null, null, null)) { // Maps ordinal index to column name according to JDBC documentation - final Map expectedGetProceduresSchema = new HashMap() { - { - put(1, "PROCEDURE_CAT"); - put(2, "PROCEDURE_SCHEM"); - put(3, "PROCEDURE_NAME"); - put(4, "FUTURE_USE1"); - put(5, "FUTURE_USE2"); - put(6, "FUTURE_USE3"); - put(7, "REMARKS"); - put(8, "PROCEDURE_TYPE"); - put(9, "SPECIFIC_NAME"); - } - }; + final Map expectedGetProceduresSchema = + new HashMap() { + { + put(1, "PROCEDURE_CAT"); + put(2, "PROCEDURE_SCHEM"); + put(3, "PROCEDURE_NAME"); + put(4, "FUTURE_USE1"); + put(5, "FUTURE_USE2"); + put(6, "FUTURE_USE3"); + put(7, "REMARKS"); + put(8, "PROCEDURE_TYPE"); + put(9, "SPECIFIC_NAME"); + } + }; testEmptyResultSet(resultSet, expectedGetProceduresSchema); } } @Test public void testGetProcedureColumns() throws SQLException { - try (ResultSet resultSet = connection.getMetaData() - .getProcedureColumns(null, null, null, null)) { + try (ResultSet resultSet = + connection.getMetaData().getProcedureColumns(null, null, null, null)) { // Maps ordinal index to column name according to JDBC documentation final Map expectedGetProcedureColumnsSchema = new HashMap() { @@ -1014,8 +1075,8 @@ public void testGetProcedureColumns() throws SQLException { @Test public void testGetColumnPrivileges() throws SQLException { - try (ResultSet resultSet = connection.getMetaData() - .getColumnPrivileges(null, null, null, null)) { + try (ResultSet resultSet = + connection.getMetaData().getColumnPrivileges(null, null, null, null)) { // Maps ordinal index to column name according to JDBC documentation final Map expectedGetColumnPrivilegesSchema = new HashMap() { @@ -1038,25 +1099,26 @@ public void testGetColumnPrivileges() throws SQLException { public void testGetTablePrivileges() throws SQLException { try (ResultSet resultSet = connection.getMetaData().getTablePrivileges(null, null, null)) { // Maps ordinal index to column name according to JDBC documentation - final Map expectedGetTablePrivilegesSchema = new HashMap() { - { - put(1, "TABLE_CAT"); - put(2, "TABLE_SCHEM"); - put(3, "TABLE_NAME"); - put(4, "GRANTOR"); - put(5, "GRANTEE"); - put(6, "PRIVILEGE"); - put(7, "IS_GRANTABLE"); - } - }; + final Map expectedGetTablePrivilegesSchema = + new HashMap() { + { + put(1, "TABLE_CAT"); + put(2, "TABLE_SCHEM"); + put(3, "TABLE_NAME"); + put(4, "GRANTOR"); + put(5, "GRANTEE"); + put(6, "PRIVILEGE"); + put(7, "IS_GRANTABLE"); + } + }; testEmptyResultSet(resultSet, expectedGetTablePrivilegesSchema); } } @Test public void testGetBestRowIdentifier() throws SQLException { - try (ResultSet resultSet = connection.getMetaData() - .getBestRowIdentifier(null, null, null, 0, true)) { + try (ResultSet resultSet = + connection.getMetaData().getBestRowIdentifier(null, null, null, 0, true)) { // Maps ordinal index to column name according to JDBC documentation final Map expectedGetBestRowIdentifierSchema = new HashMap() { @@ -1079,18 +1141,19 @@ public void testGetBestRowIdentifier() throws SQLException { public void testGetVersionColumns() throws SQLException { try (ResultSet resultSet = connection.getMetaData().getVersionColumns(null, null, null)) { // Maps ordinal index to column name according to JDBC documentation - final Map expectedGetVersionColumnsSchema = new HashMap() { - { - put(1, "SCOPE"); - put(2, "COLUMN_NAME"); - put(3, "DATA_TYPE"); - put(4, "TYPE_NAME"); - put(5, "COLUMN_SIZE"); - put(6, "BUFFER_LENGTH"); - put(7, "DECIMAL_DIGITS"); - put(8, "PSEUDO_COLUMN"); - } - }; + final Map expectedGetVersionColumnsSchema = + new HashMap() { + { + put(1, "SCOPE"); + put(2, "COLUMN_NAME"); + put(3, "DATA_TYPE"); + put(4, "TYPE_NAME"); + put(5, "COLUMN_SIZE"); + put(6, "BUFFER_LENGTH"); + put(7, "DECIMAL_DIGITS"); + put(8, "PSEUDO_COLUMN"); + } + }; testEmptyResultSet(resultSet, expectedGetVersionColumnsSchema); } } @@ -1099,54 +1162,56 @@ public void testGetVersionColumns() throws SQLException { public void testGetTypeInfo() throws SQLException { try (ResultSet resultSet = connection.getMetaData().getTypeInfo()) { // Maps ordinal index to column name according to JDBC documentation - final Map expectedGetTypeInfoSchema = new HashMap() { - { - put(1, "TYPE_NAME"); - put(2, "DATA_TYPE"); - put(3, "PRECISION"); - put(4, "LITERAL_PREFIX"); - put(5, "LITERAL_SUFFIX"); - put(6, "CREATE_PARAMS"); - put(7, "NULLABLE"); - put(8, "CASE_SENSITIVE"); - put(9, "SEARCHABLE"); - put(10, "UNSIGNED_ATTRIBUTE"); - put(11, "FIXED_PREC_SCALE"); - put(12, "AUTO_INCREMENT"); - put(13, "LOCAL_TYPE_NAME"); - put(14, "MINIMUM_SCALE"); - put(15, "MAXIMUM_SCALE"); - put(16, "SQL_DATA_TYPE"); - put(17, "SQL_DATETIME_SUB"); - put(18, "NUM_PREC_RADIX"); - } - }; + final Map expectedGetTypeInfoSchema = + new HashMap() { + { + put(1, "TYPE_NAME"); + put(2, "DATA_TYPE"); + put(3, "PRECISION"); + put(4, "LITERAL_PREFIX"); + put(5, "LITERAL_SUFFIX"); + put(6, "CREATE_PARAMS"); + put(7, "NULLABLE"); + put(8, "CASE_SENSITIVE"); + put(9, "SEARCHABLE"); + put(10, "UNSIGNED_ATTRIBUTE"); + put(11, "FIXED_PREC_SCALE"); + put(12, "AUTO_INCREMENT"); + put(13, "LOCAL_TYPE_NAME"); + put(14, "MINIMUM_SCALE"); + put(15, "MAXIMUM_SCALE"); + put(16, "SQL_DATA_TYPE"); + put(17, "SQL_DATETIME_SUB"); + put(18, "NUM_PREC_RADIX"); + } + }; testEmptyResultSet(resultSet, expectedGetTypeInfoSchema); } } @Test public void testGetIndexInfo() throws SQLException { - try (ResultSet resultSet = connection.getMetaData() - .getIndexInfo(null, null, null, false, true)) { + try (ResultSet resultSet = + connection.getMetaData().getIndexInfo(null, null, null, false, true)) { // Maps ordinal index to column name according to JDBC documentation - final Map expectedGetIndexInfoSchema = new HashMap() { - { - put(1, "TABLE_CAT"); - put(2, "TABLE_SCHEM"); - put(3, "TABLE_NAME"); - put(4, "NON_UNIQUE"); - put(5, "INDEX_QUALIFIER"); - put(6, "INDEX_NAME"); - put(7, "TYPE"); - put(8, "ORDINAL_POSITION"); - put(9, "COLUMN_NAME"); - put(10, "ASC_OR_DESC"); - put(11, "CARDINALITY"); - put(12, "PAGES"); - put(13, "FILTER_CONDITION"); - } - }; + final Map expectedGetIndexInfoSchema = + new HashMap() { + { + put(1, "TABLE_CAT"); + put(2, "TABLE_SCHEM"); + put(3, "TABLE_NAME"); + put(4, "NON_UNIQUE"); + put(5, "INDEX_QUALIFIER"); + put(6, "INDEX_NAME"); + put(7, "TYPE"); + put(8, "ORDINAL_POSITION"); + put(9, "COLUMN_NAME"); + put(10, "ASC_OR_DESC"); + put(11, "CARDINALITY"); + put(12, "PAGES"); + put(13, "FILTER_CONDITION"); + } + }; testEmptyResultSet(resultSet, expectedGetIndexInfoSchema); } } @@ -1155,17 +1220,18 @@ public void testGetIndexInfo() throws SQLException { public void testGetUDTs() throws SQLException { try (ResultSet resultSet = connection.getMetaData().getUDTs(null, null, null, null)) { // Maps ordinal index to column name according to JDBC documentation - final Map expectedGetUDTsSchema = new HashMap() { - { - put(1, "TYPE_CAT"); - put(2, "TYPE_SCHEM"); - put(3, "TYPE_NAME"); - put(4, "CLASS_NAME"); - put(5, "DATA_TYPE"); - put(6, "REMARKS"); - put(7, "BASE_TYPE"); - } - }; + final Map expectedGetUDTsSchema = + new HashMap() { + { + put(1, "TYPE_CAT"); + put(2, "TYPE_SCHEM"); + put(3, "TYPE_NAME"); + put(4, "CLASS_NAME"); + put(5, "DATA_TYPE"); + put(6, "REMARKS"); + put(7, "BASE_TYPE"); + } + }; testEmptyResultSet(resultSet, expectedGetUDTsSchema); } } @@ -1174,16 +1240,17 @@ public void testGetUDTs() throws SQLException { public void testGetSuperTypes() throws SQLException { try (ResultSet resultSet = connection.getMetaData().getSuperTypes(null, null, null)) { // Maps ordinal index to column name according to JDBC documentation - final Map expectedGetSuperTypesSchema = new HashMap() { - { - put(1, "TYPE_CAT"); - put(2, "TYPE_SCHEM"); - put(3, "TYPE_NAME"); - put(4, "SUPERTYPE_CAT"); - put(5, "SUPERTYPE_SCHEM"); - put(6, "SUPERTYPE_NAME"); - } - }; + final Map expectedGetSuperTypesSchema = + new HashMap() { + { + put(1, "TYPE_CAT"); + put(2, "TYPE_SCHEM"); + put(3, "TYPE_NAME"); + put(4, "SUPERTYPE_CAT"); + put(5, "SUPERTYPE_SCHEM"); + put(6, "SUPERTYPE_NAME"); + } + }; testEmptyResultSet(resultSet, expectedGetSuperTypesSchema); } } @@ -1192,14 +1259,15 @@ public void testGetSuperTypes() throws SQLException { public void testGetSuperTables() throws SQLException { try (ResultSet resultSet = connection.getMetaData().getSuperTables(null, null, null)) { // Maps ordinal index to column name according to JDBC documentation - final Map expectedGetSuperTablesSchema = new HashMap() { - { - put(1, "TABLE_CAT"); - put(2, "TABLE_SCHEM"); - put(3, "TABLE_NAME"); - put(4, "SUPERTABLE_NAME"); - } - }; + final Map expectedGetSuperTablesSchema = + new HashMap() { + { + put(1, "TABLE_CAT"); + put(2, "TABLE_SCHEM"); + put(3, "TABLE_NAME"); + put(4, "SUPERTABLE_NAME"); + } + }; testEmptyResultSet(resultSet, expectedGetSuperTablesSchema); } } @@ -1208,31 +1276,32 @@ public void testGetSuperTables() throws SQLException { public void testGetAttributes() throws SQLException { try (ResultSet resultSet = connection.getMetaData().getAttributes(null, null, null, null)) { // Maps ordinal index to column name according to JDBC documentation - final Map expectedGetAttributesSchema = new HashMap() { - { - put(1, "TYPE_CAT"); - put(2, "TYPE_SCHEM"); - put(3, "TYPE_NAME"); - put(4, "ATTR_NAME"); - put(5, "DATA_TYPE"); - put(6, "ATTR_TYPE_NAME"); - put(7, "ATTR_SIZE"); - put(8, "DECIMAL_DIGITS"); - put(9, "NUM_PREC_RADIX"); - put(10, "NULLABLE"); - put(11, "REMARKS"); - put(12, "ATTR_DEF"); - put(13, "SQL_DATA_TYPE"); - put(14, "SQL_DATETIME_SUB"); - put(15, "CHAR_OCTET_LENGTH"); - put(16, "ORDINAL_POSITION"); - put(17, "IS_NULLABLE"); - put(18, "SCOPE_CATALOG"); - put(19, "SCOPE_SCHEMA"); - put(20, "SCOPE_TABLE"); - put(21, "SOURCE_DATA_TYPE"); - } - }; + final Map expectedGetAttributesSchema = + new HashMap() { + { + put(1, "TYPE_CAT"); + put(2, "TYPE_SCHEM"); + put(3, "TYPE_NAME"); + put(4, "ATTR_NAME"); + put(5, "DATA_TYPE"); + put(6, "ATTR_TYPE_NAME"); + put(7, "ATTR_SIZE"); + put(8, "DECIMAL_DIGITS"); + put(9, "NUM_PREC_RADIX"); + put(10, "NULLABLE"); + put(11, "REMARKS"); + put(12, "ATTR_DEF"); + put(13, "SQL_DATA_TYPE"); + put(14, "SQL_DATETIME_SUB"); + put(15, "CHAR_OCTET_LENGTH"); + put(16, "ORDINAL_POSITION"); + put(17, "IS_NULLABLE"); + put(18, "SCOPE_CATALOG"); + put(19, "SCOPE_SCHEMA"); + put(20, "SCOPE_TABLE"); + put(21, "SOURCE_DATA_TYPE"); + } + }; testEmptyResultSet(resultSet, expectedGetAttributesSchema); } } @@ -1258,46 +1327,48 @@ public void testGetClientInfoProperties() throws SQLException { public void testGetFunctions() throws SQLException { try (ResultSet resultSet = connection.getMetaData().getFunctions(null, null, null)) { // Maps ordinal index to column name according to JDBC documentation - final Map expectedGetFunctionsSchema = new HashMap() { - { - put(1, "FUNCTION_CAT"); - put(2, "FUNCTION_SCHEM"); - put(3, "FUNCTION_NAME"); - put(4, "REMARKS"); - put(5, "FUNCTION_TYPE"); - put(6, "SPECIFIC_NAME"); - } - }; + final Map expectedGetFunctionsSchema = + new HashMap() { + { + put(1, "FUNCTION_CAT"); + put(2, "FUNCTION_SCHEM"); + put(3, "FUNCTION_NAME"); + put(4, "REMARKS"); + put(5, "FUNCTION_TYPE"); + put(6, "SPECIFIC_NAME"); + } + }; testEmptyResultSet(resultSet, expectedGetFunctionsSchema); } } @Test public void testGetFunctionColumns() throws SQLException { - try ( - ResultSet resultSet = connection.getMetaData().getFunctionColumns(null, null, null, null)) { + try (ResultSet resultSet = + connection.getMetaData().getFunctionColumns(null, null, null, null)) { // Maps ordinal index to column name according to JDBC documentation - final Map expectedGetFunctionColumnsSchema = new HashMap() { - { - put(1, "FUNCTION_CAT"); - put(2, "FUNCTION_SCHEM"); - put(3, "FUNCTION_NAME"); - put(4, "COLUMN_NAME"); - put(5, "COLUMN_TYPE"); - put(6, "DATA_TYPE"); - put(7, "TYPE_NAME"); - put(8, "PRECISION"); - put(9, "LENGTH"); - put(10, "SCALE"); - put(11, "RADIX"); - put(12, "NULLABLE"); - put(13, "REMARKS"); - put(14, "CHAR_OCTET_LENGTH"); - put(15, "ORDINAL_POSITION"); - put(16, "IS_NULLABLE"); - put(17, "SPECIFIC_NAME"); - } - }; + final Map expectedGetFunctionColumnsSchema = + new HashMap() { + { + put(1, "FUNCTION_CAT"); + put(2, "FUNCTION_SCHEM"); + put(3, "FUNCTION_NAME"); + put(4, "COLUMN_NAME"); + put(5, "COLUMN_TYPE"); + put(6, "DATA_TYPE"); + put(7, "TYPE_NAME"); + put(8, "PRECISION"); + put(9, "LENGTH"); + put(10, "SCALE"); + put(11, "RADIX"); + put(12, "NULLABLE"); + put(13, "REMARKS"); + put(14, "CHAR_OCTET_LENGTH"); + put(15, "ORDINAL_POSITION"); + put(16, "IS_NULLABLE"); + put(17, "SPECIFIC_NAME"); + } + }; testEmptyResultSet(resultSet, expectedGetFunctionColumnsSchema); } } @@ -1306,28 +1377,29 @@ public void testGetFunctionColumns() throws SQLException { public void testGetPseudoColumns() throws SQLException { try (ResultSet resultSet = connection.getMetaData().getPseudoColumns(null, null, null, null)) { // Maps ordinal index to column name according to JDBC documentation - final Map expectedGetPseudoColumnsSchema = new HashMap() { - { - put(1, "TABLE_CAT"); - put(2, "TABLE_SCHEM"); - put(3, "TABLE_NAME"); - put(4, "COLUMN_NAME"); - put(5, "DATA_TYPE"); - put(6, "COLUMN_SIZE"); - put(7, "DECIMAL_DIGITS"); - put(8, "NUM_PREC_RADIX"); - put(9, "COLUMN_USAGE"); - put(10, "REMARKS"); - put(11, "CHAR_OCTET_LENGTH"); - put(12, "IS_NULLABLE"); - } - }; + final Map expectedGetPseudoColumnsSchema = + new HashMap() { + { + put(1, "TABLE_CAT"); + put(2, "TABLE_SCHEM"); + put(3, "TABLE_NAME"); + put(4, "COLUMN_NAME"); + put(5, "DATA_TYPE"); + put(6, "COLUMN_SIZE"); + put(7, "DECIMAL_DIGITS"); + put(8, "NUM_PREC_RADIX"); + put(9, "COLUMN_USAGE"); + put(10, "REMARKS"); + put(11, "CHAR_OCTET_LENGTH"); + put(12, "IS_NULLABLE"); + } + }; testEmptyResultSet(resultSet, expectedGetPseudoColumnsSchema); } } - private void testEmptyResultSet(final ResultSet resultSet, - final Map expectedResultSetSchema) + private void testEmptyResultSet( + final ResultSet resultSet, final Map expectedResultSetSchema) throws SQLException { Assert.assertFalse(resultSet.next()); final ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); @@ -1338,76 +1410,102 @@ private void testEmptyResultSet(final ResultSet resultSet, @Test public void testGetColumnSize() { - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_BYTE), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_BYTE), ArrowDatabaseMetadata.getColumnSize(new ArrowType.Int(Byte.SIZE, true))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_SHORT), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_SHORT), ArrowDatabaseMetadata.getColumnSize(new ArrowType.Int(Short.SIZE, true))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_INT), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_INT), ArrowDatabaseMetadata.getColumnSize(new ArrowType.Int(Integer.SIZE, true))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_LONG), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_LONG), ArrowDatabaseMetadata.getColumnSize(new ArrowType.Int(Long.SIZE, true))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_VARCHAR_AND_BINARY), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_VARCHAR_AND_BINARY), ArrowDatabaseMetadata.getColumnSize(new ArrowType.Utf8())); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_VARCHAR_AND_BINARY), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_VARCHAR_AND_BINARY), ArrowDatabaseMetadata.getColumnSize(new ArrowType.Binary())); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIMESTAMP_SECONDS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIMESTAMP_SECONDS), ArrowDatabaseMetadata.getColumnSize(new ArrowType.Timestamp(TimeUnit.SECOND, null))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIMESTAMP_MILLISECONDS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIMESTAMP_MILLISECONDS), ArrowDatabaseMetadata.getColumnSize(new ArrowType.Timestamp(TimeUnit.MILLISECOND, null))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIMESTAMP_MICROSECONDS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIMESTAMP_MICROSECONDS), ArrowDatabaseMetadata.getColumnSize(new ArrowType.Timestamp(TimeUnit.MICROSECOND, null))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIMESTAMP_NANOSECONDS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIMESTAMP_NANOSECONDS), ArrowDatabaseMetadata.getColumnSize(new ArrowType.Timestamp(TimeUnit.NANOSECOND, null))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIME), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIME), ArrowDatabaseMetadata.getColumnSize(new ArrowType.Time(TimeUnit.SECOND, Integer.SIZE))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIME_MILLISECONDS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIME_MILLISECONDS), ArrowDatabaseMetadata.getColumnSize( new ArrowType.Time(TimeUnit.MILLISECOND, Integer.SIZE))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIME_MICROSECONDS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIME_MICROSECONDS), ArrowDatabaseMetadata.getColumnSize( new ArrowType.Time(TimeUnit.MICROSECOND, Integer.SIZE))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIME_NANOSECONDS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_TIME_NANOSECONDS), ArrowDatabaseMetadata.getColumnSize(new ArrowType.Time(TimeUnit.NANOSECOND, Integer.SIZE))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_DATE), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.COLUMN_SIZE_DATE), ArrowDatabaseMetadata.getColumnSize(new ArrowType.Date(DateUnit.DAY))); - Assert.assertNull(ArrowDatabaseMetadata.getColumnSize(new ArrowType.FloatingPoint( - FloatingPointPrecision.DOUBLE))); + Assert.assertNull( + ArrowDatabaseMetadata.getColumnSize( + new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE))); } @Test public void testGetDecimalDigits() { - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.NO_DECIMAL_DIGITS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.NO_DECIMAL_DIGITS), ArrowDatabaseMetadata.getDecimalDigits(new ArrowType.Int(Byte.SIZE, true))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.NO_DECIMAL_DIGITS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.NO_DECIMAL_DIGITS), ArrowDatabaseMetadata.getDecimalDigits(new ArrowType.Timestamp(TimeUnit.SECOND, null))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.DECIMAL_DIGITS_TIME_MILLISECONDS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.DECIMAL_DIGITS_TIME_MILLISECONDS), ArrowDatabaseMetadata.getDecimalDigits( new ArrowType.Timestamp(TimeUnit.MILLISECOND, null))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.DECIMAL_DIGITS_TIME_MICROSECONDS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.DECIMAL_DIGITS_TIME_MICROSECONDS), ArrowDatabaseMetadata.getDecimalDigits( new ArrowType.Timestamp(TimeUnit.MICROSECOND, null))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.DECIMAL_DIGITS_TIME_NANOSECONDS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.DECIMAL_DIGITS_TIME_NANOSECONDS), ArrowDatabaseMetadata.getDecimalDigits(new ArrowType.Timestamp(TimeUnit.NANOSECOND, null))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.NO_DECIMAL_DIGITS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.NO_DECIMAL_DIGITS), ArrowDatabaseMetadata.getDecimalDigits(new ArrowType.Time(TimeUnit.SECOND, Integer.SIZE))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.DECIMAL_DIGITS_TIME_MILLISECONDS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.DECIMAL_DIGITS_TIME_MILLISECONDS), ArrowDatabaseMetadata.getDecimalDigits( new ArrowType.Time(TimeUnit.MILLISECOND, Integer.SIZE))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.DECIMAL_DIGITS_TIME_MICROSECONDS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.DECIMAL_DIGITS_TIME_MICROSECONDS), ArrowDatabaseMetadata.getDecimalDigits( new ArrowType.Time(TimeUnit.MICROSECOND, Integer.SIZE))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.DECIMAL_DIGITS_TIME_NANOSECONDS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.DECIMAL_DIGITS_TIME_NANOSECONDS), ArrowDatabaseMetadata.getDecimalDigits( new ArrowType.Time(TimeUnit.NANOSECOND, Integer.SIZE))); - Assert.assertEquals(Integer.valueOf(ArrowDatabaseMetadata.NO_DECIMAL_DIGITS), + Assert.assertEquals( + Integer.valueOf(ArrowDatabaseMetadata.NO_DECIMAL_DIGITS), ArrowDatabaseMetadata.getDecimalDigits(new ArrowType.Date(DateUnit.DAY))); Assert.assertNull(ArrowDatabaseMetadata.getDecimalDigits(new ArrowType.Utf8())); diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcArrayTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcArrayTest.java index 76f01514c95..21b7fb3f0f8 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcArrayTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcArrayTest.java @@ -23,7 +23,6 @@ import java.sql.Types; import java.util.Arrays; import java.util.HashMap; - import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; import org.apache.arrow.vector.IntVector; import org.apache.arrow.vector.util.JsonStringArrayList; @@ -38,8 +37,7 @@ @RunWith(MockitoJUnitRunner.class) public class ArrowFlightJdbcArrayTest { - @Rule - public RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); + @Rule public RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); IntVector dataVector; diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcConnectionCookieTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcConnectionCookieTest.java index c7268e0594e..a5224eeb13d 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcConnectionCookieTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcConnectionCookieTest.java @@ -20,7 +20,6 @@ import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; - import org.apache.arrow.driver.jdbc.utils.CoreMockedSqlProducers; import org.junit.Assert; import org.junit.ClassRule; @@ -30,8 +29,7 @@ public class ArrowFlightJdbcConnectionCookieTest { - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); @ClassRule public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE = @@ -40,7 +38,7 @@ public class ArrowFlightJdbcConnectionCookieTest { @Test public void testCookies() throws SQLException { try (Connection connection = FLIGHT_SERVER_TEST_RULE.getConnection(false); - Statement statement = connection.createStatement()) { + Statement statement = connection.createStatement()) { // Expect client didn't receive cookies before any operation Assert.assertNull(FLIGHT_SERVER_TEST_RULE.getMiddlewareCookieFactory().getCookie()); @@ -51,4 +49,3 @@ public void testCookies() throws SQLException { } } } - diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcConnectionPoolDataSourceTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcConnectionPoolDataSourceTest.java index bdf2826c41e..f558e13a1fe 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcConnectionPoolDataSourceTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcConnectionPoolDataSourceTest.java @@ -18,9 +18,7 @@ package org.apache.arrow.driver.jdbc; import java.sql.Connection; - import javax.sql.PooledConnection; - import org.apache.arrow.driver.jdbc.authentication.UserPasswordAuthentication; import org.apache.arrow.driver.jdbc.utils.ConnectionWrapper; import org.apache.arrow.driver.jdbc.utils.MockFlightSqlProducer; @@ -31,8 +29,7 @@ import org.junit.Test; public class ArrowFlightJdbcConnectionPoolDataSourceTest { - @ClassRule - public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE; + @ClassRule public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE; private static final MockFlightSqlProducer PRODUCER = new MockFlightSqlProducer(); @@ -43,10 +40,11 @@ public class ArrowFlightJdbcConnectionPoolDataSourceTest { .user("user2", "pass2") .build(); - FLIGHT_SERVER_TEST_RULE = new FlightServerTestRule.Builder() - .authentication(authentication) - .producer(PRODUCER) - .build(); + FLIGHT_SERVER_TEST_RULE = + new FlightServerTestRule.Builder() + .authentication(authentication) + .producer(PRODUCER) + .build(); } private ArrowFlightJdbcConnectionPoolDataSource dataSource; @@ -107,7 +105,8 @@ public void testShouldReuseConnectionsOnPool() throws Exception { Assert.assertSame(pooledConnection, pooledConnection2); Assert.assertNotSame(connection, connection2); - Assert.assertSame(connection.unwrap(ArrowFlightConnection.class), + Assert.assertSame( + connection.unwrap(ArrowFlightConnection.class), connection2.unwrap(ArrowFlightConnection.class)); } @@ -129,7 +128,8 @@ public void testShouldNotMixConnectionsForDifferentUsers() throws Exception { Assert.assertNotSame(pooledConnection, pooledConnection2); Assert.assertNotSame(connection, connection2); - Assert.assertNotSame(connection.unwrap(ArrowFlightConnection.class), + Assert.assertNotSame( + connection.unwrap(ArrowFlightConnection.class), connection2.unwrap(ArrowFlightConnection.class)); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcCursorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcCursorTest.java index b818f7115b7..ffb08ce3fac 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcCursorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcCursorTest.java @@ -19,10 +19,10 @@ import static org.junit.Assert.assertTrue; +import com.google.common.collect.ImmutableList; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.BitVector; @@ -55,11 +55,7 @@ import org.junit.After; import org.junit.Test; -import com.google.common.collect.ImmutableList; - -/** - * Tests for {@link ArrowFlightJdbcCursor}. - */ +/** Tests for {@link ArrowFlightJdbcCursor}. */ public class ArrowFlightJdbcCursorTest { ArrowFlightJdbcCursor cursor; @@ -88,44 +84,42 @@ public void testDateVectorNullTrue() throws SQLException { @Test public void testDurationVectorNullTrue() throws SQLException { - final VectorSchemaRoot root = getVectorSchemaRoot("Duration", - new ArrowType.Duration(TimeUnit.MILLISECOND), null); + final VectorSchemaRoot root = + getVectorSchemaRoot("Duration", new ArrowType.Duration(TimeUnit.MILLISECOND), null); ((DurationVector) root.getVector("Duration")).setNull(0); testCursorWasNull(root); } @Test public void testDateInternalNullTrue() throws SQLException { - final VectorSchemaRoot root = getVectorSchemaRoot("Interval", - new ArrowType.Interval(IntervalUnit.DAY_TIME), null); + final VectorSchemaRoot root = + getVectorSchemaRoot("Interval", new ArrowType.Interval(IntervalUnit.DAY_TIME), null); ((IntervalDayVector) root.getVector("Interval")).setNull(0); testCursorWasNull(root); } @Test public void testTimeStampVectorNullTrue() throws SQLException { - final VectorSchemaRoot root = getVectorSchemaRoot("TimeStamp", - new ArrowType.Timestamp(TimeUnit.MILLISECOND, null), null); + final VectorSchemaRoot root = + getVectorSchemaRoot("TimeStamp", new ArrowType.Timestamp(TimeUnit.MILLISECOND, null), null); ((TimeStampMilliVector) root.getVector("TimeStamp")).setNull(0); testCursorWasNull(root); } @Test public void testTimeVectorNullTrue() throws SQLException { - final VectorSchemaRoot root = getVectorSchemaRoot("Time", - new ArrowType.Time(TimeUnit.MILLISECOND, 32), null); + final VectorSchemaRoot root = + getVectorSchemaRoot("Time", new ArrowType.Time(TimeUnit.MILLISECOND, 32), null); ((TimeMilliVector) root.getVector("Time")).setNull(0); testCursorWasNull(root); - } @Test public void testFixedSizeListVectorNullTrue() throws SQLException { List fieldList = new ArrayList<>(); - fieldList.add(new Field("Null", new FieldType(true, new ArrowType.Null(), null), - null)); - final VectorSchemaRoot root = getVectorSchemaRoot("FixedSizeList", - new ArrowType.FixedSizeList(10), fieldList); + fieldList.add(new Field("Null", new FieldType(true, new ArrowType.Null(), null), null)); + final VectorSchemaRoot root = + getVectorSchemaRoot("FixedSizeList", new ArrowType.FixedSizeList(10), fieldList); ((FixedSizeListVector) root.getVector("FixedSizeList")).setNull(0); testCursorWasNull(root); } @@ -133,8 +127,7 @@ public void testFixedSizeListVectorNullTrue() throws SQLException { @Test public void testLargeListVectorNullTrue() throws SQLException { List fieldList = new ArrayList<>(); - fieldList.add(new Field("Null", new FieldType(true, new ArrowType.Null(), null), - null)); + fieldList.add(new Field("Null", new FieldType(true, new ArrowType.Null(), null), null)); final VectorSchemaRoot root = getVectorSchemaRoot("LargeList", new ArrowType.LargeList(), fieldList); ((LargeListVector) root.getVector("LargeList")).setNull(0); @@ -144,8 +137,7 @@ public void testLargeListVectorNullTrue() throws SQLException { @Test public void testListVectorNullTrue() throws SQLException { List fieldList = new ArrayList<>(); - fieldList.add(new Field("Null", new FieldType(true, new ArrowType.Null(), null), - null)); + fieldList.add(new Field("Null", new FieldType(true, new ArrowType.Null(), null), null)); final VectorSchemaRoot root = getVectorSchemaRoot("List", new ArrowType.List(), fieldList); ((ListVector) root.getVector("List")).setNull(0); testCursorWasNull(root); @@ -154,13 +146,11 @@ public void testListVectorNullTrue() throws SQLException { @Test public void testMapVectorNullTrue() throws SQLException { List structChildren = new ArrayList<>(); - structChildren.add(new Field("Key", new FieldType(false, new ArrowType.Utf8(), null), - null)); - structChildren.add(new Field("Value", new FieldType(false, new ArrowType.Utf8(), null), - null)); + structChildren.add(new Field("Key", new FieldType(false, new ArrowType.Utf8(), null), null)); + structChildren.add(new Field("Value", new FieldType(false, new ArrowType.Utf8(), null), null)); List fieldList = new ArrayList<>(); - fieldList.add(new Field("Struct", new FieldType(false, new ArrowType.Struct(), null), - structChildren)); + fieldList.add( + new Field("Struct", new FieldType(false, new ArrowType.Struct(), null), structChildren)); final VectorSchemaRoot root = getVectorSchemaRoot("Map", new ArrowType.Map(false), fieldList); ((MapVector) root.getVector("Map")).setNull(0); testCursorWasNull(root); @@ -175,8 +165,8 @@ public void testStructVectorNullTrue() throws SQLException { @Test public void testBaseIntVectorNullTrue() throws SQLException { - final VectorSchemaRoot root = getVectorSchemaRoot("BaseInt", - new ArrowType.Int(32, false), null); + final VectorSchemaRoot root = + getVectorSchemaRoot("BaseInt", new ArrowType.Int(32, false), null); ((UInt4Vector) root.getVector("BaseInt")).setNull(0); testCursorWasNull(root); } @@ -190,24 +180,26 @@ public void testBitVectorNullTrue() throws SQLException { @Test public void testDecimalVectorNullTrue() throws SQLException { - final VectorSchemaRoot root = getVectorSchemaRoot("Decimal", - new ArrowType.Decimal(2, 2, 128), null); + final VectorSchemaRoot root = + getVectorSchemaRoot("Decimal", new ArrowType.Decimal(2, 2, 128), null); ((DecimalVector) root.getVector("Decimal")).setNull(0); testCursorWasNull(root); } @Test public void testFloat4VectorNullTrue() throws SQLException { - final VectorSchemaRoot root = getVectorSchemaRoot("Float4", - new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE), null); + final VectorSchemaRoot root = + getVectorSchemaRoot( + "Float4", new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE), null); ((Float4Vector) root.getVector("Float4")).setNull(0); testCursorWasNull(root); } @Test public void testFloat8VectorNullTrue() throws SQLException { - final VectorSchemaRoot root = getVectorSchemaRoot("Float8", - new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE), null); + final VectorSchemaRoot root = + getVectorSchemaRoot( + "Float8", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE), null); ((Float8Vector) root.getVector("Float8")).setNull(0); testCursorWasNull(root); } @@ -225,14 +217,11 @@ public void testNullVectorNullTrue() throws SQLException { testCursorWasNull(root); } - private VectorSchemaRoot getVectorSchemaRoot(String name, ArrowType arrowType, - List children) { - final Schema schema = new Schema(ImmutableList.of( - new Field( - name, - new FieldType(true, arrowType, - null), - children))); + private VectorSchemaRoot getVectorSchemaRoot( + String name, ArrowType arrowType, List children) { + final Schema schema = + new Schema( + ImmutableList.of(new Field(name, new FieldType(true, arrowType, null), children))); allocator = new RootAllocator(Long.MAX_VALUE); final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); root.allocateNew(); diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcDriverTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcDriverTest.java index e1f64c9dd87..44917dc00f5 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcDriverTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcDriverTest.java @@ -30,7 +30,6 @@ import java.util.Collection; import java.util.Map; import java.util.Properties; - import org.apache.arrow.driver.jdbc.authentication.UserPasswordAuthentication; import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty; import org.apache.arrow.driver.jdbc.utils.MockFlightSqlProducer; @@ -42,24 +41,24 @@ import org.junit.ClassRule; import org.junit.Test; -/** - * Tests for {@link ArrowFlightJdbcDriver}. - */ +/** Tests for {@link ArrowFlightJdbcDriver}. */ public class ArrowFlightJdbcDriverTest { - @ClassRule - public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE; + @ClassRule public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE; private static final MockFlightSqlProducer PRODUCER = new MockFlightSqlProducer(); static { UserPasswordAuthentication authentication = - new UserPasswordAuthentication.Builder().user("user1", "pass1").user("user2", "pass2") + new UserPasswordAuthentication.Builder() + .user("user1", "pass1") + .user("user2", "pass2") .build(); - FLIGHT_SERVER_TEST_RULE = new FlightServerTestRule.Builder() - .authentication(authentication) - .producer(PRODUCER) - .build(); + FLIGHT_SERVER_TEST_RULE = + new FlightServerTestRule.Builder() + .authentication(authentication) + .producer(PRODUCER) + .build(); } private BufferAllocator allocator; @@ -79,34 +78,36 @@ public void tearDown() throws Exception { } /** - * Tests whether the {@link ArrowFlightJdbcDriver} is registered in the - * {@link DriverManager}. + * Tests whether the {@link ArrowFlightJdbcDriver} is registered in the {@link DriverManager}. * * @throws SQLException If an error occurs. (This is not supposed to happen.) */ @Test public void testDriverIsRegisteredInDriverManager() throws Exception { - assertTrue(DriverManager.getDriver("jdbc:arrow-flight://localhost:32010") instanceof - ArrowFlightJdbcDriver); - assertTrue(DriverManager.getDriver("jdbc:arrow-flight-sql://localhost:32010") instanceof - ArrowFlightJdbcDriver); + assertTrue( + DriverManager.getDriver("jdbc:arrow-flight://localhost:32010") + instanceof ArrowFlightJdbcDriver); + assertTrue( + DriverManager.getDriver("jdbc:arrow-flight-sql://localhost:32010") + instanceof ArrowFlightJdbcDriver); } /** - * Tests whether the {@link ArrowFlightJdbcDriver} returns null when provided with an - * unsupported URL prefix. + * Tests whether the {@link ArrowFlightJdbcDriver} returns null when provided with an unsupported + * URL prefix. */ @Test public void testShouldDeclineUrlWithUnsupportedPrefix() throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); - assertNull(driver.connect("jdbc:mysql://localhost:32010", - dataSource.getProperties("flight", "flight123"))); + assertNull( + driver.connect( + "jdbc:mysql://localhost:32010", dataSource.getProperties("flight", "flight123"))); } /** - * Tests whether the {@link ArrowFlightJdbcDriver} can establish a successful - * connection to the Arrow Flight client. + * Tests whether the {@link ArrowFlightJdbcDriver} can establish a successful connection to the + * Arrow Flight client. * * @throws Exception If the connection fails to be established. */ @@ -116,19 +117,27 @@ public void testShouldConnectWhenProvidedWithValidUrl() throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); try (Connection connection = - driver.connect("jdbc:arrow-flight://" + - dataSource.getConfig().getHost() + ":" + - dataSource.getConfig().getPort() + "?" + - "useEncryption=false", - dataSource.getProperties(dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()))) { + driver.connect( + "jdbc:arrow-flight://" + + dataSource.getConfig().getHost() + + ":" + + dataSource.getConfig().getPort() + + "?" + + "useEncryption=false", + dataSource.getProperties( + dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()))) { assertTrue(connection.isValid(300)); } try (Connection connection = - driver.connect("jdbc:arrow-flight-sql://" + - dataSource.getConfig().getHost() + ":" + - dataSource.getConfig().getPort() + "?" + - "useEncryption=false", - dataSource.getProperties(dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()))) { + driver.connect( + "jdbc:arrow-flight-sql://" + + dataSource.getConfig().getHost() + + ":" + + dataSource.getConfig().getPort() + + "?" + + "useEncryption=false", + dataSource.getProperties( + dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()))) { assertTrue(connection.isValid(300)); } } @@ -139,19 +148,27 @@ public void testConnectWithInsensitiveCasePropertyKeys() throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); try (Connection connection = - driver.connect("jdbc:arrow-flight://" + - dataSource.getConfig().getHost() + ":" + - dataSource.getConfig().getPort() + "?" + - "UseEncryptIon=false", - dataSource.getProperties(dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()))) { + driver.connect( + "jdbc:arrow-flight://" + + dataSource.getConfig().getHost() + + ":" + + dataSource.getConfig().getPort() + + "?" + + "UseEncryptIon=false", + dataSource.getProperties( + dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()))) { assertTrue(connection.isValid(300)); } try (Connection connection = - driver.connect("jdbc:arrow-flight-sql://" + - dataSource.getConfig().getHost() + ":" + - dataSource.getConfig().getPort() + "?" + - "UseEncryptIon=false", - dataSource.getProperties(dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()))) { + driver.connect( + "jdbc:arrow-flight-sql://" + + dataSource.getConfig().getHost() + + ":" + + dataSource.getConfig().getPort() + + "?" + + "UseEncryptIon=false", + dataSource.getProperties( + dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()))) { assertTrue(connection.isValid(300)); } } @@ -161,26 +178,32 @@ public void testConnectWithInsensitiveCasePropertyKeys2() throws Exception { // Get the Arrow Flight JDBC driver by providing a property object with insensitive case keys. final Driver driver = new ArrowFlightJdbcDriver(); Properties properties = - dataSource.getProperties(dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()); + dataSource.getProperties( + dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()); properties.put("UseEncryptIon", "false"); try (Connection connection = - driver.connect("jdbc:arrow-flight://" + - dataSource.getConfig().getHost() + ":" + - dataSource.getConfig().getPort(), properties)) { + driver.connect( + "jdbc:arrow-flight://" + + dataSource.getConfig().getHost() + + ":" + + dataSource.getConfig().getPort(), + properties)) { assertTrue(connection.isValid(300)); } try (Connection connection = - driver.connect("jdbc:arrow-flight-sql://" + - dataSource.getConfig().getHost() + ":" + - dataSource.getConfig().getPort(), properties)) { + driver.connect( + "jdbc:arrow-flight-sql://" + + dataSource.getConfig().getHost() + + ":" + + dataSource.getConfig().getPort(), + properties)) { assertTrue(connection.isValid(300)); } } /** - * Tests whether an exception is thrown upon attempting to connect to a - * malformed URI. + * Tests whether an exception is thrown upon attempting to connect to a malformed URI. * * @throws SQLException If an error occurs. */ @@ -193,8 +216,7 @@ public void testShouldThrowExceptionWhenAttemptingToConnectToMalformedUrl() thro } /** - * Tests whether an exception is thrown upon attempting to connect to a - * malformed URI. + * Tests whether an exception is thrown upon attempting to connect to a malformed URI. * * @throws SQLException If an error occurs. */ @@ -203,60 +225,72 @@ public void testShouldThrowExceptionWhenAttemptingToConnectToUrlNoPrefix() throw final Driver driver = new ArrowFlightJdbcDriver(); final String malformedUri = "localhost:32010"; - driver.connect(malformedUri, dataSource.getProperties(dataSource.getConfig().getUser(), - dataSource.getConfig().getPassword())); + driver.connect( + malformedUri, + dataSource.getProperties( + dataSource.getConfig().getUser(), dataSource.getConfig().getPassword())); } - /** - * Tests whether an exception is thrown upon attempting to connect to a - * malformed URI. - */ + /** Tests whether an exception is thrown upon attempting to connect to a malformed URI. */ @Test public void testShouldThrowExceptionWhenAttemptingToConnectToUrlNoPort() { final Driver driver = new ArrowFlightJdbcDriver(); - SQLException e = assertThrows(SQLException.class, () -> { - Properties properties = dataSource.getProperties(dataSource.getConfig().getUser(), - dataSource.getConfig().getPassword()); - Connection conn = driver.connect("jdbc:arrow-flight://localhost", properties); - conn.close(); - }); + SQLException e = + assertThrows( + SQLException.class, + () -> { + Properties properties = + dataSource.getProperties( + dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()); + Connection conn = driver.connect("jdbc:arrow-flight://localhost", properties); + conn.close(); + }); assertTrue(e.getMessage().contains("URL must have a port")); - e = assertThrows(SQLException.class, () -> { - Properties properties = dataSource.getProperties(dataSource.getConfig().getUser(), - dataSource.getConfig().getPassword()); - Connection conn = driver.connect("jdbc:arrow-flight-sql://localhost", properties); - conn.close(); - }); + e = + assertThrows( + SQLException.class, + () -> { + Properties properties = + dataSource.getProperties( + dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()); + Connection conn = driver.connect("jdbc:arrow-flight-sql://localhost", properties); + conn.close(); + }); assertTrue(e.getMessage().contains("URL must have a port")); } - /** - * Tests whether an exception is thrown upon attempting to connect to a - * malformed URI. - */ + /** Tests whether an exception is thrown upon attempting to connect to a malformed URI. */ @Test public void testShouldThrowExceptionWhenAttemptingToConnectToUrlNoHost() { final Driver driver = new ArrowFlightJdbcDriver(); - SQLException e = assertThrows(SQLException.class, () -> { - Properties properties = dataSource.getProperties(dataSource.getConfig().getUser(), - dataSource.getConfig().getPassword()); - Connection conn = driver.connect("jdbc:arrow-flight://32010:localhost", properties); - conn.close(); - }); + SQLException e = + assertThrows( + SQLException.class, + () -> { + Properties properties = + dataSource.getProperties( + dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()); + Connection conn = driver.connect("jdbc:arrow-flight://32010:localhost", properties); + conn.close(); + }); assertTrue(e.getMessage().contains("URL must have a host")); - e = assertThrows(SQLException.class, () -> { - Properties properties = dataSource.getProperties(dataSource.getConfig().getUser(), - dataSource.getConfig().getPassword()); - Connection conn = driver.connect("jdbc:arrow-flight-sql://32010:localhost", properties); - conn.close(); - }); + e = + assertThrows( + SQLException.class, + () -> { + Properties properties = + dataSource.getProperties( + dataSource.getConfig().getUser(), dataSource.getConfig().getPassword()); + Connection conn = + driver.connect("jdbc:arrow-flight-sql://32010:localhost", properties); + conn.close(); + }); assertTrue(e.getMessage().contains("URL must have a host")); } /** - * Tests whether {@link ArrowFlightJdbcDriver#getUrlsArgs} returns the - * correct URL parameters. + * Tests whether {@link ArrowFlightJdbcDriver#getUrlsArgs} returns the correct URL parameters. * * @throws Exception If an error occurs. */ @@ -264,9 +298,10 @@ public void testShouldThrowExceptionWhenAttemptingToConnectToUrlNoHost() { public void testDriverUrlParsingMechanismShouldReturnTheDesiredArgsFromUrl() throws Exception { final ArrowFlightJdbcDriver driver = new ArrowFlightJdbcDriver(); - final Map parsedArgs = driver.getUrlsArgs( - "jdbc:arrow-flight-sql://localhost:2222/?key1=value1&key2=value2&a=b") - .orElseThrow(() -> new RuntimeException("URL was rejected")); + final Map parsedArgs = + driver + .getUrlsArgs("jdbc:arrow-flight-sql://localhost:2222/?key1=value1&key2=value2&a=b") + .orElseThrow(() -> new RuntimeException("URL was rejected")); // Check size == the amount of args provided (scheme not included) assertEquals(5, parsedArgs.size()); @@ -284,11 +319,13 @@ public void testDriverUrlParsingMechanismShouldReturnTheDesiredArgsFromUrl() thr } @Test - public void testDriverUrlParsingMechanismShouldReturnTheDesiredArgsFromUrlWithSemicolon() throws Exception { + public void testDriverUrlParsingMechanismShouldReturnTheDesiredArgsFromUrlWithSemicolon() + throws Exception { final ArrowFlightJdbcDriver driver = new ArrowFlightJdbcDriver(); - final Map parsedArgs = driver.getUrlsArgs( - "jdbc:arrow-flight-sql://localhost:2222/;key1=value1;key2=value2;a=b") - .orElseThrow(() -> new RuntimeException("URL was rejected")); + final Map parsedArgs = + driver + .getUrlsArgs("jdbc:arrow-flight-sql://localhost:2222/;key1=value1;key2=value2;a=b") + .orElseThrow(() -> new RuntimeException("URL was rejected")); // Check size == the amount of args provided (scheme not included) assertEquals(5, parsedArgs.size()); @@ -306,11 +343,13 @@ public void testDriverUrlParsingMechanismShouldReturnTheDesiredArgsFromUrlWithSe } @Test - public void testDriverUrlParsingMechanismShouldReturnTheDesiredArgsFromUrlWithOneSemicolon() throws Exception { + public void testDriverUrlParsingMechanismShouldReturnTheDesiredArgsFromUrlWithOneSemicolon() + throws Exception { final ArrowFlightJdbcDriver driver = new ArrowFlightJdbcDriver(); - final Map parsedArgs = driver.getUrlsArgs( - "jdbc:arrow-flight-sql://localhost:2222/;key1=value1") - .orElseThrow(() -> new RuntimeException("URL was rejected")); + final Map parsedArgs = + driver + .getUrlsArgs("jdbc:arrow-flight-sql://localhost:2222/;key1=value1") + .orElseThrow(() -> new RuntimeException("URL was rejected")); // Check size == the amount of args provided (scheme not included) assertEquals(3, parsedArgs.size()); @@ -326,22 +365,25 @@ public void testDriverUrlParsingMechanismShouldReturnTheDesiredArgsFromUrlWithOn } @Test - public void testDriverUrlParsingMechanismShouldReturnEmptyOptionalForUnknownScheme() throws SQLException { + public void testDriverUrlParsingMechanismShouldReturnEmptyOptionalForUnknownScheme() + throws SQLException { final ArrowFlightJdbcDriver driver = new ArrowFlightJdbcDriver(); assertFalse(driver.getUrlsArgs("jdbc:malformed-url-flight://localhost:2222").isPresent()); } /** - * Tests whether {@code ArrowFlightJdbcDriverTest#getUrlsArgs} returns the - * correct URL parameters when the host is an IP Address. + * Tests whether {@code ArrowFlightJdbcDriverTest#getUrlsArgs} returns the correct URL parameters + * when the host is an IP Address. * * @throws Exception If an error occurs. */ @Test public void testDriverUrlParsingMechanismShouldWorkWithIPAddress() throws Exception { final ArrowFlightJdbcDriver driver = new ArrowFlightJdbcDriver(); - final Map parsedArgs = driver.getUrlsArgs("jdbc:arrow-flight-sql://0.0.0.0:2222") - .orElseThrow(() -> new RuntimeException("URL was rejected")); + final Map parsedArgs = + driver + .getUrlsArgs("jdbc:arrow-flight-sql://0.0.0.0:2222") + .orElseThrow(() -> new RuntimeException("URL was rejected")); // Check size == the amount of args provided (scheme not included) assertEquals(2, parsedArgs.size()); @@ -354,8 +396,9 @@ public void testDriverUrlParsingMechanismShouldWorkWithIPAddress() throws Except } /** - * Tests whether {@code ArrowFlightJdbcDriverTest#getUrlsArgs} escape especial characters and returns the - * correct URL parameters when the especial character '&' is embedded in the query parameters values. + * Tests whether {@code ArrowFlightJdbcDriverTest#getUrlsArgs} escape especial characters and + * returns the correct URL parameters when the especial character '&' is embedded in the query + * parameters values. * * @throws Exception If an error occurs. */ @@ -363,9 +406,11 @@ public void testDriverUrlParsingMechanismShouldWorkWithIPAddress() throws Except public void testDriverUrlParsingMechanismShouldWorkWithEmbeddedEspecialCharacter() throws Exception { final ArrowFlightJdbcDriver driver = new ArrowFlightJdbcDriver(); - final Map parsedArgs = driver.getUrlsArgs( - "jdbc:arrow-flight-sql://0.0.0.0:2222?test1=test1value&test2%26continue=test2value&test3=test3value") - .orElseThrow(() -> new RuntimeException("URL was rejected")); + final Map parsedArgs = + driver + .getUrlsArgs( + "jdbc:arrow-flight-sql://0.0.0.0:2222?test1=test1value&test2%26continue=test2value&test3=test3value") + .orElseThrow(() -> new RuntimeException("URL was rejected")); // Check size == the amount of args provided (scheme not included) assertEquals(5, parsedArgs.size()); diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcFactoryTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcFactoryTest.java index a355e7156f7..2b9c77ff83d 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcFactoryTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcFactoryTest.java @@ -17,10 +17,10 @@ package org.apache.arrow.driver.jdbc; +import com.google.common.collect.ImmutableMap; import java.lang.reflect.Constructor; import java.sql.Connection; import java.util.Properties; - import org.apache.arrow.driver.jdbc.authentication.UserPasswordAuthentication; import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty; import org.apache.arrow.driver.jdbc.utils.MockFlightSqlProducer; @@ -33,26 +33,24 @@ import org.junit.ClassRule; import org.junit.Test; -import com.google.common.collect.ImmutableMap; - -/** - * Tests for {@link ArrowFlightJdbcDriver}. - */ +/** Tests for {@link ArrowFlightJdbcDriver}. */ public class ArrowFlightJdbcFactoryTest { - @ClassRule - public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE; + @ClassRule public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE; private static final MockFlightSqlProducer PRODUCER = new MockFlightSqlProducer(); static { UserPasswordAuthentication authentication = - new UserPasswordAuthentication.Builder().user("user1", "pass1").user("user2", "pass2") + new UserPasswordAuthentication.Builder() + .user("user1", "pass1") + .user("user2", "pass2") .build(); - FLIGHT_SERVER_TEST_RULE = new FlightServerTestRule.Builder() - .authentication(authentication) - .producer(PRODUCER) - .build(); + FLIGHT_SERVER_TEST_RULE = + new FlightServerTestRule.Builder() + .authentication(authentication) + .producer(PRODUCER) + .build(); } private BufferAllocator allocator; @@ -77,13 +75,21 @@ public void testShouldBeAbleToEstablishAConnectionSuccessfully() throws Exceptio ArrowFlightJdbcFactory factory = constructor.newInstance(); final Properties properties = new Properties(); - properties.putAll(ImmutableMap.of( - ArrowFlightConnectionProperty.HOST.camelName(), "localhost", - ArrowFlightConnectionProperty.PORT.camelName(), 32010, - ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), false)); + properties.putAll( + ImmutableMap.of( + ArrowFlightConnectionProperty.HOST.camelName(), + "localhost", + ArrowFlightConnectionProperty.PORT.camelName(), + 32010, + ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), + false)); - try (Connection connection = factory.newConnection(driver, constructor.newInstance(), - "jdbc:arrow-flight-sql://localhost:32010", properties)) { + try (Connection connection = + factory.newConnection( + driver, + constructor.newInstance(), + "jdbc:arrow-flight-sql://localhost:32010", + properties)) { assert connection.isValid(300); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcTimeTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcTimeTest.java index 104794b3ad1..044b3e1ec5e 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcTimeTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcTimeTest.java @@ -22,15 +22,13 @@ import java.time.LocalTime; import java.util.concurrent.TimeUnit; - import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.ErrorCollector; public class ArrowFlightJdbcTimeTest { - @ClassRule - public static final ErrorCollector collector = new ErrorCollector(); + @ClassRule public static final ErrorCollector collector = new ErrorCollector(); final int hour = 5; final int minute = 6; final int second = 7; @@ -38,7 +36,8 @@ public class ArrowFlightJdbcTimeTest { @Test public void testPrintingMillisNoLeadingZeroes() { // testing the regular case where the precision of the millisecond is 3 - LocalTime dateTime = LocalTime.of(hour, minute, second, (int) TimeUnit.MILLISECONDS.toNanos(999)); + LocalTime dateTime = + LocalTime.of(hour, minute, second, (int) TimeUnit.MILLISECONDS.toNanos(999)); ArrowFlightJdbcTime time = new ArrowFlightJdbcTime(dateTime); collector.checkThat(time.toString(), endsWith(".999")); collector.checkThat(time.getHours(), is(hour)); @@ -49,7 +48,8 @@ public void testPrintingMillisNoLeadingZeroes() { @Test public void testPrintingMillisOneLeadingZeroes() { // test case where one leading zero needs to be added - LocalTime dateTime = LocalTime.of(hour, minute, second, (int) TimeUnit.MILLISECONDS.toNanos(99)); + LocalTime dateTime = + LocalTime.of(hour, minute, second, (int) TimeUnit.MILLISECONDS.toNanos(99)); ArrowFlightJdbcTime time = new ArrowFlightJdbcTime(dateTime); collector.checkThat(time.toString(), endsWith(".099")); collector.checkThat(time.getHours(), is(hour)); diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatementTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatementTest.java index 0b521a704bb..ef82bf751aa 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatementTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightPreparedStatementTest.java @@ -28,7 +28,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; - import org.apache.arrow.driver.jdbc.utils.CoreMockedSqlProducers; import org.apache.arrow.driver.jdbc.utils.MockFlightSqlProducer; import org.apache.arrow.flight.sql.FlightSqlUtils; @@ -54,14 +53,14 @@ public class ArrowFlightPreparedStatementTest { public static final MockFlightSqlProducer PRODUCER = CoreMockedSqlProducers.getLegacyProducer(); + @ClassRule - public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE = FlightServerTestRule - .createStandardTestRule(PRODUCER); + public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE = + FlightServerTestRule.createStandardTestRule(PRODUCER); private static Connection connection; - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); @BeforeClass public static void setup() throws SQLException { @@ -82,7 +81,7 @@ public void before() { public void testSimpleQueryNoParameterBinding() throws SQLException { final String query = CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD; try (final PreparedStatement preparedStatement = connection.prepareStatement(query); - final ResultSet resultSet = preparedStatement.executeQuery()) { + final ResultSet resultSet = preparedStatement.executeQuery()) { CoreMockedSqlProducers.assertLegacyRegularSqlResultSet(resultSet, collector); } } @@ -90,20 +89,26 @@ public void testSimpleQueryNoParameterBinding() throws SQLException { @Test public void testQueryWithParameterBinding() throws SQLException { final String query = "Fake query with parameters"; - final Schema schema = new Schema(Collections.singletonList(Field.nullable("", Types.MinorType.INT.getType()))); - final Schema parameterSchema = new Schema(Arrays.asList( - Field.nullable("", ArrowType.Utf8.INSTANCE), - new Field("", FieldType.nullable(ArrowType.List.INSTANCE), + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("", Types.MinorType.INT.getType()))); + final Schema parameterSchema = + new Schema( + Arrays.asList( + Field.nullable("", ArrowType.Utf8.INSTANCE), + new Field( + "", + FieldType.nullable(ArrowType.List.INSTANCE), Collections.singletonList(Field.nullable("", Types.MinorType.INT.getType()))))); - final List> expected = Collections.singletonList(Arrays.asList( - new Text("foo"), - new Integer[]{1, 2, null})); - - PRODUCER.addSelectQuery(query, schema, - Collections.singletonList(listener -> { + final List> expected = + Collections.singletonList(Arrays.asList(new Text("foo"), new Integer[] {1, 2, null})); + + PRODUCER.addSelectQuery( + query, + schema, + Collections.singletonList( + listener -> { try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, - allocator)) { + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { ((IntVector) root.getVector(0)).setSafe(0, 10); root.setRowCount(1); listener.start(root); @@ -119,7 +124,8 @@ public void testQueryWithParameterBinding() throws SQLException { try (final PreparedStatement preparedStatement = connection.prepareStatement(query)) { preparedStatement.setString(1, "foo"); - preparedStatement.setArray(2, connection.createArrayOf("INTEGER", new Integer[]{1, 2, null})); + preparedStatement.setArray( + 2, connection.createArrayOf("INTEGER", new Integer[] {1, 2, null})); try (final ResultSet resultSet = preparedStatement.executeQuery()) { resultSet.next(); @@ -128,15 +134,20 @@ public void testQueryWithParameterBinding() throws SQLException { } } - @Test @Ignore("https://github.com/apache/arrow/issues/34741: flaky test") public void testPreparedStatementExecutionOnce() throws SQLException { - final PreparedStatement statement = connection.prepareStatement(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD); - // Expect that there is one entry in the map -- {prepared statement action type, invocation count}. + final PreparedStatement statement = + connection.prepareStatement(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD); + // Expect that there is one entry in the map -- {prepared statement action type, invocation + // count}. assertEquals(PRODUCER.getActionTypeCounter().size(), 1); // Expect that the prepared statement was executed exactly once. - assertEquals(PRODUCER.getActionTypeCounter().get(FlightSqlUtils.FLIGHT_SQL_CREATE_PREPARED_STATEMENT.getType()), 1); + assertEquals( + PRODUCER + .getActionTypeCounter() + .get(FlightSqlUtils.FLIGHT_SQL_CREATE_PREPARED_STATEMENT.getType()), + 1); statement.close(); } @@ -157,7 +168,7 @@ public void testReturnColumnCount() throws SQLException { @Test public void testUpdateQuery() throws SQLException { String query = "Fake update"; - PRODUCER.addUpdateQuery(query, /*updatedRows*/42); + PRODUCER.addUpdateQuery(query, /*updatedRows*/ 42); try (final PreparedStatement stmt = connection.prepareStatement(query)) { int updated = stmt.executeUpdate(); assertEquals(42, updated); @@ -167,10 +178,12 @@ public void testUpdateQuery() throws SQLException { @Test public void testUpdateQueryWithParameters() throws SQLException { String query = "Fake update with parameters"; - PRODUCER.addUpdateQuery(query, /*updatedRows*/42); - PRODUCER.addExpectedParameters(query, + PRODUCER.addUpdateQuery(query, /*updatedRows*/ 42); + PRODUCER.addExpectedParameters( + query, new Schema(Collections.singletonList(Field.nullable("", ArrowType.Utf8.INSTANCE))), - Collections.singletonList(Collections.singletonList(new Text("foo".getBytes(StandardCharsets.UTF_8))))); + Collections.singletonList( + Collections.singletonList(new Text("foo".getBytes(StandardCharsets.UTF_8))))); try (final PreparedStatement stmt = connection.prepareStatement(query)) { // TODO: make sure this is validated on the server too stmt.setString(1, "foo"); @@ -182,29 +195,29 @@ public void testUpdateQueryWithParameters() throws SQLException { @Test public void testUpdateQueryWithBatchedParameters() throws SQLException { String query = "Fake update with batched parameters"; - Schema parameterSchema = new Schema(Arrays.asList( - Field.nullable("", ArrowType.Utf8.INSTANCE), - new Field("", FieldType.nullable(ArrowType.List.INSTANCE), - Collections.singletonList(Field.nullable("", Types.MinorType.INT.getType()))))); - List> expected = Arrays.asList( + Schema parameterSchema = + new Schema( Arrays.asList( - new Text("foo"), - new Integer[]{1, 2, null}), - Arrays.asList( - new Text("bar"), - new Integer[]{0, -1, 100000}) - ); + Field.nullable("", ArrowType.Utf8.INSTANCE), + new Field( + "", + FieldType.nullable(ArrowType.List.INSTANCE), + Collections.singletonList(Field.nullable("", Types.MinorType.INT.getType()))))); + List> expected = + Arrays.asList( + Arrays.asList(new Text("foo"), new Integer[] {1, 2, null}), + Arrays.asList(new Text("bar"), new Integer[] {0, -1, 100000})); - PRODUCER.addUpdateQuery(query, /*updatedRows*/42); + PRODUCER.addUpdateQuery(query, /*updatedRows*/ 42); PRODUCER.addExpectedParameters(query, parameterSchema, expected); try (final PreparedStatement stmt = connection.prepareStatement(query)) { // TODO: make sure this is validated on the server too stmt.setString(1, "foo"); - stmt.setArray(2, connection.createArrayOf("INTEGER", new Integer[]{1, 2, null})); + stmt.setArray(2, connection.createArrayOf("INTEGER", new Integer[] {1, 2, null})); stmt.addBatch(); stmt.setString(1, "bar"); - stmt.setArray(2, connection.createArrayOf("INTEGER", new Integer[]{0, -1, 100000})); + stmt.setArray(2, connection.createArrayOf("INTEGER", new Integer[] {0, -1, 100000})); stmt.addBatch(); int[] updated = stmt.executeBatch(); assertEquals(42, updated[0]); diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteTest.java index 155fcc50827..95644775e8f 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteTest.java @@ -32,7 +32,6 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.IntStream; - import org.apache.arrow.driver.jdbc.utils.MockFlightSqlProducer; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -52,9 +51,7 @@ import org.junit.Test; import org.junit.rules.ErrorCollector; -/** - * Tests for {@link ArrowFlightStatement#execute}. - */ +/** Tests for {@link ArrowFlightStatement#execute}. */ public class ArrowFlightStatementExecuteTest { private static final String SAMPLE_QUERY_CMD = "SELECT * FROM this_test"; private static final int SAMPLE_QUERY_ROWS = Byte.MAX_VALUE; @@ -68,11 +65,12 @@ public class ArrowFlightStatementExecuteTest { "UPDATE this_large_table SET this_large_field = that_large_field FROM this_large_test WHERE this_large_condition"; private static final long SAMPLE_LARGE_UPDATE_COUNT = Long.MAX_VALUE; private static final MockFlightSqlProducer PRODUCER = new MockFlightSqlProducer(); + @ClassRule - public static final FlightServerTestRule SERVER_TEST_RULE = FlightServerTestRule.createStandardTestRule(PRODUCER); + public static final FlightServerTestRule SERVER_TEST_RULE = + FlightServerTestRule.createStandardTestRule(PRODUCER); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private Connection connection; private Statement statement; @@ -81,22 +79,24 @@ public static void setUpBeforeClass() { PRODUCER.addSelectQuery( SAMPLE_QUERY_CMD, SAMPLE_QUERY_SCHEMA, - Collections.singletonList(listener -> { - try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - final VectorSchemaRoot root = VectorSchemaRoot.create(SAMPLE_QUERY_SCHEMA, - allocator)) { - final UInt1Vector vector = (UInt1Vector) root.getVector(VECTOR_NAME); - IntStream.range(0, SAMPLE_QUERY_ROWS).forEach(index -> vector.setSafe(index, index)); - vector.setValueCount(SAMPLE_QUERY_ROWS); - root.setRowCount(SAMPLE_QUERY_ROWS); - listener.start(root); - listener.putNext(); - } catch (final Throwable throwable) { - listener.error(throwable); - } finally { - listener.completed(); - } - })); + Collections.singletonList( + listener -> { + try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); + final VectorSchemaRoot root = + VectorSchemaRoot.create(SAMPLE_QUERY_SCHEMA, allocator)) { + final UInt1Vector vector = (UInt1Vector) root.getVector(VECTOR_NAME); + IntStream.range(0, SAMPLE_QUERY_ROWS) + .forEach(index -> vector.setSafe(index, index)); + vector.setValueCount(SAMPLE_QUERY_ROWS); + root.setRowCount(SAMPLE_QUERY_ROWS); + listener.start(root); + listener.putNext(); + } catch (final Throwable throwable) { + listener.error(throwable); + } finally { + listener.completed(); + } + })); PRODUCER.addUpdateQuery(SAMPLE_UPDATE_QUERY, SAMPLE_UPDATE_COUNT); PRODUCER.addUpdateQuery(SAMPLE_LARGE_UPDATE_QUERY, SAMPLE_LARGE_UPDATE_COUNT); } @@ -119,10 +119,11 @@ public static void tearDownAfterClass() throws Exception { @Test public void testExecuteShouldRunSelectQuery() throws SQLException { - collector.checkThat(statement.execute(SAMPLE_QUERY_CMD), - is(true)); // Means this is a SELECT query. + collector.checkThat( + statement.execute(SAMPLE_QUERY_CMD), is(true)); // Means this is a SELECT query. final Set numbers = - IntStream.range(0, SAMPLE_QUERY_ROWS).boxed() + IntStream.range(0, SAMPLE_QUERY_ROWS) + .boxed() .map(Integer::byteValue) .collect(Collectors.toCollection(HashSet::new)); try (final ResultSet resultSet = statement.getResultSet()) { @@ -142,8 +143,8 @@ public void testExecuteShouldRunSelectQuery() throws SQLException { @Test public void testExecuteShouldRunUpdateQueryForSmallUpdate() throws SQLException { - collector.checkThat(statement.execute(SAMPLE_UPDATE_QUERY), - is(false)); // Means this is an UPDATE query. + collector.checkThat( + statement.execute(SAMPLE_UPDATE_QUERY), is(false)); // Means this is an UPDATE query. collector.checkThat( (long) statement.getUpdateCount(), is(allOf(equalTo(statement.getLargeUpdateCount()), equalTo(SAMPLE_UPDATE_COUNT)))); @@ -158,8 +159,10 @@ public void testExecuteShouldRunUpdateQueryForLargeUpdate() throws SQLException collector.checkThat(updateCountLarge, is(equalTo(SAMPLE_LARGE_UPDATE_COUNT))); collector.checkThat( updateCountSmall, - is(allOf(equalTo((long) AvaticaUtils.toSaturatedInt(updateCountLarge)), - not(equalTo(updateCountLarge))))); + is( + allOf( + equalTo((long) AvaticaUtils.toSaturatedInt(updateCountLarge)), + not(equalTo(updateCountLarge))))); collector.checkThat(statement.getResultSet(), is(nullValue())); } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteUpdateTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteUpdateTest.java index 43209d8913e..ad2c4d9eb96 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteUpdateTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteUpdateTest.java @@ -29,7 +29,6 @@ import java.sql.SQLFeatureNotSupportedException; import java.sql.Statement; import java.util.Collections; - import org.apache.arrow.driver.jdbc.utils.MockFlightSqlProducer; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -48,9 +47,7 @@ import org.junit.Test; import org.junit.rules.ErrorCollector; -/** - * Tests for {@link ArrowFlightStatement#executeUpdate}. - */ +/** Tests for {@link ArrowFlightStatement#executeUpdate}. */ public class ArrowFlightStatementExecuteUpdateTest { private static final String UPDATE_SAMPLE_QUERY = "UPDATE sample_table SET sample_col = sample_val WHERE sample_condition"; @@ -63,11 +60,12 @@ public class ArrowFlightStatementExecuteUpdateTest { new Schema( Collections.singletonList(Field.nullable("placeholder", MinorType.VARCHAR.getType()))); private static final MockFlightSqlProducer PRODUCER = new MockFlightSqlProducer(); + @ClassRule - public static final FlightServerTestRule SERVER_TEST_RULE = FlightServerTestRule.createStandardTestRule(PRODUCER); + public static final FlightServerTestRule SERVER_TEST_RULE = + FlightServerTestRule.createStandardTestRule(PRODUCER); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); public Connection connection; public Statement statement; @@ -78,18 +76,19 @@ public static void setUpBeforeClass() { PRODUCER.addSelectQuery( REGULAR_QUERY_SAMPLE, REGULAR_QUERY_SCHEMA, - Collections.singletonList(listener -> { - try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - final VectorSchemaRoot root = VectorSchemaRoot.create(REGULAR_QUERY_SCHEMA, - allocator)) { - listener.start(root); - listener.putNext(); - } catch (final Throwable throwable) { - listener.error(throwable); - } finally { - listener.completed(); - } - })); + Collections.singletonList( + listener -> { + try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); + final VectorSchemaRoot root = + VectorSchemaRoot.create(REGULAR_QUERY_SCHEMA, allocator)) { + listener.start(root); + listener.putNext(); + } catch (final Throwable throwable) { + listener.error(throwable); + } finally { + listener.completed(); + } + })); } @Before @@ -111,8 +110,8 @@ public static void tearDownAfterClass() throws Exception { @Test public void testExecuteUpdateShouldReturnNumColsAffectedForNumRowsFittingInt() throws SQLException { - collector.checkThat(statement.executeUpdate(UPDATE_SAMPLE_QUERY), - is(UPDATE_SAMPLE_QUERY_AFFECTED_COLS)); + collector.checkThat( + statement.executeUpdate(UPDATE_SAMPLE_QUERY), is(UPDATE_SAMPLE_QUERY_AFFECTED_COLS)); } @Test @@ -122,10 +121,13 @@ public void testExecuteUpdateShouldReturnSaturatedNumColsAffectedIfDoesNotFitInI final long expectedRowCountRaw = LARGE_UPDATE_SAMPLE_QUERY_AFFECTED_COLS; collector.checkThat( result, - is(allOf( - not(equalTo(expectedRowCountRaw)), - equalTo((long) AvaticaUtils.toSaturatedInt( - expectedRowCountRaw))))); // Because of long-to-integer overflow. + is( + allOf( + not(equalTo(expectedRowCountRaw)), + equalTo( + (long) + AvaticaUtils.toSaturatedInt( + expectedRowCountRaw))))); // Because of long-to-integer overflow. } @Test @@ -161,10 +163,10 @@ public void testExecuteUpdateUnsupportedWithArraysOfStrings() throws SQLExceptio @Test public void testExecuteShouldExecuteUpdateQueryAutomatically() throws SQLException { - collector.checkThat(statement.execute(UPDATE_SAMPLE_QUERY), - is(false)); // Meaning there was an update query. - collector.checkThat(statement.execute(REGULAR_QUERY_SAMPLE), - is(true)); // Meaning there was a select query. + collector.checkThat( + statement.execute(UPDATE_SAMPLE_QUERY), is(false)); // Meaning there was an update query. + collector.checkThat( + statement.execute(REGULAR_QUERY_SAMPLE), is(true)); // Meaning there was a select query. } @Test diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionMutualTlsTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionMutualTlsTest.java index 03f15d77ade..3906e7239ac 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionMutualTlsTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionMutualTlsTest.java @@ -27,7 +27,6 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; - import org.apache.arrow.driver.jdbc.authentication.UserPasswordAuthentication; import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler; import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty; @@ -42,13 +41,10 @@ import org.junit.ClassRule; import org.junit.Test; -/** - * Tests encrypted connections. - */ +/** Tests encrypted connections. */ public class ConnectionMutualTlsTest { - @ClassRule - public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE; + @ClassRule public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE; private static final String tlsRootCertsPath; private static final String clientMTlsCertPath; private static final String badClientMTlsCertPath; @@ -59,15 +55,15 @@ public class ConnectionMutualTlsTest { private static final String passTest = "pass1"; static { - final FlightSqlTestCertificates.CertKeyPair - certKey = FlightSqlTestCertificates.exampleTlsCerts().get(0); + final FlightSqlTestCertificates.CertKeyPair certKey = + FlightSqlTestCertificates.exampleTlsCerts().get(0); tlsRootCertsPath = certKey.cert.getPath(); final File serverMTlsCACert = FlightSqlTestCertificates.exampleCACert(); - final FlightSqlTestCertificates.CertKeyPair - clientMTlsCertKey = FlightSqlTestCertificates.exampleTlsCerts().get(1); + final FlightSqlTestCertificates.CertKeyPair clientMTlsCertKey = + FlightSqlTestCertificates.exampleTlsCerts().get(1); clientMTlsCertPath = clientMTlsCertKey.cert.getPath(); clientMTlsKeyPath = clientMTlsCertKey.key.getPath(); @@ -75,16 +71,16 @@ public class ConnectionMutualTlsTest { badClientMTlsCertPath = clientMTlsCertPath + ".bad"; badClientMTlsKeyPath = clientMTlsKeyPath + ".bad"; - UserPasswordAuthentication authentication = new UserPasswordAuthentication.Builder() - .user(userTest, passTest) - .build(); + UserPasswordAuthentication authentication = + new UserPasswordAuthentication.Builder().user(userTest, passTest).build(); - FLIGHT_SERVER_TEST_RULE = new FlightServerTestRule.Builder() - .authentication(authentication) - .useEncryption(certKey.cert, certKey.key) - .useMTlsClientVerification(serverMTlsCACert) - .producer(PRODUCER) - .build(); + FLIGHT_SERVER_TEST_RULE = + new FlightServerTestRule.Builder() + .authentication(authentication) + .useEncryption(certKey.cert, certKey.key) + .useMTlsClientVerification(serverMTlsCACert) + .producer(PRODUCER) + .build(); } private BufferAllocator allocator; @@ -109,17 +105,17 @@ public void tearDown() throws Exception { public void testGetEncryptedClientAuthenticated() throws Exception { try (ArrowFlightSqlClientHandler client = - new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) - .withUsername(userTest) - .withPassword(passTest) - .withTlsRootCertificates(tlsRootCertsPath) - .withClientCertificate(clientMTlsCertPath) - .withClientKey(clientMTlsKeyPath) - .withBufferAllocator(allocator) - .withEncryption(true) - .build()) { + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) + .withUsername(userTest) + .withPassword(passTest) + .withTlsRootCertificates(tlsRootCertsPath) + .withClientCertificate(clientMTlsCertPath) + .withClientKey(clientMTlsKeyPath) + .withBufferAllocator(allocator) + .withEncryption(true) + .build()) { assertNotNull(client); } } @@ -131,21 +127,24 @@ public void testGetEncryptedClientAuthenticated() throws Exception { @Test public void testGetEncryptedClientWithBadMTlsCertPath() { - assertThrows(SQLException.class, () -> { - try (ArrowFlightSqlClientHandler handler = new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) - .withUsername(userTest) - .withPassword(passTest) - .withTlsRootCertificates(tlsRootCertsPath) - .withClientCertificate(badClientMTlsCertPath) - .withClientKey(clientMTlsKeyPath) - .withBufferAllocator(allocator) - .withEncryption(true) - .build()) { - Assert.fail(); - } - }); + assertThrows( + SQLException.class, + () -> { + try (ArrowFlightSqlClientHandler handler = + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) + .withUsername(userTest) + .withPassword(passTest) + .withTlsRootCertificates(tlsRootCertsPath) + .withClientCertificate(badClientMTlsCertPath) + .withClientKey(clientMTlsKeyPath) + .withBufferAllocator(allocator) + .withEncryption(true) + .build()) { + Assert.fail(); + } + }); } /** @@ -155,21 +154,24 @@ public void testGetEncryptedClientWithBadMTlsCertPath() { @Test public void testGetEncryptedClientWithBadMTlsKeyPath() { - assertThrows(SQLException.class, () -> { - try (ArrowFlightSqlClientHandler handler = new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) - .withUsername(userTest) - .withPassword(passTest) - .withTlsRootCertificates(tlsRootCertsPath) - .withClientCertificate(clientMTlsCertPath) - .withClientKey(badClientMTlsKeyPath) - .withBufferAllocator(allocator) - .withEncryption(true) - .build()) { - Assert.fail(); - } - }); + assertThrows( + SQLException.class, + () -> { + try (ArrowFlightSqlClientHandler handler = + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) + .withUsername(userTest) + .withPassword(passTest) + .withTlsRootCertificates(tlsRootCertsPath) + .withClientCertificate(clientMTlsCertPath) + .withClientKey(badClientMTlsKeyPath) + .withBufferAllocator(allocator) + .withEncryption(true) + .build()) { + Assert.fail(); + } + }); } /** @@ -180,21 +182,21 @@ public void testGetEncryptedClientWithBadMTlsKeyPath() { @Test public void testGetNonAuthenticatedEncryptedClientNoAuth() throws Exception { try (ArrowFlightSqlClientHandler client = - new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withTlsRootCertificates(tlsRootCertsPath) - .withClientCertificate(clientMTlsCertPath) - .withClientKey(clientMTlsKeyPath) - .withBufferAllocator(allocator) - .withEncryption(true) - .build()) { + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withTlsRootCertificates(tlsRootCertsPath) + .withClientCertificate(clientMTlsCertPath) + .withClientKey(clientMTlsKeyPath) + .withBufferAllocator(allocator) + .withEncryption(true) + .build()) { assertNotNull(client); } } /** - * Check if an encrypted connection can be established successfully when the - * provided valid credentials and a valid TLS Root Certs path. + * Check if an encrypted connection can be established successfully when the provided valid + * credentials and a valid TLS Root Certs path. * * @throws Exception on error. */ @@ -203,18 +205,14 @@ public void testGetEncryptedConnectionWithValidCredentialsAndTlsRootsPath() thro final Properties properties = new Properties(); properties.put(ArrowFlightConnectionProperty.HOST.camelName(), "localhost"); - properties.put(ArrowFlightConnectionProperty.PORT.camelName(), - FLIGHT_SERVER_TEST_RULE.getPort()); - properties.put(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); - properties.put(ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), - tlsRootCertsPath); - properties.put(ArrowFlightConnectionProperty.CLIENT_CERTIFICATE.camelName(), - clientMTlsCertPath); - properties.put(ArrowFlightConnectionProperty.CLIENT_KEY.camelName(), - clientMTlsKeyPath); + properties.put( + ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_RULE.getPort()); + properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); + properties.put(ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); + properties.put( + ArrowFlightConnectionProperty.CLIENT_CERTIFICATE.camelName(), clientMTlsCertPath); + properties.put(ArrowFlightConnectionProperty.CLIENT_KEY.camelName(), clientMTlsKeyPath); final ArrowFlightJdbcDataSource dataSource = ArrowFlightJdbcDataSource.createNewDataSource(properties); @@ -224,8 +222,8 @@ public void testGetEncryptedConnectionWithValidCredentialsAndTlsRootsPath() thro } /** - * Check if an encrypted connection can be established successfully when not - * providing authentication. + * Check if an encrypted connection can be established successfully when not providing + * authentication. * * @throws Exception on error. */ @@ -233,22 +231,26 @@ public void testGetEncryptedConnectionWithValidCredentialsAndTlsRootsPath() thro public void testGetNonAuthenticatedEncryptedConnection() throws Exception { final Properties properties = new Properties(); - properties.put(ArrowFlightConnectionProperty.HOST.camelName(), FLIGHT_SERVER_TEST_RULE.getHost()); - properties.put(ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_RULE.getPort()); + properties.put( + ArrowFlightConnectionProperty.HOST.camelName(), FLIGHT_SERVER_TEST_RULE.getHost()); + properties.put( + ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_RULE.getPort()); properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), true); properties.put(ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); - properties.put(ArrowFlightConnectionProperty.CLIENT_CERTIFICATE.camelName(), clientMTlsCertPath); + properties.put( + ArrowFlightConnectionProperty.CLIENT_CERTIFICATE.camelName(), clientMTlsCertPath); properties.put(ArrowFlightConnectionProperty.CLIENT_KEY.camelName(), clientMTlsKeyPath); - final ArrowFlightJdbcDataSource dataSource = ArrowFlightJdbcDataSource.createNewDataSource(properties); + final ArrowFlightJdbcDataSource dataSource = + ArrowFlightJdbcDataSource.createNewDataSource(properties); try (final Connection connection = dataSource.getConnection()) { Assert.assertTrue(connection.isValid(300)); } } /** - * Check if an encrypted connection can be established successfully when connecting through - * the DriverManager using just a connection url. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using just a connection url. * * @throws Exception on error. */ @@ -257,9 +259,10 @@ public void testTLSConnectionPropertyTrueCorrectCastUrlWithDriverManager() throw final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); - final String jdbcUrl = String.format( - "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s" + - "&useEncryption=true&%s=%s&%s=%s&%s=%s", + final String jdbcUrl = + String.format( + "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s" + + "&useEncryption=true&%s=%s&%s=%s&%s=%s", FLIGHT_SERVER_TEST_RULE.getPort(), userTest, passTest, @@ -276,14 +279,15 @@ public void testTLSConnectionPropertyTrueCorrectCastUrlWithDriverManager() throw } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * a connection url and properties with String K-V pairs. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using a connection url and properties with String K-V pairs. * * @throws Exception on error. */ @Test - public void testTLSConnectionPropertyTrueCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() - throws Exception { + public void + testTLSConnectionPropertyTrueCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); @@ -291,14 +295,15 @@ public void testTLSConnectionPropertyTrueCorrectCastUrlAndPropertiesUsingSetProp properties.setProperty(ArrowFlightConnectionProperty.USER.camelName(), userTest); properties.setProperty(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); - properties.setProperty(ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); + properties.setProperty( + ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); properties.setProperty(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), "true"); - properties.setProperty(ArrowFlightConnectionProperty.CLIENT_CERTIFICATE.camelName(), clientMTlsCertPath); + properties.setProperty( + ArrowFlightConnectionProperty.CLIENT_CERTIFICATE.camelName(), clientMTlsCertPath); properties.setProperty(ArrowFlightConnectionProperty.CLIENT_KEY.camelName(), clientMTlsKeyPath); - final String jdbcUrl = String.format( - "jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()); + final String jdbcUrl = + String.format("jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()); try (Connection connection = DriverManager.getConnection(jdbcUrl, properties)) { Assert.assertTrue(connection.isValid(0)); @@ -306,8 +311,8 @@ public void testTLSConnectionPropertyTrueCorrectCastUrlAndPropertiesUsingSetProp } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * a connection url and properties with Object K-V pairs. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using a connection url and properties with Object K-V pairs. * * @throws Exception on error. */ @@ -323,12 +328,12 @@ public void testTLSConnectionPropertyTrueCorrectCastUrlAndPropertiesUsingPutWith properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), true); properties.put(ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); - properties.put(ArrowFlightConnectionProperty.CLIENT_CERTIFICATE.camelName(), clientMTlsCertPath); + properties.put( + ArrowFlightConnectionProperty.CLIENT_CERTIFICATE.camelName(), clientMTlsCertPath); properties.put(ArrowFlightConnectionProperty.CLIENT_KEY.camelName(), clientMTlsKeyPath); - final String jdbcUrl = String.format( - "jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()); + final String jdbcUrl = + String.format("jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()); try (Connection connection = DriverManager.getConnection(jdbcUrl, properties)) { Assert.assertTrue(connection.isValid(0)); @@ -336,8 +341,8 @@ public void testTLSConnectionPropertyTrueCorrectCastUrlAndPropertiesUsingPutWith } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * just a connection url and using 0 and 1 as ssl values. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using just a connection url and using 0 and 1 as ssl values. * * @throws Exception on error. */ @@ -347,9 +352,10 @@ public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlWithDriverManager( final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); - final String jdbcUrl = String.format( - "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s" + - "&useEncryption=1&useSystemTrustStore=0&%s=%s&%s=%s&%s=%s", + final String jdbcUrl = + String.format( + "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s" + + "&useEncryption=1&useSystemTrustStore=0&%s=%s&%s=%s&%s=%s", FLIGHT_SERVER_TEST_RULE.getPort(), userTest, passTest, @@ -366,14 +372,16 @@ public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlWithDriverManager( } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * a connection url and properties with String K-V pairs and using 0 and 1 as ssl values. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using a connection url and properties with String K-V pairs and using 0 and 1 as + * ssl values. * * @throws Exception on error. */ @Test - public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() - throws Exception { + public void + testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); @@ -381,12 +389,15 @@ public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsing properties.setProperty(ArrowFlightConnectionProperty.USER.camelName(), userTest); properties.setProperty(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); - properties.setProperty(ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); + properties.setProperty( + ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); properties.setProperty(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), "1"); - properties.setProperty(ArrowFlightConnectionProperty.CLIENT_CERTIFICATE.camelName(), clientMTlsCertPath); + properties.setProperty( + ArrowFlightConnectionProperty.CLIENT_CERTIFICATE.camelName(), clientMTlsCertPath); properties.setProperty(ArrowFlightConnectionProperty.CLIENT_KEY.camelName(), clientMTlsKeyPath); - final String jdbcUrl = String.format("jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()); + final String jdbcUrl = + String.format("jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()); try (Connection connection = DriverManager.getConnection(jdbcUrl, properties)) { Assert.assertTrue(connection.isValid(0)); @@ -394,14 +405,16 @@ public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsing } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * a connection url and properties with Object K-V pairs and using 0 and 1 as ssl values. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using a connection url and properties with Object K-V pairs and using 0 and 1 as + * ssl values. * * @throws Exception on error. */ @Test - public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsingPutWithDriverManager() - throws Exception { + public void + testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsingPutWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); @@ -411,11 +424,12 @@ public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsing properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), 1); properties.put(ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); - properties.put(ArrowFlightConnectionProperty.CLIENT_CERTIFICATE.camelName(), clientMTlsCertPath); + properties.put( + ArrowFlightConnectionProperty.CLIENT_CERTIFICATE.camelName(), clientMTlsCertPath); properties.put(ArrowFlightConnectionProperty.CLIENT_KEY.camelName(), clientMTlsKeyPath); - final String jdbcUrl = String.format("jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()); + final String jdbcUrl = + String.format("jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()); try (Connection connection = DriverManager.getConnection(jdbcUrl, properties)) { Assert.assertTrue(connection.isValid(0)); diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTest.java index 357506b3d17..2bc29a7df82 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTest.java @@ -28,7 +28,6 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; - import org.apache.arrow.driver.jdbc.authentication.UserPasswordAuthentication; import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler; import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty; @@ -42,27 +41,23 @@ import org.junit.ClassRule; import org.junit.Test; -/** - * Tests for {@link Connection}. - */ +/** Tests for {@link Connection}. */ public class ConnectionTest { - @ClassRule - public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE; + @ClassRule public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE; private static final MockFlightSqlProducer PRODUCER = new MockFlightSqlProducer(); private static final String userTest = "user1"; private static final String passTest = "pass1"; static { UserPasswordAuthentication authentication = - new UserPasswordAuthentication.Builder() - .user(userTest, passTest) - .build(); + new UserPasswordAuthentication.Builder().user(userTest, passTest).build(); - FLIGHT_SERVER_TEST_RULE = new FlightServerTestRule.Builder() - .authentication(authentication) - .producer(PRODUCER) - .build(); + FLIGHT_SERVER_TEST_RULE = + new FlightServerTestRule.Builder() + .authentication(authentication) + .producer(PRODUCER) + .build(); } private BufferAllocator allocator; @@ -79,8 +74,8 @@ public void tearDown() throws Exception { } /** - * Checks if an unencrypted connection can be established successfully when - * the provided valid credentials. + * Checks if an unencrypted connection can be established successfully when the provided valid + * credentials. * * @throws SQLException on error. */ @@ -90,58 +85,64 @@ public void testUnencryptedConnectionShouldOpenSuccessfullyWhenProvidedValidCred final Properties properties = new Properties(); properties.put(ArrowFlightConnectionProperty.HOST.camelName(), "localhost"); - properties.put(ArrowFlightConnectionProperty.PORT.camelName(), - FLIGHT_SERVER_TEST_RULE.getPort()); - properties.put(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); + properties.put( + ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_RULE.getPort()); + properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.put("useEncryption", false); - try (Connection connection = DriverManager.getConnection( - "jdbc:arrow-flight-sql://" + FLIGHT_SERVER_TEST_RULE.getHost() + ":" + - FLIGHT_SERVER_TEST_RULE.getPort(), properties)) { + try (Connection connection = + DriverManager.getConnection( + "jdbc:arrow-flight-sql://" + + FLIGHT_SERVER_TEST_RULE.getHost() + + ":" + + FLIGHT_SERVER_TEST_RULE.getPort(), + properties)) { Assert.assertTrue(connection.isValid(300)); } } /** - * Checks if a token is provided it takes precedence over username/pass. In this case, - * the connection should fail if a token is passed in. + * Checks if a token is provided it takes precedence over username/pass. In this case, the + * connection should fail if a token is passed in. */ @Test public void testTokenOverridesUsernameAndPasswordAuth() { final Properties properties = new Properties(); properties.put(ArrowFlightConnectionProperty.HOST.camelName(), "localhost"); - properties.put(ArrowFlightConnectionProperty.PORT.camelName(), - FLIGHT_SERVER_TEST_RULE.getPort()); - properties.put(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); + properties.put( + ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_RULE.getPort()); + properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.put(ArrowFlightConnectionProperty.TOKEN.camelName(), "token"); properties.put("useEncryption", false); - SQLException e = assertThrows(SQLException.class, () -> { - try (Connection conn = DriverManager.getConnection( - "jdbc:arrow-flight-sql://" + FLIGHT_SERVER_TEST_RULE.getHost() + ":" + FLIGHT_SERVER_TEST_RULE.getPort(), - properties)) { - Assert.fail(); - } - }); + SQLException e = + assertThrows( + SQLException.class, + () -> { + try (Connection conn = + DriverManager.getConnection( + "jdbc:arrow-flight-sql://" + + FLIGHT_SERVER_TEST_RULE.getHost() + + ":" + + FLIGHT_SERVER_TEST_RULE.getPort(), + properties)) { + Assert.fail(); + } + }); assertTrue(e.getMessage().contains("UNAUTHENTICATED")); } - /** - * Checks if the exception SQLException is thrown when trying to establish a connection without a host. + * Checks if the exception SQLException is thrown when trying to establish a connection without a + * host. * * @throws SQLException on error. */ @Test(expected = SQLException.class) - public void testUnencryptedConnectionWithEmptyHost() - throws Exception { + public void testUnencryptedConnectionWithEmptyHost() throws Exception { final Properties properties = new Properties(); properties.put("user", userTest); @@ -159,43 +160,38 @@ public void testUnencryptedConnectionWithEmptyHost() * @throws URISyntaxException on error. */ @Test - public void testGetBasicClientAuthenticatedShouldOpenConnection() - throws Exception { + public void testGetBasicClientAuthenticatedShouldOpenConnection() throws Exception { try (ArrowFlightSqlClientHandler client = - new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) - .withEncryption(false) - .withUsername(userTest) - .withPassword(passTest) - .withBufferAllocator(allocator) - .build()) { + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) + .withEncryption(false) + .withUsername(userTest) + .withPassword(passTest) + .withBufferAllocator(allocator) + .build()) { assertNotNull(client); } } /** - * Checks if the exception IllegalArgumentException is thrown when trying to establish an unencrypted - * connection providing with an invalid port. + * Checks if the exception IllegalArgumentException is thrown when trying to establish an + * unencrypted connection providing with an invalid port. * * @throws SQLException on error. */ @Test(expected = SQLException.class) - public void testUnencryptedConnectionProvidingInvalidPort() - throws Exception { + public void testUnencryptedConnectionProvidingInvalidPort() throws Exception { final Properties properties = new Properties(); properties.put(ArrowFlightConnectionProperty.HOST.camelName(), "localhost"); - properties.put(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); - properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), - false); - final String invalidUrl = "jdbc:arrow-flight-sql://" + FLIGHT_SERVER_TEST_RULE.getHost() + - ":" + 65537; + properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); + properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), false); + final String invalidUrl = + "jdbc:arrow-flight-sql://" + FLIGHT_SERVER_TEST_RULE.getHost() + ":" + 65537; try (Connection conn = DriverManager.getConnection(invalidUrl, properties)) { fail("Expected SQLException"); @@ -211,18 +207,18 @@ public void testUnencryptedConnectionProvidingInvalidPort() public void testGetBasicClientNoAuthShouldOpenConnection() throws Exception { try (ArrowFlightSqlClientHandler client = - new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withBufferAllocator(allocator) - .withEncryption(false) - .build()) { + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withBufferAllocator(allocator) + .withEncryption(false) + .build()) { assertNotNull(client); } } /** - * Checks if an unencrypted connection can be established successfully when - * not providing credentials. + * Checks if an unencrypted connection can be established successfully when not providing + * credentials. * * @throws SQLException on error. */ @@ -231,19 +227,17 @@ public void testUnencryptedConnectionShouldOpenSuccessfullyWithoutAuthentication throws Exception { final Properties properties = new Properties(); properties.put(ArrowFlightConnectionProperty.HOST.camelName(), "localhost"); - properties.put(ArrowFlightConnectionProperty.PORT.camelName(), - FLIGHT_SERVER_TEST_RULE.getPort()); - properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), - false); - try (Connection connection = DriverManager - .getConnection("jdbc:arrow-flight-sql://localhost:32010", properties)) { + properties.put( + ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_RULE.getPort()); + properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), false); + try (Connection connection = + DriverManager.getConnection("jdbc:arrow-flight-sql://localhost:32010", properties)) { Assert.assertTrue(connection.isValid(300)); } } /** - * Check if an unencrypted connection throws an exception when provided with - * invalid credentials. + * Check if an unencrypted connection throws an exception when provided with invalid credentials. * * @throws SQLException The exception expected to be thrown. */ @@ -254,17 +248,14 @@ public void testUnencryptedConnectionShouldThrowExceptionWhenProvidedWithInvalid final Properties properties = new Properties(); properties.put(ArrowFlightConnectionProperty.HOST.camelName(), "localhost"); - properties.put(ArrowFlightConnectionProperty.USER.camelName(), - "invalidUser"); - properties.put(ArrowFlightConnectionProperty.PORT.camelName(), - FLIGHT_SERVER_TEST_RULE.getPort()); - properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), - false); - properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), - "invalidPassword"); - - try (Connection ignored = DriverManager.getConnection("jdbc:arrow-flight-sql://localhost:32010", - properties)) { + properties.put(ArrowFlightConnectionProperty.USER.camelName(), "invalidUser"); + properties.put( + ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_RULE.getPort()); + properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), false); + properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), "invalidPassword"); + + try (Connection ignored = + DriverManager.getConnection("jdbc:arrow-flight-sql://localhost:32010", properties)) { Assert.fail(); } } @@ -280,12 +271,11 @@ public void testTLSConnectionPropertyFalseCorrectCastUrlWithDriverManager() thro final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); - try (Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s&useEncryption=false", - FLIGHT_SERVER_TEST_RULE.getPort(), - userTest, - passTest))) { + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s&useEncryption=false", + FLIGHT_SERVER_TEST_RULE.getPort(), userTest, passTest))) { Assert.assertTrue(connection.isValid(0)); } } @@ -297,24 +287,23 @@ public void testTLSConnectionPropertyFalseCorrectCastUrlWithDriverManager() thro * @throws Exception on error. */ @Test - public void testTLSConnectionPropertyFalseCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() - throws Exception { + public void + testTLSConnectionPropertyFalseCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); Properties properties = new Properties(); - properties.setProperty(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.setProperty(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); + properties.setProperty(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.setProperty(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.setProperty(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), "false"); - try (Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } @@ -332,17 +321,15 @@ public void testTLSConnectionPropertyFalseCorrectCastUrlAndPropertiesUsingPutWit DriverManager.registerDriver(driver); Properties properties = new Properties(); - properties.put(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); + properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), false); - try (Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } @@ -359,70 +346,67 @@ public void testTLSConnectionPropertyFalseIntegerCorrectCastUrlWithDriverManager final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); - try (Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s&useEncryption=0", - FLIGHT_SERVER_TEST_RULE.getPort(), - userTest, - passTest))) { + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s&useEncryption=0", + FLIGHT_SERVER_TEST_RULE.getPort(), userTest, passTest))) { Assert.assertTrue(connection.isValid(0)); } } /** * Check if an non-encrypted connection can be established successfully when connecting through - * the DriverManager using a connection url and properties with String K-V pairs and using - * 0 and 1 as ssl values. + * the DriverManager using a connection url and properties with String K-V pairs and using 0 and 1 + * as ssl values. * * @throws Exception on error. */ @Test - public void testTLSConnectionPropertyFalseIntegerCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() - throws Exception { + public void + testTLSConnectionPropertyFalseIntegerCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); Properties properties = new Properties(); - properties.setProperty(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.setProperty(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); + properties.setProperty(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.setProperty(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.setProperty(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), "0"); - try (Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } /** * Check if an non-encrypted connection can be established successfully when connecting through - * the DriverManager using a connection url and properties with Object K-V pairs and using - * 0 and 1 as ssl values. + * the DriverManager using a connection url and properties with Object K-V pairs and using 0 and 1 + * as ssl values. * * @throws Exception on error. */ @Test - public void testTLSConnectionPropertyFalseIntegerCorrectCastUrlAndPropertiesUsingPutWithDriverManager() - throws Exception { + public void + testTLSConnectionPropertyFalseIntegerCorrectCastUrlAndPropertiesUsingPutWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); Properties properties = new Properties(); - properties.put(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); + properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), 0); - try (Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } @@ -439,73 +423,69 @@ public void testThreadPoolSizeConnectionPropertyCorrectCastUrlWithDriverManager( final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); - try (Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s&threadPoolSize=1&useEncryption=%s", - FLIGHT_SERVER_TEST_RULE.getPort(), - userTest, - passTest, - false))) { + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s&threadPoolSize=1&useEncryption=%s", + FLIGHT_SERVER_TEST_RULE.getPort(), userTest, passTest, false))) { Assert.assertTrue(connection.isValid(0)); } } /** * Check if an non-encrypted connection can be established successfully when connecting through - * the DriverManager using a connection url and properties with String K-V pairs and using - * 0 and 1 as ssl values. + * the DriverManager using a connection url and properties with String K-V pairs and using 0 and 1 + * as ssl values. * * @throws Exception on error. */ @Test - public void testThreadPoolSizeConnectionPropertyCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() - throws Exception { + public void + testThreadPoolSizeConnectionPropertyCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); Properties properties = new Properties(); - properties.setProperty(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.setProperty(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); + properties.setProperty(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.setProperty(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.setProperty(ArrowFlightConnectionProperty.THREAD_POOL_SIZE.camelName(), "1"); properties.put("useEncryption", false); - try (Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } /** * Check if an non-encrypted connection can be established successfully when connecting through - * the DriverManager using a connection url and properties with Object K-V pairs and using - * 0 and 1 as ssl values. + * the DriverManager using a connection url and properties with Object K-V pairs and using 0 and 1 + * as ssl values. * * @throws Exception on error. */ @Test - public void testThreadPoolSizeConnectionPropertyCorrectCastUrlAndPropertiesUsingPutWithDriverManager() - throws Exception { + public void + testThreadPoolSizeConnectionPropertyCorrectCastUrlAndPropertiesUsingPutWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); Properties properties = new Properties(); - properties.put(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); + properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.put(ArrowFlightConnectionProperty.THREAD_POOL_SIZE.camelName(), 1); properties.put("useEncryption", false); - try (Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } @@ -522,71 +502,67 @@ public void testPasswordConnectionPropertyIntegerCorrectCastUrlWithDriverManager final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); - try (Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s&useEncryption=%s", - FLIGHT_SERVER_TEST_RULE.getPort(), - userTest, - passTest, - false))) { + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s&useEncryption=%s", + FLIGHT_SERVER_TEST_RULE.getPort(), userTest, passTest, false))) { Assert.assertTrue(connection.isValid(0)); } } /** * Check if an non-encrypted connection can be established successfully when connecting through - * the DriverManager using a connection url and properties with String K-V pairs and using - * 0 and 1 as ssl values. + * the DriverManager using a connection url and properties with String K-V pairs and using 0 and 1 + * as ssl values. * * @throws Exception on error. */ @Test - public void testPasswordConnectionPropertyIntegerCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() - throws Exception { + public void + testPasswordConnectionPropertyIntegerCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); Properties properties = new Properties(); - properties.setProperty(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.setProperty(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); + properties.setProperty(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.setProperty(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.put("useEncryption", false); - try (Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } /** * Check if an non-encrypted connection can be established successfully when connecting through - * the DriverManager using a connection url and properties with Object K-V pairs and using - * 0 and 1 as ssl values. + * the DriverManager using a connection url and properties with Object K-V pairs and using 0 and 1 + * as ssl values. * * @throws Exception on error. */ @Test - public void testPasswordConnectionPropertyIntegerCorrectCastUrlAndPropertiesUsingPutWithDriverManager() - throws Exception { + public void + testPasswordConnectionPropertyIntegerCorrectCastUrlAndPropertiesUsingPutWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); Properties properties = new Properties(); - properties.put(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); + properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.put("useEncryption", false); - try (Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsRootCertsTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsRootCertsTest.java index e5ffc2bcf79..8a142ba2646 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsRootCertsTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsRootCertsTest.java @@ -26,7 +26,6 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; - import org.apache.arrow.driver.jdbc.authentication.UserPasswordAuthentication; import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler; import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty; @@ -41,14 +40,10 @@ import org.junit.ClassRule; import org.junit.Test; - -/** - * Tests encrypted connections. - */ +/** Tests encrypted connections. */ public class ConnectionTlsRootCertsTest { - @ClassRule - public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE; + @ClassRule public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE; private static final String tlsRootCertsPath; private static final String badTlsRootCertsPath; private static final MockFlightSqlProducer PRODUCER = new MockFlightSqlProducer(); @@ -56,22 +51,22 @@ public class ConnectionTlsRootCertsTest { private static final String passTest = "pass1"; static { - final FlightSqlTestCertificates.CertKeyPair - certKey = FlightSqlTestCertificates.exampleTlsCerts().get(0); + final FlightSqlTestCertificates.CertKeyPair certKey = + FlightSqlTestCertificates.exampleTlsCerts().get(0); tlsRootCertsPath = certKey.cert.getPath(); badTlsRootCertsPath = certKey.cert.getPath() + ".bad"; - UserPasswordAuthentication authentication = new UserPasswordAuthentication.Builder() - .user(userTest, passTest) - .build(); + UserPasswordAuthentication authentication = + new UserPasswordAuthentication.Builder().user(userTest, passTest).build(); - FLIGHT_SERVER_TEST_RULE = new FlightServerTestRule.Builder() - .authentication(authentication) - .useEncryption(certKey.cert, certKey.key) - .producer(PRODUCER) - .build(); + FLIGHT_SERVER_TEST_RULE = + new FlightServerTestRule.Builder() + .authentication(authentication) + .useEncryption(certKey.cert, certKey.key) + .producer(PRODUCER) + .build(); } private BufferAllocator allocator; @@ -96,35 +91,38 @@ public void tearDown() throws Exception { public void testGetEncryptedClientAuthenticated() throws Exception { try (ArrowFlightSqlClientHandler client = - new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) - .withUsername(userTest) - .withPassword(passTest) - .withTlsRootCertificates(tlsRootCertsPath) - .withBufferAllocator(allocator) - .withEncryption(true) - .build()) { + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) + .withUsername(userTest) + .withPassword(passTest) + .withTlsRootCertificates(tlsRootCertsPath) + .withBufferAllocator(allocator) + .withEncryption(true) + .build()) { assertNotNull(client); } } /** - * Try to instantiate an encrypted FlightClient providing a bad TLS Root Certs Path. It's expected to - * receive the SQLException. + * Try to instantiate an encrypted FlightClient providing a bad TLS Root Certs Path. It's expected + * to receive the SQLException. */ @Test public void testGetEncryptedClientWithNoCertificateOnKeyStore() { - assertThrows(SQLException.class, () -> { - try (ArrowFlightSqlClientHandler handler = new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withTlsRootCertificates(badTlsRootCertsPath) - .withBufferAllocator(allocator) - .withEncryption(true) - .build()) { - Assert.fail(); - } - }); + assertThrows( + SQLException.class, + () -> { + try (ArrowFlightSqlClientHandler handler = + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withTlsRootCertificates(badTlsRootCertsPath) + .withBufferAllocator(allocator) + .withEncryption(true) + .build()) { + Assert.fail(); + } + }); } /** @@ -135,19 +133,19 @@ public void testGetEncryptedClientWithNoCertificateOnKeyStore() { @Test public void testGetNonAuthenticatedEncryptedClientNoAuth() throws Exception { try (ArrowFlightSqlClientHandler client = - new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withTlsRootCertificates(tlsRootCertsPath) - .withBufferAllocator(allocator) - .withEncryption(true) - .build()) { + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withTlsRootCertificates(tlsRootCertsPath) + .withBufferAllocator(allocator) + .withEncryption(true) + .build()) { assertNotNull(client); } } /** - * Check if an encrypted connection can be established successfully when the - * provided valid credentials and a valid TLS Root Certs path. + * Check if an encrypted connection can be established successfully when the provided valid + * credentials and a valid TLS Root Certs path. * * @throws Exception on error. */ @@ -156,14 +154,11 @@ public void testGetEncryptedConnectionWithValidCredentialsAndTlsRootsPath() thro final Properties properties = new Properties(); properties.put(ArrowFlightConnectionProperty.HOST.camelName(), "localhost"); - properties.put(ArrowFlightConnectionProperty.PORT.camelName(), - FLIGHT_SERVER_TEST_RULE.getPort()); - properties.put(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); - properties.put(ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), - tlsRootCertsPath); + properties.put( + ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_RULE.getPort()); + properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); + properties.put(ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); final ArrowFlightJdbcDataSource dataSource = ArrowFlightJdbcDataSource.createNewDataSource(properties); @@ -173,8 +168,8 @@ public void testGetEncryptedConnectionWithValidCredentialsAndTlsRootsPath() thro } /** - * Check if an encrypted connection can be established successfully when not - * providing authentication. + * Check if an encrypted connection can be established successfully when not providing + * authentication. * * @throws Exception on error. */ @@ -182,20 +177,23 @@ public void testGetEncryptedConnectionWithValidCredentialsAndTlsRootsPath() thro public void testGetNonAuthenticatedEncryptedConnection() throws Exception { final Properties properties = new Properties(); - properties.put(ArrowFlightConnectionProperty.HOST.camelName(), FLIGHT_SERVER_TEST_RULE.getHost()); - properties.put(ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_RULE.getPort()); + properties.put( + ArrowFlightConnectionProperty.HOST.camelName(), FLIGHT_SERVER_TEST_RULE.getHost()); + properties.put( + ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_RULE.getPort()); properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), true); properties.put(ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); - final ArrowFlightJdbcDataSource dataSource = ArrowFlightJdbcDataSource.createNewDataSource(properties); + final ArrowFlightJdbcDataSource dataSource = + ArrowFlightJdbcDataSource.createNewDataSource(properties); try (final Connection connection = dataSource.getConnection()) { Assert.assertTrue(connection.isValid(300)); } } /** - * Check if an encrypted connection can be established successfully when connecting through - * the DriverManager using just a connection url. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using just a connection url. * * @throws Exception on error. */ @@ -204,28 +202,30 @@ public void testTLSConnectionPropertyTrueCorrectCastUrlWithDriverManager() throw final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); - try (final Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s" + - "&useEncryption=true&%s=%s", - FLIGHT_SERVER_TEST_RULE.getPort(), - userTest, - passTest, - ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), - URLEncoder.encode(tlsRootCertsPath, "UTF-8")))) { + try (final Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s" + + "&useEncryption=true&%s=%s", + FLIGHT_SERVER_TEST_RULE.getPort(), + userTest, + passTest, + ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), + URLEncoder.encode(tlsRootCertsPath, "UTF-8")))) { Assert.assertTrue(connection.isValid(0)); } } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * a connection url and properties with String K-V pairs. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using a connection url and properties with String K-V pairs. * * @throws Exception on error. */ @Test - public void testTLSConnectionPropertyTrueCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() - throws Exception { + public void + testTLSConnectionPropertyTrueCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); @@ -233,21 +233,22 @@ public void testTLSConnectionPropertyTrueCorrectCastUrlAndPropertiesUsingSetProp properties.setProperty(ArrowFlightConnectionProperty.USER.camelName(), userTest); properties.setProperty(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); - properties.setProperty(ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); + properties.setProperty( + ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); properties.setProperty(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), "true"); - try (final Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (final Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * a connection url and properties with Object K-V pairs. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using a connection url and properties with Object K-V pairs. * * @throws Exception on error. */ @@ -264,18 +265,18 @@ public void testTLSConnectionPropertyTrueCorrectCastUrlAndPropertiesUsingPutWith properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), true); properties.put(ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); - try (final Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (final Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * just a connection url and using 0 and 1 as ssl values. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using just a connection url and using 0 and 1 as ssl values. * * @throws Exception on error. */ @@ -285,28 +286,31 @@ public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlWithDriverManager( final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); - try (final Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s" + - "&useEncryption=1&useSystemTrustStore=0&%s=%s", - FLIGHT_SERVER_TEST_RULE.getPort(), - userTest, - passTest, - ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), - URLEncoder.encode(tlsRootCertsPath, "UTF-8")))) { + try (final Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s" + + "&useEncryption=1&useSystemTrustStore=0&%s=%s", + FLIGHT_SERVER_TEST_RULE.getPort(), + userTest, + passTest, + ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), + URLEncoder.encode(tlsRootCertsPath, "UTF-8")))) { Assert.assertTrue(connection.isValid(0)); } } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * a connection url and properties with String K-V pairs and using 0 and 1 as ssl values. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using a connection url and properties with String K-V pairs and using 0 and 1 as + * ssl values. * * @throws Exception on error. */ @Test - public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() - throws Exception { + public void + testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); @@ -314,25 +318,30 @@ public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsing properties.setProperty(ArrowFlightConnectionProperty.USER.camelName(), userTest); properties.setProperty(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); - properties.setProperty(ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); + properties.setProperty( + ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); properties.setProperty(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), "1"); - try (final Connection connection = DriverManager.getConnection( - String.format("jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (final Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * a connection url and properties with Object K-V pairs and using 0 and 1 as ssl values. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using a connection url and properties with Object K-V pairs and using 0 and 1 as + * ssl values. * * @throws Exception on error. */ @Test - public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsingPutWithDriverManager() - throws Exception { + public void + testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsingPutWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); @@ -343,10 +352,11 @@ public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsing properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), 1); properties.put(ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(), tlsRootCertsPath); - try (final Connection connection = DriverManager.getConnection( - String.format("jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (final Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsTest.java index f5a7b68e06c..0d6adee2d46 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsTest.java @@ -26,7 +26,6 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; - import org.apache.arrow.driver.jdbc.authentication.UserPasswordAuthentication; import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler; import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty; @@ -42,30 +41,27 @@ import org.junit.ClassRule; import org.junit.Test; -/** - * Tests encrypted connections. - */ +/** Tests encrypted connections. */ public class ConnectionTlsTest { - @ClassRule - public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE; + @ClassRule public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE; private static final MockFlightSqlProducer PRODUCER = new MockFlightSqlProducer(); private static final String userTest = "user1"; private static final String passTest = "pass1"; static { - final FlightSqlTestCertificates.CertKeyPair - certKey = FlightSqlTestCertificates.exampleTlsCerts().get(0); + final FlightSqlTestCertificates.CertKeyPair certKey = + FlightSqlTestCertificates.exampleTlsCerts().get(0); - UserPasswordAuthentication authentication = new UserPasswordAuthentication.Builder() - .user(userTest, passTest) - .build(); + UserPasswordAuthentication authentication = + new UserPasswordAuthentication.Builder().user(userTest, passTest).build(); - FLIGHT_SERVER_TEST_RULE = new FlightServerTestRule.Builder() - .authentication(authentication) - .useEncryption(certKey.cert, certKey.key) - .producer(PRODUCER) - .build(); + FLIGHT_SERVER_TEST_RULE = + new FlightServerTestRule.Builder() + .authentication(authentication) + .useEncryption(certKey.cert, certKey.key) + .producer(PRODUCER) + .build(); } private String trustStorePath; @@ -75,10 +71,14 @@ public class ConnectionTlsTest { @Before public void setUp() throws Exception { - trustStorePath = Paths.get( - Preconditions.checkNotNull(getClass().getResource("/keys/keyStore.jks")).toURI()).toString(); - noCertificateKeyStorePath = Paths.get( - Preconditions.checkNotNull(getClass().getResource("/keys/noCertificate.jks")).toURI()).toString(); + trustStorePath = + Paths.get(Preconditions.checkNotNull(getClass().getResource("/keys/keyStore.jks")).toURI()) + .toString(); + noCertificateKeyStorePath = + Paths.get( + Preconditions.checkNotNull(getClass().getResource("/keys/noCertificate.jks")) + .toURI()) + .toString(); allocator = new RootAllocator(Long.MAX_VALUE); } @@ -97,15 +97,15 @@ public void tearDown() throws Exception { public void testGetEncryptedClientAuthenticatedWithDisableCertVerification() throws Exception { try (ArrowFlightSqlClientHandler client = - new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) - .withUsername(userTest) - .withPassword(passTest) - .withDisableCertificateVerification(true) - .withBufferAllocator(allocator) - .withEncryption(true) - .build()) { + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) + .withUsername(userTest) + .withPassword(passTest) + .withDisableCertificateVerification(true) + .withBufferAllocator(allocator) + .withEncryption(true) + .build()) { assertNotNull(client); } } @@ -119,24 +119,24 @@ public void testGetEncryptedClientAuthenticatedWithDisableCertVerification() thr public void testGetEncryptedClientAuthenticated() throws Exception { try (ArrowFlightSqlClientHandler client = - new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) - .withSystemTrustStore(false) - .withUsername(userTest) - .withPassword(passTest) - .withTrustStorePath(trustStorePath) - .withTrustStorePassword(trustStorePass) - .withBufferAllocator(allocator) - .withEncryption(true) - .build()) { + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) + .withSystemTrustStore(false) + .withUsername(userTest) + .withPassword(passTest) + .withTrustStorePath(trustStorePath) + .withTrustStorePassword(trustStorePass) + .withBufferAllocator(allocator) + .withEncryption(true) + .build()) { assertNotNull(client); } } /** - * Try to instantiate an encrypted FlightClient providing a keystore without certificate. It's expected to - * receive the SQLException. + * Try to instantiate an encrypted FlightClient providing a keystore without certificate. It's + * expected to receive the SQLException. * * @throws Exception on error. */ @@ -145,14 +145,14 @@ public void testGetEncryptedClientWithNoCertificateOnKeyStore() throws Exception final String noCertificateKeyStorePassword = "flight1"; try (ArrowFlightSqlClientHandler ignored = - new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withTrustStorePath(noCertificateKeyStorePath) - .withTrustStorePassword(noCertificateKeyStorePassword) - .withSystemTrustStore(false) - .withBufferAllocator(allocator) - .withEncryption(true) - .build()) { + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withTrustStorePath(noCertificateKeyStorePath) + .withTrustStorePassword(noCertificateKeyStorePassword) + .withSystemTrustStore(false) + .withBufferAllocator(allocator) + .withEncryption(true) + .build()) { Assert.fail(); } } @@ -165,14 +165,14 @@ public void testGetEncryptedClientWithNoCertificateOnKeyStore() throws Exception @Test public void testGetNonAuthenticatedEncryptedClientNoAuth() throws Exception { try (ArrowFlightSqlClientHandler client = - new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withSystemTrustStore(false) - .withTrustStorePath(trustStorePath) - .withTrustStorePassword(trustStorePass) - .withBufferAllocator(allocator) - .withEncryption(true) - .build()) { + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withSystemTrustStore(false) + .withTrustStorePath(trustStorePath) + .withTrustStorePassword(trustStorePass) + .withBufferAllocator(allocator) + .withEncryption(true) + .build()) { assertNotNull(client); } } @@ -188,21 +188,21 @@ public void testGetEncryptedClientWithKeyStoreBadPasswordAndNoAuth() throws Exce String keyStoreBadPassword = "badPassword"; try (ArrowFlightSqlClientHandler ignored = - new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withSystemTrustStore(false) - .withTrustStorePath(trustStorePath) - .withTrustStorePassword(keyStoreBadPassword) - .withBufferAllocator(allocator) - .withEncryption(true) - .build()) { + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withSystemTrustStore(false) + .withTrustStorePath(trustStorePath) + .withTrustStorePassword(keyStoreBadPassword) + .withBufferAllocator(allocator) + .withEncryption(true) + .build()) { Assert.fail(); } } /** - * Check if an encrypted connection can be established successfully when the - * provided valid credentials and a valid Keystore. + * Check if an encrypted connection can be established successfully when the provided valid + * credentials and a valid Keystore. * * @throws Exception on error. */ @@ -211,12 +211,10 @@ public void testGetEncryptedConnectionWithValidCredentialsAndKeyStore() throws E final Properties properties = new Properties(); properties.put(ArrowFlightConnectionProperty.HOST.camelName(), "localhost"); - properties.put(ArrowFlightConnectionProperty.PORT.camelName(), - FLIGHT_SERVER_TEST_RULE.getPort()); - properties.put(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); + properties.put( + ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_RULE.getPort()); + properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.put(ArrowFlightConnectionProperty.TRUST_STORE.camelName(), trustStorePath); properties.put(ArrowFlightConnectionProperty.USE_SYSTEM_TRUST_STORE.camelName(), false); properties.put(ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(), trustStorePass); @@ -229,8 +227,8 @@ public void testGetEncryptedConnectionWithValidCredentialsAndKeyStore() throws E } /** - * Check if the SQLException is thrown when trying to establish an encrypted connection - * providing valid credentials but invalid password to the Keystore. + * Check if the SQLException is thrown when trying to establish an encrypted connection providing + * valid credentials but invalid password to the Keystore. * * @throws SQLException on error. */ @@ -238,14 +236,12 @@ public void testGetEncryptedConnectionWithValidCredentialsAndKeyStore() throws E public void testGetAuthenticatedEncryptedConnectionWithKeyStoreBadPassword() throws Exception { final Properties properties = new Properties(); - properties.put(ArrowFlightConnectionProperty.HOST.camelName(), - FLIGHT_SERVER_TEST_RULE.getHost()); - properties.put(ArrowFlightConnectionProperty.PORT.camelName(), - FLIGHT_SERVER_TEST_RULE.getPort()); - properties.put(ArrowFlightConnectionProperty.USER.camelName(), - userTest); - properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), - passTest); + properties.put( + ArrowFlightConnectionProperty.HOST.camelName(), FLIGHT_SERVER_TEST_RULE.getHost()); + properties.put( + ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_RULE.getPort()); + properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest); + properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), true); properties.put(ArrowFlightConnectionProperty.TRUST_STORE.camelName(), trustStorePath); properties.put(ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(), "badpassword"); @@ -258,7 +254,8 @@ public void testGetAuthenticatedEncryptedConnectionWithKeyStoreBadPassword() thr } /** - * Check if an encrypted connection can be established successfully when not providing authentication. + * Check if an encrypted connection can be established successfully when not providing + * authentication. * * @throws Exception on error. */ @@ -266,22 +263,25 @@ public void testGetAuthenticatedEncryptedConnectionWithKeyStoreBadPassword() thr public void testGetNonAuthenticatedEncryptedConnection() throws Exception { final Properties properties = new Properties(); - properties.put(ArrowFlightConnectionProperty.HOST.camelName(), FLIGHT_SERVER_TEST_RULE.getHost()); - properties.put(ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_RULE.getPort()); + properties.put( + ArrowFlightConnectionProperty.HOST.camelName(), FLIGHT_SERVER_TEST_RULE.getHost()); + properties.put( + ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_RULE.getPort()); properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), true); properties.put(ArrowFlightConnectionProperty.USE_SYSTEM_TRUST_STORE.camelName(), false); properties.put(ArrowFlightConnectionProperty.TRUST_STORE.camelName(), trustStorePath); properties.put(ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(), trustStorePass); - final ArrowFlightJdbcDataSource dataSource = ArrowFlightJdbcDataSource.createNewDataSource(properties); + final ArrowFlightJdbcDataSource dataSource = + ArrowFlightJdbcDataSource.createNewDataSource(properties); try (final Connection connection = dataSource.getConnection()) { Assert.assertTrue(connection.isValid(300)); } } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * just a connection url. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using just a connection url. * * @throws Exception on error. */ @@ -290,30 +290,32 @@ public void testTLSConnectionPropertyTrueCorrectCastUrlWithDriverManager() throw final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); - try (final Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s" + - "&useEncryption=true&useSystemTrustStore=false&%s=%s&%s=%s", - FLIGHT_SERVER_TEST_RULE.getPort(), - userTest, - passTest, - ArrowFlightConnectionProperty.TRUST_STORE.camelName(), - URLEncoder.encode(trustStorePath, "UTF-8"), - ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(), - URLEncoder.encode(trustStorePass, "UTF-8")))) { + try (final Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s" + + "&useEncryption=true&useSystemTrustStore=false&%s=%s&%s=%s", + FLIGHT_SERVER_TEST_RULE.getPort(), + userTest, + passTest, + ArrowFlightConnectionProperty.TRUST_STORE.camelName(), + URLEncoder.encode(trustStorePath, "UTF-8"), + ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(), + URLEncoder.encode(trustStorePass, "UTF-8")))) { Assert.assertTrue(connection.isValid(0)); } } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * a connection url and properties with String K-V pairs. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using a connection url and properties with String K-V pairs. * * @throws Exception on error. */ @Test - public void testTLSConnectionPropertyTrueCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() - throws Exception { + public void + testTLSConnectionPropertyTrueCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); @@ -322,22 +324,24 @@ public void testTLSConnectionPropertyTrueCorrectCastUrlAndPropertiesUsingSetProp properties.setProperty(ArrowFlightConnectionProperty.USER.camelName(), userTest); properties.setProperty(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.setProperty(ArrowFlightConnectionProperty.TRUST_STORE.camelName(), trustStorePath); - properties.setProperty(ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(), trustStorePass); + properties.setProperty( + ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(), trustStorePass); properties.setProperty(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), "true"); - properties.setProperty(ArrowFlightConnectionProperty.USE_SYSTEM_TRUST_STORE.camelName(), "false"); - - try (final Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + properties.setProperty( + ArrowFlightConnectionProperty.USE_SYSTEM_TRUST_STORE.camelName(), "false"); + + try (final Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * a connection url and properties with Object K-V pairs. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using a connection url and properties with Object K-V pairs. * * @throws Exception on error. */ @@ -356,18 +360,18 @@ public void testTLSConnectionPropertyTrueCorrectCastUrlAndPropertiesUsingPutWith properties.put(ArrowFlightConnectionProperty.TRUST_STORE.camelName(), trustStorePath); properties.put(ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(), trustStorePass); - try (final Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (final Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * just a connection url and using 0 and 1 as ssl values. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using just a connection url and using 0 and 1 as ssl values. * * @throws Exception on error. */ @@ -377,30 +381,33 @@ public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlWithDriverManager( final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); - try (final Connection connection = DriverManager.getConnection( - String.format( - "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s" + - "&useEncryption=1&useSystemTrustStore=0&%s=%s&%s=%s", - FLIGHT_SERVER_TEST_RULE.getPort(), - userTest, - passTest, - ArrowFlightConnectionProperty.TRUST_STORE.camelName(), - URLEncoder.encode(trustStorePath, "UTF-8"), - ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(), - URLEncoder.encode(trustStorePass, "UTF-8")))) { + try (final Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s" + + "&useEncryption=1&useSystemTrustStore=0&%s=%s&%s=%s", + FLIGHT_SERVER_TEST_RULE.getPort(), + userTest, + passTest, + ArrowFlightConnectionProperty.TRUST_STORE.camelName(), + URLEncoder.encode(trustStorePath, "UTF-8"), + ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(), + URLEncoder.encode(trustStorePass, "UTF-8")))) { Assert.assertTrue(connection.isValid(0)); } } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * a connection url and properties with String K-V pairs and using 0 and 1 as ssl values. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using a connection url and properties with String K-V pairs and using 0 and 1 as + * ssl values. * * @throws Exception on error. */ @Test - public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() - throws Exception { + public void + testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsingSetPropertyWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); @@ -409,26 +416,31 @@ public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsing properties.setProperty(ArrowFlightConnectionProperty.USER.camelName(), userTest); properties.setProperty(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest); properties.setProperty(ArrowFlightConnectionProperty.TRUST_STORE.camelName(), trustStorePath); - properties.setProperty(ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(), trustStorePass); + properties.setProperty( + ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(), trustStorePass); properties.setProperty(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), "1"); properties.setProperty(ArrowFlightConnectionProperty.USE_SYSTEM_TRUST_STORE.camelName(), "0"); - try (final Connection connection = DriverManager.getConnection( - String.format("jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (final Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } /** - * Check if an encrypted connection can be established successfully when connecting through the DriverManager using - * a connection url and properties with Object K-V pairs and using 0 and 1 as ssl values. + * Check if an encrypted connection can be established successfully when connecting through the + * DriverManager using a connection url and properties with Object K-V pairs and using 0 and 1 as + * ssl values. * * @throws Exception on error. */ @Test - public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsingPutWithDriverManager() - throws Exception { + public void + testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsingPutWithDriverManager() + throws Exception { final Driver driver = new ArrowFlightJdbcDriver(); DriverManager.registerDriver(driver); @@ -441,10 +453,11 @@ public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsing properties.put(ArrowFlightConnectionProperty.TRUST_STORE.camelName(), trustStorePath); properties.put(ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(), trustStorePass); - try (final Connection connection = DriverManager.getConnection( - String.format("jdbc:arrow-flight-sql://localhost:%s", - FLIGHT_SERVER_TEST_RULE.getPort()), - properties)) { + try (final Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://localhost:%s", FLIGHT_SERVER_TEST_RULE.getPort()), + properties)) { Assert.assertTrue(connection.isValid(0)); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/FlightServerTestRule.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/FlightServerTestRule.java index 39eb0a29866..e794250c344 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/FlightServerTestRule.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/FlightServerTestRule.java @@ -27,7 +27,6 @@ import java.util.ArrayDeque; import java.util.Deque; import java.util.Properties; - import org.apache.arrow.driver.jdbc.authentication.Authentication; import org.apache.arrow.driver.jdbc.authentication.TokenAuthentication; import org.apache.arrow.driver.jdbc.authentication.UserPasswordAuthentication; @@ -51,8 +50,8 @@ import org.slf4j.LoggerFactory; /** - * Utility class for unit tests that need to instantiate a {@link FlightServer} - * and interact with it. + * Utility class for unit tests that need to instantiate a {@link FlightServer} and interact with + * it. */ public class FlightServerTestRule implements TestRule, AutoCloseable { public static final String DEFAULT_USER = "flight-test-user"; @@ -70,13 +69,14 @@ public class FlightServerTestRule implements TestRule, AutoCloseable { private final MiddlewareCookie.Factory middlewareCookieFactory = new MiddlewareCookie.Factory(); - private FlightServerTestRule(final Properties properties, - final ArrowFlightConnectionConfigImpl config, - final BufferAllocator allocator, - final FlightSqlProducer producer, - final Authentication authentication, - final CertKeyPair certKeyPair, - final File mTlsCACert) { + private FlightServerTestRule( + final Properties properties, + final ArrowFlightConnectionConfigImpl config, + final BufferAllocator allocator, + final FlightSqlProducer producer, + final Authentication authentication, + final CertKeyPair certKeyPair, + final File mTlsCACert) { this.properties = Preconditions.checkNotNull(properties); this.config = Preconditions.checkNotNull(config); this.allocator = Preconditions.checkNotNull(allocator); @@ -94,14 +94,9 @@ private FlightServerTestRule(final Properties properties, */ public static FlightServerTestRule createStandardTestRule(final FlightSqlProducer producer) { UserPasswordAuthentication authentication = - new UserPasswordAuthentication.Builder() - .user(DEFAULT_USER, DEFAULT_PASSWORD) - .build(); - - return new Builder() - .authentication(authentication) - .producer(producer) - .build(); + new UserPasswordAuthentication.Builder().user(DEFAULT_USER, DEFAULT_PASSWORD).build(); + + return new Builder().authentication(authentication).producer(producer).build(); } ArrowFlightJdbcDataSource createDataSource() { @@ -112,7 +107,8 @@ public ArrowFlightJdbcConnectionPoolDataSource createConnectionPoolDataSource() return ArrowFlightJdbcConnectionPoolDataSource.createNewDataSource(properties); } - public ArrowFlightJdbcConnectionPoolDataSource createConnectionPoolDataSource(boolean useEncryption) { + public ArrowFlightJdbcConnectionPoolDataSource createConnectionPoolDataSource( + boolean useEncryption) { setUseEncryption(useEncryption); return ArrowFlightJdbcConnectionPoolDataSource.createNewDataSource(properties); } @@ -142,9 +138,10 @@ public interface CheckedFunction { } private FlightServer initiateServer(Location location) throws IOException { - FlightServer.Builder builder = FlightServer.builder(allocator, location, producer) - .headerAuthenticator(authentication.authenticate()) - .middleware(FlightServerMiddleware.Key.of("KEY"), middlewareCookieFactory); + FlightServer.Builder builder = + FlightServer.builder(allocator, location, producer) + .headerAuthenticator(authentication.authenticate()) + .middleware(FlightServerMiddleware.Key.of("KEY"), middlewareCookieFactory); if (certKeyPair != null) { builder.useTls(certKeyPair.cert, certKeyPair.key); } @@ -170,12 +167,13 @@ public void evaluate() throws Throwable { }; } - private FlightServer getStartServer(CheckedFunction newServerFromLocation, - int retries) + private FlightServer getStartServer( + CheckedFunction newServerFromLocation, int retries) throws IOException { final Deque exceptions = new ArrayDeque<>(); for (; retries > 0; retries--) { - final FlightServer server = newServerFromLocation.apply(Location.forGrpcInsecure("localhost", 0)); + final FlightServer server = + newServerFromLocation.apply(Location.forGrpcInsecure("localhost", 0)); try { Method start = server.getClass().getMethod("start"); start.setAccessible(true); @@ -213,9 +211,7 @@ public void close() throws Exception { AutoCloseables.close(allocator); } - /** - * Builder for {@link FlightServerTestRule}. - */ + /** Builder for {@link FlightServerTestRule}. */ public static final class Builder { private final Properties properties; private FlightSqlProducer producer; @@ -240,9 +236,8 @@ public Builder producer(final FlightSqlProducer producer) { } /** - * Sets the type of the authentication that will be used in the server rules. - * There are two types of authentication: {@link UserPasswordAuthentication} and - * {@link TokenAuthentication}. + * Sets the type of the authentication that will be used in the server rules. There are two + * types of authentication: {@link UserPasswordAuthentication} and {@link TokenAuthentication}. * * @param authentication the type of authentication. * @return the Builder. @@ -256,7 +251,7 @@ public Builder authentication(final Authentication authentication) { * Enable TLS on the server. * * @param certChain The certificate chain to use. - * @param key The private key to use. + * @param key The private key to use. * @return the Builder. */ public Builder useEncryption(final File certChain, final File key) { @@ -282,14 +277,20 @@ public Builder useMTlsClientVerification(final File mTlsCACert) { */ public FlightServerTestRule build() { authentication.populateProperties(properties); - return new FlightServerTestRule(properties, new ArrowFlightConnectionConfigImpl(properties), - new RootAllocator(Long.MAX_VALUE), producer, authentication, certKeyPair, mTlsCACert); + return new FlightServerTestRule( + properties, + new ArrowFlightConnectionConfigImpl(properties), + new RootAllocator(Long.MAX_VALUE), + producer, + authentication, + certKeyPair, + mTlsCACert); } } /** - * A middleware to handle with the cookies in the server. It is used to test if cookies are - * being sent properly. + * A middleware to handle with the cookies in the server. It is used to test if cookies are being + * sent properly. */ static class MiddlewareCookie implements FlightServerMiddleware { @@ -307,26 +308,20 @@ public void onBeforeSendingHeaders(CallHeaders callHeaders) { } @Override - public void onCallCompleted(CallStatus callStatus) { - - } + public void onCallCompleted(CallStatus callStatus) {} @Override - public void onCallErrored(Throwable throwable) { + public void onCallErrored(Throwable throwable) {} - } - - /** - * A factory for the MiddlewareCookie. - */ + /** A factory for the MiddlewareCookie. */ static class Factory implements FlightServerMiddleware.Factory { private boolean receivedCookieHeader = false; private String cookie; @Override - public MiddlewareCookie onCallStarted(CallInfo callInfo, CallHeaders callHeaders, - RequestContext requestContext) { + public MiddlewareCookie onCallStarted( + CallInfo callInfo, CallHeaders callHeaders, RequestContext requestContext) { cookie = callHeaders.get("Cookie"); receivedCookieHeader = null != cookie; return new MiddlewareCookie(this); @@ -337,5 +332,4 @@ public String getCookie() { } } } - } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetMetadataTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetMetadataTest.java index 64ec7f7d9e1..a20d4457c1e 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetMetadataTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetMetadataTest.java @@ -26,7 +26,6 @@ import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; - import org.apache.arrow.driver.jdbc.utils.CoreMockedSqlProducers; import org.hamcrest.CoreMatchers; import org.junit.AfterClass; @@ -42,20 +41,19 @@ public class ResultSetMetadataTest { private static Connection connection; - @Rule - public ErrorCollector collector = new ErrorCollector(); + @Rule public ErrorCollector collector = new ErrorCollector(); @ClassRule - public static final FlightServerTestRule SERVER_TEST_RULE = FlightServerTestRule - .createStandardTestRule(CoreMockedSqlProducers.getLegacyProducer()); + public static final FlightServerTestRule SERVER_TEST_RULE = + FlightServerTestRule.createStandardTestRule(CoreMockedSqlProducers.getLegacyProducer()); @BeforeClass public static void setup() throws SQLException { connection = SERVER_TEST_RULE.getConnection(false); try (Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery( - CoreMockedSqlProducers.LEGACY_METADATA_SQL_CMD)) { + ResultSet resultSet = + statement.executeQuery(CoreMockedSqlProducers.LEGACY_METADATA_SQL_CMD)) { metadata = resultSet.getMetaData(); } } @@ -65,9 +63,7 @@ public static void teardown() throws SQLException { connection.close(); } - /** - * Test if {@link ResultSetMetaData} object is not null. - */ + /** Test if {@link ResultSetMetaData} object is not null. */ @Test public void testShouldGetResultSetMetadata() { collector.checkThat(metadata, CoreMatchers.is(notNullValue())); @@ -86,7 +82,7 @@ public void testShouldGetColumnCount() throws SQLException { } /** - * Test if {@link ResultSetMetaData#getColumnTypeName(int)} returns the correct type name for each + * Test if {@link ResultSetMetaData#getColumnTypeName(int)} returns the correct type name for each * column. * * @throws SQLException in case of error. @@ -103,7 +99,8 @@ public void testShouldGetColumnTypesName() throws SQLException { } /** - * Test if {@link ResultSetMetaData#getColumnTypeName(int)} passing an column index that does not exist. + * Test if {@link ResultSetMetaData#getColumnTypeName(int)} passing an column index that does not + * exist. * * @throws SQLException in case of error. */ @@ -130,9 +127,9 @@ public void testShouldGetColumnNames() throws SQLException { collector.checkThat(thirdColumn, equalTo("float2")); } - /** - * Test {@link ResultSetMetaData#getColumnTypeName(int)} passing an column index that does not exist. + * Test {@link ResultSetMetaData#getColumnTypeName(int)} passing an column index that does not + * exist. * * @throws SQLException in case of error. */ @@ -223,7 +220,8 @@ public void testShouldIsSearchable() throws SQLException { } /** - * Test if {@link ResultSetMetaData#getColumnTypeName(int)} passing an column index that does not exist. + * Test if {@link ResultSetMetaData#getColumnTypeName(int)} passing an column index that does not + * exist. * * @throws SQLException in case of error. */ diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java index 0e3e015a046..9c0f7220473 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java @@ -30,6 +30,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.common.collect.ImmutableSet; import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.DriverManager; @@ -44,7 +45,6 @@ import java.util.Random; import java.util.Set; import java.util.concurrent.CountDownLatch; - import org.apache.arrow.driver.jdbc.utils.CoreMockedSqlProducers; import org.apache.arrow.driver.jdbc.utils.PartitionedFlightSqlProducer; import org.apache.arrow.flight.FlightEndpoint; @@ -65,17 +65,16 @@ import org.junit.Test; import org.junit.rules.ErrorCollector; -import com.google.common.collect.ImmutableSet; - public class ResultSetTest { private static final Random RANDOM = new Random(10); + @ClassRule - public static final FlightServerTestRule SERVER_TEST_RULE = FlightServerTestRule - .createStandardTestRule(CoreMockedSqlProducers.getLegacyProducer()); + public static final FlightServerTestRule SERVER_TEST_RULE = + FlightServerTestRule.createStandardTestRule(CoreMockedSqlProducers.getLegacyProducer()); + private static Connection connection; - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); @BeforeClass public static void setup() throws SQLException { @@ -102,8 +101,8 @@ private static void resultSetNextUntilDone(ResultSet resultSet) throws SQLExcept @Test public void testShouldRunSelectQuery() throws Exception { try (Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery( - CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { + ResultSet resultSet = + statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { CoreMockedSqlProducers.assertLegacyRegularSqlResultSet(resultSet, collector); } } @@ -111,8 +110,8 @@ public void testShouldRunSelectQuery() throws Exception { @Test public void testShouldExecuteQueryNotBlockIfClosedBeforeEnd() throws Exception { try (Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery( - CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { + ResultSet resultSet = + statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { for (int i = 0; i < 7500; i++) { assertTrue(resultSet.next()); @@ -121,16 +120,16 @@ public void testShouldExecuteQueryNotBlockIfClosedBeforeEnd() throws Exception { } /** - * Tests whether the {@link ArrowFlightJdbcDriver} query only returns only the - * amount of value set by {@link org.apache.calcite.avatica.AvaticaStatement#setMaxRows(int)}. + * Tests whether the {@link ArrowFlightJdbcDriver} query only returns only the amount of value set + * by {@link org.apache.calcite.avatica.AvaticaStatement#setMaxRows(int)}. * * @throws Exception If the connection fails to be established. */ @Test public void testShouldRunSelectQuerySettingMaxRowLimit() throws Exception { try (Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery( - CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { + ResultSet resultSet = + statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { final int maxRowsLimit = 3; statement.setMaxRows(maxRowsLimit); @@ -151,8 +150,7 @@ public void testShouldRunSelectQuerySettingMaxRowLimit() throws Exception { } /** - * Tests whether the {@link ArrowFlightJdbcDriver} fails upon attempting - * to run an invalid query. + * Tests whether the {@link ArrowFlightJdbcDriver} fails upon attempting to run an invalid query. * * @throws Exception If the connection fails to be established. */ @@ -160,22 +158,22 @@ public void testShouldRunSelectQuerySettingMaxRowLimit() throws Exception { public void testShouldThrowExceptionUponAttemptingToExecuteAnInvalidSelectQuery() throws Exception { try (Statement statement = connection.createStatement(); - ResultSet result = statement.executeQuery("SELECT * FROM SHOULD-FAIL")) { + ResultSet result = statement.executeQuery("SELECT * FROM SHOULD-FAIL")) { fail(); } } /** - * Tests whether the {@link ArrowFlightJdbcDriver} query only returns only the - * amount of value set by {@link org.apache.calcite.avatica.AvaticaStatement#setLargeMaxRows(long)} (int)}. + * Tests whether the {@link ArrowFlightJdbcDriver} query only returns only the amount of value set + * by {@link org.apache.calcite.avatica.AvaticaStatement#setLargeMaxRows(long)} (int)}. * * @throws Exception If the connection fails to be established. */ @Test public void testShouldRunSelectQuerySettingLargeMaxRowLimit() throws Exception { try (Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery( - CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { + ResultSet resultSet = + statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { final long maxRowsLimit = 3; statement.setLargeMaxRows(maxRowsLimit); @@ -199,8 +197,8 @@ public void testColumnCountShouldRemainConsistentForResultSetThroughoutEntireDur throws SQLException { final Set counts = new HashSet<>(); try (final Statement statement = connection.createStatement(); - final ResultSet resultSet = statement.executeQuery( - CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { + final ResultSet resultSet = + statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { while (resultSet.next()) { counts.add(resultSet.getMetaData().getColumnCount()); } @@ -217,7 +215,8 @@ public void testColumnCountShouldRemainConsistentForResultSetThroughoutEntireDur @Test public void testShouldCloseStatementWhenIsCloseOnCompletion() throws Exception { try (Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { + ResultSet resultSet = + statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { statement.closeOnCompletion(); @@ -228,15 +227,17 @@ public void testShouldCloseStatementWhenIsCloseOnCompletion() throws Exception { } /** - * Tests whether the {@link ArrowFlightJdbcDriver} close the statement after complete ResultSet with max rows limit - * when call {@link org.apache.calcite.avatica.AvaticaStatement#closeOnCompletion()}. + * Tests whether the {@link ArrowFlightJdbcDriver} close the statement after complete ResultSet + * with max rows limit when call {@link + * org.apache.calcite.avatica.AvaticaStatement#closeOnCompletion()}. * * @throws Exception If the connection fails to be established. */ @Test public void testShouldCloseStatementWhenIsCloseOnCompletionWithMaxRowsLimit() throws Exception { try (Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { + ResultSet resultSet = + statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { final long maxRowsLimit = 3; statement.setLargeMaxRows(maxRowsLimit); @@ -249,8 +250,9 @@ public void testShouldCloseStatementWhenIsCloseOnCompletionWithMaxRowsLimit() th } /** - * Tests whether the {@link ArrowFlightJdbcDriver} not close the statement after complete ResultSet with max rows - * limit when call {@link org.apache.calcite.avatica.AvaticaStatement#closeOnCompletion()}. + * Tests whether the {@link ArrowFlightJdbcDriver} not close the statement after complete + * ResultSet with max rows limit when call {@link + * org.apache.calcite.avatica.AvaticaStatement#closeOnCompletion()}. * * @throws Exception If the connection fails to be established. */ @@ -258,8 +260,8 @@ public void testShouldCloseStatementWhenIsCloseOnCompletionWithMaxRowsLimit() th public void testShouldNotCloseStatementWhenIsNotCloseOnCompletionWithMaxRowsLimit() throws Exception { try (Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery( - CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { + ResultSet resultSet = + statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { final long maxRowsLimit = 3; statement.setLargeMaxRows(maxRowsLimit); @@ -274,8 +276,8 @@ public void testShouldNotCloseStatementWhenIsNotCloseOnCompletionWithMaxRowsLimi @Test public void testShouldCancelQueryUponCancelAfterQueryingResultSet() throws SQLException { try (final Statement statement = connection.createStatement(); - final ResultSet resultSet = statement.executeQuery( - CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { + final ResultSet resultSet = + statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { final int column = RANDOM.nextInt(resultSet.getMetaData().getColumnCount()) + 1; collector.checkThat(resultSet.isClosed(), is(false)); collector.checkThat(resultSet.next(), is(true)); @@ -294,20 +296,22 @@ public void testShouldInterruptFlightStreamsIfQueryIsCancelledMidQuerying() try (final Statement statement = connection.createStatement()) { final CountDownLatch latch = new CountDownLatch(1); final Set exceptions = synchronizedSet(new HashSet<>(1)); - final Thread thread = new Thread(() -> { - try (final ResultSet resultSet = statement.executeQuery( - CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { - final int cachedColumnCount = resultSet.getMetaData().getColumnCount(); - Thread.sleep(300); - while (resultSet.next()) { - resultSet.getObject(RANDOM.nextInt(cachedColumnCount) + 1); - } - } catch (final SQLException | InterruptedException e) { - exceptions.add(e); - } finally { - latch.countDown(); - } - }); + final Thread thread = + new Thread( + () -> { + try (final ResultSet resultSet = + statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { + final int cachedColumnCount = resultSet.getMetaData().getColumnCount(); + Thread.sleep(300); + while (resultSet.next()) { + resultSet.getObject(RANDOM.nextInt(cachedColumnCount) + 1); + } + } catch (final SQLException | InterruptedException e) { + exceptions.add(e); + } finally { + latch.countDown(); + } + }); thread.setName("Test Case: interrupt query execution before first retrieval"); thread.start(); statement.cancel(); @@ -324,18 +328,21 @@ public void testShouldInterruptFlightStreamsIfQueryIsCancelledMidQuerying() } @Test - public void testShouldInterruptFlightStreamsIfQueryIsCancelledMidProcessingForTimeConsumingQueries() - throws SQLException, InterruptedException { + public void + testShouldInterruptFlightStreamsIfQueryIsCancelledMidProcessingForTimeConsumingQueries() + throws SQLException, InterruptedException { final String query = CoreMockedSqlProducers.LEGACY_CANCELLATION_SQL_CMD; try (final Statement statement = connection.createStatement()) { final Set exceptions = synchronizedSet(new HashSet<>(1)); - final Thread thread = new Thread(() -> { - try (final ResultSet ignored = statement.executeQuery(query)) { - fail(); - } catch (final SQLException e) { - exceptions.add(e); - } - }); + final Thread thread = + new Thread( + () -> { + try (final ResultSet ignored = statement.executeQuery(query)) { + fail(); + } catch (final SQLException e) { + exceptions.add(e); + } + }); thread.setName("Test Case: interrupt query execution mid-process"); thread.setPriority(Thread.MAX_PRIORITY); thread.start(); @@ -349,8 +356,10 @@ public void testShouldInterruptFlightStreamsIfQueryIsCancelledMidProcessingForTi .reduce(StringBuilder::append) .orElseThrow(IllegalStateException::new) .toString(), - anyOf(is(format("Error while executing SQL \"%s\": Query canceled", query)), - allOf(containsString(format("Error while executing SQL \"%s\"", query)), + anyOf( + is(format("Error while executing SQL \"%s\": Query canceled", query)), + allOf( + containsString(format("Error while executing SQL \"%s\"", query)), containsString("CANCELLED")))); } } @@ -368,14 +377,11 @@ public void testShouldInterruptFlightStreamsIfQueryTimeoutIsOver() throws SQLExc } catch (final Exception e) { exceptions.add(e); } - final Throwable comparisonCause = exceptions.stream() - .findFirst() - .orElseThrow(RuntimeException::new) - .getCause() - .getCause(); - collector.checkThat(comparisonCause, - is(instanceOf(SQLTimeoutException.class))); - collector.checkThat(comparisonCause.getMessage(), + final Throwable comparisonCause = + exceptions.stream().findFirst().orElseThrow(RuntimeException::new).getCause().getCause(); + collector.checkThat(comparisonCause, is(instanceOf(SQLTimeoutException.class))); + collector.checkThat( + comparisonCause.getMessage(), is(format("Query timed out after %d %s", timeoutValue, timeoutUnit))); } } @@ -395,8 +401,9 @@ public void testFlightStreamsQueryShouldNotTimeout() throws SQLException { @Test public void testPartitionedFlightServer() throws Exception { // Arrange - final Schema schema = new Schema( - Arrays.asList(Field.nullablePrimitive("int_column", new ArrowType.Int(32, true)))); + final Schema schema = + new Schema( + Arrays.asList(Field.nullablePrimitive("int_column", new ArrowType.Int(32, true)))); try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); VectorSchemaRoot firstPartition = VectorSchemaRoot.create(schema, allocator); VectorSchemaRoot secondPartition = VectorSchemaRoot.create(schema, allocator)) { @@ -406,48 +413,55 @@ public void testPartitionedFlightServer() throws Exception { ((IntVector) secondPartition.getVector(0)).set(0, 2); // Construct the data-only nodes first. - FlightProducer firstProducer = new PartitionedFlightSqlProducer.DataOnlyFlightSqlProducer( - new Ticket("first".getBytes(StandardCharsets.UTF_8)), firstPartition); - FlightProducer secondProducer = new PartitionedFlightSqlProducer.DataOnlyFlightSqlProducer( - new Ticket("second".getBytes(StandardCharsets.UTF_8)), secondPartition); + FlightProducer firstProducer = + new PartitionedFlightSqlProducer.DataOnlyFlightSqlProducer( + new Ticket("first".getBytes(StandardCharsets.UTF_8)), firstPartition); + FlightProducer secondProducer = + new PartitionedFlightSqlProducer.DataOnlyFlightSqlProducer( + new Ticket("second".getBytes(StandardCharsets.UTF_8)), secondPartition); - final FlightServer.Builder firstBuilder = FlightServer.builder( - allocator, forGrpcInsecure("localhost", 0), firstProducer); + final FlightServer.Builder firstBuilder = + FlightServer.builder(allocator, forGrpcInsecure("localhost", 0), firstProducer); - final FlightServer.Builder secondBuilder = FlightServer.builder( - allocator, forGrpcInsecure("localhost", 0), secondProducer); + final FlightServer.Builder secondBuilder = + FlightServer.builder(allocator, forGrpcInsecure("localhost", 0), secondProducer); // Run the data-only nodes so that we can get the Locations they are running at. try (FlightServer firstServer = firstBuilder.build(); - FlightServer secondServer = secondBuilder.build()) { + FlightServer secondServer = secondBuilder.build()) { firstServer.start(); secondServer.start(); final FlightEndpoint firstEndpoint = - new FlightEndpoint(new Ticket("first".getBytes(StandardCharsets.UTF_8)), firstServer.getLocation()); + new FlightEndpoint( + new Ticket("first".getBytes(StandardCharsets.UTF_8)), firstServer.getLocation()); final FlightEndpoint secondEndpoint = - new FlightEndpoint(new Ticket("second".getBytes(StandardCharsets.UTF_8)), secondServer.getLocation()); + new FlightEndpoint( + new Ticket("second".getBytes(StandardCharsets.UTF_8)), secondServer.getLocation()); // Finally start the root node. - try (final PartitionedFlightSqlProducer rootProducer = new PartitionedFlightSqlProducer( - schema, firstEndpoint, secondEndpoint); - FlightServer rootServer = FlightServer.builder( - allocator, forGrpcInsecure("localhost", 0), rootProducer) - .build() - .start(); - Connection newConnection = DriverManager.getConnection(String.format( - "jdbc:arrow-flight-sql://%s:%d/?useEncryption=false", - rootServer.getLocation().getUri().getHost(), rootServer.getPort())); - Statement newStatement = newConnection.createStatement(); - // Act - ResultSet result = newStatement.executeQuery("Select partitioned_data")) { + try (final PartitionedFlightSqlProducer rootProducer = + new PartitionedFlightSqlProducer(schema, firstEndpoint, secondEndpoint); + FlightServer rootServer = + FlightServer.builder(allocator, forGrpcInsecure("localhost", 0), rootProducer) + .build() + .start(); + Connection newConnection = + DriverManager.getConnection( + String.format( + "jdbc:arrow-flight-sql://%s:%d/?useEncryption=false", + rootServer.getLocation().getUri().getHost(), rootServer.getPort())); + Statement newStatement = newConnection.createStatement(); + // Act + ResultSet result = newStatement.executeQuery("Select partitioned_data")) { List resultData = new ArrayList<>(); while (result.next()) { resultData.add(result.getInt(1)); } // Assert - assertEquals(firstPartition.getRowCount() + secondPartition.getRowCount(), resultData.size()); + assertEquals( + firstPartition.getRowCount() + secondPartition.getRowCount(), resultData.size()); assertTrue(resultData.contains(((IntVector) firstPartition.getVector(0)).get(0))); assertTrue(resultData.contains(((IntVector) secondPartition.getVector(0)).get(0))); } @@ -458,8 +472,8 @@ allocator, forGrpcInsecure("localhost", 0), rootProducer) @Test public void testShouldRunSelectQueryWithEmptyVectorsEmbedded() throws Exception { try (Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery( - CoreMockedSqlProducers.LEGACY_REGULAR_WITH_EMPTY_SQL_CMD)) { + ResultSet resultSet = + statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_WITH_EMPTY_SQL_CMD)) { long rowCount = 0; while (resultSet.next()) { ++rowCount; @@ -471,10 +485,11 @@ public void testShouldRunSelectQueryWithEmptyVectorsEmbedded() throws Exception @Test public void testResultSetAppMetadata() throws Exception { try (Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery( - CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { - assertArrayEquals(((ArrowFlightJdbcFlightStreamResultSet) resultSet).getAppMetadata(), - "foo".getBytes(StandardCharsets.UTF_8)); + ResultSet resultSet = + statement.executeQuery(CoreMockedSqlProducers.LEGACY_REGULAR_SQL_CMD)) { + assertArrayEquals( + ((ArrowFlightJdbcFlightStreamResultSet) resultSet).getAppMetadata(), + "foo".getBytes(StandardCharsets.UTF_8)); } } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/TokenAuthenticationTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/TokenAuthenticationTest.java index 9fe506231ec..7525cf14670 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/TokenAuthenticationTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/TokenAuthenticationTest.java @@ -19,7 +19,6 @@ import java.sql.Connection; import java.sql.SQLException; - import org.apache.arrow.driver.jdbc.authentication.TokenAuthentication; import org.apache.arrow.driver.jdbc.utils.MockFlightSqlProducer; import org.apache.arrow.util.AutoCloseables; @@ -31,16 +30,14 @@ public class TokenAuthenticationTest { private static final MockFlightSqlProducer FLIGHT_SQL_PRODUCER = new MockFlightSqlProducer(); - @ClassRule - public static FlightServerTestRule FLIGHT_SERVER_TEST_RULE; + @ClassRule public static FlightServerTestRule FLIGHT_SERVER_TEST_RULE; static { - FLIGHT_SERVER_TEST_RULE = new FlightServerTestRule.Builder() - .authentication(new TokenAuthentication.Builder() - .token("1234") - .build()) - .producer(FLIGHT_SQL_PRODUCER) - .build(); + FLIGHT_SERVER_TEST_RULE = + new FlightServerTestRule.Builder() + .authentication(new TokenAuthentication.Builder().token("1234").build()) + .producer(FLIGHT_SQL_PRODUCER) + .build(); } @AfterClass diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorFactoryTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorFactoryTest.java index 4b3744372c0..6c222bdeead 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorFactoryTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorFactoryTest.java @@ -18,7 +18,6 @@ package org.apache.arrow.driver.jdbc.accessor; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.impl.binary.ArrowFlightJdbcBinaryVectorAccessor; import org.apache.arrow.driver.jdbc.accessor.impl.calendar.ArrowFlightJdbcDateVectorAccessor; import org.apache.arrow.driver.jdbc.accessor.impl.calendar.ArrowFlightJdbcDurationVectorAccessor; @@ -66,9 +65,8 @@ public class ArrowFlightJdbcAccessorFactoryTest { public void createAccessorForUInt1Vector() { try (ValueVector valueVector = rootAllocatorTestRule.createUInt1Vector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcBaseIntVectorAccessor); } @@ -78,9 +76,8 @@ public void createAccessorForUInt1Vector() { public void createAccessorForUInt2Vector() { try (ValueVector valueVector = rootAllocatorTestRule.createUInt2Vector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcBaseIntVectorAccessor); } @@ -90,9 +87,8 @@ public void createAccessorForUInt2Vector() { public void createAccessorForUInt4Vector() { try (ValueVector valueVector = rootAllocatorTestRule.createUInt4Vector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcBaseIntVectorAccessor); } @@ -102,9 +98,8 @@ public void createAccessorForUInt4Vector() { public void createAccessorForUInt8Vector() { try (ValueVector valueVector = rootAllocatorTestRule.createUInt8Vector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcBaseIntVectorAccessor); } @@ -114,9 +109,8 @@ public void createAccessorForUInt8Vector() { public void createAccessorForTinyIntVector() { try (ValueVector valueVector = rootAllocatorTestRule.createTinyIntVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcBaseIntVectorAccessor); } @@ -126,9 +120,8 @@ public void createAccessorForTinyIntVector() { public void createAccessorForSmallIntVector() { try (ValueVector valueVector = rootAllocatorTestRule.createSmallIntVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcBaseIntVectorAccessor); } @@ -138,9 +131,8 @@ public void createAccessorForSmallIntVector() { public void createAccessorForIntVector() { try (ValueVector valueVector = rootAllocatorTestRule.createIntVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcBaseIntVectorAccessor); } @@ -150,9 +142,8 @@ public void createAccessorForIntVector() { public void createAccessorForBigIntVector() { try (ValueVector valueVector = rootAllocatorTestRule.createBigIntVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcBaseIntVectorAccessor); } @@ -162,9 +153,8 @@ public void createAccessorForBigIntVector() { public void createAccessorForFloat4Vector() { try (ValueVector valueVector = rootAllocatorTestRule.createFloat4Vector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcFloat4VectorAccessor); } @@ -174,9 +164,8 @@ public void createAccessorForFloat4Vector() { public void createAccessorForFloat8Vector() { try (ValueVector valueVector = rootAllocatorTestRule.createFloat8Vector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcFloat8VectorAccessor); } @@ -186,9 +175,8 @@ public void createAccessorForFloat8Vector() { public void createAccessorForBitVector() { try (ValueVector valueVector = rootAllocatorTestRule.createBitVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcBitVectorAccessor); } @@ -198,9 +186,8 @@ public void createAccessorForBitVector() { public void createAccessorForDecimalVector() { try (ValueVector valueVector = rootAllocatorTestRule.createDecimalVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcDecimalVectorAccessor); } @@ -210,9 +197,8 @@ public void createAccessorForDecimalVector() { public void createAccessorForDecimal256Vector() { try (ValueVector valueVector = rootAllocatorTestRule.createDecimal256Vector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcDecimalVectorAccessor); } @@ -222,9 +208,8 @@ public void createAccessorForDecimal256Vector() { public void createAccessorForVarBinaryVector() { try (ValueVector valueVector = rootAllocatorTestRule.createVarBinaryVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcBinaryVectorAccessor); } @@ -234,9 +219,8 @@ public void createAccessorForVarBinaryVector() { public void createAccessorForLargeVarBinaryVector() { try (ValueVector valueVector = rootAllocatorTestRule.createLargeVarBinaryVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcBinaryVectorAccessor); } @@ -246,9 +230,8 @@ public void createAccessorForLargeVarBinaryVector() { public void createAccessorForFixedSizeBinaryVector() { try (ValueVector valueVector = rootAllocatorTestRule.createFixedSizeBinaryVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcBinaryVectorAccessor); } @@ -258,9 +241,8 @@ public void createAccessorForFixedSizeBinaryVector() { public void createAccessorForTimeStampVector() { try (ValueVector valueVector = rootAllocatorTestRule.createTimeStampMilliVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcTimeStampVectorAccessor); } @@ -270,9 +252,8 @@ public void createAccessorForTimeStampVector() { public void createAccessorForTimeNanoVector() { try (ValueVector valueVector = rootAllocatorTestRule.createTimeNanoVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcTimeVectorAccessor); } @@ -282,9 +263,8 @@ public void createAccessorForTimeNanoVector() { public void createAccessorForTimeMicroVector() { try (ValueVector valueVector = rootAllocatorTestRule.createTimeMicroVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcTimeVectorAccessor); } @@ -294,9 +274,8 @@ public void createAccessorForTimeMicroVector() { public void createAccessorForTimeMilliVector() { try (ValueVector valueVector = rootAllocatorTestRule.createTimeMilliVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcTimeVectorAccessor); } @@ -306,9 +285,8 @@ public void createAccessorForTimeMilliVector() { public void createAccessorForTimeSecVector() { try (ValueVector valueVector = rootAllocatorTestRule.createTimeSecVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcTimeVectorAccessor); } @@ -318,9 +296,8 @@ public void createAccessorForTimeSecVector() { public void createAccessorForDateDayVector() { try (ValueVector valueVector = rootAllocatorTestRule.createDateDayVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcDateVectorAccessor); } @@ -330,9 +307,8 @@ public void createAccessorForDateDayVector() { public void createAccessorForDateMilliVector() { try (ValueVector valueVector = rootAllocatorTestRule.createDateMilliVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcDateVectorAccessor); } @@ -340,12 +316,11 @@ public void createAccessorForDateMilliVector() { @Test public void createAccessorForVarCharVector() { - try ( - ValueVector valueVector = new VarCharVector("", rootAllocatorTestRule.getRootAllocator())) { + try (ValueVector valueVector = + new VarCharVector("", rootAllocatorTestRule.getRootAllocator())) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcVarCharVectorAccessor); } @@ -353,12 +328,11 @@ public void createAccessorForVarCharVector() { @Test public void createAccessorForLargeVarCharVector() { - try (ValueVector valueVector = new LargeVarCharVector("", - rootAllocatorTestRule.getRootAllocator())) { + try (ValueVector valueVector = + new LargeVarCharVector("", rootAllocatorTestRule.getRootAllocator())) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcVarCharVectorAccessor); } @@ -367,13 +341,13 @@ public void createAccessorForLargeVarCharVector() { @Test public void createAccessorForDurationVector() { try (ValueVector valueVector = - new DurationVector("", - new FieldType(true, new ArrowType.Duration(TimeUnit.MILLISECOND), null), - rootAllocatorTestRule.getRootAllocator())) { + new DurationVector( + "", + new FieldType(true, new ArrowType.Duration(TimeUnit.MILLISECOND), null), + rootAllocatorTestRule.getRootAllocator())) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcDurationVectorAccessor); } @@ -381,12 +355,11 @@ public void createAccessorForDurationVector() { @Test public void createAccessorForIntervalDayVector() { - try (ValueVector valueVector = new IntervalDayVector("", - rootAllocatorTestRule.getRootAllocator())) { + try (ValueVector valueVector = + new IntervalDayVector("", rootAllocatorTestRule.getRootAllocator())) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcIntervalVectorAccessor); } @@ -394,12 +367,11 @@ public void createAccessorForIntervalDayVector() { @Test public void createAccessorForIntervalYearVector() { - try (ValueVector valueVector = new IntervalYearVector("", - rootAllocatorTestRule.getRootAllocator())) { + try (ValueVector valueVector = + new IntervalYearVector("", rootAllocatorTestRule.getRootAllocator())) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcIntervalVectorAccessor); } @@ -407,12 +379,11 @@ public void createAccessorForIntervalYearVector() { @Test public void createAccessorForUnionVector() { - try (ValueVector valueVector = new UnionVector("", rootAllocatorTestRule.getRootAllocator(), - null, null)) { + try (ValueVector valueVector = + new UnionVector("", rootAllocatorTestRule.getRootAllocator(), null, null)) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcUnionVectorAccessor); } @@ -420,13 +391,11 @@ public void createAccessorForUnionVector() { @Test public void createAccessorForDenseUnionVector() { - try ( - ValueVector valueVector = new DenseUnionVector("", rootAllocatorTestRule.getRootAllocator(), - null, null)) { + try (ValueVector valueVector = + new DenseUnionVector("", rootAllocatorTestRule.getRootAllocator(), null, null)) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcDenseUnionVectorAccessor); } @@ -434,12 +403,11 @@ public void createAccessorForDenseUnionVector() { @Test public void createAccessorForStructVector() { - try (ValueVector valueVector = StructVector.empty("", - rootAllocatorTestRule.getRootAllocator())) { + try (ValueVector valueVector = + StructVector.empty("", rootAllocatorTestRule.getRootAllocator())) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcStructVectorAccessor); } @@ -449,9 +417,8 @@ public void createAccessorForStructVector() { public void createAccessorForListVector() { try (ValueVector valueVector = rootAllocatorTestRule.createListVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcListVectorAccessor); } @@ -461,9 +428,8 @@ public void createAccessorForListVector() { public void createAccessorForLargeListVector() { try (ValueVector valueVector = rootAllocatorTestRule.createLargeListVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcLargeListVectorAccessor); } @@ -473,9 +439,8 @@ public void createAccessorForLargeListVector() { public void createAccessorForFixedSizeListVector() { try (ValueVector valueVector = rootAllocatorTestRule.createFixedSizeListVector()) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcFixedSizeListVectorAccessor); } @@ -483,12 +448,11 @@ public void createAccessorForFixedSizeListVector() { @Test public void createAccessorForMapVector() { - try (ValueVector valueVector = MapVector.empty("", rootAllocatorTestRule.getRootAllocator(), - true)) { + try (ValueVector valueVector = + MapVector.empty("", rootAllocatorTestRule.getRootAllocator(), true)) { ArrowFlightJdbcAccessor accessor = - ArrowFlightJdbcAccessorFactory.createAccessor(valueVector, GET_CURRENT_ROW, - (boolean wasNull) -> { - }); + ArrowFlightJdbcAccessorFactory.createAccessor( + valueVector, GET_CURRENT_ROW, (boolean wasNull) -> {}); Assert.assertTrue(accessor instanceof ArrowFlightJdbcMapVectorAccessor); } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorTest.java index a9b5c46e01e..e45553bcd73 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorTest.java @@ -25,7 +25,6 @@ import java.sql.SQLException; import java.util.HashMap; import java.util.Map; - import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -38,8 +37,7 @@ public class ArrowFlightJdbcAccessorTest { static class MockedArrowFlightJdbcAccessor extends ArrowFlightJdbcAccessor { protected MockedArrowFlightJdbcAccessor() { - super(() -> 0, (boolean wasNull) -> { - }); + super(() -> 0, (boolean wasNull) -> {}); } @Override @@ -48,8 +46,7 @@ public Class getObjectClass() { } } - @Mock - MockedArrowFlightJdbcAccessor accessor; + @Mock MockedArrowFlightJdbcAccessor accessor; @Test public void testShouldGetObjectWithByteClassReturnGetByte() throws SQLException { diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/ArrowFlightJdbcNullVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/ArrowFlightJdbcNullVectorAccessorTest.java index 57e7ecfe025..56f8adf704c 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/ArrowFlightJdbcNullVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/ArrowFlightJdbcNullVectorAccessorTest.java @@ -23,8 +23,7 @@ public class ArrowFlightJdbcNullVectorAccessorTest { ArrowFlightJdbcNullVectorAccessor accessor = - new ArrowFlightJdbcNullVectorAccessor((boolean wasNull) -> { - }); + new ArrowFlightJdbcNullVectorAccessor((boolean wasNull) -> {}); @Test public void testShouldWasNullReturnTrue() { diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/binary/ArrowFlightJdbcBinaryVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/binary/ArrowFlightJdbcBinaryVectorAccessorTest.java index f4d256c4cf8..43132071047 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/binary/ArrowFlightJdbcBinaryVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/binary/ArrowFlightJdbcBinaryVectorAccessorTest.java @@ -26,7 +26,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.function.Supplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; @@ -51,47 +50,53 @@ public class ArrowFlightJdbcBinaryVectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private ValueVector vector; private final Supplier vectorSupplier; private final AccessorTestUtils.AccessorSupplier - accessorSupplier = (vector, getCurrentRow) -> { - ArrowFlightJdbcAccessorFactory.WasNullConsumer noOpWasNullConsumer = (boolean wasNull) -> { - }; - if (vector instanceof VarBinaryVector) { - return new ArrowFlightJdbcBinaryVectorAccessor(((VarBinaryVector) vector), getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof LargeVarBinaryVector) { - return new ArrowFlightJdbcBinaryVectorAccessor(((LargeVarBinaryVector) vector), - getCurrentRow, noOpWasNullConsumer); - } else if (vector instanceof FixedSizeBinaryVector) { - return new ArrowFlightJdbcBinaryVectorAccessor(((FixedSizeBinaryVector) vector), - getCurrentRow, noOpWasNullConsumer); - } - return null; - }; + accessorSupplier = + (vector, getCurrentRow) -> { + ArrowFlightJdbcAccessorFactory.WasNullConsumer noOpWasNullConsumer = + (boolean wasNull) -> {}; + if (vector instanceof VarBinaryVector) { + return new ArrowFlightJdbcBinaryVectorAccessor( + ((VarBinaryVector) vector), getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof LargeVarBinaryVector) { + return new ArrowFlightJdbcBinaryVectorAccessor( + ((LargeVarBinaryVector) vector), getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof FixedSizeBinaryVector) { + return new ArrowFlightJdbcBinaryVectorAccessor( + ((FixedSizeBinaryVector) vector), getCurrentRow, noOpWasNullConsumer); + } + return null; + }; private final AccessorTestUtils.AccessorIterator - accessorIterator = - new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); + accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @Parameterized.Parameters(name = "{1}") public static Collection data() { - return Arrays.asList(new Object[][] { - {(Supplier) () -> rootAllocatorTestRule.createVarBinaryVector(), - "VarBinaryVector"}, - {(Supplier) () -> rootAllocatorTestRule.createLargeVarBinaryVector(), - "LargeVarBinaryVector"}, - {(Supplier) () -> rootAllocatorTestRule.createFixedSizeBinaryVector(), - "FixedSizeBinaryVector"}, - }); - } - - public ArrowFlightJdbcBinaryVectorAccessorTest(Supplier vectorSupplier, - String vectorType) { + return Arrays.asList( + new Object[][] { + { + (Supplier) () -> rootAllocatorTestRule.createVarBinaryVector(), + "VarBinaryVector" + }, + { + (Supplier) () -> rootAllocatorTestRule.createLargeVarBinaryVector(), + "LargeVarBinaryVector" + }, + { + (Supplier) () -> rootAllocatorTestRule.createFixedSizeBinaryVector(), + "FixedSizeBinaryVector" + }, + }); + } + + public ArrowFlightJdbcBinaryVectorAccessorTest( + Supplier vectorSupplier, String vectorType) { this.vectorSupplier = vectorSupplier; } @@ -107,7 +112,9 @@ public void tearDown() { @Test public void testShouldGetStringReturnExpectedString() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcBinaryVectorAccessor::getString, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcBinaryVectorAccessor::getString, (accessor) -> is(new String(accessor.getBytes(), UTF_8))); } @@ -116,14 +123,15 @@ public void testShouldGetStringReturnNull() throws Exception { vector.reset(); vector.setValueCount(5); - accessorIterator - .assertAccessorGetter(vector, ArrowFlightJdbcBinaryVectorAccessor::getString, - CoreMatchers.nullValue()); + accessorIterator.assertAccessorGetter( + vector, ArrowFlightJdbcBinaryVectorAccessor::getString, CoreMatchers.nullValue()); } @Test public void testShouldGetBytesReturnExpectedByteArray() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcBinaryVectorAccessor::getBytes, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcBinaryVectorAccessor::getBytes, (accessor, currentRow) -> { if (vector instanceof VarBinaryVector) { return is(((VarBinaryVector) vector).get(currentRow)); @@ -148,7 +156,9 @@ public void testShouldGetBytesReturnNull() throws Exception { @Test public void testShouldGetObjectReturnAsGetBytes() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcBinaryVectorAccessor::getObject, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcBinaryVectorAccessor::getObject, (accessor) -> is(accessor.getBytes())); } @@ -164,12 +174,14 @@ public void testShouldGetObjectReturnNull() { @Test public void testShouldGetUnicodeStreamReturnCorrectInputStream() throws Exception { - accessorIterator.iterate(vector, (accessor, currentRow) -> { - InputStream inputStream = accessor.getUnicodeStream(); - String actualString = IOUtils.toString(inputStream, UTF_8); - collector.checkThat(accessor.wasNull(), is(false)); - collector.checkThat(actualString, is(accessor.getString())); - }); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + InputStream inputStream = accessor.getUnicodeStream(); + String actualString = IOUtils.toString(inputStream, UTF_8); + collector.checkThat(accessor.wasNull(), is(false)); + collector.checkThat(actualString, is(accessor.getString())); + }); } @Test @@ -184,12 +196,14 @@ public void testShouldGetUnicodeStreamReturnNull() throws Exception { @Test public void testShouldGetAsciiStreamReturnCorrectInputStream() throws Exception { - accessorIterator.iterate(vector, (accessor, currentRow) -> { - InputStream inputStream = accessor.getAsciiStream(); - String actualString = IOUtils.toString(inputStream, US_ASCII); - collector.checkThat(accessor.wasNull(), is(false)); - collector.checkThat(actualString, is(accessor.getString())); - }); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + InputStream inputStream = accessor.getAsciiStream(); + String actualString = IOUtils.toString(inputStream, US_ASCII); + collector.checkThat(accessor.wasNull(), is(false)); + collector.checkThat(actualString, is(accessor.getString())); + }); } @Test @@ -204,12 +218,14 @@ public void testShouldGetAsciiStreamReturnNull() throws Exception { @Test public void testShouldGetBinaryStreamReturnCurrentInputStream() throws Exception { - accessorIterator.iterate(vector, (accessor, currentRow) -> { - InputStream inputStream = accessor.getBinaryStream(); - String actualString = IOUtils.toString(inputStream, UTF_8); - collector.checkThat(accessor.wasNull(), is(false)); - collector.checkThat(actualString, is(accessor.getString())); - }); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + InputStream inputStream = accessor.getBinaryStream(); + String actualString = IOUtils.toString(inputStream, UTF_8); + collector.checkThat(accessor.wasNull(), is(false)); + collector.checkThat(actualString, is(accessor.getString())); + }); } @Test @@ -224,12 +240,14 @@ public void testShouldGetBinaryStreamReturnNull() throws Exception { @Test public void testShouldGetCharacterStreamReturnCorrectReader() throws Exception { - accessorIterator.iterate(vector, (accessor, currentRow) -> { - Reader characterStream = accessor.getCharacterStream(); - String actualString = IOUtils.toString(characterStream); - collector.checkThat(accessor.wasNull(), is(false)); - collector.checkThat(actualString, is(accessor.getString())); - }); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + Reader characterStream = accessor.getCharacterStream(); + String actualString = IOUtils.toString(characterStream); + collector.checkThat(accessor.wasNull(), is(false)); + collector.checkThat(actualString, is(accessor.getString())); + }); } @Test diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDateVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDateVectorAccessorTest.java index 36af5134626..54e8ecc3569 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDateVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDateVectorAccessorTest.java @@ -31,7 +31,6 @@ import java.util.TimeZone; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; - import org.apache.arrow.driver.jdbc.accessor.impl.text.ArrowFlightJdbcVarCharVectorAccessor; import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; @@ -58,42 +57,44 @@ public class ArrowFlightJdbcDateVectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private BaseFixedWidthVector vector; private final Supplier vectorSupplier; private final AccessorTestUtils.AccessorSupplier - accessorSupplier = (vector, getCurrentRow) -> { - if (vector instanceof DateDayVector) { - return new ArrowFlightJdbcDateVectorAccessor((DateDayVector) vector, getCurrentRow, - (boolean wasNull) -> { - }); - } else if (vector instanceof DateMilliVector) { - return new ArrowFlightJdbcDateVectorAccessor((DateMilliVector) vector, getCurrentRow, - (boolean wasNull) -> { - }); - } - return null; - }; + accessorSupplier = + (vector, getCurrentRow) -> { + if (vector instanceof DateDayVector) { + return new ArrowFlightJdbcDateVectorAccessor( + (DateDayVector) vector, getCurrentRow, (boolean wasNull) -> {}); + } else if (vector instanceof DateMilliVector) { + return new ArrowFlightJdbcDateVectorAccessor( + (DateMilliVector) vector, getCurrentRow, (boolean wasNull) -> {}); + } + return null; + }; private final AccessorTestUtils.AccessorIterator - accessorIterator = - new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); + accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @Parameterized.Parameters(name = "{1}") public static Collection data() { - return Arrays.asList(new Object[][] { - {(Supplier) () -> rootAllocatorTestRule.createDateDayVector(), - "DateDayVector"}, - {(Supplier) () -> rootAllocatorTestRule.createDateMilliVector(), - "DateMilliVector"}, - }); + return Arrays.asList( + new Object[][] { + { + (Supplier) () -> rootAllocatorTestRule.createDateDayVector(), + "DateDayVector" + }, + { + (Supplier) () -> rootAllocatorTestRule.createDateMilliVector(), + "DateMilliVector" + }, + }); } - public ArrowFlightJdbcDateVectorAccessorTest(Supplier vectorSupplier, - String vectorType) { + public ArrowFlightJdbcDateVectorAccessorTest( + Supplier vectorSupplier, String vectorType) { this.vectorSupplier = vectorSupplier; } @@ -109,13 +110,17 @@ public void tearDown() { @Test public void testShouldGetTimestampReturnValidTimestampWithoutCalendar() throws Exception { - accessorIterator.assertAccessorGetter(vector, accessor -> accessor.getTimestamp(null), + accessorIterator.assertAccessorGetter( + vector, + accessor -> accessor.getTimestamp(null), (accessor, currentRow) -> is(getTimestampForVector(currentRow))); } @Test public void testShouldGetObjectWithDateClassReturnValidDateWithoutCalendar() throws Exception { - accessorIterator.assertAccessorGetter(vector, accessor -> accessor.getObject(Date.class), + accessorIterator.assertAccessorGetter( + vector, + accessor -> accessor.getObject(Date.class), (accessor, currentRow) -> is(new Date(getTimestampForVector(currentRow).getTime()))); } @@ -124,15 +129,17 @@ public void testShouldGetTimestampReturnValidTimestampWithCalendar() throws Exce TimeZone timeZone = TimeZone.getTimeZone(AMERICA_VANCOUVER); Calendar calendar = Calendar.getInstance(timeZone); - accessorIterator.iterate(vector, (accessor, currentRow) -> { - final Timestamp resultWithoutCalendar = accessor.getTimestamp(null); - final Timestamp result = accessor.getTimestamp(calendar); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + final Timestamp resultWithoutCalendar = accessor.getTimestamp(null); + final Timestamp result = accessor.getTimestamp(calendar); - long offset = timeZone.getOffset(resultWithoutCalendar.getTime()); + long offset = timeZone.getOffset(resultWithoutCalendar.getTime()); - collector.checkThat(resultWithoutCalendar.getTime() - result.getTime(), is(offset)); - collector.checkThat(accessor.wasNull(), is(false)); - }); + collector.checkThat(resultWithoutCalendar.getTime() - result.getTime(), is(offset)); + collector.checkThat(accessor.wasNull(), is(false)); + }); } @Test @@ -145,7 +152,9 @@ public void testShouldGetTimestampReturnNull() { @Test public void testShouldGetDateReturnValidDateWithoutCalendar() throws Exception { - accessorIterator.assertAccessorGetter(vector, accessor -> accessor.getDate(null), + accessorIterator.assertAccessorGetter( + vector, + accessor -> accessor.getDate(null), (accessor, currentRow) -> is(new Date(getTimestampForVector(currentRow).getTime()))); } @@ -154,15 +163,17 @@ public void testShouldGetDateReturnValidDateWithCalendar() throws Exception { TimeZone timeZone = TimeZone.getTimeZone(AMERICA_VANCOUVER); Calendar calendar = Calendar.getInstance(timeZone); - accessorIterator.iterate(vector, (accessor, currentRow) -> { - final Date resultWithoutCalendar = accessor.getDate(null); - final Date result = accessor.getDate(calendar); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + final Date resultWithoutCalendar = accessor.getDate(null); + final Date result = accessor.getDate(calendar); - long offset = timeZone.getOffset(resultWithoutCalendar.getTime()); + long offset = timeZone.getOffset(resultWithoutCalendar.getTime()); - collector.checkThat(resultWithoutCalendar.getTime() - result.getTime(), is(offset)); - collector.checkThat(accessor.wasNull(), is(false)); - }); + collector.checkThat(resultWithoutCalendar.getTime() - result.getTime(), is(offset)); + collector.checkThat(accessor.wasNull(), is(false)); + }); } @Test @@ -190,9 +201,8 @@ private Timestamp getTimestampForVector(int currentRow) { @Test public void testShouldGetObjectClass() throws Exception { - accessorIterator - .assertAccessorGetter(vector, ArrowFlightJdbcDateVectorAccessor::getObjectClass, - equalTo(Date.class)); + accessorIterator.assertAccessorGetter( + vector, ArrowFlightJdbcDateVectorAccessor::getObjectClass, equalTo(Date.class)); } @Test @@ -208,47 +218,51 @@ public void testShouldGetStringBeConsistentWithVarCharAccessorWithCalendar() thr @Test public void testValidateGetStringTimeZoneConsistency() throws Exception { - accessorIterator.iterate(vector, (accessor, currentRow) -> { - final TimeZone defaultTz = TimeZone.getDefault(); - try { - final String string = accessor.getString(); // Should always be UTC as no calendar is provided - - // Validate with UTC - Date date = accessor.getDate(null); - TimeZone.setDefault(TimeZone.getTimeZone("UTC")); - collector.checkThat(date.toString(), is(string)); - - // Validate with different TZ - TimeZone.setDefault(TimeZone.getTimeZone(AMERICA_VANCOUVER)); - collector.checkThat(date.toString(), not(string)); - - collector.checkThat(accessor.wasNull(), is(false)); - } finally { - // Set default Tz back - TimeZone.setDefault(defaultTz); - } - }); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + final TimeZone defaultTz = TimeZone.getDefault(); + try { + final String string = + accessor.getString(); // Should always be UTC as no calendar is provided + + // Validate with UTC + Date date = accessor.getDate(null); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + collector.checkThat(date.toString(), is(string)); + + // Validate with different TZ + TimeZone.setDefault(TimeZone.getTimeZone(AMERICA_VANCOUVER)); + collector.checkThat(date.toString(), not(string)); + + collector.checkThat(accessor.wasNull(), is(false)); + } finally { + // Set default Tz back + TimeZone.setDefault(defaultTz); + } + }); } private void assertGetStringIsConsistentWithVarCharAccessor(Calendar calendar) throws Exception { - try (VarCharVector varCharVector = new VarCharVector("", - rootAllocatorTestRule.getRootAllocator())) { + try (VarCharVector varCharVector = + new VarCharVector("", rootAllocatorTestRule.getRootAllocator())) { varCharVector.allocateNew(1); ArrowFlightJdbcVarCharVectorAccessor varCharVectorAccessor = - new ArrowFlightJdbcVarCharVectorAccessor(varCharVector, () -> 0, (boolean wasNull) -> { - }); + new ArrowFlightJdbcVarCharVectorAccessor(varCharVector, () -> 0, (boolean wasNull) -> {}); - accessorIterator.iterate(vector, (accessor, currentRow) -> { - final String string = accessor.getString(); - varCharVector.set(0, new Text(string)); - varCharVector.setValueCount(1); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + final String string = accessor.getString(); + varCharVector.set(0, new Text(string)); + varCharVector.setValueCount(1); - Date dateFromVarChar = varCharVectorAccessor.getDate(calendar); - Date date = accessor.getDate(calendar); + Date dateFromVarChar = varCharVectorAccessor.getDate(calendar); + Date date = accessor.getDate(calendar); - collector.checkThat(date, is(dateFromVarChar)); - collector.checkThat(accessor.wasNull(), is(false)); - }); + collector.checkThat(date, is(dateFromVarChar)); + collector.checkThat(accessor.wasNull(), is(false)); + }); } } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDurationVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDurationVectorAccessorTest.java index 64ddb573f1b..b2895bc4679 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDurationVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcDurationVectorAccessorTest.java @@ -21,7 +21,6 @@ import static org.hamcrest.CoreMatchers.is; import java.time.Duration; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; @@ -41,20 +40,18 @@ public class ArrowFlightJdbcDurationVectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private DurationVector vector; private final AccessorTestUtils.AccessorSupplier accessorSupplier = - (vector, getCurrentRow) -> new ArrowFlightJdbcDurationVectorAccessor((DurationVector) vector, - getCurrentRow, (boolean wasNull) -> { - }); + (vector, getCurrentRow) -> + new ArrowFlightJdbcDurationVectorAccessor( + (DurationVector) vector, getCurrentRow, (boolean wasNull) -> {}); private final AccessorTestUtils.AccessorIterator - accessorIterator = - new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); + accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @Before public void setup() { @@ -75,7 +72,9 @@ public void tearDown() { @Test public void getObject() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcDurationVectorAccessor::getObject, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcDurationVectorAccessor::getObject, (accessor, currentRow) -> is(Duration.ofDays(currentRow + 1))); } @@ -86,13 +85,17 @@ public void getObjectForNull() throws Exception { vector.setNull(i); } - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcDurationVectorAccessor::getObject, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcDurationVectorAccessor::getObject, (accessor, currentRow) -> equalTo(null)); } @Test public void getString() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcAccessor::getString, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcAccessor::getString, (accessor, currentRow) -> is(Duration.ofDays(currentRow + 1).toString())); } @@ -103,13 +106,15 @@ public void getStringForNull() throws Exception { vector.setNull(i); } - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcAccessor::getString, - (accessor, currentRow) -> equalTo(null)); + accessorIterator.assertAccessorGetter( + vector, ArrowFlightJdbcAccessor::getString, (accessor, currentRow) -> equalTo(null)); } @Test public void testShouldGetObjectClass() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcAccessor::getObjectClass, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcAccessor::getObjectClass, (accessor, currentRow) -> equalTo(Duration.class)); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcIntervalVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcIntervalVectorAccessorTest.java index 322b7d40bd6..48f9539df1d 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcIntervalVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcIntervalVectorAccessorTest.java @@ -27,7 +27,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.function.Supplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; @@ -50,59 +49,68 @@ public class ArrowFlightJdbcIntervalVectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private final Supplier vectorSupplier; private ValueVector vector; private final AccessorTestUtils.AccessorSupplier - accessorSupplier = (vector, getCurrentRow) -> { - ArrowFlightJdbcAccessorFactory.WasNullConsumer noOpWasNullConsumer = (boolean wasNull) -> { - }; - if (vector instanceof IntervalDayVector) { - return new ArrowFlightJdbcIntervalVectorAccessor((IntervalDayVector) vector, - getCurrentRow, noOpWasNullConsumer); - } else if (vector instanceof IntervalYearVector) { - return new ArrowFlightJdbcIntervalVectorAccessor((IntervalYearVector) vector, - getCurrentRow, noOpWasNullConsumer); - } - return null; - }; + accessorSupplier = + (vector, getCurrentRow) -> { + ArrowFlightJdbcAccessorFactory.WasNullConsumer noOpWasNullConsumer = + (boolean wasNull) -> {}; + if (vector instanceof IntervalDayVector) { + return new ArrowFlightJdbcIntervalVectorAccessor( + (IntervalDayVector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof IntervalYearVector) { + return new ArrowFlightJdbcIntervalVectorAccessor( + (IntervalYearVector) vector, getCurrentRow, noOpWasNullConsumer); + } + return null; + }; final AccessorTestUtils.AccessorIterator accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @Parameterized.Parameters(name = "{1}") public static Collection data() { - return Arrays.asList(new Object[][] { - {(Supplier) () -> { - IntervalDayVector vector = - new IntervalDayVector("", rootAllocatorTestRule.getRootAllocator()); - - int valueCount = 10; - vector.setValueCount(valueCount); - for (int i = 0; i < valueCount; i++) { - vector.set(i, i + 1, (i + 1) * 1000); - } - return vector; - }, "IntervalDayVector"}, - {(Supplier) () -> { - IntervalYearVector vector = - new IntervalYearVector("", rootAllocatorTestRule.getRootAllocator()); - - int valueCount = 10; - vector.setValueCount(valueCount); - for (int i = 0; i < valueCount; i++) { - vector.set(i, i + 1); - } - return vector; - }, "IntervalYearVector"}, - }); - } - - public ArrowFlightJdbcIntervalVectorAccessorTest(Supplier vectorSupplier, - String vectorType) { + return Arrays.asList( + new Object[][] { + { + (Supplier) + () -> { + IntervalDayVector vector = + new IntervalDayVector("", rootAllocatorTestRule.getRootAllocator()); + + int valueCount = 10; + vector.setValueCount(valueCount); + for (int i = 0; i < valueCount; i++) { + vector.set(i, i + 1, (i + 1) * 1000); + } + return vector; + }, + "IntervalDayVector" + }, + { + (Supplier) + () -> { + IntervalYearVector vector = + new IntervalYearVector("", rootAllocatorTestRule.getRootAllocator()); + + int valueCount = 10; + vector.setValueCount(valueCount); + for (int i = 0; i < valueCount; i++) { + vector.set(i, i + 1); + } + return vector; + }, + "IntervalYearVector" + }, + }); + } + + public ArrowFlightJdbcIntervalVectorAccessorTest( + Supplier vectorSupplier, String vectorType) { this.vectorSupplier = vectorSupplier; } @@ -118,21 +126,27 @@ public void tearDown() { @Test public void testShouldGetObjectReturnValidObject() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcIntervalVectorAccessor::getObject, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcIntervalVectorAccessor::getObject, (accessor, currentRow) -> is(getExpectedObject(vector, currentRow))); } @Test public void testShouldGetObjectPassingObjectClassAsParameterReturnValidObject() throws Exception { Class objectClass = getExpectedObjectClassForVector(vector); - accessorIterator.assertAccessorGetter(vector, accessor -> accessor.getObject(objectClass), + accessorIterator.assertAccessorGetter( + vector, + accessor -> accessor.getObject(objectClass), (accessor, currentRow) -> is(getExpectedObject(vector, currentRow))); } @Test public void testShouldGetObjectReturnNull() throws Exception { setAllNullOnVector(vector); - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcIntervalVectorAccessor::getObject, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcIntervalVectorAccessor::getObject, (accessor, currentRow) -> equalTo(null)); } @@ -149,7 +163,7 @@ private String getStringOnVector(ValueVector vector, int index) { } @Test - public void testShouldGetIntervalYear( ) { + public void testShouldGetIntervalYear() { Assert.assertEquals("-002-00", formatIntervalYear(Period.parse("P-2Y"))); Assert.assertEquals("-001-01", formatIntervalYear(Period.parse("P-1Y-1M"))); Assert.assertEquals("-001-02", formatIntervalYear(Period.parse("P-1Y-2M"))); @@ -165,22 +179,30 @@ public void testShouldGetIntervalYear( ) { } @Test - public void testShouldGetIntervalDay( ) { + public void testShouldGetIntervalDay() { Assert.assertEquals("-001 00:00:00.000", formatIntervalDay(Duration.parse("PT-24H"))); Assert.assertEquals("+001 00:00:00.000", formatIntervalDay(Duration.parse("PT+24H"))); Assert.assertEquals("-000 01:00:00.000", formatIntervalDay(Duration.parse("PT-1H"))); - // "JDK-8054978: java.time.Duration.parse() fails for negative duration with 0 seconds and nanos" not fixed on JDK8 - //Assert.assertEquals("-000 01:00:00.001", formatIntervalDay(Duration.parse("PT-1H-0M-00.001S"))); - Assert.assertEquals("-000 01:00:00.001", formatIntervalDay(Duration.ofHours(-1).minusMillis(1))); + // "JDK-8054978: java.time.Duration.parse() fails for negative duration with 0 seconds and + // nanos" not fixed on JDK8 + // Assert.assertEquals("-000 01:00:00.001", + // formatIntervalDay(Duration.parse("PT-1H-0M-00.001S"))); + Assert.assertEquals( + "-000 01:00:00.001", formatIntervalDay(Duration.ofHours(-1).minusMillis(1))); Assert.assertEquals("-000 01:01:01.000", formatIntervalDay(Duration.parse("PT-1H-1M-1S"))); Assert.assertEquals("-000 02:02:02.002", formatIntervalDay(Duration.parse("PT-2H-2M-02.002S"))); - Assert.assertEquals("-000 23:59:59.999", formatIntervalDay(Duration.parse("PT-23H-59M-59.999S"))); - // "JDK-8054978: java.time.Duration.parse() fails for negative duration with 0 seconds and nanos" not fixed on JDK8 - //Assert.assertEquals("-000 11:59:00.100", formatIntervalDay(Duration.parse("PT-11H-59M-00.100S"))); - Assert.assertEquals("-000 11:59:00.100", + Assert.assertEquals( + "-000 23:59:59.999", formatIntervalDay(Duration.parse("PT-23H-59M-59.999S"))); + // "JDK-8054978: java.time.Duration.parse() fails for negative duration with 0 seconds and + // nanos" not fixed on JDK8 + // Assert.assertEquals("-000 11:59:00.100", + // formatIntervalDay(Duration.parse("PT-11H-59M-00.100S"))); + Assert.assertEquals( + "-000 11:59:00.100", formatIntervalDay(Duration.ofHours(-11).minusMinutes(59).minusMillis(100))); Assert.assertEquals("-000 05:02:03.000", formatIntervalDay(Duration.parse("PT-5H-2M-3S"))); - Assert.assertEquals("-000 22:22:22.222", formatIntervalDay(Duration.parse("PT-22H-22M-22.222S"))); + Assert.assertEquals( + "-000 22:22:22.222", formatIntervalDay(Duration.parse("PT-22H-22M-22.222S"))); Assert.assertEquals("+000 01:00:00.000", formatIntervalDay(Duration.parse("PT+1H"))); Assert.assertEquals("+000 01:00:00.001", formatIntervalDay(Duration.parse("PT+1H0M00.001S"))); Assert.assertEquals("+000 01:01:01.000", formatIntervalDay(Duration.parse("PT+1H1M1S"))); @@ -193,29 +215,32 @@ public void testShouldGetIntervalDay( ) { @Test public void testIntervalDayWithJodaPeriodObject() { - Assert.assertEquals("+1567 00:00:00.000", - formatIntervalDay(Duration.ofDays(1567))); - Assert.assertEquals("-1567 00:00:00.000", - formatIntervalDay(Duration.ofDays(-1567))); + Assert.assertEquals("+1567 00:00:00.000", formatIntervalDay(Duration.ofDays(1567))); + Assert.assertEquals("-1567 00:00:00.000", formatIntervalDay(Duration.ofDays(-1567))); } @Test public void testShouldGetStringReturnCorrectString() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcIntervalVectorAccessor::getString, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcIntervalVectorAccessor::getString, (accessor, currentRow) -> is(getStringOnVector(vector, currentRow))); } @Test public void testShouldGetStringReturnNull() throws Exception { setAllNullOnVector(vector); - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcIntervalVectorAccessor::getString, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcIntervalVectorAccessor::getString, (accessor, currentRow) -> equalTo(null)); } @Test public void testShouldGetObjectClassReturnCorrectClass() throws Exception { Class expectedObjectClass = getExpectedObjectClassForVector(vector); - accessorIterator.assertAccessorGetter(vector, + accessorIterator.assertAccessorGetter( + vector, ArrowFlightJdbcIntervalVectorAccessor::getObjectClass, (accessor, currentRow) -> equalTo(expectedObjectClass)); } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessorTest.java index e2c17b2f085..0461ea79c0a 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessorTest.java @@ -32,7 +32,6 @@ import java.util.TimeZone; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; - import org.apache.arrow.driver.jdbc.accessor.impl.text.ArrowFlightJdbcVarCharVectorAccessor; import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; @@ -64,8 +63,7 @@ public class ArrowFlightJdbcTimeStampVectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private final String timeZone; private TimeStampVector vector; @@ -73,81 +71,114 @@ public class ArrowFlightJdbcTimeStampVectorAccessorTest { private final AccessorTestUtils.AccessorSupplier accessorSupplier = - (vector, getCurrentRow) -> new ArrowFlightJdbcTimeStampVectorAccessor( - (TimeStampVector) vector, getCurrentRow, (boolean wasNull) -> { - }); + (vector, getCurrentRow) -> + new ArrowFlightJdbcTimeStampVectorAccessor( + (TimeStampVector) vector, getCurrentRow, (boolean wasNull) -> {}); private final AccessorTestUtils.AccessorIterator - accessorIterator = - new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); + accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @Parameterized.Parameters(name = "{1} - TimeZone: {2}") public static Collection data() { - return Arrays.asList(new Object[][] { - {(Supplier) () -> rootAllocatorTestRule.createTimeStampNanoVector(), + return Arrays.asList( + new Object[][] { + { + (Supplier) () -> rootAllocatorTestRule.createTimeStampNanoVector(), "TimeStampNanoVector", - null}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampNanoTZVector("UTC"), + null + }, + { + (Supplier) + () -> rootAllocatorTestRule.createTimeStampNanoTZVector("UTC"), "TimeStampNanoTZVector", - "UTC"}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampNanoTZVector( - AMERICA_VANCOUVER), + "UTC" + }, + { + (Supplier) + () -> rootAllocatorTestRule.createTimeStampNanoTZVector(AMERICA_VANCOUVER), "TimeStampNanoTZVector", - AMERICA_VANCOUVER}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampNanoTZVector( - ASIA_BANGKOK), + AMERICA_VANCOUVER + }, + { + (Supplier) + () -> rootAllocatorTestRule.createTimeStampNanoTZVector(ASIA_BANGKOK), "TimeStampNanoTZVector", - ASIA_BANGKOK}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampMicroVector(), + ASIA_BANGKOK + }, + { + (Supplier) () -> rootAllocatorTestRule.createTimeStampMicroVector(), "TimeStampMicroVector", - null}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampMicroTZVector( - "UTC"), + null + }, + { + (Supplier) + () -> rootAllocatorTestRule.createTimeStampMicroTZVector("UTC"), "TimeStampMicroTZVector", - "UTC"}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampMicroTZVector( - AMERICA_VANCOUVER), + "UTC" + }, + { + (Supplier) + () -> rootAllocatorTestRule.createTimeStampMicroTZVector(AMERICA_VANCOUVER), "TimeStampMicroTZVector", - AMERICA_VANCOUVER}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampMicroTZVector( - ASIA_BANGKOK), + AMERICA_VANCOUVER + }, + { + (Supplier) + () -> rootAllocatorTestRule.createTimeStampMicroTZVector(ASIA_BANGKOK), "TimeStampMicroTZVector", - ASIA_BANGKOK}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampMilliVector(), + ASIA_BANGKOK + }, + { + (Supplier) () -> rootAllocatorTestRule.createTimeStampMilliVector(), "TimeStampMilliVector", - null}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampMilliTZVector( - "UTC"), + null + }, + { + (Supplier) + () -> rootAllocatorTestRule.createTimeStampMilliTZVector("UTC"), "TimeStampMilliTZVector", - "UTC"}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampMilliTZVector( - AMERICA_VANCOUVER), + "UTC" + }, + { + (Supplier) + () -> rootAllocatorTestRule.createTimeStampMilliTZVector(AMERICA_VANCOUVER), "TimeStampMilliTZVector", - AMERICA_VANCOUVER}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampMilliTZVector( - ASIA_BANGKOK), + AMERICA_VANCOUVER + }, + { + (Supplier) + () -> rootAllocatorTestRule.createTimeStampMilliTZVector(ASIA_BANGKOK), "TimeStampMilliTZVector", - ASIA_BANGKOK}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampSecVector(), + ASIA_BANGKOK + }, + { + (Supplier) () -> rootAllocatorTestRule.createTimeStampSecVector(), "TimeStampSecVector", - null}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampSecTZVector("UTC"), + null + }, + { + (Supplier) + () -> rootAllocatorTestRule.createTimeStampSecTZVector("UTC"), "TimeStampSecTZVector", - "UTC"}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampSecTZVector( - AMERICA_VANCOUVER), + "UTC" + }, + { + (Supplier) + () -> rootAllocatorTestRule.createTimeStampSecTZVector(AMERICA_VANCOUVER), "TimeStampSecTZVector", - AMERICA_VANCOUVER}, - {(Supplier) () -> rootAllocatorTestRule.createTimeStampSecTZVector( - ASIA_BANGKOK), + AMERICA_VANCOUVER + }, + { + (Supplier) + () -> rootAllocatorTestRule.createTimeStampSecTZVector(ASIA_BANGKOK), "TimeStampSecTZVector", - ASIA_BANGKOK} - }); + ASIA_BANGKOK + } + }); } - public ArrowFlightJdbcTimeStampVectorAccessorTest(Supplier vectorSupplier, - String vectorType, - String timeZone) { + public ArrowFlightJdbcTimeStampVectorAccessorTest( + Supplier vectorSupplier, String vectorType, String timeZone) { this.vectorSupplier = vectorSupplier; this.timeZone = timeZone; } @@ -164,7 +195,9 @@ public void tearDown() { @Test public void testShouldGetTimestampReturnValidTimestampWithoutCalendar() throws Exception { - accessorIterator.assertAccessorGetter(vector, accessor -> accessor.getTimestamp(null), + accessorIterator.assertAccessorGetter( + vector, + accessor -> accessor.getTimestamp(null), (accessor, currentRow) -> is(getTimestampForVector(currentRow))); } @@ -175,16 +208,19 @@ public void testShouldGetTimestampReturnValidTimestampWithCalendar() throws Exce TimeZone timeZoneForVector = getTimeZoneForVector(vector); - accessorIterator.iterate(vector, (accessor, currentRow) -> { - final Timestamp resultWithoutCalendar = accessor.getTimestamp(null); - final Timestamp result = accessor.getTimestamp(calendar); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + final Timestamp resultWithoutCalendar = accessor.getTimestamp(null); + final Timestamp result = accessor.getTimestamp(calendar); - long offset = (long) timeZone.getOffset(resultWithoutCalendar.getTime()) - - timeZoneForVector.getOffset(resultWithoutCalendar.getTime()); + long offset = + (long) timeZone.getOffset(resultWithoutCalendar.getTime()) + - timeZoneForVector.getOffset(resultWithoutCalendar.getTime()); - collector.checkThat(resultWithoutCalendar.getTime() - result.getTime(), is(offset)); - collector.checkThat(accessor.wasNull(), is(false)); - }); + collector.checkThat(resultWithoutCalendar.getTime() - result.getTime(), is(offset)); + collector.checkThat(accessor.wasNull(), is(false)); + }); } @Test @@ -197,7 +233,9 @@ public void testShouldGetTimestampReturnNull() { @Test public void testShouldGetDateReturnValidDateWithoutCalendar() throws Exception { - accessorIterator.assertAccessorGetter(vector, accessor -> accessor.getDate(null), + accessorIterator.assertAccessorGetter( + vector, + accessor -> accessor.getDate(null), (accessor, currentRow) -> is(new Date(getTimestampForVector(currentRow).getTime()))); } @@ -208,16 +246,19 @@ public void testShouldGetDateReturnValidDateWithCalendar() throws Exception { TimeZone timeZoneForVector = getTimeZoneForVector(vector); - accessorIterator.iterate(vector, (accessor, currentRow) -> { - final Date resultWithoutCalendar = accessor.getDate(null); - final Date result = accessor.getDate(calendar); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + final Date resultWithoutCalendar = accessor.getDate(null); + final Date result = accessor.getDate(calendar); - long offset = (long) timeZone.getOffset(resultWithoutCalendar.getTime()) - - timeZoneForVector.getOffset(resultWithoutCalendar.getTime()); + long offset = + (long) timeZone.getOffset(resultWithoutCalendar.getTime()) + - timeZoneForVector.getOffset(resultWithoutCalendar.getTime()); - collector.checkThat(resultWithoutCalendar.getTime() - result.getTime(), is(offset)); - collector.checkThat(accessor.wasNull(), is(false)); - }); + collector.checkThat(resultWithoutCalendar.getTime() - result.getTime(), is(offset)); + collector.checkThat(accessor.wasNull(), is(false)); + }); } @Test @@ -230,7 +271,9 @@ public void testShouldGetDateReturnNull() { @Test public void testShouldGetTimeReturnValidTimeWithoutCalendar() throws Exception { - accessorIterator.assertAccessorGetter(vector, accessor -> accessor.getTime(null), + accessorIterator.assertAccessorGetter( + vector, + accessor -> accessor.getTime(null), (accessor, currentRow) -> is(new Time(getTimestampForVector(currentRow).getTime()))); } @@ -241,16 +284,19 @@ public void testShouldGetTimeReturnValidTimeWithCalendar() throws Exception { TimeZone timeZoneForVector = getTimeZoneForVector(vector); - accessorIterator.iterate(vector, (accessor, currentRow) -> { - final Time resultWithoutCalendar = accessor.getTime(null); - final Time result = accessor.getTime(calendar); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + final Time resultWithoutCalendar = accessor.getTime(null); + final Time result = accessor.getTime(calendar); - long offset = (long) timeZone.getOffset(resultWithoutCalendar.getTime()) - - timeZoneForVector.getOffset(resultWithoutCalendar.getTime()); + long offset = + (long) timeZone.getOffset(resultWithoutCalendar.getTime()) + - timeZoneForVector.getOffset(resultWithoutCalendar.getTime()); - collector.checkThat(resultWithoutCalendar.getTime() - result.getTime(), is(offset)); - collector.checkThat(accessor.wasNull(), is(false)); - }); + collector.checkThat(resultWithoutCalendar.getTime() - result.getTime(), is(offset)); + collector.checkThat(accessor.wasNull(), is(false)); + }); } @Test @@ -278,9 +324,8 @@ private Timestamp getTimestampForVector(int currentRow) { @Test public void testShouldGetObjectClass() throws Exception { - accessorIterator.assertAccessorGetter(vector, - ArrowFlightJdbcTimeStampVectorAccessor::getObjectClass, - equalTo(Timestamp.class)); + accessorIterator.assertAccessorGetter( + vector, ArrowFlightJdbcTimeStampVectorAccessor::getObjectClass, equalTo(Timestamp.class)); } @Test @@ -292,31 +337,34 @@ public void testShouldGetStringBeConsistentWithVarCharAccessorWithoutCalendar() public void testShouldGetStringBeConsistentWithVarCharAccessorWithCalendar() throws Exception { // Ignore for TimeStamp vectors with TZ, as VarChar accessor won't consider their TZ Assume.assumeTrue( - vector instanceof TimeStampNanoVector || vector instanceof TimeStampMicroVector || - vector instanceof TimeStampMilliVector || vector instanceof TimeStampSecVector); + vector instanceof TimeStampNanoVector + || vector instanceof TimeStampMicroVector + || vector instanceof TimeStampMilliVector + || vector instanceof TimeStampSecVector); Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(AMERICA_VANCOUVER)); assertGetStringIsConsistentWithVarCharAccessor(calendar); } private void assertGetStringIsConsistentWithVarCharAccessor(Calendar calendar) throws Exception { - try (VarCharVector varCharVector = new VarCharVector("", - rootAllocatorTestRule.getRootAllocator())) { + try (VarCharVector varCharVector = + new VarCharVector("", rootAllocatorTestRule.getRootAllocator())) { varCharVector.allocateNew(1); ArrowFlightJdbcVarCharVectorAccessor varCharVectorAccessor = - new ArrowFlightJdbcVarCharVectorAccessor(varCharVector, () -> 0, (boolean wasNull) -> { - }); + new ArrowFlightJdbcVarCharVectorAccessor(varCharVector, () -> 0, (boolean wasNull) -> {}); - accessorIterator.iterate(vector, (accessor, currentRow) -> { - final String string = accessor.getString(); - varCharVector.set(0, new Text(string)); - varCharVector.setValueCount(1); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + final String string = accessor.getString(); + varCharVector.set(0, new Text(string)); + varCharVector.setValueCount(1); - Timestamp timestampFromVarChar = varCharVectorAccessor.getTimestamp(calendar); - Timestamp timestamp = accessor.getTimestamp(calendar); + Timestamp timestampFromVarChar = varCharVectorAccessor.getTimestamp(calendar); + Timestamp timestamp = accessor.getTimestamp(calendar); - collector.checkThat(timestamp, is(timestampFromVarChar)); - collector.checkThat(accessor.wasNull(), is(false)); - }); + collector.checkThat(timestamp, is(timestampFromVarChar)); + collector.checkThat(accessor.wasNull(), is(false)); + }); } } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeVectorAccessorTest.java index d2f7eb336af..d08a3a6bf6a 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeVectorAccessorTest.java @@ -31,7 +31,6 @@ import java.util.TimeZone; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.driver.jdbc.accessor.impl.text.ArrowFlightJdbcVarCharVectorAccessor; import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; @@ -61,52 +60,60 @@ public class ArrowFlightJdbcTimeVectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private BaseFixedWidthVector vector; private final Supplier vectorSupplier; private final AccessorTestUtils.AccessorSupplier - accessorSupplier = (vector, getCurrentRow) -> { - ArrowFlightJdbcAccessorFactory.WasNullConsumer noOpWasNullConsumer = (boolean wasNull) -> { - }; - if (vector instanceof TimeNanoVector) { - return new ArrowFlightJdbcTimeVectorAccessor((TimeNanoVector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof TimeMicroVector) { - return new ArrowFlightJdbcTimeVectorAccessor((TimeMicroVector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof TimeMilliVector) { - return new ArrowFlightJdbcTimeVectorAccessor((TimeMilliVector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof TimeSecVector) { - return new ArrowFlightJdbcTimeVectorAccessor((TimeSecVector) vector, getCurrentRow, - noOpWasNullConsumer); - } - return null; - }; + accessorSupplier = + (vector, getCurrentRow) -> { + ArrowFlightJdbcAccessorFactory.WasNullConsumer noOpWasNullConsumer = + (boolean wasNull) -> {}; + if (vector instanceof TimeNanoVector) { + return new ArrowFlightJdbcTimeVectorAccessor( + (TimeNanoVector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof TimeMicroVector) { + return new ArrowFlightJdbcTimeVectorAccessor( + (TimeMicroVector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof TimeMilliVector) { + return new ArrowFlightJdbcTimeVectorAccessor( + (TimeMilliVector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof TimeSecVector) { + return new ArrowFlightJdbcTimeVectorAccessor( + (TimeSecVector) vector, getCurrentRow, noOpWasNullConsumer); + } + return null; + }; private final AccessorTestUtils.AccessorIterator - accessorIterator = - new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); + accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @Parameterized.Parameters(name = "{1}") public static Collection data() { - return Arrays.asList(new Object[][] { - {(Supplier) () -> rootAllocatorTestRule.createTimeNanoVector(), - "TimeNanoVector"}, - {(Supplier) () -> rootAllocatorTestRule.createTimeMicroVector(), - "TimeMicroVector"}, - {(Supplier) () -> rootAllocatorTestRule.createTimeMilliVector(), - "TimeMilliVector"}, - {(Supplier) () -> rootAllocatorTestRule.createTimeSecVector(), - "TimeSecVector"} - }); + return Arrays.asList( + new Object[][] { + { + (Supplier) () -> rootAllocatorTestRule.createTimeNanoVector(), + "TimeNanoVector" + }, + { + (Supplier) () -> rootAllocatorTestRule.createTimeMicroVector(), + "TimeMicroVector" + }, + { + (Supplier) () -> rootAllocatorTestRule.createTimeMilliVector(), + "TimeMilliVector" + }, + { + (Supplier) () -> rootAllocatorTestRule.createTimeSecVector(), + "TimeSecVector" + } + }); } - public ArrowFlightJdbcTimeVectorAccessorTest(Supplier vectorSupplier, - String vectorType) { + public ArrowFlightJdbcTimeVectorAccessorTest( + Supplier vectorSupplier, String vectorType) { this.vectorSupplier = vectorSupplier; } @@ -122,7 +129,9 @@ public void tearDown() { @Test public void testShouldGetTimestampReturnValidTimestampWithoutCalendar() throws Exception { - accessorIterator.assertAccessorGetter(vector, accessor -> accessor.getTimestamp(null), + accessorIterator.assertAccessorGetter( + vector, + accessor -> accessor.getTimestamp(null), (accessor, currentRow) -> is(getTimestampForVector(currentRow))); } @@ -131,15 +140,17 @@ public void testShouldGetTimestampReturnValidTimestampWithCalendar() throws Exce TimeZone timeZone = TimeZone.getTimeZone(AMERICA_VANCOUVER); Calendar calendar = Calendar.getInstance(timeZone); - accessorIterator.iterate(vector, (accessor, currentRow) -> { - final Timestamp resultWithoutCalendar = accessor.getTimestamp(null); - final Timestamp result = accessor.getTimestamp(calendar); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + final Timestamp resultWithoutCalendar = accessor.getTimestamp(null); + final Timestamp result = accessor.getTimestamp(calendar); - long offset = timeZone.getOffset(resultWithoutCalendar.getTime()); + long offset = timeZone.getOffset(resultWithoutCalendar.getTime()); - collector.checkThat(resultWithoutCalendar.getTime() - result.getTime(), is(offset)); - collector.checkThat(accessor.wasNull(), is(false)); - }); + collector.checkThat(resultWithoutCalendar.getTime() - result.getTime(), is(offset)); + collector.checkThat(accessor.wasNull(), is(false)); + }); } @Test @@ -152,7 +163,9 @@ public void testShouldGetTimestampReturnNull() { @Test public void testShouldGetTimeReturnValidTimeWithoutCalendar() throws Exception { - accessorIterator.assertAccessorGetter(vector, accessor -> accessor.getTime(null), + accessorIterator.assertAccessorGetter( + vector, + accessor -> accessor.getTime(null), (accessor, currentRow) -> { Timestamp expectedTimestamp = getTimestampForVector(currentRow); return is(new Time(expectedTimestamp.getTime())); @@ -164,15 +177,17 @@ public void testShouldGetTimeReturnValidTimeWithCalendar() throws Exception { TimeZone timeZone = TimeZone.getTimeZone(AMERICA_VANCOUVER); Calendar calendar = Calendar.getInstance(timeZone); - accessorIterator.iterate(vector, (accessor, currentRow) -> { - final Time resultWithoutCalendar = accessor.getTime(null); - final Time result = accessor.getTime(calendar); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + final Time resultWithoutCalendar = accessor.getTime(null); + final Time result = accessor.getTime(calendar); - long offset = timeZone.getOffset(resultWithoutCalendar.getTime()); + long offset = timeZone.getOffset(resultWithoutCalendar.getTime()); - collector.checkThat(resultWithoutCalendar.getTime() - result.getTime(), is(offset)); - collector.checkThat(accessor.wasNull(), is(false)); - }); + collector.checkThat(resultWithoutCalendar.getTime() - result.getTime(), is(offset)); + collector.checkThat(accessor.wasNull(), is(false)); + }); } @Test @@ -200,8 +215,8 @@ private Timestamp getTimestampForVector(int currentRow) { @Test public void testShouldGetObjectClass() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcTimeVectorAccessor::getObjectClass, - equalTo(Time.class)); + accessorIterator.assertAccessorGetter( + vector, ArrowFlightJdbcTimeVectorAccessor::getObjectClass, equalTo(Time.class)); } @Test @@ -217,47 +232,51 @@ public void testShouldGetStringBeConsistentWithVarCharAccessorWithCalendar() thr @Test public void testValidateGetStringTimeZoneConsistency() throws Exception { - accessorIterator.iterate(vector, (accessor, currentRow) -> { - final TimeZone defaultTz = TimeZone.getDefault(); - try { - final String string = accessor.getString(); // Should always be UTC as no calendar is provided - - // Validate with UTC - Time time = accessor.getTime(null); - TimeZone.setDefault(TimeZone.getTimeZone("UTC")); - collector.checkThat(time.toString(), is(string)); - - // Validate with different TZ - TimeZone.setDefault(TimeZone.getTimeZone(AMERICA_VANCOUVER)); - collector.checkThat(time.toString(), not(string)); - - collector.checkThat(accessor.wasNull(), is(false)); - } finally { - // Set default Tz back - TimeZone.setDefault(defaultTz); - } - }); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + final TimeZone defaultTz = TimeZone.getDefault(); + try { + final String string = + accessor.getString(); // Should always be UTC as no calendar is provided + + // Validate with UTC + Time time = accessor.getTime(null); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + collector.checkThat(time.toString(), is(string)); + + // Validate with different TZ + TimeZone.setDefault(TimeZone.getTimeZone(AMERICA_VANCOUVER)); + collector.checkThat(time.toString(), not(string)); + + collector.checkThat(accessor.wasNull(), is(false)); + } finally { + // Set default Tz back + TimeZone.setDefault(defaultTz); + } + }); } private void assertGetStringIsConsistentWithVarCharAccessor(Calendar calendar) throws Exception { - try (VarCharVector varCharVector = new VarCharVector("", - rootAllocatorTestRule.getRootAllocator())) { + try (VarCharVector varCharVector = + new VarCharVector("", rootAllocatorTestRule.getRootAllocator())) { varCharVector.allocateNew(1); ArrowFlightJdbcVarCharVectorAccessor varCharVectorAccessor = - new ArrowFlightJdbcVarCharVectorAccessor(varCharVector, () -> 0, (boolean wasNull) -> { - }); + new ArrowFlightJdbcVarCharVectorAccessor(varCharVector, () -> 0, (boolean wasNull) -> {}); - accessorIterator.iterate(vector, (accessor, currentRow) -> { - final String string = accessor.getString(); - varCharVector.set(0, new Text(string)); - varCharVector.setValueCount(1); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + final String string = accessor.getString(); + varCharVector.set(0, new Text(string)); + varCharVector.setValueCount(1); - Time timeFromVarChar = varCharVectorAccessor.getTime(calendar); - Time time = accessor.getTime(calendar); + Time timeFromVarChar = varCharVectorAccessor.getTime(calendar); + Time time = accessor.getTime(calendar); - collector.checkThat(time, is(timeFromVarChar)); - collector.checkThat(accessor.wasNull(), is(false)); - }); + collector.checkThat(time, is(timeFromVarChar)); + collector.checkThat(accessor.wasNull(), is(false)); + }); } } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcListAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcListAccessorTest.java index e958fb60ba4..6c5c0720e50 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcListAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcListAccessorTest.java @@ -25,7 +25,6 @@ import java.util.Collection; import java.util.List; import java.util.function.Supplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; @@ -49,46 +48,50 @@ public class AbstractArrowFlightJdbcListAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private final Supplier vectorSupplier; private ValueVector vector; private final AccessorTestUtils.AccessorSupplier - accessorSupplier = (vector, getCurrentRow) -> { - ArrowFlightJdbcAccessorFactory.WasNullConsumer noOpWasNullConsumer = (boolean wasNull) -> { - }; - if (vector instanceof ListVector) { - return new ArrowFlightJdbcListVectorAccessor((ListVector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof LargeListVector) { - return new ArrowFlightJdbcLargeListVectorAccessor((LargeListVector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof FixedSizeListVector) { - return new ArrowFlightJdbcFixedSizeListVectorAccessor((FixedSizeListVector) vector, - getCurrentRow, noOpWasNullConsumer); - } - return null; - }; + accessorSupplier = + (vector, getCurrentRow) -> { + ArrowFlightJdbcAccessorFactory.WasNullConsumer noOpWasNullConsumer = + (boolean wasNull) -> {}; + if (vector instanceof ListVector) { + return new ArrowFlightJdbcListVectorAccessor( + (ListVector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof LargeListVector) { + return new ArrowFlightJdbcLargeListVectorAccessor( + (LargeListVector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof FixedSizeListVector) { + return new ArrowFlightJdbcFixedSizeListVectorAccessor( + (FixedSizeListVector) vector, getCurrentRow, noOpWasNullConsumer); + } + return null; + }; final AccessorTestUtils.AccessorIterator - accessorIterator = - new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); + accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @Parameterized.Parameters(name = "{1}") public static Collection data() { - return Arrays.asList(new Object[][] { - {(Supplier) () -> rootAllocatorTestRule.createListVector(), "ListVector"}, - {(Supplier) () -> rootAllocatorTestRule.createLargeListVector(), - "LargeListVector"}, - {(Supplier) () -> rootAllocatorTestRule.createFixedSizeListVector(), - "FixedSizeListVector"}, - }); + return Arrays.asList( + new Object[][] { + {(Supplier) () -> rootAllocatorTestRule.createListVector(), "ListVector"}, + { + (Supplier) () -> rootAllocatorTestRule.createLargeListVector(), + "LargeListVector" + }, + { + (Supplier) () -> rootAllocatorTestRule.createFixedSizeListVector(), + "FixedSizeListVector" + }, + }); } - public AbstractArrowFlightJdbcListAccessorTest(Supplier vectorSupplier, - String vectorType) { + public AbstractArrowFlightJdbcListAccessorTest( + Supplier vectorSupplier, String vectorType) { this.vectorSupplier = vectorSupplier; } @@ -104,17 +107,19 @@ public void tearDown() { @Test public void testShouldGetObjectClassReturnCorrectClass() throws Exception { - accessorIterator.assertAccessorGetter(vector, + accessorIterator.assertAccessorGetter( + vector, AbstractArrowFlightJdbcListVectorAccessor::getObjectClass, (accessor, currentRow) -> equalTo(List.class)); } @Test public void testShouldGetObjectReturnValidList() throws Exception { - accessorIterator.assertAccessorGetter(vector, + accessorIterator.assertAccessorGetter( + vector, AbstractArrowFlightJdbcListVectorAccessor::getObject, - (accessor, currentRow) -> equalTo( - Arrays.asList(0, currentRow, currentRow * 2, currentRow * 3, currentRow * 4))); + (accessor, currentRow) -> + equalTo(Arrays.asList(0, currentRow, currentRow * 2, currentRow * 3, currentRow * 4))); } @Test @@ -123,22 +128,27 @@ public void testShouldGetObjectReturnNull() throws Exception { vector.allocateNewSafe(); vector.setValueCount(5); - accessorIterator.assertAccessorGetter(vector, + accessorIterator.assertAccessorGetter( + vector, AbstractArrowFlightJdbcListVectorAccessor::getObject, (accessor, currentRow) -> CoreMatchers.nullValue()); } @Test public void testShouldGetArrayReturnValidArray() throws Exception { - accessorIterator.iterate(vector, (accessor, currentRow) -> { - Array array = accessor.getArray(); - assert array != null; - - Object[] arrayObject = (Object[]) array.getArray(); - - collector.checkThat(arrayObject, equalTo( - new Object[] {0, currentRow, currentRow * 2, currentRow * 3, currentRow * 4})); - }); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + Array array = accessor.getArray(); + assert array != null; + + Object[] arrayObject = (Object[]) array.getArray(); + + collector.checkThat( + arrayObject, + equalTo( + new Object[] {0, currentRow, currentRow * 2, currentRow * 3, currentRow * 4})); + }); } @Test @@ -147,39 +157,42 @@ public void testShouldGetArrayReturnNull() throws Exception { vector.allocateNewSafe(); vector.setValueCount(5); - accessorIterator.assertAccessorGetter(vector, - AbstractArrowFlightJdbcListVectorAccessor::getArray, - CoreMatchers.nullValue()); + accessorIterator.assertAccessorGetter( + vector, AbstractArrowFlightJdbcListVectorAccessor::getArray, CoreMatchers.nullValue()); } @Test public void testShouldGetArrayReturnValidArrayPassingOffsets() throws Exception { - accessorIterator.iterate(vector, (accessor, currentRow) -> { - Array array = accessor.getArray(); - assert array != null; + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + Array array = accessor.getArray(); + assert array != null; - Object[] arrayObject = (Object[]) array.getArray(1, 3); + Object[] arrayObject = (Object[]) array.getArray(1, 3); - collector.checkThat(arrayObject, equalTo( - new Object[] {currentRow, currentRow * 2, currentRow * 3})); - }); + collector.checkThat( + arrayObject, equalTo(new Object[] {currentRow, currentRow * 2, currentRow * 3})); + }); } @Test public void testShouldGetArrayGetResultSetReturnValidResultSet() throws Exception { - accessorIterator.iterate(vector, (accessor, currentRow) -> { - Array array = accessor.getArray(); - assert array != null; - - try (ResultSet rs = array.getResultSet()) { - int count = 0; - while (rs.next()) { - final int value = rs.getInt(1); - collector.checkThat(value, equalTo(currentRow * count)); - count++; - } - collector.checkThat(count, equalTo(5)); - } - }); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + Array array = accessor.getArray(); + assert array != null; + + try (ResultSet rs = array.getResultSet()) { + int count = 0; + while (rs.next()) { + final int value = rs.getInt(1); + collector.checkThat(value, equalTo(currentRow * count)); + count++; + } + collector.checkThat(count, equalTo(5)); + } + }); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessorTest.java index 2b53b27dc9e..486af288321 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessorTest.java @@ -24,7 +24,6 @@ import java.sql.SQLException; import java.util.Calendar; import java.util.Map; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.driver.jdbc.accessor.impl.ArrowFlightJdbcNullVectorAccessor; import org.apache.arrow.vector.NullVector; @@ -39,10 +38,8 @@ @RunWith(MockitoJUnitRunner.class) public class AbstractArrowFlightJdbcUnionVectorAccessorTest { - @Mock - ArrowFlightJdbcAccessor innerAccessor; - @Spy - AbstractArrowFlightJdbcUnionVectorAccessorMock accessor; + @Mock ArrowFlightJdbcAccessor innerAccessor; + @Spy AbstractArrowFlightJdbcUnionVectorAccessorMock accessor; @Before public void setup() { @@ -242,14 +239,12 @@ public void testGetBigDecimalWithScaleUsesSpecificAccessor() throws SQLException private static class AbstractArrowFlightJdbcUnionVectorAccessorMock extends AbstractArrowFlightJdbcUnionVectorAccessor { protected AbstractArrowFlightJdbcUnionVectorAccessorMock() { - super(() -> 0, (boolean wasNull) -> { - }); + super(() -> 0, (boolean wasNull) -> {}); } @Override protected ArrowFlightJdbcAccessor createAccessorForVector(ValueVector vector) { - return new ArrowFlightJdbcNullVectorAccessor((boolean wasNull) -> { - }); + return new ArrowFlightJdbcNullVectorAccessor((boolean wasNull) -> {}); } @Override diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcDenseUnionVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcDenseUnionVectorAccessorTest.java index 41d5eb97e85..0bb36253a92 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcDenseUnionVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcDenseUnionVectorAccessorTest.java @@ -23,7 +23,6 @@ import java.sql.Timestamp; import java.util.Arrays; import java.util.List; - import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; import org.apache.arrow.vector.complex.DenseUnionVector; @@ -44,21 +43,22 @@ public class ArrowFlightJdbcDenseUnionVectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private DenseUnionVector vector; private final AccessorTestUtils.AccessorSupplier accessorSupplier = - (vector, getCurrentRow) -> new ArrowFlightJdbcDenseUnionVectorAccessor( - (DenseUnionVector) vector, getCurrentRow, (boolean wasNull) -> { - //No Operation - }); + (vector, getCurrentRow) -> + new ArrowFlightJdbcDenseUnionVectorAccessor( + (DenseUnionVector) vector, + getCurrentRow, + (boolean wasNull) -> { + // No Operation + }); private final AccessorTestUtils.AccessorIterator - accessorIterator = - new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); + accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @Before public void setup() throws Exception { @@ -106,12 +106,8 @@ public void tearDown() { @Test public void getObject() throws Exception { List result = accessorIterator.toList(vector); - List expected = Arrays.asList( - Long.MAX_VALUE, - Math.PI, - new Timestamp(1625702400000L), - null, - null); + List expected = + Arrays.asList(Long.MAX_VALUE, Math.PI, new Timestamp(1625702400000L), null, null); collector.checkThat(result, is(expected)); } @@ -120,7 +116,7 @@ public void getObject() throws Exception { public void getObjectForNull() throws Exception { vector.reset(); vector.setValueCount(5); - accessorIterator.assertAccessorGetter(vector, - AbstractArrowFlightJdbcUnionVectorAccessor::getObject, equalTo(null)); + accessorIterator.assertAccessorGetter( + vector, AbstractArrowFlightJdbcUnionVectorAccessor::getObject, equalTo(null)); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcMapVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcMapVectorAccessorTest.java index 7a81da4240b..eab19f083f8 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcMapVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcMapVectorAccessorTest.java @@ -21,7 +21,6 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.Map; - import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; import org.apache.arrow.vector.complex.MapVector; @@ -40,8 +39,7 @@ public class ArrowFlightJdbcMapVectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private MapVector vector; @@ -105,8 +103,8 @@ public void tearDown() { public void testShouldGetObjectReturnValidMap() { AccessorTestUtils.Cursor cursor = new AccessorTestUtils.Cursor(vector.getValueCount()); ArrowFlightJdbcMapVectorAccessor accessor = - new ArrowFlightJdbcMapVectorAccessor(vector, cursor::getCurrentRow, (boolean wasNull) -> { - }); + new ArrowFlightJdbcMapVectorAccessor( + vector, cursor::getCurrentRow, (boolean wasNull) -> {}); Map expected = new JsonStringHashMap<>(); expected.put(1, 11); @@ -135,8 +133,7 @@ public void testShouldGetObjectReturnValidMap() { public void testShouldGetObjectReturnNull() { vector.setNull(0); ArrowFlightJdbcMapVectorAccessor accessor = - new ArrowFlightJdbcMapVectorAccessor(vector, () -> 0, (boolean wasNull) -> { - }); + new ArrowFlightJdbcMapVectorAccessor(vector, () -> 0, (boolean wasNull) -> {}); Assert.assertNull(accessor.getObject()); Assert.assertTrue(accessor.wasNull()); @@ -146,8 +143,8 @@ public void testShouldGetObjectReturnNull() { public void testShouldGetArrayReturnValidArray() throws SQLException { AccessorTestUtils.Cursor cursor = new AccessorTestUtils.Cursor(vector.getValueCount()); ArrowFlightJdbcMapVectorAccessor accessor = - new ArrowFlightJdbcMapVectorAccessor(vector, cursor::getCurrentRow, (boolean wasNull) -> { - }); + new ArrowFlightJdbcMapVectorAccessor( + vector, cursor::getCurrentRow, (boolean wasNull) -> {}); Array array = accessor.getArray(); Assert.assertNotNull(array); @@ -212,8 +209,7 @@ public void testShouldGetArrayReturnNull() { ((StructVector) vector.getDataVector()).setNull(0); ArrowFlightJdbcMapVectorAccessor accessor = - new ArrowFlightJdbcMapVectorAccessor(vector, () -> 0, (boolean wasNull) -> { - }); + new ArrowFlightJdbcMapVectorAccessor(vector, () -> 0, (boolean wasNull) -> {}); Assert.assertNull(accessor.getArray()); Assert.assertTrue(accessor.wasNull()); diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcStructVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcStructVectorAccessorTest.java index 735fe9f40ba..6b87a4d9885 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcStructVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcStructVectorAccessorTest.java @@ -24,7 +24,6 @@ import java.sql.Struct; import java.util.HashMap; import java.util.Map; - import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; import org.apache.arrow.vector.Float8Vector; @@ -52,20 +51,18 @@ public class ArrowFlightJdbcStructVectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private StructVector vector; private final AccessorTestUtils.AccessorSupplier accessorSupplier = - (vector, getCurrentRow) -> new ArrowFlightJdbcStructVectorAccessor((StructVector) vector, - getCurrentRow, (boolean wasNull) -> { - }); + (vector, getCurrentRow) -> + new ArrowFlightJdbcStructVectorAccessor( + (StructVector) vector, getCurrentRow, (boolean wasNull) -> {}); private final AccessorTestUtils.AccessorIterator - accessorIterator = - new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); + accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @Before public void setUp() throws Exception { @@ -78,8 +75,8 @@ public void setUp() throws Exception { IntVector intVector = vector.addOrGet("int", FieldType.nullable(Types.MinorType.INT.getType()), IntVector.class); Float8Vector float8Vector = - vector.addOrGet("float8", FieldType.nullable(Types.MinorType.FLOAT8.getType()), - Float8Vector.class); + vector.addOrGet( + "float8", FieldType.nullable(Types.MinorType.FLOAT8.getType()), Float8Vector.class); intVector.setSafe(0, 100); float8Vector.setSafe(0, 100.05); @@ -98,14 +95,17 @@ public void tearDown() throws Exception { @Test public void testShouldGetObjectClassReturnMapClass() throws Exception { - accessorIterator.assertAccessorGetter(vector, + accessorIterator.assertAccessorGetter( + vector, ArrowFlightJdbcStructVectorAccessor::getObjectClass, (accessor, currentRow) -> equalTo(Map.class)); } @Test public void testShouldGetObjectReturnValidMap() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcStructVectorAccessor::getObject, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcStructVectorAccessor::getObject, (accessor, currentRow) -> { Map expected = new HashMap<>(); expected.put("int", 100 * (currentRow + 1)); @@ -119,37 +119,40 @@ public void testShouldGetObjectReturnValidMap() throws Exception { public void testShouldGetObjectReturnNull() throws Exception { vector.setNull(0); vector.setNull(1); - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcStructVectorAccessor::getObject, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcStructVectorAccessor::getObject, (accessor, currentRow) -> nullValue()); } @Test public void testShouldGetStructReturnValidStruct() throws Exception { - accessorIterator.iterate(vector, (accessor, currentRow) -> { - Struct struct = accessor.getStruct(); - assert struct != null; + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + Struct struct = accessor.getStruct(); + assert struct != null; - Object[] expected = new Object[] { - 100 * (currentRow + 1), - 100.05 * (currentRow + 1) - }; + Object[] expected = new Object[] {100 * (currentRow + 1), 100.05 * (currentRow + 1)}; - collector.checkThat(struct.getAttributes(), equalTo(expected)); - }); + collector.checkThat(struct.getAttributes(), equalTo(expected)); + }); } @Test public void testShouldGetStructReturnNull() throws Exception { vector.setNull(0); vector.setNull(1); - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcStructVectorAccessor::getStruct, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcStructVectorAccessor::getStruct, (accessor, currentRow) -> nullValue()); } @Test public void testShouldGetObjectWorkWithNestedComplexData() throws SQLException { - try (StructVector rootVector = StructVector.empty("", - rootAllocatorTestRule.getRootAllocator())) { + try (StructVector rootVector = + StructVector.empty("", rootAllocatorTestRule.getRootAllocator())) { StructVector structVector = rootVector.addOrGetStruct("struct"); FieldType intFieldType = FieldType.nullable(Types.MinorType.INT.getType()); @@ -199,8 +202,7 @@ public void testShouldGetObjectWorkWithNestedComplexData() throws SQLException { expected.put("union", true); ArrowFlightJdbcStructVectorAccessor accessor = - new ArrowFlightJdbcStructVectorAccessor(rootVector, () -> 0, (boolean wasNull) -> { - }); + new ArrowFlightJdbcStructVectorAccessor(rootVector, () -> 0, (boolean wasNull) -> {}); Assert.assertEquals(expected, accessor.getObject()); Assert.assertEquals(expected.toString(), accessor.getString()); diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcUnionVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcUnionVectorAccessorTest.java index 9ec9388ff87..32139b24404 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcUnionVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/ArrowFlightJdbcUnionVectorAccessorTest.java @@ -23,7 +23,6 @@ import java.sql.Timestamp; import java.util.Arrays; import java.util.List; - import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; import org.apache.arrow.vector.complex.UnionVector; @@ -43,20 +42,18 @@ public class ArrowFlightJdbcUnionVectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private UnionVector vector; private final AccessorTestUtils.AccessorSupplier accessorSupplier = - (vector, getCurrentRow) -> new ArrowFlightJdbcUnionVectorAccessor((UnionVector) vector, - getCurrentRow, (boolean wasNull) -> { - }); + (vector, getCurrentRow) -> + new ArrowFlightJdbcUnionVectorAccessor( + (UnionVector) vector, getCurrentRow, (boolean wasNull) -> {}); private final AccessorTestUtils.AccessorIterator - accessorIterator = - new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); + accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @Before public void setup() { @@ -96,12 +93,8 @@ public void tearDown() { @Test public void getObject() throws Exception { List result = accessorIterator.toList(vector); - List expected = Arrays.asList( - Long.MAX_VALUE, - Math.PI, - new Timestamp(1625702400000L), - null, - null); + List expected = + Arrays.asList(Long.MAX_VALUE, Math.PI, new Timestamp(1625702400000L), null, null); collector.checkThat(result, is(expected)); } @@ -111,8 +104,7 @@ public void getObjectForNull() throws Exception { vector.reset(); vector.setValueCount(5); - accessorIterator.assertAccessorGetter(vector, - AbstractArrowFlightJdbcUnionVectorAccessor::getObject, - equalTo(null)); + accessorIterator.assertAccessorGetter( + vector, AbstractArrowFlightJdbcUnionVectorAccessor::getObject, equalTo(null)); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBaseIntVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBaseIntVectorAccessorTest.java index 5e54b545a85..b2cb7476393 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBaseIntVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBaseIntVectorAccessorTest.java @@ -22,7 +22,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.function.Supplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; @@ -44,75 +43,87 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; - @RunWith(Parameterized.class) public class ArrowFlightJdbcBaseIntVectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private BaseIntVector vector; private final Supplier vectorSupplier; private final AccessorTestUtils.AccessorSupplier - accessorSupplier = (vector, getCurrentRow) -> { - ArrowFlightJdbcAccessorFactory.WasNullConsumer noOpWasNullConsumer = (boolean wasNull) -> { - }; - if (vector instanceof UInt1Vector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((UInt1Vector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof UInt2Vector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((UInt2Vector) vector, getCurrentRow, - noOpWasNullConsumer); - } else { - if (vector instanceof UInt4Vector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((UInt4Vector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof UInt8Vector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((UInt8Vector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof TinyIntVector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((TinyIntVector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof SmallIntVector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((SmallIntVector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof IntVector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((IntVector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof BigIntVector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((BigIntVector) vector, getCurrentRow, - noOpWasNullConsumer); - } - } - throw new UnsupportedOperationException(); - }; + accessorSupplier = + (vector, getCurrentRow) -> { + ArrowFlightJdbcAccessorFactory.WasNullConsumer noOpWasNullConsumer = + (boolean wasNull) -> {}; + if (vector instanceof UInt1Vector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (UInt1Vector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof UInt2Vector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (UInt2Vector) vector, getCurrentRow, noOpWasNullConsumer); + } else { + if (vector instanceof UInt4Vector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (UInt4Vector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof UInt8Vector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (UInt8Vector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof TinyIntVector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (TinyIntVector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof SmallIntVector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (SmallIntVector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof IntVector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (IntVector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof BigIntVector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (BigIntVector) vector, getCurrentRow, noOpWasNullConsumer); + } + } + throw new UnsupportedOperationException(); + }; private final AccessorTestUtils.AccessorIterator accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @Parameterized.Parameters(name = "{1}") public static Collection data() { - return Arrays.asList(new Object[][] { - {(Supplier) () -> rootAllocatorTestRule.createIntVector(), "IntVector"}, - {(Supplier) () -> rootAllocatorTestRule.createSmallIntVector(), - "SmallIntVector"}, - {(Supplier) () -> rootAllocatorTestRule.createTinyIntVector(), - "TinyIntVector"}, - {(Supplier) () -> rootAllocatorTestRule.createBigIntVector(), - "BigIntVector"}, - {(Supplier) () -> rootAllocatorTestRule.createUInt1Vector(), "UInt1Vector"}, - {(Supplier) () -> rootAllocatorTestRule.createUInt2Vector(), "UInt2Vector"}, - {(Supplier) () -> rootAllocatorTestRule.createUInt4Vector(), "UInt4Vector"}, - {(Supplier) () -> rootAllocatorTestRule.createUInt8Vector(), "UInt8Vector"} - }); + return Arrays.asList( + new Object[][] { + {(Supplier) () -> rootAllocatorTestRule.createIntVector(), "IntVector"}, + { + (Supplier) () -> rootAllocatorTestRule.createSmallIntVector(), + "SmallIntVector" + }, + { + (Supplier) () -> rootAllocatorTestRule.createTinyIntVector(), + "TinyIntVector" + }, + { + (Supplier) () -> rootAllocatorTestRule.createBigIntVector(), + "BigIntVector" + }, + { + (Supplier) () -> rootAllocatorTestRule.createUInt1Vector(), "UInt1Vector" + }, + { + (Supplier) () -> rootAllocatorTestRule.createUInt2Vector(), "UInt2Vector" + }, + { + (Supplier) () -> rootAllocatorTestRule.createUInt4Vector(), "UInt4Vector" + }, + {(Supplier) () -> rootAllocatorTestRule.createUInt8Vector(), "UInt8Vector"} + }); } - public ArrowFlightJdbcBaseIntVectorAccessorTest(Supplier vectorSupplier, - String vectorType) { + public ArrowFlightJdbcBaseIntVectorAccessorTest( + Supplier vectorSupplier, String vectorType) { this.vectorSupplier = vectorSupplier; } @@ -128,44 +139,55 @@ public void tearDown() { @Test public void testShouldConvertToByteMethodFromBaseIntVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcBaseIntVectorAccessor::getByte, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcBaseIntVectorAccessor::getByte, (accessor, currentRow) -> equalTo((byte) accessor.getLong())); } @Test public void testShouldConvertToShortMethodFromBaseIntVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcBaseIntVectorAccessor::getShort, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcBaseIntVectorAccessor::getShort, (accessor, currentRow) -> equalTo((short) accessor.getLong())); } @Test public void testShouldConvertToIntegerMethodFromBaseIntVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcBaseIntVectorAccessor::getInt, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcBaseIntVectorAccessor::getInt, (accessor, currentRow) -> equalTo((int) accessor.getLong())); } @Test public void testShouldConvertToFloatMethodFromBaseIntVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcBaseIntVectorAccessor::getFloat, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcBaseIntVectorAccessor::getFloat, (accessor, currentRow) -> equalTo((float) accessor.getLong())); } @Test public void testShouldConvertToDoubleMethodFromBaseIntVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcBaseIntVectorAccessor::getDouble, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcBaseIntVectorAccessor::getDouble, (accessor, currentRow) -> equalTo((double) accessor.getLong())); } @Test public void testShouldConvertToBooleanMethodFromBaseIntVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcBaseIntVectorAccessor::getBoolean, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcBaseIntVectorAccessor::getBoolean, (accessor, currentRow) -> equalTo(accessor.getLong() != 0L)); } @Test public void testShouldGetObjectClass() throws Exception { - accessorIterator.assertAccessorGetter(vector, - ArrowFlightJdbcBaseIntVectorAccessor::getObjectClass, - equalTo(Long.class)); + accessorIterator.assertAccessorGetter( + vector, ArrowFlightJdbcBaseIntVectorAccessor::getObjectClass, equalTo(Long.class)); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBaseIntVectorAccessorUnitTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBaseIntVectorAccessorUnitTest.java index 2e64b6fb402..513800d769a 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBaseIntVectorAccessorUnitTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBaseIntVectorAccessorUnitTest.java @@ -44,8 +44,7 @@ @RunWith(MockitoJUnitRunner.class) public class ArrowFlightJdbcBaseIntVectorAccessorUnitTest { - @ClassRule - public static RootAllocatorTestRule rule = new RootAllocatorTestRule(); + @ClassRule public static RootAllocatorTestRule rule = new RootAllocatorTestRule(); private static UInt4Vector int4Vector; private static UInt8Vector int8Vector; private static IntVector intVectorWithNull; @@ -53,40 +52,40 @@ public class ArrowFlightJdbcBaseIntVectorAccessorUnitTest { private static SmallIntVector smallIntVector; private static IntVector intVector; private static BigIntVector bigIntVector; - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private final AccessorTestUtils.AccessorSupplier - accessorSupplier = (vector, getCurrentRow) -> { - ArrowFlightJdbcAccessorFactory.WasNullConsumer noOpWasNullConsumer = (boolean wasNull) -> { - }; - if (vector instanceof UInt1Vector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((UInt1Vector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof UInt2Vector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((UInt2Vector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof UInt4Vector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((UInt4Vector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof UInt8Vector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((UInt8Vector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof TinyIntVector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((TinyIntVector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof SmallIntVector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((SmallIntVector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof IntVector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((IntVector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof BigIntVector) { - return new ArrowFlightJdbcBaseIntVectorAccessor((BigIntVector) vector, getCurrentRow, - noOpWasNullConsumer); - } - return null; - }; + accessorSupplier = + (vector, getCurrentRow) -> { + ArrowFlightJdbcAccessorFactory.WasNullConsumer noOpWasNullConsumer = + (boolean wasNull) -> {}; + if (vector instanceof UInt1Vector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (UInt1Vector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof UInt2Vector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (UInt2Vector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof UInt4Vector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (UInt4Vector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof UInt8Vector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (UInt8Vector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof TinyIntVector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (TinyIntVector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof SmallIntVector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (SmallIntVector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof IntVector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (IntVector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof BigIntVector) { + return new ArrowFlightJdbcBaseIntVectorAccessor( + (BigIntVector) vector, getCurrentRow, noOpWasNullConsumer); + } + return null; + }; private final AccessorTestUtils.AccessorIterator accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @@ -124,90 +123,108 @@ public static void setup() { @AfterClass public static void tearDown() throws Exception { - AutoCloseables.close(bigIntVector, intVector, smallIntVector, tinyIntVector, int4Vector, - int8Vector, intVectorWithNull, rule); + AutoCloseables.close( + bigIntVector, + intVector, + smallIntVector, + tinyIntVector, + int4Vector, + int8Vector, + intVectorWithNull, + rule); } @Test public void testShouldGetStringFromUnsignedValue() throws Exception { - accessorIterator.assertAccessorGetter(int8Vector, - ArrowFlightJdbcBaseIntVectorAccessor::getString, equalTo("18446744073709551615")); + accessorIterator.assertAccessorGetter( + int8Vector, + ArrowFlightJdbcBaseIntVectorAccessor::getString, + equalTo("18446744073709551615")); } @Test public void testShouldGetBytesFromIntVectorThrowsSqlException() throws Exception { - accessorIterator.assertAccessorGetterThrowingException(intVector, ArrowFlightJdbcBaseIntVectorAccessor::getBytes); + accessorIterator.assertAccessorGetterThrowingException( + intVector, ArrowFlightJdbcBaseIntVectorAccessor::getBytes); } @Test public void testShouldGetStringFromIntVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(intVectorWithNull, - ArrowFlightJdbcBaseIntVectorAccessor::getString, CoreMatchers.nullValue()); + accessorIterator.assertAccessorGetter( + intVectorWithNull, + ArrowFlightJdbcBaseIntVectorAccessor::getString, + CoreMatchers.nullValue()); } @Test public void testShouldGetObjectFromInt() throws Exception { - accessorIterator.assertAccessorGetter(intVector, - ArrowFlightJdbcBaseIntVectorAccessor::getObject, equalTo(0xAABBCCDD)); + accessorIterator.assertAccessorGetter( + intVector, ArrowFlightJdbcBaseIntVectorAccessor::getObject, equalTo(0xAABBCCDD)); } @Test public void testShouldGetObjectFromTinyInt() throws Exception { - accessorIterator.assertAccessorGetter(tinyIntVector, - ArrowFlightJdbcBaseIntVectorAccessor::getObject, equalTo((byte) 0xAA)); + accessorIterator.assertAccessorGetter( + tinyIntVector, ArrowFlightJdbcBaseIntVectorAccessor::getObject, equalTo((byte) 0xAA)); } @Test public void testShouldGetObjectFromSmallInt() throws Exception { - accessorIterator.assertAccessorGetter(smallIntVector, - ArrowFlightJdbcBaseIntVectorAccessor::getObject, equalTo((short) 0xAABB)); + accessorIterator.assertAccessorGetter( + smallIntVector, ArrowFlightJdbcBaseIntVectorAccessor::getObject, equalTo((short) 0xAABB)); } @Test public void testShouldGetObjectFromBigInt() throws Exception { - accessorIterator.assertAccessorGetter(bigIntVector, - ArrowFlightJdbcBaseIntVectorAccessor::getObject, equalTo(0xAABBCCDDEEFFAABBL)); + accessorIterator.assertAccessorGetter( + bigIntVector, + ArrowFlightJdbcBaseIntVectorAccessor::getObject, + equalTo(0xAABBCCDDEEFFAABBL)); } @Test public void testShouldGetObjectFromUnsignedInt() throws Exception { - accessorIterator.assertAccessorGetter(int4Vector, - ArrowFlightJdbcBaseIntVectorAccessor::getObject, equalTo(0x80000001)); + accessorIterator.assertAccessorGetter( + int4Vector, ArrowFlightJdbcBaseIntVectorAccessor::getObject, equalTo(0x80000001)); } @Test public void testShouldGetObjectFromIntVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(intVectorWithNull, - ArrowFlightJdbcBaseIntVectorAccessor::getObject, CoreMatchers.nullValue()); + accessorIterator.assertAccessorGetter( + intVectorWithNull, + ArrowFlightJdbcBaseIntVectorAccessor::getObject, + CoreMatchers.nullValue()); } @Test public void testShouldGetBigDecimalFromIntVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(intVectorWithNull, - ArrowFlightJdbcBaseIntVectorAccessor::getBigDecimal, CoreMatchers.nullValue()); + accessorIterator.assertAccessorGetter( + intVectorWithNull, + ArrowFlightJdbcBaseIntVectorAccessor::getBigDecimal, + CoreMatchers.nullValue()); } @Test public void testShouldGetBigDecimalWithScaleFromIntVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(intVectorWithNull, accessor -> accessor.getBigDecimal(2), - CoreMatchers.nullValue()); + accessorIterator.assertAccessorGetter( + intVectorWithNull, accessor -> accessor.getBigDecimal(2), CoreMatchers.nullValue()); } @Test public void testShouldGetBytesFromSmallVectorThrowsSqlException() throws Exception { - accessorIterator.assertAccessorGetterThrowingException(smallIntVector, - ArrowFlightJdbcBaseIntVectorAccessor::getBytes); + accessorIterator.assertAccessorGetterThrowingException( + smallIntVector, ArrowFlightJdbcBaseIntVectorAccessor::getBytes); } @Test public void testShouldGetBytesFromTinyIntVectorThrowsSqlException() throws Exception { - accessorIterator.assertAccessorGetterThrowingException(tinyIntVector, - ArrowFlightJdbcBaseIntVectorAccessor::getBytes); + accessorIterator.assertAccessorGetterThrowingException( + tinyIntVector, ArrowFlightJdbcBaseIntVectorAccessor::getBytes); } @Test public void testShouldGetBytesFromBigIntVectorThrowsSqlException() throws Exception { - accessorIterator.assertAccessorGetterThrowingException(bigIntVector, - ArrowFlightJdbcBaseIntVectorAccessor::getBytes); + accessorIterator.assertAccessorGetterThrowingException( + bigIntVector, ArrowFlightJdbcBaseIntVectorAccessor::getBytes); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBitVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBitVectorAccessorTest.java index 00537bfa028..394f598b6a8 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBitVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcBitVectorAccessorTest.java @@ -21,7 +21,6 @@ import static org.hamcrest.CoreMatchers.is; import java.math.BigDecimal; - import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils.AccessorIterator; import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils.CheckedFunction; @@ -38,14 +37,13 @@ public class ArrowFlightJdbcBitVectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private final AccessorTestUtils.AccessorSupplier - accessorSupplier = (vector, getCurrentRow) -> new ArrowFlightJdbcBitVectorAccessor((BitVector) vector, - getCurrentRow, (boolean wasNull) -> { - }); - private final AccessorIterator - accessorIterator = + accessorSupplier = + (vector, getCurrentRow) -> + new ArrowFlightJdbcBitVectorAccessor( + (BitVector) vector, getCurrentRow, (boolean wasNull) -> {}); + private final AccessorIterator accessorIterator = new AccessorIterator<>(collector, accessorSupplier); private BitVector vector; private BitVector vectorWithNull; @@ -64,12 +62,16 @@ public void tearDown() { this.vectorWithNull.close(); } - private void iterate(final CheckedFunction function, - final T result, - final T resultIfFalse, final BitVector vector) throws Exception { - accessorIterator.assertAccessorGetter(vector, function, - (accessor, currentRow) -> is(arrayToAssert[currentRow] ? result : resultIfFalse) - ); + private void iterate( + final CheckedFunction function, + final T result, + final T resultIfFalse, + final BitVector vector) + throws Exception { + accessorIterator.assertAccessorGetter( + vector, + function, + (accessor, currentRow) -> is(arrayToAssert[currentRow] ? result : resultIfFalse)); } @Test @@ -90,66 +92,57 @@ public void testShouldGetShortMethodFromBitVector() throws Exception { @Test public void testShouldGetIntMethodFromBitVector() throws Exception { iterate(ArrowFlightJdbcBitVectorAccessor::getInt, 1, 0, vector); - } @Test public void testShouldGetLongMethodFromBitVector() throws Exception { iterate(ArrowFlightJdbcBitVectorAccessor::getLong, (long) 1, (long) 0, vector); - } @Test public void testShouldGetFloatMethodFromBitVector() throws Exception { iterate(ArrowFlightJdbcBitVectorAccessor::getFloat, (float) 1, (float) 0, vector); - } @Test public void testShouldGetDoubleMethodFromBitVector() throws Exception { iterate(ArrowFlightJdbcBitVectorAccessor::getDouble, (double) 1, (double) 0, vector); - } @Test public void testShouldGetBigDecimalMethodFromBitVector() throws Exception { - iterate(ArrowFlightJdbcBitVectorAccessor::getBigDecimal, BigDecimal.ONE, BigDecimal.ZERO, - vector); + iterate( + ArrowFlightJdbcBitVectorAccessor::getBigDecimal, BigDecimal.ONE, BigDecimal.ZERO, vector); } @Test public void testShouldGetBigDecimalMethodFromBitVectorFromNull() throws Exception { iterate(ArrowFlightJdbcBitVectorAccessor::getBigDecimal, null, null, vectorWithNull); - } @Test public void testShouldGetObjectMethodFromBitVector() throws Exception { iterate(ArrowFlightJdbcBitVectorAccessor::getObject, true, false, vector); - } @Test public void testShouldGetObjectMethodFromBitVectorFromNull() throws Exception { iterate(ArrowFlightJdbcBitVectorAccessor::getObject, null, null, vectorWithNull); - } @Test public void testShouldGetStringMethodFromBitVector() throws Exception { iterate(ArrowFlightJdbcBitVectorAccessor::getString, "true", "false", vector); - } @Test public void testShouldGetStringMethodFromBitVectorFromNull() throws Exception { iterate(ArrowFlightJdbcBitVectorAccessor::getString, null, null, vectorWithNull); - } @Test public void testShouldGetObjectClass() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcBitVectorAccessor::getObjectClass, - equalTo(Boolean.class)); + accessorIterator.assertAccessorGetter( + vector, ArrowFlightJdbcBitVectorAccessor::getObjectClass, equalTo(Boolean.class)); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcDecimalVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcDecimalVectorAccessorTest.java index b7bd7c40fef..de933feb574 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcDecimalVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcDecimalVectorAccessorTest.java @@ -24,7 +24,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.function.Supplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessorFactory; import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; @@ -47,43 +46,47 @@ public class ArrowFlightJdbcDecimalVectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); private final Supplier vectorSupplier; private ValueVector vector; private ValueVector vectorWithNull; private final AccessorTestUtils.AccessorSupplier - accessorSupplier = (vector, getCurrentRow) -> { - ArrowFlightJdbcAccessorFactory.WasNullConsumer noOpWasNullConsumer = (boolean wasNull) -> { - }; - if (vector instanceof DecimalVector) { - return new ArrowFlightJdbcDecimalVectorAccessor((DecimalVector) vector, getCurrentRow, - noOpWasNullConsumer); - } else if (vector instanceof Decimal256Vector) { - return new ArrowFlightJdbcDecimalVectorAccessor((Decimal256Vector) vector, getCurrentRow, - noOpWasNullConsumer); - } - return null; - }; + accessorSupplier = + (vector, getCurrentRow) -> { + ArrowFlightJdbcAccessorFactory.WasNullConsumer noOpWasNullConsumer = + (boolean wasNull) -> {}; + if (vector instanceof DecimalVector) { + return new ArrowFlightJdbcDecimalVectorAccessor( + (DecimalVector) vector, getCurrentRow, noOpWasNullConsumer); + } else if (vector instanceof Decimal256Vector) { + return new ArrowFlightJdbcDecimalVectorAccessor( + (Decimal256Vector) vector, getCurrentRow, noOpWasNullConsumer); + } + return null; + }; private final AccessorTestUtils.AccessorIterator - accessorIterator = - new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); + accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @Parameterized.Parameters(name = "{1}") public static Collection data() { - return Arrays.asList(new Object[][] { - {(Supplier) () -> rootAllocatorTestRule.createDecimalVector(), - "DecimalVector"}, - {(Supplier) () -> rootAllocatorTestRule.createDecimal256Vector(), - "Decimal256Vector"}, - }); + return Arrays.asList( + new Object[][] { + { + (Supplier) () -> rootAllocatorTestRule.createDecimalVector(), + "DecimalVector" + }, + { + (Supplier) () -> rootAllocatorTestRule.createDecimal256Vector(), + "Decimal256Vector" + }, + }); } - public ArrowFlightJdbcDecimalVectorAccessorTest(Supplier vectorSupplier, - String vectorType) { + public ArrowFlightJdbcDecimalVectorAccessorTest( + Supplier vectorSupplier, String vectorType) { this.vectorSupplier = vectorSupplier; } @@ -104,145 +107,177 @@ public void tearDown() { @Test public void testShouldGetBigDecimalFromDecimalVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, + accessorIterator.assertAccessorGetter( + vector, ArrowFlightJdbcDecimalVectorAccessor::getBigDecimal, (accessor, currentRow) -> CoreMatchers.notNullValue()); } @Test public void testShouldGetDoubleMethodFromDecimalVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcDecimalVectorAccessor::getDouble, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcDecimalVectorAccessor::getDouble, (accessor, currentRow) -> equalTo(accessor.getBigDecimal().doubleValue())); } @Test public void testShouldGetFloatMethodFromDecimalVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcDecimalVectorAccessor::getFloat, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcDecimalVectorAccessor::getFloat, (accessor, currentRow) -> equalTo(accessor.getBigDecimal().floatValue())); } @Test public void testShouldGetLongMethodFromDecimalVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcDecimalVectorAccessor::getLong, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcDecimalVectorAccessor::getLong, (accessor, currentRow) -> equalTo(accessor.getBigDecimal().longValue())); } @Test public void testShouldGetIntMethodFromDecimalVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcDecimalVectorAccessor::getInt, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcDecimalVectorAccessor::getInt, (accessor, currentRow) -> equalTo(accessor.getBigDecimal().intValue())); } @Test public void testShouldGetShortMethodFromDecimalVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcDecimalVectorAccessor::getShort, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcDecimalVectorAccessor::getShort, (accessor, currentRow) -> equalTo(accessor.getBigDecimal().shortValue())); } @Test public void testShouldGetByteMethodFromDecimalVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcDecimalVectorAccessor::getByte, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcDecimalVectorAccessor::getByte, (accessor, currentRow) -> equalTo(accessor.getBigDecimal().byteValue())); } @Test public void testShouldGetStringMethodFromDecimalVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcDecimalVectorAccessor::getString, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcDecimalVectorAccessor::getString, (accessor, currentRow) -> equalTo(accessor.getBigDecimal().toString())); } @Test public void testShouldGetBooleanMethodFromDecimalVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcDecimalVectorAccessor::getBoolean, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcDecimalVectorAccessor::getBoolean, (accessor, currentRow) -> equalTo(!accessor.getBigDecimal().equals(BigDecimal.ZERO))); } @Test public void testShouldGetObjectMethodFromDecimalVector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcDecimalVectorAccessor::getObject, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcDecimalVectorAccessor::getObject, (accessor, currentRow) -> equalTo(accessor.getBigDecimal())); } @Test public void testShouldGetObjectClass() throws Exception { - accessorIterator.assertAccessorGetter(vector, + accessorIterator.assertAccessorGetter( + vector, ArrowFlightJdbcDecimalVectorAccessor::getObjectClass, (accessor, currentRow) -> equalTo(BigDecimal.class)); } @Test public void testShouldGetBigDecimalMethodFromDecimalVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(vectorWithNull, + accessorIterator.assertAccessorGetter( + vectorWithNull, ArrowFlightJdbcDecimalVectorAccessor::getBigDecimal, (accessor, currentRow) -> CoreMatchers.nullValue()); } @Test public void testShouldGetObjectMethodFromDecimalVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(vectorWithNull, + accessorIterator.assertAccessorGetter( + vectorWithNull, ArrowFlightJdbcDecimalVectorAccessor::getObject, (accessor, currentRow) -> CoreMatchers.nullValue()); } @Test public void testShouldGetStringMethodFromDecimalVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(vectorWithNull, + accessorIterator.assertAccessorGetter( + vectorWithNull, ArrowFlightJdbcDecimalVectorAccessor::getString, (accessor, currentRow) -> CoreMatchers.nullValue()); } @Test public void testShouldGetByteMethodFromDecimalVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(vectorWithNull, + accessorIterator.assertAccessorGetter( + vectorWithNull, ArrowFlightJdbcDecimalVectorAccessor::getByte, (accessor, currentRow) -> is((byte) 0)); } @Test public void testShouldGetShortMethodFromDecimalVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(vectorWithNull, + accessorIterator.assertAccessorGetter( + vectorWithNull, ArrowFlightJdbcDecimalVectorAccessor::getShort, (accessor, currentRow) -> is((short) 0)); } @Test public void testShouldGetIntMethodFromDecimalVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(vectorWithNull, + accessorIterator.assertAccessorGetter( + vectorWithNull, ArrowFlightJdbcDecimalVectorAccessor::getInt, (accessor, currentRow) -> is(0)); } @Test public void testShouldGetLongMethodFromDecimalVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(vectorWithNull, + accessorIterator.assertAccessorGetter( + vectorWithNull, ArrowFlightJdbcDecimalVectorAccessor::getLong, (accessor, currentRow) -> is((long) 0)); } @Test public void testShouldGetFloatMethodFromDecimalVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(vectorWithNull, + accessorIterator.assertAccessorGetter( + vectorWithNull, ArrowFlightJdbcDecimalVectorAccessor::getFloat, (accessor, currentRow) -> is(0.0f)); } @Test public void testShouldGetDoubleMethodFromDecimalVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(vectorWithNull, + accessorIterator.assertAccessorGetter( + vectorWithNull, ArrowFlightJdbcDecimalVectorAccessor::getDouble, (accessor, currentRow) -> is(0.0D)); } @Test public void testShouldGetBooleanMethodFromDecimalVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(vectorWithNull, + accessorIterator.assertAccessorGetter( + vectorWithNull, ArrowFlightJdbcDecimalVectorAccessor::getBoolean, (accessor, currentRow) -> is(false)); } @Test public void testShouldGetBigDecimalWithScaleMethodFromDecimalVectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(vectorWithNull, accessor -> accessor.getBigDecimal(2), + accessorIterator.assertAccessorGetter( + vectorWithNull, + accessor -> accessor.getBigDecimal(2), (accessor, currentRow) -> CoreMatchers.nullValue()); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat4VectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat4VectorAccessorTest.java index 74a65715ec0..b92c7fc12f1 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat4VectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat4VectorAccessorTest.java @@ -23,7 +23,6 @@ import java.math.BigDecimal; import java.math.RoundingMode; import java.sql.SQLException; - import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; import org.apache.arrow.vector.Float4Vector; @@ -41,23 +40,20 @@ public class ArrowFlightJdbcFloat4VectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); - @Rule - public ExpectedException exceptionCollector = ExpectedException.none(); + @Rule public ExpectedException exceptionCollector = ExpectedException.none(); private Float4Vector vector; private final AccessorTestUtils.AccessorSupplier accessorSupplier = - (vector, getCurrentRow) -> new ArrowFlightJdbcFloat4VectorAccessor((Float4Vector) vector, - getCurrentRow, (boolean wasNull) -> { - }); + (vector, getCurrentRow) -> + new ArrowFlightJdbcFloat4VectorAccessor( + (Float4Vector) vector, getCurrentRow, (boolean wasNull) -> {}); private final AccessorTestUtils.AccessorIterator - accessorIterator = - new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); + accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @Before public void setup() { @@ -71,55 +67,61 @@ public void tearDown() { @Test public void testShouldGetFloatMethodFromFloat4Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat4VectorAccessor::getFloat, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat4VectorAccessor::getFloat, (accessor, currentRow) -> is(vector.get(currentRow))); } @Test public void testShouldGetObjectMethodFromFloat4Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat4VectorAccessor::getObject, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat4VectorAccessor::getObject, (accessor) -> is(accessor.getFloat())); } @Test public void testShouldGetStringMethodFromFloat4Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat4VectorAccessor::getString, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat4VectorAccessor::getString, accessor -> is(Float.toString(accessor.getFloat()))); } @Test public void testShouldGetStringMethodFromFloat4VectorWithNull() throws Exception { - try (final Float4Vector float4Vector = new Float4Vector("ID", - rootAllocatorTestRule.getRootAllocator())) { + try (final Float4Vector float4Vector = + new Float4Vector("ID", rootAllocatorTestRule.getRootAllocator())) { float4Vector.setNull(0); float4Vector.setValueCount(1); - accessorIterator.assertAccessorGetter(float4Vector, - ArrowFlightJdbcFloat4VectorAccessor::getString, - CoreMatchers.nullValue()); + accessorIterator.assertAccessorGetter( + float4Vector, ArrowFlightJdbcFloat4VectorAccessor::getString, CoreMatchers.nullValue()); } } @Test public void testShouldGetFloatMethodFromFloat4VectorWithNull() throws Exception { - try (final Float4Vector float4Vector = new Float4Vector("ID", - rootAllocatorTestRule.getRootAllocator())) { + try (final Float4Vector float4Vector = + new Float4Vector("ID", rootAllocatorTestRule.getRootAllocator())) { float4Vector.setNull(0); float4Vector.setValueCount(1); - accessorIterator.assertAccessorGetter(float4Vector, - ArrowFlightJdbcFloat4VectorAccessor::getFloat, is(0.0f)); + accessorIterator.assertAccessorGetter( + float4Vector, ArrowFlightJdbcFloat4VectorAccessor::getFloat, is(0.0f)); } } @Test public void testShouldGetBigDecimalMethodFromFloat4VectorWithNull() throws Exception { - try (final Float4Vector float4Vector = new Float4Vector("ID", - rootAllocatorTestRule.getRootAllocator())) { + try (final Float4Vector float4Vector = + new Float4Vector("ID", rootAllocatorTestRule.getRootAllocator())) { float4Vector.setNull(0); float4Vector.setValueCount(1); - accessorIterator.assertAccessorGetter(float4Vector, + accessorIterator.assertAccessorGetter( + float4Vector, ArrowFlightJdbcFloat4VectorAccessor::getBigDecimal, CoreMatchers.nullValue()); } @@ -127,79 +129,96 @@ public void testShouldGetBigDecimalMethodFromFloat4VectorWithNull() throws Excep @Test public void testShouldGetObjectMethodFromFloat4VectorWithNull() throws Exception { - try (final Float4Vector float4Vector = new Float4Vector("ID", - rootAllocatorTestRule.getRootAllocator())) { + try (final Float4Vector float4Vector = + new Float4Vector("ID", rootAllocatorTestRule.getRootAllocator())) { float4Vector.setNull(0); float4Vector.setValueCount(1); - accessorIterator.assertAccessorGetter(float4Vector, - ArrowFlightJdbcFloat4VectorAccessor::getObject, - CoreMatchers.nullValue()); + accessorIterator.assertAccessorGetter( + float4Vector, ArrowFlightJdbcFloat4VectorAccessor::getObject, CoreMatchers.nullValue()); } } @Test public void testShouldGetBooleanMethodFromFloat4Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat4VectorAccessor::getBoolean, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat4VectorAccessor::getBoolean, accessor -> is(accessor.getFloat() != 0.0f)); } @Test public void testShouldGetByteMethodFromFloat4Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat4VectorAccessor::getByte, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat4VectorAccessor::getByte, accessor -> is((byte) accessor.getFloat())); } @Test public void testShouldGetShortMethodFromFloat4Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat4VectorAccessor::getShort, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat4VectorAccessor::getShort, accessor -> is((short) accessor.getFloat())); } @Test public void testShouldGetIntMethodFromFloat4Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat4VectorAccessor::getInt, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat4VectorAccessor::getInt, accessor -> is((int) accessor.getFloat())); } @Test public void testShouldGetLongMethodFromFloat4Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat4VectorAccessor::getLong, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat4VectorAccessor::getLong, accessor -> is((long) accessor.getFloat())); } @Test public void testShouldGetDoubleMethodFromFloat4Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat4VectorAccessor::getDouble, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat4VectorAccessor::getDouble, accessor -> is((double) accessor.getFloat())); } @Test public void testShouldGetBigDecimalMethodFromFloat4Vector() throws Exception { - accessorIterator.iterate(vector, (accessor, currentRow) -> { - float value = accessor.getFloat(); - if (Float.isInfinite(value) || Float.isNaN(value)) { - exceptionCollector.expect(SQLException.class); - } - collector.checkThat(accessor.getBigDecimal(), is(BigDecimal.valueOf(value))); - }); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + float value = accessor.getFloat(); + if (Float.isInfinite(value) || Float.isNaN(value)) { + exceptionCollector.expect(SQLException.class); + } + collector.checkThat(accessor.getBigDecimal(), is(BigDecimal.valueOf(value))); + }); } @Test public void testShouldGetBigDecimalWithScaleMethodFromFloat4Vector() throws Exception { - accessorIterator.iterate(vector, (accessor, currentRow) -> { - float value = accessor.getFloat(); - if (Float.isInfinite(value) || Float.isNaN(value)) { - exceptionCollector.expect(SQLException.class); - } - collector.checkThat(accessor.getBigDecimal(9), - is(BigDecimal.valueOf(value).setScale(9, RoundingMode.HALF_UP))); - }); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + float value = accessor.getFloat(); + if (Float.isInfinite(value) || Float.isNaN(value)) { + exceptionCollector.expect(SQLException.class); + } + collector.checkThat( + accessor.getBigDecimal(9), + is(BigDecimal.valueOf(value).setScale(9, RoundingMode.HALF_UP))); + }); } @Test public void testShouldGetObjectClass() throws Exception { - accessorIterator.assertAccessorGetter(vector, + accessorIterator.assertAccessorGetter( + vector, ArrowFlightJdbcFloat4VectorAccessor::getObjectClass, accessor -> equalTo(Float.class)); } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat8VectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat8VectorAccessorTest.java index 26758287a96..27a01ab31e0 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat8VectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/numeric/ArrowFlightJdbcFloat8VectorAccessorTest.java @@ -23,7 +23,6 @@ import java.math.BigDecimal; import java.math.RoundingMode; import java.sql.SQLException; - import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestRule; import org.apache.arrow.vector.Float8Vector; @@ -41,25 +40,21 @@ public class ArrowFlightJdbcFloat8VectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Rule - public final ErrorCollector collector = new ErrorCollector(); - - @Rule - public ExpectedException exceptionCollector = ExpectedException.none(); + @Rule public final ErrorCollector collector = new ErrorCollector(); + @Rule public ExpectedException exceptionCollector = ExpectedException.none(); private Float8Vector vector; private Float8Vector vectorWithNull; private final AccessorTestUtils.AccessorSupplier accessorSupplier = - (vector, getCurrentRow) -> new ArrowFlightJdbcFloat8VectorAccessor((Float8Vector) vector, - getCurrentRow, (boolean wasNull) -> { - }); + (vector, getCurrentRow) -> + new ArrowFlightJdbcFloat8VectorAccessor( + (Float8Vector) vector, getCurrentRow, (boolean wasNull) -> {}); private final AccessorTestUtils.AccessorIterator - accessorIterator = - new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); + accessorIterator = new AccessorTestUtils.AccessorIterator<>(collector, accessorSupplier); @Before public void setup() { @@ -75,113 +70,133 @@ public void tearDown() { @Test public void testShouldGetDoubleMethodFromFloat8Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat8VectorAccessor::getDouble, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat8VectorAccessor::getDouble, (accessor, currentRow) -> is(vector.getValueAsDouble(currentRow))); } @Test public void testShouldGetObjectMethodFromFloat8Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat8VectorAccessor::getObject, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat8VectorAccessor::getObject, (accessor) -> is(accessor.getDouble())); } @Test public void testShouldGetStringMethodFromFloat8Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat8VectorAccessor::getString, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat8VectorAccessor::getString, (accessor) -> is(Double.toString(accessor.getDouble()))); } @Test public void testShouldGetBooleanMethodFromFloat8Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat8VectorAccessor::getBoolean, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat8VectorAccessor::getBoolean, (accessor) -> is(accessor.getDouble() != 0.0)); } @Test public void testShouldGetByteMethodFromFloat8Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat8VectorAccessor::getByte, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat8VectorAccessor::getByte, (accessor) -> is((byte) accessor.getDouble())); } @Test public void testShouldGetShortMethodFromFloat8Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat8VectorAccessor::getShort, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat8VectorAccessor::getShort, (accessor) -> is((short) accessor.getDouble())); } @Test public void testShouldGetIntMethodFromFloat8Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat8VectorAccessor::getInt, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat8VectorAccessor::getInt, (accessor) -> is((int) accessor.getDouble())); } @Test public void testShouldGetLongMethodFromFloat8Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat8VectorAccessor::getLong, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat8VectorAccessor::getLong, (accessor) -> is((long) accessor.getDouble())); } @Test public void testShouldGetFloatMethodFromFloat8Vector() throws Exception { - accessorIterator.assertAccessorGetter(vector, ArrowFlightJdbcFloat8VectorAccessor::getFloat, + accessorIterator.assertAccessorGetter( + vector, + ArrowFlightJdbcFloat8VectorAccessor::getFloat, (accessor) -> is((float) accessor.getDouble())); } @Test public void testShouldGetBigDecimalMethodFromFloat8Vector() throws Exception { - accessorIterator.iterate(vector, (accessor, currentRow) -> { - double value = accessor.getDouble(); - if (Double.isInfinite(value) || Double.isNaN(value)) { - exceptionCollector.expect(SQLException.class); - } - collector.checkThat(accessor.getBigDecimal(), is(BigDecimal.valueOf(value))); - }); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + double value = accessor.getDouble(); + if (Double.isInfinite(value) || Double.isNaN(value)) { + exceptionCollector.expect(SQLException.class); + } + collector.checkThat(accessor.getBigDecimal(), is(BigDecimal.valueOf(value))); + }); } @Test public void testShouldGetObjectClass() throws Exception { - accessorIterator - .assertAccessorGetter(vector, ArrowFlightJdbcFloat8VectorAccessor::getObjectClass, - equalTo(Double.class)); + accessorIterator.assertAccessorGetter( + vector, ArrowFlightJdbcFloat8VectorAccessor::getObjectClass, equalTo(Double.class)); } @Test public void testShouldGetStringMethodFromFloat8VectorWithNull() throws Exception { - accessorIterator - .assertAccessorGetter(vectorWithNull, ArrowFlightJdbcFloat8VectorAccessor::getString, - CoreMatchers.nullValue()); + accessorIterator.assertAccessorGetter( + vectorWithNull, ArrowFlightJdbcFloat8VectorAccessor::getString, CoreMatchers.nullValue()); } @Test public void testShouldGetFloatMethodFromFloat8VectorWithNull() throws Exception { - accessorIterator - .assertAccessorGetter(vectorWithNull, ArrowFlightJdbcFloat8VectorAccessor::getFloat, - is(0.0f)); + accessorIterator.assertAccessorGetter( + vectorWithNull, ArrowFlightJdbcFloat8VectorAccessor::getFloat, is(0.0f)); } @Test public void testShouldGetBigDecimalMethodFromFloat8VectorWithNull() throws Exception { - accessorIterator.assertAccessorGetter(vectorWithNull, + accessorIterator.assertAccessorGetter( + vectorWithNull, ArrowFlightJdbcFloat8VectorAccessor::getBigDecimal, CoreMatchers.nullValue()); } @Test public void testShouldGetBigDecimalWithScaleMethodFromFloat4Vector() throws Exception { - accessorIterator.iterate(vector, (accessor, currentRow) -> { - double value = accessor.getDouble(); - if (Double.isInfinite(value) || Double.isNaN(value)) { - exceptionCollector.expect(SQLException.class); - } - collector.checkThat(accessor.getBigDecimal(9), - is(BigDecimal.valueOf(value).setScale(9, RoundingMode.HALF_UP))); - }); + accessorIterator.iterate( + vector, + (accessor, currentRow) -> { + double value = accessor.getDouble(); + if (Double.isInfinite(value) || Double.isNaN(value)) { + exceptionCollector.expect(SQLException.class); + } + collector.checkThat( + accessor.getBigDecimal(9), + is(BigDecimal.valueOf(value).setScale(9, RoundingMode.HALF_UP))); + }); } @Test public void testShouldGetObjectMethodFromFloat8VectorWithNull() throws Exception { - accessorIterator - .assertAccessorGetter(vectorWithNull, ArrowFlightJdbcFloat8VectorAccessor::getObject, - CoreMatchers.nullValue()); + accessorIterator.assertAccessorGetter( + vectorWithNull, ArrowFlightJdbcFloat8VectorAccessor::getObject, CoreMatchers.nullValue()); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessorTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessorTest.java index 799c517dd56..12237beb83f 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessorTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessorTest.java @@ -36,7 +36,6 @@ import java.util.Calendar; import java.util.TimeZone; import java.util.function.IntSupplier; - import org.apache.arrow.driver.jdbc.accessor.impl.calendar.ArrowFlightJdbcDateVectorAccessor; import org.apache.arrow.driver.jdbc.accessor.impl.calendar.ArrowFlightJdbcTimeStampVectorAccessor; import org.apache.arrow.driver.jdbc.accessor.impl.calendar.ArrowFlightJdbcTimeVectorAccessor; @@ -57,7 +56,6 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; - @RunWith(MockitoJUnitRunner.class) public class ArrowFlightJdbcVarCharVectorAccessorTest { @@ -69,21 +67,18 @@ public class ArrowFlightJdbcVarCharVectorAccessorTest { @ClassRule public static RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); - @Mock - private ArrowFlightJdbcVarCharVectorAccessor.Getter getter; + @Mock private ArrowFlightJdbcVarCharVectorAccessor.Getter getter; - @Rule - public ErrorCollector collector = new ErrorCollector(); + @Rule public ErrorCollector collector = new ErrorCollector(); - @Rule - public ExpectedException thrown = ExpectedException.none(); + @Rule public ExpectedException thrown = ExpectedException.none(); @Before public void setUp() { IntSupplier currentRowSupplier = () -> 0; accessor = - new ArrowFlightJdbcVarCharVectorAccessor(getter, currentRowSupplier, (boolean wasNull) -> { - }); + new ArrowFlightJdbcVarCharVectorAccessor( + getter, currentRowSupplier, (boolean wasNull) -> {}); } @Test @@ -313,7 +308,8 @@ public void testShouldGetLongReturnValidNegativeLong() throws Exception { } @Test - public void testShouldBigDecimalWithParametersThrowsExceptionForNonNumericValue() throws Exception { + public void testShouldBigDecimalWithParametersThrowsExceptionForNonNumericValue() + throws Exception { Text value = new Text("Invalid value for BigDecimal."); when(getter.get(0)).thenReturn(value.copyBytes()); @@ -501,8 +497,8 @@ public void testShouldGetDateReturnValidDateWithoutCalendar() throws Exception { Calendar calendar = Calendar.getInstance(); calendar.setTime(result); - collector.checkThat(dateTimeFormat.format(calendar.getTime()), - equalTo("2021-07-02T00:00:00.000Z")); + collector.checkThat( + dateTimeFormat.format(calendar.getTime()), equalTo("2021-07-02T00:00:00.000Z")); } @Test @@ -516,8 +512,8 @@ public void testShouldGetDateReturnValidDateWithCalendar() throws Exception { calendar = Calendar.getInstance(TimeZone.getTimeZone("Etc/UTC")); calendar.setTime(result); - collector.checkThat(dateTimeFormat.format(calendar.getTime()), - equalTo("2021-07-02T03:00:00.000Z")); + collector.checkThat( + dateTimeFormat.format(calendar.getTime()), equalTo("2021-07-02T03:00:00.000Z")); } @Test @@ -575,8 +571,8 @@ public void testShouldGetTimestampReturnValidDateWithoutCalendar() throws Except Calendar calendar = Calendar.getInstance(); calendar.setTime(result); - collector.checkThat(dateTimeFormat.format(calendar.getTime()), - equalTo("2021-07-02T02:30:00.000Z")); + collector.checkThat( + dateTimeFormat.format(calendar.getTime()), equalTo("2021-07-02T02:30:00.000Z")); } @Test @@ -590,8 +586,8 @@ public void testShouldGetTimestampReturnValidDateWithCalendar() throws Exception calendar = Calendar.getInstance(TimeZone.getTimeZone("Etc/UTC")); calendar.setTime(result); - collector.checkThat(dateTimeFormat.format(calendar.getTime()), - equalTo("2021-07-02T05:30:00.000Z")); + collector.checkThat( + dateTimeFormat.format(calendar.getTime()), equalTo("2021-07-02T05:30:00.000Z")); } private void assertGetBoolean(Text value, boolean expectedResult) throws SQLException { @@ -602,7 +598,8 @@ private void assertGetBoolean(Text value, boolean expectedResult) throws SQLExce private void assertGetBooleanForSQLException(Text value) { when(getter.get(0)).thenReturn(value == null ? null : value.copyBytes()); - ThrowableAssertionUtils.simpleAssertThrowableClass(SQLException.class, () -> accessor.getBoolean()); + ThrowableAssertionUtils.simpleAssertThrowableClass( + SQLException.class, () -> accessor.getBoolean()); } @Test @@ -649,8 +646,7 @@ public void testShouldGetUnicodeStreamReturnValidInputStream() throws Exception try (final InputStream result = accessor.getUnicodeStream()) { byte[] resultBytes = toByteArray(result); - collector.checkThat(new String(resultBytes, UTF_8), - equalTo(value.toString())); + collector.checkThat(new String(resultBytes, UTF_8), equalTo(value.toString())); } } @@ -683,9 +679,8 @@ public void testShouldGetCharacterStreamReturnValidReader() throws Exception { public void testShouldGetTimeStampBeConsistentWithTimeStampAccessor() throws Exception { try (TimeStampVector timeStampVector = rootAllocatorTestRule.createTimeStampMilliVector()) { ArrowFlightJdbcTimeStampVectorAccessor timeStampVectorAccessor = - new ArrowFlightJdbcTimeStampVectorAccessor(timeStampVector, () -> 0, - (boolean wasNull) -> { - }); + new ArrowFlightJdbcTimeStampVectorAccessor( + timeStampVector, () -> 0, (boolean wasNull) -> {}); Text value = new Text(timeStampVectorAccessor.getString()); when(getter.get(0)).thenReturn(value.copyBytes()); @@ -699,8 +694,7 @@ public void testShouldGetTimeStampBeConsistentWithTimeStampAccessor() throws Exc public void testShouldGetTimeBeConsistentWithTimeAccessor() throws Exception { try (TimeMilliVector timeVector = rootAllocatorTestRule.createTimeMilliVector()) { ArrowFlightJdbcTimeVectorAccessor timeVectorAccessor = - new ArrowFlightJdbcTimeVectorAccessor(timeVector, () -> 0, (boolean wasNull) -> { - }); + new ArrowFlightJdbcTimeVectorAccessor(timeVector, () -> 0, (boolean wasNull) -> {}); Text value = new Text(timeVectorAccessor.getString()); when(getter.get(0)).thenReturn(value.copyBytes()); @@ -714,8 +708,7 @@ public void testShouldGetTimeBeConsistentWithTimeAccessor() throws Exception { public void testShouldGetDateBeConsistentWithDateAccessor() throws Exception { try (DateMilliVector dateVector = rootAllocatorTestRule.createDateMilliVector()) { ArrowFlightJdbcDateVectorAccessor dateVectorAccessor = - new ArrowFlightJdbcDateVectorAccessor(dateVector, () -> 0, (boolean wasNull) -> { - }); + new ArrowFlightJdbcDateVectorAccessor(dateVector, () -> 0, (boolean wasNull) -> {}); Text value = new Text(dateVectorAccessor.getString()); when(getter.get(0)).thenReturn(value.copyBytes()); diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/authentication/Authentication.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/authentication/Authentication.java index 5fe2b0dc057..6e53c933f2c 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/authentication/Authentication.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/authentication/Authentication.java @@ -18,7 +18,6 @@ package org.apache.arrow.driver.jdbc.authentication; import java.util.Properties; - import org.apache.arrow.flight.auth2.CallHeaderAuthenticator; public interface Authentication { @@ -31,6 +30,7 @@ public interface Authentication { /** * Uses the validCredentials variable and populate the Properties object. + * * @param properties the Properties object that will be populated. */ void populateProperties(Properties properties); diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/authentication/TokenAuthentication.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/authentication/TokenAuthentication.java index 605705d1ca9..a8825ccf15a 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/authentication/TokenAuthentication.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/authentication/TokenAuthentication.java @@ -20,7 +20,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Properties; - import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl; import org.apache.arrow.flight.CallHeaders; import org.apache.arrow.flight.CallStatus; @@ -40,7 +39,9 @@ public CallHeaderAuthenticator authenticate() { public AuthResult authenticate(CallHeaders incomingHeaders) { String authorization = incomingHeaders.get("authorization"); if (!validCredentials.contains(authorization)) { - throw CallStatus.UNAUTHENTICATED.withDescription("Invalid credentials.").toRuntimeException(); + throw CallStatus.UNAUTHENTICATED + .withDescription("Invalid credentials.") + .toRuntimeException(); } return new AuthResult() { @Override @@ -54,8 +55,11 @@ public String getPeerIdentity() { @Override public void populateProperties(Properties properties) { - this.validCredentials.forEach(value -> properties.put( - ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.TOKEN.camelName(), value)); + this.validCredentials.forEach( + value -> + properties.put( + ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.TOKEN.camelName(), + value)); } public static final class Builder { diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/authentication/UserPasswordAuthentication.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/authentication/UserPasswordAuthentication.java index 5dc97c858f3..a7008781754 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/authentication/UserPasswordAuthentication.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/authentication/UserPasswordAuthentication.java @@ -20,7 +20,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Properties; - import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl; import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.auth2.BasicCallHeaderAuthenticator; @@ -42,20 +41,28 @@ private String getCredentials(String key) { @Override public CallHeaderAuthenticator authenticate() { return new GeneratedBearerTokenAuthenticator( - new BasicCallHeaderAuthenticator((username, password) -> { - if (validCredentials.containsKey(username) && getCredentials(username).equals(password)) { - return () -> username; - } - throw CallStatus.UNAUTHENTICATED.withDescription("Invalid credentials.").toRuntimeException(); - })); + new BasicCallHeaderAuthenticator( + (username, password) -> { + if (validCredentials.containsKey(username) + && getCredentials(username).equals(password)) { + return () -> username; + } + throw CallStatus.UNAUTHENTICATED + .withDescription("Invalid credentials.") + .toRuntimeException(); + })); } @Override public void populateProperties(Properties properties) { - validCredentials.forEach((key, value) -> { - properties.put(ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.USER.camelName(), key); - properties.put(ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.PASSWORD.camelName(), value); - }); + validCredentials.forEach( + (key, value) -> { + properties.put( + ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.USER.camelName(), key); + properties.put( + ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.PASSWORD.camelName(), + value); + }); } public static class Builder { diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerBuilderTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerBuilderTest.java index 6565a85ddf9..5e6470de12a 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerBuilderTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerBuilderTest.java @@ -32,13 +32,11 @@ import org.junit.ClassRule; import org.junit.Test; -/** - * Test the behavior of ArrowFlightSqlClientHandler.Builder - */ +/** Test the behavior of ArrowFlightSqlClientHandler.Builder */ public class ArrowFlightSqlClientHandlerBuilderTest { @ClassRule - public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE = FlightServerTestRule - .createStandardTestRule(CoreMockedSqlProducers.getLegacyProducer()); + public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE = + FlightServerTestRule.createStandardTestRule(CoreMockedSqlProducers.getLegacyProducer()); private static BufferAllocator allocator; @@ -55,19 +53,21 @@ public static void tearDown() { @Test public void testRetainCookiesOnAuthOff() throws Exception { // Arrange - final ArrowFlightSqlClientHandler.Builder rootBuilder = new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) - .withBufferAllocator(allocator) - .withUsername(FlightServerTestRule.DEFAULT_USER) - .withPassword(FlightServerTestRule.DEFAULT_PASSWORD) - .withEncryption(false) - .withRetainCookies(true) - .withRetainAuth(false); + final ArrowFlightSqlClientHandler.Builder rootBuilder = + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) + .withBufferAllocator(allocator) + .withUsername(FlightServerTestRule.DEFAULT_USER) + .withPassword(FlightServerTestRule.DEFAULT_PASSWORD) + .withEncryption(false) + .withRetainCookies(true) + .withRetainAuth(false); try (ArrowFlightSqlClientHandler rootHandler = rootBuilder.build()) { // Act - final ArrowFlightSqlClientHandler.Builder testBuilder = new ArrowFlightSqlClientHandler.Builder(rootBuilder); + final ArrowFlightSqlClientHandler.Builder testBuilder = + new ArrowFlightSqlClientHandler.Builder(rootBuilder); // Assert assertSame(rootBuilder.cookieFactory, testBuilder.cookieFactory); @@ -78,19 +78,21 @@ public void testRetainCookiesOnAuthOff() throws Exception { @Test public void testRetainCookiesOffAuthOff() throws Exception { // Arrange - final ArrowFlightSqlClientHandler.Builder rootBuilder = new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) - .withBufferAllocator(allocator) - .withUsername(FlightServerTestRule.DEFAULT_USER) - .withPassword(FlightServerTestRule.DEFAULT_PASSWORD) - .withEncryption(false) - .withRetainCookies(false) - .withRetainAuth(false); + final ArrowFlightSqlClientHandler.Builder rootBuilder = + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) + .withBufferAllocator(allocator) + .withUsername(FlightServerTestRule.DEFAULT_USER) + .withPassword(FlightServerTestRule.DEFAULT_PASSWORD) + .withEncryption(false) + .withRetainCookies(false) + .withRetainAuth(false); try (ArrowFlightSqlClientHandler rootHandler = rootBuilder.build()) { // Act - final ArrowFlightSqlClientHandler.Builder testBuilder = new ArrowFlightSqlClientHandler.Builder(rootBuilder); + final ArrowFlightSqlClientHandler.Builder testBuilder = + new ArrowFlightSqlClientHandler.Builder(rootBuilder); // Assert assertNotSame(rootBuilder.cookieFactory, testBuilder.cookieFactory); @@ -101,19 +103,21 @@ public void testRetainCookiesOffAuthOff() throws Exception { @Test public void testRetainCookiesOnAuthOn() throws Exception { // Arrange - final ArrowFlightSqlClientHandler.Builder rootBuilder = new ArrowFlightSqlClientHandler.Builder() - .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) - .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) - .withBufferAllocator(allocator) - .withUsername(FlightServerTestRule.DEFAULT_USER) - .withPassword(FlightServerTestRule.DEFAULT_PASSWORD) - .withEncryption(false) - .withRetainCookies(true) - .withRetainAuth(true); + final ArrowFlightSqlClientHandler.Builder rootBuilder = + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_RULE.getHost()) + .withPort(FLIGHT_SERVER_TEST_RULE.getPort()) + .withBufferAllocator(allocator) + .withUsername(FlightServerTestRule.DEFAULT_USER) + .withPassword(FlightServerTestRule.DEFAULT_PASSWORD) + .withEncryption(false) + .withRetainCookies(true) + .withRetainAuth(true); try (ArrowFlightSqlClientHandler rootHandler = rootBuilder.build()) { // Act - final ArrowFlightSqlClientHandler.Builder testBuilder = new ArrowFlightSqlClientHandler.Builder(rootBuilder); + final ArrowFlightSqlClientHandler.Builder testBuilder = + new ArrowFlightSqlClientHandler.Builder(rootBuilder); // Assert assertSame(rootBuilder.cookieFactory, testBuilder.cookieFactory); diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtilsTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtilsTest.java index b7977462e9c..923dc2dd652 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtilsTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtilsTest.java @@ -30,7 +30,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; - import org.bouncycastle.openssl.jcajce.JcaPEMWriter; import org.junit.Assert; import org.junit.Test; @@ -42,8 +41,7 @@ @RunWith(MockitoJUnitRunner.class) public class ClientAuthenticationUtilsTest { - @Mock - KeyStore keyStoreMock; + @Mock KeyStore keyStoreMock; @Test public void testGetCertificatesInputStream() throws IOException, KeyStoreException { @@ -61,38 +59,36 @@ public void testGetCertificatesInputStream() throws IOException, KeyStoreExcepti } @Test - public void testGetKeyStoreInstance() throws IOException, - KeyStoreException, CertificateException, NoSuchAlgorithmException { + public void testGetKeyStoreInstance() + throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException { try (MockedStatic keyStoreMockedStatic = Mockito.mockStatic(KeyStore.class)) { keyStoreMockedStatic .when(() -> ClientAuthenticationUtils.getKeyStoreInstance(Mockito.any())) .thenReturn(keyStoreMock); KeyStore receiveKeyStore = ClientAuthenticationUtils.getKeyStoreInstance("test1"); - Mockito - .verify(keyStoreMock) - .load(null, null); + Mockito.verify(keyStoreMock).load(null, null); Assert.assertEquals(receiveKeyStore, keyStoreMock); } } @Test - public void testGetDefaultKeyStoreInstancePassword() throws IOException, - KeyStoreException, CertificateException, NoSuchAlgorithmException { + public void testGetDefaultKeyStoreInstancePassword() + throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException { try (MockedStatic keyStoreMockedStatic = Mockito.mockStatic(KeyStore.class)) { keyStoreMockedStatic - .when(() -> ClientAuthenticationUtils.getDefaultKeyStoreInstance("changeit")) - .thenReturn(keyStoreMock); + .when(() -> ClientAuthenticationUtils.getDefaultKeyStoreInstance("changeit")) + .thenReturn(keyStoreMock); KeyStore receiveKeyStore = ClientAuthenticationUtils.getDefaultKeyStoreInstance("changeit"); Assert.assertEquals(receiveKeyStore, keyStoreMock); } } @Test - public void testGetDefaultKeyStoreInstanceNoPassword() throws IOException, - KeyStoreException, CertificateException, NoSuchAlgorithmException { + public void testGetDefaultKeyStoreInstanceNoPassword() + throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException { try (MockedStatic keyStoreMockedStatic = Mockito.mockStatic(KeyStore.class)) { keyStoreMockedStatic @@ -103,44 +99,44 @@ public void testGetDefaultKeyStoreInstanceNoPassword() throws IOException, } } - @Test - public void testGetCertificateInputStreamFromMacSystem() throws IOException, - KeyStoreException, CertificateException, NoSuchAlgorithmException { + public void testGetCertificateInputStreamFromMacSystem() + throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException { InputStream mock = mock(InputStream.class); try (MockedStatic keyStoreMockedStatic = createKeyStoreStaticMock(); - MockedStatic - clientAuthenticationUtilsMockedStatic = createClientAuthenticationUtilsStaticMock()) { + MockedStatic clientAuthenticationUtilsMockedStatic = + createClientAuthenticationUtilsStaticMock()) { setOperatingSystemMock(clientAuthenticationUtilsMockedStatic, false, true); - keyStoreMockedStatic.when(() -> ClientAuthenticationUtils - .getKeyStoreInstance("KeychainStore")) + keyStoreMockedStatic + .when(() -> ClientAuthenticationUtils.getKeyStoreInstance("KeychainStore")) .thenReturn(keyStoreMock); - keyStoreMockedStatic.when(() -> ClientAuthenticationUtils - .getDefaultKeyStoreInstance("changeit")) + keyStoreMockedStatic + .when(() -> ClientAuthenticationUtils.getDefaultKeyStoreInstance("changeit")) .thenReturn(keyStoreMock); clientAuthenticationUtilsMockedStatic .when(ClientAuthenticationUtils::getKeystoreInputStream) .thenCallRealMethod(); keyStoreMockedStatic.when(KeyStore::getDefaultType).thenCallRealMethod(); - keyStoreMockedStatic.when(() -> ClientAuthenticationUtils - .getCertificatesInputStream(Mockito.any())) + keyStoreMockedStatic + .when(() -> ClientAuthenticationUtils.getCertificatesInputStream(Mockito.any())) .thenReturn(mock); - InputStream inputStream = ClientAuthenticationUtils.getCertificateInputStreamFromSystem("changeit"); + InputStream inputStream = + ClientAuthenticationUtils.getCertificateInputStreamFromSystem("changeit"); Assert.assertEquals(inputStream, mock); } } @Test - public void testGetCertificateInputStreamFromWindowsSystem() throws IOException, - KeyStoreException, CertificateException, NoSuchAlgorithmException { + public void testGetCertificateInputStreamFromWindowsSystem() + throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException { InputStream mock = mock(InputStream.class); try (MockedStatic keyStoreMockedStatic = createKeyStoreStaticMock(); - MockedStatic - clientAuthenticationUtilsMockedStatic = createClientAuthenticationUtilsStaticMock()) { + MockedStatic clientAuthenticationUtilsMockedStatic = + createClientAuthenticationUtilsStaticMock()) { setOperatingSystemMock(clientAuthenticationUtilsMockedStatic, true, false); keyStoreMockedStatic @@ -153,67 +149,74 @@ public void testGetCertificateInputStreamFromWindowsSystem() throws IOException, .when(() -> ClientAuthenticationUtils.getCertificatesInputStream(Mockito.any())) .thenReturn(mock); - InputStream inputStream = ClientAuthenticationUtils.getCertificateInputStreamFromSystem("test"); + InputStream inputStream = + ClientAuthenticationUtils.getCertificateInputStreamFromSystem("test"); Assert.assertEquals(inputStream, mock); } } @Test - public void testGetCertificateInputStreamFromLinuxSystem() throws IOException, - KeyStoreException, CertificateException, NoSuchAlgorithmException { + public void testGetCertificateInputStreamFromLinuxSystem() + throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException { InputStream mock = mock(InputStream.class); - try ( - MockedStatic keyStoreMockedStatic = createKeyStoreStaticMock(); - MockedStatic - clientAuthenticationUtilsMockedStatic = createClientAuthenticationUtilsStaticMock()) { + try (MockedStatic keyStoreMockedStatic = createKeyStoreStaticMock(); + MockedStatic clientAuthenticationUtilsMockedStatic = + createClientAuthenticationUtilsStaticMock()) { setOperatingSystemMock(clientAuthenticationUtilsMockedStatic, false, false); - keyStoreMockedStatic.when(() -> ClientAuthenticationUtils - .getCertificatesInputStream(Mockito.any())) + keyStoreMockedStatic + .when(() -> ClientAuthenticationUtils.getCertificatesInputStream(Mockito.any())) .thenReturn(mock); - keyStoreMockedStatic.when(() -> ClientAuthenticationUtils - .getDefaultKeyStoreInstance(Mockito.any())) + keyStoreMockedStatic + .when(() -> ClientAuthenticationUtils.getDefaultKeyStoreInstance(Mockito.any())) .thenReturn(keyStoreMock); clientAuthenticationUtilsMockedStatic .when(ClientAuthenticationUtils::getKeystoreInputStream) .thenCallRealMethod(); keyStoreMockedStatic.when(KeyStore::getDefaultType).thenCallRealMethod(); - InputStream inputStream = ClientAuthenticationUtils.getCertificateInputStreamFromSystem("changeit"); + InputStream inputStream = + ClientAuthenticationUtils.getCertificateInputStreamFromSystem("changeit"); Assert.assertEquals(inputStream, mock); inputStream = ClientAuthenticationUtils.getCertificateInputStreamFromSystem(null); Assert.assertEquals(inputStream, mock); } } - private MockedStatic createKeyStoreStaticMock() { - return Mockito.mockStatic(KeyStore.class, invocationOnMock -> { + return Mockito.mockStatic( + KeyStore.class, + invocationOnMock -> { Method method = invocationOnMock.getMethod(); if (method.getName().equals("getInstance")) { return invocationOnMock.callRealMethod(); } return method.invoke(invocationOnMock.getMock(), invocationOnMock.getArguments()); - } - ); + }); } private MockedStatic createClientAuthenticationUtilsStaticMock() { - return Mockito.mockStatic(ClientAuthenticationUtils.class , invocationOnMock -> { - Method method = invocationOnMock.getMethod(); - if (method.getName().equals("getCertificateInputStreamFromSystem")) { - return invocationOnMock.callRealMethod(); - } - return method.invoke(invocationOnMock.getMock(), invocationOnMock.getArguments()); - }); + return Mockito.mockStatic( + ClientAuthenticationUtils.class, + invocationOnMock -> { + Method method = invocationOnMock.getMethod(); + if (method.getName().equals("getCertificateInputStreamFromSystem")) { + return invocationOnMock.callRealMethod(); + } + return method.invoke(invocationOnMock.getMock(), invocationOnMock.getArguments()); + }); } - private void setOperatingSystemMock(MockedStatic clientAuthenticationUtilsMockedStatic, - boolean isWindows, boolean isMac) { + private void setOperatingSystemMock( + MockedStatic clientAuthenticationUtilsMockedStatic, + boolean isWindows, + boolean isMac) { clientAuthenticationUtilsMockedStatic.when(ClientAuthenticationUtils::isMac).thenReturn(isMac); Assert.assertEquals(ClientAuthenticationUtils.isMac(), isMac); - clientAuthenticationUtilsMockedStatic.when(ClientAuthenticationUtils::isWindows).thenReturn(isWindows); + clientAuthenticationUtilsMockedStatic + .when(ClientAuthenticationUtils::isWindows) + .thenReturn(isWindows); Assert.assertEquals(ClientAuthenticationUtils.isWindows(), isWindows); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/AccessorTestUtils.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/AccessorTestUtils.java index bc1e8a04203..08f37e4a754 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/AccessorTestUtils.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/AccessorTestUtils.java @@ -26,7 +26,6 @@ import java.util.function.Function; import java.util.function.IntSupplier; import java.util.function.Supplier; - import org.apache.arrow.driver.jdbc.accessor.ArrowFlightJdbcAccessor; import org.apache.arrow.vector.ValueVector; import org.hamcrest.Matcher; @@ -106,35 +105,43 @@ public List toList(ValueVector vector) throws Exception { return result; } - public void assertAccessorGetter(ValueVector vector, CheckedFunction getter, - MatcherGetter matcherGetter) throws Exception { - iterate(vector, (accessor, currentRow) -> { - R object = getter.apply(accessor); - boolean wasNull = accessor.wasNull(); - - collector.checkThat(object, matcherGetter.get(accessor, currentRow)); - collector.checkThat(wasNull, is(accessor.getObject() == null)); - }); + public void assertAccessorGetter( + ValueVector vector, CheckedFunction getter, MatcherGetter matcherGetter) + throws Exception { + iterate( + vector, + (accessor, currentRow) -> { + R object = getter.apply(accessor); + boolean wasNull = accessor.wasNull(); + + collector.checkThat(object, matcherGetter.get(accessor, currentRow)); + collector.checkThat(wasNull, is(accessor.getObject() == null)); + }); } - public void assertAccessorGetterThrowingException(ValueVector vector, CheckedFunction getter) - throws Exception { - iterate(vector, (accessor, currentRow) -> - ThrowableAssertionUtils.simpleAssertThrowableClass(SQLException.class, () -> getter.apply(accessor))); + public void assertAccessorGetterThrowingException( + ValueVector vector, CheckedFunction getter) throws Exception { + iterate( + vector, + (accessor, currentRow) -> + ThrowableAssertionUtils.simpleAssertThrowableClass( + SQLException.class, () -> getter.apply(accessor))); } - public void assertAccessorGetter(ValueVector vector, CheckedFunction getter, - Function> matcherGetter) throws Exception { + public void assertAccessorGetter( + ValueVector vector, CheckedFunction getter, Function> matcherGetter) + throws Exception { assertAccessorGetter(vector, getter, (accessor, currentRow) -> matcherGetter.apply(accessor)); } - public void assertAccessorGetter(ValueVector vector, CheckedFunction getter, - Supplier> matcherGetter) throws Exception { + public void assertAccessorGetter( + ValueVector vector, CheckedFunction getter, Supplier> matcherGetter) + throws Exception { assertAccessorGetter(vector, getter, (accessor, currentRow) -> matcherGetter.get()); } - public void assertAccessorGetter(ValueVector vector, CheckedFunction getter, - Matcher matcher) throws Exception { + public void assertAccessorGetter( + ValueVector vector, CheckedFunction getter, Matcher matcher) throws Exception { assertAccessorGetter(vector, getter, (accessor, currentRow) -> matcher); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImplTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImplTest.java index 4fb07428af4..467a12baa1c 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImplTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImplTest.java @@ -31,7 +31,6 @@ import java.util.Properties; import java.util.Random; import java.util.function.Function; - import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty; import org.junit.Before; import org.junit.Rule; @@ -50,11 +49,9 @@ public final class ArrowFlightConnectionConfigImplTest { private final Properties properties = new Properties(); private ArrowFlightConnectionConfigImpl arrowFlightConnectionConfig; - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); - @Parameter - public ArrowFlightConnectionProperty property; + @Parameter public ArrowFlightConnectionProperty property; @Parameter(value = 1) public Object value; @@ -70,27 +67,47 @@ public void setUp() { @Test public void testGetProperty() { - collector.checkThat(arrowFlightConnectionConfigFunction.apply(arrowFlightConnectionConfig), - is(value)); + collector.checkThat( + arrowFlightConnectionConfigFunction.apply(arrowFlightConnectionConfig), is(value)); } @Parameters(name = "<{0}> as <{1}>") public static List provideParameters() { - return asList(new Object[][] { - {HOST, "host", - (Function) ArrowFlightConnectionConfigImpl::getHost}, - {PORT, + return asList( + new Object[][] { + { + HOST, + "host", + (Function) ArrowFlightConnectionConfigImpl::getHost + }, + { + PORT, RANDOM.nextInt(Short.toUnsignedInt(Short.MAX_VALUE)), - (Function) ArrowFlightConnectionConfigImpl::getPort}, - {USER, "user", - (Function) ArrowFlightConnectionConfigImpl::getUser}, - {PASSWORD, "password", - (Function) ArrowFlightConnectionConfigImpl::getPassword}, - {USE_ENCRYPTION, RANDOM.nextBoolean(), - (Function) ArrowFlightConnectionConfigImpl::useEncryption}, - {THREAD_POOL_SIZE, + (Function) ArrowFlightConnectionConfigImpl::getPort + }, + { + USER, + "user", + (Function) ArrowFlightConnectionConfigImpl::getUser + }, + { + PASSWORD, + "password", + (Function) + ArrowFlightConnectionConfigImpl::getPassword + }, + { + USE_ENCRYPTION, + RANDOM.nextBoolean(), + (Function) + ArrowFlightConnectionConfigImpl::useEncryption + }, + { + THREAD_POOL_SIZE, RANDOM.nextInt(getRuntime().availableProcessors()), - (Function) ArrowFlightConnectionConfigImpl::threadPoolSize}, - }); + (Function) + ArrowFlightConnectionConfigImpl::threadPoolSize + }, + }); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionPropertyTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionPropertyTest.java index 25a48612cbd..dc302f26169 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionPropertyTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionPropertyTest.java @@ -23,7 +23,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Properties; - import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty; import org.junit.After; import org.junit.Assert; @@ -39,13 +38,11 @@ @RunWith(Parameterized.class) public final class ArrowFlightConnectionPropertyTest { - @Mock - public Properties properties; + @Mock public Properties properties; private AutoCloseable mockitoResource; - @Parameter - public ArrowFlightConnectionProperty arrowFlightConnectionProperty; + @Parameter public ArrowFlightConnectionProperty arrowFlightConnectionProperty; @Before public void setUp() { @@ -59,21 +56,22 @@ public void tearDown() throws Exception { @Test public void testWrapIsUnsupported() { - ThrowableAssertionUtils.simpleAssertThrowableClass(UnsupportedOperationException.class, - () -> arrowFlightConnectionProperty.wrap(properties)); + ThrowableAssertionUtils.simpleAssertThrowableClass( + UnsupportedOperationException.class, () -> arrowFlightConnectionProperty.wrap(properties)); } @Test public void testRequiredPropertyThrows() { Assume.assumeTrue(arrowFlightConnectionProperty.required()); - ThrowableAssertionUtils.simpleAssertThrowableClass(IllegalStateException.class, - () -> arrowFlightConnectionProperty.get(new Properties())); + ThrowableAssertionUtils.simpleAssertThrowableClass( + IllegalStateException.class, () -> arrowFlightConnectionProperty.get(new Properties())); } @Test public void testOptionalPropertyReturnsDefault() { Assume.assumeTrue(!arrowFlightConnectionProperty.required()); - Assert.assertEquals(arrowFlightConnectionProperty.defaultValue(), + Assert.assertEquals( + arrowFlightConnectionProperty.defaultValue(), arrowFlightConnectionProperty.get(new Properties())); } @@ -82,7 +80,8 @@ public static List provideParameters() { final ArrowFlightConnectionProperty[] arrowFlightConnectionProperties = ArrowFlightConnectionProperty.values(); final List parameters = new ArrayList<>(arrowFlightConnectionProperties.length); - for (final ArrowFlightConnectionProperty arrowFlightConnectionProperty : arrowFlightConnectionProperties) { + for (final ArrowFlightConnectionProperty arrowFlightConnectionProperty : + arrowFlightConnectionProperties) { parameters.add(new Object[] {arrowFlightConnectionProperty}); } return parameters; diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ConnectionWrapperTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ConnectionWrapperTest.java index 6044f3a363c..ebbc42cbaff 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ConnectionWrapperTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ConnectionWrapperTest.java @@ -35,7 +35,6 @@ import java.sql.Statement; import java.util.Arrays; import java.util.Random; - import org.apache.arrow.driver.jdbc.ArrowFlightConnection; import org.apache.arrow.util.AutoCloseables; import org.apache.calcite.avatica.AvaticaConnection; @@ -66,11 +65,9 @@ public final class ConnectionWrapperTest { private static final Random RANDOM = new Random(Long.MAX_VALUE); private static final int TIMEOUT = RANDOM.nextInt(Integer.MAX_VALUE); - @Mock - public AvaticaConnection underlyingConnection; + @Mock public AvaticaConnection underlyingConnection; private ConnectionWrapper connectionWrapper; - @Rule - public final ErrorCollector collector = new ErrorCollector(); + @Rule public final ErrorCollector collector = new ErrorCollector(); @Before public void setUp() { @@ -93,10 +90,10 @@ public void testUnwrappingUnderlyingConnectionShouldReturnUnderlyingConnection() collector.checkThat( collector.checkSucceeds(() -> connectionWrapper.unwrap(AvaticaConnection.class)), is(sameInstance(underlyingConnection))); - ThrowableAssertionUtils.simpleAssertThrowableClass(ClassCastException.class, - () -> connectionWrapper.unwrap(ArrowFlightConnection.class)); - ThrowableAssertionUtils.simpleAssertThrowableClass(ClassCastException.class, - () -> connectionWrapper.unwrap(ConnectionWrapper.class)); + ThrowableAssertionUtils.simpleAssertThrowableClass( + ClassCastException.class, () -> connectionWrapper.unwrap(ArrowFlightConnection.class)); + ThrowableAssertionUtils.simpleAssertThrowableClass( + ClassCastException.class, () -> connectionWrapper.unwrap(ConnectionWrapper.class)); } @Test @@ -106,14 +103,16 @@ public void testCreateStatementShouldCreateStatementFromUnderlyingConnection() connectionWrapper.createStatement(), is(sameInstance(verify(underlyingConnection, times(1)).createStatement()))); collector.checkThat( - connectionWrapper.createStatement(RESULT_SET_TYPE, RESULT_SET_CONCURRENCY, - RESULT_SET_HOLDABILITY), - is(verify(underlyingConnection, times(1)) - .createStatement(RESULT_SET_TYPE, RESULT_SET_CONCURRENCY, RESULT_SET_HOLDABILITY))); + connectionWrapper.createStatement( + RESULT_SET_TYPE, RESULT_SET_CONCURRENCY, RESULT_SET_HOLDABILITY), + is( + verify(underlyingConnection, times(1)) + .createStatement(RESULT_SET_TYPE, RESULT_SET_CONCURRENCY, RESULT_SET_HOLDABILITY))); collector.checkThat( connectionWrapper.createStatement(RESULT_SET_TYPE, RESULT_SET_CONCURRENCY), - is(verify(underlyingConnection, times(1)) - .createStatement(RESULT_SET_TYPE, RESULT_SET_CONCURRENCY))); + is( + verify(underlyingConnection, times(1)) + .createStatement(RESULT_SET_TYPE, RESULT_SET_CONCURRENCY))); } @Test @@ -121,49 +120,62 @@ public void testPrepareStatementShouldPrepareStatementFromUnderlyingConnection() throws SQLException { collector.checkThat( connectionWrapper.prepareStatement(PLACEHOLDER_QUERY), - is(sameInstance( - verify(underlyingConnection, times(1)).prepareStatement(PLACEHOLDER_QUERY)))); + is( + sameInstance( + verify(underlyingConnection, times(1)).prepareStatement(PLACEHOLDER_QUERY)))); collector.checkThat( connectionWrapper.prepareStatement(PLACEHOLDER_QUERY, COLUMN_INDICES), - is(allOf(sameInstance(verify(underlyingConnection, times(1)) - .prepareStatement(PLACEHOLDER_QUERY, COLUMN_INDICES)), - nullValue()))); + is( + allOf( + sameInstance( + verify(underlyingConnection, times(1)) + .prepareStatement(PLACEHOLDER_QUERY, COLUMN_INDICES)), + nullValue()))); collector.checkThat( connectionWrapper.prepareStatement(PLACEHOLDER_QUERY, COLUMN_NAMES), - is(allOf(sameInstance(verify(underlyingConnection, times(1)) - .prepareStatement(PLACEHOLDER_QUERY, COLUMN_NAMES)), - nullValue()))); - collector.checkThat( - connectionWrapper.prepareStatement(PLACEHOLDER_QUERY, RESULT_SET_TYPE, - RESULT_SET_CONCURRENCY), - is(allOf(sameInstance(verify(underlyingConnection, times(1)) - .prepareStatement(PLACEHOLDER_QUERY, RESULT_SET_TYPE, RESULT_SET_CONCURRENCY)), - nullValue()))); + is( + allOf( + sameInstance( + verify(underlyingConnection, times(1)) + .prepareStatement(PLACEHOLDER_QUERY, COLUMN_NAMES)), + nullValue()))); + collector.checkThat( + connectionWrapper.prepareStatement( + PLACEHOLDER_QUERY, RESULT_SET_TYPE, RESULT_SET_CONCURRENCY), + is( + allOf( + sameInstance( + verify(underlyingConnection, times(1)) + .prepareStatement( + PLACEHOLDER_QUERY, RESULT_SET_TYPE, RESULT_SET_CONCURRENCY)), + nullValue()))); collector.checkThat( connectionWrapper.prepareStatement(PLACEHOLDER_QUERY, GENERATED_KEYS), - is(allOf(sameInstance(verify(underlyingConnection, times(1)) - .prepareStatement(PLACEHOLDER_QUERY, GENERATED_KEYS)), - nullValue()))); + is( + allOf( + sameInstance( + verify(underlyingConnection, times(1)) + .prepareStatement(PLACEHOLDER_QUERY, GENERATED_KEYS)), + nullValue()))); } @Test public void testPrepareCallShouldPrepareCallFromUnderlyingConnection() throws SQLException { collector.checkThat( connectionWrapper.prepareCall(PLACEHOLDER_QUERY), - is(sameInstance( - verify(underlyingConnection, times(1)).prepareCall(PLACEHOLDER_QUERY)))); + is(sameInstance(verify(underlyingConnection, times(1)).prepareCall(PLACEHOLDER_QUERY)))); collector.checkThat( connectionWrapper.prepareCall(PLACEHOLDER_QUERY, RESULT_SET_TYPE, RESULT_SET_CONCURRENCY), - is(verify(underlyingConnection, times(1)) - .prepareCall(PLACEHOLDER_QUERY, RESULT_SET_TYPE, RESULT_SET_CONCURRENCY))); + is( + verify(underlyingConnection, times(1)) + .prepareCall(PLACEHOLDER_QUERY, RESULT_SET_TYPE, RESULT_SET_CONCURRENCY))); } @Test public void testNativeSqlShouldGetNativeSqlFromUnderlyingConnection() throws SQLException { collector.checkThat( connectionWrapper.nativeSQL(PLACEHOLDER_QUERY), - is(sameInstance( - verify(underlyingConnection, times(1)).nativeSQL(PLACEHOLDER_QUERY)))); + is(sameInstance(verify(underlyingConnection, times(1)).nativeSQL(PLACEHOLDER_QUERY)))); } @Test @@ -221,8 +233,8 @@ public void testSetReadOnlyShouldSetUnderlyingConnectionAsReadOnly() throws SQLE @Test public void testSetIsReadOnlyShouldGetStatusFromUnderlyingConnection() throws SQLException { - collector.checkThat(connectionWrapper.isReadOnly(), - is(verify(underlyingConnection).isReadOnly())); + collector.checkThat( + connectionWrapper.isReadOnly(), is(verify(underlyingConnection).isReadOnly())); } @Test @@ -257,9 +269,7 @@ public void getTransactionIsolationShouldGetUnderlyingConnectionIsolation() thro public void getWarningShouldGetWarningsFromUnderlyingConnection() throws SQLException { collector.checkThat( connectionWrapper.getWarnings(), - is(allOf( - sameInstance(verify(underlyingConnection, times(1)).getWarnings()), - nullValue()))); + is(allOf(sameInstance(verify(underlyingConnection, times(1)).getWarnings()), nullValue()))); } @Test @@ -272,8 +282,7 @@ public void testClearWarningShouldClearWarningsFromUnderlyingConnection() throws public void getTypeMapShouldGetTypeMapFromUnderlyingConnection() throws SQLException { when(underlyingConnection.getTypeMap()).thenReturn(null); collector.checkThat( - connectionWrapper.getTypeMap(), - is(verify(underlyingConnection, times(1)).getTypeMap())); + connectionWrapper.getTypeMap(), is(verify(underlyingConnection, times(1)).getTypeMap())); } @Test @@ -299,13 +308,12 @@ public void testGetHoldabilityShouldGetHoldabilityFromUnderlyingConnection() thr public void testSetSavepointShouldSetSavepointInUnderlyingConnection() throws SQLException { collector.checkThat( connectionWrapper.setSavepoint(), - is(allOf( - sameInstance(verify(underlyingConnection, times(1)).setSavepoint()), - nullValue()))); + is( + allOf( + sameInstance(verify(underlyingConnection, times(1)).setSavepoint()), nullValue()))); collector.checkThat( connectionWrapper.setSavepoint(SAVEPOINT_NAME), - is(sameInstance( - verify(underlyingConnection, times(1)).setSavepoint(SAVEPOINT_NAME)))); + is(sameInstance(verify(underlyingConnection, times(1)).setSavepoint(SAVEPOINT_NAME)))); } @Test @@ -325,32 +333,30 @@ public void testReleaseSavepointShouldReleaseSavepointFromUnderlyingConnection() public void testCreateClobShouldCreateClobFromUnderlyingConnection() throws SQLException { collector.checkThat( connectionWrapper.createClob(), - is(allOf(sameInstance( - verify(underlyingConnection, times(1)).createClob()), nullValue()))); + is(allOf(sameInstance(verify(underlyingConnection, times(1)).createClob()), nullValue()))); } @Test public void testCreateBlobShouldCreateBlobFromUnderlyingConnection() throws SQLException { collector.checkThat( connectionWrapper.createBlob(), - is(allOf(sameInstance( - verify(underlyingConnection, times(1)).createBlob()), nullValue()))); + is(allOf(sameInstance(verify(underlyingConnection, times(1)).createBlob()), nullValue()))); } @Test public void testCreateNClobShouldCreateNClobFromUnderlyingConnection() throws SQLException { collector.checkThat( connectionWrapper.createNClob(), - is(allOf(sameInstance( - verify(underlyingConnection, times(1)).createNClob()), nullValue()))); + is(allOf(sameInstance(verify(underlyingConnection, times(1)).createNClob()), nullValue()))); } @Test public void testCreateSQLXMLShouldCreateSQLXMLFromUnderlyingConnection() throws SQLException { collector.checkThat( connectionWrapper.createSQLXML(), - is(allOf(sameInstance( - verify(underlyingConnection, times(1)).createSQLXML()), nullValue()))); + is( + allOf( + sameInstance(verify(underlyingConnection, times(1)).createSQLXML()), nullValue()))); } @Test @@ -371,16 +377,16 @@ public void testSetClientInfoShouldSetClientInfoInUnderlyingConnection() public void testGetClientInfoShouldGetClientInfoFromUnderlyingConnection() throws SQLException { collector.checkThat( connectionWrapper.getClientInfo(CLIENT_INFO), - is(allOf( - sameInstance( - verify(underlyingConnection, times(1)).getClientInfo(CLIENT_INFO)), - nullValue()))); + is( + allOf( + sameInstance(verify(underlyingConnection, times(1)).getClientInfo(CLIENT_INFO)), + nullValue()))); collector.checkThat( connectionWrapper.getClientInfo(), - is(allOf( - sameInstance( - verify(underlyingConnection, times(1)).getClientInfo()), - nullValue()))); + is( + allOf( + sameInstance(verify(underlyingConnection, times(1)).getClientInfo()), + nullValue()))); } @Test @@ -388,10 +394,11 @@ public void testCreateArrayOfShouldCreateArrayFromUnderlyingConnection() throws final Object[] elements = range(0, 100).boxed().toArray(); collector.checkThat( connectionWrapper.createArrayOf(TYPE_NAME, elements), - is(allOf( - sameInstance( - verify(underlyingConnection, times(1)).createArrayOf(TYPE_NAME, elements)), - nullValue()))); + is( + allOf( + sameInstance( + verify(underlyingConnection, times(1)).createArrayOf(TYPE_NAME, elements)), + nullValue()))); } @Test @@ -399,10 +406,11 @@ public void testCreateStructShouldCreateStructFromUnderlyingConnection() throws final Object[] attributes = range(0, 120).boxed().toArray(); collector.checkThat( connectionWrapper.createStruct(TYPE_NAME, attributes), - is(allOf( - sameInstance( - verify(underlyingConnection, times(1)).createStruct(TYPE_NAME, attributes)), - nullValue()))); + is( + allOf( + sameInstance( + verify(underlyingConnection, times(1)).createStruct(TYPE_NAME, attributes)), + nullValue()))); } @Test @@ -415,9 +423,7 @@ public void testSetSchemaShouldSetSchemaInUnderlyingConnection() throws SQLExcep public void testGetSchemaShouldGetSchemaFromUnderlyingConnection() throws SQLException { collector.checkThat( connectionWrapper.getSchema(), - is(allOf( - sameInstance(verify(underlyingConnection, times(1)).getSchema()), - nullValue()))); + is(allOf(sameInstance(verify(underlyingConnection, times(1)).getSchema()), nullValue()))); } @Test diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ConvertUtilsTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ConvertUtilsTest.java index 5cea3749283..2d7cc09392d 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ConvertUtilsTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ConvertUtilsTest.java @@ -19,8 +19,8 @@ import static org.hamcrest.CoreMatchers.equalTo; +import com.google.common.collect.ImmutableList; import java.util.List; - import org.apache.arrow.flight.sql.FlightSqlColumnMetadata; import org.apache.arrow.vector.types.pojo.ArrowType; import org.apache.arrow.vector.types.pojo.Field; @@ -31,28 +31,26 @@ import org.junit.Test; import org.junit.rules.ErrorCollector; -import com.google.common.collect.ImmutableList; - public class ConvertUtilsTest { - @Rule - public ErrorCollector collector = new ErrorCollector(); + @Rule public ErrorCollector collector = new ErrorCollector(); @Test public void testShouldSetOnColumnMetaDataBuilder() { final Common.ColumnMetaData.Builder builder = Common.ColumnMetaData.newBuilder(); - final FlightSqlColumnMetadata expectedColumnMetaData = new FlightSqlColumnMetadata.Builder() - .catalogName("catalog1") - .schemaName("schema1") - .tableName("table1") - .isAutoIncrement(true) - .isCaseSensitive(true) - .isReadOnly(true) - .isSearchable(true) - .precision(20) - .scale(10) - .build(); + final FlightSqlColumnMetadata expectedColumnMetaData = + new FlightSqlColumnMetadata.Builder() + .catalogName("catalog1") + .schemaName("schema1") + .tableName("table1") + .isAutoIncrement(true) + .isCaseSensitive(true) + .isReadOnly(true) + .isSearchable(true) + .precision(20) + .scale(10) + .build(); ConvertUtils.setOnColumnMetaDataBuilder(builder, expectedColumnMetaData.getMetadataMap()); assertBuilder(builder, expectedColumnMetaData); } @@ -60,57 +58,77 @@ public void testShouldSetOnColumnMetaDataBuilder() { @Test public void testShouldConvertArrowFieldsToColumnMetaDataList() { - final List listField = ImmutableList.of( - new Field("col1", - new FieldType(true, ArrowType.Utf8.INSTANCE, null, - new FlightSqlColumnMetadata.Builder() - .catalogName("catalog1") - .schemaName("schema1") - .tableName("table1") - .build().getMetadataMap() - ), null)); - - final List expectedColumnMetaData = ImmutableList.of( - ColumnMetaData.fromProto( - Common.ColumnMetaData.newBuilder() - .setCatalogName("catalog1") - .setSchemaName("schema1") - .setTableName("table1") - .build())); - - final List actualColumnMetaData = ConvertUtils.convertArrowFieldsToColumnMetaDataList(listField); + final List listField = + ImmutableList.of( + new Field( + "col1", + new FieldType( + true, + ArrowType.Utf8.INSTANCE, + null, + new FlightSqlColumnMetadata.Builder() + .catalogName("catalog1") + .schemaName("schema1") + .tableName("table1") + .build() + .getMetadataMap()), + null)); + + final List expectedColumnMetaData = + ImmutableList.of( + ColumnMetaData.fromProto( + Common.ColumnMetaData.newBuilder() + .setCatalogName("catalog1") + .setSchemaName("schema1") + .setTableName("table1") + .build())); + + final List actualColumnMetaData = + ConvertUtils.convertArrowFieldsToColumnMetaDataList(listField); assertColumnMetaData(expectedColumnMetaData, actualColumnMetaData); } - private void assertColumnMetaData(final List expected, final List actual) { + private void assertColumnMetaData( + final List expected, final List actual) { collector.checkThat(expected.size(), equalTo(actual.size())); int size = expected.size(); for (int i = 0; i < size; i++) { final ColumnMetaData expectedColumnMetaData = expected.get(i); final ColumnMetaData actualColumnMetaData = actual.get(i); - collector.checkThat(expectedColumnMetaData.catalogName, equalTo(actualColumnMetaData.catalogName)); - collector.checkThat(expectedColumnMetaData.schemaName, equalTo(actualColumnMetaData.schemaName)); - collector.checkThat(expectedColumnMetaData.tableName, equalTo(actualColumnMetaData.tableName)); + collector.checkThat( + expectedColumnMetaData.catalogName, equalTo(actualColumnMetaData.catalogName)); + collector.checkThat( + expectedColumnMetaData.schemaName, equalTo(actualColumnMetaData.schemaName)); + collector.checkThat( + expectedColumnMetaData.tableName, equalTo(actualColumnMetaData.tableName)); collector.checkThat(expectedColumnMetaData.readOnly, equalTo(actualColumnMetaData.readOnly)); - collector.checkThat(expectedColumnMetaData.autoIncrement, equalTo(actualColumnMetaData.autoIncrement)); - collector.checkThat(expectedColumnMetaData.precision, equalTo(actualColumnMetaData.precision)); + collector.checkThat( + expectedColumnMetaData.autoIncrement, equalTo(actualColumnMetaData.autoIncrement)); + collector.checkThat( + expectedColumnMetaData.precision, equalTo(actualColumnMetaData.precision)); collector.checkThat(expectedColumnMetaData.scale, equalTo(actualColumnMetaData.scale)); - collector.checkThat(expectedColumnMetaData.caseSensitive, equalTo(actualColumnMetaData.caseSensitive)); - collector.checkThat(expectedColumnMetaData.searchable, equalTo(actualColumnMetaData.searchable)); + collector.checkThat( + expectedColumnMetaData.caseSensitive, equalTo(actualColumnMetaData.caseSensitive)); + collector.checkThat( + expectedColumnMetaData.searchable, equalTo(actualColumnMetaData.searchable)); } } - private void assertBuilder(final Common.ColumnMetaData.Builder builder, - final FlightSqlColumnMetadata flightSqlColumnMetaData) { + private void assertBuilder( + final Common.ColumnMetaData.Builder builder, + final FlightSqlColumnMetadata flightSqlColumnMetaData) { final Integer precision = flightSqlColumnMetaData.getPrecision(); final Integer scale = flightSqlColumnMetaData.getScale(); - collector.checkThat(flightSqlColumnMetaData.getCatalogName(), equalTo(builder.getCatalogName())); + collector.checkThat( + flightSqlColumnMetaData.getCatalogName(), equalTo(builder.getCatalogName())); collector.checkThat(flightSqlColumnMetaData.getSchemaName(), equalTo(builder.getSchemaName())); collector.checkThat(flightSqlColumnMetaData.getTableName(), equalTo(builder.getTableName())); - collector.checkThat(flightSqlColumnMetaData.isAutoIncrement(), equalTo(builder.getAutoIncrement())); - collector.checkThat(flightSqlColumnMetaData.isCaseSensitive(), equalTo(builder.getCaseSensitive())); + collector.checkThat( + flightSqlColumnMetaData.isAutoIncrement(), equalTo(builder.getAutoIncrement())); + collector.checkThat( + flightSqlColumnMetaData.isCaseSensitive(), equalTo(builder.getCaseSensitive())); collector.checkThat(flightSqlColumnMetaData.isSearchable(), equalTo(builder.getSearchable())); collector.checkThat(flightSqlColumnMetaData.isReadOnly(), equalTo(builder.getReadOnly())); collector.checkThat(precision == null ? 0 : precision, equalTo(builder.getPrecision())); diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/CoreMockedSqlProducers.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/CoreMockedSqlProducers.java index a8e2e7f2e4c..607d60c1f44 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/CoreMockedSqlProducers.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/CoreMockedSqlProducers.java @@ -21,6 +21,7 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; +import com.google.common.collect.ImmutableList; import java.sql.Date; import java.sql.ResultSet; import java.sql.SQLException; @@ -30,7 +31,6 @@ import java.util.List; import java.util.function.Consumer; import java.util.stream.IntStream; - import org.apache.arrow.flight.FlightProducer.ServerStreamListener; import org.apache.arrow.flight.sql.FlightSqlColumnMetadata; import org.apache.arrow.memory.BufferAllocator; @@ -53,11 +53,7 @@ import org.apache.arrow.vector.util.Text; import org.junit.rules.ErrorCollector; -import com.google.common.collect.ImmutableList; - -/** - * Standard {@link MockFlightSqlProducer} instances for tests. - */ +/** Standard {@link MockFlightSqlProducer} instances for tests. */ // TODO Remove this once all tests are refactor to use only the queries they need. public final class CoreMockedSqlProducers { @@ -86,210 +82,216 @@ public static MockFlightSqlProducer getLegacyProducer() { } private static void addQueryWithEmbeddedEmptyRoot(final MockFlightSqlProducer producer) { - final Schema querySchema = new Schema(ImmutableList.of( - new Field( - "ID", - new FieldType(true, new ArrowType.Int(64, true), - null), - null) - )); + final Schema querySchema = + new Schema( + ImmutableList.of( + new Field("ID", new FieldType(true, new ArrowType.Int(64, true), null), null))); final List> resultProducers = new ArrayList<>(); - Consumer dataRoot = listener -> { - try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - final VectorSchemaRoot root = VectorSchemaRoot.create(querySchema, allocator)) { - root.allocateNew(); - root.setRowCount(0); - listener.start(root); - listener.putNext(); // empty root - ((BigIntVector) root.getVector("ID")).setSafe(0, 100L); - root.setRowCount(1); - listener.putNext(); // data root - root.clear(); - root.setRowCount(0); - listener.putNext(); // empty root - ((BigIntVector) root.getVector("ID")).setSafe(0, 100L); - root.setRowCount(1); - listener.putNext(); // data root - } finally { - listener.completed(); - } - }; + Consumer dataRoot = + listener -> { + try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); + final VectorSchemaRoot root = VectorSchemaRoot.create(querySchema, allocator)) { + root.allocateNew(); + root.setRowCount(0); + listener.start(root); + listener.putNext(); // empty root + ((BigIntVector) root.getVector("ID")).setSafe(0, 100L); + root.setRowCount(1); + listener.putNext(); // data root + root.clear(); + root.setRowCount(0); + listener.putNext(); // empty root + ((BigIntVector) root.getVector("ID")).setSafe(0, 100L); + root.setRowCount(1); + listener.putNext(); // data root + } finally { + listener.completed(); + } + }; resultProducers.add(dataRoot); producer.addSelectQuery(LEGACY_REGULAR_WITH_EMPTY_SQL_CMD, querySchema, resultProducers); } private static void addLegacyRegularSqlCmdSupport(final MockFlightSqlProducer producer) { - final Schema querySchema = new Schema(ImmutableList.of( - new Field( - "ID", - new FieldType(true, new ArrowType.Int(64, true), - null), - null), - new Field( - "Name", - new FieldType(true, new ArrowType.Utf8(), null), - null), - new Field( - "Age", - new FieldType(true, new ArrowType.Int(32, false), - null), - null), - new Field( - "Salary", - new FieldType(true, new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE), - null), - null), - new Field( - "Hire Date", - new FieldType(true, new ArrowType.Date(DateUnit.DAY), null), - null), - new Field( - "Last Sale", - new FieldType(true, new ArrowType.Timestamp(TimeUnit.MILLISECOND, null), - null), - null) - )); + final Schema querySchema = + new Schema( + ImmutableList.of( + new Field("ID", new FieldType(true, new ArrowType.Int(64, true), null), null), + new Field("Name", new FieldType(true, new ArrowType.Utf8(), null), null), + new Field("Age", new FieldType(true, new ArrowType.Int(32, false), null), null), + new Field( + "Salary", + new FieldType( + true, new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE), null), + null), + new Field( + "Hire Date", new FieldType(true, new ArrowType.Date(DateUnit.DAY), null), null), + new Field( + "Last Sale", + new FieldType(true, new ArrowType.Timestamp(TimeUnit.MILLISECOND, null), null), + null))); final List> resultProducers = new ArrayList<>(); - IntStream.range(0, 10).forEach(page -> { - resultProducers.add(listener -> { - final int rowsPerPage = 5000; - try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - final VectorSchemaRoot root = VectorSchemaRoot.create(querySchema, allocator)) { - root.allocateNew(); - listener.start(root); - int batchSize = 500; - int indexOnBatch = 0; - int resultsOffset = page * rowsPerPage; - for (int i = 0; i < rowsPerPage; i++) { - ((BigIntVector) root.getVector("ID")) - .setSafe(indexOnBatch, (long) Integer.MAX_VALUE + 1 + i + resultsOffset); - ((VarCharVector) root.getVector("Name")) - .setSafe(indexOnBatch, new Text("Test Name #" + (resultsOffset + i))); - ((UInt4Vector) root.getVector("Age")) - .setSafe(indexOnBatch, (int) Short.MAX_VALUE + 1 + i + resultsOffset); - ((Float8Vector) root.getVector("Salary")) - .setSafe(indexOnBatch, - Math.scalb((double) (i + resultsOffset) / 2, i + resultsOffset)); - ((DateDayVector) root.getVector("Hire Date")) - .setSafe(indexOnBatch, i + resultsOffset); - ((TimeStampMilliVector) root.getVector("Last Sale")) - .setSafe(indexOnBatch, Long.MAX_VALUE - i - resultsOffset); - indexOnBatch++; - if (indexOnBatch == batchSize) { - root.setRowCount(indexOnBatch); - if (listener.isCancelled()) { - return; - } - listener.putNext(); - root.allocateNew(); - indexOnBatch = 0; - } - } - if (listener.isCancelled()) { - return; - } - root.setRowCount(indexOnBatch); - listener.putNext(); - } finally { - listener.completed(); - } - }); - }); + IntStream.range(0, 10) + .forEach( + page -> { + resultProducers.add( + listener -> { + final int rowsPerPage = 5000; + try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); + final VectorSchemaRoot root = + VectorSchemaRoot.create(querySchema, allocator)) { + root.allocateNew(); + listener.start(root); + int batchSize = 500; + int indexOnBatch = 0; + int resultsOffset = page * rowsPerPage; + for (int i = 0; i < rowsPerPage; i++) { + ((BigIntVector) root.getVector("ID")) + .setSafe( + indexOnBatch, (long) Integer.MAX_VALUE + 1 + i + resultsOffset); + ((VarCharVector) root.getVector("Name")) + .setSafe(indexOnBatch, new Text("Test Name #" + (resultsOffset + i))); + ((UInt4Vector) root.getVector("Age")) + .setSafe(indexOnBatch, (int) Short.MAX_VALUE + 1 + i + resultsOffset); + ((Float8Vector) root.getVector("Salary")) + .setSafe( + indexOnBatch, + Math.scalb((double) (i + resultsOffset) / 2, i + resultsOffset)); + ((DateDayVector) root.getVector("Hire Date")) + .setSafe(indexOnBatch, i + resultsOffset); + ((TimeStampMilliVector) root.getVector("Last Sale")) + .setSafe(indexOnBatch, Long.MAX_VALUE - i - resultsOffset); + indexOnBatch++; + if (indexOnBatch == batchSize) { + root.setRowCount(indexOnBatch); + if (listener.isCancelled()) { + return; + } + listener.putNext(); + root.allocateNew(); + indexOnBatch = 0; + } + } + if (listener.isCancelled()) { + return; + } + root.setRowCount(indexOnBatch); + listener.putNext(); + } finally { + listener.completed(); + } + }); + }); producer.addSelectQuery(LEGACY_REGULAR_SQL_CMD, querySchema, resultProducers); } private static void addLegacyMetadataSqlCmdSupport(final MockFlightSqlProducer producer) { - final Schema metadataSchema = new Schema(ImmutableList.of( - new Field( - "integer0", - new FieldType(true, new ArrowType.Int(64, true), - null, new FlightSqlColumnMetadata.Builder() - .catalogName("CATALOG_NAME_1") - .schemaName("SCHEMA_NAME_1") - .tableName("TABLE_NAME_1") - .typeName("TYPE_NAME_1") - .precision(10) - .scale(0) - .isAutoIncrement(true) - .isCaseSensitive(false) - .isReadOnly(true) - .isSearchable(true) - .build().getMetadataMap()), - null), - new Field( - "string1", - new FieldType(true, new ArrowType.Utf8(), - null, new FlightSqlColumnMetadata.Builder() - .catalogName("CATALOG_NAME_2") - .schemaName("SCHEMA_NAME_2") - .tableName("TABLE_NAME_2") - .typeName("TYPE_NAME_2") - .precision(65535) - .scale(0) - .isAutoIncrement(false) - .isCaseSensitive(true) - .isReadOnly(false) - .isSearchable(true) - .build().getMetadataMap()), - null), - new Field( - "float2", - new FieldType(true, new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE), - null, new FlightSqlColumnMetadata.Builder() - .catalogName("CATALOG_NAME_3") - .schemaName("SCHEMA_NAME_3") - .tableName("TABLE_NAME_3") - .typeName("TYPE_NAME_3") - .precision(15) - .scale(20) - .isAutoIncrement(false) - .isCaseSensitive(false) - .isReadOnly(false) - .isSearchable(true) - .build().getMetadataMap()), - null))); - final Consumer formula = listener -> { - try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - final VectorSchemaRoot root = VectorSchemaRoot.create(metadataSchema, allocator)) { - root.allocateNew(); - ((BigIntVector) root.getVector("integer0")).setSafe(0, 1); - ((VarCharVector) root.getVector("string1")).setSafe(0, new Text("teste")); - ((Float4Vector) root.getVector("float2")).setSafe(0, (float) 4.1); - root.setRowCount(1); - listener.start(root); - listener.putNext(); - } finally { - listener.completed(); - } - }; - producer.addSelectQuery(LEGACY_METADATA_SQL_CMD, metadataSchema, - Collections.singletonList(formula)); + final Schema metadataSchema = + new Schema( + ImmutableList.of( + new Field( + "integer0", + new FieldType( + true, + new ArrowType.Int(64, true), + null, + new FlightSqlColumnMetadata.Builder() + .catalogName("CATALOG_NAME_1") + .schemaName("SCHEMA_NAME_1") + .tableName("TABLE_NAME_1") + .typeName("TYPE_NAME_1") + .precision(10) + .scale(0) + .isAutoIncrement(true) + .isCaseSensitive(false) + .isReadOnly(true) + .isSearchable(true) + .build() + .getMetadataMap()), + null), + new Field( + "string1", + new FieldType( + true, + new ArrowType.Utf8(), + null, + new FlightSqlColumnMetadata.Builder() + .catalogName("CATALOG_NAME_2") + .schemaName("SCHEMA_NAME_2") + .tableName("TABLE_NAME_2") + .typeName("TYPE_NAME_2") + .precision(65535) + .scale(0) + .isAutoIncrement(false) + .isCaseSensitive(true) + .isReadOnly(false) + .isSearchable(true) + .build() + .getMetadataMap()), + null), + new Field( + "float2", + new FieldType( + true, + new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE), + null, + new FlightSqlColumnMetadata.Builder() + .catalogName("CATALOG_NAME_3") + .schemaName("SCHEMA_NAME_3") + .tableName("TABLE_NAME_3") + .typeName("TYPE_NAME_3") + .precision(15) + .scale(20) + .isAutoIncrement(false) + .isCaseSensitive(false) + .isReadOnly(false) + .isSearchable(true) + .build() + .getMetadataMap()), + null))); + final Consumer formula = + listener -> { + try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); + final VectorSchemaRoot root = VectorSchemaRoot.create(metadataSchema, allocator)) { + root.allocateNew(); + ((BigIntVector) root.getVector("integer0")).setSafe(0, 1); + ((VarCharVector) root.getVector("string1")).setSafe(0, new Text("teste")); + ((Float4Vector) root.getVector("float2")).setSafe(0, (float) 4.1); + root.setRowCount(1); + listener.start(root); + listener.putNext(); + } finally { + listener.completed(); + } + }; + producer.addSelectQuery( + LEGACY_METADATA_SQL_CMD, metadataSchema, Collections.singletonList(formula)); } private static void addLegacyCancellationSqlCmdSupport(final MockFlightSqlProducer producer) { producer.addSelectQuery( LEGACY_CANCELLATION_SQL_CMD, - new Schema(Collections.singletonList(new Field( - "integer0", - new FieldType(true, new ArrowType.Int(64, true), null), - null))), - Collections.singletonList(listener -> { - // Should keep hanging until canceled. - })); + new Schema( + Collections.singletonList( + new Field( + "integer0", new FieldType(true, new ArrowType.Int(64, true), null), null))), + Collections.singletonList( + listener -> { + // Should keep hanging until canceled. + })); } /** - * Asserts that the values in the provided {@link ResultSet} are expected for the - * legacy {@link MockFlightSqlProducer}. + * Asserts that the values in the provided {@link ResultSet} are expected for the legacy {@link + * MockFlightSqlProducer}. * * @param resultSet the result set. * @param collector the {@link ErrorCollector} to use. * @throws SQLException on error. */ - public static void assertLegacyRegularSqlResultSet(final ResultSet resultSet, - final ErrorCollector collector) - throws SQLException { + public static void assertLegacyRegularSqlResultSet( + final ResultSet resultSet, final ErrorCollector collector) throws SQLException { final int expectedRowCount = 50_000; final long[] expectedIds = new long[expectedRowCount]; diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/DateTimeUtilsTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/DateTimeUtilsTest.java index adb892fcdc7..58db9723990 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/DateTimeUtilsTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/DateTimeUtilsTest.java @@ -23,15 +23,13 @@ import java.time.Instant; import java.util.Calendar; import java.util.TimeZone; - import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.ErrorCollector; public class DateTimeUtilsTest { - @ClassRule - public static final ErrorCollector collector = new ErrorCollector(); + @ClassRule public static final ErrorCollector collector = new ErrorCollector(); private final TimeZone defaultTimezone = TimeZone.getTimeZone("UTC"); private final TimeZone alternateTimezone = TimeZone.getTimeZone("America/Vancouver"); private final long positiveEpochMilli = 959817600000L; // 2000-06-01 00:00:00 UTC @@ -48,7 +46,8 @@ public void testShouldGetOffsetWithSameTimeZone() { try { // Trying to guarantee timezone returns to its original value final long expected = epochMillis + offset; - final long actual = DateTimeUtils.applyCalendarOffset(epochMillis, Calendar.getInstance(defaultTimezone)); + final long actual = + DateTimeUtils.applyCalendarOffset(epochMillis, Calendar.getInstance(defaultTimezone)); collector.checkThat(actual, is(expected)); } finally { @@ -68,8 +67,8 @@ public void testShouldGetOffsetWithDifferentTimeZone() { try { // Trying to guarantee timezone returns to its original value final long expectedEpochMillis = epochMillis + offset; - final long actualEpochMillis = DateTimeUtils.applyCalendarOffset(epochMillis, Calendar.getInstance( - defaultTimezone)); + final long actualEpochMillis = + DateTimeUtils.applyCalendarOffset(epochMillis, Calendar.getInstance(defaultTimezone)); collector.checkThat(actualEpochMillis, is(expectedEpochMillis)); } finally { diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/FlightEndpointDataQueueTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/FlightEndpointDataQueueTest.java index 05325faa18e..d6e5726f219 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/FlightEndpointDataQueueTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/FlightEndpointDataQueueTest.java @@ -22,7 +22,6 @@ import static org.mockito.Mockito.mock; import java.util.concurrent.CompletionService; - import org.apache.arrow.driver.jdbc.client.CloseableEndpointStreamPair; import org.junit.Before; import org.junit.Rule; @@ -32,16 +31,12 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; -/** - * Tests for {@link FlightEndpointDataQueue}. - */ +/** Tests for {@link FlightEndpointDataQueue}. */ @RunWith(MockitoJUnitRunner.class) public class FlightEndpointDataQueueTest { - @Rule - public final ErrorCollector collector = new ErrorCollector(); - @Mock - private CompletionService mockedService; + @Rule public final ErrorCollector collector = new ErrorCollector(); + @Mock private CompletionService mockedService; private FlightEndpointDataQueue queue; @Before @@ -57,24 +52,27 @@ public void testNextShouldRetrieveNullIfEmpty() throws Exception { @Test public void testNextShouldThrowExceptionUponClose() throws Exception { queue.close(); - ThrowableAssertionUtils.simpleAssertThrowableClass(IllegalStateException.class, () -> queue.next()); + ThrowableAssertionUtils.simpleAssertThrowableClass( + IllegalStateException.class, () -> queue.next()); } @Test public void testEnqueueShouldThrowExceptionUponClose() throws Exception { queue.close(); - ThrowableAssertionUtils.simpleAssertThrowableClass(IllegalStateException.class, - () -> queue.enqueue(mock(CloseableEndpointStreamPair.class))); + ThrowableAssertionUtils.simpleAssertThrowableClass( + IllegalStateException.class, () -> queue.enqueue(mock(CloseableEndpointStreamPair.class))); } @Test public void testCheckOpen() throws Exception { - collector.checkSucceeds(() -> { - queue.checkOpen(); - return true; - }); + collector.checkSucceeds( + () -> { + queue.checkOpen(); + return true; + }); queue.close(); - ThrowableAssertionUtils.simpleAssertThrowableClass(IllegalStateException.class, () -> queue.checkOpen()); + ThrowableAssertionUtils.simpleAssertThrowableClass( + IllegalStateException.class, () -> queue.checkOpen()); } @Test diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/FlightSqlTestCertificates.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/FlightSqlTestCertificates.java index 89e27be9f53..744e87a8c4a 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/FlightSqlTestCertificates.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/FlightSqlTestCertificates.java @@ -24,9 +24,7 @@ import java.util.List; import java.util.Objects; -/** - * Utility class for unit tests that need to reference the certificate params. - */ +/** Utility class for unit tests that need to reference the certificate params. */ public class FlightSqlTestCertificates { public static final String TEST_DATA_ENV_VAR = "ARROW_TEST_DATA"; @@ -37,9 +35,12 @@ static Path getTestDataRoot() { if (path == null) { path = System.getProperty(TEST_DATA_PROPERTY); } - return Paths.get(Objects.requireNonNull(path, - String.format("Could not find test data path. Set the environment variable %s or the JVM property %s.", - TEST_DATA_ENV_VAR, TEST_DATA_PROPERTY))); + return Paths.get( + Objects.requireNonNull( + path, + String.format( + "Could not find test data path. Set the environment variable %s or the JVM property %s.", + TEST_DATA_ENV_VAR, TEST_DATA_PROPERTY))); } /** @@ -68,10 +69,9 @@ public static File exampleCACert() { */ public static List exampleTlsCerts() { final Path root = getFlightTestDataRoot(); - return Arrays.asList(new CertKeyPair(root.resolve("cert0.pem") - .toFile(), root.resolve("cert0.pkcs1").toFile()), - new CertKeyPair(root.resolve("cert1.pem") - .toFile(), root.resolve("cert1.pkcs1").toFile())); + return Arrays.asList( + new CertKeyPair(root.resolve("cert0.pem").toFile(), root.resolve("cert0.pkcs1").toFile()), + new CertKeyPair(root.resolve("cert1.pem").toFile(), root.resolve("cert1.pkcs1").toFile())); } public static class CertKeyPair { diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/MockFlightSqlProducer.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/MockFlightSqlProducer.java index 52a397edab1..e6e4722daa2 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/MockFlightSqlProducer.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/MockFlightSqlProducer.java @@ -23,6 +23,9 @@ import static java.util.UUID.randomUUID; import static java.util.stream.Collectors.toList; +import com.google.protobuf.Any; +import com.google.protobuf.ByteString; +import com.google.protobuf.Message; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.ByteBuffer; @@ -40,7 +43,6 @@ import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.stream.IntStream; - import org.apache.arrow.flight.Action; import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.Criteria; @@ -84,13 +86,7 @@ import org.apache.arrow.vector.util.JsonStringArrayList; import org.apache.calcite.avatica.Meta.StatementType; -import com.google.protobuf.Any; -import com.google.protobuf.ByteString; -import com.google.protobuf.Message; - -/** - * An ad-hoc {@link FlightSqlProducer} for tests. - */ +/** An ad-hoc {@link FlightSqlProducer} for tests. */ public final class MockFlightSqlProducer implements FlightSqlProducer { private final Map>> queryResults = new HashMap<>(); @@ -99,26 +95,26 @@ public final class MockFlightSqlProducer implements FlightSqlProducer { private final Map> catalogQueriesResults = new HashMap<>(); private final Map>> - updateResultProviders = - new HashMap<>(); + updateResultProviders = new HashMap<>(); private final SqlInfoBuilder sqlInfoBuilder = new SqlInfoBuilder(); private final Map parameterSchemas = new HashMap<>(); private final Map>> expectedParameterValues = new HashMap<>(); private final Map actionTypeCounter = new HashMap<>(); - private static FlightInfo getFlightInfoExportedAndImportedKeys(final Message message, - final FlightDescriptor descriptor) { + private static FlightInfo getFlightInfoExportedAndImportedKeys( + final Message message, final FlightDescriptor descriptor) { return getFlightInfo(message, Schemas.GET_IMPORTED_KEYS_SCHEMA, descriptor); } - private static FlightInfo getFlightInfo(final Message message, final Schema schema, - final FlightDescriptor descriptor) { + private static FlightInfo getFlightInfo( + final Message message, final Schema schema, final FlightDescriptor descriptor) { return new FlightInfo( schema, descriptor, Collections.singletonList(new FlightEndpoint(new Ticket(Any.pack(message).toByteArray()))), - -1, -1); + -1, + -1); } public static ByteBuffer serializeSchema(final Schema schema) { @@ -135,12 +131,14 @@ public static ByteBuffer serializeSchema(final Schema schema) { /** * Registers a new {@link StatementType#SELECT} SQL query. * - * @param sqlCommand the SQL command under which to register the new query. - * @param schema the schema to use for the query result. + * @param sqlCommand the SQL command under which to register the new query. + * @param schema the schema to use for the query result. * @param resultProviders the result provider for this query. */ - public void addSelectQuery(final String sqlCommand, final Schema schema, - final List> resultProviders) { + public void addSelectQuery( + final String sqlCommand, + final Schema schema, + final List> resultProviders) { final int providers = resultProviders.size(); final List uuids = IntStream.range(0, providers) @@ -155,59 +153,64 @@ public void addSelectQuery(final String sqlCommand, final Schema schema, /** * Registers a new {@link StatementType#UPDATE} SQL query. * - * @param sqlCommand the SQL command. + * @param sqlCommand the SQL command. * @param updatedRows the number of rows affected. */ public void addUpdateQuery(final String sqlCommand, final long updatedRows) { - addUpdateQuery(sqlCommand, (flightStream, putResultStreamListener) -> { - final DoPutUpdateResult result = - DoPutUpdateResult.newBuilder().setRecordCount(updatedRows).build(); - try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - final ArrowBuf buffer = allocator.buffer(result.getSerializedSize())) { - buffer.writeBytes(result.toByteArray()); - putResultStreamListener.onNext(PutResult.metadata(buffer)); - } catch (final Throwable throwable) { - putResultStreamListener.onError(throwable); - } finally { - putResultStreamListener.onCompleted(); - } - }); + addUpdateQuery( + sqlCommand, + (flightStream, putResultStreamListener) -> { + final DoPutUpdateResult result = + DoPutUpdateResult.newBuilder().setRecordCount(updatedRows).build(); + try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); + final ArrowBuf buffer = allocator.buffer(result.getSerializedSize())) { + buffer.writeBytes(result.toByteArray()); + putResultStreamListener.onNext(PutResult.metadata(buffer)); + } catch (final Throwable throwable) { + putResultStreamListener.onError(throwable); + } finally { + putResultStreamListener.onCompleted(); + } + }); } /** * Adds a catalog query to the results. * - * @param message the {@link Message} corresponding to the catalog query request type to register. + * @param message the {@link Message} corresponding to the catalog query request type to register. * @param resultsProvider the results provider. */ - public void addCatalogQuery(final Message message, - final Consumer resultsProvider) { + public void addCatalogQuery( + final Message message, final Consumer resultsProvider) { catalogQueriesResults.put(message, resultsProvider); } /** * Registers a new {@link StatementType#UPDATE} SQL query. * - * @param sqlCommand the SQL command. + * @param sqlCommand the SQL command. * @param resultsProvider consumer for producing update results. */ - void addUpdateQuery(final String sqlCommand, - final BiConsumer> resultsProvider) { + void addUpdateQuery( + final String sqlCommand, + final BiConsumer> resultsProvider) { Preconditions.checkState( updateResultProviders.putIfAbsent(sqlCommand, resultsProvider) == null, format("Attempted to overwrite preexisting query: <%s>.", sqlCommand)); } /** Registers parameters expected to be provided with a prepared statement. */ - public void addExpectedParameters(String query, Schema parameterSchema, List> expectedValues) { + public void addExpectedParameters( + String query, Schema parameterSchema, List> expectedValues) { parameterSchemas.put(query, parameterSchema); expectedParameterValues.put(query, expectedValues); } @Override - public void createPreparedStatement(final ActionCreatePreparedStatementRequest request, - final CallContext callContext, - final StreamListener listener) { + public void createPreparedStatement( + final ActionCreatePreparedStatementRequest request, + final CallContext callContext, + final StreamListener listener) { try { final ByteString preparedStatementHandle = copyFrom(randomUUID().toString().getBytes(StandardCharsets.UTF_8)); @@ -222,8 +225,7 @@ public void createPreparedStatement(final ActionCreatePreparedStatementRequest r preparedStatements.put(preparedStatementHandle, query); final Schema datasetSchema = entry.getKey(); - final ByteString datasetSchemaBytes = - ByteString.copyFrom(serializeSchema(datasetSchema)); + final ByteString datasetSchemaBytes = ByteString.copyFrom(serializeSchema(datasetSchema)); resultBuilder.setDatasetSchema(datasetSchemaBytes); } else if (updateResultProviders.containsKey(query)) { @@ -238,7 +240,8 @@ public void createPreparedStatement(final ActionCreatePreparedStatementRequest r final Schema parameterSchema = parameterSchemas.get(query); if (parameterSchema != null) { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - MessageSerializer.serialize(new WriteChannel(Channels.newChannel(outputStream)), parameterSchema); + MessageSerializer.serialize( + new WriteChannel(Channels.newChannel(outputStream)), parameterSchema); resultBuilder.setParameterSchema(ByteString.copyFrom(outputStream.toByteArray())); } @@ -253,19 +256,21 @@ public void createPreparedStatement(final ActionCreatePreparedStatementRequest r @Override public void closePreparedStatement( final ActionClosePreparedStatementRequest actionClosePreparedStatementRequest, - final CallContext callContext, final StreamListener streamListener) { + final CallContext callContext, + final StreamListener streamListener) { // TODO Implement this method. streamListener.onCompleted(); } @Override - public FlightInfo getFlightInfoStatement(final CommandStatementQuery commandStatementQuery, - final CallContext callContext, - final FlightDescriptor flightDescriptor) { + public FlightInfo getFlightInfoStatement( + final CommandStatementQuery commandStatementQuery, + final CallContext callContext, + final FlightDescriptor flightDescriptor) { final String query = commandStatementQuery.getQuery(); final Entry> queryInfo = - Preconditions.checkNotNull(queryResults.get(query), - format("Query not registered: <%s>.", query)); + Preconditions.checkNotNull( + queryResults.get(query), format("Query not registered: <%s>.", query)); final List endpoints = queryInfo.getValue().stream() .map(TicketConversionUtils::getTicketBytesFromUuid) @@ -273,8 +278,8 @@ public FlightInfo getFlightInfoStatement(final CommandStatementQuery commandStat .map(TicketConversionUtils::getEndpointFromMessage) .collect(toList()); return FlightInfo.builder(queryInfo.getKey(), flightDescriptor, endpoints) - .setAppMetadata("foo".getBytes(StandardCharsets.UTF_8)) - .build(); + .setAppMetadata("foo".getBytes(StandardCharsets.UTF_8)) + .build(); } @Override @@ -285,12 +290,13 @@ public FlightInfo getFlightInfoPreparedStatement( final ByteString preparedStatementHandle = commandPreparedStatementQuery.getPreparedStatementHandle(); - final String query = Preconditions.checkNotNull( - preparedStatements.get(preparedStatementHandle), - format("No query registered under handle: <%s>.", preparedStatementHandle)); + final String query = + Preconditions.checkNotNull( + preparedStatements.get(preparedStatementHandle), + format("No query registered under handle: <%s>.", preparedStatementHandle)); final Entry> queryInfo = - Preconditions.checkNotNull(queryResults.get(query), - format("Query not registered: <%s>.", query)); + Preconditions.checkNotNull( + queryResults.get(query), format("Query not registered: <%s>.", query)); final List endpoints = queryInfo.getValue().stream() .map(TicketConversionUtils::getTicketBytesFromUuid) @@ -298,30 +304,33 @@ public FlightInfo getFlightInfoPreparedStatement( .map(TicketConversionUtils::getEndpointFromMessage) .collect(toList()); return FlightInfo.builder(queryInfo.getKey(), flightDescriptor, endpoints) - .setAppMetadata("foo".getBytes(StandardCharsets.UTF_8)) - .build(); + .setAppMetadata("foo".getBytes(StandardCharsets.UTF_8)) + .build(); } @Override - public SchemaResult getSchemaStatement(final CommandStatementQuery commandStatementQuery, - final CallContext callContext, - final FlightDescriptor flightDescriptor) { + public SchemaResult getSchemaStatement( + final CommandStatementQuery commandStatementQuery, + final CallContext callContext, + final FlightDescriptor flightDescriptor) { final String query = commandStatementQuery.getQuery(); final Entry> queryInfo = - Preconditions.checkNotNull(queryResults.get(query), - format("Query not registered: <%s>.", query)); + Preconditions.checkNotNull( + queryResults.get(query), format("Query not registered: <%s>.", query)); return new SchemaResult(queryInfo.getKey()); } @Override - public void getStreamStatement(final TicketStatementQuery ticketStatementQuery, - final CallContext callContext, - final ServerStreamListener serverStreamListener) { + public void getStreamStatement( + final TicketStatementQuery ticketStatementQuery, + final CallContext callContext, + final ServerStreamListener serverStreamListener) { final UUID uuid = UUID.fromString(ticketStatementQuery.getStatementHandle().toStringUtf8()); Preconditions.checkNotNull( selectResultProviders.get(uuid), - "No consumer was registered for the specified UUID: <%s>.", uuid) + "No consumer was registered for the specified UUID: <%s>.", + uuid) .accept(serverStreamListener); } @@ -334,15 +343,17 @@ public void getStreamPreparedStatement( UUID.fromString(commandPreparedStatementQuery.getPreparedStatementHandle().toStringUtf8()); Preconditions.checkNotNull( selectResultProviders.get(uuid), - "No consumer was registered for the specified UUID: <%s>.", uuid) + "No consumer was registered for the specified UUID: <%s>.", + uuid) .accept(serverStreamListener); } @Override - public Runnable acceptPutStatement(final CommandStatementUpdate commandStatementUpdate, - final CallContext callContext, - final FlightStream flightStream, - final StreamListener streamListener) { + public Runnable acceptPutStatement( + final CommandStatementUpdate commandStatementUpdate, + final CallContext callContext, + final FlightStream flightStream, + final StreamListener streamListener) { return () -> { final String query = commandStatementUpdate.getQuery(); final BiConsumer> resultProvider = @@ -353,9 +364,8 @@ public Runnable acceptPutStatement(final CommandStatementUpdate commandStatement }; } - private boolean validateParameters(String query, - FlightStream flightStream, - StreamListener streamListener) { + private boolean validateParameters( + String query, FlightStream flightStream, StreamListener streamListener) { final List> expectedValues = expectedParameterValues.get(query); if (expectedValues != null) { int index = 0; @@ -363,16 +373,18 @@ private boolean validateParameters(String query, final VectorSchemaRoot root = flightStream.getRoot(); for (int i = 0; i < root.getRowCount(); i++) { if (index >= expectedValues.size()) { - streamListener.onError(CallStatus.INVALID_ARGUMENT - .withDescription("More parameter rows provided than expected") - .toRuntimeException()); + streamListener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription("More parameter rows provided than expected") + .toRuntimeException()); return true; } List expectedRow = expectedValues.get(index++); if (root.getFieldVectors().size() != expectedRow.size()) { - streamListener.onError(CallStatus.INVALID_ARGUMENT - .withDescription("Parameter count mismatch") - .toRuntimeException()); + streamListener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription("Parameter count mismatch") + .toRuntimeException()); return true; } @@ -381,23 +393,28 @@ private boolean validateParameters(String query, Object actual = root.getVector(paramIndex).getObject(i); boolean matches; if (expected.getClass().isArray()) { - matches = Arrays.equals((Object[]) expected, ((JsonStringArrayList) actual).toArray()); + matches = + Arrays.equals((Object[]) expected, ((JsonStringArrayList) actual).toArray()); } else { matches = Objects.equals(expected, actual); } if (!matches) { - streamListener.onError(CallStatus.INVALID_ARGUMENT - .withDescription(String.format("Parameter mismatch. Expected: %s Actual: %s", expected, actual)) - .toRuntimeException()); + streamListener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription( + String.format( + "Parameter mismatch. Expected: %s Actual: %s", expected, actual)) + .toRuntimeException()); return true; } } } } if (index < expectedValues.size()) { - streamListener.onError(CallStatus.INVALID_ARGUMENT - .withDescription("Fewer parameter rows provided than expected") - .toRuntimeException()); + streamListener.onError( + CallStatus.INVALID_ARGUMENT + .withDescription("Fewer parameter rows provided than expected") + .toRuntimeException()); return true; } } @@ -407,147 +424,167 @@ private boolean validateParameters(String query, @Override public Runnable acceptPutPreparedStatementUpdate( final CommandPreparedStatementUpdate commandPreparedStatementUpdate, - final CallContext callContext, final FlightStream flightStream, + final CallContext callContext, + final FlightStream flightStream, final StreamListener streamListener) { final ByteString handle = commandPreparedStatementUpdate.getPreparedStatementHandle(); - final String query = Preconditions.checkNotNull( - preparedStatements.get(handle), - format("No query registered under handle: <%s>.", handle)); + final String query = + Preconditions.checkNotNull( + preparedStatements.get(handle), + format("No query registered under handle: <%s>.", handle)); if (validateParameters(query, flightStream, streamListener)) { - return () -> { }; + return () -> {}; } return acceptPutStatement( - CommandStatementUpdate.newBuilder().setQuery(query).build(), callContext, flightStream, + CommandStatementUpdate.newBuilder().setQuery(query).build(), + callContext, + flightStream, streamListener); } @Override public Runnable acceptPutPreparedStatementQuery( final CommandPreparedStatementQuery commandPreparedStatementQuery, - final CallContext callContext, final FlightStream flightStream, + final CallContext callContext, + final FlightStream flightStream, final StreamListener streamListener) { final ByteString handle = commandPreparedStatementQuery.getPreparedStatementHandle(); - final String query = Preconditions.checkNotNull( - preparedStatements.get(handle), - format("No query registered under handle: <%s>.", handle)); + final String query = + Preconditions.checkNotNull( + preparedStatements.get(handle), + format("No query registered under handle: <%s>.", handle)); if (validateParameters(query, flightStream, streamListener)) { - return () -> { }; + return () -> {}; } return streamListener::onCompleted; } @Override - public FlightInfo getFlightInfoSqlInfo(final CommandGetSqlInfo commandGetSqlInfo, - final CallContext callContext, - final FlightDescriptor flightDescriptor) { + public FlightInfo getFlightInfoSqlInfo( + final CommandGetSqlInfo commandGetSqlInfo, + final CallContext callContext, + final FlightDescriptor flightDescriptor) { return getFlightInfo(commandGetSqlInfo, Schemas.GET_SQL_INFO_SCHEMA, flightDescriptor); } @Override - public void getStreamSqlInfo(final CommandGetSqlInfo commandGetSqlInfo, - final CallContext callContext, - final ServerStreamListener serverStreamListener) { + public void getStreamSqlInfo( + final CommandGetSqlInfo commandGetSqlInfo, + final CallContext callContext, + final ServerStreamListener serverStreamListener) { sqlInfoBuilder.send(commandGetSqlInfo.getInfoList(), serverStreamListener); } @Override - public FlightInfo getFlightInfoTypeInfo(FlightSql.CommandGetXdbcTypeInfo request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoTypeInfo( + FlightSql.CommandGetXdbcTypeInfo request, CallContext context, FlightDescriptor descriptor) { // TODO Implement this return null; } @Override - public void getStreamTypeInfo(FlightSql.CommandGetXdbcTypeInfo request, CallContext context, - ServerStreamListener listener) { + public void getStreamTypeInfo( + FlightSql.CommandGetXdbcTypeInfo request, + CallContext context, + ServerStreamListener listener) { // TODO Implement this } @Override - public FlightInfo getFlightInfoCatalogs(final CommandGetCatalogs commandGetCatalogs, - final CallContext callContext, - final FlightDescriptor flightDescriptor) { + public FlightInfo getFlightInfoCatalogs( + final CommandGetCatalogs commandGetCatalogs, + final CallContext callContext, + final FlightDescriptor flightDescriptor) { return getFlightInfo(commandGetCatalogs, Schemas.GET_CATALOGS_SCHEMA, flightDescriptor); } @Override - public void getStreamCatalogs(final CallContext callContext, - final ServerStreamListener serverStreamListener) { + public void getStreamCatalogs( + final CallContext callContext, final ServerStreamListener serverStreamListener) { final CommandGetCatalogs command = CommandGetCatalogs.getDefaultInstance(); getStreamCatalogFunctions(command, serverStreamListener); } @Override - public FlightInfo getFlightInfoSchemas(final CommandGetDbSchemas commandGetSchemas, - final CallContext callContext, - final FlightDescriptor flightDescriptor) { + public FlightInfo getFlightInfoSchemas( + final CommandGetDbSchemas commandGetSchemas, + final CallContext callContext, + final FlightDescriptor flightDescriptor) { return getFlightInfo(commandGetSchemas, Schemas.GET_SCHEMAS_SCHEMA, flightDescriptor); } @Override - public void getStreamSchemas(final CommandGetDbSchemas commandGetSchemas, - final CallContext callContext, - final ServerStreamListener serverStreamListener) { + public void getStreamSchemas( + final CommandGetDbSchemas commandGetSchemas, + final CallContext callContext, + final ServerStreamListener serverStreamListener) { getStreamCatalogFunctions(commandGetSchemas, serverStreamListener); } @Override - public FlightInfo getFlightInfoTables(final CommandGetTables commandGetTables, - final CallContext callContext, - final FlightDescriptor flightDescriptor) { + public FlightInfo getFlightInfoTables( + final CommandGetTables commandGetTables, + final CallContext callContext, + final FlightDescriptor flightDescriptor) { return getFlightInfo(commandGetTables, Schemas.GET_TABLES_SCHEMA_NO_SCHEMA, flightDescriptor); } @Override - public void getStreamTables(final CommandGetTables commandGetTables, - final CallContext callContext, - final ServerStreamListener serverStreamListener) { + public void getStreamTables( + final CommandGetTables commandGetTables, + final CallContext callContext, + final ServerStreamListener serverStreamListener) { getStreamCatalogFunctions(commandGetTables, serverStreamListener); } @Override - public FlightInfo getFlightInfoTableTypes(final CommandGetTableTypes commandGetTableTypes, - final CallContext callContext, - final FlightDescriptor flightDescriptor) { + public FlightInfo getFlightInfoTableTypes( + final CommandGetTableTypes commandGetTableTypes, + final CallContext callContext, + final FlightDescriptor flightDescriptor) { return getFlightInfo(commandGetTableTypes, Schemas.GET_TABLE_TYPES_SCHEMA, flightDescriptor); } @Override - public void getStreamTableTypes(final CallContext callContext, - final ServerStreamListener serverStreamListener) { + public void getStreamTableTypes( + final CallContext callContext, final ServerStreamListener serverStreamListener) { final CommandGetTableTypes command = CommandGetTableTypes.getDefaultInstance(); getStreamCatalogFunctions(command, serverStreamListener); } @Override - public FlightInfo getFlightInfoPrimaryKeys(final CommandGetPrimaryKeys commandGetPrimaryKeys, - final CallContext callContext, - final FlightDescriptor flightDescriptor) { + public FlightInfo getFlightInfoPrimaryKeys( + final CommandGetPrimaryKeys commandGetPrimaryKeys, + final CallContext callContext, + final FlightDescriptor flightDescriptor) { return getFlightInfo(commandGetPrimaryKeys, Schemas.GET_PRIMARY_KEYS_SCHEMA, flightDescriptor); } @Override - public void getStreamPrimaryKeys(final CommandGetPrimaryKeys commandGetPrimaryKeys, - final CallContext callContext, - final ServerStreamListener serverStreamListener) { + public void getStreamPrimaryKeys( + final CommandGetPrimaryKeys commandGetPrimaryKeys, + final CallContext callContext, + final ServerStreamListener serverStreamListener) { getStreamCatalogFunctions(commandGetPrimaryKeys, serverStreamListener); } @Override - public FlightInfo getFlightInfoExportedKeys(final CommandGetExportedKeys commandGetExportedKeys, - final CallContext callContext, - final FlightDescriptor flightDescriptor) { + public FlightInfo getFlightInfoExportedKeys( + final CommandGetExportedKeys commandGetExportedKeys, + final CallContext callContext, + final FlightDescriptor flightDescriptor) { return getFlightInfoExportedAndImportedKeys(commandGetExportedKeys, flightDescriptor); } @Override - public FlightInfo getFlightInfoImportedKeys(final CommandGetImportedKeys commandGetImportedKeys, - final CallContext callContext, - final FlightDescriptor flightDescriptor) { + public FlightInfo getFlightInfoImportedKeys( + final CommandGetImportedKeys commandGetImportedKeys, + final CallContext callContext, + final FlightDescriptor flightDescriptor) { return getFlightInfoExportedAndImportedKeys(commandGetImportedKeys, flightDescriptor); } @@ -560,23 +597,26 @@ public FlightInfo getFlightInfoCrossReference( } @Override - public void getStreamExportedKeys(final CommandGetExportedKeys commandGetExportedKeys, - final CallContext callContext, - final ServerStreamListener serverStreamListener) { + public void getStreamExportedKeys( + final CommandGetExportedKeys commandGetExportedKeys, + final CallContext callContext, + final ServerStreamListener serverStreamListener) { getStreamCatalogFunctions(commandGetExportedKeys, serverStreamListener); } @Override - public void getStreamImportedKeys(final CommandGetImportedKeys commandGetImportedKeys, - final CallContext callContext, - final ServerStreamListener serverStreamListener) { + public void getStreamImportedKeys( + final CommandGetImportedKeys commandGetImportedKeys, + final CallContext callContext, + final ServerStreamListener serverStreamListener) { getStreamCatalogFunctions(commandGetImportedKeys, serverStreamListener); } @Override - public void getStreamCrossReference(final CommandGetCrossReference commandGetCrossReference, - final CallContext callContext, - final ServerStreamListener serverStreamListener) { + public void getStreamCrossReference( + final CommandGetCrossReference commandGetCrossReference, + final CallContext callContext, + final ServerStreamListener serverStreamListener) { getStreamCatalogFunctions(commandGetCrossReference, serverStreamListener); } @@ -586,8 +626,10 @@ public void close() { } @Override - public void listFlights(final CallContext callContext, final Criteria criteria, - final StreamListener streamListener) { + public void listFlights( + final CallContext callContext, + final Criteria criteria, + final StreamListener streamListener) { // TODO Implement this method. throw CallStatus.UNIMPLEMENTED.toRuntimeException(); } @@ -595,11 +637,13 @@ public void listFlights(final CallContext callContext, final Criteria criteria, @Override public void doAction(CallContext context, Action action, StreamListener listener) { FlightSqlProducer.super.doAction(context, action, listener); - actionTypeCounter.put(action.getType(), actionTypeCounter.getOrDefault(action.getType(), 0) + 1); + actionTypeCounter.put( + action.getType(), actionTypeCounter.getOrDefault(action.getType(), 0) + 1); } /** - * Clear the `actionTypeCounter` map and restore to its default state. Intended to be used in tests. + * Clear the `actionTypeCounter` map and restore to its default state. Intended to be used in + * tests. */ public void clearActionTypeCounter() { actionTypeCounter.clear(); @@ -609,9 +653,8 @@ public Map getActionTypeCounter() { return actionTypeCounter; } - - private void getStreamCatalogFunctions(final Message ticket, - final ServerStreamListener serverStreamListener) { + private void getStreamCatalogFunctions( + final Message ticket, final ServerStreamListener serverStreamListener) { Preconditions.checkNotNull( catalogQueriesResults.get(ticket), format("Query not registered for ticket: <%s>", ticket)) diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/PartitionedFlightSqlProducer.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/PartitionedFlightSqlProducer.java index 3230ce626fa..a5f1851c224 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/PartitionedFlightSqlProducer.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/PartitionedFlightSqlProducer.java @@ -19,9 +19,10 @@ import static com.google.protobuf.Any.pack; +import com.google.protobuf.ByteString; +import com.google.protobuf.Message; import java.util.Arrays; import java.util.List; - import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.FlightDescriptor; import org.apache.arrow.flight.FlightEndpoint; @@ -34,14 +35,9 @@ import org.apache.arrow.vector.VectorSchemaRoot; import org.apache.arrow.vector.types.pojo.Schema; -import com.google.protobuf.ByteString; -import com.google.protobuf.Message; - public class PartitionedFlightSqlProducer extends BasicFlightSqlProducer { - /** - * A minimal FlightProducer intended to just serve data when given the correct Ticket. - */ + /** A minimal FlightProducer intended to just serve data when given the correct Ticket. */ public static class DataOnlyFlightSqlProducer extends NoOpFlightProducer { private final Ticket ticket; private final VectorSchemaRoot data; @@ -54,7 +50,8 @@ public DataOnlyFlightSqlProducer(Ticket ticket, VectorSchemaRoot data) { @Override public void getStream(CallContext context, Ticket ticket, ServerStreamListener listener) { if (!Arrays.equals(ticket.getBytes(), this.ticket.getBytes())) { - listener.error(CallStatus.INVALID_ARGUMENT.withDescription("Illegal ticket.").toRuntimeException()); + listener.error( + CallStatus.INVALID_ARGUMENT.withDescription("Illegal ticket.").toRuntimeException()); return; } @@ -80,8 +77,10 @@ protected List determineEndpoints( } @Override - public void createPreparedStatement(FlightSql.ActionCreatePreparedStatementRequest request, - CallContext context, StreamListener listener) { + public void createPreparedStatement( + FlightSql.ActionCreatePreparedStatementRequest request, + CallContext context, + StreamListener listener) { final FlightSql.ActionCreatePreparedStatementResult.Builder resultBuilder = FlightSql.ActionCreatePreparedStatementResult.newBuilder() .setPreparedStatementHandle(ByteString.EMPTY); @@ -100,14 +99,18 @@ public FlightInfo getFlightInfoStatement( } @Override - public FlightInfo getFlightInfoPreparedStatement(FlightSql.CommandPreparedStatementQuery command, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoPreparedStatement( + FlightSql.CommandPreparedStatementQuery command, + CallContext context, + FlightDescriptor descriptor) { return FlightInfo.builder(schema, descriptor, endpoints).build(); } @Override - public void closePreparedStatement(FlightSql.ActionClosePreparedStatementRequest request, - CallContext context, StreamListener listener) { + public void closePreparedStatement( + FlightSql.ActionClosePreparedStatementRequest request, + CallContext context, + StreamListener listener) { listener.onCompleted(); } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ResultSetTestUtils.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ResultSetTestUtils.java index d5ce7fb8fb3..c78da3dd935 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ResultSetTestUtils.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ResultSetTestUtils.java @@ -25,34 +25,35 @@ import java.util.ArrayList; import java.util.List; import java.util.function.Function; - import org.apache.arrow.util.Preconditions; import org.junit.rules.ErrorCollector; /** - * Utility class for testing that require asserting that the values in a {@link ResultSet} are expected. + * Utility class for testing that require asserting that the values in a {@link ResultSet} are + * expected. */ public final class ResultSetTestUtils { private final ErrorCollector collector; public ResultSetTestUtils(final ErrorCollector collector) { - this.collector = - Preconditions.checkNotNull(collector, "Error collector cannot be null."); + this.collector = Preconditions.checkNotNull(collector, "Error collector cannot be null."); } /** * Checks that the values (rows and columns) in the provided {@link ResultSet} are expected. * - * @param resultSet the {@code ResultSet} to assert. + * @param resultSet the {@code ResultSet} to assert. * @param expectedResults the rows and columns representing the only values the {@code resultSet} - * is expected to have. - * @param collector the {@link ErrorCollector} to use for asserting that the {@code resultSet} - * has the expected values. - * @param the type to be found in the expected results for the {@code resultSet}. + * is expected to have. + * @param collector the {@link ErrorCollector} to use for asserting that the {@code resultSet} has + * the expected values. + * @param the type to be found in the expected results for the {@code resultSet}. * @throws SQLException if querying the {@code ResultSet} fails at some point unexpectedly. */ - public static void testData(final ResultSet resultSet, final List> expectedResults, - final ErrorCollector collector) + public static void testData( + final ResultSet resultSet, + final List> expectedResults, + final ErrorCollector collector) throws SQLException { testData( resultSet, @@ -64,19 +65,21 @@ public static void testData(final ResultSet resultSet, final List> e /** * Checks that the values (rows and columns) in the provided {@link ResultSet} are expected. * - * @param resultSet the {@code ResultSet} to assert. - * @param columnNames the column names to fetch in the {@code ResultSet} for comparison. + * @param resultSet the {@code ResultSet} to assert. + * @param columnNames the column names to fetch in the {@code ResultSet} for comparison. * @param expectedResults the rows and columns representing the only values the {@code resultSet} - * is expected to have. - * @param collector the {@link ErrorCollector} to use for asserting that the {@code resultSet} - * has the expected values. - * @param the type to be found in the expected results for the {@code resultSet}. + * is expected to have. + * @param collector the {@link ErrorCollector} to use for asserting that the {@code resultSet} has + * the expected values. + * @param the type to be found in the expected results for the {@code resultSet}. * @throws SQLException if querying the {@code ResultSet} fails at some point unexpectedly. */ @SuppressWarnings("unchecked") - public static void testData(final ResultSet resultSet, final List columnNames, - final List> expectedResults, - final ErrorCollector collector) + public static void testData( + final ResultSet resultSet, + final List columnNames, + final List> expectedResults, + final ErrorCollector collector) throws SQLException { testData( resultSet, @@ -98,19 +101,21 @@ public static void testData(final ResultSet resultSet, final List co /** * Checks that the values (rows and columns) in the provided {@link ResultSet} are expected. * - * @param resultSet the {@code ResultSet} to assert. - * @param columnIndices the column indices to fetch in the {@code ResultSet} for comparison. + * @param resultSet the {@code ResultSet} to assert. + * @param columnIndices the column indices to fetch in the {@code ResultSet} for comparison. * @param expectedResults the rows and columns representing the only values the {@code resultSet} - * is expected to have. - * @param collector the {@link ErrorCollector} to use for asserting that the {@code resultSet} - * has the expected values. - * @param the type to be found in the expected results for the {@code resultSet}. + * is expected to have. + * @param collector the {@link ErrorCollector} to use for asserting that the {@code resultSet} has + * the expected values. + * @param the type to be found in the expected results for the {@code resultSet}. * @throws SQLException if querying the {@code ResultSet} fails at some point unexpectedly. */ @SuppressWarnings("unchecked") - public static void testData(final ResultSet resultSet, final int[] columnIndices, - final List> expectedResults, - final ErrorCollector collector) + public static void testData( + final ResultSet resultSet, + final int[] columnIndices, + final List> expectedResults, + final ErrorCollector collector) throws SQLException { testData( resultSet, @@ -132,19 +137,20 @@ public static void testData(final ResultSet resultSet, final int[] columnInd /** * Checks that the values (rows and columns) in the provided {@link ResultSet} are expected. * - * @param resultSet the {@code ResultSet} to assert. - * @param dataConsumer the column indices to fetch in the {@code ResultSet} for comparison. + * @param resultSet the {@code ResultSet} to assert. + * @param dataConsumer the column indices to fetch in the {@code ResultSet} for comparison. * @param expectedResults the rows and columns representing the only values the {@code resultSet} - * is expected to have. - * @param collector the {@link ErrorCollector} to use for asserting that the {@code resultSet} - * has the expected values. - * @param the type to be found in the expected results for the {@code resultSet}. + * is expected to have. + * @param collector the {@link ErrorCollector} to use for asserting that the {@code resultSet} has + * the expected values. + * @param the type to be found in the expected results for the {@code resultSet}. * @throws SQLException if querying the {@code ResultSet} fails at some point unexpectedly. */ - public static void testData(final ResultSet resultSet, - final Function> dataConsumer, - final List> expectedResults, - final ErrorCollector collector) + public static void testData( + final ResultSet resultSet, + final Function> dataConsumer, + final List> expectedResults, + final ErrorCollector collector) throws SQLException { final List> actualResults = new ArrayList<>(); while (resultSet.next()) { @@ -156,9 +162,10 @@ public static void testData(final ResultSet resultSet, /** * Checks that the values (rows and columns) in the provided {@link ResultSet} are expected. * - * @param resultSet the {@code ResultSet} to assert. - * @param expectedResults the rows and columns representing the only values the {@code resultSet} is expected to have. - * @param the type to be found in the expected results for the {@code resultSet}. + * @param resultSet the {@code ResultSet} to assert. + * @param expectedResults the rows and columns representing the only values the {@code resultSet} + * is expected to have. + * @param the type to be found in the expected results for the {@code resultSet}. * @throws SQLException if querying the {@code ResultSet} fails at some point unexpectedly. */ public void testData(final ResultSet resultSet, final List> expectedResults) @@ -169,45 +176,54 @@ public void testData(final ResultSet resultSet, final List> expected /** * Checks that the values (rows and columns) in the provided {@link ResultSet} are expected. * - * @param resultSet the {@code ResultSet} to assert. - * @param columnNames the column names to fetch in the {@code ResultSet} for comparison. - * @param expectedResults the rows and columns representing the only values the {@code resultSet} is expected to have. - * @param the type to be found in the expected results for the {@code resultSet}. + * @param resultSet the {@code ResultSet} to assert. + * @param columnNames the column names to fetch in the {@code ResultSet} for comparison. + * @param expectedResults the rows and columns representing the only values the {@code resultSet} + * is expected to have. + * @param the type to be found in the expected results for the {@code resultSet}. * @throws SQLException if querying the {@code ResultSet} fails at some point unexpectedly. */ @SuppressWarnings("unchecked") - public void testData(final ResultSet resultSet, final List columnNames, - final List> expectedResults) throws SQLException { + public void testData( + final ResultSet resultSet, + final List columnNames, + final List> expectedResults) + throws SQLException { testData(resultSet, columnNames, expectedResults, collector); } /** * Checks that the values (rows and columns) in the provided {@link ResultSet} are expected. * - * @param resultSet the {@code ResultSet} to assert. - * @param columnIndices the column indices to fetch in the {@code ResultSet} for comparison. - * @param expectedResults the rows and columns representing the only values the {@code resultSet} is expected to have. - * @param the type to be found in the expected results for the {@code resultSet}. + * @param resultSet the {@code ResultSet} to assert. + * @param columnIndices the column indices to fetch in the {@code ResultSet} for comparison. + * @param expectedResults the rows and columns representing the only values the {@code resultSet} + * is expected to have. + * @param the type to be found in the expected results for the {@code resultSet}. * @throws SQLException if querying the {@code ResultSet} fails at some point unexpectedly. */ @SuppressWarnings("unchecked") - public void testData(final ResultSet resultSet, final int[] columnIndices, - final List> expectedResults) throws SQLException { + public void testData( + final ResultSet resultSet, final int[] columnIndices, final List> expectedResults) + throws SQLException { testData(resultSet, columnIndices, expectedResults, collector); } /** * Checks that the values (rows and columns) in the provided {@link ResultSet} are expected. * - * @param resultSet the {@code ResultSet} to assert. - * @param dataConsumer the column indices to fetch in the {@code ResultSet} for comparison. - * @param expectedResults the rows and columns representing the only values the {@code resultSet} is expected to have. - * @param the type to be found in the expected results for the {@code resultSet}. + * @param resultSet the {@code ResultSet} to assert. + * @param dataConsumer the column indices to fetch in the {@code ResultSet} for comparison. + * @param expectedResults the rows and columns representing the only values the {@code resultSet} + * is expected to have. + * @param the type to be found in the expected results for the {@code resultSet}. * @throws SQLException if querying the {@code ResultSet} fails at some point unexpectedly. */ - public void testData(final ResultSet resultSet, - final Function> dataConsumer, - final List> expectedResults) throws SQLException { + public void testData( + final ResultSet resultSet, + final Function> dataConsumer, + final List> expectedResults) + throws SQLException { testData(resultSet, dataConsumer, expectedResults, collector); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/RootAllocatorTestRule.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/RootAllocatorTestRule.java index fd8fb57fcaf..f176093d47b 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/RootAllocatorTestRule.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/RootAllocatorTestRule.java @@ -22,7 +22,6 @@ import java.util.Random; import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.util.AutoCloseables; @@ -103,31 +102,32 @@ public void close() throws Exception { * @return Float8Vector */ public Float8Vector createFloat8Vector() { - double[] doubleVectorValues = new double[] { - 0, - 1, - -1, - Byte.MIN_VALUE, - Byte.MAX_VALUE, - Short.MIN_VALUE, - Short.MAX_VALUE, - Integer.MIN_VALUE, - Integer.MAX_VALUE, - Long.MIN_VALUE, - Long.MAX_VALUE, - Float.MAX_VALUE, - -Float.MAX_VALUE, - Float.NEGATIVE_INFINITY, - Float.POSITIVE_INFINITY, - Float.MIN_VALUE, - -Float.MIN_VALUE, - Double.MAX_VALUE, - -Double.MAX_VALUE, - Double.NEGATIVE_INFINITY, - Double.POSITIVE_INFINITY, - Double.MIN_VALUE, - -Double.MIN_VALUE, - }; + double[] doubleVectorValues = + new double[] { + 0, + 1, + -1, + Byte.MIN_VALUE, + Byte.MAX_VALUE, + Short.MIN_VALUE, + Short.MAX_VALUE, + Integer.MIN_VALUE, + Integer.MAX_VALUE, + Long.MIN_VALUE, + Long.MAX_VALUE, + Float.MAX_VALUE, + -Float.MAX_VALUE, + Float.NEGATIVE_INFINITY, + Float.POSITIVE_INFINITY, + Float.MIN_VALUE, + -Float.MIN_VALUE, + Double.MAX_VALUE, + -Double.MAX_VALUE, + Double.NEGATIVE_INFINITY, + Double.POSITIVE_INFINITY, + Double.MIN_VALUE, + -Double.MIN_VALUE, + }; Float8Vector result = new Float8Vector("", this.getRootAllocator()); result.setValueCount(MAX_VALUE); @@ -158,25 +158,26 @@ public Float8Vector createFloat8VectorForNullTests() { */ public Float4Vector createFloat4Vector() { - float[] floatVectorValues = new float[] { - 0, - 1, - -1, - Byte.MIN_VALUE, - Byte.MAX_VALUE, - Short.MIN_VALUE, - Short.MAX_VALUE, - Integer.MIN_VALUE, - Integer.MAX_VALUE, - Long.MIN_VALUE, - Long.MAX_VALUE, - Float.MAX_VALUE, - -Float.MAX_VALUE, - Float.NEGATIVE_INFINITY, - Float.POSITIVE_INFINITY, - Float.MIN_VALUE, - -Float.MIN_VALUE, - }; + float[] floatVectorValues = + new float[] { + 0, + 1, + -1, + Byte.MIN_VALUE, + Byte.MAX_VALUE, + Short.MIN_VALUE, + Short.MAX_VALUE, + Integer.MIN_VALUE, + Integer.MAX_VALUE, + Long.MIN_VALUE, + Long.MAX_VALUE, + Float.MAX_VALUE, + -Float.MAX_VALUE, + Float.NEGATIVE_INFINITY, + Float.POSITIVE_INFINITY, + Float.MIN_VALUE, + -Float.MIN_VALUE, + }; Float4Vector result = new Float4Vector("", this.getRootAllocator()); result.setValueCount(MAX_VALUE); @@ -198,17 +199,18 @@ public Float4Vector createFloat4Vector() { */ public IntVector createIntVector() { - int[] intVectorValues = new int[] { - 0, - 1, - -1, - Byte.MIN_VALUE, - Byte.MAX_VALUE, - Short.MIN_VALUE, - Short.MAX_VALUE, - Integer.MIN_VALUE, - Integer.MAX_VALUE, - }; + int[] intVectorValues = + new int[] { + 0, + 1, + -1, + Byte.MIN_VALUE, + Byte.MAX_VALUE, + Short.MIN_VALUE, + Short.MAX_VALUE, + Integer.MIN_VALUE, + Integer.MAX_VALUE, + }; IntVector result = new IntVector("", this.getRootAllocator()); result.setValueCount(MAX_VALUE); @@ -230,15 +232,10 @@ public IntVector createIntVector() { */ public SmallIntVector createSmallIntVector() { - short[] smallIntVectorValues = new short[] { - 0, - 1, - -1, - Byte.MIN_VALUE, - Byte.MAX_VALUE, - Short.MIN_VALUE, - Short.MAX_VALUE, - }; + short[] smallIntVectorValues = + new short[] { + 0, 1, -1, Byte.MIN_VALUE, Byte.MAX_VALUE, Short.MIN_VALUE, Short.MAX_VALUE, + }; SmallIntVector result = new SmallIntVector("", this.getRootAllocator()); result.setValueCount(MAX_VALUE); @@ -260,13 +257,10 @@ public SmallIntVector createSmallIntVector() { */ public TinyIntVector createTinyIntVector() { - byte[] byteVectorValues = new byte[] { - 0, - 1, - -1, - Byte.MIN_VALUE, - Byte.MAX_VALUE, - }; + byte[] byteVectorValues = + new byte[] { + 0, 1, -1, Byte.MIN_VALUE, Byte.MAX_VALUE, + }; TinyIntVector result = new TinyIntVector("", this.getRootAllocator()); result.setValueCount(MAX_VALUE); @@ -288,19 +282,20 @@ public TinyIntVector createTinyIntVector() { */ public BigIntVector createBigIntVector() { - long[] longVectorValues = new long[] { - 0, - 1, - -1, - Byte.MIN_VALUE, - Byte.MAX_VALUE, - Short.MIN_VALUE, - Short.MAX_VALUE, - Integer.MIN_VALUE, - Integer.MAX_VALUE, - Long.MIN_VALUE, - Long.MAX_VALUE, - }; + long[] longVectorValues = + new long[] { + 0, + 1, + -1, + Byte.MIN_VALUE, + Byte.MAX_VALUE, + Short.MIN_VALUE, + Short.MAX_VALUE, + Integer.MIN_VALUE, + Integer.MAX_VALUE, + Long.MIN_VALUE, + Long.MAX_VALUE, + }; BigIntVector result = new BigIntVector("", this.getRootAllocator()); result.setValueCount(MAX_VALUE); @@ -322,13 +317,10 @@ public BigIntVector createBigIntVector() { */ public UInt1Vector createUInt1Vector() { - short[] uInt1VectorValues = new short[] { - 0, - 1, - -1, - Byte.MIN_VALUE, - Byte.MAX_VALUE, - }; + short[] uInt1VectorValues = + new short[] { + 0, 1, -1, Byte.MIN_VALUE, Byte.MAX_VALUE, + }; UInt1Vector result = new UInt1Vector("", this.getRootAllocator()); result.setValueCount(MAX_VALUE); @@ -350,15 +342,10 @@ public UInt1Vector createUInt1Vector() { */ public UInt2Vector createUInt2Vector() { - int[] uInt2VectorValues = new int[] { - 0, - 1, - -1, - Byte.MIN_VALUE, - Byte.MAX_VALUE, - Short.MIN_VALUE, - Short.MAX_VALUE, - }; + int[] uInt2VectorValues = + new int[] { + 0, 1, -1, Byte.MIN_VALUE, Byte.MAX_VALUE, Short.MIN_VALUE, Short.MAX_VALUE, + }; UInt2Vector result = new UInt2Vector("", this.getRootAllocator()); result.setValueCount(MAX_VALUE); @@ -380,18 +367,18 @@ public UInt2Vector createUInt2Vector() { */ public UInt4Vector createUInt4Vector() { - - int[] uInt4VectorValues = new int[] { - 0, - 1, - -1, - Byte.MIN_VALUE, - Byte.MAX_VALUE, - Short.MIN_VALUE, - Short.MAX_VALUE, - Integer.MIN_VALUE, - Integer.MAX_VALUE - }; + int[] uInt4VectorValues = + new int[] { + 0, + 1, + -1, + Byte.MIN_VALUE, + Byte.MAX_VALUE, + Short.MIN_VALUE, + Short.MAX_VALUE, + Integer.MIN_VALUE, + Integer.MAX_VALUE + }; UInt4Vector result = new UInt4Vector("", this.getRootAllocator()); result.setValueCount(MAX_VALUE); @@ -413,19 +400,20 @@ public UInt4Vector createUInt4Vector() { */ public UInt8Vector createUInt8Vector() { - long[] uInt8VectorValues = new long[] { - 0, - 1, - -1, - Byte.MIN_VALUE, - Byte.MAX_VALUE, - Short.MIN_VALUE, - Short.MAX_VALUE, - Integer.MIN_VALUE, - Integer.MAX_VALUE, - Long.MIN_VALUE, - Long.MAX_VALUE - }; + long[] uInt8VectorValues = + new long[] { + 0, + 1, + -1, + Byte.MIN_VALUE, + Byte.MAX_VALUE, + Short.MIN_VALUE, + Short.MAX_VALUE, + Integer.MIN_VALUE, + Integer.MAX_VALUE, + Long.MIN_VALUE, + Long.MAX_VALUE + }; UInt8Vector result = new UInt8Vector("", this.getRootAllocator()); result.setValueCount(MAX_VALUE); @@ -504,20 +492,21 @@ public FixedSizeBinaryVector createFixedSizeBinaryVector() { */ public DecimalVector createDecimalVector() { - BigDecimal[] bigDecimalValues = new BigDecimal[] { - new BigDecimal(0), - new BigDecimal(1), - new BigDecimal(-1), - new BigDecimal(Byte.MIN_VALUE), - new BigDecimal(Byte.MAX_VALUE), - new BigDecimal(-Short.MAX_VALUE), - new BigDecimal(Short.MIN_VALUE), - new BigDecimal(Integer.MIN_VALUE), - new BigDecimal(Integer.MAX_VALUE), - new BigDecimal(Long.MIN_VALUE), - new BigDecimal(-Long.MAX_VALUE), - new BigDecimal("170141183460469231731687303715884105727") - }; + BigDecimal[] bigDecimalValues = + new BigDecimal[] { + new BigDecimal(0), + new BigDecimal(1), + new BigDecimal(-1), + new BigDecimal(Byte.MIN_VALUE), + new BigDecimal(Byte.MAX_VALUE), + new BigDecimal(-Short.MAX_VALUE), + new BigDecimal(Short.MIN_VALUE), + new BigDecimal(Integer.MIN_VALUE), + new BigDecimal(Integer.MAX_VALUE), + new BigDecimal(Long.MIN_VALUE), + new BigDecimal(-Long.MAX_VALUE), + new BigDecimal("170141183460469231731687303715884105727") + }; DecimalVector result = new DecimalVector("ID", this.getRootAllocator(), 39, 0); result.setValueCount(MAX_VALUE); @@ -539,30 +528,31 @@ public DecimalVector createDecimalVector() { */ public Decimal256Vector createDecimal256Vector() { - BigDecimal[] bigDecimalValues = new BigDecimal[] { - new BigDecimal(0), - new BigDecimal(1), - new BigDecimal(-1), - new BigDecimal(Byte.MIN_VALUE), - new BigDecimal(Byte.MAX_VALUE), - new BigDecimal(-Short.MAX_VALUE), - new BigDecimal(Short.MIN_VALUE), - new BigDecimal(Integer.MIN_VALUE), - new BigDecimal(Integer.MAX_VALUE), - new BigDecimal(Long.MIN_VALUE), - new BigDecimal(-Long.MAX_VALUE), - new BigDecimal("170141183460469231731687303715884105727"), - new BigDecimal("17014118346046923173168234157303715884105727"), - new BigDecimal("1701411834604692317316823415265417303715884105727"), - new BigDecimal("-17014118346046923173168234152654115451237303715884105727"), - new BigDecimal("-17014118346046923173168234152654115451231545157303715884105727"), - new BigDecimal("1701411834604692315815656534152654115451231545157303715884105727"), - new BigDecimal("30560141183460469231581565634152654115451231545157303715884105727"), - new BigDecimal( - "57896044618658097711785492504343953926634992332820282019728792003956564819967"), - new BigDecimal( - "-56896044618658097711785492504343953926634992332820282019728792003956564819967") - }; + BigDecimal[] bigDecimalValues = + new BigDecimal[] { + new BigDecimal(0), + new BigDecimal(1), + new BigDecimal(-1), + new BigDecimal(Byte.MIN_VALUE), + new BigDecimal(Byte.MAX_VALUE), + new BigDecimal(-Short.MAX_VALUE), + new BigDecimal(Short.MIN_VALUE), + new BigDecimal(Integer.MIN_VALUE), + new BigDecimal(Integer.MAX_VALUE), + new BigDecimal(Long.MIN_VALUE), + new BigDecimal(-Long.MAX_VALUE), + new BigDecimal("170141183460469231731687303715884105727"), + new BigDecimal("17014118346046923173168234157303715884105727"), + new BigDecimal("1701411834604692317316823415265417303715884105727"), + new BigDecimal("-17014118346046923173168234152654115451237303715884105727"), + new BigDecimal("-17014118346046923173168234152654115451231545157303715884105727"), + new BigDecimal("1701411834604692315815656534152654115451231545157303715884105727"), + new BigDecimal("30560141183460469231581565634152654115451231545157303715884105727"), + new BigDecimal( + "57896044618658097711785492504343953926634992332820282019728792003956564819967"), + new BigDecimal( + "-56896044618658097711785492504343953926634992332820282019728792003956564819967") + }; Decimal256Vector result = new Decimal256Vector("ID", this.getRootAllocator(), 77, 0); result.setValueCount(MAX_VALUE); @@ -764,13 +754,14 @@ public ListVector createListVector(String fieldName) { IntStream range = IntStream.range(0, MAX_VALUE); - range.forEach(row -> { - writer.startList(); - writer.setPosition(row); - IntStream.range(0, 5).map(j -> j * row).forEach(writer::writeInt); - writer.setValueCount(5); - writer.endList(); - }); + range.forEach( + row -> { + writer.startList(); + writer.setPosition(row); + IntStream.range(0, 5).map(j -> j * row).forEach(writer::writeInt); + writer.setValueCount(5); + writer.endList(); + }); valueVector.setValueCount(MAX_VALUE); @@ -785,13 +776,14 @@ public LargeListVector createLargeListVector() { IntStream range = IntStream.range(0, MAX_VALUE); - range.forEach(row -> { - writer.startList(); - writer.setPosition(row); - IntStream.range(0, 5).map(j -> j * row).forEach(writer::writeInt); - writer.setValueCount(5); - writer.endList(); - }); + range.forEach( + row -> { + writer.startList(); + writer.setPosition(row); + IntStream.range(0, 5).map(j -> j * row).forEach(writer::writeInt); + writer.setValueCount(5); + writer.endList(); + }); valueVector.setValueCount(MAX_VALUE); @@ -806,13 +798,14 @@ public FixedSizeListVector createFixedSizeListVector() { IntStream range = IntStream.range(0, MAX_VALUE); - range.forEach(row -> { - writer.startList(); - writer.setPosition(row); - IntStream.range(0, 5).map(j -> j * row).forEach(writer::writeInt); - writer.setValueCount(5); - writer.endList(); - }); + range.forEach( + row -> { + writer.startList(); + writer.setPosition(row); + IntStream.range(0, 5).map(j -> j * row).forEach(writer::writeInt); + writer.setValueCount(5); + writer.endList(); + }); valueVector.setValueCount(MAX_VALUE); diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/SqlTypesTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/SqlTypesTest.java index 5c7c873e55c..efc32e8354b 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/SqlTypesTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/SqlTypesTest.java @@ -22,7 +22,6 @@ import static org.junit.Assert.assertEquals; import java.sql.Types; - import org.apache.arrow.vector.types.DateUnit; import org.apache.arrow.vector.types.FloatingPointPrecision; import org.apache.arrow.vector.types.IntervalUnit; @@ -48,17 +47,20 @@ public void testGetSqlTypeIdFromArrowType() { assertEquals(Types.LONGVARCHAR, getSqlTypeIdFromArrowType(new ArrowType.LargeUtf8())); assertEquals(Types.DATE, getSqlTypeIdFromArrowType(new ArrowType.Date(DateUnit.MILLISECOND))); - assertEquals(Types.TIME, - getSqlTypeIdFromArrowType(new ArrowType.Time(TimeUnit.MILLISECOND, 32))); - assertEquals(Types.TIMESTAMP, + assertEquals( + Types.TIME, getSqlTypeIdFromArrowType(new ArrowType.Time(TimeUnit.MILLISECOND, 32))); + assertEquals( + Types.TIMESTAMP, getSqlTypeIdFromArrowType(new ArrowType.Timestamp(TimeUnit.MILLISECOND, ""))); assertEquals(Types.BOOLEAN, getSqlTypeIdFromArrowType(new ArrowType.Bool())); assertEquals(Types.DECIMAL, getSqlTypeIdFromArrowType(new ArrowType.Decimal(0, 0, 64))); - assertEquals(Types.DOUBLE, + assertEquals( + Types.DOUBLE, getSqlTypeIdFromArrowType(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE))); - assertEquals(Types.FLOAT, + assertEquals( + Types.FLOAT, getSqlTypeIdFromArrowType(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE))); assertEquals(Types.ARRAY, getSqlTypeIdFromArrowType(new ArrowType.List())); @@ -66,12 +68,13 @@ public void testGetSqlTypeIdFromArrowType() { assertEquals(Types.ARRAY, getSqlTypeIdFromArrowType(new ArrowType.FixedSizeList(10))); assertEquals(Types.JAVA_OBJECT, getSqlTypeIdFromArrowType(new ArrowType.Struct())); - assertEquals(Types.JAVA_OBJECT, - getSqlTypeIdFromArrowType(new ArrowType.Duration(TimeUnit.MILLISECOND))); - assertEquals(Types.JAVA_OBJECT, + assertEquals( + Types.JAVA_OBJECT, getSqlTypeIdFromArrowType(new ArrowType.Duration(TimeUnit.MILLISECOND))); + assertEquals( + Types.JAVA_OBJECT, getSqlTypeIdFromArrowType(new ArrowType.Interval(IntervalUnit.DAY_TIME))); - assertEquals(Types.JAVA_OBJECT, - getSqlTypeIdFromArrowType(new ArrowType.Union(UnionMode.Dense, null))); + assertEquals( + Types.JAVA_OBJECT, getSqlTypeIdFromArrowType(new ArrowType.Union(UnionMode.Dense, null))); assertEquals(Types.JAVA_OBJECT, getSqlTypeIdFromArrowType(new ArrowType.Map(true))); assertEquals(Types.NULL, getSqlTypeIdFromArrowType(new ArrowType.Null())); @@ -93,15 +96,18 @@ public void testGetSqlTypeNameFromArrowType() { assertEquals("DATE", getSqlTypeNameFromArrowType(new ArrowType.Date(DateUnit.MILLISECOND))); assertEquals("TIME", getSqlTypeNameFromArrowType(new ArrowType.Time(TimeUnit.MILLISECOND, 32))); - assertEquals("TIMESTAMP", + assertEquals( + "TIMESTAMP", getSqlTypeNameFromArrowType(new ArrowType.Timestamp(TimeUnit.MILLISECOND, ""))); assertEquals("BOOLEAN", getSqlTypeNameFromArrowType(new ArrowType.Bool())); assertEquals("DECIMAL", getSqlTypeNameFromArrowType(new ArrowType.Decimal(0, 0, 64))); - assertEquals("DOUBLE", + assertEquals( + "DOUBLE", getSqlTypeNameFromArrowType(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE))); - assertEquals("FLOAT", + assertEquals( + "FLOAT", getSqlTypeNameFromArrowType(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE))); assertEquals("ARRAY", getSqlTypeNameFromArrowType(new ArrowType.List())); @@ -110,12 +116,12 @@ public void testGetSqlTypeNameFromArrowType() { assertEquals("JAVA_OBJECT", getSqlTypeNameFromArrowType(new ArrowType.Struct())); - assertEquals("JAVA_OBJECT", - getSqlTypeNameFromArrowType(new ArrowType.Duration(TimeUnit.MILLISECOND))); - assertEquals("JAVA_OBJECT", - getSqlTypeNameFromArrowType(new ArrowType.Interval(IntervalUnit.DAY_TIME))); - assertEquals("JAVA_OBJECT", - getSqlTypeNameFromArrowType(new ArrowType.Union(UnionMode.Dense, null))); + assertEquals( + "JAVA_OBJECT", getSqlTypeNameFromArrowType(new ArrowType.Duration(TimeUnit.MILLISECOND))); + assertEquals( + "JAVA_OBJECT", getSqlTypeNameFromArrowType(new ArrowType.Interval(IntervalUnit.DAY_TIME))); + assertEquals( + "JAVA_OBJECT", getSqlTypeNameFromArrowType(new ArrowType.Union(UnionMode.Dense, null))); assertEquals("JAVA_OBJECT", getSqlTypeNameFromArrowType(new ArrowType.Map(true))); assertEquals("NULL", getSqlTypeNameFromArrowType(new ArrowType.Null())); diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ThrowableAssertionUtils.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ThrowableAssertionUtils.java index 48334dc0f92..da00c7855c7 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ThrowableAssertionUtils.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ThrowableAssertionUtils.java @@ -18,12 +18,11 @@ package org.apache.arrow.driver.jdbc.utils; /** - * Utility class to avoid upgrading JUnit to version >= 4.13 and keep using code to assert a {@link Throwable}. - * This should be removed as soon as we can use the proper assertThrows/checkThrows. + * Utility class to avoid upgrading JUnit to version >= 4.13 and keep using code to assert a {@link + * Throwable}. This should be removed as soon as we can use the proper assertThrows/checkThrows. */ public class ThrowableAssertionUtils { - private ThrowableAssertionUtils() { - } + private ThrowableAssertionUtils() {} public static void simpleAssertThrowableClass( final Class expectedThrowable, final ThrowingRunnable runnable) { @@ -33,15 +32,17 @@ public static void simpleAssertThrowableClass( if (expectedThrowable.isInstance(actualThrown)) { return; } else { - final String mismatchMessage = String.format("unexpected exception type thrown;\nexpected: %s\nactual: %s", - formatClass(expectedThrowable), - formatClass(actualThrown.getClass())); + final String mismatchMessage = + String.format( + "unexpected exception type thrown;\nexpected: %s\nactual: %s", + formatClass(expectedThrowable), formatClass(actualThrown.getClass())); throw new AssertionError(mismatchMessage, actualThrown); } } - final String notThrownMessage = String.format("expected %s to be thrown, but nothing was thrown", - formatClass(expectedThrowable)); + final String notThrownMessage = + String.format( + "expected %s to be thrown, but nothing was thrown", formatClass(expectedThrowable)); throw new AssertionError(notThrownMessage); } diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/UrlParserTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/UrlParserTest.java index 4e764ab322c..4d2999abaed 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/UrlParserTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/UrlParserTest.java @@ -21,7 +21,6 @@ import static org.junit.jupiter.api.Assertions.assertNull; import java.util.Map; - import org.junit.jupiter.api.Test; class UrlParserTest { diff --git a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/VectorSchemaRootTransformerTest.java b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/VectorSchemaRootTransformerTest.java index 1804b42cecb..02f07ab940c 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/VectorSchemaRootTransformerTest.java +++ b/java/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/VectorSchemaRootTransformerTest.java @@ -17,9 +17,9 @@ package org.apache.arrow.driver.jdbc.utils; +import com.google.common.collect.ImmutableList; import java.util.List; import java.util.stream.Collectors; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.VarBinaryVector; @@ -34,12 +34,9 @@ import org.junit.Rule; import org.junit.Test; -import com.google.common.collect.ImmutableList; - public class VectorSchemaRootTransformerTest { - @Rule - public RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); + @Rule public RootAllocatorTestRule rootAllocatorTestRule = new RootAllocatorTestRule(); private final BufferAllocator rootAllocator = rootAllocatorTestRule.getRootAllocator(); @Test @@ -49,11 +46,10 @@ public void testTransformerBuilderWorksCorrectly() throws Exception { final VarBinaryVector field3 = rootAllocatorTestRule.createVarBinaryVector("FIELD_3"); try (final VectorSchemaRoot originalRoot = VectorSchemaRoot.of(field1, field2, field3); - final VectorSchemaRoot clonedRoot = cloneVectorSchemaRoot(originalRoot)) { + final VectorSchemaRoot clonedRoot = cloneVectorSchemaRoot(originalRoot)) { final VectorSchemaRootTransformer.Builder builder = - new VectorSchemaRootTransformer.Builder(originalRoot.getSchema(), - rootAllocator); + new VectorSchemaRootTransformer.Builder(originalRoot.getSchema(), rootAllocator); builder.renameFieldVector("FIELD_3", "FIELD_3_RENAMED"); builder.addEmptyField("EMPTY_FIELD", new ArrowType.Bool()); @@ -62,12 +58,13 @@ public void testTransformerBuilderWorksCorrectly() throws Exception { final VectorSchemaRootTransformer transformer = builder.build(); - final Schema transformedSchema = new Schema(ImmutableList.of( - Field.nullable("FIELD_3_RENAMED", new ArrowType.Binary()), - Field.nullable("EMPTY_FIELD", new ArrowType.Bool()), - Field.nullable("FIELD_2_RENAMED", new ArrowType.Binary()), - Field.nullable("FIELD_1_RENAMED", new ArrowType.Binary()) - )); + final Schema transformedSchema = + new Schema( + ImmutableList.of( + Field.nullable("FIELD_3_RENAMED", new ArrowType.Binary()), + Field.nullable("EMPTY_FIELD", new ArrowType.Bool()), + Field.nullable("FIELD_2_RENAMED", new ArrowType.Binary()), + Field.nullable("FIELD_1_RENAMED", new ArrowType.Binary()))); try (final VectorSchemaRoot transformedRoot = createVectorSchemaRoot(transformedSchema)) { Assert.assertSame(transformedRoot, transformer.transform(clonedRoot, transformedRoot)); Assert.assertEquals(transformedSchema, transformedRoot.getSchema()); @@ -75,12 +72,9 @@ public void testTransformerBuilderWorksCorrectly() throws Exception { final int rowCount = originalRoot.getRowCount(); Assert.assertEquals(rowCount, transformedRoot.getRowCount()); - final VarBinaryVector originalField1 = - (VarBinaryVector) originalRoot.getVector("FIELD_1"); - final VarBinaryVector originalField2 = - (VarBinaryVector) originalRoot.getVector("FIELD_2"); - final VarBinaryVector originalField3 = - (VarBinaryVector) originalRoot.getVector("FIELD_3"); + final VarBinaryVector originalField1 = (VarBinaryVector) originalRoot.getVector("FIELD_1"); + final VarBinaryVector originalField2 = (VarBinaryVector) originalRoot.getVector("FIELD_2"); + final VarBinaryVector originalField3 = (VarBinaryVector) originalRoot.getVector("FIELD_3"); final VarBinaryVector transformedField1 = (VarBinaryVector) transformedRoot.getVector("FIELD_1_RENAMED"); @@ -111,9 +105,10 @@ private VectorSchemaRoot cloneVectorSchemaRoot(final VectorSchemaRoot originalRo } private VectorSchemaRoot createVectorSchemaRoot(final Schema schema) { - final List fieldVectors = schema.getFields().stream() - .map(field -> field.createVector(rootAllocator)) - .collect(Collectors.toList()); + final List fieldVectors = + schema.getFields().stream() + .map(field -> field.createVector(rootAllocator)) + .collect(Collectors.toList()); return new VectorSchemaRoot(fieldVectors); } } diff --git a/java/flight/flight-sql-jdbc-core/src/test/resources/logback.xml b/java/flight/flight-sql-jdbc-core/src/test/resources/logback.xml index ce66f8d82ac..d72538644f0 100644 --- a/java/flight/flight-sql-jdbc-core/src/test/resources/logback.xml +++ b/java/flight/flight-sql-jdbc-core/src/test/resources/logback.xml @@ -1,4 +1,4 @@ - + - - - - - arrow-flight - org.apache.arrow - 16.0.0-SNAPSHOT - ../pom.xml - - 4.0.0 - - flight-sql-jdbc-driver - Arrow Flight SQL JDBC Driver - A JDBC driver based on Arrow Flight SQL. - jar - https://arrow.apache.org - - - - - org.hamcrest - hamcrest-core - 1.3 - test - - - - org.apache.arrow - flight-sql-jdbc-core - runtime - - - - org.bouncycastle - bcpkix-jdk15on - 1.70 - runtime - - - - - org.apache.arrow - arrow-memory-core - runtime - - - - org.apache.arrow - flight-sql - runtime - - - - org.mockito - mockito-core - ${mockito.core.version} - test - - - - org.mockito - mockito-inline - ${mockito.inline.version} - test - - - - org.apache.arrow - flight-core - - - io.netty - netty-transport-native-kqueue - - - io.netty - netty-transport-native-epoll - - - runtime - - - - org.slf4j - slf4j-api - runtime - - - - io.netty - netty-common - runtime - - - - joda-time - joda-time - 2.12.6 - runtime - - - - org.apache.calcite.avatica - avatica - 1.24.0 - runtime - - - - com.google.protobuf - protobuf-java - runtime - - - - org.apache.arrow - arrow-vector - ${arrow.vector.classifier} - runtime - - - - com.google.guava - guava - runtime - - - - commons-io - commons-io - 2.7 - test - - - - - - - maven-surefire-plugin - - false - - ${project.basedir}/../../../testing/data - - - - - org.apache.maven.plugins - maven-shade-plugin - - - package - - shade - - - false - false - false - - - *:* - - - - - com. - cfjd.com. - - com.sun.** - - - - org. - cfjd.org. - - org.apache.arrow.driver.jdbc.** - org.slf4j.** - - org.apache.arrow.flight.name - org.apache.arrow.flight.version - org.apache.arrow.flight.jdbc-driver.name - org.apache.arrow.flight.jdbc-driver.version - - - - io. - cfjd.io. - - - - META-INF.native.libnetty_ - META-INF.native.libcfjd_netty_ - - - META-INF.native.netty_ - META-INF.native.cfjd_netty_ - - - - - - - - org.apache.calcite.avatica:* - - META-INF/services/java.sql.Driver - - - - *:* - - **/*.SF - **/*.RSA - **/*.DSA - META-INF/native/libio_grpc_netty* - META-INF/native/io_grpc_netty_shaded* - - - - - - - - - + license agreements. See the NOTICE file distributed with this work for additional + information regarding copyright ownership. The ASF licenses this file to + You under the Apache License, Version 2.0 (the "License"); you may not use + this file except in compliance with the License. You may obtain a copy of + the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required + by applicable law or agreed to in writing, software distributed under the + License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS + OF ANY KIND, either express or implied. See the License for the specific + language governing permissions and limitations under the License. --> + + 4.0.0 + + org.apache.arrow + arrow-flight + 16.0.0-SNAPSHOT + ../pom.xml + + + flight-sql-jdbc-driver + jar + Arrow Flight SQL JDBC Driver + A JDBC driver based on Arrow Flight SQL. + https://arrow.apache.org + + + + + org.hamcrest + hamcrest-core + 1.3 + test + + + + org.apache.arrow + flight-sql-jdbc-core + runtime + + + + org.bouncycastle + bcpkix-jdk15on + 1.70 + runtime + + + + + org.apache.arrow + arrow-memory-core + runtime + + + + org.apache.arrow + flight-sql + runtime + + + + org.mockito + mockito-core + ${mockito.core.version} + test + + + + org.mockito + mockito-inline + ${mockito.inline.version} + test + + + + org.apache.arrow + flight-core + runtime + + + io.netty + netty-transport-native-kqueue + + + io.netty + netty-transport-native-epoll + + + + + + org.slf4j + slf4j-api + runtime + + + + io.netty + netty-common + runtime + + + + joda-time + joda-time + 2.12.6 + runtime + + + + org.apache.calcite.avatica + avatica + 1.24.0 + runtime + + + + com.google.protobuf + protobuf-java + runtime + + + + org.apache.arrow + arrow-vector + ${arrow.vector.classifier} + runtime + + + + com.google.guava + guava + runtime + + + + commons-io + commons-io + 2.7 + test + + + + + + + maven-surefire-plugin + + false + + ${project.basedir}/../../../testing/data + + + + + org.apache.maven.plugins + maven-shade-plugin + + + + shade + + package + + false + false + false + + + *:* + + + + + com. + cfjd.com. + + com.sun.** + + + + org. + cfjd.org. + + org.apache.arrow.driver.jdbc.** + org.slf4j.** + + org.apache.arrow.flight.name + org.apache.arrow.flight.version + org.apache.arrow.flight.jdbc-driver.name + org.apache.arrow.flight.jdbc-driver.version + + + + io. + cfjd.io. + + + + META-INF.native.libnetty_ + META-INF.native.libcfjd_netty_ + + + META-INF.native.netty_ + META-INF.native.cfjd_netty_ + + + + + + + + org.apache.calcite.avatica:* + + META-INF/services/java.sql.Driver + + + + *:* + + **/*.SF + **/*.RSA + **/*.DSA + META-INF/native/libio_grpc_netty* + META-INF/native/io_grpc_netty_shaded* + + + + + + + + + diff --git a/java/flight/flight-sql/pom.xml b/java/flight/flight-sql/pom.xml index a0598f70b95..f74a8b514a4 100644 --- a/java/flight/flight-sql/pom.xml +++ b/java/flight/flight-sql/pom.xml @@ -12,16 +12,16 @@ 4.0.0 - arrow-flight org.apache.arrow + arrow-flight 16.0.0-SNAPSHOT ../pom.xml flight-sql + jar Arrow Flight SQL (Experimental)Contains utility classes to expose Flight SQL semantics for clients and servers over Arrow Flight - jar 1 diff --git a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/BasicFlightSqlProducer.java b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/BasicFlightSqlProducer.java index ea99191f28e..810257eff46 100644 --- a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/BasicFlightSqlProducer.java +++ b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/BasicFlightSqlProducer.java @@ -17,48 +17,44 @@ package org.apache.arrow.flight.sql; +import com.google.protobuf.Message; import java.util.List; - import org.apache.arrow.flight.FlightDescriptor; import org.apache.arrow.flight.FlightEndpoint; import org.apache.arrow.flight.FlightInfo; import org.apache.arrow.flight.sql.impl.FlightSql; import org.apache.arrow.vector.types.pojo.Schema; -import com.google.protobuf.Message; - -/** - * A {@link FlightSqlProducer} that implements getting FlightInfo for each metadata request. - */ +/** A {@link FlightSqlProducer} that implements getting FlightInfo for each metadata request. */ public abstract class BasicFlightSqlProducer extends NoOpFlightSqlProducer { @Override - public FlightInfo getFlightInfoSqlInfo(FlightSql.CommandGetSqlInfo request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoSqlInfo( + FlightSql.CommandGetSqlInfo request, CallContext context, FlightDescriptor descriptor) { return generateFlightInfo(request, descriptor, Schemas.GET_SQL_INFO_SCHEMA); } @Override - public FlightInfo getFlightInfoTypeInfo(FlightSql.CommandGetXdbcTypeInfo request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoTypeInfo( + FlightSql.CommandGetXdbcTypeInfo request, CallContext context, FlightDescriptor descriptor) { return generateFlightInfo(request, descriptor, Schemas.GET_TYPE_INFO_SCHEMA); } @Override - public FlightInfo getFlightInfoCatalogs(FlightSql.CommandGetCatalogs request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoCatalogs( + FlightSql.CommandGetCatalogs request, CallContext context, FlightDescriptor descriptor) { return generateFlightInfo(request, descriptor, Schemas.GET_CATALOGS_SCHEMA); } @Override - public FlightInfo getFlightInfoSchemas(FlightSql.CommandGetDbSchemas request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoSchemas( + FlightSql.CommandGetDbSchemas request, CallContext context, FlightDescriptor descriptor) { return generateFlightInfo(request, descriptor, Schemas.GET_SCHEMAS_SCHEMA); } @Override - public FlightInfo getFlightInfoTables(FlightSql.CommandGetTables request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoTables( + FlightSql.CommandGetTables request, CallContext context, FlightDescriptor descriptor) { if (request.getIncludeSchema()) { return generateFlightInfo(request, descriptor, Schemas.GET_TABLES_SCHEMA); } @@ -66,43 +62,46 @@ public FlightInfo getFlightInfoTables(FlightSql.CommandGetTables request, CallCo } @Override - public FlightInfo getFlightInfoTableTypes(FlightSql.CommandGetTableTypes request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoTableTypes( + FlightSql.CommandGetTableTypes request, CallContext context, FlightDescriptor descriptor) { return generateFlightInfo(request, descriptor, Schemas.GET_TABLE_TYPES_SCHEMA); } @Override - public FlightInfo getFlightInfoPrimaryKeys(FlightSql.CommandGetPrimaryKeys request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoPrimaryKeys( + FlightSql.CommandGetPrimaryKeys request, CallContext context, FlightDescriptor descriptor) { return generateFlightInfo(request, descriptor, Schemas.GET_PRIMARY_KEYS_SCHEMA); } @Override - public FlightInfo getFlightInfoExportedKeys(FlightSql.CommandGetExportedKeys request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoExportedKeys( + FlightSql.CommandGetExportedKeys request, CallContext context, FlightDescriptor descriptor) { return generateFlightInfo(request, descriptor, Schemas.GET_EXPORTED_KEYS_SCHEMA); } @Override - public FlightInfo getFlightInfoImportedKeys(FlightSql.CommandGetImportedKeys request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoImportedKeys( + FlightSql.CommandGetImportedKeys request, CallContext context, FlightDescriptor descriptor) { return generateFlightInfo(request, descriptor, Schemas.GET_IMPORTED_KEYS_SCHEMA); } @Override - public FlightInfo getFlightInfoCrossReference(FlightSql.CommandGetCrossReference request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoCrossReference( + FlightSql.CommandGetCrossReference request, + CallContext context, + FlightDescriptor descriptor) { return generateFlightInfo(request, descriptor, Schemas.GET_CROSS_REFERENCE_SCHEMA); } /** - * Return a list of FlightEndpoints for the given request and FlightDescriptor. This method should validate that - * the request is supported by this FlightSqlProducer. + * Return a list of FlightEndpoints for the given request and FlightDescriptor. This method should + * validate that the request is supported by this FlightSqlProducer. */ - protected abstract - List determineEndpoints(T request, FlightDescriptor flightDescriptor, Schema schema); + protected abstract List determineEndpoints( + T request, FlightDescriptor flightDescriptor, Schema schema); - protected FlightInfo generateFlightInfo(T request, FlightDescriptor descriptor, Schema schema) { + protected FlightInfo generateFlightInfo( + T request, FlightDescriptor descriptor, Schema schema) { final List endpoints = determineEndpoints(request, descriptor, schema); return new FlightInfo(schema, descriptor, endpoints, -1, -1); } diff --git a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/CancelListener.java b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/CancelListener.java index e7b645be747..b3ddeffa75c 100644 --- a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/CancelListener.java +++ b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/CancelListener.java @@ -17,12 +17,11 @@ package org.apache.arrow.flight.sql; +import com.google.protobuf.Any; import org.apache.arrow.flight.FlightProducer; import org.apache.arrow.flight.Result; import org.apache.arrow.flight.sql.impl.FlightSql; -import com.google.protobuf.Any; - /** Typed StreamListener for cancelQuery. */ @SuppressWarnings("deprecation") class CancelListener implements FlightProducer.StreamListener { @@ -34,9 +33,8 @@ class CancelListener implements FlightProducer.StreamListener { @Override public void onNext(CancelResult val) { - FlightSql.ActionCancelQueryResult result = FlightSql.ActionCancelQueryResult.newBuilder() - .setResult(val.toProtocol()) - .build(); + FlightSql.ActionCancelQueryResult result = + FlightSql.ActionCancelQueryResult.newBuilder().setResult(val.toProtocol()).build(); listener.onNext(new Result(Any.pack(result).toByteArray())); } diff --git a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlClient.java b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlClient.java index 93d933f00f3..80ea1029899 100644 --- a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlClient.java +++ b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlClient.java @@ -45,6 +45,9 @@ import static org.apache.arrow.flight.sql.impl.FlightSql.DoPutUpdateResult; import static org.apache.arrow.flight.sql.impl.FlightSql.SqlInfo; +import com.google.protobuf.Any; +import com.google.protobuf.ByteString; +import com.google.protobuf.InvalidProtocolBufferException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.channels.Channels; @@ -55,7 +58,6 @@ import java.util.Objects; import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; - import org.apache.arrow.flight.Action; import org.apache.arrow.flight.CallOption; import org.apache.arrow.flight.CallStatus; @@ -83,13 +85,7 @@ import org.apache.arrow.vector.ipc.message.MessageSerializer; import org.apache.arrow.vector.types.pojo.Schema; -import com.google.protobuf.Any; -import com.google.protobuf.ByteString; -import com.google.protobuf.InvalidProtocolBufferException; - -/** - * Flight client with Flight SQL semantics. - */ +/** Flight client with Flight SQL semantics. */ public class FlightSqlClient implements AutoCloseable { private final FlightClient client; @@ -100,7 +96,7 @@ public FlightSqlClient(final FlightClient client) { /** * Execute a query on the server. * - * @param query The query to execute. + * @param query The query to execute. * @param options RPC-layer hints for this call. * @return a FlightInfo object representing the stream(s) to fetch. */ @@ -116,12 +112,15 @@ public FlightInfo execute(final String query, final CallOption... options) { * @param options RPC-layer hints for this call. * @return a FlightInfo object representing the stream(s) to fetch. */ - public FlightInfo execute(final String query, Transaction transaction, final CallOption... options) { - final CommandStatementQuery.Builder builder = CommandStatementQuery.newBuilder().setQuery(query); + public FlightInfo execute( + final String query, Transaction transaction, final CallOption... options) { + final CommandStatementQuery.Builder builder = + CommandStatementQuery.newBuilder().setQuery(query); if (transaction != null) { builder.setTransactionId(ByteString.copyFrom(transaction.getTransactionId())); } - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getInfo(descriptor, options); } @@ -144,61 +143,67 @@ public FlightInfo executeSubstrait(SubstraitPlan plan, CallOption... options) { * @param options RPC-layer hints for this call. * @return a FlightInfo object representing the stream(s) to fetch. */ - public FlightInfo executeSubstrait(SubstraitPlan plan, Transaction transaction, CallOption... options) { - final CommandStatementSubstraitPlan.Builder builder = CommandStatementSubstraitPlan.newBuilder(); - builder.getPlanBuilder().setPlan(ByteString.copyFrom(plan.getPlan())).setVersion(plan.getVersion()); + public FlightInfo executeSubstrait( + SubstraitPlan plan, Transaction transaction, CallOption... options) { + final CommandStatementSubstraitPlan.Builder builder = + CommandStatementSubstraitPlan.newBuilder(); + builder + .getPlanBuilder() + .setPlan(ByteString.copyFrom(plan.getPlan())) + .setVersion(plan.getVersion()); if (transaction != null) { builder.setTransactionId(ByteString.copyFrom(transaction.getTransactionId())); } - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getInfo(descriptor, options); } - /** - * Get the schema of the result set of a query. - */ - public SchemaResult getExecuteSchema(String query, Transaction transaction, CallOption... options) { + /** Get the schema of the result set of a query. */ + public SchemaResult getExecuteSchema( + String query, Transaction transaction, CallOption... options) { final CommandStatementQuery.Builder builder = CommandStatementQuery.newBuilder(); builder.setQuery(query); if (transaction != null) { builder.setTransactionId(ByteString.copyFrom(transaction.getTransactionId())); } - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getSchema(descriptor, options); } - /** - * Get the schema of the result set of a query. - */ + /** Get the schema of the result set of a query. */ public SchemaResult getExecuteSchema(String query, CallOption... options) { - return getExecuteSchema(query, /*transaction*/null, options); + return getExecuteSchema(query, /*transaction*/ null, options); } - /** - * Get the schema of the result set of a Substrait plan. - */ - public SchemaResult getExecuteSubstraitSchema(SubstraitPlan plan, Transaction transaction, - final CallOption... options) { - final CommandStatementSubstraitPlan.Builder builder = CommandStatementSubstraitPlan.newBuilder(); - builder.getPlanBuilder().setPlan(ByteString.copyFrom(plan.getPlan())).setVersion(plan.getVersion()); + /** Get the schema of the result set of a Substrait plan. */ + public SchemaResult getExecuteSubstraitSchema( + SubstraitPlan plan, Transaction transaction, final CallOption... options) { + final CommandStatementSubstraitPlan.Builder builder = + CommandStatementSubstraitPlan.newBuilder(); + builder + .getPlanBuilder() + .setPlan(ByteString.copyFrom(plan.getPlan())) + .setVersion(plan.getVersion()); if (transaction != null) { builder.setTransactionId(ByteString.copyFrom(transaction.getTransactionId())); } - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getSchema(descriptor, options); } - /** - * Get the schema of the result set of a Substrait plan. - */ - public SchemaResult getExecuteSubstraitSchema(SubstraitPlan substraitPlan, final CallOption... options) { - return getExecuteSubstraitSchema(substraitPlan, /*transaction*/null, options); + /** Get the schema of the result set of a Substrait plan. */ + public SchemaResult getExecuteSubstraitSchema( + SubstraitPlan substraitPlan, final CallOption... options) { + return getExecuteSubstraitSchema(substraitPlan, /*transaction*/ null, options); } /** * Execute an update query on the server. * - * @param query The query to execute. + * @param query The query to execute. * @param options RPC-layer hints for this call. * @return the number of rows affected. */ @@ -209,24 +214,27 @@ public long executeUpdate(final String query, final CallOption... options) { /** * Execute an update query on the server. * - * @param query The query to execute. + * @param query The query to execute. * @param transaction The transaction that this query is part of. * @param options RPC-layer hints for this call. * @return the number of rows affected. */ - public long executeUpdate(final String query, Transaction transaction, final CallOption... options) { - final CommandStatementUpdate.Builder builder = CommandStatementUpdate.newBuilder().setQuery(query); + public long executeUpdate( + final String query, Transaction transaction, final CallOption... options) { + final CommandStatementUpdate.Builder builder = + CommandStatementUpdate.newBuilder().setQuery(query); if (transaction != null) { builder.setTransactionId(ByteString.copyFrom(transaction.getTransactionId())); } - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); try (final SyncPutListener putListener = new SyncPutListener()) { final FlightClient.ClientStreamListener listener = client.startPut(descriptor, VectorSchemaRoot.of(), putListener, options); try (final PutResult result = putListener.read()) { - final DoPutUpdateResult doPutUpdateResult = DoPutUpdateResult.parseFrom( - result.getApplicationMetadata().nioBuffer()); + final DoPutUpdateResult doPutUpdateResult = + DoPutUpdateResult.parseFrom(result.getApplicationMetadata().nioBuffer()); return doPutUpdateResult.getRecordCount(); } finally { listener.getResult(); @@ -257,20 +265,26 @@ public long executeSubstraitUpdate(SubstraitPlan plan, CallOption... options) { * @param options RPC-layer hints for this call. * @return the number of rows affected. */ - public long executeSubstraitUpdate(SubstraitPlan plan, Transaction transaction, CallOption... options) { - final CommandStatementSubstraitPlan.Builder builder = CommandStatementSubstraitPlan.newBuilder(); - builder.getPlanBuilder().setPlan(ByteString.copyFrom(plan.getPlan())).setVersion(plan.getVersion()); + public long executeSubstraitUpdate( + SubstraitPlan plan, Transaction transaction, CallOption... options) { + final CommandStatementSubstraitPlan.Builder builder = + CommandStatementSubstraitPlan.newBuilder(); + builder + .getPlanBuilder() + .setPlan(ByteString.copyFrom(plan.getPlan())) + .setVersion(plan.getVersion()); if (transaction != null) { builder.setTransactionId(ByteString.copyFrom(transaction.getTransactionId())); } - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); try (final SyncPutListener putListener = new SyncPutListener()) { final FlightClient.ClientStreamListener listener = client.startPut(descriptor, VectorSchemaRoot.of(), putListener, options); try (final PutResult result = putListener.read()) { - final DoPutUpdateResult doPutUpdateResult = DoPutUpdateResult.parseFrom( - result.getApplicationMetadata().nioBuffer()); + final DoPutUpdateResult doPutUpdateResult = + DoPutUpdateResult.parseFrom(result.getApplicationMetadata().nioBuffer()); return doPutUpdateResult.getRecordCount(); } finally { listener.getResult(); @@ -290,7 +304,8 @@ public long executeSubstraitUpdate(SubstraitPlan plan, Transaction transaction, */ public FlightInfo getCatalogs(final CallOption... options) { final CommandGetCatalogs.Builder builder = CommandGetCatalogs.newBuilder(); - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getInfo(descriptor, options); } @@ -308,12 +323,13 @@ public SchemaResult getCatalogsSchema(final CallOption... options) { /** * Request a list of schemas. * - * @param catalog The catalog. + * @param catalog The catalog. * @param dbSchemaFilterPattern The schema filter pattern. - * @param options RPC-layer hints for this call. + * @param options RPC-layer hints for this call. * @return a FlightInfo object representing the stream(s) to fetch. */ - public FlightInfo getSchemas(final String catalog, final String dbSchemaFilterPattern, final CallOption... options) { + public FlightInfo getSchemas( + final String catalog, final String dbSchemaFilterPattern, final CallOption... options) { final CommandGetDbSchemas.Builder builder = CommandGetDbSchemas.newBuilder(); if (catalog != null) { @@ -324,7 +340,8 @@ public FlightInfo getSchemas(final String catalog, final String dbSchemaFilterPa builder.setDbSchemaFilterPattern(dbSchemaFilterPattern); } - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getInfo(descriptor, options); } @@ -343,7 +360,7 @@ public SchemaResult getSchemasSchema(final CallOption... options) { * Get schema for a stream. * * @param descriptor The descriptor for the stream. - * @param options RPC-layer hints for this call. + * @param options RPC-layer hints for this call. */ public SchemaResult getSchema(FlightDescriptor descriptor, CallOption... options) { return client.getSchema(descriptor, options); @@ -352,7 +369,7 @@ public SchemaResult getSchema(FlightDescriptor descriptor, CallOption... options /** * Retrieve a stream from the server. * - * @param ticket The ticket granting access to the data stream. + * @param ticket The ticket granting access to the data stream. * @param options RPC-layer hints for this call. */ public FlightStream getStream(Ticket ticket, CallOption... options) { @@ -372,7 +389,7 @@ public FlightInfo getSqlInfo(final SqlInfo... info) { /** * Request a set of Flight SQL metadata. * - * @param info The set of metadata to retrieve. None to retrieve all metadata. + * @param info The set of metadata to retrieve. None to retrieve all metadata. * @param options RPC-layer hints for this call. * @return a FlightInfo object representing the stream(s) to fetch. */ @@ -382,11 +399,10 @@ public FlightInfo getSqlInfo(final SqlInfo[] info, final CallOption... options) } /** - * Request a set of Flight SQL metadata. - * Use this method if you would like to retrieve custom metadata, where the custom metadata key values start - * from 10_000. + * Request a set of Flight SQL metadata. Use this method if you would like to retrieve custom + * metadata, where the custom metadata key values start from 10_000. * - * @param info The set of metadata to retrieve. None to retrieve all metadata. + * @param info The set of metadata to retrieve. None to retrieve all metadata. * @param options RPC-layer hints for this call. * @return a FlightInfo object representing the stream(s) to fetch. */ @@ -395,18 +411,18 @@ public FlightInfo getSqlInfo(final int[] info, final CallOption... options) { } /** - * Request a set of Flight SQL metadata. - * Use this method if you would like to retrieve custom metadata, where the custom metadata key values start - * from 10_000. + * Request a set of Flight SQL metadata. Use this method if you would like to retrieve custom + * metadata, where the custom metadata key values start from 10_000. * - * @param info The set of metadata to retrieve. None to retrieve all metadata. + * @param info The set of metadata to retrieve. None to retrieve all metadata. * @param options RPC-layer hints for this call. * @return a FlightInfo object representing the stream(s) to fetch. */ public FlightInfo getSqlInfo(final Iterable info, final CallOption... options) { final CommandGetSqlInfo.Builder builder = CommandGetSqlInfo.newBuilder(); builder.addAllInfo(info); - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getInfo(descriptor, options); } @@ -422,11 +438,10 @@ public SchemaResult getSqlInfoSchema(final CallOption... options) { } /** - * Request the information about the data types supported related to - * a filter data type. + * Request the information about the data types supported related to a filter data type. * - * @param dataType the data type to be used as filter. - * @param options RPC-layer hints for this call. + * @param dataType the data type to be used as filter. + * @param options RPC-layer hints for this call. * @return a FlightInfo object representing the stream(s) to fetch. */ public FlightInfo getXdbcTypeInfo(final int dataType, final CallOption... options) { @@ -434,20 +449,22 @@ public FlightInfo getXdbcTypeInfo(final int dataType, final CallOption... option builder.setDataType(dataType); - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getInfo(descriptor, options); } /** * Request the information about all the data types supported. * - * @param options RPC-layer hints for this call. + * @param options RPC-layer hints for this call. * @return a FlightInfo object representing the stream(s) to fetch. */ public FlightInfo getXdbcTypeInfo(final CallOption... options) { final CommandGetXdbcTypeInfo.Builder builder = CommandGetXdbcTypeInfo.newBuilder(); - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getInfo(descriptor, options); } @@ -465,17 +482,21 @@ public SchemaResult getXdbcTypeInfoSchema(final CallOption... options) { /** * Request a list of tables. * - * @param catalog The catalog. + * @param catalog The catalog. * @param dbSchemaFilterPattern The schema filter pattern. - * @param tableFilterPattern The table filter pattern. - * @param tableTypes The table types to include. - * @param includeSchema True to include the schema upon return, false to not include the schema. - * @param options RPC-layer hints for this call. + * @param tableFilterPattern The table filter pattern. + * @param tableTypes The table types to include. + * @param includeSchema True to include the schema upon return, false to not include the schema. + * @param options RPC-layer hints for this call. * @return a FlightInfo object representing the stream(s) to fetch. */ - public FlightInfo getTables(final String catalog, final String dbSchemaFilterPattern, - final String tableFilterPattern, final List tableTypes, - final boolean includeSchema, final CallOption... options) { + public FlightInfo getTables( + final String catalog, + final String dbSchemaFilterPattern, + final String tableFilterPattern, + final List tableTypes, + final boolean includeSchema, + final CallOption... options) { final CommandGetTables.Builder builder = CommandGetTables.newBuilder(); if (catalog != null) { @@ -495,18 +516,21 @@ public FlightInfo getTables(final String catalog, final String dbSchemaFilterPat } builder.setIncludeSchema(includeSchema); - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getInfo(descriptor, options); } /** - * Get the schema of {@link #getTables(String, String, String, List, boolean, CallOption...)} from the server. + * Get the schema of {@link #getTables(String, String, String, List, boolean, CallOption...)} from + * the server. * - *

Should be identical to {@link FlightSqlProducer.Schemas#GET_TABLES_SCHEMA} or - * {@link FlightSqlProducer.Schemas#GET_TABLES_SCHEMA_NO_SCHEMA}. + *

Should be identical to {@link FlightSqlProducer.Schemas#GET_TABLES_SCHEMA} or {@link + * FlightSqlProducer.Schemas#GET_TABLES_SCHEMA_NO_SCHEMA}. */ public SchemaResult getTablesSchema(boolean includeSchema, final CallOption... options) { - final CommandGetTables command = CommandGetTables.newBuilder().setIncludeSchema(includeSchema).build(); + final CommandGetTables command = + CommandGetTables.newBuilder().setIncludeSchema(includeSchema).build(); final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(command).toByteArray()); return client.getSchema(descriptor, options); } @@ -514,8 +538,8 @@ public SchemaResult getTablesSchema(boolean includeSchema, final CallOption... o /** * Request the primary keys for a table. * - * @param tableRef An object which hold info about catalog, dbSchema and table. - * @param options RPC-layer hints for this call. + * @param tableRef An object which hold info about catalog, dbSchema and table. + * @param options RPC-layer hints for this call. * @return a FlightInfo object representing the stream(s) to fetch. */ public FlightInfo getPrimaryKeys(final TableRef tableRef, final CallOption... options) { @@ -532,7 +556,8 @@ public FlightInfo getPrimaryKeys(final TableRef tableRef, final CallOption... op Objects.requireNonNull(tableRef.getTable()); builder.setTable(tableRef.getTable()); - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getInfo(descriptor, options); } @@ -548,10 +573,11 @@ public SchemaResult getPrimaryKeysSchema(final CallOption... options) { } /** - * Retrieves a description about the foreign key columns that reference the primary key columns of the given table. + * Retrieves a description about the foreign key columns that reference the primary key columns of + * the given table. * - * @param tableRef An object which hold info about catalog, dbSchema and table. - * @param options RPC-layer hints for this call. + * @param tableRef An object which hold info about catalog, dbSchema and table. + * @param options RPC-layer hints for this call. * @return a FlightInfo object representing the stream(s) to fetch. */ public FlightInfo getExportedKeys(final TableRef tableRef, final CallOption... options) { @@ -570,7 +596,8 @@ public FlightInfo getExportedKeys(final TableRef tableRef, final CallOption... o Objects.requireNonNull(tableRef.getTable()); builder.setTable(tableRef.getTable()); - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getInfo(descriptor, options); } @@ -588,12 +615,11 @@ public SchemaResult getExportedKeysSchema(final CallOption... options) { /** * Retrieves the foreign key columns for the given table. * - * @param tableRef An object which hold info about catalog, dbSchema and table. - * @param options RPC-layer hints for this call. + * @param tableRef An object which hold info about catalog, dbSchema and table. + * @param options RPC-layer hints for this call. * @return a FlightInfo object representing the stream(s) to fetch. */ - public FlightInfo getImportedKeys(final TableRef tableRef, - final CallOption... options) { + public FlightInfo getImportedKeys(final TableRef tableRef, final CallOption... options) { Objects.requireNonNull(tableRef.getTable(), "Table cannot be null."); final CommandGetImportedKeys.Builder builder = CommandGetImportedKeys.newBuilder(); @@ -609,7 +635,8 @@ public FlightInfo getImportedKeys(final TableRef tableRef, Objects.requireNonNull(tableRef.getTable()); builder.setTable(tableRef.getTable()); - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getInfo(descriptor, options); } @@ -625,16 +652,18 @@ public SchemaResult getImportedKeysSchema(final CallOption... options) { } /** - * Retrieves a description of the foreign key columns that reference the given table's - * primary key columns (the foreign keys exported by a table). + * Retrieves a description of the foreign key columns that reference the given table's primary key + * columns (the foreign keys exported by a table). * - * @param pkTableRef An object which hold info about catalog, dbSchema and table from a primary table. - * @param fkTableRef An object which hold info about catalog, dbSchema and table from a foreign table. - * @param options RPC-layer hints for this call. + * @param pkTableRef An object which hold info about catalog, dbSchema and table from a primary + * table. + * @param fkTableRef An object which hold info about catalog, dbSchema and table from a foreign + * table. + * @param options RPC-layer hints for this call. * @return a FlightInfo object representing the stream(s) to fetch. */ - public FlightInfo getCrossReference(final TableRef pkTableRef, - final TableRef fkTableRef, final CallOption... options) { + public FlightInfo getCrossReference( + final TableRef pkTableRef, final TableRef fkTableRef, final CallOption... options) { Objects.requireNonNull(pkTableRef.getTable(), "Parent Table cannot be null."); Objects.requireNonNull(fkTableRef.getTable(), "Foreign Table cannot be null."); @@ -659,12 +688,14 @@ public FlightInfo getCrossReference(final TableRef pkTableRef, builder.setPkTable(pkTableRef.getTable()); builder.setFkTable(fkTableRef.getTable()); - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getInfo(descriptor, options); } /** - * Get the schema of {@link #getCrossReference(TableRef, TableRef, CallOption...)} from the server. + * Get the schema of {@link #getCrossReference(TableRef, TableRef, CallOption...)} from the + * server. * *

Should be identical to {@link FlightSqlProducer.Schemas#GET_CROSS_REFERENCE_SCHEMA}. */ @@ -682,7 +713,8 @@ public SchemaResult getCrossReferenceSchema(final CallOption... options) { */ public FlightInfo getTableTypes(final CallOption... options) { final CommandGetTableTypes.Builder builder = CommandGetTableTypes.newBuilder(); - final FlightDescriptor descriptor = FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command(Any.pack(builder.build()).toByteArray()); return client.getInfo(descriptor, options); } @@ -700,7 +732,7 @@ public SchemaResult getTableTypesSchema(final CallOption... options) { /** * Create a prepared statement for a SQL query on the server. * - * @param query The query to prepare. + * @param query The query to prepare. * @param options RPC-layer hints for this call. * @return The representation of the prepared statement which exists on the server. */ @@ -722,7 +754,8 @@ public PreparedStatement prepare(String query, Transaction transaction, CallOpti if (transaction != null) { builder.setTransactionId(ByteString.copyFrom(transaction.getTransactionId())); } - return new PreparedStatement(client, + return new PreparedStatement( + client, new Action( FlightSqlUtils.FLIGHT_SQL_CREATE_PREPARED_STATEMENT.getType(), Any.pack(builder.build()).toByteArray()), @@ -732,7 +765,7 @@ public PreparedStatement prepare(String query, Transaction transaction, CallOpti /** * Create a prepared statement for a Substrait plan on the server. * - * @param plan The query to prepare. + * @param plan The query to prepare. * @param options RPC-layer hints for this call. * @return The representation of the prepared statement which exists on the server. */ @@ -748,14 +781,19 @@ public PreparedStatement prepare(SubstraitPlan plan, CallOption... options) { * @param options RPC-layer hints for this call. * @return The representation of the prepared statement which exists on the server. */ - public PreparedStatement prepare(SubstraitPlan plan, Transaction transaction, CallOption... options) { + public PreparedStatement prepare( + SubstraitPlan plan, Transaction transaction, CallOption... options) { ActionCreatePreparedSubstraitPlanRequest.Builder builder = ActionCreatePreparedSubstraitPlanRequest.newBuilder(); - builder.getPlanBuilder().setPlan(ByteString.copyFrom(plan.getPlan())).setVersion(plan.getVersion()); + builder + .getPlanBuilder() + .setPlan(ByteString.copyFrom(plan.getPlan())) + .setVersion(plan.getVersion()); if (transaction != null) { builder.setTransactionId(ByteString.copyFrom(transaction.getTransactionId())); } - return new PreparedStatement(client, + return new PreparedStatement( + client, new Action( FlightSqlUtils.FLIGHT_SQL_CREATE_PREPARED_SUBSTRAIT_PLAN.getType(), Any.pack(builder.build()).toByteArray()), @@ -764,95 +802,114 @@ public PreparedStatement prepare(SubstraitPlan plan, Transaction transaction, Ca /** Begin a transaction. */ public Transaction beginTransaction(CallOption... options) { - final Action action = new Action( - FlightSqlUtils.FLIGHT_SQL_BEGIN_TRANSACTION.getType(), - Any.pack(ActionBeginTransactionRequest.getDefaultInstance()).toByteArray()); + final Action action = + new Action( + FlightSqlUtils.FLIGHT_SQL_BEGIN_TRANSACTION.getType(), + Any.pack(ActionBeginTransactionRequest.getDefaultInstance()).toByteArray()); final Iterator preparedStatementResults = client.doAction(action, options); - final ActionBeginTransactionResult result = FlightSqlUtils.unpackAndParseOrThrow( - preparedStatementResults.next().getBody(), - ActionBeginTransactionResult.class); - preparedStatementResults.forEachRemaining((ignored) -> { }); + final ActionBeginTransactionResult result = + FlightSqlUtils.unpackAndParseOrThrow( + preparedStatementResults.next().getBody(), ActionBeginTransactionResult.class); + preparedStatementResults.forEachRemaining((ignored) -> {}); if (result.getTransactionId().isEmpty()) { - throw CallStatus.INTERNAL.withDescription("Server returned an empty transaction ID").toRuntimeException(); + throw CallStatus.INTERNAL + .withDescription("Server returned an empty transaction ID") + .toRuntimeException(); } return new Transaction(result.getTransactionId().toByteArray()); } /** Create a savepoint within a transaction. */ public Savepoint beginSavepoint(Transaction transaction, String name, CallOption... options) { - Preconditions.checkArgument(transaction.getTransactionId().length != 0, "Transaction must be initialized"); - ActionBeginSavepointRequest request = ActionBeginSavepointRequest.newBuilder() - .setTransactionId(ByteString.copyFrom(transaction.getTransactionId())) - .setName(name) - .build(); - final Action action = new Action( - FlightSqlUtils.FLIGHT_SQL_BEGIN_SAVEPOINT.getType(), - Any.pack(request).toByteArray()); + Preconditions.checkArgument( + transaction.getTransactionId().length != 0, "Transaction must be initialized"); + ActionBeginSavepointRequest request = + ActionBeginSavepointRequest.newBuilder() + .setTransactionId(ByteString.copyFrom(transaction.getTransactionId())) + .setName(name) + .build(); + final Action action = + new Action( + FlightSqlUtils.FLIGHT_SQL_BEGIN_SAVEPOINT.getType(), Any.pack(request).toByteArray()); final Iterator preparedStatementResults = client.doAction(action, options); - final ActionBeginSavepointResult result = FlightSqlUtils.unpackAndParseOrThrow( - preparedStatementResults.next().getBody(), - ActionBeginSavepointResult.class); - preparedStatementResults.forEachRemaining((ignored) -> { }); + final ActionBeginSavepointResult result = + FlightSqlUtils.unpackAndParseOrThrow( + preparedStatementResults.next().getBody(), ActionBeginSavepointResult.class); + preparedStatementResults.forEachRemaining((ignored) -> {}); if (result.getSavepointId().isEmpty()) { - throw CallStatus.INTERNAL.withDescription("Server returned an empty transaction ID").toRuntimeException(); + throw CallStatus.INTERNAL + .withDescription("Server returned an empty transaction ID") + .toRuntimeException(); } return new Savepoint(result.getSavepointId().toByteArray()); } /** Commit a transaction. */ public void commit(Transaction transaction, CallOption... options) { - Preconditions.checkArgument(transaction.getTransactionId().length != 0, "Transaction must be initialized"); - ActionEndTransactionRequest request = ActionEndTransactionRequest.newBuilder() - .setTransactionId(ByteString.copyFrom(transaction.getTransactionId())) - .setActionValue(ActionEndTransactionRequest.EndTransaction.END_TRANSACTION_COMMIT.getNumber()) - .build(); - final Action action = new Action( - FlightSqlUtils.FLIGHT_SQL_END_TRANSACTION.getType(), - Any.pack(request).toByteArray()); + Preconditions.checkArgument( + transaction.getTransactionId().length != 0, "Transaction must be initialized"); + ActionEndTransactionRequest request = + ActionEndTransactionRequest.newBuilder() + .setTransactionId(ByteString.copyFrom(transaction.getTransactionId())) + .setActionValue( + ActionEndTransactionRequest.EndTransaction.END_TRANSACTION_COMMIT.getNumber()) + .build(); + final Action action = + new Action( + FlightSqlUtils.FLIGHT_SQL_END_TRANSACTION.getType(), Any.pack(request).toByteArray()); final Iterator preparedStatementResults = client.doAction(action, options); - preparedStatementResults.forEachRemaining((ignored) -> { }); + preparedStatementResults.forEachRemaining((ignored) -> {}); } /** Release a savepoint. */ public void release(Savepoint savepoint, CallOption... options) { - Preconditions.checkArgument(savepoint.getSavepointId().length != 0, "Savepoint must be initialized"); - ActionEndSavepointRequest request = ActionEndSavepointRequest.newBuilder() - .setSavepointId(ByteString.copyFrom(savepoint.getSavepointId())) - .setActionValue(ActionEndSavepointRequest.EndSavepoint.END_SAVEPOINT_RELEASE.getNumber()) - .build(); - final Action action = new Action( - FlightSqlUtils.FLIGHT_SQL_END_SAVEPOINT.getType(), - Any.pack(request).toByteArray()); + Preconditions.checkArgument( + savepoint.getSavepointId().length != 0, "Savepoint must be initialized"); + ActionEndSavepointRequest request = + ActionEndSavepointRequest.newBuilder() + .setSavepointId(ByteString.copyFrom(savepoint.getSavepointId())) + .setActionValue( + ActionEndSavepointRequest.EndSavepoint.END_SAVEPOINT_RELEASE.getNumber()) + .build(); + final Action action = + new Action( + FlightSqlUtils.FLIGHT_SQL_END_SAVEPOINT.getType(), Any.pack(request).toByteArray()); final Iterator preparedStatementResults = client.doAction(action, options); - preparedStatementResults.forEachRemaining((ignored) -> { }); + preparedStatementResults.forEachRemaining((ignored) -> {}); } /** Rollback a transaction. */ public void rollback(Transaction transaction, CallOption... options) { - Preconditions.checkArgument(transaction.getTransactionId().length != 0, "Transaction must be initialized"); - ActionEndTransactionRequest request = ActionEndTransactionRequest.newBuilder() - .setTransactionId(ByteString.copyFrom(transaction.getTransactionId())) - .setActionValue(ActionEndTransactionRequest.EndTransaction.END_TRANSACTION_ROLLBACK.getNumber()) - .build(); - final Action action = new Action( - FlightSqlUtils.FLIGHT_SQL_END_TRANSACTION.getType(), - Any.pack(request).toByteArray()); + Preconditions.checkArgument( + transaction.getTransactionId().length != 0, "Transaction must be initialized"); + ActionEndTransactionRequest request = + ActionEndTransactionRequest.newBuilder() + .setTransactionId(ByteString.copyFrom(transaction.getTransactionId())) + .setActionValue( + ActionEndTransactionRequest.EndTransaction.END_TRANSACTION_ROLLBACK.getNumber()) + .build(); + final Action action = + new Action( + FlightSqlUtils.FLIGHT_SQL_END_TRANSACTION.getType(), Any.pack(request).toByteArray()); final Iterator preparedStatementResults = client.doAction(action, options); - preparedStatementResults.forEachRemaining((ignored) -> { }); + preparedStatementResults.forEachRemaining((ignored) -> {}); } /** Rollback to a savepoint. */ public void rollback(Savepoint savepoint, CallOption... options) { - Preconditions.checkArgument(savepoint.getSavepointId().length != 0, "Savepoint must be initialized"); - ActionEndSavepointRequest request = ActionEndSavepointRequest.newBuilder() - .setSavepointId(ByteString.copyFrom(savepoint.getSavepointId())) - .setActionValue(ActionEndSavepointRequest.EndSavepoint.END_SAVEPOINT_RELEASE.getNumber()) - .build(); - final Action action = new Action( - FlightSqlUtils.FLIGHT_SQL_END_SAVEPOINT.getType(), - Any.pack(request).toByteArray()); + Preconditions.checkArgument( + savepoint.getSavepointId().length != 0, "Savepoint must be initialized"); + ActionEndSavepointRequest request = + ActionEndSavepointRequest.newBuilder() + .setSavepointId(ByteString.copyFrom(savepoint.getSavepointId())) + .setActionValue( + ActionEndSavepointRequest.EndSavepoint.END_SAVEPOINT_RELEASE.getNumber()) + .build(); + final Action action = + new Action( + FlightSqlUtils.FLIGHT_SQL_END_SAVEPOINT.getType(), Any.pack(request).toByteArray()); final Iterator preparedStatementResults = client.doAction(action, options); - preparedStatementResults.forEachRemaining((ignored) -> { }); + preparedStatementResults.forEachRemaining((ignored) -> {}); } /** @@ -862,35 +919,36 @@ public void rollback(Savepoint savepoint, CallOption... options) { * @param options Call options. * @return The server response. */ - public CancelFlightInfoResult cancelFlightInfo(CancelFlightInfoRequest request, CallOption... options) { + public CancelFlightInfoResult cancelFlightInfo( + CancelFlightInfoRequest request, CallOption... options) { return client.cancelFlightInfo(request, options); } /** * Explicitly cancel a running query. - *

- * This lets a single client explicitly cancel work, no matter how many clients - * are involved/whether the query is distributed or not, given server support. - * The transaction/statement is not rolled back; it is the application's job to - * commit or rollback as appropriate. This only indicates the client no longer - * wishes to read the remainder of the query results or continue submitting - * data. + * + *

This lets a single client explicitly cancel work, no matter how many clients are + * involved/whether the query is distributed or not, given server support. The + * transaction/statement is not rolled back; it is the application's job to commit or rollback as + * appropriate. This only indicates the client no longer wishes to read the remainder of the query + * results or continue submitting data. * * @deprecated Prefer {@link #cancelFlightInfo}. */ @Deprecated public CancelResult cancelQuery(FlightInfo info, CallOption... options) { - ActionCancelQueryRequest request = ActionCancelQueryRequest.newBuilder() - .setInfo(ByteString.copyFrom(info.serialize())) - .build(); - final Action action = new Action( - FlightSqlUtils.FLIGHT_SQL_CANCEL_QUERY.getType(), - Any.pack(request).toByteArray()); + ActionCancelQueryRequest request = + ActionCancelQueryRequest.newBuilder() + .setInfo(ByteString.copyFrom(info.serialize())) + .build(); + final Action action = + new Action( + FlightSqlUtils.FLIGHT_SQL_CANCEL_QUERY.getType(), Any.pack(request).toByteArray()); final Iterator preparedStatementResults = client.doAction(action, options); - final ActionCancelQueryResult result = FlightSqlUtils.unpackAndParseOrThrow( - preparedStatementResults.next().getBody(), - ActionCancelQueryResult.class); - preparedStatementResults.forEachRemaining((ignored) -> { }); + final ActionCancelQueryResult result = + FlightSqlUtils.unpackAndParseOrThrow( + preparedStatementResults.next().getBody(), ActionCancelQueryResult.class); + preparedStatementResults.forEachRemaining((ignored) -> {}); switch (result.getResult()) { case CANCEL_RESULT_UNSPECIFIED: return CancelResult.UNSPECIFIED; @@ -902,7 +960,9 @@ public CancelResult cancelQuery(FlightInfo info, CallOption... options) { return CancelResult.NOT_CANCELLABLE; case UNRECOGNIZED: default: - throw CallStatus.INTERNAL.withDescription("Unknown result: " + result.getResult()).toRuntimeException(); + throw CallStatus.INTERNAL + .withDescription("Unknown result: " + result.getResult()) + .toRuntimeException(); } } @@ -913,7 +973,8 @@ public CancelResult cancelQuery(FlightInfo info, CallOption... options) { * @param options Call options. * @return The new endpoint with an updated expiration time. */ - public FlightEndpoint renewFlightEndpoint(RenewFlightEndpointRequest request, CallOption... options) { + public FlightEndpoint renewFlightEndpoint( + RenewFlightEndpointRequest request, CallOption... options) { return client.renewFlightEndpoint(request, options); } @@ -922,9 +983,7 @@ public void close() throws Exception { AutoCloseables.close(client); } - /** - * Helper class to encapsulate Flight SQL prepared statement logic. - */ + /** Helper class to encapsulate Flight SQL prepared statement logic. */ public static class PreparedStatement implements AutoCloseable { private final FlightClient client; private final ActionCreatePreparedStatementResult preparedStatementResult; @@ -937,18 +996,18 @@ public static class PreparedStatement implements AutoCloseable { this.client = client; final Iterator preparedStatementResults = client.doAction(action, options); - preparedStatementResult = FlightSqlUtils.unpackAndParseOrThrow( - preparedStatementResults.next().getBody(), - ActionCreatePreparedStatementResult.class); + preparedStatementResult = + FlightSqlUtils.unpackAndParseOrThrow( + preparedStatementResults.next().getBody(), ActionCreatePreparedStatementResult.class); isClosed = false; } /** - * Set the {@link #parameterBindingRoot} containing the parameter binding from a {@link PreparedStatement} - * operation. + * Set the {@link #parameterBindingRoot} containing the parameter binding from a {@link + * PreparedStatement} operation. * - * @param parameterBindingRoot a {@code VectorSchemaRoot} object containing the values to be used in the - * {@code PreparedStatement} setters. + * @param parameterBindingRoot a {@code VectorSchemaRoot} object containing the values to be + * used in the {@code PreparedStatement} setters. */ public void setParameters(final VectorSchemaRoot parameterBindingRoot) { if (parameterBindingRoot == this.parameterBindingRoot) { @@ -960,8 +1019,8 @@ public void setParameters(final VectorSchemaRoot parameterBindingRoot) { } /** - * Closes the {@link #parameterBindingRoot}, which contains the parameter binding from - * a {@link PreparedStatement} operation, releasing its resources. + * Closes the {@link #parameterBindingRoot}, which contains the parameter binding from a {@link + * PreparedStatement} operation, releasing its resources. */ public void clearParameters() { if (parameterBindingRoot != null) { @@ -995,27 +1054,28 @@ public Schema getParameterSchema() { return parameterSchema; } - /** - * Get the schema of the result set (should be identical to {@link #getResultSetSchema()}). - */ + /** Get the schema of the result set (should be identical to {@link #getResultSetSchema()}). */ public SchemaResult fetchSchema(CallOption... options) { checkOpen(); - final FlightDescriptor descriptor = FlightDescriptor - .command(Any.pack(CommandPreparedStatementQuery.newBuilder() - .setPreparedStatementHandle(preparedStatementResult.getPreparedStatementHandle()) - .build()) - .toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command( + Any.pack( + CommandPreparedStatementQuery.newBuilder() + .setPreparedStatementHandle( + preparedStatementResult.getPreparedStatementHandle()) + .build()) + .toByteArray()); return client.getSchema(descriptor, options); } private Schema deserializeSchema(final ByteString bytes) { try { - return bytes.isEmpty() ? - new Schema(Collections.emptyList()) : - MessageSerializer.deserializeSchema( - new ReadChannel(Channels.newChannel( - new ByteArrayInputStream(bytes.toByteArray())))); + return bytes.isEmpty() + ? new Schema(Collections.emptyList()) + : MessageSerializer.deserializeSchema( + new ReadChannel( + Channels.newChannel(new ByteArrayInputStream(bytes.toByteArray())))); } catch (final IOException e) { throw new RuntimeException("Failed to deserialize schema", e); } @@ -1030,11 +1090,14 @@ private Schema deserializeSchema(final ByteString bytes) { public FlightInfo execute(final CallOption... options) { checkOpen(); - final FlightDescriptor descriptor = FlightDescriptor - .command(Any.pack(CommandPreparedStatementQuery.newBuilder() - .setPreparedStatementHandle(preparedStatementResult.getPreparedStatementHandle()) - .build()) - .toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command( + Any.pack( + CommandPreparedStatementQuery.newBuilder() + .setPreparedStatementHandle( + preparedStatementResult.getPreparedStatementHandle()) + .build()) + .toByteArray()); if (parameterBindingRoot != null && parameterBindingRoot.getRowCount() > 0) { putParameters(descriptor, options); @@ -1047,7 +1110,7 @@ private SyncPutListener putParameters(FlightDescriptor descriptor, CallOption... final SyncPutListener putListener = new SyncPutListener(); FlightClient.ClientStreamListener listener = - client.startPut(descriptor, parameterBindingRoot, putListener, options); + client.startPut(descriptor, parameterBindingRoot, putListener, options); listener.putNext(); listener.completed(); @@ -1073,11 +1136,14 @@ protected final void checkOpen() { */ public long executeUpdate(final CallOption... options) { checkOpen(); - final FlightDescriptor descriptor = FlightDescriptor - .command(Any.pack(CommandPreparedStatementUpdate.newBuilder() - .setPreparedStatementHandle(preparedStatementResult.getPreparedStatementHandle()) - .build()) - .toByteArray()); + final FlightDescriptor descriptor = + FlightDescriptor.command( + Any.pack( + CommandPreparedStatementUpdate.newBuilder() + .setPreparedStatementHandle( + preparedStatementResult.getPreparedStatementHandle()) + .build()) + .toByteArray()); setParameters(parameterBindingRoot == null ? VectorSchemaRoot.of() : parameterBindingRoot); SyncPutListener putListener = putParameters(descriptor, options); @@ -1105,15 +1171,17 @@ public void close(final CallOption... options) { return; } isClosed = true; - final Action action = new Action( - FlightSqlUtils.FLIGHT_SQL_CLOSE_PREPARED_STATEMENT.getType(), - Any.pack(ActionClosePreparedStatementRequest.newBuilder() - .setPreparedStatementHandle(preparedStatementResult.getPreparedStatementHandle()) - .build()) - .toByteArray()); + final Action action = + new Action( + FlightSqlUtils.FLIGHT_SQL_CLOSE_PREPARED_STATEMENT.getType(), + Any.pack( + ActionClosePreparedStatementRequest.newBuilder() + .setPreparedStatementHandle( + preparedStatementResult.getPreparedStatementHandle()) + .build()) + .toByteArray()); final Iterator closePreparedStatementResults = client.doAction(action, options); - closePreparedStatementResults.forEachRemaining(result -> { - }); + closePreparedStatementResults.forEachRemaining(result -> {}); clearParameters(); } @@ -1202,10 +1270,13 @@ public int hashCode() { @Override public String toString() { - return "SubstraitPlan{" + - "plan=" + Arrays.toString(plan) + - ", version='" + version + '\'' + - '}'; + return "SubstraitPlan{" + + "plan=" + + Arrays.toString(plan) + + ", version='" + + version + + '\'' + + '}'; } } } diff --git a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlColumnMetadata.java b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlColumnMetadata.java index 186e8bc04ec..d8dd954a035 100644 --- a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlColumnMetadata.java +++ b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlColumnMetadata.java @@ -24,14 +24,16 @@ /** * Metadata for a column in a Flight SQL query. * - * This can be used with FlightSqlClient to access column's metadata contained in schemas returned - * by GetTables and query execution as follows: + *

This can be used with FlightSqlClient to access column's metadata contained in schemas + * returned by GetTables and query execution as follows: + * *

  *   FlightSqlColumnMetadata metadata = new FlightSqlColumnMetadata(field.getMetadata());
  *   Integer precision = metadata.getPrecision();
  * 
* * FlightSqlProducer can use this to set metadata on a column in a schema as follows: + * *
  *   FlightSqlColumnMetadata metadata = new FlightSqlColumnMetadata.Builder()
  *         .precision(10)
@@ -58,15 +60,14 @@ public class FlightSqlColumnMetadata {
 
   private final Map metadataMap;
 
-  /**
-   * Creates a new instance of FlightSqlColumnMetadata.
-   */
+  /** Creates a new instance of FlightSqlColumnMetadata. */
   public FlightSqlColumnMetadata(Map metadataMap) {
     this.metadataMap = new HashMap<>(metadataMap);
   }
 
   /**
    * Returns the metadata map.
+   *
    * @return The metadata map.
    */
   public Map getMetadataMap() {
@@ -75,6 +76,7 @@ public Map getMetadataMap() {
 
   /**
    * Returns the catalog name.
+   *
    * @return The catalog name.
    */
   public String getCatalogName() {
@@ -83,6 +85,7 @@ public String getCatalogName() {
 
   /**
    * Returns the schema name.
+   *
    * @return The schema name.
    */
   public String getSchemaName() {
@@ -91,6 +94,7 @@ public String getSchemaName() {
 
   /**
    * Returns the table name.
+   *
    * @return The table name.
    */
   public String getTableName() {
@@ -99,6 +103,7 @@ public String getTableName() {
 
   /**
    * Returns the type name.
+   *
    * @return The type name.
    */
   public String getTypeName() {
@@ -107,6 +112,7 @@ public String getTypeName() {
 
   /**
    * Returns the precision / column size.
+   *
    * @return The precision / column size.
    */
   public Integer getPrecision() {
@@ -120,6 +126,7 @@ public Integer getPrecision() {
 
   /**
    * Returns the scale / decimal digits.
+   *
    * @return The scale / decimal digits.
    */
   public Integer getScale() {
@@ -133,6 +140,7 @@ public Integer getScale() {
 
   /**
    * Returns if the column is auto incremented.
+   *
    * @return True if the column is auto incremented, false otherwise.
    */
   public Boolean isAutoIncrement() {
@@ -146,6 +154,7 @@ public Boolean isAutoIncrement() {
 
   /**
    * Returns if the column is case-sensitive.
+   *
    * @return True if the column is case-sensitive, false otherwise.
    */
   public Boolean isCaseSensitive() {
@@ -159,6 +168,7 @@ public Boolean isCaseSensitive() {
 
   /**
    * Returns if the column is read only.
+   *
    * @return True if the column is read only, false otherwise.
    */
   public Boolean isReadOnly() {
@@ -172,6 +182,7 @@ public Boolean isReadOnly() {
 
   /**
    * Returns if the column is searchable.
+   *
    * @return True if the column is searchable, false otherwise.
    */
   public Boolean isSearchable() {
@@ -183,21 +194,18 @@ public Boolean isSearchable() {
     return stringToBoolean(value);
   }
 
-  /**
-   * Builder of FlightSqlColumnMetadata, used on FlightSqlProducer implementations.
-   */
+  /** Builder of FlightSqlColumnMetadata, used on FlightSqlProducer implementations. */
   public static class Builder {
     private final Map metadataMap;
 
-    /**
-     * Creates a new instance of FlightSqlColumnMetadata.Builder.
-     */
+    /** Creates a new instance of FlightSqlColumnMetadata.Builder. */
     public Builder() {
       this.metadataMap = new HashMap<>();
     }
 
     /**
      * Sets the catalog name.
+     *
      * @param catalogName the catalog name.
      * @return This builder.
      */
@@ -208,6 +216,7 @@ public Builder catalogName(String catalogName) {
 
     /**
      * Sets the schema name.
+     *
      * @param schemaName The schema name.
      * @return This builder.
      */
@@ -218,6 +227,7 @@ public Builder schemaName(String schemaName) {
 
     /**
      * Sets the table name.
+     *
      * @param tableName The table name.
      * @return This builder.
      */
@@ -228,6 +238,7 @@ public Builder tableName(String tableName) {
 
     /**
      * Sets the type name.
+     *
      * @param typeName The type name.
      * @return This builder.
      */
@@ -238,6 +249,7 @@ public Builder typeName(String typeName) {
 
     /**
      * Sets the precision / column size.
+     *
      * @param precision The precision / column size.
      * @return This builder.
      */
@@ -248,6 +260,7 @@ public Builder precision(int precision) {
 
     /**
      * Sets the scale / decimal digits.
+     *
      * @param scale The scale / decimal digits.
      * @return This builder.
      */
@@ -258,6 +271,7 @@ public Builder scale(int scale) {
 
     /**
      * Sets if the column is auto incremented.
+     *
      * @param isAutoIncrement True if the column is auto incremented.
      * @return This builder.
      */
@@ -268,6 +282,7 @@ public Builder isAutoIncrement(boolean isAutoIncrement) {
 
     /**
      * Sets if the column is case-sensitive.
+     *
      * @param isCaseSensitive If the column is case-sensitive.
      * @return This builder.
      */
@@ -278,6 +293,7 @@ public Builder isCaseSensitive(boolean isCaseSensitive) {
 
     /**
      * Sets if the column is read only.
+     *
      * @param isReadOnly If the column is read only.
      * @return This builder.
      */
@@ -288,6 +304,7 @@ public Builder isReadOnly(boolean isReadOnly) {
 
     /**
      * Sets if the column is searchable.
+     *
      * @param isSearchable If the column is searchable.
      * @return This builder.
      */
@@ -298,6 +315,7 @@ public Builder isSearchable(boolean isSearchable) {
 
     /**
      * Builds a new instance of FlightSqlColumnMetadata.
+     *
      * @return A new instance of FlightSqlColumnMetadata.
      */
     public FlightSqlColumnMetadata build() {
diff --git a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlProducer.java b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlProducer.java
index dbe39ab1d07..254fce8f08b 100644
--- a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlProducer.java
+++ b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlProducer.java
@@ -46,11 +46,13 @@
 import static org.apache.arrow.vector.types.Types.MinorType.UINT4;
 import static org.apache.arrow.vector.types.Types.MinorType.VARCHAR;
 
+import com.google.common.collect.ImmutableList;
+import com.google.protobuf.Any;
+import com.google.protobuf.InvalidProtocolBufferException;
 import java.io.IOException;
 import java.net.URISyntaxException;
 import java.nio.ByteBuffer;
 import java.util.List;
-
 import org.apache.arrow.flight.Action;
 import org.apache.arrow.flight.ActionType;
 import org.apache.arrow.flight.CallStatus;
@@ -88,20 +90,13 @@
 import org.apache.arrow.vector.types.pojo.FieldType;
 import org.apache.arrow.vector.types.pojo.Schema;
 
-import com.google.common.collect.ImmutableList;
-import com.google.protobuf.Any;
-import com.google.protobuf.InvalidProtocolBufferException;
-
-/**
- * API to Implement an Arrow Flight SQL producer.
- */
+/** API to Implement an Arrow Flight SQL producer. */
 public interface FlightSqlProducer extends FlightProducer, AutoCloseable {
   /**
-   * Depending on the provided command, method either:
-   * 1. Return information about a SQL query, or
+   * Depending on the provided command, method either: 1. Return information about a SQL query, or
    * 2. Return information about a prepared statement. In this case, parameters binding is allowed.
    *
-   * @param context    Per-call context.
+   * @param context Per-call context.
    * @param descriptor The descriptor identifying the data stream.
    * @return information about the given SQL query, or the given prepared statement.
    */
@@ -114,10 +109,14 @@ default FlightInfo getFlightInfo(CallContext context, FlightDescriptor descripto
           FlightSqlUtils.unpackOrThrow(command, CommandStatementQuery.class), context, descriptor);
     } else if (command.is(CommandStatementSubstraitPlan.class)) {
       return getFlightInfoSubstraitPlan(
-          FlightSqlUtils.unpackOrThrow(command, CommandStatementSubstraitPlan.class), context, descriptor);
+          FlightSqlUtils.unpackOrThrow(command, CommandStatementSubstraitPlan.class),
+          context,
+          descriptor);
     } else if (command.is(CommandPreparedStatementQuery.class)) {
       return getFlightInfoPreparedStatement(
-          FlightSqlUtils.unpackOrThrow(command, CommandPreparedStatementQuery.class), context, descriptor);
+          FlightSqlUtils.unpackOrThrow(command, CommandPreparedStatementQuery.class),
+          context,
+          descriptor);
     } else if (command.is(CommandGetCatalogs.class)) {
       return getFlightInfoCatalogs(
           FlightSqlUtils.unpackOrThrow(command, CommandGetCatalogs.class), context, descriptor);
@@ -144,7 +143,9 @@ default FlightInfo getFlightInfo(CallContext context, FlightDescriptor descripto
           FlightSqlUtils.unpackOrThrow(command, CommandGetImportedKeys.class), context, descriptor);
     } else if (command.is(CommandGetCrossReference.class)) {
       return getFlightInfoCrossReference(
-          FlightSqlUtils.unpackOrThrow(command, CommandGetCrossReference.class), context, descriptor);
+          FlightSqlUtils.unpackOrThrow(command, CommandGetCrossReference.class),
+          context,
+          descriptor);
     } else if (command.is(CommandGetXdbcTypeInfo.class)) {
       return getFlightInfoTypeInfo(
           FlightSqlUtils.unpackOrThrow(command, CommandGetXdbcTypeInfo.class), context, descriptor);
@@ -158,7 +159,7 @@ default FlightInfo getFlightInfo(CallContext context, FlightDescriptor descripto
   /**
    * Returns the schema of the result produced by the SQL query.
    *
-   * @param context    Per-call context.
+   * @param context Per-call context.
    * @param descriptor The descriptor identifying the data stream.
    * @return the result set schema.
    */
@@ -171,10 +172,14 @@ default SchemaResult getSchema(CallContext context, FlightDescriptor descriptor)
           FlightSqlUtils.unpackOrThrow(command, CommandStatementQuery.class), context, descriptor);
     } else if (command.is(CommandPreparedStatementQuery.class)) {
       return getSchemaPreparedStatement(
-          FlightSqlUtils.unpackOrThrow(command, CommandPreparedStatementQuery.class), context, descriptor);
+          FlightSqlUtils.unpackOrThrow(command, CommandPreparedStatementQuery.class),
+          context,
+          descriptor);
     } else if (command.is(CommandStatementSubstraitPlan.class)) {
       return getSchemaSubstraitPlan(
-          FlightSqlUtils.unpackOrThrow(command, CommandStatementSubstraitPlan.class), context, descriptor);
+          FlightSqlUtils.unpackOrThrow(command, CommandStatementSubstraitPlan.class),
+          context,
+          descriptor);
     } else if (command.is(CommandGetCatalogs.class)) {
       return new SchemaResult(Schemas.GET_CATALOGS_SCHEMA);
     } else if (command.is(CommandGetCrossReference.class)) {
@@ -206,12 +211,12 @@ default SchemaResult getSchema(CallContext context, FlightDescriptor descriptor)
   }
 
   /**
-   * Depending on the provided command, method either:
-   * 1. Return data for a stream produced by executing the provided SQL query, or
-   * 2. Return data for a prepared statement. In this case, parameters binding is allowed.
+   * Depending on the provided command, method either: 1. Return data for a stream produced by
+   * executing the provided SQL query, or 2. Return data for a prepared statement. In this case,
+   * parameters binding is allowed.
    *
-   * @param context  Per-call context.
-   * @param ticket   The application-defined ticket identifying this stream.
+   * @param context Per-call context.
+   * @param ticket The application-defined ticket identifying this stream.
    * @param listener An interface for sending data back to the client.
    */
   @Override
@@ -230,73 +235,94 @@ default void getStream(CallContext context, Ticket ticket, ServerStreamListener
           FlightSqlUtils.unpackOrThrow(command, TicketStatementQuery.class), context, listener);
     } else if (command.is(CommandPreparedStatementQuery.class)) {
       getStreamPreparedStatement(
-          FlightSqlUtils.unpackOrThrow(command, CommandPreparedStatementQuery.class), context, listener);
+          FlightSqlUtils.unpackOrThrow(command, CommandPreparedStatementQuery.class),
+          context,
+          listener);
     } else if (command.is(CommandGetCatalogs.class)) {
       getStreamCatalogs(context, listener);
     } else if (command.is(CommandGetDbSchemas.class)) {
-      getStreamSchemas(FlightSqlUtils.unpackOrThrow(command, CommandGetDbSchemas.class), context, listener);
+      getStreamSchemas(
+          FlightSqlUtils.unpackOrThrow(command, CommandGetDbSchemas.class), context, listener);
     } else if (command.is(CommandGetTables.class)) {
-      getStreamTables(FlightSqlUtils.unpackOrThrow(command, CommandGetTables.class), context, listener);
+      getStreamTables(
+          FlightSqlUtils.unpackOrThrow(command, CommandGetTables.class), context, listener);
     } else if (command.is(CommandGetTableTypes.class)) {
       getStreamTableTypes(context, listener);
     } else if (command.is(CommandGetSqlInfo.class)) {
-      getStreamSqlInfo(FlightSqlUtils.unpackOrThrow(command, CommandGetSqlInfo.class), context, listener);
+      getStreamSqlInfo(
+          FlightSqlUtils.unpackOrThrow(command, CommandGetSqlInfo.class), context, listener);
     } else if (command.is(CommandGetPrimaryKeys.class)) {
-      getStreamPrimaryKeys(FlightSqlUtils.unpackOrThrow(command, CommandGetPrimaryKeys.class), context, listener);
+      getStreamPrimaryKeys(
+          FlightSqlUtils.unpackOrThrow(command, CommandGetPrimaryKeys.class), context, listener);
     } else if (command.is(CommandGetExportedKeys.class)) {
-      getStreamExportedKeys(FlightSqlUtils.unpackOrThrow(command, CommandGetExportedKeys.class), context, listener);
+      getStreamExportedKeys(
+          FlightSqlUtils.unpackOrThrow(command, CommandGetExportedKeys.class), context, listener);
     } else if (command.is(CommandGetImportedKeys.class)) {
-      getStreamImportedKeys(FlightSqlUtils.unpackOrThrow(command, CommandGetImportedKeys.class), context, listener);
+      getStreamImportedKeys(
+          FlightSqlUtils.unpackOrThrow(command, CommandGetImportedKeys.class), context, listener);
     } else if (command.is(CommandGetCrossReference.class)) {
-      getStreamCrossReference(FlightSqlUtils.unpackOrThrow(command, CommandGetCrossReference.class), context, listener);
+      getStreamCrossReference(
+          FlightSqlUtils.unpackOrThrow(command, CommandGetCrossReference.class), context, listener);
     } else if (command.is(CommandGetXdbcTypeInfo.class)) {
-      getStreamTypeInfo(FlightSqlUtils.unpackOrThrow(command, CommandGetXdbcTypeInfo.class), context, listener);
+      getStreamTypeInfo(
+          FlightSqlUtils.unpackOrThrow(command, CommandGetXdbcTypeInfo.class), context, listener);
     } else {
-      throw CallStatus.INVALID_ARGUMENT.withDescription("The defined request is invalid.").toRuntimeException();
+      throw CallStatus.INVALID_ARGUMENT
+          .withDescription("The defined request is invalid.")
+          .toRuntimeException();
     }
   }
 
   /**
-   * Depending on the provided command, method either:
-   * 1. Execute provided SQL query as an update statement, or
-   * 2. Execute provided update SQL query prepared statement. In this case, parameters binding
-   * is allowed, or
-   * 3. Binds parameters to the provided prepared statement.
+   * Depending on the provided command, method either: 1. Execute provided SQL query as an update
+   * statement, or 2. Execute provided update SQL query prepared statement. In this case, parameters
+   * binding is allowed, or 3. Binds parameters to the provided prepared statement.
    *
-   * @param context      Per-call context.
+   * @param context Per-call context.
    * @param flightStream The data stream being uploaded.
-   * @param ackStream    The data stream listener for update result acknowledgement.
+   * @param ackStream The data stream listener for update result acknowledgement.
    * @return a Runnable to process the stream.
    */
   @Override
-  default Runnable acceptPut(CallContext context, FlightStream flightStream, StreamListener ackStream) {
+  default Runnable acceptPut(
+      CallContext context, FlightStream flightStream, StreamListener ackStream) {
     final Any command = FlightSqlUtils.parseOrThrow(flightStream.getDescriptor().getCommand());
 
     if (command.is(CommandStatementUpdate.class)) {
       return acceptPutStatement(
           FlightSqlUtils.unpackOrThrow(command, CommandStatementUpdate.class),
-          context, flightStream, ackStream);
+          context,
+          flightStream,
+          ackStream);
     } else if (command.is(CommandStatementSubstraitPlan.class)) {
       return acceptPutSubstraitPlan(
           FlightSqlUtils.unpackOrThrow(command, CommandStatementSubstraitPlan.class),
-          context, flightStream, ackStream);
+          context,
+          flightStream,
+          ackStream);
     } else if (command.is(CommandPreparedStatementUpdate.class)) {
       return acceptPutPreparedStatementUpdate(
           FlightSqlUtils.unpackOrThrow(command, CommandPreparedStatementUpdate.class),
-          context, flightStream, ackStream);
+          context,
+          flightStream,
+          ackStream);
     } else if (command.is(CommandPreparedStatementQuery.class)) {
       return acceptPutPreparedStatementQuery(
           FlightSqlUtils.unpackOrThrow(command, CommandPreparedStatementQuery.class),
-          context, flightStream, ackStream);
+          context,
+          flightStream,
+          ackStream);
     }
 
-    throw CallStatus.INVALID_ARGUMENT.withDescription("The defined request is invalid.").toRuntimeException();
+    throw CallStatus.INVALID_ARGUMENT
+        .withDescription("The defined request is invalid.")
+        .toRuntimeException();
   }
 
   /**
    * Lists all available Flight SQL actions.
    *
-   * @param context  Per-call context.
+   * @param context Per-call context.
    * @param listener An interface for sending data back to the client.
    */
   @Override
@@ -308,8 +334,8 @@ default void listActions(CallContext context, StreamListener listene
   /**
    * Performs the requested Flight SQL action.
    *
-   * @param context  Per-call context.
-   * @param action   Client-supplied parameters.
+   * @param context Per-call context.
+   * @param action Client-supplied parameters.
    * @param listener A stream of responses.
    */
   @Override
@@ -322,7 +348,8 @@ default void doAction(CallContext context, Action action, StreamListener
       beginSavepoint(request, context, new ProtoListener<>(listener));
     } else if (actionType.equals(FlightSqlUtils.FLIGHT_SQL_BEGIN_TRANSACTION.getType())) {
       final ActionBeginTransactionRequest request =
-          FlightSqlUtils.unpackAndParseOrThrow(action.getBody(), ActionBeginTransactionRequest.class);
+          FlightSqlUtils.unpackAndParseOrThrow(
+              action.getBody(), ActionBeginTransactionRequest.class);
       beginTransaction(request, context, new ProtoListener<>(listener));
     } else if (actionType.equals(FlightSqlUtils.FLIGHT_SQL_CANCEL_QUERY.getType())) {
       //noinspection deprecation
@@ -332,24 +359,29 @@ default void doAction(CallContext context, Action action, StreamListener
       try {
         info = FlightInfo.deserialize(request.getInfo().asReadOnlyByteBuffer());
       } catch (IOException | URISyntaxException e) {
-        listener.onError(CallStatus.INTERNAL
-            .withDescription("Could not unpack FlightInfo: " + e)
-            .withCause(e)
-            .toRuntimeException());
+        listener.onError(
+            CallStatus.INTERNAL
+                .withDescription("Could not unpack FlightInfo: " + e)
+                .withCause(e)
+                .toRuntimeException());
         return;
       }
       cancelQuery(info, context, new CancelListener(listener));
     } else if (actionType.equals(FlightSqlUtils.FLIGHT_SQL_CREATE_PREPARED_STATEMENT.getType())) {
-      final ActionCreatePreparedStatementRequest request = FlightSqlUtils.unpackAndParseOrThrow(action.getBody(),
-          ActionCreatePreparedStatementRequest.class);
+      final ActionCreatePreparedStatementRequest request =
+          FlightSqlUtils.unpackAndParseOrThrow(
+              action.getBody(), ActionCreatePreparedStatementRequest.class);
       createPreparedStatement(request, context, listener);
-    } else if (actionType.equals(FlightSqlUtils.FLIGHT_SQL_CREATE_PREPARED_SUBSTRAIT_PLAN.getType())) {
+    } else if (actionType.equals(
+        FlightSqlUtils.FLIGHT_SQL_CREATE_PREPARED_SUBSTRAIT_PLAN.getType())) {
       final ActionCreatePreparedSubstraitPlanRequest request =
-          FlightSqlUtils.unpackAndParseOrThrow(action.getBody(), ActionCreatePreparedSubstraitPlanRequest.class);
+          FlightSqlUtils.unpackAndParseOrThrow(
+              action.getBody(), ActionCreatePreparedSubstraitPlanRequest.class);
       createPreparedSubstraitPlan(request, context, new ProtoListener<>(listener));
     } else if (actionType.equals(FlightSqlUtils.FLIGHT_SQL_CLOSE_PREPARED_STATEMENT.getType())) {
       final ActionClosePreparedStatementRequest request =
-          FlightSqlUtils.unpackAndParseOrThrow(action.getBody(), ActionClosePreparedStatementRequest.class);
+          FlightSqlUtils.unpackAndParseOrThrow(
+              action.getBody(), ActionClosePreparedStatementRequest.class);
       closePreparedStatement(request, context, new NoResultListener(listener));
     } else if (actionType.equals(FlightSqlUtils.FLIGHT_SQL_END_SAVEPOINT.getType())) {
       ActionEndSavepointRequest request =
@@ -364,10 +396,11 @@ default void doAction(CallContext context, Action action, StreamListener
       try {
         request = CancelFlightInfoRequest.deserialize(ByteBuffer.wrap(action.getBody()));
       } catch (IOException | URISyntaxException e) {
-        listener.onError(CallStatus.INTERNAL
-            .withDescription("Could not unpack FlightInfo: " + e)
-            .withCause(e)
-            .toRuntimeException());
+        listener.onError(
+            CallStatus.INTERNAL
+                .withDescription("Could not unpack FlightInfo: " + e)
+                .withCause(e)
+                .toRuntimeException());
         return;
       }
       cancelFlightInfo(request, context, new CancelStatusListener(listener));
@@ -376,10 +409,11 @@ default void doAction(CallContext context, Action action, StreamListener
       try {
         request = RenewFlightEndpointRequest.deserialize(ByteBuffer.wrap(action.getBody()));
       } catch (IOException | URISyntaxException e) {
-        listener.onError(CallStatus.INTERNAL
-            .withDescription("Could not unpack FlightInfo: " + e)
-            .withCause(e)
-            .toRuntimeException());
+        listener.onError(
+            CallStatus.INTERNAL
+                .withDescription("Could not unpack FlightInfo: " + e)
+                .withCause(e)
+                .toRuntimeException());
         return;
       }
       renewFlightEndpoint(request, context, new FlightEndpointListener(listener));
@@ -393,24 +427,28 @@ default void doAction(CallContext context, Action action, StreamListener
   /**
    * Create a savepoint within a transaction.
    *
-   * @param request  The savepoint request.
-   * @param context  Per-call context.
+   * @param request The savepoint request.
+   * @param context Per-call context.
    * @param listener The newly created savepoint ID.
    */
-  default void beginSavepoint(ActionBeginSavepointRequest request, CallContext context,
-                              StreamListener listener) {
+  default void beginSavepoint(
+      ActionBeginSavepointRequest request,
+      CallContext context,
+      StreamListener listener) {
     listener.onError(CallStatus.UNIMPLEMENTED.toRuntimeException());
   }
 
   /**
    * Begin a transaction.
    *
-   * @param request  The transaction request.
-   * @param context  Per-call context.
+   * @param request The transaction request.
+   * @param context Per-call context.
    * @param listener The newly created transaction ID.
    */
-  default void beginTransaction(ActionBeginTransactionRequest request, CallContext context,
-                                StreamListener listener) {
+  default void beginTransaction(
+      ActionBeginTransactionRequest request,
+      CallContext context,
+      StreamListener listener) {
     listener.onError(CallStatus.UNIMPLEMENTED.toRuntimeException());
   }
 
@@ -421,172 +459,182 @@ default void beginTransaction(ActionBeginTransactionRequest request, CallContext
    * @param context Per-call context.
    * @param listener An interface for sending data back to the client.
    */
-  default void cancelFlightInfo(CancelFlightInfoRequest request, CallContext context,
-                                StreamListener listener) {
+  default void cancelFlightInfo(
+      CancelFlightInfoRequest request, CallContext context, StreamListener listener) {
     listener.onError(CallStatus.UNIMPLEMENTED.toRuntimeException());
   }
 
-
   /**
    * Explicitly cancel a query.
    *
-   * @param info     The FlightInfo of the query to cancel.
-   * @param context  Per-call context.
+   * @param info The FlightInfo of the query to cancel.
+   * @param context Per-call context.
    * @param listener Whether cancellation succeeded.
-   * @deprecated Prefer {@link #cancelFlightInfo(CancelFlightInfoRequest, CallContext, StreamListener)}.
+   * @deprecated Prefer {@link #cancelFlightInfo(CancelFlightInfoRequest, CallContext,
+   *     StreamListener)}.
    */
   @Deprecated
-  default void cancelQuery(FlightInfo info, CallContext context, StreamListener listener) {
+  default void cancelQuery(
+      FlightInfo info, CallContext context, StreamListener listener) {
     CancelFlightInfoRequest request = new CancelFlightInfoRequest(info);
-    cancelFlightInfo(request, context, new StreamListener() {
-      @Override
-      public void onNext(CancelStatus val) {
-        switch (val) {
-          case UNSPECIFIED:
-            listener.onNext(CancelResult.UNSPECIFIED);
-            break;
-          case CANCELLED:
-            listener.onNext(CancelResult.CANCELLED);
-            break;
-          case CANCELLING:
-            listener.onNext(CancelResult.CANCELLING);
-            break;
-          case NOT_CANCELLABLE:
-            listener.onNext(CancelResult.NOT_CANCELLABLE);
-            break;
-          default:
-            // XXX: CheckStyle requires a default clause which arguably makes the code worse.
-            throw new AssertionError("Unknown enum variant " + val);
-        }
-      }
-
-      @Override
-      public void onError(Throwable t) {
-        listener.onError(t);
-      }
-
-      @Override
-      public void onCompleted() {
-        listener.onCompleted();
-      }
-    });
+    cancelFlightInfo(
+        request,
+        context,
+        new StreamListener() {
+          @Override
+          public void onNext(CancelStatus val) {
+            switch (val) {
+              case UNSPECIFIED:
+                listener.onNext(CancelResult.UNSPECIFIED);
+                break;
+              case CANCELLED:
+                listener.onNext(CancelResult.CANCELLED);
+                break;
+              case CANCELLING:
+                listener.onNext(CancelResult.CANCELLING);
+                break;
+              case NOT_CANCELLABLE:
+                listener.onNext(CancelResult.NOT_CANCELLABLE);
+                break;
+              default:
+                // XXX: CheckStyle requires a default clause which arguably makes the code worse.
+                throw new AssertionError("Unknown enum variant " + val);
+            }
+          }
+
+          @Override
+          public void onError(Throwable t) {
+            listener.onError(t);
+          }
+
+          @Override
+          public void onCompleted() {
+            listener.onCompleted();
+          }
+        });
   }
 
   /**
-   * Creates a prepared statement on the server and returns a handle and metadata for in a
-   * {@link ActionCreatePreparedStatementResult} object in a {@link Result}
-   * object.
+   * Creates a prepared statement on the server and returns a handle and metadata for in a {@link
+   * ActionCreatePreparedStatementResult} object in a {@link Result} object.
    *
-   * @param request  The sql command to generate the prepared statement.
-   * @param context  Per-call context.
+   * @param request The sql command to generate the prepared statement.
+   * @param context Per-call context.
    * @param listener A stream of responses.
    */
-  void createPreparedStatement(ActionCreatePreparedStatementRequest request, CallContext context,
-                               StreamListener listener);
+  void createPreparedStatement(
+      ActionCreatePreparedStatementRequest request,
+      CallContext context,
+      StreamListener listener);
 
   /**
    * Pre-compile a Substrait plan.
-   * @param request  The plan.
-   * @param context  Per-call context.
+   *
+   * @param request The plan.
+   * @param context Per-call context.
    * @param listener The resulting prepared statement.
    */
-  default void createPreparedSubstraitPlan(ActionCreatePreparedSubstraitPlanRequest request, CallContext context,
-                                           StreamListener listener) {
+  default void createPreparedSubstraitPlan(
+      ActionCreatePreparedSubstraitPlanRequest request,
+      CallContext context,
+      StreamListener listener) {
     listener.onError(CallStatus.UNIMPLEMENTED.toRuntimeException());
   }
 
   /**
    * Closes a prepared statement on the server. No result is expected.
    *
-   * @param request  The sql command to generate the prepared statement.
-   * @param context  Per-call context.
+   * @param request The sql command to generate the prepared statement.
+   * @param context Per-call context.
    * @param listener A stream of responses.
    */
-  void closePreparedStatement(ActionClosePreparedStatementRequest request, CallContext context,
-                              StreamListener listener);
+  void closePreparedStatement(
+      ActionClosePreparedStatementRequest request,
+      CallContext context,
+      StreamListener listener);
 
   /**
    * Release or roll back to a savepoint.
    *
-   * @param request  The savepoint, and whether to release/rollback.
-   * @param context  Per-call context.
-   * @param listener Call {@link StreamListener#onCompleted()} or
-   *                 {@link StreamListener#onError(Throwable)} when done; do not send a result.
+   * @param request The savepoint, and whether to release/rollback.
+   * @param context Per-call context.
+   * @param listener Call {@link StreamListener#onCompleted()} or {@link
+   *     StreamListener#onError(Throwable)} when done; do not send a result.
    */
-  default void endSavepoint(ActionEndSavepointRequest request, CallContext context,
-                            StreamListener listener) {
+  default void endSavepoint(
+      ActionEndSavepointRequest request, CallContext context, StreamListener listener) {
     listener.onError(CallStatus.UNIMPLEMENTED.toRuntimeException());
   }
 
   /**
    * Commit or roll back to a transaction.
    *
-   * @param request  The transaction, and whether to release/rollback.
-   * @param context  Per-call context.
-   * @param listener Call {@link StreamListener#onCompleted()} or
-   *                 {@link StreamListener#onError(Throwable)} when done; do not send a result.
+   * @param request The transaction, and whether to release/rollback.
+   * @param context Per-call context.
+   * @param listener Call {@link StreamListener#onCompleted()} or {@link
+   *     StreamListener#onError(Throwable)} when done; do not send a result.
    */
-  default void endTransaction(ActionEndTransactionRequest request, CallContext context,
-                              StreamListener listener) {
+  default void endTransaction(
+      ActionEndTransactionRequest request, CallContext context, StreamListener listener) {
     listener.onError(CallStatus.UNIMPLEMENTED.toRuntimeException());
   }
 
   /**
    * Evaluate a SQL query.
    *
-   * @param command    The SQL query.
-   * @param context    Per-call context.
+   * @param command The SQL query.
+   * @param context Per-call context.
    * @param descriptor The descriptor identifying the data stream.
    * @return Metadata about the stream.
    */
-  FlightInfo getFlightInfoStatement(CommandStatementQuery command, CallContext context,
-                                    FlightDescriptor descriptor);
+  FlightInfo getFlightInfoStatement(
+      CommandStatementQuery command, CallContext context, FlightDescriptor descriptor);
 
   /**
    * Evaluate a Substrait plan.
    *
-   * @param command    The Substrait plan.
-   * @param context    Per-call context.
+   * @param command The Substrait plan.
+   * @param context Per-call context.
    * @param descriptor The descriptor identifying the data stream.
    * @return Metadata about the stream.
    */
-  default FlightInfo getFlightInfoSubstraitPlan(CommandStatementSubstraitPlan command, CallContext context,
-                                                FlightDescriptor descriptor) {
+  default FlightInfo getFlightInfoSubstraitPlan(
+      CommandStatementSubstraitPlan command, CallContext context, FlightDescriptor descriptor) {
     throw CallStatus.UNIMPLEMENTED.toRuntimeException();
   }
 
   /**
    * Gets information about a particular prepared statement data stream.
    *
-   * @param command    The prepared statement to generate the data stream.
-   * @param context    Per-call context.
+   * @param command The prepared statement to generate the data stream.
+   * @param context Per-call context.
    * @param descriptor The descriptor identifying the data stream.
    * @return Metadata about the stream.
    */
-  FlightInfo getFlightInfoPreparedStatement(CommandPreparedStatementQuery command,
-                                            CallContext context, FlightDescriptor descriptor);
+  FlightInfo getFlightInfoPreparedStatement(
+      CommandPreparedStatementQuery command, CallContext context, FlightDescriptor descriptor);
 
   /**
    * Get the result schema for a SQL query.
    *
-   * @param command    The SQL query.
-   * @param context    Per-call context.
+   * @param command The SQL query.
+   * @param context Per-call context.
    * @param descriptor The descriptor identifying the data stream.
    * @return the schema of the result set.
    */
-  SchemaResult getSchemaStatement(CommandStatementQuery command, CallContext context,
-                                  FlightDescriptor descriptor);
+  SchemaResult getSchemaStatement(
+      CommandStatementQuery command, CallContext context, FlightDescriptor descriptor);
 
   /**
    * Get the schema of the result set of a prepared statement.
    *
-   * @param command    The prepared statement handle.
-   * @param context    Per-call context.
+   * @param command The prepared statement handle.
+   * @param context Per-call context.
    * @param descriptor The descriptor identifying the data stream.
    * @return the schema of the result set.
    */
-  default SchemaResult getSchemaPreparedStatement(CommandPreparedStatementQuery command, CallContext context,
-                                  FlightDescriptor descriptor) {
+  default SchemaResult getSchemaPreparedStatement(
+      CommandPreparedStatementQuery command, CallContext context, FlightDescriptor descriptor) {
     throw CallStatus.UNIMPLEMENTED
         .withDescription("GetSchema with CommandPreparedStatementQuery is not implemented")
         .toRuntimeException();
@@ -595,59 +643,67 @@ default SchemaResult getSchemaPreparedStatement(CommandPreparedStatementQuery co
   /**
    * Get the result schema for a Substrait plan.
    *
-   * @param command    The Substrait plan.
-   * @param context    Per-call context.
+   * @param command The Substrait plan.
+   * @param context Per-call context.
    * @param descriptor The descriptor identifying the data stream.
    * @return Schema for the stream.
    */
-  default SchemaResult getSchemaSubstraitPlan(CommandStatementSubstraitPlan command, CallContext context,
-                                              FlightDescriptor descriptor) {
+  default SchemaResult getSchemaSubstraitPlan(
+      CommandStatementSubstraitPlan command, CallContext context, FlightDescriptor descriptor) {
     throw CallStatus.UNIMPLEMENTED.toRuntimeException();
   }
 
   /**
    * Returns data for a SQL query based data stream.
-   * @param ticket   Ticket message containing the statement handle.
-   * @param context  Per-call context.
+   *
+   * @param ticket Ticket message containing the statement handle.
+   * @param context Per-call context.
    * @param listener An interface for sending data back to the client.
    */
-  void getStreamStatement(TicketStatementQuery ticket, CallContext context,
-                          ServerStreamListener listener);
+  void getStreamStatement(
+      TicketStatementQuery ticket, CallContext context, ServerStreamListener listener);
 
   /**
    * Returns data for a particular prepared statement query instance.
    *
-   * @param command  The prepared statement to generate the data stream.
-   * @param context  Per-call context.
+   * @param command The prepared statement to generate the data stream.
+   * @param context Per-call context.
    * @param listener An interface for sending data back to the client.
    */
-  void getStreamPreparedStatement(CommandPreparedStatementQuery command, CallContext context,
-                                  ServerStreamListener listener);
+  void getStreamPreparedStatement(
+      CommandPreparedStatementQuery command, CallContext context, ServerStreamListener listener);
 
   /**
    * Accepts uploaded data for a particular SQL query based data stream.
+   *
    * 

`PutResult`s must be in the form of a {@link DoPutUpdateResult}. * - * @param command The sql command to generate the data stream. - * @param context Per-call context. + * @param command The sql command to generate the data stream. + * @param context Per-call context. * @param flightStream The data stream being uploaded. - * @param ackStream The result data stream. + * @param ackStream The result data stream. * @return A runnable to process the stream. */ - Runnable acceptPutStatement(CommandStatementUpdate command, CallContext context, - FlightStream flightStream, StreamListener ackStream); + Runnable acceptPutStatement( + CommandStatementUpdate command, + CallContext context, + FlightStream flightStream, + StreamListener ackStream); /** * Handle a Substrait plan with uploaded data. * - * @param command The Substrait plan to evaluate. - * @param context Per-call context. + * @param command The Substrait plan to evaluate. + * @param context Per-call context. * @param flightStream The data stream being uploaded. - * @param ackStream The result data stream. + * @param ackStream The result data stream. * @return A runnable to process the stream. */ - default Runnable acceptPutSubstraitPlan(CommandStatementSubstraitPlan command, CallContext context, - FlightStream flightStream, StreamListener ackStream) { + default Runnable acceptPutSubstraitPlan( + CommandStatementSubstraitPlan command, + CallContext context, + FlightStream flightStream, + StreamListener ackStream) { return () -> { ackStream.onError(CallStatus.UNIMPLEMENTED.toRuntimeException()); }; @@ -655,239 +711,247 @@ default Runnable acceptPutSubstraitPlan(CommandStatementSubstraitPlan command, C /** * Accepts uploaded data for a particular prepared statement data stream. + * *

`PutResult`s must be in the form of a {@link DoPutUpdateResult}. * - * @param command The prepared statement to generate the data stream. - * @param context Per-call context. + * @param command The prepared statement to generate the data stream. + * @param context Per-call context. * @param flightStream The data stream being uploaded. - * @param ackStream The result data stream. + * @param ackStream The result data stream. * @return A runnable to process the stream. */ - Runnable acceptPutPreparedStatementUpdate(CommandPreparedStatementUpdate command, - CallContext context, FlightStream flightStream, - StreamListener ackStream); + Runnable acceptPutPreparedStatementUpdate( + CommandPreparedStatementUpdate command, + CallContext context, + FlightStream flightStream, + StreamListener ackStream); /** * Accepts uploaded parameter values for a particular prepared statement query. * - * @param command The prepared statement the parameter values will bind to. - * @param context Per-call context. + * @param command The prepared statement the parameter values will bind to. + * @param context Per-call context. * @param flightStream The data stream being uploaded. - * @param ackStream The result data stream. + * @param ackStream The result data stream. * @return A runnable to process the stream. */ - Runnable acceptPutPreparedStatementQuery(CommandPreparedStatementQuery command, - CallContext context, FlightStream flightStream, - StreamListener ackStream); + Runnable acceptPutPreparedStatementQuery( + CommandPreparedStatementQuery command, + CallContext context, + FlightStream flightStream, + StreamListener ackStream); /** - * Returns the SQL Info of the server by returning a - * {@link CommandGetSqlInfo} in a {@link Result}. + * Returns the SQL Info of the server by returning a {@link CommandGetSqlInfo} in a {@link + * Result}. * - * @param request request filter parameters. - * @param context Per-call context. + * @param request request filter parameters. + * @param context Per-call context. * @param descriptor The descriptor identifying the data stream. * @return Metadata about the stream. */ - FlightInfo getFlightInfoSqlInfo(CommandGetSqlInfo request, CallContext context, - FlightDescriptor descriptor); + FlightInfo getFlightInfoSqlInfo( + CommandGetSqlInfo request, CallContext context, FlightDescriptor descriptor); /** * Returns data for SQL info based data stream. * - * @param command The command to generate the data stream. - * @param context Per-call context. + * @param command The command to generate the data stream. + * @param context Per-call context. * @param listener An interface for sending data back to the client. */ - void getStreamSqlInfo(CommandGetSqlInfo command, CallContext context, ServerStreamListener listener); - + void getStreamSqlInfo( + CommandGetSqlInfo command, CallContext context, ServerStreamListener listener); /** * Returns a description of all the data types supported by source. * - * @param request request filter parameters. - * @param descriptor The descriptor identifying the data stream. - * @return Metadata about the stream. + * @param request request filter parameters. + * @param descriptor The descriptor identifying the data stream. + * @return Metadata about the stream. */ - FlightInfo getFlightInfoTypeInfo(CommandGetXdbcTypeInfo request, CallContext context, - FlightDescriptor descriptor); + FlightInfo getFlightInfoTypeInfo( + CommandGetXdbcTypeInfo request, CallContext context, FlightDescriptor descriptor); /** * Returns data for type info based data stream. * - * @param context Per-call context. + * @param context Per-call context. * @param listener An interface for sending data back to the client. */ - void getStreamTypeInfo(CommandGetXdbcTypeInfo request, CallContext context, ServerStreamListener listener); + void getStreamTypeInfo( + CommandGetXdbcTypeInfo request, CallContext context, ServerStreamListener listener); /** - * Returns the available catalogs by returning a stream of - * {@link CommandGetCatalogs} objects in {@link Result} objects. + * Returns the available catalogs by returning a stream of {@link CommandGetCatalogs} objects in + * {@link Result} objects. * - * @param request request filter parameters. - * @param context Per-call context. + * @param request request filter parameters. + * @param context Per-call context. * @param descriptor The descriptor identifying the data stream. * @return Metadata about the stream. */ - FlightInfo getFlightInfoCatalogs(CommandGetCatalogs request, CallContext context, - FlightDescriptor descriptor); + FlightInfo getFlightInfoCatalogs( + CommandGetCatalogs request, CallContext context, FlightDescriptor descriptor); /** * Returns data for catalogs based data stream. * - * @param context Per-call context. + * @param context Per-call context. * @param listener An interface for sending data back to the client. */ void getStreamCatalogs(CallContext context, ServerStreamListener listener); /** - * Returns the available schemas by returning a stream of - * {@link CommandGetDbSchemas} objects in {@link Result} objects. + * Returns the available schemas by returning a stream of {@link CommandGetDbSchemas} objects in + * {@link Result} objects. * - * @param request request filter parameters. - * @param context Per-call context. + * @param request request filter parameters. + * @param context Per-call context. * @param descriptor The descriptor identifying the data stream. * @return Metadata about the stream. */ - FlightInfo getFlightInfoSchemas(CommandGetDbSchemas request, CallContext context, - FlightDescriptor descriptor); + FlightInfo getFlightInfoSchemas( + CommandGetDbSchemas request, CallContext context, FlightDescriptor descriptor); /** * Returns data for schemas based data stream. * - * @param command The command to generate the data stream. - * @param context Per-call context. + * @param command The command to generate the data stream. + * @param context Per-call context. * @param listener An interface for sending data back to the client. */ - void getStreamSchemas(CommandGetDbSchemas command, CallContext context, ServerStreamListener listener); + void getStreamSchemas( + CommandGetDbSchemas command, CallContext context, ServerStreamListener listener); /** - * Returns the available tables by returning a stream of - * {@link CommandGetTables} objects in {@link Result} objects. + * Returns the available tables by returning a stream of {@link CommandGetTables} objects in + * {@link Result} objects. * - * @param request request filter parameters. - * @param context Per-call context. + * @param request request filter parameters. + * @param context Per-call context. * @param descriptor The descriptor identifying the data stream. * @return Metadata about the stream. */ - FlightInfo getFlightInfoTables(CommandGetTables request, CallContext context, - FlightDescriptor descriptor); + FlightInfo getFlightInfoTables( + CommandGetTables request, CallContext context, FlightDescriptor descriptor); /** * Returns data for tables based data stream. * - * @param command The command to generate the data stream. - * @param context Per-call context. + * @param command The command to generate the data stream. + * @param context Per-call context. * @param listener An interface for sending data back to the client. */ - void getStreamTables(CommandGetTables command, CallContext context, ServerStreamListener listener); + void getStreamTables( + CommandGetTables command, CallContext context, ServerStreamListener listener); /** - * Returns the available table types by returning a stream of - * {@link CommandGetTableTypes} objects in {@link Result} objects. + * Returns the available table types by returning a stream of {@link CommandGetTableTypes} objects + * in {@link Result} objects. * - * @param context Per-call context. + * @param context Per-call context. * @param descriptor The descriptor identifying the data stream. * @return Metadata about the stream. */ - FlightInfo getFlightInfoTableTypes(CommandGetTableTypes request, CallContext context, - FlightDescriptor descriptor); + FlightInfo getFlightInfoTableTypes( + CommandGetTableTypes request, CallContext context, FlightDescriptor descriptor); /** * Returns data for table types based data stream. * - * @param context Per-call context. + * @param context Per-call context. * @param listener An interface for sending data back to the client. */ void getStreamTableTypes(CallContext context, ServerStreamListener listener); /** - * Returns the available primary keys by returning a stream of - * {@link CommandGetPrimaryKeys} objects in {@link Result} objects. + * Returns the available primary keys by returning a stream of {@link CommandGetPrimaryKeys} + * objects in {@link Result} objects. * - * @param request request filter parameters. - * @param context Per-call context. + * @param request request filter parameters. + * @param context Per-call context. * @param descriptor The descriptor identifying the data stream. * @return Metadata about the stream. */ - FlightInfo getFlightInfoPrimaryKeys(CommandGetPrimaryKeys request, CallContext context, - FlightDescriptor descriptor); + FlightInfo getFlightInfoPrimaryKeys( + CommandGetPrimaryKeys request, CallContext context, FlightDescriptor descriptor); /** * Returns data for primary keys based data stream. * - * @param command The command to generate the data stream. - * @param context Per-call context. + * @param command The command to generate the data stream. + * @param context Per-call context. * @param listener An interface for sending data back to the client. */ - void getStreamPrimaryKeys(CommandGetPrimaryKeys command, CallContext context, - ServerStreamListener listener); + void getStreamPrimaryKeys( + CommandGetPrimaryKeys command, CallContext context, ServerStreamListener listener); /** - * Retrieves a description of the foreign key columns that reference the given table's primary key columns - * {@link CommandGetExportedKeys} objects in {@link Result} objects. + * Retrieves a description of the foreign key columns that reference the given table's primary key + * columns {@link CommandGetExportedKeys} objects in {@link Result} objects. * - * @param request request filter parameters. - * @param context Per-call context. + * @param request request filter parameters. + * @param context Per-call context. * @param descriptor The descriptor identifying the data stream. * @return Metadata about the stream. */ - FlightInfo getFlightInfoExportedKeys(CommandGetExportedKeys request, CallContext context, - FlightDescriptor descriptor); + FlightInfo getFlightInfoExportedKeys( + CommandGetExportedKeys request, CallContext context, FlightDescriptor descriptor); /** - * Retrieves a description of the primary key columns that are referenced by given table's foreign key columns - * {@link CommandGetImportedKeys} objects in {@link Result} objects. + * Retrieves a description of the primary key columns that are referenced by given table's foreign + * key columns {@link CommandGetImportedKeys} objects in {@link Result} objects. * - * @param request request filter parameters. - * @param context Per-call context. + * @param request request filter parameters. + * @param context Per-call context. * @param descriptor The descriptor identifying the data stream. * @return Metadata about the stream. */ - FlightInfo getFlightInfoImportedKeys(CommandGetImportedKeys request, CallContext context, - FlightDescriptor descriptor); + FlightInfo getFlightInfoImportedKeys( + CommandGetImportedKeys request, CallContext context, FlightDescriptor descriptor); /** - * Retrieve a description of the foreign key columns that reference the given table's primary key columns - * {@link CommandGetCrossReference} objects in {@link Result} objects. + * Retrieve a description of the foreign key columns that reference the given table's primary key + * columns {@link CommandGetCrossReference} objects in {@link Result} objects. * - * @param request request filter parameters. - * @param context Per-call context. + * @param request request filter parameters. + * @param context Per-call context. * @param descriptor The descriptor identifying the data stream. * @return Metadata about the stream. */ - FlightInfo getFlightInfoCrossReference(CommandGetCrossReference request, CallContext context, - FlightDescriptor descriptor); + FlightInfo getFlightInfoCrossReference( + CommandGetCrossReference request, CallContext context, FlightDescriptor descriptor); /** * Returns data for foreign keys based data stream. * - * @param command The command to generate the data stream. - * @param context Per-call context. + * @param command The command to generate the data stream. + * @param context Per-call context. * @param listener An interface for sending data back to the client. */ - void getStreamExportedKeys(CommandGetExportedKeys command, CallContext context, - ServerStreamListener listener); + void getStreamExportedKeys( + CommandGetExportedKeys command, CallContext context, ServerStreamListener listener); /** * Returns data for foreign keys based data stream. * - * @param command The command to generate the data stream. - * @param context Per-call context. + * @param command The command to generate the data stream. + * @param context Per-call context. * @param listener An interface for sending data back to the client. */ - void getStreamImportedKeys(CommandGetImportedKeys command, CallContext context, - ServerStreamListener listener); + void getStreamImportedKeys( + CommandGetImportedKeys command, CallContext context, ServerStreamListener listener); /** * Returns data for cross reference based data stream. * - * @param command The command to generate the data stream. - * @param context Per-call context. + * @param command The command to generate the data stream. + * @param context Per-call context. * @param listener An interface for sending data back to the client. */ - void getStreamCrossReference(CommandGetCrossReference command, CallContext context, - ServerStreamListener listener); + void getStreamCrossReference( + CommandGetCrossReference command, CallContext context, ServerStreamListener listener); /** * Renew the duration of the given endpoint. @@ -896,107 +960,129 @@ void getStreamCrossReference(CommandGetCrossReference command, CallContext conte * @param context Per-call context. * @param listener An interface for sending data back to the client. */ - default void renewFlightEndpoint(RenewFlightEndpointRequest request, CallContext context, - StreamListener listener) { + default void renewFlightEndpoint( + RenewFlightEndpointRequest request, + CallContext context, + StreamListener listener) { listener.onError(CallStatus.UNIMPLEMENTED.toRuntimeException()); } - /** - * Default schema templates for the {@link FlightSqlProducer}. - */ + /** Default schema templates for the {@link FlightSqlProducer}. */ final class Schemas { - public static final Schema GET_TABLES_SCHEMA = new Schema(asList( - Field.nullable("catalog_name", VARCHAR.getType()), - Field.nullable("db_schema_name", VARCHAR.getType()), - Field.notNullable("table_name", VARCHAR.getType()), - Field.notNullable("table_type", VARCHAR.getType()), - Field.notNullable("table_schema", MinorType.VARBINARY.getType()))); - public static final Schema GET_TABLES_SCHEMA_NO_SCHEMA = new Schema(asList( - Field.nullable("catalog_name", VARCHAR.getType()), - Field.nullable("db_schema_name", VARCHAR.getType()), - Field.notNullable("table_name", VARCHAR.getType()), - Field.notNullable("table_type", VARCHAR.getType()))); - public static final Schema GET_CATALOGS_SCHEMA = new Schema( - singletonList(Field.notNullable("catalog_name", VARCHAR.getType()))); + public static final Schema GET_TABLES_SCHEMA = + new Schema( + asList( + Field.nullable("catalog_name", VARCHAR.getType()), + Field.nullable("db_schema_name", VARCHAR.getType()), + Field.notNullable("table_name", VARCHAR.getType()), + Field.notNullable("table_type", VARCHAR.getType()), + Field.notNullable("table_schema", MinorType.VARBINARY.getType()))); + public static final Schema GET_TABLES_SCHEMA_NO_SCHEMA = + new Schema( + asList( + Field.nullable("catalog_name", VARCHAR.getType()), + Field.nullable("db_schema_name", VARCHAR.getType()), + Field.notNullable("table_name", VARCHAR.getType()), + Field.notNullable("table_type", VARCHAR.getType()))); + public static final Schema GET_CATALOGS_SCHEMA = + new Schema(singletonList(Field.notNullable("catalog_name", VARCHAR.getType()))); public static final Schema GET_TABLE_TYPES_SCHEMA = new Schema(singletonList(Field.notNullable("table_type", VARCHAR.getType()))); public static final Schema GET_SCHEMAS_SCHEMA = - new Schema(asList( - Field.nullable("catalog_name", VARCHAR.getType()), - Field.notNullable("db_schema_name", VARCHAR.getType()))); + new Schema( + asList( + Field.nullable("catalog_name", VARCHAR.getType()), + Field.notNullable("db_schema_name", VARCHAR.getType()))); private static final Schema GET_IMPORTED_EXPORTED_AND_CROSS_REFERENCE_KEYS_SCHEMA = - new Schema(asList( - Field.nullable("pk_catalog_name", VARCHAR.getType()), - Field.nullable("pk_db_schema_name", VARCHAR.getType()), - Field.notNullable("pk_table_name", VARCHAR.getType()), - Field.notNullable("pk_column_name", VARCHAR.getType()), - Field.nullable("fk_catalog_name", VARCHAR.getType()), - Field.nullable("fk_db_schema_name", VARCHAR.getType()), - Field.notNullable("fk_table_name", VARCHAR.getType()), - Field.notNullable("fk_column_name", VARCHAR.getType()), - Field.notNullable("key_sequence", INT.getType()), - Field.nullable("fk_key_name", VARCHAR.getType()), - Field.nullable("pk_key_name", VARCHAR.getType()), - Field.notNullable("update_rule", MinorType.UINT1.getType()), - Field.notNullable("delete_rule", MinorType.UINT1.getType()))); - public static final Schema GET_IMPORTED_KEYS_SCHEMA = GET_IMPORTED_EXPORTED_AND_CROSS_REFERENCE_KEYS_SCHEMA; - public static final Schema GET_EXPORTED_KEYS_SCHEMA = GET_IMPORTED_EXPORTED_AND_CROSS_REFERENCE_KEYS_SCHEMA; - public static final Schema GET_CROSS_REFERENCE_SCHEMA = GET_IMPORTED_EXPORTED_AND_CROSS_REFERENCE_KEYS_SCHEMA; - private static final List GET_SQL_INFO_DENSE_UNION_SCHEMA_FIELDS = asList( - Field.notNullable("string_value", VARCHAR.getType()), - Field.notNullable("bool_value", BIT.getType()), - Field.notNullable("bigint_value", BIGINT.getType()), - Field.notNullable("int32_bitmask", INT.getType()), - new Field( - "string_list", FieldType.notNullable(LIST.getType()), - singletonList(Field.nullable("item", VARCHAR.getType()))), - new Field( - "int32_to_int32_list_map", FieldType.notNullable(new ArrowType.Map(false)), - singletonList(new Field(DATA_VECTOR_NAME, new FieldType(false, STRUCT.getType(), null), - ImmutableList.of( - Field.notNullable(KEY_NAME, INT.getType()), + new Schema( + asList( + Field.nullable("pk_catalog_name", VARCHAR.getType()), + Field.nullable("pk_db_schema_name", VARCHAR.getType()), + Field.notNullable("pk_table_name", VARCHAR.getType()), + Field.notNullable("pk_column_name", VARCHAR.getType()), + Field.nullable("fk_catalog_name", VARCHAR.getType()), + Field.nullable("fk_db_schema_name", VARCHAR.getType()), + Field.notNullable("fk_table_name", VARCHAR.getType()), + Field.notNullable("fk_column_name", VARCHAR.getType()), + Field.notNullable("key_sequence", INT.getType()), + Field.nullable("fk_key_name", VARCHAR.getType()), + Field.nullable("pk_key_name", VARCHAR.getType()), + Field.notNullable("update_rule", MinorType.UINT1.getType()), + Field.notNullable("delete_rule", MinorType.UINT1.getType()))); + public static final Schema GET_IMPORTED_KEYS_SCHEMA = + GET_IMPORTED_EXPORTED_AND_CROSS_REFERENCE_KEYS_SCHEMA; + public static final Schema GET_EXPORTED_KEYS_SCHEMA = + GET_IMPORTED_EXPORTED_AND_CROSS_REFERENCE_KEYS_SCHEMA; + public static final Schema GET_CROSS_REFERENCE_SCHEMA = + GET_IMPORTED_EXPORTED_AND_CROSS_REFERENCE_KEYS_SCHEMA; + private static final List GET_SQL_INFO_DENSE_UNION_SCHEMA_FIELDS = + asList( + Field.notNullable("string_value", VARCHAR.getType()), + Field.notNullable("bool_value", BIT.getType()), + Field.notNullable("bigint_value", BIGINT.getType()), + Field.notNullable("int32_bitmask", INT.getType()), + new Field( + "string_list", + FieldType.notNullable(LIST.getType()), + singletonList(Field.nullable("item", VARCHAR.getType()))), + new Field( + "int32_to_int32_list_map", + FieldType.notNullable(new ArrowType.Map(false)), + singletonList( new Field( - VALUE_NAME, FieldType.nullable(LIST.getType()), - singletonList(Field.nullable("item", INT.getType())))))))); + DATA_VECTOR_NAME, + new FieldType(false, STRUCT.getType(), null), + ImmutableList.of( + Field.notNullable(KEY_NAME, INT.getType()), + new Field( + VALUE_NAME, + FieldType.nullable(LIST.getType()), + singletonList(Field.nullable("item", INT.getType())))))))); public static final Schema GET_SQL_INFO_SCHEMA = - new Schema(asList( - Field.notNullable("info_name", UINT4.getType()), - new Field("value", - FieldType.notNullable( - new Union(UnionMode.Dense, range(0, GET_SQL_INFO_DENSE_UNION_SCHEMA_FIELDS.size()).toArray())), - GET_SQL_INFO_DENSE_UNION_SCHEMA_FIELDS))); + new Schema( + asList( + Field.notNullable("info_name", UINT4.getType()), + new Field( + "value", + FieldType.notNullable( + new Union( + UnionMode.Dense, + range(0, GET_SQL_INFO_DENSE_UNION_SCHEMA_FIELDS.size()).toArray())), + GET_SQL_INFO_DENSE_UNION_SCHEMA_FIELDS))); public static final Schema GET_TYPE_INFO_SCHEMA = - new Schema(asList( - Field.notNullable("type_name", VARCHAR.getType()), - Field.notNullable("data_type", INT.getType()), - Field.nullable("column_size", INT.getType()), - Field.nullable("literal_prefix", VARCHAR.getType()), - Field.nullable("literal_suffix", VARCHAR.getType()), - new Field( - "create_params", FieldType.nullable(LIST.getType()), - singletonList(Field.notNullable("item", VARCHAR.getType()))), - Field.notNullable("nullable", INT.getType()), - Field.notNullable("case_sensitive", BIT.getType()), - Field.notNullable("searchable", INT.getType()), - Field.nullable("unsigned_attribute", BIT.getType()), - Field.notNullable("fixed_prec_scale", BIT.getType()), - Field.nullable("auto_increment", BIT.getType()), - Field.nullable("local_type_name", VARCHAR.getType()), - Field.nullable("minimum_scale", INT.getType()), - Field.nullable("maximum_scale", INT.getType()), - Field.notNullable("sql_data_type", INT.getType()), - Field.nullable("datetime_subcode", INT.getType()), - Field.nullable("num_prec_radix", INT.getType()), - Field.nullable("interval_precision", INT.getType()) - )); + new Schema( + asList( + Field.notNullable("type_name", VARCHAR.getType()), + Field.notNullable("data_type", INT.getType()), + Field.nullable("column_size", INT.getType()), + Field.nullable("literal_prefix", VARCHAR.getType()), + Field.nullable("literal_suffix", VARCHAR.getType()), + new Field( + "create_params", + FieldType.nullable(LIST.getType()), + singletonList(Field.notNullable("item", VARCHAR.getType()))), + Field.notNullable("nullable", INT.getType()), + Field.notNullable("case_sensitive", BIT.getType()), + Field.notNullable("searchable", INT.getType()), + Field.nullable("unsigned_attribute", BIT.getType()), + Field.notNullable("fixed_prec_scale", BIT.getType()), + Field.nullable("auto_increment", BIT.getType()), + Field.nullable("local_type_name", VARCHAR.getType()), + Field.nullable("minimum_scale", INT.getType()), + Field.nullable("maximum_scale", INT.getType()), + Field.notNullable("sql_data_type", INT.getType()), + Field.nullable("datetime_subcode", INT.getType()), + Field.nullable("num_prec_radix", INT.getType()), + Field.nullable("interval_precision", INT.getType()))); public static final Schema GET_PRIMARY_KEYS_SCHEMA = - new Schema(asList( - Field.nullable("catalog_name", VARCHAR.getType()), - Field.nullable("db_schema_name", VARCHAR.getType()), - Field.notNullable("table_name", VARCHAR.getType()), - Field.notNullable("column_name", VARCHAR.getType()), - Field.notNullable("key_sequence", INT.getType()), - Field.nullable("key_name", VARCHAR.getType()))); + new Schema( + asList( + Field.nullable("catalog_name", VARCHAR.getType()), + Field.nullable("db_schema_name", VARCHAR.getType()), + Field.notNullable("table_name", VARCHAR.getType()), + Field.notNullable("column_name", VARCHAR.getType()), + Field.notNullable("key_sequence", INT.getType()), + Field.nullable("key_name", VARCHAR.getType()))); private Schemas() { // Prevent instantiation. diff --git a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlUtils.java b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlUtils.java index 532921a8ac6..e95caec696a 100644 --- a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlUtils.java +++ b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlUtils.java @@ -17,69 +17,73 @@ package org.apache.arrow.flight.sql; -import java.util.List; - -import org.apache.arrow.flight.ActionType; -import org.apache.arrow.flight.CallStatus; - import com.google.common.collect.ImmutableList; import com.google.protobuf.Any; import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.Message; +import java.util.List; +import org.apache.arrow.flight.ActionType; +import org.apache.arrow.flight.CallStatus; -/** - * Utilities to work with Flight SQL semantics. - */ +/** Utilities to work with Flight SQL semantics. */ public final class FlightSqlUtils { public static final ActionType FLIGHT_SQL_BEGIN_SAVEPOINT = - new ActionType("BeginSavepoint", - "Create a new savepoint.\n" + - "Request Message: ActionBeginSavepointRequest\n" + - "Response Message: ActionBeginSavepointResult"); + new ActionType( + "BeginSavepoint", + "Create a new savepoint.\n" + + "Request Message: ActionBeginSavepointRequest\n" + + "Response Message: ActionBeginSavepointResult"); public static final ActionType FLIGHT_SQL_BEGIN_TRANSACTION = - new ActionType("BeginTransaction", - "Start a new transaction.\n" + - "Request Message: ActionBeginTransactionRequest\n" + - "Response Message: ActionBeginTransactionResult"); - public static final ActionType FLIGHT_SQL_CREATE_PREPARED_STATEMENT = new ActionType("CreatePreparedStatement", - "Creates a reusable prepared statement resource on the server. \n" + - "Request Message: ActionCreatePreparedStatementRequest\n" + - "Response Message: ActionCreatePreparedStatementResult"); + new ActionType( + "BeginTransaction", + "Start a new transaction.\n" + + "Request Message: ActionBeginTransactionRequest\n" + + "Response Message: ActionBeginTransactionResult"); + public static final ActionType FLIGHT_SQL_CREATE_PREPARED_STATEMENT = + new ActionType( + "CreatePreparedStatement", + "Creates a reusable prepared statement resource on the server. \n" + + "Request Message: ActionCreatePreparedStatementRequest\n" + + "Response Message: ActionCreatePreparedStatementResult"); - public static final ActionType FLIGHT_SQL_CLOSE_PREPARED_STATEMENT = new ActionType("ClosePreparedStatement", - "Closes a reusable prepared statement resource on the server. \n" + - "Request Message: ActionClosePreparedStatementRequest\n" + - "Response Message: N/A"); + public static final ActionType FLIGHT_SQL_CLOSE_PREPARED_STATEMENT = + new ActionType( + "ClosePreparedStatement", + "Closes a reusable prepared statement resource on the server. \n" + + "Request Message: ActionClosePreparedStatementRequest\n" + + "Response Message: N/A"); public static final ActionType FLIGHT_SQL_CREATE_PREPARED_SUBSTRAIT_PLAN = - new ActionType("CreatePreparedSubstraitPlan", - "Creates a reusable prepared statement resource on the server.\n" + - "Request Message: ActionCreatePreparedSubstraitPlanRequest\n" + - "Response Message: ActionCreatePreparedStatementResult"); + new ActionType( + "CreatePreparedSubstraitPlan", + "Creates a reusable prepared statement resource on the server.\n" + + "Request Message: ActionCreatePreparedSubstraitPlanRequest\n" + + "Response Message: ActionCreatePreparedStatementResult"); public static final ActionType FLIGHT_SQL_CANCEL_QUERY = - new ActionType("CancelQuery", - "Explicitly cancel a running query.\n" + - "Request Message: ActionCancelQueryRequest\n" + - "Response Message: ActionCancelQueryResult"); + new ActionType( + "CancelQuery", + "Explicitly cancel a running query.\n" + + "Request Message: ActionCancelQueryRequest\n" + + "Response Message: ActionCancelQueryResult"); public static final ActionType FLIGHT_SQL_END_SAVEPOINT = - new ActionType("EndSavepoint", - "End a savepoint.\n" + - "Request Message: ActionEndSavepointRequest\n" + - "Response Message: N/A"); + new ActionType( + "EndSavepoint", + "End a savepoint.\n" + + "Request Message: ActionEndSavepointRequest\n" + + "Response Message: N/A"); public static final ActionType FLIGHT_SQL_END_TRANSACTION = - new ActionType("EndTransaction", - "End a transaction.\n" + - "Request Message: ActionEndTransactionRequest\n" + - "Response Message: N/A"); + new ActionType( + "EndTransaction", + "End a transaction.\n" + + "Request Message: ActionEndTransactionRequest\n" + + "Response Message: N/A"); - public static final List FLIGHT_SQL_ACTIONS = ImmutableList.of( - FLIGHT_SQL_CREATE_PREPARED_STATEMENT, - FLIGHT_SQL_CLOSE_PREPARED_STATEMENT - ); + public static final List FLIGHT_SQL_ACTIONS = + ImmutableList.of(FLIGHT_SQL_CREATE_PREPARED_STATEMENT, FLIGHT_SQL_CLOSE_PREPARED_STATEMENT); /** * Helper to parse {@link com.google.protobuf.Any} objects to the specific protobuf object. @@ -102,8 +106,8 @@ public static Any parseOrThrow(byte[] source) { * Helper to unpack {@link com.google.protobuf.Any} objects to the specific protobuf object. * * @param source the parsed Source value. - * @param as the class to unpack as. - * @param the class to unpack as. + * @param as the class to unpack as. + * @param the class to unpack as. * @return the materialized protobuf object. */ public static T unpackOrThrow(Any source, Class as) { @@ -118,11 +122,12 @@ public static T unpackOrThrow(Any source, Class as) { } /** - * Helper to parse and unpack {@link com.google.protobuf.Any} objects to the specific protobuf object. + * Helper to parse and unpack {@link com.google.protobuf.Any} objects to the specific protobuf + * object. * * @param source the raw bytes source value. - * @param as the class to unpack as. - * @param the class to unpack as. + * @param as the class to unpack as. + * @param the class to unpack as. * @return the materialized protobuf object. */ public static T unpackAndParseOrThrow(byte[] source, Class as) { diff --git a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/NoOpFlightSqlProducer.java b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/NoOpFlightSqlProducer.java index a02cee64bd8..64bae109f78 100644 --- a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/NoOpFlightSqlProducer.java +++ b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/NoOpFlightSqlProducer.java @@ -27,195 +27,233 @@ import org.apache.arrow.flight.SchemaResult; import org.apache.arrow.flight.sql.impl.FlightSql; -/** - * A {@link FlightSqlProducer} that throws on all FlightSql-specific operations. - */ +/** A {@link FlightSqlProducer} that throws on all FlightSql-specific operations. */ public class NoOpFlightSqlProducer implements FlightSqlProducer { @Override - public void createPreparedStatement(FlightSql.ActionCreatePreparedStatementRequest request, - CallContext context, StreamListener listener) { - listener.onError(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void createPreparedStatement( + FlightSql.ActionCreatePreparedStatementRequest request, + CallContext context, + StreamListener listener) { + listener.onError( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public void closePreparedStatement(FlightSql.ActionClosePreparedStatementRequest request, - CallContext context, StreamListener listener) { - listener.onError(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void closePreparedStatement( + FlightSql.ActionClosePreparedStatementRequest request, + CallContext context, + StreamListener listener) { + listener.onError( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public FlightInfo getFlightInfoStatement(FlightSql.CommandStatementQuery command, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoStatement( + FlightSql.CommandStatementQuery command, CallContext context, FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public FlightInfo getFlightInfoPreparedStatement(FlightSql.CommandPreparedStatementQuery command, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoPreparedStatement( + FlightSql.CommandPreparedStatementQuery command, + CallContext context, + FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public SchemaResult getSchemaStatement(FlightSql.CommandStatementQuery command, - CallContext context, FlightDescriptor descriptor) { + public SchemaResult getSchemaStatement( + FlightSql.CommandStatementQuery command, CallContext context, FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public void getStreamStatement(FlightSql.TicketStatementQuery ticket, - CallContext context, ServerStreamListener listener) { - listener.error(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void getStreamStatement( + FlightSql.TicketStatementQuery ticket, CallContext context, ServerStreamListener listener) { + listener.error( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public void getStreamPreparedStatement(FlightSql.CommandPreparedStatementQuery command, - CallContext context, ServerStreamListener listener) { - listener.error(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void getStreamPreparedStatement( + FlightSql.CommandPreparedStatementQuery command, + CallContext context, + ServerStreamListener listener) { + listener.error( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public Runnable acceptPutStatement(FlightSql.CommandStatementUpdate command, CallContext context, - FlightStream flightStream, StreamListener ackStream) { + public Runnable acceptPutStatement( + FlightSql.CommandStatementUpdate command, + CallContext context, + FlightStream flightStream, + StreamListener ackStream) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public Runnable acceptPutPreparedStatementUpdate(FlightSql.CommandPreparedStatementUpdate command, - CallContext context, FlightStream flightStream, - StreamListener ackStream) { + public Runnable acceptPutPreparedStatementUpdate( + FlightSql.CommandPreparedStatementUpdate command, + CallContext context, + FlightStream flightStream, + StreamListener ackStream) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public Runnable acceptPutPreparedStatementQuery(FlightSql.CommandPreparedStatementQuery command, CallContext context, - FlightStream flightStream, StreamListener ackStream) { + public Runnable acceptPutPreparedStatementQuery( + FlightSql.CommandPreparedStatementQuery command, + CallContext context, + FlightStream flightStream, + StreamListener ackStream) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public FlightInfo getFlightInfoSqlInfo(FlightSql.CommandGetSqlInfo request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoSqlInfo( + FlightSql.CommandGetSqlInfo request, CallContext context, FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public void getStreamSqlInfo(FlightSql.CommandGetSqlInfo command, CallContext context, - ServerStreamListener listener) { - listener.error(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void getStreamSqlInfo( + FlightSql.CommandGetSqlInfo command, CallContext context, ServerStreamListener listener) { + listener.error( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public FlightInfo getFlightInfoTypeInfo(FlightSql.CommandGetXdbcTypeInfo request, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoTypeInfo( + FlightSql.CommandGetXdbcTypeInfo request, CallContext context, FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public void getStreamTypeInfo(FlightSql.CommandGetXdbcTypeInfo request, - CallContext context, ServerStreamListener listener) { - listener.error(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void getStreamTypeInfo( + FlightSql.CommandGetXdbcTypeInfo request, + CallContext context, + ServerStreamListener listener) { + listener.error( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public FlightInfo getFlightInfoCatalogs(FlightSql.CommandGetCatalogs request, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoCatalogs( + FlightSql.CommandGetCatalogs request, CallContext context, FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override public void getStreamCatalogs(CallContext context, ServerStreamListener listener) { - listener.error(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + listener.error( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public FlightInfo getFlightInfoSchemas(FlightSql.CommandGetDbSchemas request, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoSchemas( + FlightSql.CommandGetDbSchemas request, CallContext context, FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public void getStreamSchemas(FlightSql.CommandGetDbSchemas command, - CallContext context, ServerStreamListener listener) { - listener.error(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void getStreamSchemas( + FlightSql.CommandGetDbSchemas command, CallContext context, ServerStreamListener listener) { + listener.error( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public FlightInfo getFlightInfoTables(FlightSql.CommandGetTables request, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoTables( + FlightSql.CommandGetTables request, CallContext context, FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public void getStreamTables(FlightSql.CommandGetTables command, CallContext context, ServerStreamListener listener) { - listener.error(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void getStreamTables( + FlightSql.CommandGetTables command, CallContext context, ServerStreamListener listener) { + listener.error( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public FlightInfo getFlightInfoTableTypes(FlightSql.CommandGetTableTypes request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoTableTypes( + FlightSql.CommandGetTableTypes request, CallContext context, FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override public void getStreamTableTypes(CallContext context, ServerStreamListener listener) { - listener.error(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + listener.error( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public FlightInfo getFlightInfoPrimaryKeys(FlightSql.CommandGetPrimaryKeys request, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoPrimaryKeys( + FlightSql.CommandGetPrimaryKeys request, CallContext context, FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public void getStreamPrimaryKeys(FlightSql.CommandGetPrimaryKeys command, - CallContext context, ServerStreamListener listener) { - listener.error(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void getStreamPrimaryKeys( + FlightSql.CommandGetPrimaryKeys command, CallContext context, ServerStreamListener listener) { + listener.error( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public FlightInfo getFlightInfoExportedKeys(FlightSql.CommandGetExportedKeys request, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoExportedKeys( + FlightSql.CommandGetExportedKeys request, CallContext context, FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public FlightInfo getFlightInfoImportedKeys(FlightSql.CommandGetImportedKeys request, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoImportedKeys( + FlightSql.CommandGetImportedKeys request, CallContext context, FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public FlightInfo getFlightInfoCrossReference(FlightSql.CommandGetCrossReference request, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoCrossReference( + FlightSql.CommandGetCrossReference request, + CallContext context, + FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public void getStreamExportedKeys(FlightSql.CommandGetExportedKeys command, - CallContext context, ServerStreamListener listener) { - listener.error(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void getStreamExportedKeys( + FlightSql.CommandGetExportedKeys command, + CallContext context, + ServerStreamListener listener) { + listener.error( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public void getStreamImportedKeys(FlightSql.CommandGetImportedKeys command, CallContext context, - ServerStreamListener listener) { - listener.error(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void getStreamImportedKeys( + FlightSql.CommandGetImportedKeys command, + CallContext context, + ServerStreamListener listener) { + listener.error( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public void getStreamCrossReference(FlightSql.CommandGetCrossReference command, CallContext context, - ServerStreamListener listener) { - listener.error(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + public void getStreamCrossReference( + FlightSql.CommandGetCrossReference command, + CallContext context, + ServerStreamListener listener) { + listener.error( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } @Override - public void close() throws Exception { - - } + public void close() throws Exception {} @Override - public void listFlights(CallContext context, Criteria criteria, StreamListener listener) { + public void listFlights( + CallContext context, Criteria criteria, StreamListener listener) { throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } } diff --git a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/ProtoListener.java b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/ProtoListener.java index fd5fd048962..0323f243b1d 100644 --- a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/ProtoListener.java +++ b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/ProtoListener.java @@ -17,11 +17,10 @@ package org.apache.arrow.flight.sql; -import org.apache.arrow.flight.FlightProducer; -import org.apache.arrow.flight.Result; - import com.google.protobuf.Any; import com.google.protobuf.Message; +import org.apache.arrow.flight.FlightProducer; +import org.apache.arrow.flight.Result; /** * A StreamListener that accepts a particular type. diff --git a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/SqlInfoBuilder.java b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/SqlInfoBuilder.java index 338a60e2ae6..60d1377d955 100644 --- a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/SqlInfoBuilder.java +++ b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/SqlInfoBuilder.java @@ -22,6 +22,7 @@ import static org.apache.arrow.flight.sql.impl.FlightSql.SqlSupportedTransaction; import static org.apache.arrow.flight.sql.util.SqlInfoOptionsUtils.createBitmaskFromEnums; +import com.google.protobuf.ProtocolMessageEnum; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; @@ -29,7 +30,6 @@ import java.util.Map; import java.util.function.Consumer; import java.util.function.ObjIntConsumer; - import org.apache.arrow.flight.sql.impl.FlightSql.SqlInfo; import org.apache.arrow.flight.sql.impl.FlightSql.SqlNullOrdering; import org.apache.arrow.flight.sql.impl.FlightSql.SqlOuterJoinsSupportLevel; @@ -59,24 +59,24 @@ import org.apache.arrow.vector.holders.NullableIntHolder; import org.apache.arrow.vector.holders.NullableVarCharHolder; -import com.google.protobuf.ProtocolMessageEnum; - /** - * Auxiliary class meant to facilitate the implementation of {@link FlightSqlProducer#getStreamSqlInfo}. - *

- * Usage requires the user to add the required SqlInfo values using the {@code with*} methods - * like {@link SqlInfoBuilder#withFlightSqlServerName(String)}, and request it back - * through the {@link SqlInfoBuilder#send(List, ServerStreamListener)} method. + * Auxiliary class meant to facilitate the implementation of {@link + * FlightSqlProducer#getStreamSqlInfo}. + * + *

Usage requires the user to add the required SqlInfo values using the {@code with*} methods + * like {@link SqlInfoBuilder#withFlightSqlServerName(String)}, and request it back through the + * {@link SqlInfoBuilder#send(List, ServerStreamListener)} method. */ @SuppressWarnings({"unused"}) public class SqlInfoBuilder { private final Map> providers = new HashMap<>(); /** - * Gets a {@link NullableVarCharHolder} from the provided {@code string} using the provided {@code buf}. + * Gets a {@link NullableVarCharHolder} from the provided {@code string} using the provided {@code + * buf}. * * @param string the {@link StandardCharsets#UTF_8}-encoded text input to store onto the holder. - * @param buf the {@link ArrowBuf} from which to create the new holder. + * @param buf the {@link ArrowBuf} from which to create the new holder. * @return a new {@link NullableVarCharHolder} with the provided input data {@code string}. */ public static NullableVarCharHolder getHolderForUtf8(final String string, final ArrowBuf buf) { @@ -292,7 +292,8 @@ public SqlInfoBuilder withSqlSupportsTableCorrelationNames(final boolean value) /** * Sets a value for {@link SqlInfo#SQL_SUPPORTS_DIFFERENT_TABLE_CORRELATION_NAMES} in the builder. * - * @param value the value for {@link SqlInfo#SQL_SUPPORTS_DIFFERENT_TABLE_CORRELATION_NAMES} to be set. + * @param value the value for {@link SqlInfo#SQL_SUPPORTS_DIFFERENT_TABLE_CORRELATION_NAMES} to be + * set. * @return the SqlInfoBuilder itself. */ public SqlInfoBuilder withSqlSupportsDifferentTableCorrelationNames(final boolean value) { @@ -342,7 +343,8 @@ public SqlInfoBuilder withSqlSupportsNonNullableColumns(final boolean value) { /** * Sets a value for {@link SqlInfo#SQL_SUPPORTS_INTEGRITY_ENHANCEMENT_FACILITY} in the builder. * - * @param value the value for {@link SqlInfo#SQL_SUPPORTS_INTEGRITY_ENHANCEMENT_FACILITY} to be set. + * @param value the value for {@link SqlInfo#SQL_SUPPORTS_INTEGRITY_ENHANCEMENT_FACILITY} to be + * set. * @return the SqlInfoBuilder itself. */ public SqlInfoBuilder withSqlSupportsIntegrityEnhancementFacility(final boolean value) { @@ -412,23 +414,23 @@ public SqlInfoBuilder withSqlTransactionsSupported(final boolean value) { /** * Sets a value for {@link SqlInfo#SQL_DATA_DEFINITION_CAUSES_TRANSACTION_COMMIT} in the builder. * - * @param value the value for {@link SqlInfo#SQL_DATA_DEFINITION_CAUSES_TRANSACTION_COMMIT} to be set. + * @param value the value for {@link SqlInfo#SQL_DATA_DEFINITION_CAUSES_TRANSACTION_COMMIT} to be + * set. * @return the SqlInfoBuilder itself. */ public SqlInfoBuilder withSqlDataDefinitionCausesTransactionCommit(final boolean value) { - return withBooleanProvider(SqlInfo.SQL_DATA_DEFINITION_CAUSES_TRANSACTION_COMMIT_VALUE, - value); + return withBooleanProvider(SqlInfo.SQL_DATA_DEFINITION_CAUSES_TRANSACTION_COMMIT_VALUE, value); } /** * Sets a value for {@link SqlInfo#SQL_DATA_DEFINITIONS_IN_TRANSACTIONS_IGNORED} in the builder. * - * @param value the value for {@link SqlInfo#SQL_DATA_DEFINITIONS_IN_TRANSACTIONS_IGNORED} to be set. + * @param value the value for {@link SqlInfo#SQL_DATA_DEFINITIONS_IN_TRANSACTIONS_IGNORED} to be + * set. * @return the SqlInfoBuilder itself. */ public SqlInfoBuilder withSqlDataDefinitionsInTransactionsIgnored(final boolean value) { - return withBooleanProvider(SqlInfo.SQL_DATA_DEFINITIONS_IN_TRANSACTIONS_IGNORED_VALUE, - value); + return withBooleanProvider(SqlInfo.SQL_DATA_DEFINITIONS_IN_TRANSACTIONS_IGNORED_VALUE, value); } /** @@ -472,14 +474,16 @@ public SqlInfoBuilder withSqlLocatorsUpdateCopy(final boolean value) { } /** - * Sets a value for {@link SqlInfo#SQL_STORED_FUNCTIONS_USING_CALL_SYNTAX_SUPPORTED} in the builder. + * Sets a value for {@link SqlInfo#SQL_STORED_FUNCTIONS_USING_CALL_SYNTAX_SUPPORTED} in the + * builder. * - * @param value the value for {@link SqlInfo#SQL_STORED_FUNCTIONS_USING_CALL_SYNTAX_SUPPORTED} to be set. + * @param value the value for {@link SqlInfo#SQL_STORED_FUNCTIONS_USING_CALL_SYNTAX_SUPPORTED} to + * be set. * @return the SqlInfoBuilder itself. */ public SqlInfoBuilder withSqlStoredFunctionsUsingCallSyntaxSupported(final boolean value) { - return withBooleanProvider(SqlInfo.SQL_STORED_FUNCTIONS_USING_CALL_SYNTAX_SUPPORTED_VALUE, - value); + return withBooleanProvider( + SqlInfo.SQL_STORED_FUNCTIONS_USING_CALL_SYNTAX_SUPPORTED_VALUE, value); } /** @@ -758,7 +762,8 @@ public SqlInfoBuilder withSqlSupportedGrammar(final SupportedSqlGrammar... value * @param values the value for {@link SqlInfo#SQL_ANSI92_SUPPORTED_LEVEL} to be set. * @return the SqlInfoBuilder itself. */ - public SqlInfoBuilder withSqlAnsi92SupportedLevel(final SupportedAnsi92SqlGrammarLevel... values) { + public SqlInfoBuilder withSqlAnsi92SupportedLevel( + final SupportedAnsi92SqlGrammarLevel... values) { return withEnumProvider(SqlInfo.SQL_ANSI92_SUPPORTED_LEVEL_VALUE, values); } @@ -778,7 +783,8 @@ public SqlInfoBuilder withSqlSchemasSupportedActions(final SqlSupportedElementAc * @param values the value for {@link SqlInfo#SQL_CATALOGS_SUPPORTED_ACTIONS} to be set. * @return the SqlInfoBuilder itself. */ - public SqlInfoBuilder withSqlCatalogsSupportedActions(final SqlSupportedElementActions... values) { + public SqlInfoBuilder withSqlCatalogsSupportedActions( + final SqlSupportedElementActions... values) { return withEnumProvider(SqlInfo.SQL_CATALOGS_SUPPORTED_ACTIONS_VALUE, values); } @@ -788,7 +794,8 @@ public SqlInfoBuilder withSqlCatalogsSupportedActions(final SqlSupportedElementA * @param values the value for {@link SqlInfo#SQL_SUPPORTED_POSITIONED_COMMANDS} to be set. * @return the SqlInfoBuilder itself. */ - public SqlInfoBuilder withSqlSupportedPositionedCommands(final SqlSupportedPositionedCommands... values) { + public SqlInfoBuilder withSqlSupportedPositionedCommands( + final SqlSupportedPositionedCommands... values) { return withEnumProvider(SqlInfo.SQL_SUPPORTED_POSITIONED_COMMANDS_VALUE, values); } @@ -825,10 +832,12 @@ public SqlInfoBuilder withSqlOuterJoinSupportLevel(final SqlOuterJoinsSupportLev /** * Sets a value for {@link SqlInfo#SQL_SUPPORTED_TRANSACTIONS_ISOLATION_LEVELS} in the builder. * - * @param values the values for {@link SqlInfo#SQL_SUPPORTED_TRANSACTIONS_ISOLATION_LEVELS} to be set. + * @param values the values for {@link SqlInfo#SQL_SUPPORTED_TRANSACTIONS_ISOLATION_LEVELS} to be + * set. * @return the SqlInfoBuilder itself. */ - public SqlInfoBuilder withSqlSupportedTransactionsIsolationLevels(final SqlTransactionIsolationLevel... values) { + public SqlInfoBuilder withSqlSupportedTransactionsIsolationLevels( + final SqlTransactionIsolationLevel... values) { return withEnumProvider(SqlInfo.SQL_SUPPORTED_TRANSACTIONS_ISOLATION_LEVELS_VALUE, values); } @@ -839,8 +848,7 @@ public SqlInfoBuilder withSqlSupportedTransactionsIsolationLevels(final SqlTrans * @return the SqlInfoBuilder itself. */ public SqlInfoBuilder withSqlSupportedResultSetTypes(final SqlSupportedResultSetType... values) { - return withEnumProvider(SqlInfo.SQL_SUPPORTED_RESULT_SET_TYPES_VALUE, values - ); + return withEnumProvider(SqlInfo.SQL_SUPPORTED_RESULT_SET_TYPES_VALUE, values); } /** @@ -921,8 +929,7 @@ private SqlInfoBuilder withBitIntProvider(final int sqlInfo, final long value) { return this; } - private SqlInfoBuilder withBooleanProvider(final int sqlInfo, - final boolean value) { + private SqlInfoBuilder withBooleanProvider(final int sqlInfo, final boolean value) { addProvider(sqlInfo, (root, index) -> setDataForBooleanField(root, index, sqlInfo, value)); return this; } @@ -932,14 +939,13 @@ private SqlInfoBuilder withStringProvider(final int sqlInfo, final String value) return this; } - private SqlInfoBuilder withStringArrayProvider(final int sqlInfo, - final String[] value) { + private SqlInfoBuilder withStringArrayProvider(final int sqlInfo, final String[] value) { addProvider(sqlInfo, (root, index) -> setDataVarCharListField(root, index, sqlInfo, value)); return this; } - private SqlInfoBuilder withIntToIntListMapProvider(final int sqlInfo, - final Map> value) { + private SqlInfoBuilder withIntToIntListMapProvider( + final int sqlInfo, final Map> value) { addProvider(sqlInfo, (root, index) -> setIntToIntListMapField(root, index, sqlInfo, value)); return this; } @@ -947,7 +953,7 @@ private SqlInfoBuilder withIntToIntListMapProvider(final int sqlInfo, /** * Send the requested information to given ServerStreamListener. * - * @param infos List of SqlInfo to be sent. + * @param infos List of SqlInfo to be sent. * @param listener ServerStreamListener to send data to. */ public void send(List infos, final ServerStreamListener listener) { @@ -955,9 +961,8 @@ public void send(List infos, final ServerStreamListener listener) { infos = new ArrayList<>(providers.keySet()); } try (final BufferAllocator allocator = new RootAllocator(); - final VectorSchemaRoot root = VectorSchemaRoot.create( - FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA, - allocator)) { + final VectorSchemaRoot root = + VectorSchemaRoot.create(FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA, allocator)) { final int rows = infos.size(); for (int i = 0; i < rows; i++) { providers.get(infos.get(i)).accept(root, i); @@ -977,8 +982,11 @@ private void setInfoName(final VectorSchemaRoot root, final int index, final int infoName.setSafe(index, info); } - private void setValues(final VectorSchemaRoot root, final int index, final byte typeId, - final Consumer dataSetter) { + private void setValues( + final VectorSchemaRoot root, + final int index, + final byte typeId, + final Consumer dataSetter) { final DenseUnionVector values = (DenseUnionVector) root.getVector("value"); values.setTypeId(index, typeId); dataSetter.accept(values); @@ -991,23 +999,24 @@ private void setValues(final VectorSchemaRoot root, final int index, final byte */ private void onCreateArrowBuf(final Consumer executor) { try (final BufferAllocator allocator = new RootAllocator(); - final ArrowBuf buf = allocator.buffer(1024)) { + final ArrowBuf buf = allocator.buffer(1024)) { executor.accept(buf); } } - private void setDataForUtf8Field(final VectorSchemaRoot root, final int index, - final int sqlInfo, final String value) { + private void setDataForUtf8Field( + final VectorSchemaRoot root, final int index, final int sqlInfo, final String value) { setInfoName(root, index, sqlInfo); - onCreateArrowBuf(buf -> { - final Consumer producer = - values -> values.setSafe(index, getHolderForUtf8(value, buf)); - setValues(root, index, (byte) 0, producer); - }); + onCreateArrowBuf( + buf -> { + final Consumer producer = + values -> values.setSafe(index, getHolderForUtf8(value, buf)); + setValues(root, index, (byte) 0, producer); + }); } - private void setDataForIntField(final VectorSchemaRoot root, final int index, - final int sqlInfo, final int value) { + private void setDataForIntField( + final VectorSchemaRoot root, final int index, final int sqlInfo, final int value) { setInfoName(root, index, sqlInfo); final NullableIntHolder dataHolder = new NullableIntHolder(); dataHolder.isSet = 1; @@ -1015,8 +1024,8 @@ private void setDataForIntField(final VectorSchemaRoot root, final int index, setValues(root, index, (byte) 3, values -> values.setSafe(index, dataHolder)); } - private void setDataForBigIntField(final VectorSchemaRoot root, final int index, - final int sqlInfo, final long value) { + private void setDataForBigIntField( + final VectorSchemaRoot root, final int index, final int sqlInfo, final long value) { setInfoName(root, index, sqlInfo); final NullableBigIntHolder dataHolder = new NullableBigIntHolder(); dataHolder.isSet = 1; @@ -1024,8 +1033,8 @@ private void setDataForBigIntField(final VectorSchemaRoot root, final int index, setValues(root, index, (byte) 2, values -> values.setSafe(index, dataHolder)); } - private void setDataForBooleanField(final VectorSchemaRoot root, final int index, - final int sqlInfo, final boolean value) { + private void setDataForBooleanField( + final VectorSchemaRoot root, final int index, final int sqlInfo, final boolean value) { setInfoName(root, index, sqlInfo); final NullableBitHolder dataHolder = new NullableBitHolder(); dataHolder.isSet = 1; @@ -1033,9 +1042,8 @@ private void setDataForBooleanField(final VectorSchemaRoot root, final int index setValues(root, index, (byte) 1, values -> values.setSafe(index, dataHolder)); } - private void setDataVarCharListField(final VectorSchemaRoot root, final int index, - final int sqlInfo, - final String[] values) { + private void setDataVarCharListField( + final VectorSchemaRoot root, final int index, final int sqlInfo, final String[] values) { final DenseUnionVector denseUnion = (DenseUnionVector) root.getVector("value"); final ListVector listVector = denseUnion.getList((byte) 4); final int listIndex = listVector.getValueCount(); @@ -1049,11 +1057,14 @@ private void setDataVarCharListField(final VectorSchemaRoot root, final int inde writer.startList(); final int length = values.length; range(0, length) - .forEach(i -> onCreateArrowBuf(buf -> { - final byte[] bytes = values[i].getBytes(StandardCharsets.UTF_8); - buf.setBytes(0, bytes); - writer.writeVarChar(0, bytes.length, buf); - })); + .forEach( + i -> + onCreateArrowBuf( + buf -> { + final byte[] bytes = values[i].getBytes(StandardCharsets.UTF_8); + buf.setBytes(0, bytes); + writer.writeVarChar(0, bytes.length, buf); + })); writer.endList(); writer.setValueCount(listVectorValueCount); @@ -1062,9 +1073,11 @@ private void setDataVarCharListField(final VectorSchemaRoot root, final int inde setInfoName(root, index, sqlInfo); } - private void setIntToIntListMapField(final VectorSchemaRoot root, final int index, - final int sqlInfo, - final Map> values) { + private void setIntToIntListMapField( + final VectorSchemaRoot root, + final int index, + final int sqlInfo, + final Map> values) { final DenseUnionVector denseUnion = (DenseUnionVector) root.getVector("value"); final MapVector mapVector = denseUnion.getMap((byte) 5); final int mapIndex = mapVector.getValueCount(); @@ -1074,17 +1087,18 @@ private void setIntToIntListMapField(final VectorSchemaRoot root, final int inde final UnionMapWriter mapWriter = mapVector.getWriter(); mapWriter.setPosition(mapIndex); mapWriter.startMap(); - values.forEach((key, value) -> { - mapWriter.startEntry(); - mapWriter.key().integer().writeInt(key); - final BaseWriter.ListWriter listWriter = mapWriter.value().list(); - listWriter.startList(); - for (final int v : value) { - listWriter.integer().writeInt(v); - } - listWriter.endList(); - mapWriter.endEntry(); - }); + values.forEach( + (key, value) -> { + mapWriter.startEntry(); + mapWriter.key().integer().writeInt(key); + final BaseWriter.ListWriter listWriter = mapWriter.value().list(); + listWriter.startList(); + for (final int v : value) { + listWriter.integer().writeInt(v); + } + listWriter.endList(); + mapWriter.endEntry(); + }); mapWriter.endMap(); mapWriter.setValueCount(mapIndex + 1); diff --git a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/example/FlightSqlClientDemoApp.java b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/example/FlightSqlClientDemoApp.java index 4d3241c7c37..20bd8462cda 100644 --- a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/example/FlightSqlClientDemoApp.java +++ b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/example/FlightSqlClientDemoApp.java @@ -19,7 +19,6 @@ import java.util.ArrayList; import java.util.List; - import org.apache.arrow.flight.CallOption; import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightInfo; @@ -37,9 +36,7 @@ import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; -/** - * Flight SQL Client Demo CLI Application. - */ +/** Flight SQL Client Demo CLI Application. */ public class FlightSqlClientDemoApp implements AutoCloseable { public final List callOptions = new ArrayList<>(); public final BufferAllocator allocator; @@ -67,7 +64,8 @@ public static void main(final String[] args) throws Exception { try { cmd = parser.parse(options, args); - try (final FlightSqlClientDemoApp thisApp = new FlightSqlClientDemoApp(new RootAllocator(Integer.MAX_VALUE))) { + try (final FlightSqlClientDemoApp thisApp = + new FlightSqlClientDemoApp(new RootAllocator(Integer.MAX_VALUE))) { thisApp.executeApp(cmd); } @@ -79,8 +77,8 @@ public static void main(final String[] args) throws Exception { } /** - * Gets the current {@link CallOption} as an array; usually used as an - * argument in {@link FlightSqlClient} methods. + * Gets the current {@link CallOption} as an array; usually used as an argument in {@link + * FlightSqlClient} methods. * * @return current {@link CallOption} array. */ @@ -89,12 +87,12 @@ public CallOption[] getCallOptions() { } /** - * Calls {@link FlightSqlClientDemoApp#createFlightSqlClient(String, int)} - * in order to create a {@link FlightSqlClient} to be used in future calls, - * and then calls {@link FlightSqlClientDemoApp#executeCommand(CommandLine)} - * to execute the command parsed at execution. + * Calls {@link FlightSqlClientDemoApp#createFlightSqlClient(String, int)} in order to create a + * {@link FlightSqlClient} to be used in future calls, and then calls {@link + * FlightSqlClientDemoApp#executeCommand(CommandLine)} to execute the command parsed at execution. * - * @param cmd parsed {@link CommandLine}; often the result of {@link DefaultParser#parse(Options, String[])}. + * @param cmd parsed {@link CommandLine}; often the result of {@link DefaultParser#parse(Options, + * String[])}. */ public void executeApp(final CommandLine cmd) throws Exception { final String host = cmd.getOptionValue("host").trim(); @@ -107,29 +105,22 @@ public void executeApp(final CommandLine cmd) throws Exception { /** * Parses the "{@code command}" CLI argument and redirects to the appropriate method. * - * @param cmd parsed {@link CommandLine}; often the result of - * {@link DefaultParser#parse(Options, String[])}. + * @param cmd parsed {@link CommandLine}; often the result of {@link DefaultParser#parse(Options, + * String[])}. */ public void executeCommand(CommandLine cmd) throws Exception { switch (cmd.getOptionValue("command").trim()) { case "Execute": - exampleExecute( - cmd.getOptionValue("query") - ); + exampleExecute(cmd.getOptionValue("query")); break; case "ExecuteUpdate": - exampleExecuteUpdate( - cmd.getOptionValue("query") - ); + exampleExecuteUpdate(cmd.getOptionValue("query")); break; case "GetCatalogs": exampleGetCatalogs(); break; case "GetSchemas": - exampleGetSchemas( - cmd.getOptionValue("catalog"), - cmd.getOptionValue("schema") - ); + exampleGetSchemas(cmd.getOptionValue("catalog"), cmd.getOptionValue("schema")); break; case "GetTableTypes": exampleGetTableTypes(); @@ -138,41 +129,38 @@ public void executeCommand(CommandLine cmd) throws Exception { exampleGetTables( cmd.getOptionValue("catalog"), cmd.getOptionValue("schema"), - cmd.getOptionValue("table") - ); + cmd.getOptionValue("table")); break; case "GetExportedKeys": exampleGetExportedKeys( cmd.getOptionValue("catalog"), cmd.getOptionValue("schema"), - cmd.getOptionValue("table") - ); + cmd.getOptionValue("table")); break; case "GetImportedKeys": exampleGetImportedKeys( cmd.getOptionValue("catalog"), cmd.getOptionValue("schema"), - cmd.getOptionValue("table") - ); + cmd.getOptionValue("table")); break; case "GetPrimaryKeys": exampleGetPrimaryKeys( cmd.getOptionValue("catalog"), cmd.getOptionValue("schema"), - cmd.getOptionValue("table") - ); + cmd.getOptionValue("table")); break; default: - System.out.println("Command used is not valid! Please use one of: \n" + - "[\"ExecuteUpdate\",\n" + - "\"Execute\",\n" + - "\"GetCatalogs\",\n" + - "\"GetSchemas\",\n" + - "\"GetTableTypes\",\n" + - "\"GetTables\",\n" + - "\"GetExportedKeys\",\n" + - "\"GetImportedKeys\",\n" + - "\"GetPrimaryKeys\"]"); + System.out.println( + "Command used is not valid! Please use one of: \n" + + "[\"ExecuteUpdate\",\n" + + "\"Execute\",\n" + + "\"GetCatalogs\",\n" + + "\"GetSchemas\",\n" + + "\"GetTableTypes\",\n" + + "\"GetTables\",\n" + + "\"GetExportedKeys\",\n" + + "\"GetImportedKeys\",\n" + + "\"GetPrimaryKeys\"]"); } } @@ -192,7 +180,8 @@ private void exampleExecute(final String query) throws Exception { } private void exampleExecuteUpdate(final String query) { - System.out.println("Updated: " + flightSqlClient.executeUpdate(query, getCallOptions()) + "rows."); + System.out.println( + "Updated: " + flightSqlClient.executeUpdate(query, getCallOptions()) + "rows."); } private void exampleGetCatalogs() throws Exception { @@ -207,22 +196,29 @@ private void exampleGetTableTypes() throws Exception { printFlightInfoResults(flightSqlClient.getTableTypes(getCallOptions())); } - private void exampleGetTables(final String catalog, final String schema, final String table) throws Exception { + private void exampleGetTables(final String catalog, final String schema, final String table) + throws Exception { // For now, this won't filter by table types. - printFlightInfoResults(flightSqlClient.getTables( - catalog, schema, table, null, false, getCallOptions())); + printFlightInfoResults( + flightSqlClient.getTables(catalog, schema, table, null, false, getCallOptions())); } - private void exampleGetExportedKeys(final String catalog, final String schema, final String table) throws Exception { - printFlightInfoResults(flightSqlClient.getExportedKeys(TableRef.of(catalog, schema, table), getCallOptions())); + private void exampleGetExportedKeys(final String catalog, final String schema, final String table) + throws Exception { + printFlightInfoResults( + flightSqlClient.getExportedKeys(TableRef.of(catalog, schema, table), getCallOptions())); } - private void exampleGetImportedKeys(final String catalog, final String schema, final String table) throws Exception { - printFlightInfoResults(flightSqlClient.getImportedKeys(TableRef.of(catalog, schema, table), getCallOptions())); + private void exampleGetImportedKeys(final String catalog, final String schema, final String table) + throws Exception { + printFlightInfoResults( + flightSqlClient.getImportedKeys(TableRef.of(catalog, schema, table), getCallOptions())); } - private void exampleGetPrimaryKeys(final String catalog, final String schema, final String table) throws Exception { - printFlightInfoResults(flightSqlClient.getPrimaryKeys(TableRef.of(catalog, schema, table), getCallOptions())); + private void exampleGetPrimaryKeys(final String catalog, final String schema, final String table) + throws Exception { + printFlightInfoResults( + flightSqlClient.getPrimaryKeys(TableRef.of(catalog, schema, table), getCallOptions())); } private void printFlightInfoResults(final FlightInfo flightInfo) throws Exception { diff --git a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/util/SqlInfoOptionsUtils.java b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/util/SqlInfoOptionsUtils.java index c43c48eb8e0..223e9bad54f 100644 --- a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/util/SqlInfoOptionsUtils.java +++ b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/util/SqlInfoOptionsUtils.java @@ -17,31 +17,29 @@ package org.apache.arrow.flight.sql.util; +import com.google.protobuf.ProtocolMessageEnum; import java.util.Arrays; import java.util.Collection; - import org.apache.arrow.flight.sql.FlightSqlClient; import org.apache.arrow.flight.sql.impl.FlightSql.SqlInfo; -import com.google.protobuf.ProtocolMessageEnum; - -/** - * Utility class for {@link SqlInfo} and {@link FlightSqlClient#getSqlInfo} option parsing. - */ +/** Utility class for {@link SqlInfo} and {@link FlightSqlClient#getSqlInfo} option parsing. */ public final class SqlInfoOptionsUtils { private SqlInfoOptionsUtils() { // Prevent instantiation. } /** - * Returns whether the provided {@code bitmask} points to the provided {@link ProtocolMessageEnum} by comparing - * {@link ProtocolMessageEnum#getNumber} with the respective bit index of the {@code bitmask}. + * Returns whether the provided {@code bitmask} points to the provided {@link ProtocolMessageEnum} + * by comparing {@link ProtocolMessageEnum#getNumber} with the respective bit index of the {@code + * bitmask}. * * @param enumInstance the protobuf message enum to use. - * @param bitmask the bitmask response from {@link FlightSqlClient#getSqlInfo}. + * @param bitmask the bitmask response from {@link FlightSqlClient#getSqlInfo}. * @return whether the provided {@code bitmask} points to the specified {@code enumInstance}. */ - public static boolean doesBitmaskTranslateToEnum(final ProtocolMessageEnum enumInstance, final long bitmask) { + public static boolean doesBitmaskTranslateToEnum( + final ProtocolMessageEnum enumInstance, final long bitmask) { return ((bitmask >> enumInstance.getNumber()) & 1) == 1; } diff --git a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/util/TableRef.java b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/util/TableRef.java index b3751cab903..f4e97a5317e 100644 --- a/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/util/TableRef.java +++ b/java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/util/TableRef.java @@ -17,10 +17,7 @@ package org.apache.arrow.flight.sql.util; -/** - * A helper class to reference a table to be passed to the flight - * sql client. - */ +/** A helper class to reference a table to be passed to the flight sql client. */ public class TableRef { private final String catalog; private final String dbSchema; @@ -28,9 +25,10 @@ public class TableRef { /** * The complete constructor for the TableRef class. - * @param catalog the catalog from a table. - * @param dbSchema the database schema from a table. - * @param table the table name from a table. + * + * @param catalog the catalog from a table. + * @param dbSchema the database schema from a table. + * @param table the table name from a table. */ public TableRef(String catalog, String dbSchema, String table) { this.catalog = catalog; @@ -40,10 +38,11 @@ public TableRef(String catalog, String dbSchema, String table) { /** * A static initializer of the TableRef with all the arguments. - * @param catalog the catalog from a table. - * @param dbSchema the database schema from a table. - * @param table the table name from a table. - * @return A TableRef object. + * + * @param catalog the catalog from a table. + * @param dbSchema the database schema from a table. + * @param table the table name from a table. + * @return A TableRef object. */ public static TableRef of(String catalog, String dbSchema, String table) { return new TableRef(catalog, dbSchema, table); @@ -51,7 +50,8 @@ public static TableRef of(String catalog, String dbSchema, String table) { /** * Retrieve the catalog from the object. - * @return the catalog. + * + * @return the catalog. */ public String getCatalog() { return catalog; @@ -59,7 +59,8 @@ public String getCatalog() { /** * Retrieves the db schema from the object. - * @return the dbSchema + * + * @return the dbSchema */ public String getDbSchema() { return dbSchema; @@ -67,10 +68,10 @@ public String getDbSchema() { /** * Retrieves the table from the object. - * @return the table. + * + * @return the table. */ public String getTable() { return table; } } - diff --git a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/FlightSqlExample.java b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/FlightSqlExample.java index 1d43728b789..a2237f8db60 100644 --- a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/FlightSqlExample.java +++ b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/FlightSqlExample.java @@ -38,6 +38,15 @@ import static org.apache.arrow.util.Preconditions.checkState; import static org.slf4j.LoggerFactory.getLogger; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.RemovalListener; +import com.google.common.cache.RemovalNotification; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.protobuf.ByteString; +import com.google.protobuf.Message; +import com.google.protobuf.ProtocolStringList; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; @@ -75,7 +84,6 @@ import java.util.function.Consumer; import java.util.function.Predicate; import java.util.stream.Stream; - import org.apache.arrow.adapter.jdbc.ArrowVectorIterator; import org.apache.arrow.adapter.jdbc.JdbcFieldInfo; import org.apache.arrow.adapter.jdbc.JdbcParameterBinder; @@ -141,19 +149,9 @@ import org.apache.commons.pool2.impl.GenericObjectPool; import org.slf4j.Logger; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.RemovalListener; -import com.google.common.cache.RemovalNotification; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.protobuf.ByteString; -import com.google.protobuf.Message; -import com.google.protobuf.ProtocolStringList; - /** - * Example {@link FlightSqlProducer} implementation showing an Apache Derby backed Flight SQL server that generally - * supports all current features of Flight SQL. + * Example {@link FlightSqlProducer} implementation showing an Apache Derby backed Flight SQL server + * that generally supports all current features of Flight SQL. */ public class FlightSqlExample implements FlightSqlProducer, AutoCloseable { private static final String DATABASE_URI = "jdbc:derby:target/derbyDB"; @@ -164,7 +162,8 @@ public class FlightSqlExample implements FlightSqlProducer, AutoCloseable { private final Location location; private final PoolingDataSource dataSource; private final BufferAllocator rootAllocator = new RootAllocator(); - private final Cache> preparedStatementLoadingCache; + private final Cache> + preparedStatementLoadingCache; private final Cache> statementLoadingCache; private final SqlInfoBuilder sqlInfoBuilder; @@ -173,7 +172,8 @@ public static void main(String[] args) throws Exception { final FlightSqlExample example = new FlightSqlExample(location); Location listenLocation = Location.forGrpcInsecure("0.0.0.0", 55555); try (final BufferAllocator allocator = new RootAllocator(); - final FlightServer server = FlightServer.builder(allocator, listenLocation, example).build()) { + final FlightServer server = + FlightServer.builder(allocator, listenLocation, example).build()) { server.start(); server.awaitTermination(); } @@ -188,7 +188,8 @@ public FlightSqlExample(final Location location) { new DriverManagerConnectionFactory(DATABASE_URI, new Properties()); final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null); - final ObjectPool connectionPool = new GenericObjectPool<>(poolableConnectionFactory); + final ObjectPool connectionPool = + new GenericObjectPool<>(poolableConnectionFactory); poolableConnectionFactory.setPool(connectionPool); // PoolingDataSource takes ownership of `connectionPool` @@ -214,7 +215,8 @@ public FlightSqlExample(final Location location) { try (final Connection connection = dataSource.getConnection()) { final DatabaseMetaData metaData = connection.getMetaData(); - sqlInfoBuilder.withFlightSqlServerName(metaData.getDatabaseProductName()) + sqlInfoBuilder + .withFlightSqlServerName(metaData.getDatabaseProductName()) .withFlightSqlServerVersion(metaData.getDatabaseProductVersion()) .withFlightSqlServerArrowVersion(metaData.getDriverVersion()) .withFlightSqlServerReadOnly(metaData.isReadOnly()) @@ -223,29 +225,30 @@ public FlightSqlExample(final Location location) { .withFlightSqlServerTransaction(SqlSupportedTransaction.SQL_SUPPORTED_TRANSACTION_NONE) .withSqlIdentifierQuoteChar(metaData.getIdentifierQuoteString()) .withSqlDdlCatalog(metaData.supportsCatalogsInDataManipulation()) - .withSqlDdlSchema( metaData.supportsSchemasInDataManipulation()) - .withSqlDdlTable( metaData.allTablesAreSelectable()) - .withSqlIdentifierCase(metaData.storesMixedCaseIdentifiers() ? - SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_CASE_INSENSITIVE : - metaData.storesUpperCaseIdentifiers() ? - SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_UPPERCASE : - metaData.storesLowerCaseIdentifiers() ? - SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_LOWERCASE : - SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_UNKNOWN) - .withSqlQuotedIdentifierCase(metaData.storesMixedCaseQuotedIdentifiers() ? - SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_CASE_INSENSITIVE : - metaData.storesUpperCaseQuotedIdentifiers() ? - SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_UPPERCASE : - metaData.storesLowerCaseQuotedIdentifiers() ? - SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_LOWERCASE : - SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_UNKNOWN) + .withSqlDdlSchema(metaData.supportsSchemasInDataManipulation()) + .withSqlDdlTable(metaData.allTablesAreSelectable()) + .withSqlIdentifierCase( + metaData.storesMixedCaseIdentifiers() + ? SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_CASE_INSENSITIVE + : metaData.storesUpperCaseIdentifiers() + ? SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_UPPERCASE + : metaData.storesLowerCaseIdentifiers() + ? SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_LOWERCASE + : SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_UNKNOWN) + .withSqlQuotedIdentifierCase( + metaData.storesMixedCaseQuotedIdentifiers() + ? SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_CASE_INSENSITIVE + : metaData.storesUpperCaseQuotedIdentifiers() + ? SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_UPPERCASE + : metaData.storesLowerCaseQuotedIdentifiers() + ? SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_LOWERCASE + : SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_UNKNOWN) .withSqlAllTablesAreSelectable(true) .withSqlNullOrdering(SqlNullOrdering.SQL_NULLS_SORTED_AT_END) .withSqlMaxColumnsInTable(42); } catch (SQLException e) { throw new RuntimeException(e); } - } private static boolean removeDerbyDatabaseIfExists() { @@ -261,8 +264,12 @@ private static boolean removeDerbyDatabaseIfExists() { * If for whatever reason the resulting `Stream` is empty, throw an `IOException`; * this not expected. */ - wasSuccess = walk.sorted(Comparator.reverseOrder()).map(Path::toFile).map(File::delete) - .reduce(Boolean::logicalAnd).orElseThrow(IOException::new); + wasSuccess = + walk.sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .map(File::delete) + .reduce(Boolean::logicalAnd) + .orElseThrow(IOException::new); } catch (IOException e) { /* * The only acceptable scenario for an `IOException` to be thrown here is if @@ -278,23 +285,27 @@ private static boolean removeDerbyDatabaseIfExists() { } private static boolean populateDerbyDatabase() { - try (final Connection connection = DriverManager.getConnection("jdbc:derby:target/derbyDB;create=true"); - Statement statement = connection.createStatement()) { - statement.execute("CREATE TABLE foreignTable (" + - "id INT not null primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), " + - "foreignName varchar(100), " + - "value int)"); - statement.execute("CREATE TABLE intTable (" + - "id INT not null primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), " + - "keyName varchar(100), " + - "value int, " + - "foreignId int references foreignTable(id))"); + try (final Connection connection = + DriverManager.getConnection("jdbc:derby:target/derbyDB;create=true"); + Statement statement = connection.createStatement()) { + statement.execute( + "CREATE TABLE foreignTable (" + + "id INT not null primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), " + + "foreignName varchar(100), " + + "value int)"); + statement.execute( + "CREATE TABLE intTable (" + + "id INT not null primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), " + + "keyName varchar(100), " + + "value int, " + + "foreignId int references foreignTable(id))"); statement.execute("INSERT INTO foreignTable (foreignName, value) VALUES ('keyOne', 1)"); statement.execute("INSERT INTO foreignTable (foreignName, value) VALUES ('keyTwo', 0)"); statement.execute("INSERT INTO foreignTable (foreignName, value) VALUES ('keyThree', -1)"); statement.execute("INSERT INTO intTable (keyName, value, foreignId) VALUES ('one', 1, 1)"); statement.execute("INSERT INTO intTable (keyName, value, foreignId) VALUES ('zero', 0, 1)"); - statement.execute("INSERT INTO intTable (keyName, value, foreignId) VALUES ('negative one', -1, 1)"); + statement.execute( + "INSERT INTO intTable (keyName, value, foreignId) VALUES ('negative one', -1, 1)"); } catch (final SQLException e) { LOGGER.error(format("Failed attempt to populate DerbyDB: <%s>", e.getMessage()), e); return false; @@ -302,10 +313,11 @@ private static boolean populateDerbyDatabase() { return true; } - private static ArrowType getArrowTypeFromJdbcType(final int jdbcDataType, final int precision, final int scale) { + private static ArrowType getArrowTypeFromJdbcType( + final int jdbcDataType, final int precision, final int scale) { try { - return JdbcToArrowUtils.getArrowTypeFromJdbcType(new JdbcFieldInfo(jdbcDataType, precision, scale), - DEFAULT_CALENDAR); + return JdbcToArrowUtils.getArrowTypeFromJdbcType( + new JdbcFieldInfo(jdbcDataType, precision, scale), DEFAULT_CALENDAR); } catch (UnsupportedOperationException ignored) { return ArrowType.Utf8.INSTANCE; } @@ -329,19 +341,29 @@ private static void saveToVector(final Byte data, final BitVector vector, final private static void saveToVector(final String data, final VarCharVector vector, final int index) { preconditionCheckSaveToVector(vector, index); - vectorConsumer(data, vector, fieldVector -> fieldVector.setNull(index), + vectorConsumer( + data, + vector, + fieldVector -> fieldVector.setNull(index), (theData, fieldVector) -> fieldVector.setSafe(index, new Text(theData))); } private static void saveToVector(final Integer data, final IntVector vector, final int index) { preconditionCheckSaveToVector(vector, index); - vectorConsumer(data, vector, fieldVector -> fieldVector.setNull(index), + vectorConsumer( + data, + vector, + fieldVector -> fieldVector.setNull(index), (theData, fieldVector) -> fieldVector.setSafe(index, theData)); } - private static void saveToVector(final byte[] data, final VarBinaryVector vector, final int index) { + private static void saveToVector( + final byte[] data, final VarBinaryVector vector, final int index) { preconditionCheckSaveToVector(vector, index); - vectorConsumer(data, vector, fieldVector -> fieldVector.setNull(index), + vectorConsumer( + data, + vector, + fieldVector -> fieldVector.setNull(index), (theData, fieldVector) -> fieldVector.setSafe(index, theData)); } @@ -350,9 +372,11 @@ private static void preconditionCheckSaveToVector(final FieldVector vector, fina checkState(index >= 0, "Index must be a positive number!"); } - private static void vectorConsumer(final T data, final V vector, - final Consumer consumerIfNullable, - final BiConsumer defaultConsumer) { + private static void vectorConsumer( + final T data, + final V vector, + final Consumer consumerIfNullable, + final BiConsumer defaultConsumer) { if (isNull(data)) { consumerIfNullable.accept(vector); return; @@ -360,33 +384,41 @@ private static void vectorConsumer(final T data, fina defaultConsumer.accept(data, vector); } - private static VectorSchemaRoot getSchemasRoot(final ResultSet data, final BufferAllocator allocator) - throws SQLException { + private static VectorSchemaRoot getSchemasRoot( + final ResultSet data, final BufferAllocator allocator) throws SQLException { final VarCharVector catalogs = new VarCharVector("catalog_name", allocator); final VarCharVector schemas = - new VarCharVector("db_schema_name", FieldType.notNullable(MinorType.VARCHAR.getType()), allocator); + new VarCharVector( + "db_schema_name", FieldType.notNullable(MinorType.VARCHAR.getType()), allocator); final List vectors = ImmutableList.of(catalogs, schemas); vectors.forEach(FieldVector::allocateNew); - final Map vectorToColumnName = ImmutableMap.of( - catalogs, "TABLE_CATALOG", - schemas, "TABLE_SCHEM"); + final Map vectorToColumnName = + ImmutableMap.of( + catalogs, "TABLE_CATALOG", + schemas, "TABLE_SCHEM"); saveToVectors(vectorToColumnName, data); - final int rows = vectors.stream().map(FieldVector::getValueCount).findAny().orElseThrow(IllegalStateException::new); + final int rows = + vectors.stream() + .map(FieldVector::getValueCount) + .findAny() + .orElseThrow(IllegalStateException::new); vectors.forEach(vector -> vector.setValueCount(rows)); return new VectorSchemaRoot(vectors); } - private static int saveToVectors(final Map vectorToColumnName, - final ResultSet data, boolean emptyToNull) + private static int saveToVectors( + final Map vectorToColumnName, final ResultSet data, boolean emptyToNull) throws SQLException { Predicate alwaysTrue = (resultSet) -> true; return saveToVectors(vectorToColumnName, data, emptyToNull, alwaysTrue); } @SuppressWarnings("StringSplitter") - private static int saveToVectors(final Map vectorToColumnName, - final ResultSet data, boolean emptyToNull, - Predicate resultSetPredicate) + private static int saveToVectors( + final Map vectorToColumnName, + final ResultSet data, + boolean emptyToNull, + Predicate resultSetPredicate) throws SQLException { Objects.requireNonNull(vectorToColumnName, "vectorToColumnName cannot be null."); Objects.requireNonNull(data, "data cannot be null."); @@ -402,7 +434,8 @@ private static int saveToVectors(final Map ve final String columnName = vectorToColumn.getValue(); if (vector instanceof VarCharVector) { String thisData = data.getString(columnName); - saveToVector(emptyToNull ? emptyToNull(thisData) : thisData, (VarCharVector) vector, rows); + saveToVector( + emptyToNull ? emptyToNull(thisData) : thisData, (VarCharVector) vector, rows); } else if (vector instanceof IntVector) { final int intValue = data.getInt(columnName); saveToVector(data.wasNull() ? null : intValue, (IntVector) vector, rows); @@ -427,21 +460,25 @@ private static int saveToVectors(final Map ve String[] split = createParamsValues.split(","); range(0, split.length) - .forEach(i -> { - byte[] bytes = split[i].getBytes(UTF_8); - Preconditions.checkState(bytes.length < 1024, - "The amount of bytes is greater than what the ArrowBuf supports"); - buf.setBytes(0, bytes); - writer.varChar().writeVarChar(0, bytes.length, buf); - }); + .forEach( + i -> { + byte[] bytes = split[i].getBytes(UTF_8); + Preconditions.checkState( + bytes.length < 1024, + "The amount of bytes is greater than what the ArrowBuf supports"); + buf.setBytes(0, bytes); + writer.varChar().writeVarChar(0, bytes.length, buf); + }); } buf.close(); writer.endList(); } else { - throw CallStatus.INVALID_ARGUMENT.withDescription("Provided vector not supported").toRuntimeException(); + throw CallStatus.INVALID_ARGUMENT + .withDescription("Provided vector not supported") + .toRuntimeException(); } } - rows ++; + rows++; } for (final Entry vectorToColumn : entrySet) { vectorToColumn.getKey().setValueCount(rows); @@ -450,35 +487,38 @@ private static int saveToVectors(final Map ve return rows; } - private static void saveToVectors(final Map vectorToColumnName, - final ResultSet data) - throws SQLException { + private static void saveToVectors( + final Map vectorToColumnName, final ResultSet data) throws SQLException { saveToVectors(vectorToColumnName, data, false); } - private static VectorSchemaRoot getTableTypesRoot(final ResultSet data, final BufferAllocator allocator) - throws SQLException { + private static VectorSchemaRoot getTableTypesRoot( + final ResultSet data, final BufferAllocator allocator) throws SQLException { return getRoot(data, allocator, "table_type", "TABLE_TYPE"); } - private static VectorSchemaRoot getCatalogsRoot(final ResultSet data, final BufferAllocator allocator) - throws SQLException { + private static VectorSchemaRoot getCatalogsRoot( + final ResultSet data, final BufferAllocator allocator) throws SQLException { return getRoot(data, allocator, "catalog_name", "TABLE_CATALOG"); } - private static VectorSchemaRoot getRoot(final ResultSet data, final BufferAllocator allocator, - final String fieldVectorName, final String columnName) + private static VectorSchemaRoot getRoot( + final ResultSet data, + final BufferAllocator allocator, + final String fieldVectorName, + final String columnName) throws SQLException { final VarCharVector dataVector = - new VarCharVector(fieldVectorName, FieldType.notNullable(MinorType.VARCHAR.getType()), allocator); + new VarCharVector( + fieldVectorName, FieldType.notNullable(MinorType.VARCHAR.getType()), allocator); saveToVectors(ImmutableMap.of(dataVector, columnName), data); final int rows = dataVector.getValueCount(); dataVector.setValueCount(rows); return new VectorSchemaRoot(singletonList(dataVector)); } - private static VectorSchemaRoot getTypeInfoRoot(CommandGetXdbcTypeInfo request, ResultSet typeInfo, - final BufferAllocator allocator) + private static VectorSchemaRoot getTypeInfoRoot( + CommandGetXdbcTypeInfo request, ResultSet typeInfo, final BufferAllocator allocator) throws SQLException { Preconditions.checkNotNull(allocator, "BufferAllocator cannot be null."); @@ -506,13 +546,14 @@ private static VectorSchemaRoot getTypeInfoRoot(CommandGetXdbcTypeInfo request, Predicate predicate; if (request.hasDataType()) { - predicate = (resultSet) -> { - try { - return resultSet.getInt("DATA_TYPE") == request.getDataType(); - } catch (SQLException e) { - throw new RuntimeException(e); - } - }; + predicate = + (resultSet) -> { + try { + return resultSet.getInt("DATA_TYPE") == request.getDataType(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + }; } else { predicate = resultSet -> true; } @@ -523,13 +564,14 @@ private static VectorSchemaRoot getTypeInfoRoot(CommandGetXdbcTypeInfo request, return root; } - private static VectorSchemaRoot getTablesRoot(final DatabaseMetaData databaseMetaData, - final BufferAllocator allocator, - final boolean includeSchema, - final String catalog, - final String schemaFilterPattern, - final String tableFilterPattern, - final String... tableTypes) + private static VectorSchemaRoot getTablesRoot( + final DatabaseMetaData databaseMetaData, + final BufferAllocator allocator, + final boolean includeSchema, + final String catalog, + final String schemaFilterPattern, + final String tableFilterPattern, + final String... tableTypes) throws SQLException, IOException { /* * TODO Fix DerbyDB inconsistency if possible. @@ -545,9 +587,11 @@ private static VectorSchemaRoot getTablesRoot(final DatabaseMetaData databaseMet final VarCharVector catalogNameVector = new VarCharVector("catalog_name", allocator); final VarCharVector schemaNameVector = new VarCharVector("db_schema_name", allocator); final VarCharVector tableNameVector = - new VarCharVector("table_name", FieldType.notNullable(MinorType.VARCHAR.getType()), allocator); + new VarCharVector( + "table_name", FieldType.notNullable(MinorType.VARCHAR.getType()), allocator); final VarCharVector tableTypeVector = - new VarCharVector("table_type", FieldType.notNullable(MinorType.VARCHAR.getType()), allocator); + new VarCharVector( + "table_type", FieldType.notNullable(MinorType.VARCHAR.getType()), allocator); final List vectors = new ArrayList<>(4); vectors.add(catalogNameVector); @@ -557,30 +601,35 @@ private static VectorSchemaRoot getTablesRoot(final DatabaseMetaData databaseMet vectors.forEach(FieldVector::allocateNew); - final Map vectorToColumnName = ImmutableMap.of( - catalogNameVector, "TABLE_CAT", - schemaNameVector, "TABLE_SCHEM", - tableNameVector, "TABLE_NAME", - tableTypeVector, "TABLE_TYPE"); + final Map vectorToColumnName = + ImmutableMap.of( + catalogNameVector, "TABLE_CAT", + schemaNameVector, "TABLE_SCHEM", + tableNameVector, "TABLE_NAME", + tableTypeVector, "TABLE_TYPE"); try (final ResultSet data = - Objects.requireNonNull( - databaseMetaData, - format("%s cannot be null.", databaseMetaData.getClass().getName())) - .getTables(catalog, schemaFilterPattern, tableFilterPattern, tableTypes)) { + Objects.requireNonNull( + databaseMetaData, + format("%s cannot be null.", databaseMetaData.getClass().getName())) + .getTables(catalog, schemaFilterPattern, tableFilterPattern, tableTypes)) { saveToVectors(vectorToColumnName, data, true); final int rows = - vectors.stream().map(FieldVector::getValueCount).findAny().orElseThrow(IllegalStateException::new); + vectors.stream() + .map(FieldVector::getValueCount) + .findAny() + .orElseThrow(IllegalStateException::new); vectors.forEach(vector -> vector.setValueCount(rows)); if (includeSchema) { final VarBinaryVector tableSchemaVector = - new VarBinaryVector("table_schema", FieldType.notNullable(MinorType.VARBINARY.getType()), allocator); + new VarBinaryVector( + "table_schema", FieldType.notNullable(MinorType.VARBINARY.getType()), allocator); tableSchemaVector.allocateNew(rows); try (final ResultSet columnsData = - databaseMetaData.getColumns(catalog, schemaFilterPattern, tableFilterPattern, null)) { + databaseMetaData.getColumns(catalog, schemaFilterPattern, tableFilterPattern, null)) { final Map> tableToFields = new HashMap<>(); while (columnsData.next()) { @@ -590,23 +639,26 @@ private static VectorSchemaRoot getTablesRoot(final DatabaseMetaData databaseMet final String typeName = columnsData.getString("TYPE_NAME"); final String fieldName = columnsData.getString("COLUMN_NAME"); final int dataType = columnsData.getInt("DATA_TYPE"); - final boolean isNullable = columnsData.getInt("NULLABLE") != DatabaseMetaData.columnNoNulls; + final boolean isNullable = + columnsData.getInt("NULLABLE") != DatabaseMetaData.columnNoNulls; final int precision = columnsData.getInt("COLUMN_SIZE"); final int scale = columnsData.getInt("DECIMAL_DIGITS"); boolean isAutoIncrement = Objects.equals(columnsData.getString("IS_AUTOINCREMENT"), "YES"); - final List fields = tableToFields.computeIfAbsent(tableName, tableName_ -> new ArrayList<>()); + final List fields = + tableToFields.computeIfAbsent(tableName, tableName_ -> new ArrayList<>()); - final FlightSqlColumnMetadata columnMetadata = new FlightSqlColumnMetadata.Builder() - .catalogName(catalogName) - .schemaName(schemaName) - .tableName(tableName) - .typeName(typeName) - .precision(precision) - .scale(scale) - .isAutoIncrement(isAutoIncrement) - .build(); + final FlightSqlColumnMetadata columnMetadata = + new FlightSqlColumnMetadata.Builder() + .catalogName(catalogName) + .schemaName(schemaName) + .tableName(tableName) + .typeName(typeName) + .precision(precision) + .scale(scale) + .isAutoIncrement(isAutoIncrement) + .build(); final Field field = new Field( @@ -624,8 +676,7 @@ private static VectorSchemaRoot getTablesRoot(final DatabaseMetaData databaseMet final String tableName = tableNameVector.getObject(index).toString(); final Schema schema = new Schema(tableToFields.get(tableName)); saveToVector( - copyFrom(serializeMetadata(schema)).toByteArray(), - tableSchemaVector, index); + copyFrom(serializeMetadata(schema)).toByteArray(), tableSchemaVector, index); } } @@ -649,15 +700,19 @@ private static ByteBuffer serializeMetadata(final Schema schema) { } @Override - public void getStreamPreparedStatement(final CommandPreparedStatementQuery command, final CallContext context, - final ServerStreamListener listener) { + public void getStreamPreparedStatement( + final CommandPreparedStatementQuery command, + final CallContext context, + final ServerStreamListener listener) { final ByteString handle = command.getPreparedStatementHandle(); - StatementContext statementContext = preparedStatementLoadingCache.getIfPresent(handle); + StatementContext statementContext = + preparedStatementLoadingCache.getIfPresent(handle); Objects.requireNonNull(statementContext); final PreparedStatement statement = statementContext.getStatement(); try (final ResultSet resultSet = statement.executeQuery()) { final Schema schema = jdbcToArrowSchema(resultSet.getMetaData(), DEFAULT_CALENDAR); - try (final VectorSchemaRoot vectorSchemaRoot = VectorSchemaRoot.create(schema, rootAllocator)) { + try (final VectorSchemaRoot vectorSchemaRoot = + VectorSchemaRoot.create(schema, rootAllocator)) { final VectorLoader loader = new VectorLoader(vectorSchemaRoot); listener.start(vectorSchemaRoot); @@ -677,60 +732,68 @@ public void getStreamPreparedStatement(final CommandPreparedStatementQuery comma } } catch (final SQLException | IOException e) { LOGGER.error(format("Failed to getStreamPreparedStatement: <%s>.", e.getMessage()), e); - listener.error(CallStatus.INTERNAL.withDescription("Failed to prepare statement: " + e).toRuntimeException()); + listener.error( + CallStatus.INTERNAL + .withDescription("Failed to prepare statement: " + e) + .toRuntimeException()); } finally { listener.completed(); } } @Override - public void closePreparedStatement(final ActionClosePreparedStatementRequest request, final CallContext context, - final StreamListener listener) { + public void closePreparedStatement( + final ActionClosePreparedStatementRequest request, + final CallContext context, + final StreamListener listener) { // Running on another thread - Future unused = executorService.submit(() -> { - try { - preparedStatementLoadingCache.invalidate(request.getPreparedStatementHandle()); - } catch (final Exception e) { - listener.onError(e); - return; - } - listener.onCompleted(); - }); + Future unused = + executorService.submit( + () -> { + try { + preparedStatementLoadingCache.invalidate(request.getPreparedStatementHandle()); + } catch (final Exception e) { + listener.onError(e); + return; + } + listener.onCompleted(); + }); } @Override - public FlightInfo getFlightInfoStatement(final CommandStatementQuery request, final CallContext context, - final FlightDescriptor descriptor) { + public FlightInfo getFlightInfoStatement( + final CommandStatementQuery request, + final CallContext context, + final FlightDescriptor descriptor) { ByteString handle = copyFrom(randomUUID().toString().getBytes(UTF_8)); try { // Ownership of the connection will be passed to the context. Do NOT close! final Connection connection = dataSource.getConnection(); - final Statement statement = connection.createStatement( - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); + final Statement statement = + connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); final String query = request.getQuery(); final StatementContext statementContext = new StatementContext<>(statement, query); statementLoadingCache.put(handle, statementContext); final ResultSet resultSet = statement.executeQuery(query); - TicketStatementQuery ticket = TicketStatementQuery.newBuilder() - .setStatementHandle(handle) - .build(); - return getFlightInfoForSchema(ticket, descriptor, - jdbcToArrowSchema(resultSet.getMetaData(), DEFAULT_CALENDAR)); + TicketStatementQuery ticket = + TicketStatementQuery.newBuilder().setStatementHandle(handle).build(); + return getFlightInfoForSchema( + ticket, descriptor, jdbcToArrowSchema(resultSet.getMetaData(), DEFAULT_CALENDAR)); } catch (final SQLException e) { LOGGER.error( - format("There was a problem executing the prepared statement: <%s>.", e.getMessage()), - e); + format("There was a problem executing the prepared statement: <%s>.", e.getMessage()), e); throw CallStatus.INTERNAL.withCause(e).toRuntimeException(); } } @Override - public FlightInfo getFlightInfoPreparedStatement(final CommandPreparedStatementQuery command, - final CallContext context, - final FlightDescriptor descriptor) { + public FlightInfo getFlightInfoPreparedStatement( + final CommandPreparedStatementQuery command, + final CallContext context, + final FlightDescriptor descriptor) { final ByteString preparedStatementHandle = command.getPreparedStatementHandle(); StatementContext statementContext = preparedStatementLoadingCache.getIfPresent(preparedStatementHandle); @@ -739,19 +802,20 @@ public FlightInfo getFlightInfoPreparedStatement(final CommandPreparedStatementQ PreparedStatement statement = statementContext.getStatement(); ResultSetMetaData metaData = statement.getMetaData(); - return getFlightInfoForSchema(command, descriptor, - jdbcToArrowSchema(metaData, DEFAULT_CALENDAR)); + return getFlightInfoForSchema( + command, descriptor, jdbcToArrowSchema(metaData, DEFAULT_CALENDAR)); } catch (final SQLException e) { LOGGER.error( - format("There was a problem executing the prepared statement: <%s>.", e.getMessage()), - e); + format("There was a problem executing the prepared statement: <%s>.", e.getMessage()), e); throw CallStatus.INTERNAL.withCause(e).toRuntimeException(); } } @Override - public SchemaResult getSchemaStatement(final CommandStatementQuery command, final CallContext context, - final FlightDescriptor descriptor) { + public SchemaResult getSchemaStatement( + final CommandStatementQuery command, + final CallContext context, + final FlightDescriptor descriptor) { throw CallStatus.UNIMPLEMENTED.toRuntimeException(); } @@ -767,52 +831,68 @@ public void close() throws Exception { } @Override - public void listFlights(CallContext context, Criteria criteria, StreamListener listener) { + public void listFlights( + CallContext context, Criteria criteria, StreamListener listener) { // TODO - build example implementation throw CallStatus.UNIMPLEMENTED.toRuntimeException(); } @Override - public void createPreparedStatement(final ActionCreatePreparedStatementRequest request, final CallContext context, - final StreamListener listener) { + public void createPreparedStatement( + final ActionCreatePreparedStatementRequest request, + final CallContext context, + final StreamListener listener) { // Running on another thread - Future unused = executorService.submit(() -> { - try { - final ByteString preparedStatementHandle = copyFrom(randomUUID().toString().getBytes(UTF_8)); - // Ownership of the connection will be passed to the context. Do NOT close! - final Connection connection = dataSource.getConnection(); - final PreparedStatement preparedStatement = connection.prepareStatement(request.getQuery(), - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); - final StatementContext preparedStatementContext = - new StatementContext<>(preparedStatement, request.getQuery()); - - preparedStatementLoadingCache.put(preparedStatementHandle, preparedStatementContext); - - final Schema parameterSchema = - jdbcToArrowSchema(preparedStatement.getParameterMetaData(), DEFAULT_CALENDAR); - - final ResultSetMetaData metaData = preparedStatement.getMetaData(); - final ByteString bytes = isNull(metaData) ? - ByteString.EMPTY : - ByteString.copyFrom( - serializeMetadata(jdbcToArrowSchema(metaData, DEFAULT_CALENDAR))); - final ActionCreatePreparedStatementResult result = ActionCreatePreparedStatementResult.newBuilder() - .setDatasetSchema(bytes) - .setParameterSchema(copyFrom(serializeMetadata(parameterSchema))) - .setPreparedStatementHandle(preparedStatementHandle) - .build(); - listener.onNext(new Result(pack(result).toByteArray())); - } catch (final SQLException e) { - listener.onError(CallStatus.INTERNAL - .withDescription("Failed to create prepared statement: " + e) - .toRuntimeException()); - return; - } catch (final Throwable t) { - listener.onError(CallStatus.INTERNAL.withDescription("Unknown error: " + t).toRuntimeException()); - return; - } - listener.onCompleted(); - }); + Future unused = + executorService.submit( + () -> { + try { + final ByteString preparedStatementHandle = + copyFrom(randomUUID().toString().getBytes(UTF_8)); + // Ownership of the connection will be passed to the context. Do NOT close! + final Connection connection = dataSource.getConnection(); + final PreparedStatement preparedStatement = + connection.prepareStatement( + request.getQuery(), + ResultSet.TYPE_SCROLL_INSENSITIVE, + ResultSet.CONCUR_READ_ONLY); + final StatementContext preparedStatementContext = + new StatementContext<>(preparedStatement, request.getQuery()); + + preparedStatementLoadingCache.put( + preparedStatementHandle, preparedStatementContext); + + final Schema parameterSchema = + jdbcToArrowSchema(preparedStatement.getParameterMetaData(), DEFAULT_CALENDAR); + + final ResultSetMetaData metaData = preparedStatement.getMetaData(); + final ByteString bytes = + isNull(metaData) + ? ByteString.EMPTY + : ByteString.copyFrom( + serializeMetadata(jdbcToArrowSchema(metaData, DEFAULT_CALENDAR))); + final ActionCreatePreparedStatementResult result = + ActionCreatePreparedStatementResult.newBuilder() + .setDatasetSchema(bytes) + .setParameterSchema(copyFrom(serializeMetadata(parameterSchema))) + .setPreparedStatementHandle(preparedStatementHandle) + .build(); + listener.onNext(new Result(pack(result).toByteArray())); + } catch (final SQLException e) { + listener.onError( + CallStatus.INTERNAL + .withDescription("Failed to create prepared statement: " + e) + .toRuntimeException()); + return; + } catch (final Throwable t) { + listener.onError( + CallStatus.INTERNAL + .withDescription("Unknown error: " + t) + .toRuntimeException()); + return; + } + listener.onCompleted(); + }); } @Override @@ -822,14 +902,16 @@ public void doExchange(CallContext context, FlightStream reader, ServerStreamLis } @Override - public Runnable acceptPutStatement(CommandStatementUpdate command, - CallContext context, FlightStream flightStream, - StreamListener ackStream) { + public Runnable acceptPutStatement( + CommandStatementUpdate command, + CallContext context, + FlightStream flightStream, + StreamListener ackStream) { final String query = command.getQuery(); return () -> { try (final Connection connection = dataSource.getConnection(); - final Statement statement = connection.createStatement()) { + final Statement statement = connection.createStatement()) { final int result = statement.executeUpdate(query); final DoPutUpdateResult build = @@ -841,28 +923,34 @@ public Runnable acceptPutStatement(CommandStatementUpdate command, ackStream.onCompleted(); } } catch (SQLSyntaxErrorException e) { - ackStream.onError(CallStatus.INVALID_ARGUMENT - .withDescription("Failed to execute statement (invalid syntax): " + e) - .toRuntimeException()); + ackStream.onError( + CallStatus.INVALID_ARGUMENT + .withDescription("Failed to execute statement (invalid syntax): " + e) + .toRuntimeException()); } catch (SQLException e) { - ackStream.onError(CallStatus.INTERNAL - .withDescription("Failed to execute statement: " + e) - .toRuntimeException()); + ackStream.onError( + CallStatus.INTERNAL + .withDescription("Failed to execute statement: " + e) + .toRuntimeException()); } }; } @Override - public Runnable acceptPutPreparedStatementUpdate(CommandPreparedStatementUpdate command, CallContext context, - FlightStream flightStream, StreamListener ackStream) { + public Runnable acceptPutPreparedStatementUpdate( + CommandPreparedStatementUpdate command, + CallContext context, + FlightStream flightStream, + StreamListener ackStream) { final StatementContext statement = preparedStatementLoadingCache.getIfPresent(command.getPreparedStatementHandle()); return () -> { if (statement == null) { - ackStream.onError(CallStatus.NOT_FOUND - .withDescription("Prepared statement does not exist") - .toRuntimeException()); + ackStream.onError( + CallStatus.NOT_FOUND + .withDescription("Prepared statement does not exist") + .toRuntimeException()); return; } try { @@ -878,7 +966,8 @@ public Runnable acceptPutPreparedStatementUpdate(CommandPreparedStatementUpdate preparedStatement.execute(); recordCount = preparedStatement.getUpdateCount(); } else { - final JdbcParameterBinder binder = JdbcParameterBinder.builder(preparedStatement, root).bindAll().build(); + final JdbcParameterBinder binder = + JdbcParameterBinder.builder(preparedStatement, root).bindAll().build(); while (binder.next()) { preparedStatement.addBatch(); } @@ -895,7 +984,10 @@ public Runnable acceptPutPreparedStatementUpdate(CommandPreparedStatementUpdate } } } catch (SQLException e) { - ackStream.onError(CallStatus.INTERNAL.withDescription("Failed to execute update: " + e).toRuntimeException()); + ackStream.onError( + CallStatus.INTERNAL + .withDescription("Failed to execute update: " + e) + .toRuntimeException()); return; } ackStream.onCompleted(); @@ -903,8 +995,11 @@ public Runnable acceptPutPreparedStatementUpdate(CommandPreparedStatementUpdate } @Override - public Runnable acceptPutPreparedStatementQuery(CommandPreparedStatementQuery command, CallContext context, - FlightStream flightStream, StreamListener ackStream) { + public Runnable acceptPutPreparedStatementQuery( + CommandPreparedStatementQuery command, + CallContext context, + FlightStream flightStream, + StreamListener ackStream) { final StatementContext statementContext = preparedStatementLoadingCache.getIfPresent(command.getPreparedStatementHandle()); @@ -915,17 +1010,19 @@ public Runnable acceptPutPreparedStatementQuery(CommandPreparedStatementQuery co try { while (flightStream.next()) { final VectorSchemaRoot root = flightStream.getRoot(); - final JdbcParameterBinder binder = JdbcParameterBinder.builder(preparedStatement, root).bindAll().build(); + final JdbcParameterBinder binder = + JdbcParameterBinder.builder(preparedStatement, root).bindAll().build(); while (binder.next()) { // Do not execute() - will be done in a getStream call } } } catch (SQLException e) { - ackStream.onError(CallStatus.INTERNAL - .withDescription("Failed to bind parameters: " + e.getMessage()) - .withCause(e) - .toRuntimeException()); + ackStream.onError( + CallStatus.INTERNAL + .withDescription("Failed to bind parameters: " + e.getMessage()) + .withCause(e) + .toRuntimeException()); return; } ackStream.onCompleted(); @@ -933,29 +1030,34 @@ public Runnable acceptPutPreparedStatementQuery(CommandPreparedStatementQuery co } @Override - public FlightInfo getFlightInfoSqlInfo(final CommandGetSqlInfo request, final CallContext context, - final FlightDescriptor descriptor) { + public FlightInfo getFlightInfoSqlInfo( + final CommandGetSqlInfo request, + final CallContext context, + final FlightDescriptor descriptor) { return getFlightInfoForSchema(request, descriptor, Schemas.GET_SQL_INFO_SCHEMA); } @Override - public void getStreamSqlInfo(final CommandGetSqlInfo command, final CallContext context, - final ServerStreamListener listener) { + public void getStreamSqlInfo( + final CommandGetSqlInfo command, + final CallContext context, + final ServerStreamListener listener) { this.sqlInfoBuilder.send(command.getInfoList(), listener); } @Override - public FlightInfo getFlightInfoTypeInfo(CommandGetXdbcTypeInfo request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoTypeInfo( + CommandGetXdbcTypeInfo request, CallContext context, FlightDescriptor descriptor) { return getFlightInfoForSchema(request, descriptor, Schemas.GET_TYPE_INFO_SCHEMA); } @Override - public void getStreamTypeInfo(CommandGetXdbcTypeInfo request, CallContext context, - ServerStreamListener listener) { + public void getStreamTypeInfo( + CommandGetXdbcTypeInfo request, CallContext context, ServerStreamListener listener) { try (final Connection connection = dataSource.getConnection(); - final ResultSet typeInfo = connection.getMetaData().getTypeInfo(); - final VectorSchemaRoot vectorSchemaRoot = getTypeInfoRoot(request, typeInfo, rootAllocator)) { + final ResultSet typeInfo = connection.getMetaData().getTypeInfo(); + final VectorSchemaRoot vectorSchemaRoot = + getTypeInfoRoot(request, typeInfo, rootAllocator)) { listener.start(vectorSchemaRoot); listener.putNext(); } catch (SQLException e) { @@ -967,16 +1069,18 @@ public void getStreamTypeInfo(CommandGetXdbcTypeInfo request, CallContext contex } @Override - public FlightInfo getFlightInfoCatalogs(final CommandGetCatalogs request, final CallContext context, - final FlightDescriptor descriptor) { + public FlightInfo getFlightInfoCatalogs( + final CommandGetCatalogs request, + final CallContext context, + final FlightDescriptor descriptor) { return getFlightInfoForSchema(request, descriptor, Schemas.GET_CATALOGS_SCHEMA); } @Override public void getStreamCatalogs(final CallContext context, final ServerStreamListener listener) { try (final Connection connection = dataSource.getConnection(); - final ResultSet catalogs = connection.getMetaData().getCatalogs(); - final VectorSchemaRoot vectorSchemaRoot = getCatalogsRoot(catalogs, rootAllocator)) { + final ResultSet catalogs = connection.getMetaData().getCatalogs(); + final VectorSchemaRoot vectorSchemaRoot = getCatalogsRoot(catalogs, rootAllocator)) { listener.start(vectorSchemaRoot); listener.putNext(); } catch (SQLException e) { @@ -988,19 +1092,25 @@ public void getStreamCatalogs(final CallContext context, final ServerStreamListe } @Override - public FlightInfo getFlightInfoSchemas(final CommandGetDbSchemas request, final CallContext context, - final FlightDescriptor descriptor) { + public FlightInfo getFlightInfoSchemas( + final CommandGetDbSchemas request, + final CallContext context, + final FlightDescriptor descriptor) { return getFlightInfoForSchema(request, descriptor, Schemas.GET_SCHEMAS_SCHEMA); } @Override - public void getStreamSchemas(final CommandGetDbSchemas command, final CallContext context, - final ServerStreamListener listener) { + public void getStreamSchemas( + final CommandGetDbSchemas command, + final CallContext context, + final ServerStreamListener listener) { final String catalog = command.hasCatalog() ? command.getCatalog() : null; - final String schemaFilterPattern = command.hasDbSchemaFilterPattern() ? command.getDbSchemaFilterPattern() : null; + final String schemaFilterPattern = + command.hasDbSchemaFilterPattern() ? command.getDbSchemaFilterPattern() : null; try (final Connection connection = dataSource.getConnection(); - final ResultSet schemas = connection.getMetaData().getSchemas(catalog, schemaFilterPattern); - final VectorSchemaRoot vectorSchemaRoot = getSchemasRoot(schemas, rootAllocator)) { + final ResultSet schemas = + connection.getMetaData().getSchemas(catalog, schemaFilterPattern); + final VectorSchemaRoot vectorSchemaRoot = getSchemasRoot(schemas, rootAllocator)) { listener.start(vectorSchemaRoot); listener.putNext(); } catch (SQLException e) { @@ -1012,8 +1122,10 @@ public void getStreamSchemas(final CommandGetDbSchemas command, final CallContex } @Override - public FlightInfo getFlightInfoTables(final CommandGetTables request, final CallContext context, - final FlightDescriptor descriptor) { + public FlightInfo getFlightInfoTables( + final CommandGetTables request, + final CallContext context, + final FlightDescriptor descriptor) { Schema schemaToUse = Schemas.GET_TABLES_SCHEMA; if (!request.getIncludeSchema()) { schemaToUse = Schemas.GET_TABLES_SCHEMA_NO_SCHEMA; @@ -1022,8 +1134,10 @@ public FlightInfo getFlightInfoTables(final CommandGetTables request, final Call } @Override - public void getStreamTables(final CommandGetTables command, final CallContext context, - final ServerStreamListener listener) { + public void getStreamTables( + final CommandGetTables command, + final CallContext context, + final ServerStreamListener listener) { final String catalog = command.hasCatalog() ? command.getCatalog() : null; final String schemaFilterPattern = command.hasDbSchemaFilterPattern() ? command.getDbSchemaFilterPattern() : null; @@ -1036,11 +1150,15 @@ public void getStreamTables(final CommandGetTables command, final CallContext co protocolSize == 0 ? null : protocolStringList.toArray(new String[protocolSize]); try (final Connection connection = DriverManager.getConnection(DATABASE_URI); - final VectorSchemaRoot vectorSchemaRoot = getTablesRoot( - connection.getMetaData(), - rootAllocator, - command.getIncludeSchema(), - catalog, schemaFilterPattern, tableFilterPattern, tableTypes)) { + final VectorSchemaRoot vectorSchemaRoot = + getTablesRoot( + connection.getMetaData(), + rootAllocator, + command.getIncludeSchema(), + catalog, + schemaFilterPattern, + tableFilterPattern, + tableTypes)) { listener.start(vectorSchemaRoot); listener.putNext(); } catch (SQLException | IOException e) { @@ -1052,16 +1170,18 @@ public void getStreamTables(final CommandGetTables command, final CallContext co } @Override - public FlightInfo getFlightInfoTableTypes(final CommandGetTableTypes request, final CallContext context, - final FlightDescriptor descriptor) { + public FlightInfo getFlightInfoTableTypes( + final CommandGetTableTypes request, + final CallContext context, + final FlightDescriptor descriptor) { return getFlightInfoForSchema(request, descriptor, Schemas.GET_TABLE_TYPES_SCHEMA); } @Override public void getStreamTableTypes(final CallContext context, final ServerStreamListener listener) { try (final Connection connection = dataSource.getConnection(); - final ResultSet tableTypes = connection.getMetaData().getTableTypes(); - final VectorSchemaRoot vectorSchemaRoot = getTableTypesRoot(tableTypes, rootAllocator)) { + final ResultSet tableTypes = connection.getMetaData().getTableTypes(); + final VectorSchemaRoot vectorSchemaRoot = getTableTypesRoot(tableTypes, rootAllocator)) { listener.start(vectorSchemaRoot); listener.putNext(); } catch (SQLException e) { @@ -1073,14 +1193,18 @@ public void getStreamTableTypes(final CallContext context, final ServerStreamLis } @Override - public FlightInfo getFlightInfoPrimaryKeys(final CommandGetPrimaryKeys request, final CallContext context, - final FlightDescriptor descriptor) { + public FlightInfo getFlightInfoPrimaryKeys( + final CommandGetPrimaryKeys request, + final CallContext context, + final FlightDescriptor descriptor) { return getFlightInfoForSchema(request, descriptor, Schemas.GET_PRIMARY_KEYS_SCHEMA); } @Override - public void getStreamPrimaryKeys(final CommandGetPrimaryKeys command, final CallContext context, - final ServerStreamListener listener) { + public void getStreamPrimaryKeys( + final CommandGetPrimaryKeys command, + final CallContext context, + final ServerStreamListener listener) { final String catalog = command.hasCatalog() ? command.getCatalog() : null; final String schema = command.hasDbSchema() ? command.getDbSchema() : null; @@ -1099,7 +1223,11 @@ public void getStreamPrimaryKeys(final CommandGetPrimaryKeys command, final Call final List vectors = new ArrayList<>( ImmutableList.of( - catalogNameVector, schemaNameVector, tableNameVector, columnNameVector, keySequenceVector, + catalogNameVector, + schemaNameVector, + tableNameVector, + columnNameVector, + keySequenceVector, keyNameVector)); vectors.forEach(FieldVector::allocateNew); @@ -1128,21 +1256,25 @@ public void getStreamPrimaryKeys(final CommandGetPrimaryKeys command, final Call } @Override - public FlightInfo getFlightInfoExportedKeys(final CommandGetExportedKeys request, final CallContext context, - final FlightDescriptor descriptor) { + public FlightInfo getFlightInfoExportedKeys( + final CommandGetExportedKeys request, + final CallContext context, + final FlightDescriptor descriptor) { return getFlightInfoForSchema(request, descriptor, Schemas.GET_EXPORTED_KEYS_SCHEMA); } @Override - public void getStreamExportedKeys(final CommandGetExportedKeys command, final CallContext context, - final ServerStreamListener listener) { + public void getStreamExportedKeys( + final CommandGetExportedKeys command, + final CallContext context, + final ServerStreamListener listener) { String catalog = command.hasCatalog() ? command.getCatalog() : null; String schema = command.hasDbSchema() ? command.getDbSchema() : null; String table = command.getTable(); try (Connection connection = DriverManager.getConnection(DATABASE_URI); - ResultSet keys = connection.getMetaData().getExportedKeys(catalog, schema, table); - VectorSchemaRoot vectorSchemaRoot = createVectors(keys)) { + ResultSet keys = connection.getMetaData().getExportedKeys(catalog, schema, table); + VectorSchemaRoot vectorSchemaRoot = createVectors(keys)) { listener.start(vectorSchemaRoot); listener.putNext(); } catch (SQLException e) { @@ -1153,21 +1285,25 @@ public void getStreamExportedKeys(final CommandGetExportedKeys command, final Ca } @Override - public FlightInfo getFlightInfoImportedKeys(final CommandGetImportedKeys request, final CallContext context, - final FlightDescriptor descriptor) { + public FlightInfo getFlightInfoImportedKeys( + final CommandGetImportedKeys request, + final CallContext context, + final FlightDescriptor descriptor) { return getFlightInfoForSchema(request, descriptor, Schemas.GET_IMPORTED_KEYS_SCHEMA); } @Override - public void getStreamImportedKeys(final CommandGetImportedKeys command, final CallContext context, - final ServerStreamListener listener) { + public void getStreamImportedKeys( + final CommandGetImportedKeys command, + final CallContext context, + final ServerStreamListener listener) { String catalog = command.hasCatalog() ? command.getCatalog() : null; String schema = command.hasDbSchema() ? command.getDbSchema() : null; String table = command.getTable(); try (Connection connection = DriverManager.getConnection(DATABASE_URI); - ResultSet keys = connection.getMetaData().getImportedKeys(catalog, schema, table); - VectorSchemaRoot vectorSchemaRoot = createVectors(keys)) { + ResultSet keys = connection.getMetaData().getImportedKeys(catalog, schema, table); + VectorSchemaRoot vectorSchemaRoot = createVectors(keys)) { listener.start(vectorSchemaRoot); listener.putNext(); } catch (final SQLException e) { @@ -1178,14 +1314,14 @@ public void getStreamImportedKeys(final CommandGetImportedKeys command, final Ca } @Override - public FlightInfo getFlightInfoCrossReference(CommandGetCrossReference request, CallContext context, - FlightDescriptor descriptor) { + public FlightInfo getFlightInfoCrossReference( + CommandGetCrossReference request, CallContext context, FlightDescriptor descriptor) { return getFlightInfoForSchema(request, descriptor, Schemas.GET_CROSS_REFERENCE_SCHEMA); } @Override - public void getStreamCrossReference(CommandGetCrossReference command, CallContext context, - ServerStreamListener listener) { + public void getStreamCrossReference( + CommandGetCrossReference command, CallContext context, ServerStreamListener listener) { final String pkCatalog = command.hasPkCatalog() ? command.getPkCatalog() : null; final String pkSchema = command.hasPkDbSchema() ? command.getPkDbSchema() : null; final String fkCatalog = command.hasFkCatalog() ? command.getFkCatalog() : null; @@ -1194,9 +1330,11 @@ public void getStreamCrossReference(CommandGetCrossReference command, CallContex final String fkTable = command.getFkTable(); try (Connection connection = DriverManager.getConnection(DATABASE_URI); - ResultSet keys = connection.getMetaData() - .getCrossReference(pkCatalog, pkSchema, pkTable, fkCatalog, fkSchema, fkTable); - VectorSchemaRoot vectorSchemaRoot = createVectors(keys)) { + ResultSet keys = + connection + .getMetaData() + .getCrossReference(pkCatalog, pkSchema, pkTable, fkCatalog, fkSchema, fkTable); + VectorSchemaRoot vectorSchemaRoot = createVectors(keys)) { listener.start(vectorSchemaRoot); listener.putNext(); } catch (final SQLException e) { @@ -1236,10 +1374,21 @@ private VectorSchemaRoot createVectors(ResultSet keys) throws SQLException { vectorToColumnName.put(fkKeyNameVector, "FK_NAME"); vectorToColumnName.put(pkKeyNameVector, "PK_NAME"); - final VectorSchemaRoot vectorSchemaRoot = VectorSchemaRoot.of( - pkCatalogNameVector, pkSchemaNameVector, pkTableNameVector, pkColumnNameVector, fkCatalogNameVector, - fkSchemaNameVector, fkTableNameVector, fkColumnNameVector, keySequenceVector, fkKeyNameVector, - pkKeyNameVector, updateRuleVector, deleteRuleVector); + final VectorSchemaRoot vectorSchemaRoot = + VectorSchemaRoot.of( + pkCatalogNameVector, + pkSchemaNameVector, + pkTableNameVector, + pkColumnNameVector, + fkCatalogNameVector, + fkSchemaNameVector, + fkTableNameVector, + fkColumnNameVector, + keySequenceVector, + fkKeyNameVector, + pkKeyNameVector, + updateRuleVector, + deleteRuleVector); vectorSchemaRoot.allocateNew(); final int rowCount = saveToVectors(vectorToColumnName, keys, true); @@ -1250,8 +1399,10 @@ private VectorSchemaRoot createVectors(ResultSet keys) throws SQLException { } @Override - public void getStreamStatement(final TicketStatementQuery ticketStatementQuery, final CallContext context, - final ServerStreamListener listener) { + public void getStreamStatement( + final TicketStatementQuery ticketStatementQuery, + final CallContext context, + final ServerStreamListener listener) { final ByteString handle = ticketStatementQuery.getStatementHandle(); final StatementContext statementContext = Objects.requireNonNull(statementLoadingCache.getIfPresent(handle)); @@ -1280,8 +1431,8 @@ public void getStreamStatement(final TicketStatementQuery ticketStatementQuery, } } - private FlightInfo getFlightInfoForSchema(final T request, final FlightDescriptor descriptor, - final Schema schema) { + private FlightInfo getFlightInfoForSchema( + final T request, final FlightDescriptor descriptor, final Schema schema) { final Ticket ticket = new Ticket(pack(request).toByteArray()); // TODO Support multiple endpoints. final List endpoints = singletonList(new FlightEndpoint(ticket, location)); diff --git a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/StatementContext.java b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/StatementContext.java index 764ef3f54aa..8d0c4e7f206 100644 --- a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/StatementContext.java +++ b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/example/StatementContext.java @@ -20,7 +20,6 @@ import java.sql.Connection; import java.sql.Statement; import java.util.Objects; - import org.apache.arrow.flight.sql.FlightSqlProducer; import org.apache.arrow.util.AutoCloseables; diff --git a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSql.java b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSql.java index a39736e939f..cc71c6ddb9b 100644 --- a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSql.java +++ b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSql.java @@ -29,6 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import com.google.common.collect.ImmutableList; import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; @@ -37,7 +38,6 @@ import java.util.Map; import java.util.Optional; import java.util.stream.IntStream; - import org.apache.arrow.flight.CancelFlightInfoRequest; import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightInfo; @@ -73,23 +73,25 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; -import com.google.common.collect.ImmutableList; - -/** - * Test direct usage of Flight SQL workflows. - */ +/** Test direct usage of Flight SQL workflows. */ public class TestFlightSql { - protected static final Schema SCHEMA_INT_TABLE = new Schema(asList( - new Field("ID", new FieldType(false, MinorType.INT.getType(), null), null), - Field.nullable("KEYNAME", MinorType.VARCHAR.getType()), - Field.nullable("VALUE", MinorType.INT.getType()), - Field.nullable("FOREIGNID", MinorType.INT.getType()))); - private static final List> EXPECTED_RESULTS_FOR_STAR_SELECT_QUERY = ImmutableList.of( - asList("1", "one", "1", "1"), asList("2", "zero", "0", "1"), asList("3", "negative one", "-1", "1")); - private static final List> EXPECTED_RESULTS_FOR_PARAMETER_BINDING = ImmutableList.of( - asList("1", "one", "1", "1")); - private static final Map GET_SQL_INFO_EXPECTED_RESULTS_MAP = new LinkedHashMap<>(); + protected static final Schema SCHEMA_INT_TABLE = + new Schema( + asList( + new Field("ID", new FieldType(false, MinorType.INT.getType(), null), null), + Field.nullable("KEYNAME", MinorType.VARCHAR.getType()), + Field.nullable("VALUE", MinorType.INT.getType()), + Field.nullable("FOREIGNID", MinorType.INT.getType()))); + private static final List> EXPECTED_RESULTS_FOR_STAR_SELECT_QUERY = + ImmutableList.of( + asList("1", "one", "1", "1"), + asList("2", "zero", "0", "1"), + asList("3", "negative one", "-1", "1")); + private static final List> EXPECTED_RESULTS_FOR_PARAMETER_BINDING = + ImmutableList.of(asList("1", "one", "1", "1")); + private static final Map GET_SQL_INFO_EXPECTED_RESULTS_MAP = + new LinkedHashMap<>(); private static final String LOCALHOST = "localhost"; private static BufferAllocator allocator; private static FlightServer server; @@ -100,45 +102,45 @@ public static void setUp() throws Exception { allocator = new RootAllocator(Integer.MAX_VALUE); final Location serverLocation = Location.forGrpcInsecure(LOCALHOST, 0); - server = FlightServer.builder(allocator, serverLocation, new FlightSqlExample(serverLocation)) - .build() - .start(); + server = + FlightServer.builder(allocator, serverLocation, new FlightSqlExample(serverLocation)) + .build() + .start(); final Location clientLocation = Location.forGrpcInsecure(LOCALHOST, server.getPort()); sqlClient = new FlightSqlClient(FlightClient.builder(allocator, clientLocation).build()); - GET_SQL_INFO_EXPECTED_RESULTS_MAP - .put(Integer.toString(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_NAME_VALUE), "Apache Derby"); - GET_SQL_INFO_EXPECTED_RESULTS_MAP - .put(Integer.toString(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_VERSION_VALUE), "10.14.2.0 - (1828579)"); - GET_SQL_INFO_EXPECTED_RESULTS_MAP - .put(Integer.toString(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_ARROW_VERSION_VALUE), "10.14.2.0 - (1828579)"); - GET_SQL_INFO_EXPECTED_RESULTS_MAP - .put(Integer.toString(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_READ_ONLY_VALUE), "false"); - GET_SQL_INFO_EXPECTED_RESULTS_MAP - .put(Integer.toString(FlightSql.SqlInfo.SQL_ALL_TABLES_ARE_SELECTABLE_VALUE), "true"); - GET_SQL_INFO_EXPECTED_RESULTS_MAP - .put( - Integer.toString(FlightSql.SqlInfo.SQL_NULL_ORDERING_VALUE), - Integer.toString(FlightSql.SqlNullOrdering.SQL_NULLS_SORTED_AT_END_VALUE)); - GET_SQL_INFO_EXPECTED_RESULTS_MAP - .put(Integer.toString(FlightSql.SqlInfo.SQL_DDL_CATALOG_VALUE), "false"); - GET_SQL_INFO_EXPECTED_RESULTS_MAP - .put(Integer.toString(FlightSql.SqlInfo.SQL_DDL_SCHEMA_VALUE), "true"); - GET_SQL_INFO_EXPECTED_RESULTS_MAP - .put(Integer.toString(FlightSql.SqlInfo.SQL_DDL_TABLE_VALUE), "true"); - GET_SQL_INFO_EXPECTED_RESULTS_MAP - .put( - Integer.toString(FlightSql.SqlInfo.SQL_IDENTIFIER_CASE_VALUE), - Integer.toString(SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_UPPERCASE_VALUE)); - GET_SQL_INFO_EXPECTED_RESULTS_MAP - .put(Integer.toString(FlightSql.SqlInfo.SQL_IDENTIFIER_QUOTE_CHAR_VALUE), "\""); - GET_SQL_INFO_EXPECTED_RESULTS_MAP - .put( - Integer.toString(FlightSql.SqlInfo.SQL_QUOTED_IDENTIFIER_CASE_VALUE), - Integer.toString(SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_CASE_INSENSITIVE_VALUE)); - GET_SQL_INFO_EXPECTED_RESULTS_MAP - .put(Integer.toString(FlightSql.SqlInfo.SQL_MAX_COLUMNS_IN_TABLE_VALUE), "42"); + GET_SQL_INFO_EXPECTED_RESULTS_MAP.put( + Integer.toString(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_NAME_VALUE), "Apache Derby"); + GET_SQL_INFO_EXPECTED_RESULTS_MAP.put( + Integer.toString(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_VERSION_VALUE), + "10.14.2.0 - (1828579)"); + GET_SQL_INFO_EXPECTED_RESULTS_MAP.put( + Integer.toString(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_ARROW_VERSION_VALUE), + "10.14.2.0 - (1828579)"); + GET_SQL_INFO_EXPECTED_RESULTS_MAP.put( + Integer.toString(FlightSql.SqlInfo.FLIGHT_SQL_SERVER_READ_ONLY_VALUE), "false"); + GET_SQL_INFO_EXPECTED_RESULTS_MAP.put( + Integer.toString(FlightSql.SqlInfo.SQL_ALL_TABLES_ARE_SELECTABLE_VALUE), "true"); + GET_SQL_INFO_EXPECTED_RESULTS_MAP.put( + Integer.toString(FlightSql.SqlInfo.SQL_NULL_ORDERING_VALUE), + Integer.toString(FlightSql.SqlNullOrdering.SQL_NULLS_SORTED_AT_END_VALUE)); + GET_SQL_INFO_EXPECTED_RESULTS_MAP.put( + Integer.toString(FlightSql.SqlInfo.SQL_DDL_CATALOG_VALUE), "false"); + GET_SQL_INFO_EXPECTED_RESULTS_MAP.put( + Integer.toString(FlightSql.SqlInfo.SQL_DDL_SCHEMA_VALUE), "true"); + GET_SQL_INFO_EXPECTED_RESULTS_MAP.put( + Integer.toString(FlightSql.SqlInfo.SQL_DDL_TABLE_VALUE), "true"); + GET_SQL_INFO_EXPECTED_RESULTS_MAP.put( + Integer.toString(FlightSql.SqlInfo.SQL_IDENTIFIER_CASE_VALUE), + Integer.toString(SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_UPPERCASE_VALUE)); + GET_SQL_INFO_EXPECTED_RESULTS_MAP.put( + Integer.toString(FlightSql.SqlInfo.SQL_IDENTIFIER_QUOTE_CHAR_VALUE), "\""); + GET_SQL_INFO_EXPECTED_RESULTS_MAP.put( + Integer.toString(FlightSql.SqlInfo.SQL_QUOTED_IDENTIFIER_CASE_VALUE), + Integer.toString(SqlSupportedCaseSensitivity.SQL_CASE_SENSITIVITY_CASE_INSENSITIVE_VALUE)); + GET_SQL_INFO_EXPECTED_RESULTS_MAP.put( + Integer.toString(FlightSql.SqlInfo.SQL_MAX_COLUMNS_IN_TABLE_VALUE), "42"); } @AfterAll @@ -146,8 +148,10 @@ public static void tearDown() throws Exception { close(sqlClient, server, allocator); } - private static List> getNonConformingResultsForGetSqlInfo(final List> results) { - return getNonConformingResultsForGetSqlInfo(results, + private static List> getNonConformingResultsForGetSqlInfo( + final List> results) { + return getNonConformingResultsForGetSqlInfo( + results, FlightSql.SqlInfo.FLIGHT_SQL_SERVER_NAME, FlightSql.SqlInfo.FLIGHT_SQL_SERVER_VERSION, FlightSql.SqlInfo.FLIGHT_SQL_SERVER_ARROW_VERSION, @@ -164,8 +168,7 @@ private static List> getNonConformingResultsForGetSqlInfo(final Lis } private static List> getNonConformingResultsForGetSqlInfo( - final List> results, - final FlightSql.SqlInfo... args) { + final List> results, final FlightSql.SqlInfo... args) { final List> nonConformingResults = new ArrayList<>(); if (results.size() == args.length) { for (int index = 0; index < results.size(); index++) { @@ -173,8 +176,8 @@ private static List> getNonConformingResultsForGetSqlInfo( final String providedName = result.get(0); final String expectedName = Integer.toString(args[index].getNumber()); System.err.println(expectedName); - if (!(GET_SQL_INFO_EXPECTED_RESULTS_MAP.get(providedName).equals(result.get(1)) && - providedName.equals(expectedName))) { + if (!(GET_SQL_INFO_EXPECTED_RESULTS_MAP.get(providedName).equals(result.get(1)) + && providedName.equals(expectedName))) { nonConformingResults.add(result); break; } @@ -186,181 +189,248 @@ private static List> getNonConformingResultsForGetSqlInfo( @Test public void testGetTablesSchema() { final FlightInfo info = sqlClient.getTables(null, null, null, null, true); - MatcherAssert.assertThat(info.getSchemaOptional(), is(Optional.of(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA))); + MatcherAssert.assertThat( + info.getSchemaOptional(), is(Optional.of(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA))); } @Test public void testGetTablesSchemaExcludeSchema() { final FlightInfo info = sqlClient.getTables(null, null, null, null, false); MatcherAssert.assertThat( - info.getSchemaOptional(), - is(Optional.of(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA_NO_SCHEMA))); + info.getSchemaOptional(), + is(Optional.of(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA_NO_SCHEMA))); } @Test public void testGetTablesResultNoSchema() throws Exception { try (final FlightStream stream = - sqlClient.getStream( - sqlClient.getTables(null, null, null, null, false) - .getEndpoints().get(0).getTicket())) { + sqlClient.getStream( + sqlClient.getTables(null, null, null, null, false).getEndpoints().get(0).getTicket())) { Assertions.assertAll( () -> { - MatcherAssert.assertThat(stream.getSchema(), is(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA_NO_SCHEMA)); + MatcherAssert.assertThat( + stream.getSchema(), is(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA_NO_SCHEMA)); }, () -> { final List> results = getResults(stream); - final List> expectedResults = ImmutableList.of( - // catalog_name | schema_name | table_name | table_type | table_schema - asList(null /* TODO No catalog yet */, "SYS", "SYSALIASES", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSCHECKS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSCOLPERMS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSCOLUMNS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSCONGLOMERATES", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSCONSTRAINTS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSDEPENDS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSFILES", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSFOREIGNKEYS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSKEYS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSPERMS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSROLES", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSROUTINEPERMS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSSCHEMAS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSSEQUENCES", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSSTATEMENTS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSSTATISTICS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSTABLEPERMS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSTABLES", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSTRIGGERS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSUSERS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYS", "SYSVIEWS", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "SYSIBM", "SYSDUMMY1", "SYSTEM TABLE"), - asList(null /* TODO No catalog yet */, "APP", "FOREIGNTABLE", "TABLE"), - asList(null /* TODO No catalog yet */, "APP", "INTTABLE", "TABLE")); + final List> expectedResults = + ImmutableList.of( + // catalog_name | schema_name | table_name | table_type | table_schema + asList(null /* TODO No catalog yet */, "SYS", "SYSALIASES", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSCHECKS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSCOLPERMS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSCOLUMNS", "SYSTEM TABLE"), + asList( + null /* TODO No catalog yet */, "SYS", "SYSCONGLOMERATES", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSCONSTRAINTS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSDEPENDS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSFILES", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSFOREIGNKEYS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSKEYS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSPERMS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSROLES", "SYSTEM TABLE"), + asList( + null /* TODO No catalog yet */, "SYS", "SYSROUTINEPERMS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSSCHEMAS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSSEQUENCES", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSSTATEMENTS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSSTATISTICS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSTABLEPERMS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSTABLES", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSTRIGGERS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSUSERS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYS", "SYSVIEWS", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "SYSIBM", "SYSDUMMY1", "SYSTEM TABLE"), + asList(null /* TODO No catalog yet */, "APP", "FOREIGNTABLE", "TABLE"), + asList(null /* TODO No catalog yet */, "APP", "INTTABLE", "TABLE")); MatcherAssert.assertThat(results, is(expectedResults)); - } - ); + }); } } @Test public void testGetTablesResultFilteredNoSchema() throws Exception { try (final FlightStream stream = - sqlClient.getStream( - sqlClient.getTables(null, null, null, singletonList("TABLE"), false) - .getEndpoints().get(0).getTicket())) { + sqlClient.getStream( + sqlClient + .getTables(null, null, null, singletonList("TABLE"), false) + .getEndpoints() + .get(0) + .getTicket())) { Assertions.assertAll( - () -> MatcherAssert.assertThat(stream.getSchema(), is(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA_NO_SCHEMA)), + () -> + MatcherAssert.assertThat( + stream.getSchema(), is(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA_NO_SCHEMA)), () -> { final List> results = getResults(stream); - final List> expectedResults = ImmutableList.of( - // catalog_name | schema_name | table_name | table_type | table_schema - asList(null /* TODO No catalog yet */, "APP", "FOREIGNTABLE", "TABLE"), - asList(null /* TODO No catalog yet */, "APP", "INTTABLE", "TABLE")); + final List> expectedResults = + ImmutableList.of( + // catalog_name | schema_name | table_name | table_type | table_schema + asList(null /* TODO No catalog yet */, "APP", "FOREIGNTABLE", "TABLE"), + asList(null /* TODO No catalog yet */, "APP", "INTTABLE", "TABLE")); MatcherAssert.assertThat(results, is(expectedResults)); - } - ); + }); } } @Test public void testGetTablesResultFilteredWithSchema() throws Exception { try (final FlightStream stream = - sqlClient.getStream( - sqlClient.getTables(null, null, null, singletonList("TABLE"), true) - .getEndpoints().get(0).getTicket())) { + sqlClient.getStream( + sqlClient + .getTables(null, null, null, singletonList("TABLE"), true) + .getEndpoints() + .get(0) + .getTicket())) { Assertions.assertAll( - () -> MatcherAssert.assertThat(stream.getSchema(), is(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA)), + () -> + MatcherAssert.assertThat( + stream.getSchema(), is(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA)), () -> { - MatcherAssert.assertThat(stream.getSchema(), is(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA)); + MatcherAssert.assertThat( + stream.getSchema(), is(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA)); final List> results = getResults(stream); - final List> expectedResults = ImmutableList.of( - // catalog_name | schema_name | table_name | table_type | table_schema - asList( - null /* TODO No catalog yet */, - "APP", - "FOREIGNTABLE", - "TABLE", - new Schema(asList( - new Field("ID", new FieldType(false, MinorType.INT.getType(), null, - new FlightSqlColumnMetadata.Builder() - .catalogName("") - .typeName("INTEGER") - .schemaName("APP") - .tableName("FOREIGNTABLE") - .precision(10) - .scale(0) - .isAutoIncrement(true) - .build().getMetadataMap()), null), - new Field("FOREIGNNAME", new FieldType(true, MinorType.VARCHAR.getType(), null, - new FlightSqlColumnMetadata.Builder() - .catalogName("") - .typeName("VARCHAR") - .schemaName("APP") - .tableName("FOREIGNTABLE") - .precision(100) - .scale(0) - .isAutoIncrement(false) - .build().getMetadataMap()), null), - new Field("VALUE", new FieldType(true, MinorType.INT.getType(), null, - new FlightSqlColumnMetadata.Builder() - .catalogName("") - .typeName("INTEGER") - .schemaName("APP") - .tableName("FOREIGNTABLE") - .precision(10) - .scale(0) - .isAutoIncrement(false) - .build().getMetadataMap()), null))).toJson()), - asList( - null /* TODO No catalog yet */, - "APP", - "INTTABLE", - "TABLE", - new Schema(asList( - new Field("ID", new FieldType(false, MinorType.INT.getType(), null, - new FlightSqlColumnMetadata.Builder() - .catalogName("") - .typeName("INTEGER") - .schemaName("APP") - .tableName("INTTABLE") - .precision(10) - .scale(0) - .isAutoIncrement(true) - .build().getMetadataMap()), null), - new Field("KEYNAME", new FieldType(true, MinorType.VARCHAR.getType(), null, - new FlightSqlColumnMetadata.Builder() - .catalogName("") - .typeName("VARCHAR") - .schemaName("APP") - .tableName("INTTABLE") - .precision(100) - .scale(0) - .isAutoIncrement(false) - .build().getMetadataMap()), null), - new Field("VALUE", new FieldType(true, MinorType.INT.getType(), null, - new FlightSqlColumnMetadata.Builder() - .catalogName("") - .typeName("INTEGER") - .schemaName("APP") - .tableName("INTTABLE") - .precision(10) - .scale(0) - .isAutoIncrement(false) - .build().getMetadataMap()), null), - new Field("FOREIGNID", new FieldType(true, MinorType.INT.getType(), null, - new FlightSqlColumnMetadata.Builder() - .catalogName("") - .typeName("INTEGER") - .schemaName("APP") - .tableName("INTTABLE") - .precision(10) - .scale(0) - .isAutoIncrement(false) - .build().getMetadataMap()), null))).toJson())); + final List> expectedResults = + ImmutableList.of( + // catalog_name | schema_name | table_name | table_type | table_schema + asList( + null /* TODO No catalog yet */, + "APP", + "FOREIGNTABLE", + "TABLE", + new Schema( + asList( + new Field( + "ID", + new FieldType( + false, + MinorType.INT.getType(), + null, + new FlightSqlColumnMetadata.Builder() + .catalogName("") + .typeName("INTEGER") + .schemaName("APP") + .tableName("FOREIGNTABLE") + .precision(10) + .scale(0) + .isAutoIncrement(true) + .build() + .getMetadataMap()), + null), + new Field( + "FOREIGNNAME", + new FieldType( + true, + MinorType.VARCHAR.getType(), + null, + new FlightSqlColumnMetadata.Builder() + .catalogName("") + .typeName("VARCHAR") + .schemaName("APP") + .tableName("FOREIGNTABLE") + .precision(100) + .scale(0) + .isAutoIncrement(false) + .build() + .getMetadataMap()), + null), + new Field( + "VALUE", + new FieldType( + true, + MinorType.INT.getType(), + null, + new FlightSqlColumnMetadata.Builder() + .catalogName("") + .typeName("INTEGER") + .schemaName("APP") + .tableName("FOREIGNTABLE") + .precision(10) + .scale(0) + .isAutoIncrement(false) + .build() + .getMetadataMap()), + null))) + .toJson()), + asList( + null /* TODO No catalog yet */, + "APP", + "INTTABLE", + "TABLE", + new Schema( + asList( + new Field( + "ID", + new FieldType( + false, + MinorType.INT.getType(), + null, + new FlightSqlColumnMetadata.Builder() + .catalogName("") + .typeName("INTEGER") + .schemaName("APP") + .tableName("INTTABLE") + .precision(10) + .scale(0) + .isAutoIncrement(true) + .build() + .getMetadataMap()), + null), + new Field( + "KEYNAME", + new FieldType( + true, + MinorType.VARCHAR.getType(), + null, + new FlightSqlColumnMetadata.Builder() + .catalogName("") + .typeName("VARCHAR") + .schemaName("APP") + .tableName("INTTABLE") + .precision(100) + .scale(0) + .isAutoIncrement(false) + .build() + .getMetadataMap()), + null), + new Field( + "VALUE", + new FieldType( + true, + MinorType.INT.getType(), + null, + new FlightSqlColumnMetadata.Builder() + .catalogName("") + .typeName("INTEGER") + .schemaName("APP") + .tableName("INTTABLE") + .precision(10) + .scale(0) + .isAutoIncrement(false) + .build() + .getMetadataMap()), + null), + new Field( + "FOREIGNID", + new FieldType( + true, + MinorType.INT.getType(), + null, + new FlightSqlColumnMetadata.Builder() + .catalogName("") + .typeName("INTEGER") + .schemaName("APP") + .tableName("INTTABLE") + .precision(10) + .scale(0) + .isAutoIncrement(false) + .build() + .getMetadataMap()), + null))) + .toJson())); MatcherAssert.assertThat(results, is(expectedResults)); - } - ); + }); } } @@ -371,25 +441,24 @@ public void testSimplePreparedStatementSchema() throws Exception { () -> { final Schema actualSchema = preparedStatement.getResultSetSchema(); MatcherAssert.assertThat(actualSchema, is(SCHEMA_INT_TABLE)); - }, () -> { final FlightInfo info = preparedStatement.execute(); MatcherAssert.assertThat(info.getSchemaOptional(), is(Optional.of(SCHEMA_INT_TABLE))); - } - ); + }); } } @Test public void testSimplePreparedStatementResults() throws Exception { try (final PreparedStatement preparedStatement = sqlClient.prepare("SELECT * FROM intTable"); - final FlightStream stream = sqlClient.getStream( - preparedStatement.execute().getEndpoints().get(0).getTicket())) { + final FlightStream stream = + sqlClient.getStream(preparedStatement.execute().getEndpoints().get(0).getTicket())) { Assertions.assertAll( () -> MatcherAssert.assertThat(stream.getSchema(), is(SCHEMA_INT_TABLE)), - () -> MatcherAssert.assertThat(getResults(stream), is(EXPECTED_RESULTS_FOR_STAR_SELECT_QUERY)) - ); + () -> + MatcherAssert.assertThat( + getResults(stream), is(EXPECTED_RESULTS_FOR_STAR_SELECT_QUERY))); } } @@ -397,7 +466,8 @@ public void testSimplePreparedStatementResults() throws Exception { public void testSimplePreparedStatementResultsWithParameterBinding() throws Exception { try (PreparedStatement prepare = sqlClient.prepare("SELECT * FROM intTable WHERE id = ?")) { final Schema parameterSchema = prepare.getParameterSchema(); - try (final VectorSchemaRoot insertRoot = VectorSchemaRoot.create(parameterSchema, allocator)) { + try (final VectorSchemaRoot insertRoot = + VectorSchemaRoot.create(parameterSchema, allocator)) { insertRoot.allocateNew(); final IntVector valueVector = (IntVector) insertRoot.getVector(0); @@ -407,24 +477,26 @@ public void testSimplePreparedStatementResultsWithParameterBinding() throws Exce prepare.setParameters(insertRoot); FlightInfo flightInfo = prepare.execute(); - FlightStream stream = sqlClient.getStream(flightInfo - .getEndpoints() - .get(0).getTicket()); + FlightStream stream = sqlClient.getStream(flightInfo.getEndpoints().get(0).getTicket()); Assertions.assertAll( () -> MatcherAssert.assertThat(stream.getSchema(), is(SCHEMA_INT_TABLE)), - () -> MatcherAssert.assertThat(getResults(stream), is(EXPECTED_RESULTS_FOR_PARAMETER_BINDING)) - ); + () -> + MatcherAssert.assertThat( + getResults(stream), is(EXPECTED_RESULTS_FOR_PARAMETER_BINDING))); } } } @Test public void testSimplePreparedStatementUpdateResults() throws SQLException { - try (PreparedStatement prepare = sqlClient.prepare("INSERT INTO INTTABLE (keyName, value ) VALUES (?, ?)"); - PreparedStatement deletePrepare = sqlClient.prepare("DELETE FROM INTTABLE WHERE keyName = ?")) { + try (PreparedStatement prepare = + sqlClient.prepare("INSERT INTO INTTABLE (keyName, value ) VALUES (?, ?)"); + PreparedStatement deletePrepare = + sqlClient.prepare("DELETE FROM INTTABLE WHERE keyName = ?")) { final Schema parameterSchema = prepare.getParameterSchema(); - try (final VectorSchemaRoot insertRoot = VectorSchemaRoot.create(parameterSchema, allocator)) { + try (final VectorSchemaRoot insertRoot = + VectorSchemaRoot.create(parameterSchema, allocator)) { final VarCharVector varCharVector = (VarCharVector) insertRoot.getVector(0); final IntVector valueVector = (IntVector) insertRoot.getVector(1); final int counter = 10; @@ -432,10 +504,11 @@ public void testSimplePreparedStatementUpdateResults() throws SQLException { final IntStream range = IntStream.range(0, counter); - range.forEach(i -> { - valueVector.setSafe(i, i * counter); - varCharVector.setSafe(i, new Text("value" + i)); - }); + range.forEach( + i -> { + valueVector.setSafe(i, i * counter); + varCharVector.setSafe(i, new Text("value" + i)); + }); insertRoot.setRowCount(counter); @@ -449,25 +522,24 @@ public void testSimplePreparedStatementUpdateResults() throws SQLException { } Assertions.assertAll( () -> MatcherAssert.assertThat(updatedRows, is(10L)), - () -> MatcherAssert.assertThat(deletedRows, is(10L)) - ); + () -> MatcherAssert.assertThat(deletedRows, is(10L))); } } } @Test public void testSimplePreparedStatementUpdateResultsWithoutParameters() throws SQLException { - try (PreparedStatement prepare = sqlClient - .prepare("INSERT INTO INTTABLE (keyName, value ) VALUES ('test', 1000)"); - PreparedStatement deletePrepare = sqlClient.prepare("DELETE FROM INTTABLE WHERE keyName = 'test'")) { + try (PreparedStatement prepare = + sqlClient.prepare("INSERT INTO INTTABLE (keyName, value ) VALUES ('test', 1000)"); + PreparedStatement deletePrepare = + sqlClient.prepare("DELETE FROM INTTABLE WHERE keyName = 'test'")) { final long updatedRows = prepare.executeUpdate(); final long deletedRows = deletePrepare.executeUpdate(); Assertions.assertAll( () -> MatcherAssert.assertThat(updatedRows, is(1L)), - () -> MatcherAssert.assertThat(deletedRows, is(1L)) - ); + () -> MatcherAssert.assertThat(deletedRows, is(1L))); } } @@ -481,27 +553,28 @@ public void testSimplePreparedStatementClosesProperly() { () -> { preparedStatement.close(); MatcherAssert.assertThat(preparedStatement.isClosed(), is(true)); - } - ); + }); } @Test public void testGetCatalogsSchema() { final FlightInfo info = sqlClient.getCatalogs(); - MatcherAssert.assertThat(info.getSchemaOptional(), is(Optional.of(FlightSqlProducer.Schemas.GET_CATALOGS_SCHEMA))); + MatcherAssert.assertThat( + info.getSchemaOptional(), is(Optional.of(FlightSqlProducer.Schemas.GET_CATALOGS_SCHEMA))); } @Test public void testGetCatalogsResults() throws Exception { try (final FlightStream stream = - sqlClient.getStream(sqlClient.getCatalogs().getEndpoints().get(0).getTicket())) { + sqlClient.getStream(sqlClient.getCatalogs().getEndpoints().get(0).getTicket())) { Assertions.assertAll( - () -> MatcherAssert.assertThat(stream.getSchema(), is(FlightSqlProducer.Schemas.GET_CATALOGS_SCHEMA)), + () -> + MatcherAssert.assertThat( + stream.getSchema(), is(FlightSqlProducer.Schemas.GET_CATALOGS_SCHEMA)), () -> { List> catalogs = getResults(stream); MatcherAssert.assertThat(catalogs, is(emptyList())); - } - ); + }); } } @@ -509,65 +582,67 @@ public void testGetCatalogsResults() throws Exception { public void testGetTableTypesSchema() { final FlightInfo info = sqlClient.getTableTypes(); MatcherAssert.assertThat( - info.getSchemaOptional(), - is(Optional.of(FlightSqlProducer.Schemas.GET_TABLE_TYPES_SCHEMA))); + info.getSchemaOptional(), + is(Optional.of(FlightSqlProducer.Schemas.GET_TABLE_TYPES_SCHEMA))); } @Test public void testGetTableTypesResult() throws Exception { try (final FlightStream stream = - sqlClient.getStream(sqlClient.getTableTypes().getEndpoints().get(0).getTicket())) { + sqlClient.getStream(sqlClient.getTableTypes().getEndpoints().get(0).getTicket())) { Assertions.assertAll( () -> { - MatcherAssert.assertThat(stream.getSchema(), is(FlightSqlProducer.Schemas.GET_TABLE_TYPES_SCHEMA)); + MatcherAssert.assertThat( + stream.getSchema(), is(FlightSqlProducer.Schemas.GET_TABLE_TYPES_SCHEMA)); }, () -> { final List> tableTypes = getResults(stream); - final List> expectedTableTypes = ImmutableList.of( - // table_type - singletonList("SYNONYM"), - singletonList("SYSTEM TABLE"), - singletonList("TABLE"), - singletonList("VIEW") - ); + final List> expectedTableTypes = + ImmutableList.of( + // table_type + singletonList("SYNONYM"), + singletonList("SYSTEM TABLE"), + singletonList("TABLE"), + singletonList("VIEW")); MatcherAssert.assertThat(tableTypes, is(expectedTableTypes)); - } - ); + }); } } @Test public void testGetSchemasSchema() { final FlightInfo info = sqlClient.getSchemas(null, null); - MatcherAssert.assertThat(info.getSchemaOptional(), is(Optional.of(FlightSqlProducer.Schemas.GET_SCHEMAS_SCHEMA))); + MatcherAssert.assertThat( + info.getSchemaOptional(), is(Optional.of(FlightSqlProducer.Schemas.GET_SCHEMAS_SCHEMA))); } @Test public void testGetSchemasResult() throws Exception { try (final FlightStream stream = - sqlClient.getStream(sqlClient.getSchemas(null, null).getEndpoints().get(0).getTicket())) { + sqlClient.getStream(sqlClient.getSchemas(null, null).getEndpoints().get(0).getTicket())) { Assertions.assertAll( () -> { - MatcherAssert.assertThat(stream.getSchema(), is(FlightSqlProducer.Schemas.GET_SCHEMAS_SCHEMA)); + MatcherAssert.assertThat( + stream.getSchema(), is(FlightSqlProducer.Schemas.GET_SCHEMAS_SCHEMA)); }, () -> { final List> schemas = getResults(stream); - final List> expectedSchemas = ImmutableList.of( - // catalog_name | schema_name - asList(null /* TODO Add catalog. */, "APP"), - asList(null /* TODO Add catalog. */, "NULLID"), - asList(null /* TODO Add catalog. */, "SQLJ"), - asList(null /* TODO Add catalog. */, "SYS"), - asList(null /* TODO Add catalog. */, "SYSCAT"), - asList(null /* TODO Add catalog. */, "SYSCS_DIAG"), - asList(null /* TODO Add catalog. */, "SYSCS_UTIL"), - asList(null /* TODO Add catalog. */, "SYSFUN"), - asList(null /* TODO Add catalog. */, "SYSIBM"), - asList(null /* TODO Add catalog. */, "SYSPROC"), - asList(null /* TODO Add catalog. */, "SYSSTAT")); + final List> expectedSchemas = + ImmutableList.of( + // catalog_name | schema_name + asList(null /* TODO Add catalog. */, "APP"), + asList(null /* TODO Add catalog. */, "NULLID"), + asList(null /* TODO Add catalog. */, "SQLJ"), + asList(null /* TODO Add catalog. */, "SYS"), + asList(null /* TODO Add catalog. */, "SYSCAT"), + asList(null /* TODO Add catalog. */, "SYSCS_DIAG"), + asList(null /* TODO Add catalog. */, "SYSCS_UTIL"), + asList(null /* TODO Add catalog. */, "SYSFUN"), + asList(null /* TODO Add catalog. */, "SYSIBM"), + asList(null /* TODO Add catalog. */, "SYSPROC"), + asList(null /* TODO Add catalog. */, "SYSSTAT")); MatcherAssert.assertThat(schemas, is(expectedSchemas)); - } - ); + }); } } @@ -588,16 +663,15 @@ public void testGetPrimaryKey() { () -> MatcherAssert.assertThat(result.get(2), is("INTTABLE")), () -> MatcherAssert.assertThat(result.get(3), is("ID")), () -> MatcherAssert.assertThat(result.get(4), is("1")), - () -> MatcherAssert.assertThat(result.get(5), notNullValue()) - ); - } - ); + () -> MatcherAssert.assertThat(result.get(5), notNullValue())); + }); } @Test public void testGetSqlInfoSchema() { final FlightInfo info = sqlClient.getSqlInfo(); - MatcherAssert.assertThat(info.getSchemaOptional(), is(Optional.of(FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA))); + MatcherAssert.assertThat( + info.getSchemaOptional(), is(Optional.of(FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA))); } @Test @@ -605,9 +679,12 @@ public void testGetSqlInfoResults() throws Exception { final FlightInfo info = sqlClient.getSqlInfo(); try (final FlightStream stream = sqlClient.getStream(info.getEndpoints().get(0).getTicket())) { Assertions.assertAll( - () -> MatcherAssert.assertThat(stream.getSchema(), is(FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA)), - () -> MatcherAssert.assertThat(getNonConformingResultsForGetSqlInfo(getResults(stream)), is(emptyList())) - ); + () -> + MatcherAssert.assertThat( + stream.getSchema(), is(FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA)), + () -> + MatcherAssert.assertThat( + getNonConformingResultsForGetSqlInfo(getResults(stream)), is(emptyList()))); } } @@ -617,40 +694,41 @@ public void testGetSqlInfoResultsWithSingleArg() throws Exception { final FlightInfo info = sqlClient.getSqlInfo(arg); try (final FlightStream stream = sqlClient.getStream(info.getEndpoints().get(0).getTicket())) { Assertions.assertAll( - () -> MatcherAssert.assertThat(stream.getSchema(), is(FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA)), - () -> MatcherAssert.assertThat(getNonConformingResultsForGetSqlInfo(getResults(stream), arg), is(emptyList())) - ); + () -> + MatcherAssert.assertThat( + stream.getSchema(), is(FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA)), + () -> + MatcherAssert.assertThat( + getNonConformingResultsForGetSqlInfo(getResults(stream), arg), is(emptyList()))); } } @Test public void testGetSqlInfoResultsWithManyArgs() throws Exception { final FlightSql.SqlInfo[] args = { - FlightSql.SqlInfo.FLIGHT_SQL_SERVER_NAME, - FlightSql.SqlInfo.FLIGHT_SQL_SERVER_VERSION, - FlightSql.SqlInfo.FLIGHT_SQL_SERVER_ARROW_VERSION, - FlightSql.SqlInfo.FLIGHT_SQL_SERVER_READ_ONLY, - FlightSql.SqlInfo.SQL_ALL_TABLES_ARE_SELECTABLE, - FlightSql.SqlInfo.SQL_NULL_ORDERING, - FlightSql.SqlInfo.SQL_DDL_CATALOG, - FlightSql.SqlInfo.SQL_DDL_SCHEMA, - FlightSql.SqlInfo.SQL_DDL_TABLE, - FlightSql.SqlInfo.SQL_IDENTIFIER_CASE, - FlightSql.SqlInfo.SQL_IDENTIFIER_QUOTE_CHAR, - FlightSql.SqlInfo.SQL_QUOTED_IDENTIFIER_CASE, - FlightSql.SqlInfo.SQL_MAX_COLUMNS_IN_TABLE}; + FlightSql.SqlInfo.FLIGHT_SQL_SERVER_NAME, + FlightSql.SqlInfo.FLIGHT_SQL_SERVER_VERSION, + FlightSql.SqlInfo.FLIGHT_SQL_SERVER_ARROW_VERSION, + FlightSql.SqlInfo.FLIGHT_SQL_SERVER_READ_ONLY, + FlightSql.SqlInfo.SQL_ALL_TABLES_ARE_SELECTABLE, + FlightSql.SqlInfo.SQL_NULL_ORDERING, + FlightSql.SqlInfo.SQL_DDL_CATALOG, + FlightSql.SqlInfo.SQL_DDL_SCHEMA, + FlightSql.SqlInfo.SQL_DDL_TABLE, + FlightSql.SqlInfo.SQL_IDENTIFIER_CASE, + FlightSql.SqlInfo.SQL_IDENTIFIER_QUOTE_CHAR, + FlightSql.SqlInfo.SQL_QUOTED_IDENTIFIER_CASE, + FlightSql.SqlInfo.SQL_MAX_COLUMNS_IN_TABLE + }; final FlightInfo info = sqlClient.getSqlInfo(args); try (final FlightStream stream = sqlClient.getStream(info.getEndpoints().get(0).getTicket())) { Assertions.assertAll( - () -> MatcherAssert.assertThat( - stream.getSchema(), - is(FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA) - ), - () -> MatcherAssert.assertThat( - getNonConformingResultsForGetSqlInfo(getResults(stream), args), - is(emptyList()) - ) - ); + () -> + MatcherAssert.assertThat( + stream.getSchema(), is(FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA)), + () -> + MatcherAssert.assertThat( + getNonConformingResultsForGetSqlInfo(getResults(stream), args), is(emptyList()))); } } @@ -658,25 +736,29 @@ public void testGetSqlInfoResultsWithManyArgs() throws Exception { public void testGetCommandExportedKeys() throws Exception { try (final FlightStream stream = sqlClient.getStream( - sqlClient.getExportedKeys(TableRef.of(null, null, "FOREIGNTABLE")) - .getEndpoints().get(0).getTicket())) { + sqlClient + .getExportedKeys(TableRef.of(null, null, "FOREIGNTABLE")) + .getEndpoints() + .get(0) + .getTicket())) { final List> results = getResults(stream); - final List> matchers = asList( - nullValue(String.class), // pk_catalog_name - is("APP"), // pk_schema_name - is("FOREIGNTABLE"), // pk_table_name - is("ID"), // pk_column_name - nullValue(String.class), // fk_catalog_name - is("APP"), // fk_schema_name - is("INTTABLE"), // fk_table_name - is("FOREIGNID"), // fk_column_name - is("1"), // key_sequence - containsString("SQL"), // fk_key_name - containsString("SQL"), // pk_key_name - is("3"), // update_rule - is("3")); // delete_rule + final List> matchers = + asList( + nullValue(String.class), // pk_catalog_name + is("APP"), // pk_schema_name + is("FOREIGNTABLE"), // pk_table_name + is("ID"), // pk_column_name + nullValue(String.class), // fk_catalog_name + is("APP"), // fk_schema_name + is("INTTABLE"), // fk_table_name + is("FOREIGNID"), // fk_column_name + is("1"), // key_sequence + containsString("SQL"), // fk_key_name + containsString("SQL"), // pk_key_name + is("3"), // update_rule + is("3")); // delete_rule final List assertions = new ArrayList<>(); Assertions.assertEquals(1, results.size()); @@ -693,25 +775,29 @@ public void testGetCommandExportedKeys() throws Exception { public void testGetCommandImportedKeys() throws Exception { try (final FlightStream stream = sqlClient.getStream( - sqlClient.getImportedKeys(TableRef.of(null, null, "INTTABLE")) - .getEndpoints().get(0).getTicket())) { + sqlClient + .getImportedKeys(TableRef.of(null, null, "INTTABLE")) + .getEndpoints() + .get(0) + .getTicket())) { final List> results = getResults(stream); - final List> matchers = asList( - nullValue(String.class), // pk_catalog_name - is("APP"), // pk_schema_name - is("FOREIGNTABLE"), // pk_table_name - is("ID"), // pk_column_name - nullValue(String.class), // fk_catalog_name - is("APP"), // fk_schema_name - is("INTTABLE"), // fk_table_name - is("FOREIGNID"), // fk_column_name - is("1"), // key_sequence - containsString("SQL"), // fk_key_name - containsString("SQL"), // pk_key_name - is("3"), // update_rule - is("3")); // delete_rule + final List> matchers = + asList( + nullValue(String.class), // pk_catalog_name + is("APP"), // pk_schema_name + is("FOREIGNTABLE"), // pk_table_name + is("ID"), // pk_column_name + nullValue(String.class), // fk_catalog_name + is("APP"), // fk_schema_name + is("INTTABLE"), // fk_table_name + is("FOREIGNID"), // fk_column_name + is("1"), // key_sequence + containsString("SQL"), // fk_key_name + containsString("SQL"), // pk_key_name + is("3"), // update_rule + is("3")); // delete_rule Assertions.assertEquals(1, results.size()); final List assertions = new ArrayList<>(); @@ -732,73 +818,448 @@ public void testGetTypeInfo() throws Exception { final List> results = getResults(stream); - final List> matchers = ImmutableList.of( - asList("BIGINT", "-5", "19", null, null, emptyList().toString(), "1", "false", "2", "false", "false", "true", - "BIGINT", "0", "0", - null, null, "10", null), - asList("LONG VARCHAR FOR BIT DATA", "-4", "32700", "X'", "'", emptyList().toString(), "1", "false", "0", - "true", "false", "false", - "LONG VARCHAR FOR BIT DATA", null, null, null, null, null, null), - asList("VARCHAR () FOR BIT DATA", "-3", "32672", "X'", "'", singletonList("length").toString(), "1", "false", - "2", "true", "false", - "false", "VARCHAR () FOR BIT DATA", null, null, null, null, null, null), - asList("CHAR () FOR BIT DATA", "-2", "254", "X'", "'", singletonList("length").toString(), "1", "false", "2", - "true", "false", "false", - "CHAR () FOR BIT DATA", null, null, null, null, null, null), - asList("LONG VARCHAR", "-1", "32700", "'", "'", emptyList().toString(), "1", "true", "1", "true", "false", - "false", - "LONG VARCHAR", null, null, null, null, null, null), - asList("CHAR", "1", "254", "'", "'", singletonList("length").toString(), "1", "true", "3", "true", "false", - "false", "CHAR", null, null, - null, null, null, null), - asList("NUMERIC", "2", "31", null, null, Arrays.asList("precision", "scale").toString(), "1", "false", "2", - "false", "true", "false", - "NUMERIC", "0", "31", null, null, "10", null), - asList("DECIMAL", "3", "31", null, null, Arrays.asList("precision", "scale").toString(), "1", "false", "2", - "false", "true", "false", - "DECIMAL", "0", "31", null, null, "10", null), - asList("INTEGER", "4", "10", null, null, emptyList().toString(), "1", "false", "2", "false", "false", "true", - "INTEGER", "0", "0", - null, null, "10", null), - asList("SMALLINT", "5", "5", null, null, emptyList().toString(), "1", "false", "2", "false", "false", "true", - "SMALLINT", "0", - "0", null, null, "10", null), - asList("FLOAT", "6", "52", null, null, singletonList("precision").toString(), "1", "false", "2", "false", - "false", "false", "FLOAT", null, - null, null, null, "2", null), - asList("REAL", "7", "23", null, null, emptyList().toString(), "1", "false", "2", "false", "false", "false", - "REAL", null, null, - null, null, "2", null), - asList("DOUBLE", "8", "52", null, null, emptyList().toString(), "1", "false", "2", "false", "false", "false", - "DOUBLE", null, - null, null, null, "2", null), - asList("VARCHAR", "12", "32672", "'", "'", singletonList("length").toString(), "1", "true", "3", "true", - "false", "false", "VARCHAR", - null, null, null, null, null, null), - asList("BOOLEAN", "16", "1", null, null, emptyList().toString(), "1", "false", "2", "true", "false", "false", - "BOOLEAN", null, - null, null, null, null, null), - asList("DATE", "91", "10", "DATE'", "'", emptyList().toString(), "1", "false", "2", "true", "false", "false", - "DATE", "0", "0", - null, null, "10", null), - asList("TIME", "92", "8", "TIME'", "'", emptyList().toString(), "1", "false", "2", "true", "false", "false", - "TIME", "0", "0", - null, null, "10", null), - asList("TIMESTAMP", "93", "29", "TIMESTAMP'", "'", emptyList().toString(), "1", "false", "2", "true", "false", - "false", - "TIMESTAMP", "0", "9", null, null, "10", null), - asList("OBJECT", "2000", null, null, null, emptyList().toString(), "1", "false", "2", "true", "false", - "false", "OBJECT", null, - null, null, null, null, null), - asList("BLOB", "2004", "2147483647", null, null, singletonList("length").toString(), "1", "false", "0", null, - "false", null, "BLOB", null, - null, null, null, null, null), - asList("CLOB", "2005", "2147483647", "'", "'", singletonList("length").toString(), "1", "true", "1", null, - "false", null, "CLOB", null, - null, null, null, null, null), - asList("XML", "2009", null, null, null, emptyList().toString(), "1", "true", "0", "false", "false", "false", - "XML", null, null, - null, null, null, null)); + final List> matchers = + ImmutableList.of( + asList( + "BIGINT", + "-5", + "19", + null, + null, + emptyList().toString(), + "1", + "false", + "2", + "false", + "false", + "true", + "BIGINT", + "0", + "0", + null, + null, + "10", + null), + asList( + "LONG VARCHAR FOR BIT DATA", + "-4", + "32700", + "X'", + "'", + emptyList().toString(), + "1", + "false", + "0", + "true", + "false", + "false", + "LONG VARCHAR FOR BIT DATA", + null, + null, + null, + null, + null, + null), + asList( + "VARCHAR () FOR BIT DATA", + "-3", + "32672", + "X'", + "'", + singletonList("length").toString(), + "1", + "false", + "2", + "true", + "false", + "false", + "VARCHAR () FOR BIT DATA", + null, + null, + null, + null, + null, + null), + asList( + "CHAR () FOR BIT DATA", + "-2", + "254", + "X'", + "'", + singletonList("length").toString(), + "1", + "false", + "2", + "true", + "false", + "false", + "CHAR () FOR BIT DATA", + null, + null, + null, + null, + null, + null), + asList( + "LONG VARCHAR", + "-1", + "32700", + "'", + "'", + emptyList().toString(), + "1", + "true", + "1", + "true", + "false", + "false", + "LONG VARCHAR", + null, + null, + null, + null, + null, + null), + asList( + "CHAR", + "1", + "254", + "'", + "'", + singletonList("length").toString(), + "1", + "true", + "3", + "true", + "false", + "false", + "CHAR", + null, + null, + null, + null, + null, + null), + asList( + "NUMERIC", + "2", + "31", + null, + null, + Arrays.asList("precision", "scale").toString(), + "1", + "false", + "2", + "false", + "true", + "false", + "NUMERIC", + "0", + "31", + null, + null, + "10", + null), + asList( + "DECIMAL", + "3", + "31", + null, + null, + Arrays.asList("precision", "scale").toString(), + "1", + "false", + "2", + "false", + "true", + "false", + "DECIMAL", + "0", + "31", + null, + null, + "10", + null), + asList( + "INTEGER", + "4", + "10", + null, + null, + emptyList().toString(), + "1", + "false", + "2", + "false", + "false", + "true", + "INTEGER", + "0", + "0", + null, + null, + "10", + null), + asList( + "SMALLINT", + "5", + "5", + null, + null, + emptyList().toString(), + "1", + "false", + "2", + "false", + "false", + "true", + "SMALLINT", + "0", + "0", + null, + null, + "10", + null), + asList( + "FLOAT", + "6", + "52", + null, + null, + singletonList("precision").toString(), + "1", + "false", + "2", + "false", + "false", + "false", + "FLOAT", + null, + null, + null, + null, + "2", + null), + asList( + "REAL", + "7", + "23", + null, + null, + emptyList().toString(), + "1", + "false", + "2", + "false", + "false", + "false", + "REAL", + null, + null, + null, + null, + "2", + null), + asList( + "DOUBLE", + "8", + "52", + null, + null, + emptyList().toString(), + "1", + "false", + "2", + "false", + "false", + "false", + "DOUBLE", + null, + null, + null, + null, + "2", + null), + asList( + "VARCHAR", + "12", + "32672", + "'", + "'", + singletonList("length").toString(), + "1", + "true", + "3", + "true", + "false", + "false", + "VARCHAR", + null, + null, + null, + null, + null, + null), + asList( + "BOOLEAN", + "16", + "1", + null, + null, + emptyList().toString(), + "1", + "false", + "2", + "true", + "false", + "false", + "BOOLEAN", + null, + null, + null, + null, + null, + null), + asList( + "DATE", + "91", + "10", + "DATE'", + "'", + emptyList().toString(), + "1", + "false", + "2", + "true", + "false", + "false", + "DATE", + "0", + "0", + null, + null, + "10", + null), + asList( + "TIME", + "92", + "8", + "TIME'", + "'", + emptyList().toString(), + "1", + "false", + "2", + "true", + "false", + "false", + "TIME", + "0", + "0", + null, + null, + "10", + null), + asList( + "TIMESTAMP", + "93", + "29", + "TIMESTAMP'", + "'", + emptyList().toString(), + "1", + "false", + "2", + "true", + "false", + "false", + "TIMESTAMP", + "0", + "9", + null, + null, + "10", + null), + asList( + "OBJECT", + "2000", + null, + null, + null, + emptyList().toString(), + "1", + "false", + "2", + "true", + "false", + "false", + "OBJECT", + null, + null, + null, + null, + null, + null), + asList( + "BLOB", + "2004", + "2147483647", + null, + null, + singletonList("length").toString(), + "1", + "false", + "0", + null, + "false", + null, + "BLOB", + null, + null, + null, + null, + null, + null), + asList( + "CLOB", + "2005", + "2147483647", + "'", + "'", + singletonList("length").toString(), + "1", + "true", + "1", + null, + "false", + null, + "CLOB", + null, + null, + null, + null, + null, + null), + asList( + "XML", + "2009", + null, + null, + null, + emptyList().toString(), + "1", + "true", + "0", + "false", + "false", + "false", + "XML", + null, + null, + null, + null, + null, + null)); MatcherAssert.assertThat(results, is(matchers)); } } @@ -811,36 +1272,57 @@ public void testGetTypeInfoWithFiltering() throws Exception { final List> results = getResults(stream); - final List> matchers = ImmutableList.of( - asList("BIGINT", "-5", "19", null, null, emptyList().toString(), "1", "false", "2", "false", "false", "true", - "BIGINT", "0", "0", - null, null, "10", null)); + final List> matchers = + ImmutableList.of( + asList( + "BIGINT", + "-5", + "19", + null, + null, + emptyList().toString(), + "1", + "false", + "2", + "false", + "false", + "true", + "BIGINT", + "0", + "0", + null, + null, + "10", + null)); MatcherAssert.assertThat(results, is(matchers)); } } @Test public void testGetCommandCrossReference() throws Exception { - final FlightInfo flightInfo = sqlClient.getCrossReference(TableRef.of(null, null, - "FOREIGNTABLE"), TableRef.of(null, null, "INTTABLE")); - try (final FlightStream stream = sqlClient.getStream(flightInfo.getEndpoints().get(0).getTicket())) { + final FlightInfo flightInfo = + sqlClient.getCrossReference( + TableRef.of(null, null, "FOREIGNTABLE"), TableRef.of(null, null, "INTTABLE")); + try (final FlightStream stream = + sqlClient.getStream(flightInfo.getEndpoints().get(0).getTicket())) { final List> results = getResults(stream); - final List> matchers = asList( - nullValue(String.class), // pk_catalog_name - is("APP"), // pk_schema_name - is("FOREIGNTABLE"), // pk_table_name - is("ID"), // pk_column_name - nullValue(String.class), // fk_catalog_name - is("APP"), // fk_schema_name - is("INTTABLE"), // fk_table_name - is("FOREIGNID"), // fk_column_name - is("1"), // key_sequence - containsString("SQL"), // fk_key_name - containsString("SQL"), // pk_key_name - is("3"), // update_rule - is("3")); // delete_rule + final List> matchers = + asList( + nullValue(String.class), // pk_catalog_name + is("APP"), // pk_schema_name + is("FOREIGNTABLE"), // pk_table_name + is("ID"), // pk_column_name + nullValue(String.class), // fk_catalog_name + is("APP"), // fk_schema_name + is("INTTABLE"), // fk_table_name + is("FOREIGNID"), // fk_column_name + is("1"), // key_sequence + containsString("SQL"), // fk_key_name + containsString("SQL"), // pk_key_name + is("3"), // update_rule + is("3")); // delete_rule Assertions.assertEquals(1, results.size()); final List assertions = new ArrayList<>(); @@ -868,16 +1350,17 @@ public void testCreateStatementSchema() throws Exception { @Test public void testCreateStatementResults() throws Exception { - try (final FlightStream stream = sqlClient - .getStream(sqlClient.execute("SELECT * FROM intTable").getEndpoints().get(0).getTicket())) { + try (final FlightStream stream = + sqlClient.getStream( + sqlClient.execute("SELECT * FROM intTable").getEndpoints().get(0).getTicket())) { Assertions.assertAll( () -> { MatcherAssert.assertThat(stream.getSchema(), is(SCHEMA_INT_TABLE)); }, () -> { - MatcherAssert.assertThat(getResults(stream), is(EXPECTED_RESULTS_FOR_STAR_SELECT_QUERY)); - } - ); + MatcherAssert.assertThat( + getResults(stream), is(EXPECTED_RESULTS_FOR_STAR_SELECT_QUERY)); + }); } } @@ -885,36 +1368,38 @@ public void testCreateStatementResults() throws Exception { public void testExecuteUpdate() { Assertions.assertAll( () -> { - long insertedCount = sqlClient.executeUpdate("INSERT INTO INTTABLE (keyName, value) VALUES " + - "('KEYNAME1', 1001), ('KEYNAME2', 1002), ('KEYNAME3', 1003)"); + long insertedCount = + sqlClient.executeUpdate( + "INSERT INTO INTTABLE (keyName, value) VALUES " + + "('KEYNAME1', 1001), ('KEYNAME2', 1002), ('KEYNAME3', 1003)"); MatcherAssert.assertThat(insertedCount, is(3L)); - }, () -> { - long updatedCount = sqlClient.executeUpdate("UPDATE INTTABLE SET keyName = 'KEYNAME1' " + - "WHERE keyName = 'KEYNAME2' OR keyName = 'KEYNAME3'"); + long updatedCount = + sqlClient.executeUpdate( + "UPDATE INTTABLE SET keyName = 'KEYNAME1' " + + "WHERE keyName = 'KEYNAME2' OR keyName = 'KEYNAME3'"); MatcherAssert.assertThat(updatedCount, is(2L)); - }, () -> { - long deletedCount = sqlClient.executeUpdate("DELETE FROM INTTABLE WHERE keyName = 'KEYNAME1'"); + long deletedCount = + sqlClient.executeUpdate("DELETE FROM INTTABLE WHERE keyName = 'KEYNAME1'"); MatcherAssert.assertThat(deletedCount, is(3L)); - } - ); + }); } @Test public void testQueryWithNoResultsShouldNotHang() throws Exception { - try (final PreparedStatement preparedStatement = sqlClient.prepare("SELECT * FROM intTable WHERE 1 = 0"); - final FlightStream stream = sqlClient - .getStream(preparedStatement.execute().getEndpoints().get(0).getTicket())) { + try (final PreparedStatement preparedStatement = + sqlClient.prepare("SELECT * FROM intTable WHERE 1 = 0"); + final FlightStream stream = + sqlClient.getStream(preparedStatement.execute().getEndpoints().get(0).getTicket())) { Assertions.assertAll( () -> MatcherAssert.assertThat(stream.getSchema(), is(SCHEMA_INT_TABLE)), () -> { final List> result = getResults(stream); MatcherAssert.assertThat(result, is(emptyList())); - } - ); + }); } } @@ -922,22 +1407,28 @@ public void testQueryWithNoResultsShouldNotHang() throws Exception { public void testCancelFlightInfo() { FlightInfo info = sqlClient.getSqlInfo(); CancelFlightInfoRequest request = new CancelFlightInfoRequest(info); - FlightRuntimeException fre = assertThrows(FlightRuntimeException.class, () -> sqlClient.cancelFlightInfo(request)); + FlightRuntimeException fre = + assertThrows(FlightRuntimeException.class, () -> sqlClient.cancelFlightInfo(request)); Assertions.assertEquals(FlightStatusCode.UNIMPLEMENTED, fre.status().code()); } @Test public void testCancelQuery() { FlightInfo info = sqlClient.getSqlInfo(); - FlightRuntimeException fre = assertThrows(FlightRuntimeException.class, () -> sqlClient.cancelQuery(info)); + FlightRuntimeException fre = + assertThrows(FlightRuntimeException.class, () -> sqlClient.cancelQuery(info)); assertEquals(FlightStatusCode.UNIMPLEMENTED, fre.status().code()); } @Test public void testRenewEndpoint() { FlightInfo info = sqlClient.getSqlInfo(); - FlightRuntimeException fre = assertThrows(FlightRuntimeException.class, - () -> sqlClient.renewFlightEndpoint(new RenewFlightEndpointRequest(info.getEndpoints().get(0)))); + FlightRuntimeException fre = + assertThrows( + FlightRuntimeException.class, + () -> + sqlClient.renewFlightEndpoint( + new RenewFlightEndpointRequest(info.getEndpoints().get(0)))); assertEquals(FlightStatusCode.UNIMPLEMENTED, fre.status().code()); } } diff --git a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSqlStreams.java b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSqlStreams.java index 1dd96f0fd4e..4fae0d1ab79 100644 --- a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSqlStreams.java +++ b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/test/TestFlightSqlStreams.java @@ -25,9 +25,11 @@ import static org.apache.arrow.vector.types.Types.MinorType.INT; import static org.hamcrest.CoreMatchers.is; +import com.google.common.collect.ImmutableList; +import com.google.protobuf.Any; +import com.google.protobuf.Message; import java.util.Collections; import java.util.List; - import org.apache.arrow.flight.CallStatus; import org.apache.arrow.flight.FlightClient; import org.apache.arrow.flight.FlightDescriptor; @@ -57,23 +59,21 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import com.google.common.collect.ImmutableList; -import com.google.protobuf.Any; -import com.google.protobuf.Message; - public class TestFlightSqlStreams { /** - * A limited {@link FlightSqlProducer} for testing GetTables, GetTableTypes, GetSqlInfo, and limited SQL commands. + * A limited {@link FlightSqlProducer} for testing GetTables, GetTableTypes, GetSqlInfo, and + * limited SQL commands. */ private static class FlightSqlTestProducer extends BasicFlightSqlProducer { - // Note that for simplicity the getStream* implementations are blocking, but a proper FlightSqlProducer should + // Note that for simplicity the getStream* implementations are blocking, but a proper + // FlightSqlProducer should // have non-blocking implementations of getStream*. private static final String FIXED_QUERY = "SELECT 1 AS c1 FROM test_table"; - private static final Schema FIXED_SCHEMA = new Schema(asList( - Field.nullable("c1", Types.MinorType.INT.getType()))); + private static final Schema FIXED_SCHEMA = + new Schema(asList(Field.nullable("c1", Types.MinorType.INT.getType()))); private BufferAllocator allocator; @@ -82,39 +82,45 @@ private static class FlightSqlTestProducer extends BasicFlightSqlProducer { } @Override - protected List determineEndpoints(T request, FlightDescriptor flightDescriptor, - Schema schema) { - if (request instanceof FlightSql.CommandGetTables || - request instanceof FlightSql.CommandGetTableTypes || - request instanceof FlightSql.CommandGetXdbcTypeInfo || - request instanceof FlightSql.CommandGetSqlInfo) { - return Collections.singletonList(new FlightEndpoint(new Ticket(Any.pack(request).toByteArray()))); - } else if (request instanceof FlightSql.CommandStatementQuery && - ((FlightSql.CommandStatementQuery) request).getQuery().equals(FIXED_QUERY)) { - - // Tickets from CommandStatementQuery requests should be built using TicketStatementQuery then packed() into - // a ticket. The content of the statement handle is specific to the FlightSqlProducer. It does not need to + protected List determineEndpoints( + T request, FlightDescriptor flightDescriptor, Schema schema) { + if (request instanceof FlightSql.CommandGetTables + || request instanceof FlightSql.CommandGetTableTypes + || request instanceof FlightSql.CommandGetXdbcTypeInfo + || request instanceof FlightSql.CommandGetSqlInfo) { + return Collections.singletonList( + new FlightEndpoint(new Ticket(Any.pack(request).toByteArray()))); + } else if (request instanceof FlightSql.CommandStatementQuery + && ((FlightSql.CommandStatementQuery) request).getQuery().equals(FIXED_QUERY)) { + + // Tickets from CommandStatementQuery requests should be built using TicketStatementQuery + // then packed() into + // a ticket. The content of the statement handle is specific to the FlightSqlProducer. It + // does not need to // be the query. It can be a query ID for example. - FlightSql.TicketStatementQuery ticketStatementQuery = FlightSql.TicketStatementQuery.newBuilder() - .setStatementHandle(((FlightSql.CommandStatementQuery) request).getQueryBytes()) - .build(); - return Collections.singletonList(new FlightEndpoint(new Ticket(Any.pack(ticketStatementQuery).toByteArray()))); + FlightSql.TicketStatementQuery ticketStatementQuery = + FlightSql.TicketStatementQuery.newBuilder() + .setStatementHandle(((FlightSql.CommandStatementQuery) request).getQueryBytes()) + .build(); + return Collections.singletonList( + new FlightEndpoint(new Ticket(Any.pack(ticketStatementQuery).toByteArray()))); } throw CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException(); } @Override - public FlightInfo getFlightInfoStatement(FlightSql.CommandStatementQuery command, - CallContext context, FlightDescriptor descriptor) { + public FlightInfo getFlightInfoStatement( + FlightSql.CommandStatementQuery command, CallContext context, FlightDescriptor descriptor) { return generateFlightInfo(command, descriptor, FIXED_SCHEMA); } @Override - public void getStreamStatement(FlightSql.TicketStatementQuery ticket, - CallContext context, ServerStreamListener listener) { + public void getStreamStatement( + FlightSql.TicketStatementQuery ticket, CallContext context, ServerStreamListener listener) { final String query = ticket.getStatementHandle().toStringUtf8(); if (!query.equals(FIXED_QUERY)) { - listener.error(CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); + listener.error( + CallStatus.UNIMPLEMENTED.withDescription("Not implemented.").toRuntimeException()); } try (VectorSchemaRoot root = VectorSchemaRoot.create(FIXED_SCHEMA, allocator)) { @@ -127,9 +133,10 @@ public void getStreamStatement(FlightSql.TicketStatementQuery ticket, } @Override - public void getStreamSqlInfo(FlightSql.CommandGetSqlInfo command, CallContext context, - ServerStreamListener listener) { - try (VectorSchemaRoot root = VectorSchemaRoot.create(Schemas.GET_SQL_INFO_SCHEMA, allocator)) { + public void getStreamSqlInfo( + FlightSql.CommandGetSqlInfo command, CallContext context, ServerStreamListener listener) { + try (VectorSchemaRoot root = + VectorSchemaRoot.create(Schemas.GET_SQL_INFO_SCHEMA, allocator)) { root.setRowCount(0); listener.start(root); listener.putNext(); @@ -138,9 +145,12 @@ public void getStreamSqlInfo(FlightSql.CommandGetSqlInfo command, CallContext co } @Override - public void getStreamTypeInfo(FlightSql.CommandGetXdbcTypeInfo request, - CallContext context, ServerStreamListener listener) { - try (VectorSchemaRoot root = VectorSchemaRoot.create(Schemas.GET_TYPE_INFO_SCHEMA, allocator)) { + public void getStreamTypeInfo( + FlightSql.CommandGetXdbcTypeInfo request, + CallContext context, + ServerStreamListener listener) { + try (VectorSchemaRoot root = + VectorSchemaRoot.create(Schemas.GET_TYPE_INFO_SCHEMA, allocator)) { root.setRowCount(1); ((VarCharVector) root.getVector("type_name")).setSafe(0, new Text("Integer")); ((IntVector) root.getVector("data_type")).setSafe(0, INT.ordinal()); @@ -148,9 +158,11 @@ public void getStreamTypeInfo(FlightSql.CommandGetXdbcTypeInfo request, root.getVector("literal_prefix").setNull(0); root.getVector("literal_suffix").setNull(0); root.getVector("create_params").setNull(0); - ((IntVector) root.getVector("nullable")).setSafe(0, FlightSql.Nullable.NULLABILITY_NULLABLE.getNumber()); + ((IntVector) root.getVector("nullable")) + .setSafe(0, FlightSql.Nullable.NULLABILITY_NULLABLE.getNumber()); ((BitVector) root.getVector("case_sensitive")).setSafe(0, 1); - ((IntVector) root.getVector("nullable")).setSafe(0, FlightSql.Searchable.SEARCHABLE_FULL.getNumber()); + ((IntVector) root.getVector("nullable")) + .setSafe(0, FlightSql.Searchable.SEARCHABLE_FULL.getNumber()); ((BitVector) root.getVector("unsigned_attribute")).setSafe(0, 1); root.getVector("fixed_prec_scale").setNull(0); ((BitVector) root.getVector("auto_increment")).setSafe(0, 1); @@ -169,9 +181,10 @@ public void getStreamTypeInfo(FlightSql.CommandGetXdbcTypeInfo request, } @Override - public void getStreamTables(FlightSql.CommandGetTables command, CallContext context, - ServerStreamListener listener) { - try (VectorSchemaRoot root = VectorSchemaRoot.create(Schemas.GET_TABLES_SCHEMA_NO_SCHEMA, allocator)) { + public void getStreamTables( + FlightSql.CommandGetTables command, CallContext context, ServerStreamListener listener) { + try (VectorSchemaRoot root = + VectorSchemaRoot.create(Schemas.GET_TABLES_SCHEMA_NO_SCHEMA, allocator)) { root.setRowCount(1); root.getVector("catalog_name").setNull(0); root.getVector("db_schema_name").setNull(0); @@ -186,7 +199,8 @@ public void getStreamTables(FlightSql.CommandGetTables command, CallContext cont @Override public void getStreamTableTypes(CallContext context, ServerStreamListener listener) { - try (VectorSchemaRoot root = VectorSchemaRoot.create(Schemas.GET_TABLE_TYPES_SCHEMA, allocator)) { + try (VectorSchemaRoot root = + VectorSchemaRoot.create(Schemas.GET_TABLE_TYPES_SCHEMA, allocator)) { root.setRowCount(1); ((VarCharVector) root.getVector("table_type")).setSafe(0, new Text("TABLE")); @@ -207,9 +221,10 @@ public static void setUp() throws Exception { allocator = new RootAllocator(Integer.MAX_VALUE); final Location serverLocation = Location.forGrpcInsecure("localhost", 0); - server = FlightServer.builder(allocator, serverLocation, new FlightSqlTestProducer(allocator)) - .build() - .start(); + server = + FlightServer.builder(allocator, serverLocation, new FlightSqlTestProducer(allocator)) + .build() + .start(); final Location clientLocation = Location.forGrpcInsecure("localhost", server.getPort()); sqlClient = new FlightSqlClient(FlightClient.builder(allocator, clientLocation).build()); @@ -227,37 +242,39 @@ public static void tearDown() throws Exception { @Test public void testGetTablesResultNoSchema() throws Exception { try (final FlightStream stream = - sqlClient.getStream( - sqlClient.getTables(null, null, null, null, false) - .getEndpoints().get(0).getTicket())) { + sqlClient.getStream( + sqlClient.getTables(null, null, null, null, false).getEndpoints().get(0).getTicket())) { Assertions.assertAll( - () -> MatcherAssert.assertThat(stream.getSchema(), is(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA_NO_SCHEMA)), + () -> + MatcherAssert.assertThat( + stream.getSchema(), is(FlightSqlProducer.Schemas.GET_TABLES_SCHEMA_NO_SCHEMA)), () -> { final List> results = getResults(stream); - final List> expectedResults = ImmutableList.of( - // catalog_name | schema_name | table_name | table_type | table_schema - asList(null, null, "test_table", "TABLE")); + final List> expectedResults = + ImmutableList.of( + // catalog_name | schema_name | table_name | table_type | table_schema + asList(null, null, "test_table", "TABLE")); MatcherAssert.assertThat(results, is(expectedResults)); - } - ); + }); } } @Test public void testGetTableTypesResult() throws Exception { try (final FlightStream stream = - sqlClient.getStream(sqlClient.getTableTypes().getEndpoints().get(0).getTicket())) { + sqlClient.getStream(sqlClient.getTableTypes().getEndpoints().get(0).getTicket())) { Assertions.assertAll( - () -> MatcherAssert.assertThat(stream.getSchema(), is(FlightSqlProducer.Schemas.GET_TABLE_TYPES_SCHEMA)), + () -> + MatcherAssert.assertThat( + stream.getSchema(), is(FlightSqlProducer.Schemas.GET_TABLE_TYPES_SCHEMA)), () -> { final List> tableTypes = getResults(stream); - final List> expectedTableTypes = ImmutableList.of( - // table_type - singletonList("TABLE") - ); + final List> expectedTableTypes = + ImmutableList.of( + // table_type + singletonList("TABLE")); MatcherAssert.assertThat(tableTypes, is(expectedTableTypes)); - } - ); + }); } } @@ -266,9 +283,10 @@ public void testGetSqlInfoResults() throws Exception { final FlightInfo info = sqlClient.getSqlInfo(); try (final FlightStream stream = sqlClient.getStream(info.getEndpoints().get(0).getTicket())) { Assertions.assertAll( - () -> MatcherAssert.assertThat(stream.getSchema(), is(FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA)), - () -> MatcherAssert.assertThat(getResults(stream), is(emptyList())) - ); + () -> + MatcherAssert.assertThat( + stream.getSchema(), is(FlightSqlProducer.Schemas.GET_SQL_INFO_SCHEMA)), + () -> MatcherAssert.assertThat(getResults(stream), is(emptyList()))); } } @@ -280,9 +298,11 @@ public void testGetTypeInfo() throws Exception { final List> results = getResults(stream); - final List> matchers = ImmutableList.of( - asList("Integer", "4", "400", null, null, "3", "true", null, "true", null, "true", - "Integer", null, null, "4", null, "10", null)); + final List> matchers = + ImmutableList.of( + asList( + "Integer", "4", "400", null, null, "3", "true", null, "true", null, "true", + "Integer", null, null, "4", null, "10", null)); MatcherAssert.assertThat(results, is(matchers)); } @@ -290,12 +310,18 @@ public void testGetTypeInfo() throws Exception { @Test public void testExecuteQuery() throws Exception { - try (final FlightStream stream = sqlClient - .getStream(sqlClient.execute(FlightSqlTestProducer.FIXED_QUERY).getEndpoints().get(0).getTicket())) { + try (final FlightStream stream = + sqlClient.getStream( + sqlClient + .execute(FlightSqlTestProducer.FIXED_QUERY) + .getEndpoints() + .get(0) + .getTicket())) { Assertions.assertAll( - () -> MatcherAssert.assertThat(stream.getSchema(), is(FlightSqlTestProducer.FIXED_SCHEMA)), - () -> MatcherAssert.assertThat(getResults(stream), is(singletonList(singletonList("1")))) - ); + () -> + MatcherAssert.assertThat(stream.getSchema(), is(FlightSqlTestProducer.FIXED_SCHEMA)), + () -> + MatcherAssert.assertThat(getResults(stream), is(singletonList(singletonList("1"))))); } } } diff --git a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/AdhocTestOption.java b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/AdhocTestOption.java index 6988a86049d..af4b88223dc 100644 --- a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/AdhocTestOption.java +++ b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/AdhocTestOption.java @@ -22,7 +22,9 @@ import com.google.protobuf.ProtocolMessageEnum; enum AdhocTestOption implements ProtocolMessageEnum { - OPTION_A, OPTION_B, OPTION_C; + OPTION_A, + OPTION_B, + OPTION_C; @Override public int getNumber() { @@ -40,6 +42,7 @@ public EnumDescriptor getDescriptorForType() { } private UnsupportedOperationException getUnsupportedException() { - return new UnsupportedOperationException("Unimplemented method is irrelevant for the scope of this test."); + return new UnsupportedOperationException( + "Unimplemented method is irrelevant for the scope of this test."); } } diff --git a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/FlightStreamUtils.java b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/FlightStreamUtils.java index fbbe9ef0181..7eecd4fb2ff 100644 --- a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/FlightStreamUtils.java +++ b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/FlightStreamUtils.java @@ -25,7 +25,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; - import org.apache.arrow.flight.FlightStream; import org.apache.arrow.vector.BitVector; import org.apache.arrow.vector.FieldVector; @@ -52,75 +51,82 @@ public static List> getResults(FlightStream stream) { results.add(new ArrayList<>()); } - root.getSchema().getFields().forEach(field -> { - try (final FieldVector fieldVector = root.getVector(field.getName())) { - if (fieldVector instanceof VarCharVector) { - final VarCharVector varcharVector = (VarCharVector) fieldVector; - for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { - final Text data = varcharVector.getObject(rowIndex); - results.get(rowIndex).add(isNull(data) ? null : data.toString()); - } - } else if (fieldVector instanceof IntVector) { - for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { - Object data = fieldVector.getObject(rowIndex); - results.get(rowIndex).add(isNull(data) ? null : Objects.toString(data)); - } - } else if (fieldVector instanceof VarBinaryVector) { - final VarBinaryVector varbinaryVector = (VarBinaryVector) fieldVector; - for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { - final byte[] data = varbinaryVector.getObject(rowIndex); - final String output; - try { - output = isNull(data) ? - null : - MessageSerializer.deserializeSchema( - new ReadChannel(Channels.newChannel(new ByteArrayInputStream(data)))).toJson(); - } catch (final IOException e) { - throw new RuntimeException("Failed to deserialize schema", e); - } - results.get(rowIndex).add(output); - } - } else if (fieldVector instanceof DenseUnionVector) { - final DenseUnionVector denseUnionVector = (DenseUnionVector) fieldVector; - for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { - final Object data = denseUnionVector.getObject(rowIndex); - results.get(rowIndex).add(isNull(data) ? null : Objects.toString(data)); - } - } else if (fieldVector instanceof ListVector) { - for (int i = 0; i < fieldVector.getValueCount(); i++) { - if (!fieldVector.isNull(i)) { - List elements = (List) ((ListVector) fieldVector).getObject(i); - List values = new ArrayList<>(); + root.getSchema() + .getFields() + .forEach( + field -> { + try (final FieldVector fieldVector = root.getVector(field.getName())) { + if (fieldVector instanceof VarCharVector) { + final VarCharVector varcharVector = (VarCharVector) fieldVector; + for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { + final Text data = varcharVector.getObject(rowIndex); + results.get(rowIndex).add(isNull(data) ? null : data.toString()); + } + } else if (fieldVector instanceof IntVector) { + for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { + Object data = fieldVector.getObject(rowIndex); + results.get(rowIndex).add(isNull(data) ? null : Objects.toString(data)); + } + } else if (fieldVector instanceof VarBinaryVector) { + final VarBinaryVector varbinaryVector = (VarBinaryVector) fieldVector; + for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { + final byte[] data = varbinaryVector.getObject(rowIndex); + final String output; + try { + output = + isNull(data) + ? null + : MessageSerializer.deserializeSchema( + new ReadChannel( + Channels.newChannel(new ByteArrayInputStream(data)))) + .toJson(); + } catch (final IOException e) { + throw new RuntimeException("Failed to deserialize schema", e); + } + results.get(rowIndex).add(output); + } + } else if (fieldVector instanceof DenseUnionVector) { + final DenseUnionVector denseUnionVector = (DenseUnionVector) fieldVector; + for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { + final Object data = denseUnionVector.getObject(rowIndex); + results.get(rowIndex).add(isNull(data) ? null : Objects.toString(data)); + } + } else if (fieldVector instanceof ListVector) { + for (int i = 0; i < fieldVector.getValueCount(); i++) { + if (!fieldVector.isNull(i)) { + List elements = + (List) ((ListVector) fieldVector).getObject(i); + List values = new ArrayList<>(); - for (Text element : elements) { - values.add(element.toString()); - } - results.get(i).add(values.toString()); - } - } + for (Text element : elements) { + values.add(element.toString()); + } + results.get(i).add(values.toString()); + } + } - } else if (fieldVector instanceof UInt4Vector) { - final UInt4Vector uInt4Vector = (UInt4Vector) fieldVector; - for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { - final Object data = uInt4Vector.getObject(rowIndex); - results.get(rowIndex).add(isNull(data) ? null : Objects.toString(data)); - } - } else if (fieldVector instanceof UInt1Vector) { - final UInt1Vector uInt1Vector = (UInt1Vector) fieldVector; - for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { - final Object data = uInt1Vector.getObject(rowIndex); - results.get(rowIndex).add(isNull(data) ? null : Objects.toString(data)); - } - } else if (fieldVector instanceof BitVector) { - for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { - Object data = fieldVector.getObject(rowIndex); - results.get(rowIndex).add(isNull(data) ? null : Objects.toString(data)); - } - } else { - throw new UnsupportedOperationException("Not yet implemented"); - } - } - }); + } else if (fieldVector instanceof UInt4Vector) { + final UInt4Vector uInt4Vector = (UInt4Vector) fieldVector; + for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { + final Object data = uInt4Vector.getObject(rowIndex); + results.get(rowIndex).add(isNull(data) ? null : Objects.toString(data)); + } + } else if (fieldVector instanceof UInt1Vector) { + final UInt1Vector uInt1Vector = (UInt1Vector) fieldVector; + for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { + final Object data = uInt1Vector.getObject(rowIndex); + results.get(rowIndex).add(isNull(data) ? null : Objects.toString(data)); + } + } else if (fieldVector instanceof BitVector) { + for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { + Object data = fieldVector.getObject(rowIndex); + results.get(rowIndex).add(isNull(data) ? null : Objects.toString(data)); + } + } else { + throw new UnsupportedOperationException("Not yet implemented"); + } + } + }); } } diff --git a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/SqlInfoOptionsUtilsBitmaskCreationTest.java b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/SqlInfoOptionsUtilsBitmaskCreationTest.java index dfb1b9da3e2..c3d4c5606a8 100644 --- a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/SqlInfoOptionsUtilsBitmaskCreationTest.java +++ b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/SqlInfoOptionsUtilsBitmaskCreationTest.java @@ -24,7 +24,6 @@ import static org.apache.arrow.flight.sql.util.SqlInfoOptionsUtils.createBitmaskFromEnums; import java.util.List; - import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -33,23 +32,22 @@ public final class SqlInfoOptionsUtilsBitmaskCreationTest { public static List provideParameters() { return asList( - new Object[][]{ - {new AdhocTestOption[0], 0L}, - {new AdhocTestOption[]{OPTION_A}, 1L}, - {new AdhocTestOption[]{OPTION_B}, 0b10L}, - {new AdhocTestOption[]{OPTION_A, OPTION_B}, 0b11L}, - {new AdhocTestOption[]{OPTION_C}, 0b100L}, - {new AdhocTestOption[]{OPTION_A, OPTION_C}, 0b101L}, - {new AdhocTestOption[]{OPTION_B, OPTION_C}, 0b110L}, - {AdhocTestOption.values(), 0b111L}, + new Object[][] { + {new AdhocTestOption[0], 0L}, + {new AdhocTestOption[] {OPTION_A}, 1L}, + {new AdhocTestOption[] {OPTION_B}, 0b10L}, + {new AdhocTestOption[] {OPTION_A, OPTION_B}, 0b11L}, + {new AdhocTestOption[] {OPTION_C}, 0b100L}, + {new AdhocTestOption[] {OPTION_A, OPTION_C}, 0b101L}, + {new AdhocTestOption[] {OPTION_B, OPTION_C}, 0b110L}, + {AdhocTestOption.values(), 0b111L}, }); } @ParameterizedTest @MethodSource("provideParameters") public void testShouldBuildBitmaskFromEnums( - AdhocTestOption[] adhocTestOptions, long expectedBitmask - ) { + AdhocTestOption[] adhocTestOptions, long expectedBitmask) { Assertions.assertEquals(createBitmaskFromEnums(adhocTestOptions), expectedBitmask); } } diff --git a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/SqlInfoOptionsUtilsBitmaskParsingTest.java b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/SqlInfoOptionsUtilsBitmaskParsingTest.java index 818326a582d..4baabf1da22 100644 --- a/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/SqlInfoOptionsUtilsBitmaskParsingTest.java +++ b/java/flight/flight-sql/src/test/java/org/apache/arrow/flight/sql/util/SqlInfoOptionsUtilsBitmaskParsingTest.java @@ -28,7 +28,6 @@ import java.util.EnumSet; import java.util.List; import java.util.Set; - import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -37,21 +36,22 @@ public final class SqlInfoOptionsUtilsBitmaskParsingTest { public static List provideParameters() { return asList( - new Object[][]{ - {0L, EnumSet.noneOf(AdhocTestOption.class)}, - {1L, EnumSet.of(OPTION_A)}, - {0b10L, EnumSet.of(OPTION_B)}, - {0b11L, EnumSet.of(OPTION_A, OPTION_B)}, - {0b100L, EnumSet.of(OPTION_C)}, - {0b101L, EnumSet.of(OPTION_A, OPTION_C)}, - {0b110L, EnumSet.of(OPTION_B, OPTION_C)}, - {0b111L, EnumSet.allOf(AdhocTestOption.class)}, + new Object[][] { + {0L, EnumSet.noneOf(AdhocTestOption.class)}, + {1L, EnumSet.of(OPTION_A)}, + {0b10L, EnumSet.of(OPTION_B)}, + {0b11L, EnumSet.of(OPTION_A, OPTION_B)}, + {0b100L, EnumSet.of(OPTION_C)}, + {0b101L, EnumSet.of(OPTION_A, OPTION_C)}, + {0b110L, EnumSet.of(OPTION_B, OPTION_C)}, + {0b111L, EnumSet.allOf(AdhocTestOption.class)}, }); } @ParameterizedTest @MethodSource("provideParameters") - public void testShouldFilterOutEnumsBasedOnBitmask(long bitmask, Set expectedOptions) { + public void testShouldFilterOutEnumsBasedOnBitmask( + long bitmask, Set expectedOptions) { final Set actualOptions = stream(AdhocTestOption.values()) .filter(enumInstance -> doesBitmaskTranslateToEnum(enumInstance, bitmask)) diff --git a/java/flight/flight-sql/src/test/resources/logback.xml b/java/flight/flight-sql/src/test/resources/logback.xml index 4c54d18a210..9fa3f1e3b43 100644 --- a/java/flight/flight-sql/src/test/resources/logback.xml +++ b/java/flight/flight-sql/src/test/resources/logback.xml @@ -1,4 +1,4 @@ - + - diff --git a/java/flight/pom.xml b/java/flight/pom.xml index 2f777ab42b7..b7afab9a79d 100644 --- a/java/flight/pom.xml +++ b/java/flight/pom.xml @@ -9,59 +9,54 @@ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> - - - arrow-java-root - org.apache.arrow - 16.0.0-SNAPSHOT - - 4.0.0 + + 4.0.0 + + org.apache.arrow + arrow-java-root + 16.0.0-SNAPSHOT + + arrow-flight - Arrow Flight - arrow-flight + pom - pom + Arrow Flight - - flight-core - flight-sql - flight-sql-jdbc-core - flight-sql-jdbc-driver - flight-integration-tests - + + flight-core + flight-sql + flight-sql-jdbc-core + flight-sql-jdbc-driver + flight-integration-tests + - - - - - org.xolstice.maven.plugins - protobuf-maven-plugin - 0.6.1 - - - com.google.protobuf:protoc:${dep.protobuf-bom.version}:exe:${os.detected.classifier} - - grpc-java - io.grpc:protoc-gen-grpc-java:${dep.grpc-bom.version}:exe:${os.detected.classifier} - - - - - - + + + + + org.xolstice.maven.plugins + protobuf-maven-plugin + 0.6.1 + + com.google.protobuf:protoc:${dep.protobuf-bom.version}:exe:${os.detected.classifier} + grpc-java + io.grpc:protoc-gen-grpc-java:${dep.grpc-bom.version}:exe:${os.detected.classifier} + + + + + - - - pin-mockito-jdk8 - - 1.8 - - - 4.11.0 - 5.2.0 - - - + + + pin-mockito-jdk8 + + 1.8 + + + 4.11.0 + 5.2.0 + + + diff --git a/java/format/pom.xml b/java/format/pom.xml index a98edefbeb2..5f089e676ee 100644 --- a/java/format/pom.xml +++ b/java/format/pom.xml @@ -1,4 +1,4 @@ - + -4.0.0 + 4.0.0 - - arrow-java-root - org.apache.arrow - 16.0.0-SNAPSHOT - + + org.apache.arrow + arrow-java-root + 16.0.0-SNAPSHOT + -arrow-format -jar -Arrow Format -Generated Java files from the IPC Flatbuffer definitions. + arrow-format + jar + Arrow Format + Generated Java files from the IPC Flatbuffer definitions. @@ -30,17 +30,9 @@ - + - - - org.apache.maven.plugins - maven-checkstyle-plugin - - true - - - + - + diff --git a/java/format/src/main/java/module-info.java b/java/format/src/main/java/module-info.java index bda779c91af..df490b27fb2 100644 --- a/java/format/src/main/java/module-info.java +++ b/java/format/src/main/java/module-info.java @@ -17,5 +17,6 @@ module org.apache.arrow.format { exports org.apache.arrow.flatbuf; + requires transitive flatbuffers.java; } diff --git a/java/maven/module-info-compiler-maven-plugin/pom.xml b/java/maven/module-info-compiler-maven-plugin/pom.xml index 37d14ad412d..8509aa8a501 100644 --- a/java/maven/module-info-compiler-maven-plugin/pom.xml +++ b/java/maven/module-info-compiler-maven-plugin/pom.xml @@ -9,9 +9,7 @@ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> - + 4.0.0 org.apache.arrow.maven.plugins @@ -72,7 +70,8 @@ - + + maven-clean-plugin diff --git a/java/maven/module-info-compiler-maven-plugin/src/main/java/org/apache/arrow/maven/plugins/BaseModuleInfoCompilerPlugin.java b/java/maven/module-info-compiler-maven-plugin/src/main/java/org/apache/arrow/maven/plugins/BaseModuleInfoCompilerPlugin.java index 37cbf5d7e77..a9df63bc0b4 100644 --- a/java/maven/module-info-compiler-maven-plugin/src/main/java/org/apache/arrow/maven/plugins/BaseModuleInfoCompilerPlugin.java +++ b/java/maven/module-info-compiler-maven-plugin/src/main/java/org/apache/arrow/maven/plugins/BaseModuleInfoCompilerPlugin.java @@ -28,14 +28,11 @@ import java.nio.file.Paths; import java.util.List; import java.util.Optional; - import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.glavo.mic.ModuleInfoCompiler; -/** - * Compiles the first module-info.java file in the project purely syntactically. - */ +/** Compiles the first module-info.java file in the project purely syntactically. */ public abstract class BaseModuleInfoCompilerPlugin extends AbstractMojo { protected abstract List getSourceRoots(); @@ -60,9 +57,10 @@ public void execute() throws MojoExecutionException { // Invoke the compiler, ModuleInfoCompiler compiler = new ModuleInfoCompiler(); - try (Reader reader = new InputStreamReader(Files.newInputStream(moduleInfoFile.get().toPath()), - StandardCharsets.UTF_8); - OutputStream output = Files.newOutputStream(targetPath)) { + try (Reader reader = + new InputStreamReader( + Files.newInputStream(moduleInfoFile.get().toPath()), StandardCharsets.UTF_8); + OutputStream output = Files.newOutputStream(targetPath)) { compiler.compile(reader, output); getLog().info("Successfully wrote module-info.class file."); } catch (IOException ex) { @@ -73,18 +71,17 @@ public void execute() throws MojoExecutionException { } } - /** - * Finds the first module-info.java file in the set of source directories. - */ + /** Finds the first module-info.java file in the set of source directories. */ private Optional findFirstModuleInfo(List sourceDirectories) { if (sourceDirectories == null) { return Optional.empty(); } - return sourceDirectories.stream().map(Paths::get) - .map(sourcePath -> - sourcePath.toFile().listFiles(file -> - file.getName().equals("module-info.java"))) + return sourceDirectories.stream() + .map(Paths::get) + .map( + sourcePath -> + sourcePath.toFile().listFiles(file -> file.getName().equals("module-info.java"))) .filter(matchingFiles -> matchingFiles != null && matchingFiles.length != 0) .map(matchingFiles -> matchingFiles[0]) .findAny(); diff --git a/java/maven/module-info-compiler-maven-plugin/src/main/java/org/apache/arrow/maven/plugins/ModuleInfoCompilerPlugin.java b/java/maven/module-info-compiler-maven-plugin/src/main/java/org/apache/arrow/maven/plugins/ModuleInfoCompilerPlugin.java index 31df6372925..fc8b347abc1 100644 --- a/java/maven/module-info-compiler-maven-plugin/src/main/java/org/apache/arrow/maven/plugins/ModuleInfoCompilerPlugin.java +++ b/java/maven/module-info-compiler-maven-plugin/src/main/java/org/apache/arrow/maven/plugins/ModuleInfoCompilerPlugin.java @@ -19,19 +19,18 @@ import java.util.ArrayList; import java.util.List; - import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; -/** - * A maven plugin for compiler module-info files in main code with JDK8. - */ +/** A maven plugin for compiler module-info files in main code with JDK8. */ @Mojo(name = "compile", defaultPhase = LifecyclePhase.COMPILE) public class ModuleInfoCompilerPlugin extends BaseModuleInfoCompilerPlugin { - @Parameter(defaultValue = "${project.compileSourceRoots}", property = "compileSourceRoots", + @Parameter( + defaultValue = "${project.compileSourceRoots}", + property = "compileSourceRoots", required = true) private final List compileSourceRoots = new ArrayList<>(); diff --git a/java/maven/module-info-compiler-maven-plugin/src/main/java/org/apache/arrow/maven/plugins/ModuleInfoTestCompilerPlugin.java b/java/maven/module-info-compiler-maven-plugin/src/main/java/org/apache/arrow/maven/plugins/ModuleInfoTestCompilerPlugin.java index 4705506ac53..dcd6bdbb477 100644 --- a/java/maven/module-info-compiler-maven-plugin/src/main/java/org/apache/arrow/maven/plugins/ModuleInfoTestCompilerPlugin.java +++ b/java/maven/module-info-compiler-maven-plugin/src/main/java/org/apache/arrow/maven/plugins/ModuleInfoTestCompilerPlugin.java @@ -18,15 +18,12 @@ package org.apache.arrow.maven.plugins; import java.util.List; - import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; -/** - * A maven plugin for compiler module-info files in unit tests with JDK8. - */ +/** A maven plugin for compiler module-info files in unit tests with JDK8. */ @Mojo(name = "testCompile", defaultPhase = LifecyclePhase.TEST_COMPILE) public class ModuleInfoTestCompilerPlugin extends BaseModuleInfoCompilerPlugin { diff --git a/java/maven/pom.xml b/java/maven/pom.xml index 3a88ec762e1..3e903b2c9f5 100644 --- a/java/maven/pom.xml +++ b/java/maven/pom.xml @@ -1,4 +1,4 @@ - + + + + + + + src/main/**/*.xml + src/test/**/*.xml + **/pom.xml + + + ${maven.multiModuleProjectDirectory}/spotless/asf-xml.license + (<configuration|<project) + + + + + + true + 2 + + + true + 2 + + + + + ${maven.multiModuleProjectDirectory}/spotless/asf-java.license + (package|import) + + + + + true + 2 + + + true + 2 + + + false + + + + + + spotless-check + + check + + validate + + + + + + + diff --git a/java/memory/memory-core/pom.xml b/java/memory/memory-core/pom.xml index 2a92d032942..2c7212cea55 100644 --- a/java/memory/memory-core/pom.xml +++ b/java/memory/memory-core/pom.xml @@ -10,12 +10,12 @@ OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> + 4.0.0 - arrow-memory org.apache.arrow + arrow-memory 16.0.0-SNAPSHOT - 4.0.0 arrow-memory-core @@ -96,10 +96,10 @@ opens-tests - test test + test -Dfoo=bar @@ -133,12 +133,15 @@ 8 UTF-8 - -Xmaxerrs + -Xmaxerrs + 10000 -Xmaxwarns 10000 - -AskipDefs=.*Test - -AatfDoNotCache + -AskipDefs=.*Test + + -AatfDoNotCache + diff --git a/java/memory/memory-core/src/main/java/module-info.java b/java/memory/memory-core/src/main/java/module-info.java index 34ba34e80bc..71affe44175 100644 --- a/java/memory/memory-core/src/main/java/module-info.java +++ b/java/memory/memory-core/src/main/java/module-info.java @@ -21,6 +21,7 @@ exports org.apache.arrow.memory.util; exports org.apache.arrow.memory.util.hash; exports org.apache.arrow.util; + requires transitive jdk.unsupported; requires jsr305; requires org.immutables.value; diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/Accountant.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/Accountant.java index b87f1345a51..b3b218396cf 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/Accountant.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/Accountant.java @@ -18,55 +18,51 @@ package org.apache.arrow.memory; import java.util.concurrent.atomic.AtomicLong; - import javax.annotation.concurrent.ThreadSafe; - import org.apache.arrow.util.Preconditions; import org.checkerframework.checker.nullness.qual.Nullable; /** - * Provides a concurrent way to manage account for memory usage without locking. Used as basis - * for Allocators. All - * operations are threadsafe (except for close). + * Provides a concurrent way to manage account for memory usage without locking. Used as basis for + * Allocators. All operations are threadsafe (except for close). */ @ThreadSafe class Accountant implements AutoCloseable { - /** - * The parent allocator. - */ + /** The parent allocator. */ protected final @Nullable Accountant parent; private final String name; /** - * The amount of memory reserved for this allocator. Releases below this amount of memory will - * not be returned to the - * parent Accountant until this Accountant is closed. + * The amount of memory reserved for this allocator. Releases below this amount of memory will not + * be returned to the parent Accountant until this Accountant is closed. */ protected final long reservation; private final AtomicLong peakAllocation = new AtomicLong(); /** - * Maximum local memory that can be held. This can be externally updated. Changing it won't - * cause past memory to - * change but will change responses to future allocation efforts + * Maximum local memory that can be held. This can be externally updated. Changing it won't cause + * past memory to change but will change responses to future allocation efforts */ private final AtomicLong allocationLimit = new AtomicLong(); - /** - * Currently allocated amount of memory. - */ + /** Currently allocated amount of memory. */ private final AtomicLong locallyHeldMemory = new AtomicLong(); - public Accountant(@Nullable Accountant parent, String name, long reservation, long maxAllocation) { + public Accountant( + @Nullable Accountant parent, String name, long reservation, long maxAllocation) { Preconditions.checkNotNull(name, "name must not be null"); - Preconditions.checkArgument(reservation >= 0, "The initial reservation size must be non-negative."); - Preconditions.checkArgument(maxAllocation >= 0, "The maximum allocation limit must be non-negative."); - Preconditions.checkArgument(reservation <= maxAllocation, + Preconditions.checkArgument( + reservation >= 0, "The initial reservation size must be non-negative."); + Preconditions.checkArgument( + maxAllocation >= 0, "The maximum allocation limit must be non-negative."); + Preconditions.checkArgument( + reservation <= maxAllocation, "The initial reservation size must be <= the maximum allocation."); - Preconditions.checkArgument(reservation == 0 || parent != null, "The root accountant can't reserve memory."); + Preconditions.checkArgument( + reservation == 0 || parent != null, "The root accountant can't reserve memory."); this.parent = parent; this.name = name; @@ -78,9 +74,12 @@ public Accountant(@Nullable Accountant parent, String name, long reservation, lo // we will allocate a reservation from our parent. final AllocationOutcome outcome = parent.allocateBytes(reservation); if (!outcome.isOk()) { - throw new OutOfMemoryException(String.format( - "Failure trying to allocate initial reservation for Allocator. " + - "Attempted to allocate %d bytes.", reservation), outcome.getDetails()); + throw new OutOfMemoryException( + String.format( + "Failure trying to allocate initial reservation for Allocator. " + + "Attempted to allocate %d bytes.", + reservation), + outcome.getDetails()); } } } @@ -98,16 +97,18 @@ AllocationOutcome allocateBytes(long size) { return AllocationOutcome.SUCCESS_INSTANCE; } else { // Try again, but with details this time. - // Populating details only on failures avoids performance overhead in the common case (success case). + // Populating details only on failures avoids performance overhead in the common case (success + // case). AllocationOutcomeDetails details = new AllocationOutcomeDetails(); status = allocateBytesInternal(size, details); return new AllocationOutcome(status, details); } } - private AllocationOutcome.Status allocateBytesInternal(long size, @Nullable AllocationOutcomeDetails details) { - final AllocationOutcome.Status status = allocate(size, - true /*incomingUpdatePeek*/, false /*forceAllocation*/, details); + private AllocationOutcome.Status allocateBytesInternal( + long size, @Nullable AllocationOutcomeDetails details) { + final AllocationOutcome.Status status = + allocate(size, true /*incomingUpdatePeek*/, false /*forceAllocation*/, details); if (!status.isOk()) { releaseBytes(size); } @@ -135,7 +136,6 @@ private void updatePeak() { } } - /** * Increase the accounting. Returns whether the allocation fit within limits. * @@ -149,33 +149,32 @@ public boolean forceAllocate(long size) { /** * Internal method for allocation. This takes a forced approach to allocation to ensure that we - * manage reservation - * boundary issues consistently. Allocation is always done through the entire tree. The two - * options that we influence - * are whether the allocation should be forced and whether or not the peak memory allocation - * should be updated. If at - * some point during allocation escalation we determine that the allocation is no longer - * possible, we will continue to - * do a complete and consistent allocation but we will stop updating the peak allocation. We do - * this because we know - * that we will be directly unwinding this allocation (and thus never actually making the - * allocation). If force - * allocation is passed, then we continue to update the peak limits since we now know that this - * allocation will occur - * despite our moving past one or more limits. + * manage reservation boundary issues consistently. Allocation is always done through the entire + * tree. The two options that we influence are whether the allocation should be forced and whether + * or not the peak memory allocation should be updated. If at some point during allocation + * escalation we determine that the allocation is no longer possible, we will continue to do a + * complete and consistent allocation but we will stop updating the peak allocation. We do this + * because we know that we will be directly unwinding this allocation (and thus never actually + * making the allocation). If force allocation is passed, then we continue to update the peak + * limits since we now know that this allocation will occur despite our moving past one or more + * limits. * - * @param size The size of the allocation. + * @param size The size of the allocation. * @param incomingUpdatePeak Whether we should update the local peak for this allocation. - * @param forceAllocation Whether we should force the allocation. + * @param forceAllocation Whether we should force the allocation. * @return The outcome of the allocation. */ - private AllocationOutcome.Status allocate(final long size, final boolean incomingUpdatePeak, - final boolean forceAllocation, @Nullable AllocationOutcomeDetails details) { + private AllocationOutcome.Status allocate( + final long size, + final boolean incomingUpdatePeak, + final boolean forceAllocation, + @Nullable AllocationOutcomeDetails details) { final long oldLocal = locallyHeldMemory.getAndAdd(size); final long newLocal = oldLocal + size; // Borrowed from Math.addExact (but avoid exception here) // Overflow if result has opposite sign of both arguments - // No need to reset locallyHeldMemory on overflow; allocateBytesInternal will releaseBytes on failure + // No need to reset locallyHeldMemory on overflow; allocateBytesInternal will releaseBytes on + // failure final boolean overflow = ((oldLocal ^ newLocal) & (size ^ newLocal)) < 0; final long beyondReservation = newLocal - reservation; final boolean beyondLimit = overflow || newLocal > allocationLimit.get(); @@ -203,8 +202,10 @@ private AllocationOutcome.Status allocate(final long size, final boolean incomin if (beyondLimit) { finalOutcome = AllocationOutcome.Status.FAILED_LOCAL; } else { - finalOutcome = parentOutcome.isOk() ? AllocationOutcome.Status.SUCCESS - : AllocationOutcome.Status.FAILED_PARENT; + finalOutcome = + parentOutcome.isOk() + ? AllocationOutcome.Status.SUCCESS + : AllocationOutcome.Status.FAILED_PARENT; } if (updatePeak) { @@ -233,9 +234,7 @@ public boolean isOverLimit() { return getAllocatedMemory() > getLimit() || (parent != null && parent.isOverLimit()); } - /** - * Close this Accountant. This will release any reservation bytes back to a parent Accountant. - */ + /** Close this Accountant. This will release any reservation bytes back to a parent Accountant. */ @Override public void close() { // return memory reservation to parent allocator. @@ -272,8 +271,8 @@ public long getInitReservation() { } /** - * Set the maximum amount of memory that can be allocated in the this Accountant before failing - * an allocation. + * Set the maximum amount of memory that can be allocated in the this Accountant before failing an + * allocation. * * @param newLimit The limit in bytes. */ @@ -282,9 +281,8 @@ public void setLimit(long newLimit) { } /** - * Return the current amount of allocated memory that this Accountant is managing accounting - * for. Note this does not - * include reservation memory that hasn't been allocated. + * Return the current amount of allocated memory that this Accountant is managing accounting for. + * Note this does not include reservation memory that hasn't been allocated. * * @return Currently allocate memory in bytes. */ @@ -311,5 +309,4 @@ public long getHeadroom() { long reservedHeadroom = Math.max(0, reservation - locallyHeldMemory.get()); return Math.min(localHeadroom, parent.getHeadroom() + reservedHeadroom); } - } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationListener.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationListener.java index ff2b25dfa30..1c8310cad08 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationListener.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationListener.java @@ -20,8 +20,8 @@ /** * An allocation listener being notified for allocation/deallocation * - *

It might be called from multiple threads if the allocator hierarchy shares a listener, in which - * case, the provider should take care of making the implementation thread-safe. + *

It might be called from multiple threads if the allocator hierarchy shares a listener, in + * which case, the provider should take care of making the implementation thread-safe. */ public interface AllocationListener { @@ -49,18 +49,18 @@ default void onAllocation(long size) {} * Informed each time a buffer is released from allocation. * *

An exception cannot be thrown by this method. + * * @param size The size of the buffer being released. */ default void onRelease(long size) {} - /** * Called whenever an allocation failed, giving the caller a chance to create some space in the * allocator (either by freeing some resource, or by changing the limit), and, if successful, * allowing the allocator to retry the allocation. * - * @param size the buffer size that was being allocated - * @param outcome the outcome of the failed allocation. Carries information of what failed + * @param size the buffer size that was being allocated + * @param outcome the outcome of the failed allocation. Carries information of what failed * @return true, if the allocation can be retried; false if the allocation should fail */ default boolean onFailedAllocation(long size, AllocationOutcome outcome) { @@ -71,7 +71,7 @@ default boolean onFailedAllocation(long size, AllocationOutcome outcome) { * Called immediately after a child allocator was added to the parent allocator. * * @param parentAllocator The parent allocator to which a child was added - * @param childAllocator The child allocator that was just added + * @param childAllocator The child allocator that was just added */ default void onChildAdded(BufferAllocator parentAllocator, BufferAllocator childAllocator) {} diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationManager.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationManager.java index 6ccefdd9c16..3b18ccb6ade 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationManager.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationManager.java @@ -23,35 +23,47 @@ /** * An AllocationManager is the implementation of a physical memory allocation. * - *

Manages the relationship between the allocators and a particular memory allocation. Ensures that - * one allocator owns the memory that multiple allocators may be referencing. Manages a BufferLedger between - * each of its associated allocators. It does not track the reference count; that is the role of {@link BufferLedger} - * (aka {@link ReferenceManager}). + *

Manages the relationship between the allocators and a particular memory allocation. Ensures + * that one allocator owns the memory that multiple allocators may be referencing. Manages a + * BufferLedger between each of its associated allocators. It does not track the reference count; + * that is the role of {@link BufferLedger} (aka {@link ReferenceManager}). * - *

This is a public interface implemented by concrete allocator implementations (e.g. Netty or Unsafe). + *

This is a public interface implemented by concrete allocator implementations (e.g. Netty or + * Unsafe). * *

Threading: AllocationManager manages thread-safety internally. Operations within the context - * of a single BufferLedger are lockless in nature and can be leveraged by multiple threads. Operations that cross the - * context of two ledgers will acquire a lock on the AllocationManager instance. Important note, there is one - * AllocationManager per physical buffer allocation. As such, there will be thousands of these in a - * typical query. The contention of acquiring a lock on AllocationManager should be very low. + * of a single BufferLedger are lockless in nature and can be leveraged by multiple threads. + * Operations that cross the context of two ledgers will acquire a lock on the AllocationManager + * instance. Important note, there is one AllocationManager per physical buffer allocation. As such, + * there will be thousands of these in a typical query. The contention of acquiring a lock on + * AllocationManager should be very low. */ public abstract class AllocationManager { - // The RootAllocator we are associated with. An allocation can only ever be associated with a single RootAllocator. + // The RootAllocator we are associated with. An allocation can only ever be associated with a + // single RootAllocator. private final BufferAllocator root; - // An allocation can be tracked by multiple allocators. (This is because an allocator is more like a ledger.) - // All such allocators track reference counts individually, via BufferLedger instances. When an individual - // reference count reaches zero, the allocator will be dissociated from this allocation. If that was via the - // owningLedger, then no more allocators should be tracking this allocation, and the allocation will be freed. + // An allocation can be tracked by multiple allocators. (This is because an allocator is more like + // a ledger.) + // All such allocators track reference counts individually, via BufferLedger instances. When an + // individual + // reference count reaches zero, the allocator will be dissociated from this allocation. If that + // was via the + // owningLedger, then no more allocators should be tracking this allocation, and the allocation + // will be freed. // ARROW-1627: Trying to minimize memory overhead caused by previously used IdentityHashMap - private final LowCostIdentityHashMap map = new LowCostIdentityHashMap<>(); + private final LowCostIdentityHashMap map = + new LowCostIdentityHashMap<>(); // The primary BufferLedger (i.e. reference count) tracking this allocation. - // This is mostly a semantic constraint on the API user: if the reference count reaches 0 in the owningLedger, then - // there are not supposed to be any references through other allocators. In practice, this doesn't do anything - // as the implementation just forces ownership to be transferred to one of the other extant references. + // This is mostly a semantic constraint on the API user: if the reference count reaches 0 in the + // owningLedger, then + // there are not supposed to be any references through other allocators. In practice, this doesn't + // do anything + // as the implementation just forces ownership to be transferred to one of the other extant + // references. private volatile @Nullable BufferLedger owningLedger; - @SuppressWarnings("nullness:method.invocation") //call to associate(a, b) not allowed on the given receiver + @SuppressWarnings( + "nullness:method.invocation") // call to associate(a, b) not allowed on the given receiver protected AllocationManager(BufferAllocator accountingAllocator) { Preconditions.checkNotNull(accountingAllocator); accountingAllocator.assertOpen(); @@ -72,12 +84,12 @@ void setOwningLedger(final BufferLedger ledger) { } /** - * Associate the existing underlying buffer with a new allocator. This will increase the - * reference count on the corresponding buffer ledger by 1. + * Associate the existing underlying buffer with a new allocator. This will increase the reference + * count on the corresponding buffer ledger by 1. * * @param allocator The target allocator to associate this buffer with. - * @return The reference manager (new or existing) that associates the underlying - * buffer to this new ledger. + * @return The reference manager (new or existing) that associates the underlying buffer to this + * new ledger. */ BufferLedger associate(final BufferAllocator allocator) { return associate(allocator, true); @@ -85,8 +97,9 @@ BufferLedger associate(final BufferAllocator allocator) { private BufferLedger associate(final BufferAllocator allocator, final boolean retain) { allocator.assertOpen(); - Preconditions.checkState(root == allocator.getRoot(), - "A buffer can only be associated between two allocators that share the same root"); + Preconditions.checkState( + root == allocator.getRoot(), + "A buffer can only be associated between two allocators that share the same root"); synchronized (this) { BufferLedger ledger = map.get(allocator); @@ -109,7 +122,8 @@ private BufferLedger associate(final BufferAllocator allocator, final boolean re // store the mapping for BufferLedger oldLedger = map.put(ledger); - Preconditions.checkState(oldLedger == null, + Preconditions.checkState( + oldLedger == null, "Detected inconsistent state: A reference manager already exists for this allocator"); if (allocator instanceof BaseAllocator) { @@ -123,10 +137,9 @@ private BufferLedger associate(final BufferAllocator allocator, final boolean re /** * The way that a particular ReferenceManager (BufferLedger) communicates back to the - * AllocationManager that it no longer needs to hold a reference to a particular - * piece of memory. Reference manager needs to hold a lock to invoke this method - * It is called when the shared refcount of all the ArrowBufs managed by the - * calling ReferenceManager drops to 0. + * AllocationManager that it no longer needs to hold a reference to a particular piece of memory. + * Reference manager needs to hold a lock to invoke this method It is called when the shared + * refcount of all the ArrowBufs managed by the calling ReferenceManager drops to 0. */ void release(final BufferLedger ledger) { final BufferAllocator allocator = ledger.getAllocator(); @@ -134,10 +147,11 @@ void release(final BufferLedger ledger) { // remove the mapping for the allocator // of calling BufferLedger - Preconditions.checkState(map.containsKey(allocator), - "Expecting a mapping for allocator and reference manager"); + Preconditions.checkState( + map.containsKey(allocator), "Expecting a mapping for allocator and reference manager"); final BufferLedger oldLedger = map.remove(allocator); - Preconditions.checkState(oldLedger != null, "Expecting a mapping for allocator and reference manager"); + Preconditions.checkState( + oldLedger != null, "Expecting a mapping for allocator and reference manager"); BufferAllocator oldAllocator = oldLedger.getAllocator(); if (oldAllocator instanceof BaseAllocator) { // needed for debug only: tell the allocator that AllocationManager is removing a @@ -169,8 +183,9 @@ void release(final BufferLedger ledger) { } else { // the release call was made by a non-owning reference manager, so after remove there have // to be 1 or more mappings - Preconditions.checkState(map.size() > 0, - "The final removal of reference manager should be connected to owning reference manager"); + Preconditions.checkState( + map.size() > 0, + "The final removal of reference manager should be connected to owning reference manager"); } } @@ -183,27 +198,23 @@ void release(final BufferLedger ledger) { */ public abstract long getSize(); - /** - * Return the absolute memory address pointing to the fist byte of underlying memory chunk. - */ + /** Return the absolute memory address pointing to the fist byte of underlying memory chunk. */ protected abstract long memoryAddress(); - /** - * Release the underlying memory chunk. - */ + /** Release the underlying memory chunk. */ protected abstract void release0(); /** - * A factory interface for creating {@link AllocationManager}. - * One may extend this interface to use a user-defined AllocationManager implementation. + * A factory interface for creating {@link AllocationManager}. One may extend this interface to + * use a user-defined AllocationManager implementation. */ public interface Factory { /** * Create an {@link AllocationManager}. * - * @param accountingAllocator The allocator that are expected to be associated with newly created AllocationManager. - * Currently it is always equivalent to "this" + * @param accountingAllocator The allocator that are expected to be associated with newly + * created AllocationManager. Currently it is always equivalent to "this" * @param size Size (in bytes) of memory managed by the AllocationManager * @return The created AllocationManager used by this allocator */ diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationOutcome.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationOutcome.java index 21a57eee49b..3757d8497c7 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationOutcome.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationOutcome.java @@ -18,13 +18,9 @@ package org.apache.arrow.memory; import java.util.Optional; - import org.checkerframework.checker.nullness.qual.Nullable; - -/** - * Describes the type of outcome that occurred when trying to account for allocation of memory. - */ +/** Describes the type of outcome that occurred when trying to account for allocation of memory. */ public class AllocationOutcome { private final Status status; private final @Nullable AllocationOutcomeDetails details; @@ -41,6 +37,7 @@ public class AllocationOutcome { /** * Get the status of the allocation. + * * @return status code. */ public Status getStatus() { @@ -49,6 +46,7 @@ public Status getStatus() { /** * Get additional details of the allocation (like the status at each allocator in the hierarchy). + * * @return details of allocation */ public Optional getDetails() { @@ -57,34 +55,25 @@ public Optional getDetails() { /** * Returns true if the allocation was a success. + * * @return true if allocation was successful, false otherwise. */ public boolean isOk() { return status.isOk(); } - /** - * Allocation status code. - */ + /** Allocation status code. */ public enum Status { - /** - * Allocation succeeded. - */ + /** Allocation succeeded. */ SUCCESS(true), - /** - * Allocation succeeded but only because the allocator was forced to move beyond a limit. - */ + /** Allocation succeeded but only because the allocator was forced to move beyond a limit. */ FORCED_SUCCESS(true), - /** - * Allocation failed because the local allocator's limits were exceeded. - */ + /** Allocation failed because the local allocator's limits were exceeded. */ FAILED_LOCAL(false), - /** - * Allocation failed because a parent allocator's limits were exceeded. - */ + /** Allocation failed because a parent allocator's limits were exceeded. */ FAILED_PARENT(false); private final boolean ok; diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationOutcomeDetails.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationOutcomeDetails.java index 3ceda71cce0..bf8f6523f45 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationOutcomeDetails.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationOutcomeDetails.java @@ -19,13 +19,9 @@ import java.util.ArrayDeque; import java.util.Deque; - import org.checkerframework.checker.nullness.qual.Nullable; - -/** - * Captures details of allocation for each accountant in the hierarchical chain. - */ +/** Captures details of allocation for each accountant in the hierarchical chain. */ public class AllocationOutcomeDetails { Deque allocEntries; @@ -33,8 +29,12 @@ public class AllocationOutcomeDetails { allocEntries = new ArrayDeque<>(); } - void pushEntry(Accountant accountant, long totalUsedBeforeAllocation, long requestedSize, - long allocatedSize, boolean allocationFailed) { + void pushEntry( + Accountant accountant, + long totalUsedBeforeAllocation, + long requestedSize, + long allocatedSize, + boolean allocationFailed) { Entry top = allocEntries.peekLast(); if (top != null && top.allocationFailed) { @@ -42,12 +42,14 @@ void pushEntry(Accountant accountant, long totalUsedBeforeAllocation, long reque return; } - allocEntries.addLast(new Entry(accountant, totalUsedBeforeAllocation, requestedSize, - allocatedSize, allocationFailed)); + allocEntries.addLast( + new Entry( + accountant, totalUsedBeforeAllocation, requestedSize, allocatedSize, allocationFailed)); } /** * Get the allocator that caused the failure. + * * @return the allocator that caused failure, null if there was no failure. */ public @Nullable BufferAllocator getFailedAllocator() { @@ -67,9 +69,7 @@ public String toString() { return sb.toString(); } - /** - * Outcome of the allocation request at one accountant in the hierarchy. - */ + /** Outcome of the allocation request at one accountant in the hierarchy. */ public static class Entry { private final Accountant accountant; @@ -82,8 +82,12 @@ public static class Entry { private final long allocatedSize; private final boolean allocationFailed; - Entry(Accountant accountant, long totalUsedBeforeAllocation, long requestedSize, - long allocatedSize, boolean allocationFailed) { + Entry( + Accountant accountant, + long totalUsedBeforeAllocation, + long requestedSize, + long allocatedSize, + boolean allocationFailed) { this.accountant = accountant; this.limit = accountant.getLimit(); this.used = totalUsedBeforeAllocation; @@ -131,5 +135,4 @@ public String toString() { .toString(); } } - } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationReservation.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationReservation.java index c672dc48d79..92d60992564 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationReservation.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocationReservation.java @@ -19,22 +19,18 @@ /** * Supports cumulative allocation reservation. Clients may increase the size of the reservation - * repeatedly until they - * call for an allocation of the current total size. The reservation can only be used once, and - * will throw an exception - * if it is used more than once. - *

- * For the purposes of airtight memory accounting, the reservation must be close()d whether it is - * used or not. - * This is not threadsafe. - *

+ * repeatedly until they call for an allocation of the current total size. The reservation can only + * be used once, and will throw an exception if it is used more than once. + * + *

For the purposes of airtight memory accounting, the reservation must be close()d whether it is + * used or not. This is not threadsafe. */ public interface AllocationReservation extends AutoCloseable { /** * Add to the current reservation. * - *

Adding may fail if the allocator is not allowed to consume any more space.

+ *

Adding may fail if the allocator is not allowed to consume any more space. * * @param nBytes the number of bytes to add * @return true if the addition is possible, false otherwise @@ -45,7 +41,7 @@ public interface AllocationReservation extends AutoCloseable { /** * Requests a reservation of additional space. * - *

The implementation of the allocator's inner class provides this.

+ *

The implementation of the allocator's inner class provides this. * * @param nBytes the amount to reserve * @return true if the reservation can be satisfied, false otherwise @@ -55,8 +51,8 @@ public interface AllocationReservation extends AutoCloseable { /** * Allocate a buffer whose size is the total of all the add()s made. * - *

The allocation request can still fail, even if the amount of space - * requested is available, if the allocation cannot be made contiguously.

+ *

The allocation request can still fail, even if the amount of space requested is available, + * if the allocation cannot be made contiguously. * * @return the buffer, or null, if the request cannot be satisfied * @throws IllegalStateException if called more than once diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocatorClosedException.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocatorClosedException.java index 39c2ef82e09..88b7531c5ad 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocatorClosedException.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/AllocatorClosedException.java @@ -17,10 +17,7 @@ package org.apache.arrow.memory; -/** - * Exception thrown when a closed BufferAllocator is used. Note - * this is an unchecked exception. - */ +/** Exception thrown when a closed BufferAllocator is used. Note this is an unchecked exception. */ @SuppressWarnings("serial") public class AllocatorClosedException extends RuntimeException { diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ArrowBuf.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ArrowBuf.java index 112d36ece04..ba102b81da1 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ArrowBuf.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ArrowBuf.java @@ -26,7 +26,6 @@ import java.nio.ByteOrder; import java.nio.ReadOnlyBufferException; import java.util.concurrent.atomic.AtomicLong; - import org.apache.arrow.memory.BaseAllocator.Verbosity; import org.apache.arrow.memory.util.CommonUtil; import org.apache.arrow.memory.util.HistoricalLog; @@ -36,24 +35,18 @@ import org.checkerframework.checker.nullness.qual.Nullable; /** - * ArrowBuf serves as a facade over underlying memory by providing - * several access APIs to read/write data into a chunk of direct - * memory. All the accounting, ownership and reference management - * is done by {@link ReferenceManager} and ArrowBuf can work - * with a custom user provided implementation of ReferenceManager - *

- * Two important instance variables of an ArrowBuf: - * (1) address - starting virtual address in the underlying memory - * chunk that this ArrowBuf has access to - * (2) length - length (in bytes) in the underlying memory chunk - * that this ArrowBuf has access to - *

- *

- * The management (allocation, deallocation, reference counting etc) for - * the memory chunk is not done by ArrowBuf. - * Default implementation of ReferenceManager, allocation is in - * {@link BaseAllocator}, {@link BufferLedger} and {@link AllocationManager} - *

+ * ArrowBuf serves as a facade over underlying memory by providing several access APIs to read/write + * data into a chunk of direct memory. All the accounting, ownership and reference management is + * done by {@link ReferenceManager} and ArrowBuf can work with a custom user provided implementation + * of ReferenceManager + * + *

Two important instance variables of an ArrowBuf: (1) address - starting virtual address in the + * underlying memory chunk that this ArrowBuf has access to (2) length - length (in bytes) in the + * underlying memory chunk that this ArrowBuf has access to + * + *

The management (allocation, deallocation, reference counting etc) for the memory chunk is not + * done by ArrowBuf. Default implementation of ReferenceManager, allocation is in {@link + * BaseAllocator}, {@link BufferLedger} and {@link AllocationManager} */ public final class ArrowBuf implements AutoCloseable { @@ -73,14 +66,17 @@ public final class ArrowBuf implements AutoCloseable { private final long addr; private long readerIndex; private long writerIndex; - private final @Nullable HistoricalLog historicalLog = BaseAllocator.DEBUG ? - new HistoricalLog(BaseAllocator.DEBUG_LOG_LENGTH, "ArrowBuf[%d]", id) : null; + private final @Nullable HistoricalLog historicalLog = + BaseAllocator.DEBUG + ? new HistoricalLog(BaseAllocator.DEBUG_LOG_LENGTH, "ArrowBuf[%d]", id) + : null; private volatile long capacity; /** * Constructs a new ArrowBuf. * - * @param referenceManager The memory manager to track memory usage and reference count of this buffer + * @param referenceManager The memory manager to track memory usage and reference count of this + * buffer * @param capacity The capacity in bytes of this buffer */ public ArrowBuf( @@ -107,11 +103,10 @@ public int refCnt() { * Allows a function to determine whether not reading a particular string of bytes is valid. * *

Will throw an exception if the memory is not readable for some reason. Only doesn't - * something in the case that - * AssertionUtil.BOUNDS_CHECKING_ENABLED is true. + * something in the case that AssertionUtil.BOUNDS_CHECKING_ENABLED is true. * * @param start The starting position of the bytes to be read. - * @param end The exclusive endpoint of the bytes to be read. + * @param end The exclusive endpoint of the bytes to be read. */ public void checkBytes(long start, long end) { if (BoundsChecking.BOUNDS_CHECKING_ENABLED) { @@ -119,9 +114,7 @@ public void checkBytes(long start, long end) { } } - /** - * For get/set operations, reference count should be >= 1. - */ + /** For get/set operations, reference count should be >= 1. */ private void ensureAccessible() { if (this.refCnt() == 0) { throw new IllegalStateException("Ref count should be >= 1 for accessing the ArrowBuf"); @@ -130,6 +123,7 @@ private void ensureAccessible() { /** * Get reference manager for this ArrowBuf. + * * @return user provided implementation of {@link ReferenceManager} */ public ReferenceManager getReferenceManager() { @@ -141,7 +135,7 @@ public long capacity() { } /** - * Adjusts the capacity of this buffer. Size increases are NOT supported. + * Adjusts the capacity of this buffer. Size increases are NOT supported. * * @param newCapacity Must be in in the range [0, length). */ @@ -158,42 +152,36 @@ public synchronized ArrowBuf capacity(long newCapacity) { return this; } - throw new UnsupportedOperationException("Buffers don't support resizing that increases the size."); + throw new UnsupportedOperationException( + "Buffers don't support resizing that increases the size."); } - /** - * Returns the byte order of elements in this buffer. - */ + /** Returns the byte order of elements in this buffer. */ public ByteOrder order() { return ByteOrder.nativeOrder(); } - /** - * Returns the number of bytes still available to read in this buffer. - */ + /** Returns the number of bytes still available to read in this buffer. */ public long readableBytes() { - Preconditions.checkState(writerIndex >= readerIndex, - "Writer index cannot be less than reader index"); + Preconditions.checkState( + writerIndex >= readerIndex, "Writer index cannot be less than reader index"); return writerIndex - readerIndex; } /** - * Returns the number of bytes still available to write into this buffer before capacity is reached. + * Returns the number of bytes still available to write into this buffer before capacity is + * reached. */ public long writableBytes() { return capacity() - writerIndex; } - /** - * Returns a slice of only the readable bytes in the buffer. - */ + /** Returns a slice of only the readable bytes in the buffer. */ public ArrowBuf slice() { return slice(readerIndex, readableBytes()); } - /** - * Returns a slice (view) starting at index with the given length. - */ + /** Returns a slice (view) starting at index with the given length. */ public ArrowBuf slice(long index, long length) { Preconditions.checkPositionIndex(index, this.capacity); @@ -209,17 +197,12 @@ public ArrowBuf slice(long index, long length) { return newBuf; } - /** - * Make a nio byte buffer from this arrowbuf. - */ + /** Make a nio byte buffer from this arrowbuf. */ public ByteBuffer nioBuffer() { return nioBuffer(readerIndex, checkedCastToInt(readableBytes())); } - - /** - * Make a nio byte buffer from this ArrowBuf. - */ + /** Make a nio byte buffer from this ArrowBuf. */ public ByteBuffer nioBuffer(long index, int length) { chk(index, length); return getDirectBuffer(index, length); @@ -283,22 +266,18 @@ private long addr(long index) { return addr + index; } - - /*-------------------------------------------------* - | Following are a set of fast path data set and | - | get APIs to write/read data from ArrowBuf | - | at a given index (0 based relative to this | - | ArrowBuf and not relative to the underlying | - | memory chunk). | - | | + | Following are a set of fast path data set and | + | get APIs to write/read data from ArrowBuf | + | at a given index (0 based relative to this | + | ArrowBuf and not relative to the underlying | + | memory chunk). | + | | *-------------------------------------------------*/ - - /** - * Helper function to do bounds checking at a particular - * index for particular length of data. + * Helper function to do bounds checking at a particular index for particular length of data. + * * @param index index (0 based relative to this ArrowBuf) * @param length provided length of data for get/set */ @@ -317,16 +296,17 @@ private void checkIndexD(long index, long fieldLength) { if (historicalLog != null) { historicalLog.logHistory(logger); } - throw new IndexOutOfBoundsException(String.format( - "index: %d, length: %d (expected: range(0, %d))", index, fieldLength, capacity())); + throw new IndexOutOfBoundsException( + String.format( + "index: %d, length: %d (expected: range(0, %d))", index, fieldLength, capacity())); } } /** - * Get long value stored at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be read from + * Get long value stored at a particular index in the underlying memory chunk this ArrowBuf has + * access to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be read from * @return 8 byte long value */ public long getLong(long index) { @@ -335,10 +315,10 @@ public long getLong(long index) { } /** - * Set long value at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be written + * Set long value at a particular index in the underlying memory chunk this ArrowBuf has access + * to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be written * @param value value to write */ public void setLong(long index, long value) { @@ -347,10 +327,10 @@ public void setLong(long index, long value) { } /** - * Get float value stored at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be read from + * Get float value stored at a particular index in the underlying memory chunk this ArrowBuf has + * access to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be read from * @return 4 byte float value */ public float getFloat(long index) { @@ -358,10 +338,10 @@ public float getFloat(long index) { } /** - * Set float value at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be written + * Set float value at a particular index in the underlying memory chunk this ArrowBuf has access + * to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be written * @param value value to write */ public void setFloat(long index, float value) { @@ -370,10 +350,10 @@ public void setFloat(long index, float value) { } /** - * Get double value stored at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be read from + * Get double value stored at a particular index in the underlying memory chunk this ArrowBuf has + * access to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be read from * @return 8 byte double value */ public double getDouble(long index) { @@ -381,10 +361,10 @@ public double getDouble(long index) { } /** - * Set double value at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be written + * Set double value at a particular index in the underlying memory chunk this ArrowBuf has access + * to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be written * @param value value to write */ public void setDouble(long index, double value) { @@ -393,10 +373,10 @@ public void setDouble(long index, double value) { } /** - * Get char value stored at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be read from + * Get char value stored at a particular index in the underlying memory chunk this ArrowBuf has + * access to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be read from * @return 2 byte char value */ public char getChar(long index) { @@ -404,10 +384,10 @@ public char getChar(long index) { } /** - * Set char value at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be written + * Set char value at a particular index in the underlying memory chunk this ArrowBuf has access + * to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be written * @param value value to write */ public void setChar(long index, int value) { @@ -416,10 +396,10 @@ public void setChar(long index, int value) { } /** - * Get int value stored at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be read from + * Get int value stored at a particular index in the underlying memory chunk this ArrowBuf has + * access to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be read from * @return 4 byte int value */ public int getInt(long index) { @@ -428,10 +408,9 @@ public int getInt(long index) { } /** - * Set int value at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be written + * Set int value at a particular index in the underlying memory chunk this ArrowBuf has access to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be written * @param value value to write */ public void setInt(long index, int value) { @@ -440,10 +419,10 @@ public void setInt(long index, int value) { } /** - * Get short value stored at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be read from + * Get short value stored at a particular index in the underlying memory chunk this ArrowBuf has + * access to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be read from * @return 2 byte short value */ public short getShort(long index) { @@ -452,10 +431,10 @@ public short getShort(long index) { } /** - * Set short value at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be written + * Set short value at a particular index in the underlying memory chunk this ArrowBuf has access + * to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be written * @param value value to write */ public void setShort(long index, int value) { @@ -463,10 +442,10 @@ public void setShort(long index, int value) { } /** - * Set short value at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be written + * Set short value at a particular index in the underlying memory chunk this ArrowBuf has access + * to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be written * @param value value to write */ public void setShort(long index, short value) { @@ -475,10 +454,10 @@ public void setShort(long index, short value) { } /** - * Set byte value at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be written + * Set byte value at a particular index in the underlying memory chunk this ArrowBuf has access + * to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be written * @param value value to write */ public void setByte(long index, int value) { @@ -487,10 +466,10 @@ public void setByte(long index, int value) { } /** - * Set byte value at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be written + * Set byte value at a particular index in the underlying memory chunk this ArrowBuf has access + * to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be written * @param value value to write */ public void setByte(long index, byte value) { @@ -499,10 +478,10 @@ public void setByte(long index, byte value) { } /** - * Get byte value stored at a particular index in the - * underlying memory chunk this ArrowBuf has access to. - * @param index index (0 based relative to this ArrowBuf) - * where the value will be read from + * Get byte value stored at a particular index in the underlying memory chunk this ArrowBuf has + * access to. + * + * @param index index (0 based relative to this ArrowBuf) where the value will be read from * @return byte value */ public byte getByte(long index) { @@ -510,20 +489,16 @@ public byte getByte(long index) { return MemoryUtil.UNSAFE.getByte(addr(index)); } - - /*--------------------------------------------------* - | Following are another set of data set APIs | - | that directly work with writerIndex | - | | + | Following are another set of data set APIs | + | that directly work with writerIndex | + | | *--------------------------------------------------*/ - - /** - * Helper function to do bound checking w.r.t writerIndex - * by checking if we can set "length" bytes of data at the - * writerIndex in this ArrowBuf. + * Helper function to do bound checking w.r.t writerIndex by checking if we can set "length" bytes + * of data at the writerIndex in this ArrowBuf. + * * @param length provided length of data for set */ private void ensureWritable(final int length) { @@ -534,15 +509,17 @@ private void ensureWritable(final int length) { // check bounds if (length > writableBytes()) { throw new IndexOutOfBoundsException( - String.format("writerIndex(%d) + length(%d) exceeds capacity(%d)", writerIndex, length, capacity())); + String.format( + "writerIndex(%d) + length(%d) exceeds capacity(%d)", + writerIndex, length, capacity())); } } } /** - * Helper function to do bound checking w.r.t readerIndex - * by checking if we can read "length" bytes of data at the - * readerIndex in this ArrowBuf. + * Helper function to do bound checking w.r.t readerIndex by checking if we can read "length" + * bytes of data at the readerIndex in this ArrowBuf. + * * @param length provided length of data for get */ private void ensureReadable(final int length) { @@ -553,13 +530,16 @@ private void ensureReadable(final int length) { // check bounds if (length > readableBytes()) { throw new IndexOutOfBoundsException( - String.format("readerIndex(%d) + length(%d) exceeds writerIndex(%d)", readerIndex, length, writerIndex)); + String.format( + "readerIndex(%d) + length(%d) exceeds writerIndex(%d)", + readerIndex, length, writerIndex)); } } } /** * Read the byte at readerIndex. + * * @return byte value */ public byte readByte() { @@ -571,6 +551,7 @@ public byte readByte() { /** * Read dst.length bytes at readerIndex into dst byte array + * * @param dst byte array where the data will be written */ public void readBytes(byte[] dst) { @@ -581,6 +562,7 @@ public void readBytes(byte[] dst) { /** * Set the provided byte value at the writerIndex. + * * @param value value to set */ public void writeByte(byte value) { @@ -590,8 +572,8 @@ public void writeByte(byte value) { } /** - * Set the lower order byte for the provided value at - * the writerIndex. + * Set the lower order byte for the provided value at the writerIndex. + * * @param value value to be set */ public void writeByte(int value) { @@ -601,8 +583,8 @@ public void writeByte(int value) { } /** - * Write the bytes from given byte array into this - * ArrowBuf starting at writerIndex. + * Write the bytes from given byte array into this ArrowBuf starting at writerIndex. + * * @param src src byte array */ public void writeBytes(byte[] src) { @@ -611,8 +593,9 @@ public void writeBytes(byte[] src) { } /** - * Write the bytes from given byte array starting at srcIndex - * into this ArrowBuf starting at writerIndex. + * Write the bytes from given byte array starting at srcIndex into this ArrowBuf starting at + * writerIndex. + * * @param src src byte array * @param srcIndex index in the byte array where the copy will being from * @param length length of data to copy @@ -625,6 +608,7 @@ public void writeBytes(byte[] src, int srcIndex, int length) { /** * Set the provided int value as short at the writerIndex. + * * @param value value to set */ public void writeShort(int value) { @@ -635,6 +619,7 @@ public void writeShort(int value) { /** * Set the provided int value at the writerIndex. + * * @param value value to set */ public void writeInt(int value) { @@ -645,6 +630,7 @@ public void writeInt(int value) { /** * Set the provided long value at the writerIndex. + * * @param value value to set */ public void writeLong(long value) { @@ -655,6 +641,7 @@ public void writeLong(long value) { /** * Set the provided float value at the writerIndex. + * * @param value value to set */ public void writeFloat(float value) { @@ -665,6 +652,7 @@ public void writeFloat(float value) { /** * Set the provided double value at the writerIndex. + * * @param value value to set */ public void writeDouble(double value) { @@ -673,21 +661,21 @@ public void writeDouble(double value) { writerIndex += DOUBLE_SIZE; } - /*--------------------------------------------------* - | Following are another set of data set/get APIs | - | that read and write stream of bytes from/to byte | - | arrays, ByteBuffer, ArrowBuf etc | - | | + | Following are another set of data set/get APIs | + | that read and write stream of bytes from/to byte | + | arrays, ByteBuffer, ArrowBuf etc | + | | *--------------------------------------------------*/ /** * Determine if the requested {@code index} and {@code length} will fit within {@code capacity}. + * * @param index The starting index. * @param length The length which will be utilized (starting from {@code index}). * @param capacity The capacity that {@code index + length} is allowed to be within. - * @return {@code true} if the requested {@code index} and {@code length} will fit within {@code capacity}. - * {@code false} if this would result in an index out of bounds exception. + * @return {@code true} if the requested {@code index} and {@code length} will fit within {@code + * capacity}. {@code false} if this would result in an index out of bounds exception. */ private static boolean isOutOfBounds(long index, long length, long capacity) { return (index | length | (index + length) | (capacity - (index + length))) < 0; @@ -699,17 +687,19 @@ private void checkIndex(long index, long fieldLength) { this.ensureAccessible(); // check bounds if (isOutOfBounds(index, fieldLength, this.capacity())) { - throw new IndexOutOfBoundsException(String.format("index: %d, length: %d (expected: range(0, %d))", + throw new IndexOutOfBoundsException( + String.format( + "index: %d, length: %d (expected: range(0, %d))", index, fieldLength, this.capacity())); } } } /** - * Copy data from this ArrowBuf at a given index in into destination - * byte array. - * @param index starting index (0 based relative to the portion of memory) - * this ArrowBuf has access to + * Copy data from this ArrowBuf at a given index in into destination byte array. + * + * @param index starting index (0 based relative to the portion of memory) this ArrowBuf has + * access to * @param dst byte array to copy the data into */ public void getBytes(long index, byte[] dst) { @@ -718,8 +708,8 @@ public void getBytes(long index, byte[] dst) { /** * Copy data from this ArrowBuf at a given index into destination byte array. - * @param index index (0 based relative to the portion of memory - * this ArrowBuf has access to) + * + * @param index index (0 based relative to the portion of memory this ArrowBuf has access to) * @param dst byte array to copy the data into * @param dstIndex starting index in dst byte array to copy into * @param length length of data to copy from this ArrowBuf @@ -732,20 +722,22 @@ public void getBytes(long index, byte[] dst, int dstIndex, int length) { // bound check for dst byte array where the data will be copied to if (isOutOfBounds(dstIndex, length, dst.length)) { // not enough space to copy "length" bytes into dst array from dstIndex onwards - throw new IndexOutOfBoundsException("Not enough space to copy data into destination" + dstIndex); + throw new IndexOutOfBoundsException( + "Not enough space to copy data into destination" + dstIndex); } if (length != 0) { // copy "length" bytes from this ArrowBuf starting at addr(index) address // into dst byte array at dstIndex onwards - MemoryUtil.copyMemory(null, addr(index), dst, MemoryUtil.BYTE_ARRAY_BASE_OFFSET + dstIndex, length); + MemoryUtil.copyMemory( + null, addr(index), dst, MemoryUtil.BYTE_ARRAY_BASE_OFFSET + dstIndex, length); } } /** - * Copy data from a given byte array into this ArrowBuf starting at - * a given index. - * @param index starting index (0 based relative to the portion of memory) - * this ArrowBuf has access to + * Copy data from a given byte array into this ArrowBuf starting at a given index. + * + * @param index starting index (0 based relative to the portion of memory) this ArrowBuf has + * access to * @param src byte array to copy the data from */ public void setBytes(long index, byte[] src) { @@ -753,10 +745,10 @@ public void setBytes(long index, byte[] src) { } /** - * Copy data from a given byte array starting at the given source index into - * this ArrowBuf at a given index. - * @param index index (0 based relative to the portion of memory this ArrowBuf - * has access to) + * Copy data from a given byte array starting at the given source index into this ArrowBuf at a + * given index. + * + * @param index index (0 based relative to the portion of memory this ArrowBuf has access to) * @param src src byte array to copy the data from * @param srcIndex index in the byte array where the copy will start from * @param length length of data to copy from byte array @@ -769,20 +761,21 @@ public void setBytes(long index, byte[] src, int srcIndex, long length) { // bound check for src byte array where the data will be copied from if (isOutOfBounds(srcIndex, length, src.length)) { // not enough space to copy "length" bytes into dst array from dstIndex onwards - throw new IndexOutOfBoundsException("Not enough space to copy data from byte array" + srcIndex); + throw new IndexOutOfBoundsException( + "Not enough space to copy data from byte array" + srcIndex); } if (length > 0) { // copy "length" bytes from src byte array at the starting index (srcIndex) // into this ArrowBuf starting at address "addr(index)" - MemoryUtil.copyMemory(src, MemoryUtil.BYTE_ARRAY_BASE_OFFSET + srcIndex, null, addr(index), length); + MemoryUtil.copyMemory( + src, MemoryUtil.BYTE_ARRAY_BASE_OFFSET + srcIndex, null, addr(index), length); } } /** - * Copy data from this ArrowBuf at a given index into the destination - * ByteBuffer. - * @param index index (0 based relative to the portion of memory this ArrowBuf - * has access to) + * Copy data from this ArrowBuf at a given index into the destination ByteBuffer. + * + * @param index index (0 based relative to the portion of memory this ArrowBuf has access to) * @param dst dst ByteBuffer where the data will be copied into */ public void getBytes(long index, ByteBuffer dst) { @@ -809,20 +802,25 @@ public void getBytes(long index, ByteBuffer dst) { // index dstIndex final int dstIndex = dst.arrayOffset() + dst.position(); MemoryUtil.copyMemory( - null, srcAddress, dst.array(), MemoryUtil.BYTE_ARRAY_BASE_OFFSET + dstIndex, dst.remaining()); + null, + srcAddress, + dst.array(), + MemoryUtil.BYTE_ARRAY_BASE_OFFSET + dstIndex, + dst.remaining()); // after copy, bump the next write position for the dst ByteBuffer dst.position(dst.position() + dst.remaining()); } else { - throw new UnsupportedOperationException("Copy from this ArrowBuf to ByteBuffer is not supported"); + throw new UnsupportedOperationException( + "Copy from this ArrowBuf to ByteBuffer is not supported"); } } } /** - * Copy data into this ArrowBuf at a given index onwards from - * a source ByteBuffer. - * @param index index index (0 based relative to the portion of memory - * this ArrowBuf has access to) + * Copy data into this ArrowBuf at a given index onwards from a source ByteBuffer. + * + * @param index index index (0 based relative to the portion of memory this ArrowBuf has access + * to) * @param src src ByteBuffer where the data will be copied from */ public void setBytes(long index, ByteBuffer src) { @@ -845,7 +843,7 @@ public void setBytes(long index, ByteBuffer src) { // index srcIndex into this ArrowBuf starting at address dstAddress final int srcIndex = src.arrayOffset() + src.position(); MemoryUtil.copyMemory( - src.array(), MemoryUtil.BYTE_ARRAY_BASE_OFFSET + srcIndex, null, dstAddress, length); + src.array(), MemoryUtil.BYTE_ARRAY_BASE_OFFSET + srcIndex, null, dstAddress, length); // after copy, bump the next read position for the src ByteBuffer src.position(src.position() + length); } else { @@ -879,14 +877,12 @@ public void setBytes(long index, ByteBuffer src) { } /** - * Copy data into this ArrowBuf at a given index onwards from - * a source ByteBuffer starting at a given srcIndex for a certain - * length. - * @param index index (0 based relative to the portion of memory - * this ArrowBuf has access to) + * Copy data into this ArrowBuf at a given index onwards from a source ByteBuffer starting at a + * given srcIndex for a certain length. + * + * @param index index (0 based relative to the portion of memory this ArrowBuf has access to) * @param src src ByteBuffer where the data will be copied from - * @param srcIndex starting index in the src ByteBuffer where the data copy - * will start from + * @param srcIndex starting index in the src ByteBuffer where the data copy will start from * @param length length of data to copy from src ByteBuffer */ public void setBytes(long index, ByteBuffer src, int srcIndex, int length) { @@ -912,13 +908,12 @@ public void setBytes(long index, ByteBuffer src, int srcIndex, int length) { } /** - * Copy a given length of data from this ArrowBuf starting at a given index - * into a dst ArrowBuf at dstIndex. - * @param index index (0 based relative to the portion of memory - * this ArrowBuf has access to) + * Copy a given length of data from this ArrowBuf starting at a given index into a dst ArrowBuf at + * dstIndex. + * + * @param index index (0 based relative to the portion of memory this ArrowBuf has access to) * @param dst dst ArrowBuf where the data will be copied into - * @param dstIndex index (0 based relative to the portion of memory - * dst ArrowBuf has access to) + * @param dstIndex index (0 based relative to the portion of memory dst ArrowBuf has access to) * @param length length of data to copy */ public void getBytes(long index, ArrowBuf dst, long dstIndex, int length) { @@ -928,8 +923,9 @@ public void getBytes(long index, ArrowBuf dst, long dstIndex, int length) { Preconditions.checkArgument(dst != null, "expecting a valid ArrowBuf"); // bound check for dst ArrowBuf if (isOutOfBounds(dstIndex, length, dst.capacity())) { - throw new IndexOutOfBoundsException(String.format("index: %d, length: %d (expected: range(0, %d))", - dstIndex, length, dst.capacity())); + throw new IndexOutOfBoundsException( + String.format( + "index: %d, length: %d (expected: range(0, %d))", dstIndex, length, dst.capacity())); } if (length != 0) { // copy length bytes of data from this ArrowBuf starting at @@ -942,13 +938,12 @@ public void getBytes(long index, ArrowBuf dst, long dstIndex, int length) { } /** - * Copy data from src ArrowBuf starting at index srcIndex into this - * ArrowBuf at given index. - * @param index index index (0 based relative to the portion of memory - * this ArrowBuf has access to) + * Copy data from src ArrowBuf starting at index srcIndex into this ArrowBuf at given index. + * + * @param index index index (0 based relative to the portion of memory this ArrowBuf has access + * to) * @param src src ArrowBuf where the data will be copied from - * @param srcIndex starting index in the src ArrowBuf where the copy - * will begin from + * @param srcIndex starting index in the src ArrowBuf where the copy will begin from * @param length length of data to copy from src ArrowBuf */ public void setBytes(long index, ArrowBuf src, long srcIndex, long length) { @@ -958,8 +953,9 @@ public void setBytes(long index, ArrowBuf src, long srcIndex, long length) { Preconditions.checkArgument(src != null, "expecting a valid ArrowBuf"); // bound check for src ArrowBuf if (isOutOfBounds(srcIndex, length, src.capacity())) { - throw new IndexOutOfBoundsException(String.format("index: %d, length: %d (expected: range(0, %d))", - index, length, src.capacity())); + throw new IndexOutOfBoundsException( + String.format( + "index: %d, length: %d (expected: range(0, %d))", index, length, src.capacity())); } if (length != 0) { // copy length bytes of data from src ArrowBuf starting at @@ -972,11 +968,11 @@ public void setBytes(long index, ArrowBuf src, long srcIndex, long length) { } /** - * Copy readableBytes() number of bytes from src ArrowBuf - * starting from its readerIndex into this ArrowBuf starting - * at the given index. - * @param index index index (0 based relative to the portion of memory - * this ArrowBuf has access to) + * Copy readableBytes() number of bytes from src ArrowBuf starting from its readerIndex into this + * ArrowBuf starting at the given index. + * + * @param index index index (0 based relative to the portion of memory this ArrowBuf has access + * to) * @param src src ArrowBuf where the data will be copied from */ public void setBytes(long index, ArrowBuf src) { @@ -992,10 +988,10 @@ public void setBytes(long index, ArrowBuf src) { } /** - * Copy a certain length of bytes from given InputStream - * into this ArrowBuf at the provided index. - * @param index index index (0 based relative to the portion of memory - * this ArrowBuf has access to) + * Copy a certain length of bytes from given InputStream into this ArrowBuf at the provided index. + * + * @param index index index (0 based relative to the portion of memory this ArrowBuf has access + * to) * @param in src stream to copy from * @param length length of data to copy * @return number of bytes copied from stream into ArrowBuf @@ -1019,10 +1015,10 @@ public int setBytes(long index, InputStream in, int length) throws IOException { } /** - * Copy a certain length of bytes from this ArrowBuf at a given - * index into the given OutputStream. - * @param index index index (0 based relative to the portion of memory - * this ArrowBuf has access to) + * Copy a certain length of bytes from this ArrowBuf at a given index into the given OutputStream. + * + * @param index index index (0 based relative to the portion of memory this ArrowBuf has access + * to) * @param out dst stream to copy data into * @param length length of data to copy * @throws IOException on failing to write to stream @@ -1046,8 +1042,9 @@ public void close() { } /** - * Returns the possible memory consumed by this ArrowBuf in the worse case scenario. - * (not shared, connected to larger underlying buffer of allocated memory) + * Returns the possible memory consumed by this ArrowBuf in the worse case scenario. (not shared, + * connected to larger underlying buffer of allocated memory) + * * @return Size in bytes. */ public long getPossibleMemoryConsumed() { @@ -1057,6 +1054,7 @@ public long getPossibleMemoryConsumed() { /** * Return that is Accounted for by this buffer (and its potentially shared siblings within the * context of the associated allocator). + * * @return Size in bytes. */ public long getActualMemoryConsumed() { @@ -1066,7 +1064,7 @@ public long getActualMemoryConsumed() { /** * Return the buffer's byte contents in the form of a hex dump. * - * @param start the starting byte index + * @param start the starting byte index * @param length how many bytes to log * @return A hex dump in a String. */ @@ -1092,6 +1090,7 @@ public String toHexString(final long start, final int length) { /** * Get the integer id assigned to this ArrowBuf for debugging purposes. + * * @return integer id */ public long getId() { @@ -1099,12 +1098,11 @@ public long getId() { } /** - * Print information of this buffer into sb at the given - * indentation and verbosity level. - * - *

It will include history if BaseAllocator.DEBUG is true and - * the verbosity.includeHistoricalLog are true. + * Print information of this buffer into sb at the given indentation and verbosity + * level. * + *

It will include history if BaseAllocator.DEBUG is true and the + * verbosity.includeHistoricalLog are true. */ @VisibleForTesting public void print(StringBuilder sb, int indent, Verbosity verbosity) { @@ -1120,7 +1118,6 @@ public void print(StringBuilder sb, int indent, Verbosity verbosity) { * Print detailed information of this buffer into sb. * *

Most information will only be present if BaseAllocator.DEBUG is true. - * */ public void print(StringBuilder sb, int indent) { print(sb, indent, Verbosity.LOG_WITH_STACKTRACE); @@ -1128,6 +1125,7 @@ public void print(StringBuilder sb, int indent) { /** * Get the index at which the next byte will be read from. + * * @return reader index */ public long readerIndex() { @@ -1136,6 +1134,7 @@ public long readerIndex() { /** * Get the index at which next byte will be written to. + * * @return writer index */ public long writerIndex() { @@ -1144,6 +1143,7 @@ public long writerIndex() { /** * Set the reader index for this ArrowBuf. + * * @param readerIndex new reader index * @return this ArrowBuf */ @@ -1154,6 +1154,7 @@ public ArrowBuf readerIndex(long readerIndex) { /** * Set the writer index for this ArrowBuf. + * * @param writerIndex new writer index * @return this ArrowBuf */ @@ -1163,10 +1164,10 @@ public ArrowBuf writerIndex(long writerIndex) { } /** - * Zero-out the bytes in this ArrowBuf starting at - * the given index for the given length. - * @param index index index (0 based relative to the portion of memory - * this ArrowBuf has access to) + * Zero-out the bytes in this ArrowBuf starting at the given index for the given length. + * + * @param index index index (0 based relative to the portion of memory this ArrowBuf has access + * to) * @param length length of bytes to zero-out * @return this ArrowBuf */ @@ -1180,8 +1181,9 @@ public ArrowBuf setZero(long index, long length) { /** * Sets all bits to one in the specified range. - * @param index index index (0 based relative to the portion of memory - * this ArrowBuf has access to) + * + * @param index index index (0 based relative to the portion of memory this ArrowBuf has access + * to) * @param length length of bytes to set. * @return this ArrowBuf * @deprecated use {@link ArrowBuf#setOne(long, long)} instead. @@ -1197,8 +1199,9 @@ public ArrowBuf setOne(int index, int length) { /** * Sets all bits to one in the specified range. - * @param index index index (0 based relative to the portion of memory - * this ArrowBuf has access to) + * + * @param index index index (0 based relative to the portion of memory this ArrowBuf has access + * to) * @param length length of bytes to set. * @return this ArrowBuf */ @@ -1211,8 +1214,8 @@ public ArrowBuf setOne(long index, long length) { } /** - * Returns this if size is less than {@link #capacity()}, otherwise - * delegates to {@link BufferManager#replace(ArrowBuf, long)} to get a new buffer. + * Returns this if size is less than {@link #capacity()}, otherwise delegates to + * {@link BufferManager#replace(ArrowBuf, long)} to get a new buffer. */ public ArrowBuf reallocIfNeeded(final long size) { Preconditions.checkArgument(size >= 0, "reallocation size must be non-negative"); @@ -1223,7 +1226,7 @@ public ArrowBuf reallocIfNeeded(final long size) { return bufferManager.replace(this, size); } else { throw new UnsupportedOperationException( - "Realloc is only available in the context of operator's UDFs"); + "Realloc is only available in the context of operator's UDFs"); } } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BaseAllocator.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BaseAllocator.java index 8779c7a3434..f21af6ccc70 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BaseAllocator.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BaseAllocator.java @@ -23,7 +23,6 @@ import java.util.IdentityHashMap; import java.util.Map; import java.util.Set; - import org.apache.arrow.memory.rounding.DefaultRoundingPolicy; import org.apache.arrow.memory.rounding.RoundingPolicy; import org.apache.arrow.memory.util.AssertionUtil; @@ -39,17 +38,19 @@ /** * A base-class that implements all functionality of {@linkplain BufferAllocator}s. * - *

The class is abstract to enforce usage of {@linkplain RootAllocator}/{@linkplain ChildAllocator} - * facades. + *

The class is abstract to enforce usage of {@linkplain RootAllocator}/{@linkplain + * ChildAllocator} facades. */ abstract class BaseAllocator extends Accountant implements BufferAllocator { public static final String DEBUG_ALLOCATOR = "arrow.memory.debug.allocator"; public static final int DEBUG_LOG_LENGTH = 6; public static final boolean DEBUG; - private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(BaseAllocator.class); + private static final org.slf4j.Logger logger = + org.slf4j.LoggerFactory.getLogger(BaseAllocator.class); - // Initialize this before DEFAULT_CONFIG as DEFAULT_CONFIG will eventually initialize the allocation manager, + // Initialize this before DEFAULT_CONFIG as DEFAULT_CONFIG will eventually initialize the + // allocation manager, // which in turn allocates an ArrowBuf, which requires DEBUG to have been properly initialized static { // the system property takes precedence. @@ -59,8 +60,11 @@ abstract class BaseAllocator extends Accountant implements BufferAllocator { } else { DEBUG = false; } - logger.info("Debug mode " + (DEBUG ? "enabled." - : "disabled. Enable with the VM option -Darrow.memory.debug.allocator=true.")); + logger.info( + "Debug mode " + + (DEBUG + ? "enabled." + : "disabled. Enable with the VM option -Darrow.memory.debug.allocator=true.")); } public static final Config DEFAULT_CONFIG = ImmutableConfig.builder().build(); @@ -85,18 +89,16 @@ abstract class BaseAllocator extends Accountant implements BufferAllocator { /** * Initialize an allocator. * - * @param parentAllocator parent allocator. null if defining a root allocator - * @param name name of this allocator - * @param config configuration including other options of this allocator - * + * @param parentAllocator parent allocator. null if defining a root allocator + * @param name name of this allocator + * @param config configuration including other options of this allocator * @see Config */ @SuppressWarnings({"nullness:method.invocation", "nullness:cast.unsafe"}) - //{"call to hist(,...) not allowed on the given receiver.", "cast cannot be statically verified"} + // {"call to hist(,...) not allowed on the given receiver.", "cast cannot be statically verified"} protected BaseAllocator( - final @Nullable BaseAllocator parentAllocator, - final String name, - final Config config) throws OutOfMemoryException { + final @Nullable BaseAllocator parentAllocator, final String name, final Config config) + throws OutOfMemoryException { super(parentAllocator, name, config.getInitReservation(), config.getMaxAllocation()); this.listener = config.getListener(); @@ -109,8 +111,8 @@ protected BaseAllocator( this.root = (@Initialized RootAllocator) this; empty = createEmpty(); } else { - throw new IllegalStateException("An parent allocator must either carry a root or be the " + - "root."); + throw new IllegalStateException( + "An parent allocator must either carry a root or be the " + "root."); } this.parentAllocator = parentAllocator; @@ -148,15 +150,17 @@ public Collection getChildAllocators() { } } - private static String createErrorMsg(final BufferAllocator allocator, final long rounded, final long requested) { + private static String createErrorMsg( + final BufferAllocator allocator, final long rounded, final long requested) { if (rounded != requested) { return String.format( - "Unable to allocate buffer of size %d (rounded from %d) due to memory limit. Current " + - "allocation: %d", rounded, requested, allocator.getAllocatedMemory()); + "Unable to allocate buffer of size %d (rounded from %d) due to memory limit. Current " + + "allocation: %d", + rounded, requested, allocator.getAllocatedMemory()); } else { return String.format( - "Unable to allocate buffer of size %d due to memory limit. Current " + - "allocation: %d", rounded, allocator.getAllocatedMemory()); + "Unable to allocate buffer of size %d due to memory limit. Current " + "allocation: %d", + rounded, allocator.getAllocatedMemory()); } } @@ -168,8 +172,8 @@ public static boolean isDebug() { public void assertOpen() { if (AssertionUtil.ASSERT_ENABLED) { if (isClosed) { - throw new IllegalStateException("Attempting operation on allocator when allocator is closed.\n" + - toVerboseString()); + throw new IllegalStateException( + "Attempting operation on allocator when allocator is closed.\n" + toVerboseString()); } } } @@ -185,9 +189,8 @@ public ArrowBuf getEmpty() { } /** - * For debug/verification purposes only. Allows an AllocationManager to tell the allocator that - * we have a new ledger - * associated with this allocator. + * For debug/verification purposes only. Allows an AllocationManager to tell the allocator that we + * have a new ledger associated with this allocator. */ void associateLedger(BufferLedger ledger) { assertOpen(); @@ -201,9 +204,8 @@ void associateLedger(BufferLedger ledger) { } /** - * For debug/verification purposes only. Allows an AllocationManager to tell the allocator that - * we are removing a - * ledger associated with this allocator + * For debug/verification purposes only. Allows an AllocationManager to tell the allocator that we + * are removing a ledger associated with this allocator */ void dissociateLedger(BufferLedger ledger) { assertOpen(); @@ -235,8 +237,12 @@ private void childClosed(final BaseAllocator childAllocator) { if (childAllocator.historicalLog != null) { childAllocator.historicalLog.logHistory(logger); } - throw new IllegalStateException("Child allocator[" + childAllocator.name + - "] not found in parent allocator[" + name + "]'s childAllocators"); + throw new IllegalStateException( + "Child allocator[" + + childAllocator.name + + "] not found in parent allocator[" + + name + + "]'s childAllocators"); } } } else { @@ -257,15 +263,14 @@ public ArrowBuf wrapForeignAllocation(ForeignAllocation allocation) { outcome = this.allocateBytes(size); } if (!outcome.isOk()) { - throw new OutOfMemoryException(createErrorMsg(this, size, - size), outcome.getDetails()); + throw new OutOfMemoryException(createErrorMsg(this, size, size), outcome.getDetails()); } } try { final AllocationManager manager = new ForeignAllocationManager(this, allocation); final BufferLedger ledger = manager.associate(this); final ArrowBuf buf = - new ArrowBuf(ledger, /*bufferManager=*/null, size, allocation.memoryAddress()); + new ArrowBuf(ledger, /* bufferManager= */ null, size, allocation.memoryAddress()); buf.writerIndex(size); listener.onAllocation(size); return buf; @@ -291,7 +296,8 @@ public ArrowBuf buffer(final long initialRequestSize) { return buffer(initialRequestSize, null); } - @SuppressWarnings("nullness:dereference.of.nullable")//dereference of possibly-null reference allocationManagerFactory + @SuppressWarnings("nullness:dereference.of.nullable") // dereference of possibly-null reference + // allocationManagerFactory private ArrowBuf createEmpty() { return allocationManagerFactory.empty(); } @@ -318,8 +324,8 @@ public ArrowBuf buffer(final long initialRequestSize, @Nullable BufferManager ma outcome = this.allocateBytes(actualRequestSize); } if (!outcome.isOk()) { - throw new OutOfMemoryException(createErrorMsg(this, actualRequestSize, - initialRequestSize), outcome.getDetails()); + throw new OutOfMemoryException( + createErrorMsg(this, actualRequestSize, initialRequestSize), outcome.getDetails()); } } @@ -339,12 +345,11 @@ public ArrowBuf buffer(final long initialRequestSize, @Nullable BufferManager ma } /** - * Used by usual allocation as well as for allocating a pre-reserved buffer. - * Skips the typical accounting associated with creating a new buffer. + * Used by usual allocation as well as for allocating a pre-reserved buffer. Skips the typical + * accounting associated with creating a new buffer. */ - private ArrowBuf bufferWithoutReservation( - final long size, - @Nullable BufferManager bufferManager) throws OutOfMemoryException { + private ArrowBuf bufferWithoutReservation(final long size, @Nullable BufferManager bufferManager) + throws OutOfMemoryException { assertOpen(); final AllocationManager manager = newAllocationManager(size); @@ -352,8 +357,11 @@ private ArrowBuf bufferWithoutReservation( final ArrowBuf buffer = ledger.newArrowBuf(size, bufferManager); // make sure that our allocation is equal to what we expected. - Preconditions.checkArgument(buffer.capacity() == size, - "Allocated capacity %d was not equal to requested capacity %d.", buffer.capacity(), size); + Preconditions.checkArgument( + buffer.capacity() == size, + "Allocated capacity %d was not equal to requested capacity %d.", + buffer.capacity(), + size); return buffer; } @@ -362,7 +370,6 @@ private AllocationManager newAllocationManager(long size) { return newAllocationManager(this, size); } - private AllocationManager newAllocationManager(BaseAllocator accountingAllocator, long size) { return allocationManagerFactory.create(accountingAllocator, size); } @@ -374,9 +381,7 @@ public BufferAllocator getRoot() { @Override public BufferAllocator newChildAllocator( - final String name, - final long initReservation, - final long maxAllocation) { + final String name, final long initReservation, final long maxAllocation) { return newChildAllocator(name, this.listener, initReservation, maxAllocation); } @@ -389,20 +394,23 @@ public BufferAllocator newChildAllocator( assertOpen(); final ChildAllocator childAllocator = - new ChildAllocator(this, name, configBuilder() - .listener(listener) - .initReservation(initReservation) - .maxAllocation(maxAllocation) - .roundingPolicy(roundingPolicy) - .allocationManagerFactory(allocationManagerFactory) - .build()); + new ChildAllocator( + this, + name, + configBuilder() + .listener(listener) + .initReservation(initReservation) + .maxAllocation(maxAllocation) + .roundingPolicy(roundingPolicy) + .allocationManagerFactory(allocationManagerFactory) + .build()); if (DEBUG) { synchronized (DEBUG_LOCK) { childAllocators.put(childAllocator, childAllocator); if (historicalLog != null) { - historicalLog.recordEvent("allocator[%s] created new child allocator[%s]", name, - childAllocator.getName()); + historicalLog.recordEvent( + "allocator[%s] created new child allocator[%s]", name, childAllocator.getName()); } } } else { @@ -441,32 +449,33 @@ public synchronized void close() { if (!childAllocators.isEmpty()) { for (final BaseAllocator childAllocator : childAllocators.keySet()) { if (childAllocator.isClosed) { - logger.warn(String.format( - "Closed child allocator[%s] on parent allocator[%s]'s child list.\n%s", - childAllocator.name, name, toString())); + logger.warn( + String.format( + "Closed child allocator[%s] on parent allocator[%s]'s child list.\n%s", + childAllocator.name, name, toString())); } } throw new IllegalStateException( - String.format("Allocator[%s] closed with outstanding child allocators.\n%s", name, - toString())); + String.format( + "Allocator[%s] closed with outstanding child allocators.\n%s", name, toString())); } // are there outstanding buffers? final int allocatedCount = childLedgers != null ? childLedgers.size() : 0; if (allocatedCount > 0) { throw new IllegalStateException( - String.format("Allocator[%s] closed with outstanding buffers allocated (%d).\n%s", - name, allocatedCount, toString())); + String.format( + "Allocator[%s] closed with outstanding buffers allocated (%d).\n%s", + name, allocatedCount, toString())); } if (reservations != null && reservations.size() != 0) { throw new IllegalStateException( - String.format("Allocator[%s] closed with outstanding reservations (%d).\n%s", name, - reservations.size(), - toString())); + String.format( + "Allocator[%s] closed with outstanding reservations (%d).\n%s", + name, reservations.size(), toString())); } - } } else { if (!childAllocators.isEmpty()) { @@ -485,8 +494,10 @@ public synchronized void close() { if (parent != null && reservation > allocated) { parent.releaseBytes(reservation - allocated); } - String msg = String.format("Memory was leaked by query. Memory leaked: (%d)\n%s%s", allocated, - outstandingChildAllocators.toString(), toString()); + String msg = + String.format( + "Memory was leaked by query. Memory leaked: (%d)\n%s%s", + allocated, outstandingChildAllocators.toString(), toString()); logger.error(msg); throw new IllegalStateException(msg); } @@ -505,14 +516,12 @@ public synchronized void close() { } logger.debug(String.format("closed allocator[%s].", name)); } - - } @Override public String toString() { - final Verbosity verbosity = logger.isTraceEnabled() ? Verbosity.LOG_WITH_STACKTRACE - : Verbosity.BASIC; + final Verbosity verbosity = + logger.isTraceEnabled() ? Verbosity.LOG_WITH_STACKTRACE : Verbosity.BASIC; final StringBuilder sb = new StringBuilder(); print(sb, 0, verbosity); return sb.toString(); @@ -520,8 +529,7 @@ public String toString() { /** * Provide a verbose string of the current allocator state. Includes the state of all child - * allocators, along with - * historical logs of each object and including stacktraces. + * allocators, along with historical logs of each object and including stacktraces. * * @return A Verbose string of current allocator state. */ @@ -549,12 +557,12 @@ void verifyAllocator() { } /** - * Verifies the accounting state of the allocator (Only works for DEBUG) - * This overload is used for recursive calls, allowing for checking - * that ArrowBufs are unique across all allocators that are checked. + * Verifies the accounting state of the allocator (Only works for DEBUG) This overload is used for + * recursive calls, allowing for checking that ArrowBufs are unique across all allocators that are + * checked. * * @param buffersSeen a map of buffers that have already been seen when walking a tree of - * allocators + * allocators * @throws IllegalStateException when any problems are found */ private void verifyAllocator( @@ -596,15 +604,20 @@ private void verifyAllocator( } logger.debug("allocator[" + name + "] child event logs END"); throw new IllegalStateException( - "Child allocators own more memory (" + childTotal + ") than their parent (name = " + - name + " ) has allocated (" + getAllocatedMemory() + ')'); + "Child allocators own more memory (" + + childTotal + + ") than their parent (name = " + + name + + " ) has allocated (" + + getAllocatedMemory() + + ')'); } // Furthermore, the amount I've allocated should be that plus buffers I've allocated. long bufferTotal = 0; - final Set<@KeyFor("this.childLedgers") BufferLedger> ledgerSet = childLedgers != null ? - childLedgers.keySet() : null; + final Set<@KeyFor("this.childLedgers") BufferLedger> ledgerSet = + childLedgers != null ? childLedgers.keySet() : null; if (ledgerSet != null) { for (final BufferLedger ledger : ledgerSet) { if (!ledger.isOwningLedger()) { @@ -618,8 +631,8 @@ private void verifyAllocator( */ final BaseAllocator otherOwner = buffersSeen.get(am); if (otherOwner != null) { - throw new IllegalStateException("This allocator's ArrowBuf already owned by another " + - "allocator"); + throw new IllegalStateException( + "This allocator's ArrowBuf already owned by another " + "allocator"); } buffersSeen.put(am, this); @@ -628,8 +641,8 @@ private void verifyAllocator( } // Preallocated space has to be accounted for - final Set<@KeyFor("this.reservations") Reservation> reservationSet = reservations != null ? - reservations.keySet() : null; + final Set<@KeyFor("this.reservations") Reservation> reservationSet = + reservations != null ? reservations.keySet() : null; long reservedTotal = 0; if (reservationSet != null) { for (final Reservation reservation : reservationSet) { @@ -687,14 +700,15 @@ private void verifyAllocator( final long allocated2 = getAllocatedMemory(); if (allocated2 != allocated) { - throw new IllegalStateException(String.format( - "allocator[%s]: allocated t1 (%d) + allocated t2 (%d). Someone released memory while in verification.", - name, allocated, allocated2)); - + throw new IllegalStateException( + String.format( + "allocator[%s]: allocated t1 (%d) + allocated t2 (%d). Someone released memory while in verification.", + name, allocated, allocated2)); } - throw new IllegalStateException(String.format( - "allocator[%s]: buffer space (%d) + prealloc space (%d) + child space (%d) != allocated (%d)", - name, bufferTotal, reservedTotal, childTotal, allocated)); + throw new IllegalStateException( + String.format( + "allocator[%s]: buffer space (%d) + prealloc space (%d) + child space (%d) != allocated (%d)", + name, bufferTotal, reservedTotal, childTotal, allocated)); } } } @@ -702,37 +716,39 @@ private void verifyAllocator( void print(StringBuilder sb, int level, Verbosity verbosity) { CommonUtil.indent(sb, level) - .append("Allocator(") - .append(name) - .append(") ") - .append(reservation) - .append('/') - .append(getAllocatedMemory()) - .append('/') - .append(getPeakMemoryAllocation()) - .append('/') - .append(getLimit()) - .append(" (res/actual/peak/limit)") - .append('\n'); + .append("Allocator(") + .append(name) + .append(") ") + .append(reservation) + .append('/') + .append(getAllocatedMemory()) + .append('/') + .append(getPeakMemoryAllocation()) + .append('/') + .append(getLimit()) + .append(" (res/actual/peak/limit)") + .append('\n'); if (DEBUG) { - CommonUtil.indent(sb, level + 1).append(String.format("child allocators: %d\n", childAllocators.size())); + CommonUtil.indent(sb, level + 1) + .append(String.format("child allocators: %d\n", childAllocators.size())); for (BaseAllocator child : childAllocators.keySet()) { child.print(sb, level + 2, verbosity); } - CommonUtil.indent(sb, level + 1).append(String.format("ledgers: %d\n", childLedgers != null ? - childLedgers.size() : 0)); + CommonUtil.indent(sb, level + 1) + .append(String.format("ledgers: %d\n", childLedgers != null ? childLedgers.size() : 0)); if (childLedgers != null) { for (BufferLedger ledger : childLedgers.keySet()) { ledger.print(sb, level + 2, verbosity); } } - final Set<@KeyFor("this.reservations") Reservation> reservations = this.reservations != null ? - this.reservations.keySet() : null; - CommonUtil.indent(sb, level + 1).append(String.format("reservations: %d\n", - reservations != null ? reservations.size() : 0)); + final Set<@KeyFor("this.reservations") Reservation> reservations = + this.reservations != null ? this.reservations.keySet() : null; + CommonUtil.indent(sb, level + 1) + .append( + String.format("reservations: %d\n", reservations != null ? reservations.size() : 0)); if (reservations != null) { for (final Reservation reservation : reservations) { if (verbosity.includeHistoricalLog) { @@ -742,13 +758,12 @@ void print(StringBuilder sb, int level, Verbosity verbosity) { } } } - } - } - private void dumpBuffers(final StringBuilder sb, - final @Nullable Set<@KeyFor("this.childLedgers") BufferLedger> ledgerSet) { + private void dumpBuffers( + final StringBuilder sb, + final @Nullable Set<@KeyFor("this.childLedgers") BufferLedger> ledgerSet) { if (ledgerSet != null) { for (final BufferLedger ledger : ledgerSet) { if (!ledger.isOwningLedger()) { @@ -764,14 +779,12 @@ private void dumpBuffers(final StringBuilder sb, } } - /** - * Enum for logging verbosity. - */ + /** Enum for logging verbosity. */ public enum Verbosity { BASIC(false, false), // only include basic information LOG(true, false), // include basic LOG_WITH_STACKTRACE(true, true) // - ; + ; public final boolean includeHistoricalLog; public final boolean includeStackTraces; @@ -789,12 +802,9 @@ public enum Verbosity { */ public static Config defaultConfig() { return DEFAULT_CONFIG; - } - /** - * Returns a builder class for configuring BaseAllocator's options. - */ + /** Returns a builder class for configuring BaseAllocator's options. */ public static ImmutableConfig.Builder configBuilder() { return ImmutableConfig.builder(); } @@ -804,47 +814,37 @@ public RoundingPolicy getRoundingPolicy() { return roundingPolicy; } - /** - * Config class of {@link BaseAllocator}. - */ + /** Config class of {@link BaseAllocator}. */ @Value.Immutable abstract static class Config { - /** - * Factory for creating {@link AllocationManager} instances. - */ + /** Factory for creating {@link AllocationManager} instances. */ @Value.Default AllocationManager.Factory getAllocationManagerFactory() { return DefaultAllocationManagerOption.getDefaultAllocationManagerFactory(); } - /** - * Listener callback. Must be non-null. - */ + /** Listener callback. Must be non-null. */ @Value.Default AllocationListener getListener() { return AllocationListener.NOOP; } - /** - * Initial reservation size (in bytes) for this allocator. - */ + /** Initial reservation size (in bytes) for this allocator. */ @Value.Default long getInitReservation() { return 0; } /** - * Max allocation size (in bytes) for this allocator, allocations past this limit fail. - * Can be modified after construction. + * Max allocation size (in bytes) for this allocator, allocations past this limit fail. Can be + * modified after construction. */ @Value.Default long getMaxAllocation() { return Long.MAX_VALUE; } - /** - * The policy for rounding the buffer size. - */ + /** The policy for rounding the buffer size. */ @Value.Default RoundingPolicy getRoundingPolicy() { return DefaultRoundingPolicy.DEFAULT_ROUNDING_POLICY; @@ -852,8 +852,8 @@ RoundingPolicy getRoundingPolicy() { } /** - * Implementation of {@link AllocationReservation} that supports - * history tracking under {@linkplain #DEBUG} is true. + * Implementation of {@link AllocationReservation} that supports history tracking under + * {@linkplain #DEBUG} is true. */ public class Reservation implements AllocationReservation { @@ -865,14 +865,16 @@ public class Reservation implements AllocationReservation { /** * Creates a new reservation. * - *

If {@linkplain #DEBUG} is true this will capture a historical - * log of events relevant to this Reservation. + *

If {@linkplain #DEBUG} is true this will capture a historical log of events relevant to + * this Reservation. */ - @SuppressWarnings("nullness:argument")//to handle null assignment on third party dependency: System.identityHashCode + @SuppressWarnings("nullness:argument") // to handle null assignment on third party dependency: + // System.identityHashCode public Reservation() { if (DEBUG) { - historicalLog = new HistoricalLog("Reservation[allocator[%s], %d]", name, System - .identityHashCode(this)); + historicalLog = + new HistoricalLog( + "Reservation[allocator[%s], %d]", name, System.identityHashCode(this)); historicalLog.recordEvent("created"); synchronized (DEBUG_LOCK) { if (reservations != null) { @@ -889,8 +891,10 @@ public boolean add(final int nBytes) { assertOpen(); Preconditions.checkArgument(nBytes >= 0, "nBytes(%d) < 0", nBytes); - Preconditions.checkState(!closed, "Attempt to increase reservation after reservation has been closed"); - Preconditions.checkState(!used, "Attempt to increase reservation after reservation has been used"); + Preconditions.checkState( + !closed, "Attempt to increase reservation after reservation has been closed"); + Preconditions.checkState( + !used, "Attempt to increase reservation after reservation has been used"); // we round up to next power of two since all reservations are done in powers of two. This // may overestimate the @@ -954,8 +958,9 @@ public void close() { final StringBuilder sb = new StringBuilder(); print(sb, 0, Verbosity.LOG_WITH_STACKTRACE); logger.debug(sb.toString()); - throw new IllegalStateException(String.format("Didn't find closing reservation[%d]", - System.identityHashCode(this))); + throw new IllegalStateException( + String.format( + "Didn't find closing reservation[%d]", System.identityHashCode(this))); } if (historicalLog != null) { @@ -1001,7 +1006,7 @@ private ArrowBuf allocate(int nBytes) { * The reservation already added the requested bytes to the allocators owned and allocated * bytes via reserve(). * This ensures that they can't go away. But when we ask for the buffer here, that will add - * to the allocated bytes + * to the allocated bytes * as well, so we need to return the same number back to avoid double-counting them. */ try { @@ -1009,8 +1014,8 @@ private ArrowBuf allocate(int nBytes) { listener.onAllocation(nBytes); if (historicalLog != null) { - historicalLog.recordEvent("allocate() => %s", String.format("ArrowBuf[%d]", arrowBuf - .getId())); + historicalLog.recordEvent( + "allocate() => %s", String.format("ArrowBuf[%d]", arrowBuf.getId())); } success = true; return arrowBuf; @@ -1035,6 +1040,5 @@ private void releaseReservation(int nBytes) { historicalLog.recordEvent("releaseReservation(%d)", nBytes); } } - } } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BoundsChecking.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BoundsChecking.java index bbf7ff34dc9..9c1260f65a5 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BoundsChecking.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BoundsChecking.java @@ -20,13 +20,11 @@ /** * Configuration class to determine if bounds checking should be turned on or off. * - *

- * Bounds checking is on by default. You can disable it by setting either the system property or - * the environmental variable to "true". The system property can be "arrow.enable_unsafe_memory_access" - * or "drill.enable_unsafe_memory_access". The latter is deprecated. The environmental variable is named - * "ARROW_ENABLE_UNSAFE_MEMORY_ACCESS". - * When both the system property and the environmental variable are set, the system property takes precedence. - *

+ *

Bounds checking is on by default. You can disable it by setting either the system property or + * the environmental variable to "true". The system property can be + * "arrow.enable_unsafe_memory_access" or "drill.enable_unsafe_memory_access". The latter is + * deprecated. The environmental variable is named "ARROW_ENABLE_UNSAFE_MEMORY_ACCESS". When both + * the system property and the environmental variable are set, the system property takes precedence. */ public class BoundsChecking { @@ -37,9 +35,11 @@ public class BoundsChecking { String envProperty = System.getenv("ARROW_ENABLE_UNSAFE_MEMORY_ACCESS"); String oldProperty = System.getProperty("drill.enable_unsafe_memory_access"); if (oldProperty != null) { - logger.warn("\"drill.enable_unsafe_memory_access\" has been renamed to \"arrow.enable_unsafe_memory_access\""); - logger.warn("\"arrow.enable_unsafe_memory_access\" can be set to: " + - " true (to not check) or false (to check, default)"); + logger.warn( + "\"drill.enable_unsafe_memory_access\" has been renamed to \"arrow.enable_unsafe_memory_access\""); + logger.warn( + "\"arrow.enable_unsafe_memory_access\" can be set to: " + + " true (to not check) or false (to check, default)"); } String newProperty = System.getProperty("arrow.enable_unsafe_memory_access"); @@ -57,7 +57,5 @@ public class BoundsChecking { BOUNDS_CHECKING_ENABLED = !"true".equals(unsafeFlagValue); } - private BoundsChecking() { - } - + private BoundsChecking() {} } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferAllocator.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferAllocator.java index c279e18f1ea..4c67b4bfc58 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferAllocator.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferAllocator.java @@ -18,21 +18,17 @@ package org.apache.arrow.memory; import java.util.Collection; - import org.apache.arrow.memory.rounding.DefaultRoundingPolicy; import org.apache.arrow.memory.rounding.RoundingPolicy; import org.checkerframework.checker.nullness.qual.Nullable; -/** - * Wrapper class to deal with byte buffer allocation. Ensures users only use designated methods. - */ +/** Wrapper class to deal with byte buffer allocation. Ensures users only use designated methods. */ public interface BufferAllocator extends AutoCloseable { /** - * Allocate a new or reused buffer of the provided size. Note that the buffer may technically - * be larger than the - * requested size for rounding purposes. However, the buffer's capacity will be set to the - * configured size. + * Allocate a new or reused buffer of the provided size. Note that the buffer may technically be + * larger than the requested size for rounding purposes. However, the buffer's capacity will be + * set to the configured size. * * @param size The size in bytes. * @return a new ArrowBuf, or null if the request can't be satisfied @@ -41,12 +37,11 @@ public interface BufferAllocator extends AutoCloseable { ArrowBuf buffer(long size); /** - * Allocate a new or reused buffer of the provided size. Note that the buffer may technically - * be larger than the - * requested size for rounding purposes. However, the buffer's capacity will be set to the - * configured size. + * Allocate a new or reused buffer of the provided size. Note that the buffer may technically be + * larger than the requested size for rounding purposes. However, the buffer's capacity will be + * set to the configured size. * - * @param size The size in bytes. + * @param size The size in bytes. * @param manager A buffer manager to manage reallocation. * @return a new ArrowBuf, or null if the request can't be satisfied * @throws OutOfMemoryException if buffer cannot be allocated @@ -54,8 +49,8 @@ public interface BufferAllocator extends AutoCloseable { ArrowBuf buffer(long size, BufferManager manager); /** - * Get the root allocator of this allocator. If this allocator is already a root, return - * this directly. + * Get the root allocator of this allocator. If this allocator is already a root, return this + * directly. * * @return The root allocator */ @@ -64,9 +59,9 @@ public interface BufferAllocator extends AutoCloseable { /** * Create a new child allocator. * - * @param name the name of the allocator. + * @param name the name of the allocator. * @param initReservation the initial space reservation (obtained from this allocator) - * @param maxAllocation maximum amount of space the new allocator can allocate + * @param maxAllocation maximum amount of space the new allocator can allocate * @return the new allocator, or null if it can't be created */ BufferAllocator newChildAllocator(String name, long initReservation, long maxAllocation); @@ -74,23 +69,20 @@ public interface BufferAllocator extends AutoCloseable { /** * Create a new child allocator. * - * @param name the name of the allocator. - * @param listener allocation listener for the newly created child + * @param name the name of the allocator. + * @param listener allocation listener for the newly created child * @param initReservation the initial space reservation (obtained from this allocator) - * @param maxAllocation maximum amount of space the new allocator can allocate + * @param maxAllocation maximum amount of space the new allocator can allocate * @return the new allocator, or null if it can't be created */ BufferAllocator newChildAllocator( - String name, - AllocationListener listener, - long initReservation, - long maxAllocation); + String name, AllocationListener listener, long initReservation, long maxAllocation); /** * Close and release all buffers generated from this buffer pool. * - *

When assertions are on, complains if there are any outstanding buffers; to avoid - * that, release all buffers before the allocator is closed.

+ *

When assertions are on, complains if there are any outstanding buffers; to avoid that, + * release all buffers before the allocator is closed. */ @Override void close(); @@ -131,8 +123,8 @@ BufferAllocator newChildAllocator( long getPeakMemoryAllocation(); /** - * Returns the amount of memory that can probably be allocated at this moment - * without exceeding this or any parents allocation maximum. + * Returns the amount of memory that can probably be allocated at this moment without exceeding + * this or any parents allocation maximum. * * @return Headroom in bytes */ @@ -146,7 +138,6 @@ BufferAllocator newChildAllocator( */ boolean forceAllocate(long size); - /** * Release bytes from this allocator. * @@ -157,8 +148,8 @@ BufferAllocator newChildAllocator( /** * Returns the allocation listener used by this allocator. * - * @return the {@link AllocationListener} instance. Or {@link AllocationListener#NOOP} by default if no listener - * is configured when this allocator was created. + * @return the {@link AllocationListener} instance. Or {@link AllocationListener#NOOP} by default + * if no listener is configured when this allocator was created. */ AllocationListener getListener(); @@ -177,8 +168,8 @@ BufferAllocator newChildAllocator( Collection getChildAllocators(); /** - * Create an allocation reservation. A reservation is a way of building up - * a request for a buffer whose size is not known in advance. See + * Create an allocation reservation. A reservation is a way of building up a request for a buffer + * whose size is not known in advance. See * * @return the newly created reservation * @see AllocationReservation @@ -186,10 +177,9 @@ BufferAllocator newChildAllocator( AllocationReservation newReservation(); /** - * Get a reference to the empty buffer associated with this allocator. Empty buffers are - * special because we don't - * worry about them leaking or managing reference counts on them since they don't actually - * point to any memory. + * Get a reference to the empty buffer associated with this allocator. Empty buffers are special + * because we don't worry about them leaking or managing reference counts on them since they don't + * actually point to any memory. * * @return the empty buffer */ @@ -197,8 +187,7 @@ BufferAllocator newChildAllocator( /** * Return the name of this allocator. This is a human readable name that can help debugging. - * Typically provides - * coordinates about where this allocator was created + * Typically provides coordinates about where this allocator was created * * @return the name of the allocator */ @@ -206,9 +195,8 @@ BufferAllocator newChildAllocator( /** * Return whether or not this allocator (or one if its parents) is over its limits. In the case - * that an allocator is - * over its limit, all consumers of that allocator should aggressively try to address the - * overlimit situation. + * that an allocator is over its limit, all consumers of that allocator should aggressively try to + * address the overlimit situation. * * @return whether or not this allocator (or one if its parents) is over its limits */ @@ -216,8 +204,7 @@ BufferAllocator newChildAllocator( /** * Return a verbose string describing this allocator. If in DEBUG mode, this will also include - * relevant stacktraces - * and historical logs for underlying objects + * relevant stacktraces and historical logs for underlying objects * * @return A very verbose description of the allocator hierarchy. */ @@ -225,14 +212,11 @@ BufferAllocator newChildAllocator( /** * Asserts (using java assertions) that the provided allocator is currently open. If assertions - * are disabled, this is - * a no-op. + * are disabled, this is a no-op. */ void assertOpen(); - /** - * Gets the rounding policy of the allocator. - */ + /** Gets the rounding policy of the allocator. */ default RoundingPolicy getRoundingPolicy() { return DefaultRoundingPolicy.DEFAULT_ROUNDING_POLICY; } @@ -240,12 +224,12 @@ default RoundingPolicy getRoundingPolicy() { /** * EXPERIMENTAL: Wrap an allocation created outside this BufferAllocator. * - *

This is useful to integrate allocations from native code into the same memory management framework as - * Java-allocated buffers, presenting users a consistent API. The created buffer will be tracked by this allocator - * and can be transferred like Java-allocated buffers. + *

This is useful to integrate allocations from native code into the same memory management + * framework as Java-allocated buffers, presenting users a consistent API. The created buffer will + * be tracked by this allocator and can be transferred like Java-allocated buffers. * - *

The underlying allocation will be closed when all references to the buffer are released. If this method throws, - * the underlying allocation will also be closed. + *

The underlying allocation will be closed when all references to the buffer are released. If + * this method throws, the underlying allocation will also be closed. * * @param allocation The underlying allocation. */ diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferLedger.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferLedger.java index 1ca3e08ecf0..e17cd35d478 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferLedger.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferLedger.java @@ -20,21 +20,19 @@ import java.util.IdentityHashMap; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; - import org.apache.arrow.memory.util.CommonUtil; import org.apache.arrow.memory.util.HistoricalLog; import org.apache.arrow.util.Preconditions; import org.checkerframework.checker.nullness.qual.Nullable; /** - * The reference manager that binds an {@link AllocationManager} to - * {@link BufferAllocator} and a set of {@link ArrowBuf}. The set of - * ArrowBufs managed by this reference manager share a common + * The reference manager that binds an {@link AllocationManager} to {@link BufferAllocator} and a + * set of {@link ArrowBuf}. The set of ArrowBufs managed by this reference manager share a common * fate (same reference count). */ public class BufferLedger implements ValueWithKeyIncluded, ReferenceManager { private final @Nullable IdentityHashMap buffers = - BaseAllocator.DEBUG ? new IdentityHashMap<>() : null; + BaseAllocator.DEBUG ? new IdentityHashMap<>() : null; private static final AtomicLong LEDGER_ID_GENERATOR = new AtomicLong(0); // unique ID assigned to each ledger private final long ledgerId = LEDGER_ID_GENERATOR.incrementAndGet(); @@ -45,8 +43,9 @@ public class BufferLedger implements ValueWithKeyIncluded, Refe private final BufferAllocator allocator; private final AllocationManager allocationManager; private final @Nullable HistoricalLog historicalLog = - BaseAllocator.DEBUG ? new HistoricalLog(BaseAllocator.DEBUG_LOG_LENGTH, - "BufferLedger[%d]", 1) : null; + BaseAllocator.DEBUG + ? new HistoricalLog(BaseAllocator.DEBUG_LOG_LENGTH, "BufferLedger[%d]", 1) + : null; private volatile long lDestructionTime = 0; BufferLedger(final BufferAllocator allocator, final AllocationManager allocationManager) { @@ -64,6 +63,7 @@ public BufferAllocator getKey() { /** * Get the buffer allocator associated with this reference manager. + * * @return buffer allocator */ @Override @@ -73,6 +73,7 @@ public BufferAllocator getAllocator() { /** * Get this ledger's reference count. + * * @return reference count */ @Override @@ -81,23 +82,21 @@ public int getRefCount() { } /** - * Increment the ledger's reference count for the associated - * underlying memory chunk. All ArrowBufs managed by this ledger - * will share the ref count. + * Increment the ledger's reference count for the associated underlying memory chunk. All + * ArrowBufs managed by this ledger will share the ref count. */ void increment() { bufRefCnt.incrementAndGet(); } /** - * Decrement the ledger's reference count by 1 for the associated underlying - * memory chunk. If the reference count drops to 0, it implies that - * no ArrowBufs managed by this reference manager need access to the memory - * chunk. In that case, the ledger should inform the allocation manager - * about releasing its ownership for the chunk. Whether or not the memory - * chunk will be released is something that {@link AllocationManager} will - * decide since tracks the usage of memory chunk across multiple reference - * managers and allocators. + * Decrement the ledger's reference count by 1 for the associated underlying memory chunk. If the + * reference count drops to 0, it implies that no ArrowBufs managed by this reference manager need + * access to the memory chunk. In that case, the ledger should inform the allocation manager about + * releasing its ownership for the chunk. Whether or not the memory chunk will be released is + * something that {@link AllocationManager} will decide since tracks the usage of memory chunk + * across multiple reference managers and allocators. + * * @return true if the new ref count has dropped to 0, false otherwise */ @Override @@ -106,26 +105,24 @@ public boolean release() { } /** - * Decrement the ledger's reference count for the associated underlying - * memory chunk. If the reference count drops to 0, it implies that - * no ArrowBufs managed by this reference manager need access to the memory - * chunk. In that case, the ledger should inform the allocation manager - * about releasing its ownership for the chunk. Whether or not the memory - * chunk will be released is something that {@link AllocationManager} will - * decide since tracks the usage of memory chunk across multiple reference - * managers and allocators. + * Decrement the ledger's reference count for the associated underlying memory chunk. If the + * reference count drops to 0, it implies that no ArrowBufs managed by this reference manager need + * access to the memory chunk. In that case, the ledger should inform the allocation manager about + * releasing its ownership for the chunk. Whether or not the memory chunk will be released is + * something that {@link AllocationManager} will decide since tracks the usage of memory chunk + * across multiple reference managers and allocators. + * * @param decrement amount to decrease the reference count by * @return true if the new ref count has dropped to 0, false otherwise */ @Override public boolean release(int decrement) { - Preconditions.checkState(decrement >= 1, - "ref count decrement should be greater than or equal to 1"); + Preconditions.checkState( + decrement >= 1, "ref count decrement should be greater than or equal to 1"); // decrement the ref count final int refCnt = decrement(decrement); if (historicalLog != null) { - historicalLog.recordEvent("release(%d). original value: %d", - decrement, refCnt + decrement); + historicalLog.recordEvent("release(%d). original value: %d", decrement, refCnt + decrement); } // the new ref count should be >= 0 Preconditions.checkState(refCnt >= 0, "RefCnt has gone negative"); @@ -133,14 +130,12 @@ public boolean release(int decrement) { } /** - * Decrement the ledger's reference count for the associated underlying - * memory chunk. If the reference count drops to 0, it implies that - * no ArrowBufs managed by this reference manager need access to the memory - * chunk. In that case, the ledger should inform the allocation manager - * about releasing its ownership for the chunk. Whether or not the memory - * chunk will be released is something that {@link AllocationManager} will - * decide since tracks the usage of memory chunk across multiple reference - * managers and allocators. + * Decrement the ledger's reference count for the associated underlying memory chunk. If the + * reference count drops to 0, it implies that no ArrowBufs managed by this reference manager need + * access to the memory chunk. In that case, the ledger should inform the allocation manager about + * releasing its ownership for the chunk. Whether or not the memory chunk will be released is + * something that {@link AllocationManager} will decide since tracks the usage of memory chunk + * across multiple reference managers and allocators. * * @param decrement amount to decrease the reference count by * @return the new reference count @@ -161,18 +156,15 @@ private int decrement(int decrement) { return outcome; } - /** - * Increment the ledger's reference count for associated - * underlying memory chunk by 1. - */ + /** Increment the ledger's reference count for associated underlying memory chunk by 1. */ @Override public void retain() { retain(1); } /** - * Increment the ledger's reference count for associated - * underlying memory chunk by the given amount. + * Increment the ledger's reference count for associated underlying memory chunk by the given + * amount. * * @param increment amount to increase the reference count by */ @@ -187,21 +179,19 @@ public void retain(int increment) { } /** - * Derive a new ArrowBuf from a given source ArrowBuf. The new derived - * ArrowBuf will share the same reference count as rest of the ArrowBufs - * associated with this ledger. This operation is typically used for - * slicing -- creating new ArrowBufs from a compound ArrowBuf starting at - * a particular index in the underlying memory and having access to a - * particular length (in bytes) of data in memory chunk. - *

- * This method is also used as a helper for transferring ownership and retain to target + * Derive a new ArrowBuf from a given source ArrowBuf. The new derived ArrowBuf will share the + * same reference count as rest of the ArrowBufs associated with this ledger. This operation is + * typically used for slicing -- creating new ArrowBufs from a compound ArrowBuf starting at a + * particular index in the underlying memory and having access to a particular length (in bytes) + * of data in memory chunk. + * + *

This method is also used as a helper for transferring ownership and retain to target * allocator. - *

+ * * @param sourceBuffer source ArrowBuf - * @param index index (relative to source ArrowBuf) new ArrowBuf should be - * derived from - * @param length length (bytes) of data in underlying memory that derived buffer will - * have access to in underlying memory + * @param index index (relative to source ArrowBuf) new ArrowBuf should be derived from + * @param length length (bytes) of data in underlying memory that derived buffer will have access + * to in underlying memory * @return derived buffer */ @Override @@ -226,11 +216,13 @@ public ArrowBuf deriveBuffer(final ArrowBuf sourceBuffer, long index, long lengt final long derivedBufferAddress = sourceBuffer.memoryAddress() + index; // create new ArrowBuf - final ArrowBuf derivedBuf = new ArrowBuf( + final ArrowBuf derivedBuf = + new ArrowBuf( this, null, length, // length (in bytes) in the underlying memory chunk for this new ArrowBuf - derivedBufferAddress // starting byte address in the underlying memory for this new ArrowBuf + derivedBufferAddress // starting byte address in the underlying memory for this new + // ArrowBuf ); // logging @@ -238,16 +230,15 @@ public ArrowBuf deriveBuffer(final ArrowBuf sourceBuffer, long index, long lengt } /** - * Used by an allocator to create a new ArrowBuf. This is provided - * as a helper method for the allocator when it allocates a new memory chunk - * using a new instance of allocation manager and creates a new reference manager - * too. + * Used by an allocator to create a new ArrowBuf. This is provided as a helper method for the + * allocator when it allocates a new memory chunk using a new instance of allocation manager and + * creates a new reference manager too. * - * @param length The length in bytes that this ArrowBuf will provide access to. - * @param manager An optional BufferManager argument that can be used to manage expansion of - * this ArrowBuf - * @return A new ArrowBuf that shares references with all ArrowBufs associated - * with this BufferLedger + * @param length The length in bytes that this ArrowBuf will provide access to. + * @param manager An optional BufferManager argument that can be used to manage expansion of this + * ArrowBuf + * @return A new ArrowBuf that shares references with all ArrowBufs associated with this + * BufferLedger */ ArrowBuf newArrowBuf(final long length, final @Nullable BufferManager manager) { allocator.assertOpen(); @@ -265,9 +256,12 @@ ArrowBuf newArrowBuf(final long length, final @Nullable BufferManager manager) { private ArrowBuf loggingArrowBufHistoricalLog(ArrowBuf buf) { if (historicalLog != null) { historicalLog.recordEvent( - "ArrowBuf(BufferLedger, BufferAllocator[%s], " + - "UnsafeDirectLittleEndian[identityHashCode == " + "%d](%s)) => ledger hc == %d", - allocator.getName(), System.identityHashCode(buf), buf.toString(), + "ArrowBuf(BufferLedger, BufferAllocator[%s], " + + "UnsafeDirectLittleEndian[identityHashCode == " + + "%d](%s)) => ledger hc == %d", + allocator.getName(), + System.identityHashCode(buf), + buf.toString(), System.identityHashCode(this)); Preconditions.checkState(buffers != null, "IdentityHashMap of buffers must not be null"); synchronized (buffers) { @@ -281,15 +275,15 @@ private ArrowBuf loggingArrowBufHistoricalLog(ArrowBuf buf) { /** * Create a new ArrowBuf that is associated with an alternative allocator for the purposes of * memory ownership and accounting. This has no impact on the reference counting for the current - * ArrowBuf except in the situation where the passed in Allocator is the same as the current buffer. - *

- * This operation has no impact on the reference count of this ArrowBuf. The newly created + * ArrowBuf except in the situation where the passed in Allocator is the same as the current + * buffer. + * + *

This operation has no impact on the reference count of this ArrowBuf. The newly created * ArrowBuf with either have a reference count of 1 (in the case that this is the first time this - * memory is being associated with the target allocator or in other words allocation manager currently - * doesn't hold a mapping for the target allocator) or the current value of the reference count for - * the target allocator-reference manager combination + 1 in the case that the provided allocator - * already had an association to this underlying memory. - *

+ * memory is being associated with the target allocator or in other words allocation manager + * currently doesn't hold a mapping for the target allocator) or the current value of the + * reference count for the target allocator-reference manager combination + 1 in the case that the + * provided allocator already had an association to this underlying memory. * * @param srcBuffer source ArrowBuf * @param target The target allocator to create an association with. @@ -319,20 +313,21 @@ public ArrowBuf retain(final ArrowBuf srcBuffer, BufferAllocator target) { } /** - * Transfer any balance the current ledger has to the target ledger. In the case - * that the current ledger holds no memory, no transfer is made to the new ledger. + * Transfer any balance the current ledger has to the target ledger. In the case that the current + * ledger holds no memory, no transfer is made to the new ledger. * * @param targetReferenceManager The ledger to transfer ownership account to. * @return Whether transfer fit within target ledgers limits. */ boolean transferBalance(final @Nullable ReferenceManager targetReferenceManager) { - Preconditions.checkArgument(targetReferenceManager != null, - "Expecting valid target reference manager"); + Preconditions.checkArgument( + targetReferenceManager != null, "Expecting valid target reference manager"); boolean overlimit = false; if (targetReferenceManager != null) { final BufferAllocator targetAllocator = targetReferenceManager.getAllocator(); - Preconditions.checkArgument(allocator.getRoot() == targetAllocator.getRoot(), - "You can only transfer between two allocators that share the same root."); + Preconditions.checkArgument( + allocator.getRoot() == targetAllocator.getRoot(), + "You can only transfer between two allocators that share the same root."); allocator.assertOpen(); targetReferenceManager.getAllocator().assertOpen(); @@ -354,8 +349,8 @@ boolean transferBalance(final @Nullable ReferenceManager targetReferenceManager) } if (BaseAllocator.DEBUG && this.historicalLog != null) { - this.historicalLog.recordEvent("transferBalance(%s)", - targetReferenceManager.getAllocator().getName()); + this.historicalLog.recordEvent( + "transferBalance(%s)", targetReferenceManager.getAllocator().getName()); } overlimit = targetAllocator.forceAllocate(allocationManager.getSize()); @@ -370,32 +365,30 @@ boolean transferBalance(final @Nullable ReferenceManager targetReferenceManager) } /** - * Transfer the memory accounting ownership of this ArrowBuf to another allocator. - * This will generate a new ArrowBuf that carries an association with the underlying memory - * of this ArrowBuf. If this ArrowBuf is connected to the owning BufferLedger of this memory, - * that memory ownership/accounting will be transferred to the target allocator. If this - * ArrowBuf does not currently own the memory underlying it (and is only associated with it), - * this does not transfer any ownership to the newly created ArrowBuf. - *

- * This operation has no impact on the reference count of this ArrowBuf. The newly created - * ArrowBuf with either have a reference count of 1 (in the case that this is the first time - * this memory is being associated with the new allocator) or the current value of the reference - * count for the other AllocationManager/BufferLedger combination + 1 in the case that the provided + * Transfer the memory accounting ownership of this ArrowBuf to another allocator. This will + * generate a new ArrowBuf that carries an association with the underlying memory of this + * ArrowBuf. If this ArrowBuf is connected to the owning BufferLedger of this memory, that memory + * ownership/accounting will be transferred to the target allocator. If this ArrowBuf does not + * currently own the memory underlying it (and is only associated with it), this does not transfer + * any ownership to the newly created ArrowBuf. + * + *

This operation has no impact on the reference count of this ArrowBuf. The newly created + * ArrowBuf with either have a reference count of 1 (in the case that this is the first time this + * memory is being associated with the new allocator) or the current value of the reference count + * for the other AllocationManager/BufferLedger combination + 1 in the case that the provided * allocator already had an association to this underlying memory. - *

- *

- * Transfers will always succeed, even if that puts the other allocator into an overlimit + * + *

Transfers will always succeed, even if that puts the other allocator into an overlimit * situation. This is possible due to the fact that the original owning allocator may have * allocated this memory out of a local reservation whereas the target allocator may need to * allocate new memory from a parent or RootAllocator. This operation is done n a mostly-lockless * but consistent manner. As such, the overlimit==true situation could occur slightly prematurely * to an actual overlimit==true condition. This is simply conservative behavior which means we may * return overlimit slightly sooner than is necessary. - *

* * @param target The allocator to transfer ownership to. * @return A new transfer result with the impact of the transfer (whether it was overlimit) as - * well as the newly created ArrowBuf. + * well as the newly created ArrowBuf. */ @Override public TransferResult transferOwnership(final ArrowBuf srcBuffer, final BufferAllocator target) { @@ -416,9 +409,7 @@ public TransferResult transferOwnership(final ArrowBuf srcBuffer, final BufferAl return new TransferResult(allocationFit, targetArrowBuf); } - /** - * The outcome of a Transfer. - */ + /** The outcome of a Transfer. */ public class TransferResult implements OwnershipTransferResult { // Whether this transfer fit within the target allocator's capacity. @@ -445,6 +436,7 @@ public boolean getAllocationFit() { /** * Total size (in bytes) of memory underlying this reference manager. + * * @return Size (in bytes) of the memory chunk */ @Override @@ -453,9 +445,10 @@ public long getSize() { } /** - * How much memory is accounted for by this ledger. This is either getSize() - * if this is the owning ledger for the memory or zero in the case that this - * is not the owning ledger associated with this memory. + * How much memory is accounted for by this ledger. This is either getSize() if this is the owning + * ledger for the memory or zero in the case that this is not the owning ledger associated with + * this memory. + * * @return Amount of accounted(owned) memory associated with this ledger. */ @Override @@ -472,35 +465,33 @@ public long getAccountedSize() { /** * Print the current ledger state to the provided StringBuilder. * - * @param sb The StringBuilder to populate. - * @param indent The level of indentation to position the data. + * @param sb The StringBuilder to populate. + * @param indent The level of indentation to position the data. * @param verbosity The level of verbosity to print. */ void print(StringBuilder sb, int indent, BaseAllocator.Verbosity verbosity) { CommonUtil.indent(sb, indent) - .append("ledger[") - .append(ledgerId) - .append("] allocator: ") - .append(allocator.getName()) - .append("), isOwning: ") - .append(", size: ") - .append(", references: ") - .append(bufRefCnt.get()) - .append(", life: ") - .append(lCreationTime) - .append("..") - .append(lDestructionTime) - .append(", allocatorManager: [") - .append(", life: "); + .append("ledger[") + .append(ledgerId) + .append("] allocator: ") + .append(allocator.getName()) + .append("), isOwning: ") + .append(", size: ") + .append(", references: ") + .append(bufRefCnt.get()) + .append(", life: ") + .append(lCreationTime) + .append("..") + .append(lDestructionTime) + .append(", allocatorManager: [") + .append(", life: "); if (!BaseAllocator.DEBUG) { sb.append("]\n"); } else { Preconditions.checkArgument(buffers != null, "IdentityHashMap of buffers must not be null"); synchronized (buffers) { - sb.append("] holds ") - .append(buffers.size()) - .append(" buffers. \n"); + sb.append("] holds ").append(buffers.size()).append(" buffers. \n"); for (ArrowBuf buf : buffers.keySet()) { buf.print(sb, indent + 2, verbosity); sb.append('\n'); @@ -517,5 +508,4 @@ void print(StringBuilder sb, int indent, BaseAllocator.Verbosity verbosity) { public AllocationManager getAllocationManager() { return allocationManager; } - } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferManager.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferManager.java index 6b622e71927..9cc8ee6d992 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferManager.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferManager.java @@ -18,17 +18,16 @@ package org.apache.arrow.memory; /** - * Manages a list of {@link ArrowBuf}s that can be reallocated as needed. Upon - * re-allocation the old buffer will be freed. Managing a list of these buffers - * prevents some parts of the system from needing to define a correct location - * to place the final call to free them. + * Manages a list of {@link ArrowBuf}s that can be reallocated as needed. Upon re-allocation the old + * buffer will be freed. Managing a list of these buffers prevents some parts of the system from + * needing to define a correct location to place the final call to free them. */ public interface BufferManager extends AutoCloseable { /** * Replace an old buffer with a new version at least of the provided size. Does not copy data. * - * @param old Old Buffer that the user is no longer going to use. + * @param old Old Buffer that the user is no longer going to use. * @param newSize Size of new replacement buffer. * @return A new version of the buffer. */ diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/CheckAllocator.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/CheckAllocator.java index dac4a3fcff5..b7694609771 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/CheckAllocator.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/CheckAllocator.java @@ -22,7 +22,6 @@ import java.util.Enumeration; import java.util.LinkedHashSet; import java.util.Set; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,24 +38,24 @@ final class CheckAllocator { private static final String ALLOCATOR_PATH_NETTY = "org/apache/arrow/memory/netty/DefaultAllocationManagerFactory.class"; - private CheckAllocator() { - } + private CheckAllocator() {} static String check() { Set urls = scanClasspath(); URL rootAllocator = assertOnlyOne(urls); reportResult(rootAllocator); - if (rootAllocator.getPath().contains("memory-core") || - rootAllocator.getPath().contains("/org/apache/arrow/memory/core/")) { + if (rootAllocator.getPath().contains("memory-core") + || rootAllocator.getPath().contains("/org/apache/arrow/memory/core/")) { return "org.apache.arrow.memory.DefaultAllocationManagerFactory"; - } else if (rootAllocator.getPath().contains("memory-unsafe") || - rootAllocator.getPath().contains("/org/apache/arrow/memory/unsafe/")) { + } else if (rootAllocator.getPath().contains("memory-unsafe") + || rootAllocator.getPath().contains("/org/apache/arrow/memory/unsafe/")) { return "org.apache.arrow.memory.unsafe.DefaultAllocationManagerFactory"; - } else if (rootAllocator.getPath().contains("memory-netty") || - rootAllocator.getPath().contains("/org/apache/arrow/memory/netty/")) { + } else if (rootAllocator.getPath().contains("memory-netty") + || rootAllocator.getPath().contains("/org/apache/arrow/memory/netty/")) { return "org.apache.arrow.memory.netty.DefaultAllocationManagerFactory"; } else { - throw new IllegalStateException("Unknown allocation manager type to infer. Current: " + rootAllocator.getPath()); + throw new IllegalStateException( + "Unknown allocation manager type to infer. Current: " + rootAllocator.getPath()); } } @@ -105,10 +104,10 @@ private static URL assertOnlyOne(Set urls) { logger.warn("More than one DefaultAllocationManager on classpath. Choosing first found"); } if (urls.isEmpty()) { - throw new RuntimeException("No DefaultAllocationManager found on classpath. Can't allocate Arrow buffers." + - " Please consider adding arrow-memory-netty or arrow-memory-unsafe as a dependency."); + throw new RuntimeException( + "No DefaultAllocationManager found on classpath. Can't allocate Arrow buffers." + + " Please consider adding arrow-memory-netty or arrow-memory-unsafe as a dependency."); } return urls.iterator().next(); } - } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ChildAllocator.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ChildAllocator.java index 67156f89d13..eda8b296ceb 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ChildAllocator.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ChildAllocator.java @@ -17,14 +17,13 @@ package org.apache.arrow.memory; - /** - * Child allocator class. Only slightly different from the {@see RootAllocator}, - * in that these can't be created directly, but must be obtained from - * {@see BufferAllocator#newChildAllocator(AllocatorOwner, long, long, int)}. + * Child allocator class. Only slightly different from the {@see RootAllocator}, in that these can't + * be created directly, but must be obtained from {@see + * BufferAllocator#newChildAllocator(AllocatorOwner, long, long, int)}. * - *

Child allocators can only be created by the root, or other children, so - * this class is package private.

+ *

Child allocators can only be created by the root, or other children, so this class is package + * private. */ class ChildAllocator extends BaseAllocator { @@ -32,13 +31,10 @@ class ChildAllocator extends BaseAllocator { * Constructor. * * @param parentAllocator parent allocator -- the one creating this child - * @param name the name of this child allocator - * @param config configuration of this child allocator + * @param name the name of this child allocator + * @param config configuration of this child allocator */ - ChildAllocator( - BaseAllocator parentAllocator, - String name, - Config config) { + ChildAllocator(BaseAllocator parentAllocator, String name, Config config) { super(parentAllocator, name, config); } } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerOption.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerOption.java index 6d408abcc34..3da56f91aec 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerOption.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerOption.java @@ -18,58 +18,43 @@ package org.apache.arrow.memory; import java.lang.reflect.Field; - import org.apache.arrow.util.VisibleForTesting; import org.checkerframework.checker.nullness.qual.Nullable; - -/** - * A class for choosing the default allocation manager. - */ +/** A class for choosing the default allocation manager. */ public class DefaultAllocationManagerOption { - /** - * The environmental variable to set the default allocation manager type. - */ + /** The environmental variable to set the default allocation manager type. */ public static final String ALLOCATION_MANAGER_TYPE_ENV_NAME = "ARROW_ALLOCATION_MANAGER_TYPE"; - /** - * The system property to set the default allocation manager type. - */ - public static final String ALLOCATION_MANAGER_TYPE_PROPERTY_NAME = "arrow.allocation.manager.type"; + /** The system property to set the default allocation manager type. */ + public static final String ALLOCATION_MANAGER_TYPE_PROPERTY_NAME = + "arrow.allocation.manager.type"; - static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(DefaultAllocationManagerOption.class); + static final org.slf4j.Logger LOGGER = + org.slf4j.LoggerFactory.getLogger(DefaultAllocationManagerOption.class); - /** - * The default allocation manager factory. - */ + /** The default allocation manager factory. */ private static AllocationManager.@Nullable Factory DEFAULT_ALLOCATION_MANAGER_FACTORY = null; - /** - * The allocation manager type. - */ + /** The allocation manager type. */ public enum AllocationManagerType { - /** - * Netty based allocation manager. - */ + /** Netty based allocation manager. */ Netty, - /** - * Unsafe based allocation manager. - */ + /** Unsafe based allocation manager. */ Unsafe, - /** - * Unknown type. - */ + /** Unknown type. */ Unknown, } /** * Returns the default allocation manager type. + * * @return the default allocation manager type. */ - @SuppressWarnings("nullness:argument") //enum types valueOf are implicitly non-null + @SuppressWarnings("nullness:argument") // enum types valueOf are implicitly non-null @VisibleForTesting public static AllocationManagerType getDefaultAllocationManagerType() { AllocationManagerType ret = AllocationManagerType.Unknown; @@ -114,7 +99,7 @@ static AllocationManager.Factory getDefaultAllocationManagerFactory() { } @SuppressWarnings({"nullness:argument", "nullness:return"}) - //incompatible argument for parameter obj of Field.get + // incompatible argument for parameter obj of Field.get // Static member qualifying type may not be annotated private static AllocationManager.Factory getFactory(String clazzName) { try { @@ -130,8 +115,10 @@ private static AllocationManager.Factory getUnsafeFactory() { try { return getFactory("org.apache.arrow.memory.unsafe.UnsafeAllocationManager"); } catch (RuntimeException e) { - throw new RuntimeException("Please add arrow-memory-unsafe to your classpath," + - " No DefaultAllocationManager found to instantiate an UnsafeAllocationManager", e); + throw new RuntimeException( + "Please add arrow-memory-unsafe to your classpath," + + " No DefaultAllocationManager found to instantiate an UnsafeAllocationManager", + e); } } @@ -139,8 +126,10 @@ private static AllocationManager.Factory getNettyFactory() { try { return getFactory("org.apache.arrow.memory.netty.NettyAllocationManager"); } catch (RuntimeException e) { - throw new RuntimeException("Please add arrow-memory-netty to your classpath," + - " No DefaultAllocationManager found to instantiate an NettyAllocationManager", e); + throw new RuntimeException( + "Please add arrow-memory-netty to your classpath," + + " No DefaultAllocationManager found to instantiate an NettyAllocationManager", + e); } } } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ForeignAllocation.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ForeignAllocation.java index c1b47382a38..904d30e2449 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ForeignAllocation.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ForeignAllocation.java @@ -18,7 +18,8 @@ package org.apache.arrow.memory; /** - * EXPERIMENTAL: a memory allocation that does not come from a BufferAllocator, but rather an outside source (like JNI). + * EXPERIMENTAL: a memory allocation that does not come from a BufferAllocator, but rather an + * outside source (like JNI). * *

To use this, subclass this class and implement {@link #release0()} to free the allocation. */ @@ -37,22 +38,16 @@ protected ForeignAllocation(long size, long memoryAddress) { this.size = size; } - /** - * Get the size of this allocation. - */ + /** Get the size of this allocation. */ public long getSize() { return size; } - /** - * Get the address of this allocation. - */ + /** Get the address of this allocation. */ protected long memoryAddress() { return memoryAddress; } - /** - * Free this allocation. Will only be called once. - */ + /** Free this allocation. Will only be called once. */ protected abstract void release0(); } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ForeignAllocationManager.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ForeignAllocationManager.java index 741b866f819..5fcd9caadd0 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ForeignAllocationManager.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ForeignAllocationManager.java @@ -17,13 +17,12 @@ package org.apache.arrow.memory; -/** - * An AllocationManager wrapping a ForeignAllocation. - */ +/** An AllocationManager wrapping a ForeignAllocation. */ class ForeignAllocationManager extends AllocationManager { private final ForeignAllocation allocation; - protected ForeignAllocationManager(BufferAllocator accountingAllocator, ForeignAllocation allocation) { + protected ForeignAllocationManager( + BufferAllocator accountingAllocator, ForeignAllocation allocation) { super(accountingAllocator); this.allocation = allocation; } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/LowCostIdentityHashMap.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/LowCostIdentityHashMap.java index 740233ef411..b39ede69a2c 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/LowCostIdentityHashMap.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/LowCostIdentityHashMap.java @@ -24,11 +24,9 @@ import org.checkerframework.checker.nullness.qual.Nullable; /** - * Highly specialized IdentityHashMap that implements only partial - * Map APIs. - * It incurs low initial cost (just two elements by default). - * It assumes Value includes the Key - Implements @ValueWithKeyIncluded iface - * that provides "getKey" method. + * Highly specialized IdentityHashMap that implements only partial Map APIs. It incurs low initial + * cost (just two elements by default). It assumes Value includes the Key - + * Implements @ValueWithKeyIncluded iface that provides "getKey" method. * * @param Key type * @param Value type @@ -38,7 +36,7 @@ public class LowCostIdentityHashMap> { /* * The internal data structure to hold values. */ - private @Nullable Object [] elementData; // elementData[index] = null; + private @Nullable Object[] elementData; // elementData[index] = null; /* Actual number of values. */ private int size; @@ -54,9 +52,7 @@ public class LowCostIdentityHashMap> { /* Default load factor of 0.75; */ private static final int LOAD_FACTOR = 7500; - /** - * Creates a Map with default expected maximum size. - */ + /** Creates a Map with default expected maximum size. */ public LowCostIdentityHashMap() { this(DEFAULT_MIN_SIZE); } @@ -64,9 +60,7 @@ public LowCostIdentityHashMap() { /** * Creates a Map with the specified maximum size parameter. * - * @param maxSize - * The estimated maximum number of entries that will be put in - * this map. + * @param maxSize The estimated maximum number of entries that will be put in this map. */ public LowCostIdentityHashMap(int maxSize) { if (maxSize >= 0) { @@ -78,8 +72,7 @@ public LowCostIdentityHashMap(int maxSize) { } } - private int getThreshold(@UnderInitialization LowCostIdentityHashMap this, - int maxSize) { + private int getThreshold(@UnderInitialization LowCostIdentityHashMap this, int maxSize) { // assign the threshold to maxSize initially, this will change to a // higher value if rehashing occurs. return maxSize > 2 ? maxSize : 2; @@ -95,22 +88,22 @@ private int computeElementArraySize(@UnderInitialization LowCostIdentityHashMap< /** * Create a new element array. * - * @param s - * the number of elements + * @param s the number of elements * @return Reference to the element array */ - private Object[] newElementArrayInitialized(@Initialized LowCostIdentityHashMap this, int s) { + private Object[] newElementArrayInitialized( + @Initialized LowCostIdentityHashMap this, int s) { return new Object[s]; } /** * Create a new element array. * - * @param s - * the number of elements + * @param s the number of elements * @return Reference to the element array */ - private Object[] newElementArrayUnderInitialized(@UnderInitialization LowCostIdentityHashMap this, int s) { + private Object[] newElementArrayUnderInitialized( + @UnderInitialization LowCostIdentityHashMap this, int s) { return new Object[s]; } @@ -130,10 +123,8 @@ public void clear() { /** * Returns whether this map contains the specified key. * - * @param key - * the key to search for. - * @return {@code true} if this map contains the specified key, - * {@code false} otherwise. + * @param key the key to search for. + * @return {@code true} if this map contains the specified key, {@code false} otherwise. */ public boolean containsKey(K key) { Preconditions.checkNotNull(key); @@ -145,10 +136,8 @@ public boolean containsKey(K key) { /** * Returns whether this map contains the specified value. * - * @param value - * the value to search for. - * @return {@code true} if this map contains the specified value, - * {@code false} otherwise. + * @param value the value to search for. + * @return {@code true} if this map contains the specified value, {@code false} otherwise. */ public boolean containsValue(V value) { Preconditions.checkNotNull(value); @@ -172,13 +161,14 @@ public boolean containsValue(V value) { int index = findIndex(key, elementData); - return (elementData[index] == null) ? null : - (((V) elementData[index]).getKey() == key) ? (V) elementData[index] : null; + return (elementData[index] == null) + ? null + : (((V) elementData[index]).getKey() == key) ? (V) elementData[index] : null; } /** - * Returns the index where the key is found at, or the index of the next - * empty spot if the key is not found in this table. + * Returns the index where the key is found at, or the index of the next empty spot if the key is + * not found in this table. */ @VisibleForTesting int findIndex(@Nullable Object key, @Nullable Object[] array) { @@ -207,8 +197,8 @@ static int getModuloHash(@Nullable Object key, int length) { * Maps the specified key to the specified value. * * @param value the value. - * @return the value of any previous mapping with the specified key or - * {@code null} if there was no such mapping. + * @return the value of any previous mapping with the specified key or {@code null} if there was + * no such mapping. */ public V put(V value) { Preconditions.checkNotNull(value); @@ -262,8 +252,8 @@ private void computeMaxSize() { * Removes the mapping with the specified key from this map. * * @param key the key of the mapping to remove. - * @return the value of the removed mapping, or {@code null} if no mapping - * for the specified key was found. + * @return the value of the removed mapping, or {@code null} if no mapping for the specified key + * was found. */ public @Nullable V remove(K key) { Preconditions.checkNotNull(key); @@ -313,13 +303,10 @@ private void computeMaxSize() { return (V) result; } - - /** * Returns whether this Map has no elements. * - * @return {@code true} if this Map has no elements, - * {@code false} otherwise. + * @return {@code true} if this Map has no elements, {@code false} otherwise. * @see #size() */ public boolean isEmpty() { diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/OutOfMemoryException.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/OutOfMemoryException.java index 841ffcd8fa4..97784516e4f 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/OutOfMemoryException.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/OutOfMemoryException.java @@ -22,14 +22,14 @@ /** * Indicates memory could not be allocated for Arrow buffers. * - *

This is different from {@linkplain OutOfMemoryError} which indicates the JVM - * is out of memory. This error indicates that static limit of one of Arrow's - * allocators (e.g. {@linkplain BaseAllocator}) has been exceeded. + *

This is different from {@linkplain OutOfMemoryError} which indicates the JVM is out of memory. + * This error indicates that static limit of one of Arrow's allocators (e.g. {@linkplain + * BaseAllocator}) has been exceeded. */ public class OutOfMemoryException extends RuntimeException { - static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(OutOfMemoryException - .class); + static final org.slf4j.Logger logger = + org.slf4j.LoggerFactory.getLogger(OutOfMemoryException.class); private static final long serialVersionUID = -6858052345185793382L; private Optional outcomeDetails = Optional.empty(); @@ -37,14 +37,13 @@ public OutOfMemoryException() { super(); } - public OutOfMemoryException(String message, Throwable cause, boolean enableSuppression, boolean - writableStackTrace) { + public OutOfMemoryException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } public OutOfMemoryException(String message, Throwable cause) { super(message, cause); - } public OutOfMemoryException(String message) { @@ -58,7 +57,6 @@ public OutOfMemoryException(String message, Optional d public OutOfMemoryException(Throwable cause) { super(cause); - } public Optional getOutcomeDetails() { diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/OwnershipTransferNOOP.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/OwnershipTransferNOOP.java index d4fed8448bf..83f5b2471ed 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/OwnershipTransferNOOP.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/OwnershipTransferNOOP.java @@ -17,9 +17,7 @@ package org.apache.arrow.memory; -/** - * An {@link OwnershipTransferResult} indicating no transfer needed. - */ +/** An {@link OwnershipTransferResult} indicating no transfer needed. */ public class OwnershipTransferNOOP implements OwnershipTransferResult { private final ArrowBuf buffer; diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/OwnershipTransferResult.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/OwnershipTransferResult.java index ef857d827a6..fbe614a6da0 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/OwnershipTransferResult.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/OwnershipTransferResult.java @@ -17,9 +17,7 @@ package org.apache.arrow.memory; -/** - * The result of transferring an {@link ArrowBuf} between {@linkplain BufferAllocator}s. - */ +/** The result of transferring an {@link ArrowBuf} between {@linkplain BufferAllocator}s. */ public interface OwnershipTransferResult { boolean getAllocationFit(); diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ReferenceManager.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ReferenceManager.java index 7d4de18751b..080c6ce3335 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ReferenceManager.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ReferenceManager.java @@ -20,43 +20,47 @@ /** * ReferenceManager is the reference count for one or more allocations. * - *

In order to integrate with the core {@link BufferAllocator} implementation, the allocation itself should - * be represented by an {@link AllocationManager}, though this is not required by the API. + *

In order to integrate with the core {@link BufferAllocator} implementation, the allocation + * itself should be represented by an {@link AllocationManager}, though this is not required by the + * API. */ public interface ReferenceManager { /** * Return the reference count. + * * @return reference count */ int getRefCount(); /** - * Decrement this reference manager's reference count by 1 for the associated underlying - * memory. If the reference count drops to 0, it implies that ArrowBufs managed by this - * reference manager no longer need access to the underlying memory + * Decrement this reference manager's reference count by 1 for the associated underlying memory. + * If the reference count drops to 0, it implies that ArrowBufs managed by this reference manager + * no longer need access to the underlying memory + * * @return true if ref count has dropped to 0, false otherwise */ boolean release(); /** - * Decrement this reference manager's reference count for the associated underlying - * memory. If the reference count drops to 0, it implies that ArrowBufs managed by this - * reference manager no longer need access to the underlying memory + * Decrement this reference manager's reference count for the associated underlying memory. If the + * reference count drops to 0, it implies that ArrowBufs managed by this reference manager no + * longer need access to the underlying memory + * * @param decrement the count to decrease the reference count by * @return the new reference count */ boolean release(int decrement); /** - * Increment this reference manager's reference count by 1 for the associated underlying - * memory. + * Increment this reference manager's reference count by 1 for the associated underlying memory. */ void retain(); /** - * Increment this reference manager's reference count by a given amount for the - * associated underlying memory. + * Increment this reference manager's reference count by a given amount for the associated + * underlying memory. + * * @param increment the count to increase the reference count by */ void retain(int increment); @@ -64,13 +68,13 @@ public interface ReferenceManager { /** * Create a new ArrowBuf that is associated with an alternative allocator for the purposes of * memory ownership and accounting. This has no impact on the reference counting for the current - * ArrowBuf except in the situation where the passed in Allocator is the same as the current buffer. - * This operation has no impact on the reference count of this ArrowBuf. The newly created + * ArrowBuf except in the situation where the passed in Allocator is the same as the current + * buffer. This operation has no impact on the reference count of this ArrowBuf. The newly created * ArrowBuf with either have a reference count of 1 (in the case that this is the first time this - * memory is being associated with the target allocator or in other words allocation manager currently - * doesn't hold a mapping for the target allocator) or the current value of the reference count for - * the target allocator-reference manager combination + 1 in the case that the provided allocator - * already had an association to this underlying memory. + * memory is being associated with the target allocator or in other words allocation manager + * currently doesn't hold a mapping for the target allocator) or the current value of the + * reference count for the target allocator-reference manager combination + 1 in the case that the + * provided allocator already had an association to this underlying memory. * *

The underlying allocation ({@link AllocationManager}) will not be copied. * @@ -81,21 +85,21 @@ public interface ReferenceManager { ArrowBuf retain(ArrowBuf srcBuffer, BufferAllocator targetAllocator); /** - * Derive a new ArrowBuf from a given source ArrowBuf. The new derived - * ArrowBuf will share the same reference count as rest of the ArrowBufs - * associated with this reference manager. + * Derive a new ArrowBuf from a given source ArrowBuf. The new derived ArrowBuf will share the + * same reference count as rest of the ArrowBufs associated with this reference manager. + * * @param sourceBuffer source ArrowBuf * @param index index (relative to source ArrowBuf) new ArrowBuf should be derived from - * @param length length (bytes) of data in underlying memory that derived buffer will - * have access to in underlying memory + * @param length length (bytes) of data in underlying memory that derived buffer will have access + * to in underlying memory * @return derived buffer */ ArrowBuf deriveBuffer(ArrowBuf sourceBuffer, long index, long length); /** - * Duplicate the memory accounting ownership of the backing allocation of the given ArrowBuf in another allocator. - * This will generate a new ArrowBuf that carries an association with the same underlying memory - * ({@link AllocationManager}s) as the given ArrowBuf. + * Duplicate the memory accounting ownership of the backing allocation of the given ArrowBuf in + * another allocator. This will generate a new ArrowBuf that carries an association with the same + * underlying memory ({@link AllocationManager}s) as the given ArrowBuf. * * @param sourceBuffer source ArrowBuf * @param targetAllocator The target allocator to create an association with @@ -105,18 +109,21 @@ public interface ReferenceManager { /** * Get the buffer allocator associated with this reference manager. + * * @return buffer allocator. */ BufferAllocator getAllocator(); /** * Total size (in bytes) of memory underlying this reference manager. + * * @return Size (in bytes) of the memory chunk. */ long getSize(); /** * Get the total accounted size (in bytes). + * * @return accounted size. */ long getAccountedSize(); @@ -124,57 +131,58 @@ public interface ReferenceManager { String NO_OP_ERROR_MESSAGE = "Operation not supported on NO_OP Reference Manager"; // currently used for empty ArrowBufs - ReferenceManager NO_OP = new ReferenceManager() { - @Override - public int getRefCount() { - return 1; - } - - @Override - public boolean release() { - return false; - } - - @Override - public boolean release(int decrement) { - return false; - } - - @Override - public void retain() { } - - @Override - public void retain(int increment) { } - - @Override - public ArrowBuf retain(ArrowBuf srcBuffer, BufferAllocator targetAllocator) { - return srcBuffer; - } - - @Override - public ArrowBuf deriveBuffer(ArrowBuf sourceBuffer, long index, long length) { - return sourceBuffer; - } - - @Override - public OwnershipTransferResult transferOwnership(ArrowBuf sourceBuffer, BufferAllocator targetAllocator) { - return new OwnershipTransferNOOP(sourceBuffer); - } - - @Override - public BufferAllocator getAllocator() { - return new RootAllocator(0); - } - - @Override - public long getSize() { - return 0L; - } - - @Override - public long getAccountedSize() { - return 0L; - } - - }; + ReferenceManager NO_OP = + new ReferenceManager() { + @Override + public int getRefCount() { + return 1; + } + + @Override + public boolean release() { + return false; + } + + @Override + public boolean release(int decrement) { + return false; + } + + @Override + public void retain() {} + + @Override + public void retain(int increment) {} + + @Override + public ArrowBuf retain(ArrowBuf srcBuffer, BufferAllocator targetAllocator) { + return srcBuffer; + } + + @Override + public ArrowBuf deriveBuffer(ArrowBuf sourceBuffer, long index, long length) { + return sourceBuffer; + } + + @Override + public OwnershipTransferResult transferOwnership( + ArrowBuf sourceBuffer, BufferAllocator targetAllocator) { + return new OwnershipTransferNOOP(sourceBuffer); + } + + @Override + public BufferAllocator getAllocator() { + return new RootAllocator(0); + } + + @Override + public long getSize() { + return 0L; + } + + @Override + public long getAccountedSize() { + return 0L; + } + }; } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ReusableBuffer.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ReusableBuffer.java index 3530b819aad..0982497313b 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ReusableBuffer.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ReusableBuffer.java @@ -19,6 +19,7 @@ /** * A lightweight, automatically expanding container for holding byte data. + * * @param The type of the underlying buffer. */ public interface ReusableBuffer { @@ -29,19 +30,16 @@ public interface ReusableBuffer { */ long getLength(); - /** - * Get the buffer backing this ReusableBuffer. - */ + /** Get the buffer backing this ReusableBuffer. */ T getBuffer(); /** - * Set the buffer to the contents of the given ArrowBuf. - * The internal buffer must resize if it cannot fit the contents - * of the data. + * Set the buffer to the contents of the given ArrowBuf. The internal buffer must resize if it + * cannot fit the contents of the data. * - * @param srcBytes the data to copy from - * @param start the first position of the new data - * @param len the number of bytes of the new data + * @param srcBytes the data to copy from + * @param start the first position of the new data + * @param len the number of bytes of the new data */ void set(ArrowBuf srcBytes, long start, long len); } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/RootAllocator.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/RootAllocator.java index 89889118c05..634b6165dc5 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/RootAllocator.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/RootAllocator.java @@ -22,9 +22,8 @@ import org.apache.arrow.util.VisibleForTesting; /** - * A root allocator for using direct memory for Arrow Vectors/Arrays. Supports creating a - * tree of descendant child allocators to facilitate better instrumentation of memory - * allocations. + * A root allocator for using direct memory for Arrow Vectors/Arrays. Supports creating a tree of + * descendant child allocators to facilitate better instrumentation of memory allocations. */ public class RootAllocator extends BaseAllocator { @@ -37,33 +36,32 @@ public RootAllocator(final long limit) { } public RootAllocator(final AllocationListener listener, final long limit) { - //todo fix DefaultRoundingPolicy when using Netty + // todo fix DefaultRoundingPolicy when using Netty this(listener, limit, DefaultRoundingPolicy.DEFAULT_ROUNDING_POLICY); } /** * Constructor. * - * @param listener the allocation listener - * @param limit max allocation size in bytes - * @param roundingPolicy the policy for rounding the buffer size + * @param listener the allocation listener + * @param limit max allocation size in bytes + * @param roundingPolicy the policy for rounding the buffer size */ - public RootAllocator(final AllocationListener listener, final long limit, RoundingPolicy roundingPolicy) { - this(configBuilder() - .listener(listener) - .maxAllocation(limit) - .roundingPolicy(roundingPolicy) - .build() - ); + public RootAllocator( + final AllocationListener listener, final long limit, RoundingPolicy roundingPolicy) { + this( + configBuilder() + .listener(listener) + .maxAllocation(limit) + .roundingPolicy(roundingPolicy) + .build()); } public RootAllocator(Config config) { super(null, "ROOT", config); } - /** - * Verify the accounting state of the allocation system. - */ + /** Verify the accounting state of the allocation system. */ @VisibleForTesting public void verify() { verifyAllocator(); diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ValueWithKeyIncluded.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ValueWithKeyIncluded.java index 2699b6a464f..0d1b932b15d 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ValueWithKeyIncluded.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ValueWithKeyIncluded.java @@ -18,8 +18,7 @@ package org.apache.arrow.memory; /** - * Helper interface to generify a value to be included in the map where - * key is part of the value. + * Helper interface to generify a value to be included in the map where key is part of the value. * * @param The type of the key. */ diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/package-info.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/package-info.java index c04df2e0c6f..de9c82ae563 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/package-info.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/package-info.java @@ -16,9 +16,7 @@ */ /** - * Memory Allocation, Accounting and Management. - * See the Arrow Java documentation for details: Memory Management - * + * Memory Allocation, Accounting and Management. See the Arrow Java documentation for details: Memory Management */ - package org.apache.arrow.memory; diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/rounding/DefaultRoundingPolicy.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/rounding/DefaultRoundingPolicy.java index 7ba231b0c20..9840ed1fa27 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/rounding/DefaultRoundingPolicy.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/rounding/DefaultRoundingPolicy.java @@ -22,9 +22,9 @@ import org.slf4j.LoggerFactory; /** - * The default rounding policy. That is, if the requested size is within the chunk size, - * the rounded size will be the next power of two. Otherwise, the rounded size - * will be identical to the requested size. + * The default rounding policy. That is, if the requested size is within the chunk size, the rounded + * size will be the next power of two. Otherwise, the rounded size will be identical to the + * requested size. */ public class DefaultRoundingPolicy implements RoundingPolicy { private static final Logger logger = LoggerFactory.getLogger(DefaultRoundingPolicy.class); @@ -33,15 +33,13 @@ public class DefaultRoundingPolicy implements RoundingPolicy { /** * The variables here and the static block calculates the DEFAULT_CHUNK_SIZE. * - *

- * It was copied from {@link io.netty.buffer.PooledByteBufAllocator}. - *

+ *

It was copied from {@link io.netty.buffer.PooledByteBufAllocator}. */ private static final int MIN_PAGE_SIZE = 4096; + private static final int MAX_CHUNK_SIZE = (int) (((long) Integer.MAX_VALUE + 1) / 2); private static final long DEFAULT_CHUNK_SIZE; - static { int defaultPageSize = Integer.getInteger("org.apache.memory.allocator.pageSize", 8192); Throwable pageSizeFallbackCause = null; @@ -69,7 +67,8 @@ public class DefaultRoundingPolicy implements RoundingPolicy { private static int validateAndCalculatePageShifts(int pageSize) { if (pageSize < MIN_PAGE_SIZE) { - throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: " + MIN_PAGE_SIZE + ")"); + throw new IllegalArgumentException( + "pageSize: " + pageSize + " (expected: " + MIN_PAGE_SIZE + ")"); } if ((pageSize & pageSize - 1) != 0) { @@ -87,20 +86,21 @@ private static int validateAndCalculateChunkSize(int pageSize, int maxOrder) { // Ensure the resulting chunkSize does not overflow. int chunkSize = pageSize; - for (int i = maxOrder; i > 0; i --) { + for (int i = maxOrder; i > 0; i--) { if (chunkSize > MAX_CHUNK_SIZE / 2) { - throw new IllegalArgumentException(String.format( - "pageSize (%d) << maxOrder (%d) must not exceed %d", pageSize, maxOrder, MAX_CHUNK_SIZE)); + throw new IllegalArgumentException( + String.format( + "pageSize (%d) << maxOrder (%d) must not exceed %d", + pageSize, maxOrder, MAX_CHUNK_SIZE)); } chunkSize <<= 1; } return chunkSize; } - /** - * The singleton instance. - */ - public static final DefaultRoundingPolicy DEFAULT_ROUNDING_POLICY = new DefaultRoundingPolicy(DEFAULT_CHUNK_SIZE); + /** The singleton instance. */ + public static final DefaultRoundingPolicy DEFAULT_ROUNDING_POLICY = + new DefaultRoundingPolicy(DEFAULT_CHUNK_SIZE); private DefaultRoundingPolicy(long chunkSize) { this.chunkSize = chunkSize; @@ -108,7 +108,6 @@ private DefaultRoundingPolicy(long chunkSize) { @Override public long getRoundedSize(long requestSize) { - return requestSize < chunkSize ? - CommonUtil.nextPowerOfTwo(requestSize) : requestSize; + return requestSize < chunkSize ? CommonUtil.nextPowerOfTwo(requestSize) : requestSize; } } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/rounding/SegmentRoundingPolicy.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/rounding/SegmentRoundingPolicy.java index d2bc4451d0a..6e1f32504a1 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/rounding/SegmentRoundingPolicy.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/rounding/SegmentRoundingPolicy.java @@ -19,33 +19,32 @@ import org.apache.arrow.util.Preconditions; -/** - * The rounding policy that each buffer size must a multiple of the segment size. - */ +/** The rounding policy that each buffer size must a multiple of the segment size. */ public class SegmentRoundingPolicy implements RoundingPolicy { - /** - * The minimal segment size. - */ + /** The minimal segment size. */ public static final long MIN_SEGMENT_SIZE = 1024L; /** - * The segment size. It must be at least {@link SegmentRoundingPolicy#MIN_SEGMENT_SIZE}, - * and be a power of 2. + * The segment size. It must be at least {@link SegmentRoundingPolicy#MIN_SEGMENT_SIZE}, and be a + * power of 2. */ private int segmentSize; /** * Constructor for the segment rounding policy. + * * @param segmentSize the segment size. - * @throws IllegalArgumentException if the segment size is smaller than - * {@link SegmentRoundingPolicy#MIN_SEGMENT_SIZE}, or is not a power of 2. + * @throws IllegalArgumentException if the segment size is smaller than {@link + * SegmentRoundingPolicy#MIN_SEGMENT_SIZE}, or is not a power of 2. */ public SegmentRoundingPolicy(int segmentSize) { - Preconditions.checkArgument(segmentSize >= MIN_SEGMENT_SIZE, - "The segment size cannot be smaller than %s", MIN_SEGMENT_SIZE); - Preconditions.checkArgument((segmentSize & (segmentSize - 1)) == 0, - "The segment size must be a power of 2"); + Preconditions.checkArgument( + segmentSize >= MIN_SEGMENT_SIZE, + "The segment size cannot be smaller than %s", + MIN_SEGMENT_SIZE); + Preconditions.checkArgument( + (segmentSize & (segmentSize - 1)) == 0, "The segment size must be a power of 2"); this.segmentSize = segmentSize; } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/ArrowBufPointer.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/ArrowBufPointer.java index b41576847d6..e0539ff1dd6 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/ArrowBufPointer.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/ArrowBufPointer.java @@ -24,14 +24,12 @@ import org.checkerframework.checker.nullness.qual.Nullable; /** - * Pointer to a memory region within an {@link ArrowBuf}. - * It will be used as the basis for calculating hash code within a vector, and equality determination. + * Pointer to a memory region within an {@link ArrowBuf}. It will be used as the basis for + * calculating hash code within a vector, and equality determination. */ public final class ArrowBufPointer { - /** - * The hash code when the arrow buffer is null. - */ + /** The hash code when the arrow buffer is null. */ public static final int NULL_HASH_CODE = 0; private @Nullable ArrowBuf buf; @@ -44,20 +42,17 @@ public final class ArrowBufPointer { private final ArrowBufHasher hasher; - /** - * A flag indicating if the underlying memory region has changed. - */ + /** A flag indicating if the underlying memory region has changed. */ private boolean hashCodeChanged = false; - /** - * The default constructor. - */ + /** The default constructor. */ public ArrowBufPointer() { this(SimpleHasher.INSTANCE); } /** * Constructs an arrow buffer pointer with the specified hasher. + * * @param hasher the hasher to use. */ public ArrowBufPointer(ArrowBufHasher hasher) { @@ -68,6 +63,7 @@ public ArrowBufPointer(ArrowBufHasher hasher) { /** * Constructs an Arrow buffer pointer. + * * @param buf the underlying {@link ArrowBuf}, which can be null. * @param offset the start off set of the memory region pointed to. * @param length the length off set of the memory region pointed to. @@ -78,6 +74,7 @@ public ArrowBufPointer(ArrowBuf buf, long offset, long length) { /** * Constructs an Arrow buffer pointer. + * * @param buf the underlying {@link ArrowBuf}, which can be null. * @param offset the start off set of the memory region pointed to. * @param length the length off set of the memory region pointed to. @@ -91,11 +88,11 @@ public ArrowBufPointer(ArrowBuf buf, long offset, long length, ArrowBufHasher ha /** * Sets this pointer. + * * @param buf the underlying {@link ArrowBuf}, which can be null. * @param offset the start off set of the memory region pointed to. * @param length the length off set of the memory region pointed to. */ - public void set(ArrowBuf buf, long offset, long length) { this.buf = buf; this.offset = offset; @@ -106,6 +103,7 @@ public void set(ArrowBuf buf, long offset, long length) { /** * Gets the underlying buffer, or null if the underlying data is invalid or null. + * * @return the underlying buffer, if any, or null if the underlying data is invalid or null. */ public @Nullable ArrowBuf getBuf() { @@ -145,8 +143,9 @@ public boolean equals(@Nullable Object o) { } } - return ByteFunctionHelpers.equal(buf, offset, offset + length, - other.buf, other.offset, other.offset + other.length) != 0; + return ByteFunctionHelpers.equal( + buf, offset, offset + length, other.buf, other.offset, other.offset + other.length) + != 0; } @Override @@ -167,12 +166,11 @@ public int hashCode() { } /** - * Compare two arrow buffer pointers. - * The comparison is based on lexicographic order. + * Compare two arrow buffer pointers. The comparison is based on lexicographic order. + * * @param that the other pointer to compare. - * @return 0 if the two pointers are equal; - * a positive integer if this pointer is larger; - * a negative integer if this pointer is smaller. + * @return 0 if the two pointers are equal; a positive integer if this pointer is larger; a + * negative integer if this pointer is smaller. */ public int compareTo(ArrowBufPointer that) { if (this.buf == null || that.buf == null) { @@ -184,7 +182,12 @@ public int compareTo(ArrowBufPointer that) { } } - return ByteFunctionHelpers.compare(this.buf, this.offset, this.offset + this.length, - that.buf, that.offset, that.offset + that.length); + return ByteFunctionHelpers.compare( + this.buf, + this.offset, + this.offset + this.length, + that.buf, + that.offset, + that.offset + that.length); } } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/AssertionUtil.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/AssertionUtil.java index 5e5b331faeb..042ac418f5b 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/AssertionUtil.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/AssertionUtil.java @@ -18,7 +18,8 @@ package org.apache.arrow.memory.util; /** - * Utility class to that provides {@link #ASSERT_ENABLED} constant to determine if assertions are enabled. + * Utility class to that provides {@link #ASSERT_ENABLED} constant to determine if assertions are + * enabled. */ public class AssertionUtil { @@ -31,8 +32,7 @@ public class AssertionUtil { ASSERT_ENABLED = isAssertEnabled; } - private AssertionUtil() { - } + private AssertionUtil() {} public static boolean isAssertionsEnabled() { return ASSERT_ENABLED; diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/AutoCloseableLock.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/AutoCloseableLock.java index 95228cf78c7..e73c19918bf 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/AutoCloseableLock.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/AutoCloseableLock.java @@ -19,9 +19,7 @@ import java.util.concurrent.locks.Lock; -/** - * Simple wrapper class that allows Locks to be released via a try-with-resources block. - */ +/** Simple wrapper class that allows Locks to be released via a try-with-resources block. */ public class AutoCloseableLock implements AutoCloseable { private final Lock lock; @@ -39,5 +37,4 @@ public AutoCloseableLock open() { public void close() { lock.unlock(); } - } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/ByteFunctionHelpers.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/ByteFunctionHelpers.java index 9579245ca70..8c84d1f2279 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/ByteFunctionHelpers.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/ByteFunctionHelpers.java @@ -18,17 +18,15 @@ package org.apache.arrow.memory.util; import java.nio.ByteOrder; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BoundsChecking; import org.apache.arrow.memory.util.hash.ArrowBufHasher; import org.apache.arrow.memory.util.hash.SimpleHasher; -/** - * Utility methods for memory comparison at a byte level. - */ +/** Utility methods for memory comparison at a byte level. */ public class ByteFunctionHelpers { - static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ByteFunctionHelpers.class); + static final org.slf4j.Logger logger = + org.slf4j.LoggerFactory.getLogger(ByteFunctionHelpers.class); private static final boolean LITTLE_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN; @@ -37,15 +35,16 @@ private ByteFunctionHelpers() {} /** * Helper function to check for equality of bytes in two ArrowBufs. * - * @param left Left ArrowBuf for comparison + * @param left Left ArrowBuf for comparison * @param lStart start offset in the buffer - * @param lEnd end offset in the buffer - * @param right Right ArrowBuf for comparison + * @param lEnd end offset in the buffer + * @param right Right ArrowBuf for comparison * @param rStart start offset in the buffer - * @param rEnd end offset in the buffer + * @param rEnd end offset in the buffer * @return 1 if equals, 0 otherwise */ - public static int equal(final ArrowBuf left, long lStart, long lEnd, final ArrowBuf right, long rStart, long rEnd) { + public static int equal( + final ArrowBuf left, long lStart, long lEnd, final ArrowBuf right, long rStart, long rEnd) { if (BoundsChecking.BOUNDS_CHECKING_ENABLED) { left.checkBytes(lStart, lEnd); right.checkBytes(rStart, rEnd); @@ -53,8 +52,8 @@ public static int equal(final ArrowBuf left, long lStart, long lEnd, final Arrow return memEqual(left.memoryAddress(), lStart, lEnd, right.memoryAddress(), rStart, rEnd); } - private static int memEqual(final long laddr, long lStart, long lEnd, final long raddr, long rStart, - final long rEnd) { + private static int memEqual( + final long laddr, long lStart, long lEnd, final long raddr, long rStart, final long rEnd) { long n = lEnd - lStart; if (n == rEnd - rStart) { @@ -116,21 +115,16 @@ private static int memEqual(final long laddr, long lStart, long lEnd, final long * *

Function will check data before completing in the case that * - * @param left Left ArrowBuf to compare + * @param left Left ArrowBuf to compare * @param lStart start offset in the buffer - * @param lEnd end offset in the buffer - * @param right Right ArrowBuf to compare + * @param lEnd end offset in the buffer + * @param right Right ArrowBuf to compare * @param rStart start offset in the buffer - * @param rEnd end offset in the buffer + * @param rEnd end offset in the buffer * @return 1 if left input is greater, -1 if left input is smaller, 0 otherwise */ public static int compare( - final ArrowBuf left, - long lStart, - long lEnd, - final ArrowBuf right, - long rStart, - long rEnd) { + final ArrowBuf left, long lStart, long lEnd, final ArrowBuf right, long rStart, long rEnd) { if (BoundsChecking.BOUNDS_CHECKING_ENABLED) { left.checkBytes(lStart, lEnd); right.checkBytes(rStart, rEnd); @@ -139,12 +133,7 @@ public static int compare( } private static int memcmp( - final long laddr, - long lStart, - long lEnd, - final long raddr, - long rStart, - final long rEnd) { + final long laddr, long lStart, long lEnd, final long raddr, long rStart, final long rEnd) { long lLen = lEnd - lStart; long rLen = rEnd - rStart; long n = Math.min(rLen, lLen); @@ -213,37 +202,30 @@ private static int memcmp( } return lLen > rLen ? 1 : -1; - } /** * Helper function to compare a set of bytes in ArrowBuf to a ByteArray. * - * @param left Left ArrowBuf for comparison purposes + * @param left Left ArrowBuf for comparison purposes * @param lStart start offset in the buffer - * @param lEnd end offset in the buffer - * @param right second input to be compared + * @param lEnd end offset in the buffer + * @param right second input to be compared * @param rStart start offset in the byte array - * @param rEnd end offset in the byte array + * @param rEnd end offset in the byte array * @return 1 if left input is greater, -1 if left input is smaller, 0 otherwise */ public static int compare( - final ArrowBuf left, - int lStart, - int lEnd, - final byte[] right, - int rStart, - final int rEnd) { + final ArrowBuf left, int lStart, int lEnd, final byte[] right, int rStart, final int rEnd) { if (BoundsChecking.BOUNDS_CHECKING_ENABLED) { left.checkBytes(lStart, lEnd); } return memcmp(left.memoryAddress(), lStart, lEnd, right, rStart, rEnd); } - /** - * Compares the two specified {@code long} values, treating them as unsigned values between - * {@code 0} and {@code 2^64 - 1} inclusive. + * Compares the two specified {@code long} values, treating them as unsigned values between {@code + * 0} and {@code 2^64 - 1} inclusive. * * @param a the first unsigned {@code long} to compare * @param b the second unsigned {@code long} to compare @@ -259,12 +241,7 @@ public static int unsignedIntCompare(int a, int b) { } private static int memcmp( - final long laddr, - int lStart, - int lEnd, - final byte[] right, - int rStart, - final int rEnd) { + final long laddr, int lStart, int lEnd, final byte[] right, int rStart, final int rEnd) { int lLen = lEnd - lStart; int rLen = rEnd - rStart; int n = Math.min(rLen, lLen); @@ -318,9 +295,7 @@ private static int memcmp( return lLen > rLen ? 1 : -1; } - /** - * Compute hashCode with the given {@link ArrowBuf} and start/end index. - */ + /** Compute hashCode with the given {@link ArrowBuf} and start/end index. */ public static int hash(final ArrowBuf buf, long start, long end) { return hash(SimpleHasher.INSTANCE, buf, start, end); @@ -338,9 +313,7 @@ public static final int hash(ArrowBufHasher hasher, final ArrowBuf buf, long sta return hasher.hashCode(buf, start, end - start); } - /** - * Generate a new hashCode with the given current hashCode and new hashCode. - */ + /** Generate a new hashCode with the given current hashCode and new hashCode. */ public static int combineHash(int currentHash, int newHash) { return currentHash * 31 + newHash; } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/CommonUtil.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/CommonUtil.java index ccca7b1e030..20b42cf3d7a 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/CommonUtil.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/CommonUtil.java @@ -19,12 +19,10 @@ import java.util.Arrays; -/** - * Utilities and static methods needed for arrow-memory. - */ +/** Utilities and static methods needed for arrow-memory. */ public final class CommonUtil { - private CommonUtil() { } + private CommonUtil() {} /** * Rounds up the provided value to the nearest power of two. @@ -76,4 +74,3 @@ public static StringBuilder indent(StringBuilder sb, int indent) { return sb; } } - diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/HistoricalLog.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/HistoricalLog.java index 21f063c939e..a73e5702d05 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/HistoricalLog.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/HistoricalLog.java @@ -19,14 +19,12 @@ import java.util.Arrays; import java.util.LinkedList; - import org.checkerframework.checker.nullness.qual.Nullable; import org.slf4j.Logger; /** - * Utility class that can be used to log activity within a class - * for later logging and debugging. Supports recording events and - * recording the stack at the time they occur. + * Utility class that can be used to log activity within a class for later logging and debugging. + * Supports recording events and recording the stack at the time they occur. */ public class HistoricalLog { @@ -36,35 +34,34 @@ public class HistoricalLog { private @Nullable Event firstEvent; // the first stack trace recorded /** - * Constructor. The format string will be formatted and have its arguments - * substituted at the time this is called. + * Constructor. The format string will be formatted and have its arguments substituted at the time + * this is called. * * @param idStringFormat {@link String#format} format string that can be used to identify this - * object in a log. Including some kind of unique identifier that can be - * associated with the object instance is best. - * @param args for the format string, or nothing if none are required + * object in a log. Including some kind of unique identifier that can be associated with the + * object instance is best. + * @param args for the format string, or nothing if none are required */ public HistoricalLog(final String idStringFormat, Object... args) { this(Integer.MAX_VALUE, idStringFormat, args); } /** - * Constructor. The format string will be formatted and have its arguments - * substituted at the time this is called. + * Constructor. The format string will be formatted and have its arguments substituted at the time + * this is called. * - *

This form supports the specification of a limit that will limit the - * number of historical entries kept (which keeps down the amount of memory - * used). With the limit, the first entry made is always kept (under the - * assumption that this is the creation site of the object, which is usually - * interesting), and then up to the limit number of entries are kept after that. - * Each time a new entry is made, the oldest that is not the first is dropped. + *

This form supports the specification of a limit that will limit the number of historical + * entries kept (which keeps down the amount of memory used). With the limit, the first entry made + * is always kept (under the assumption that this is the creation site of the object, which is + * usually interesting), and then up to the limit number of entries are kept after that. Each time + * a new entry is made, the oldest that is not the first is dropped. * - * @param limit the maximum number of historical entries that will be kept, not including - * the first entry made + * @param limit the maximum number of historical entries that will be kept, not including the + * first entry made * @param idStringFormat {@link String#format} format string that can be used to identify this - * object in a log. Including some kind of unique identifier that can be - * associated with the object instance is best. - * @param args for the format string, or nothing if none are required + * object in a log. Including some kind of unique identifier that can be associated with the + * object instance is best. + * @param args for the format string, or nothing if none are required */ public HistoricalLog(final int limit, final String idStringFormat, Object... args) { this.limit = limit; @@ -73,12 +70,11 @@ public HistoricalLog(final int limit, final String idStringFormat, Object... arg } /** - * Record an event. Automatically captures the stack trace at the time this is - * called. The format string will be formatted and have its arguments substituted - * at the time this is called. + * Record an event. Automatically captures the stack trace at the time this is called. The format + * string will be formatted and have its arguments substituted at the time this is called. * * @param noteFormat {@link String#format} format string that describes the event - * @param args for the format string, or nothing if none are required + * @param args for the format string, or nothing if none are required */ public synchronized void recordEvent(final String noteFormat, Object... args) { final String note = String.format(noteFormat, args); @@ -93,11 +89,11 @@ public synchronized void recordEvent(final String noteFormat, Object... args) { } /** - * Write the history of this object to the given {@link StringBuilder}. The history - * includes the identifying string provided at construction time, and all the recorded - * events with their stack traces. + * Write the history of this object to the given {@link StringBuilder}. The history includes the + * identifying string provided at construction time, and all the recorded events with their stack + * traces. * - * @param sb {@link StringBuilder} to write to + * @param sb {@link StringBuilder} to write to * @param includeStackTrace whether to include the stacktrace of each event in the history */ public void buildHistory(final StringBuilder sb, boolean includeStackTrace) { @@ -107,8 +103,8 @@ public void buildHistory(final StringBuilder sb, boolean includeStackTrace) { /** * Build the history and write it to sb. * - * @param sb output - * @param indent starting indent (usually "") + * @param sb output + * @param indent starting indent (usually "") * @param includeStackTrace whether to include the stacktrace of each event. */ public synchronized void buildHistory( @@ -118,20 +114,13 @@ public synchronized void buildHistory( Arrays.fill(indentation, ' '); Arrays.fill(innerIndentation, ' '); - sb.append(indentation) - .append("event log for: ") - .append(idString) - .append('\n'); + sb.append(indentation).append("event log for: ").append(idString).append('\n'); if (firstEvent != null) { long time = firstEvent.time; String note = firstEvent.note; final StackTrace stackTrace = firstEvent.stackTrace; - sb.append(innerIndentation) - .append(time) - .append(' ') - .append(note) - .append('\n'); + sb.append(innerIndentation).append(time).append(' ').append(note).append('\n'); if (includeStackTrace) { stackTrace.writeToBuilder(sb, indent + 2); } @@ -156,9 +145,9 @@ public synchronized void buildHistory( } /** - * Write the history of this object to the given {@link Logger}. The history - * includes the identifying string provided at construction time, and all the recorded - * events with their stack traces. + * Write the history of this object to the given {@link Logger}. The history includes the + * identifying string provided at construction time, and all the recorded events with their stack + * traces. * * @param logger {@link Logger} to write to */ diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/LargeMemoryUtil.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/LargeMemoryUtil.java index db63bbd14ba..53d1bdebecd 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/LargeMemoryUtil.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/LargeMemoryUtil.java @@ -24,10 +24,7 @@ public final class LargeMemoryUtil { private LargeMemoryUtil() {} - /** - * Casts length to an int, but raises an exception the value is outside - * the range of an int. - */ + /** Casts length to an int, but raises an exception the value is outside the range of an int. */ public static int checkedCastToInt(long length) { if (BoundsChecking.BOUNDS_CHECKING_ENABLED) { return Math.toIntExact(length); @@ -35,9 +32,7 @@ public static int checkedCastToInt(long length) { return (int) length; } - /** - * Returns a min(Integer.MAX_VALUE, length). - */ + /** Returns a min(Integer.MAX_VALUE, length). */ public static int capAtMaxInt(long length) { return (int) Math.min(length, Integer.MAX_VALUE); } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/MemoryUtil.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/MemoryUtil.java index 2f74a985a3f..c147ff9cf37 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/MemoryUtil.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/MemoryUtil.java @@ -24,37 +24,26 @@ import java.nio.ByteOrder; import java.security.AccessController; import java.security.PrivilegedAction; - import org.checkerframework.checker.nullness.qual.Nullable; - import sun.misc.Unsafe; - -/** - * Utilities for memory related operations. - */ +/** Utilities for memory related operations. */ public class MemoryUtil { - private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(MemoryUtil.class); + private static final org.slf4j.Logger logger = + org.slf4j.LoggerFactory.getLogger(MemoryUtil.class); private static final @Nullable Constructor DIRECT_BUFFER_CONSTRUCTOR; - /** - * The unsafe object from which to access the off-heap memory. - */ + + /** The unsafe object from which to access the off-heap memory. */ public static final Unsafe UNSAFE; - /** - * The start offset of array data relative to the start address of the array object. - */ + /** The start offset of array data relative to the start address of the array object. */ public static final long BYTE_ARRAY_BASE_OFFSET; - /** - * The offset of the address field with the {@link java.nio.ByteBuffer} object. - */ + /** The offset of the address field with the {@link java.nio.ByteBuffer} object. */ static final long BYTE_BUFFER_ADDRESS_OFFSET; - /** - * If the native byte order is little-endian. - */ + /** If the native byte order is little-endian. */ public static final boolean LITTLE_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN; // Java 1.8, 9, 11, 17, 21 becomes 1, 9, 11, 17, and 21. @@ -64,21 +53,23 @@ public class MemoryUtil { static { try { // try to get the unsafe object - final Object maybeUnsafe = AccessController.doPrivileged(new PrivilegedAction() { - @Override - @SuppressWarnings({"nullness:argument", "nullness:return"}) - // incompatible argument for parameter obj of Field.get - // incompatible types in return - public Object run() { - try { - final Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe"); - unsafeField.setAccessible(true); - return unsafeField.get(null); - } catch (Throwable e) { - return e; - } - } - }); + final Object maybeUnsafe = + AccessController.doPrivileged( + new PrivilegedAction() { + @Override + @SuppressWarnings({"nullness:argument", "nullness:return"}) + // incompatible argument for parameter obj of Field.get + // incompatible types in return + public Object run() { + try { + final Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe"); + unsafeField.setAccessible(true); + return unsafeField.get(null); + } catch (Throwable e) { + return e; + } + } + }); if (maybeUnsafe instanceof Throwable) { throw (Throwable) maybeUnsafe; @@ -100,25 +91,27 @@ public Object run() { try { final Object maybeDirectBufferConstructor = - AccessController.doPrivileged(new PrivilegedAction() { - @Override - public Object run() { - try { - final Constructor constructor = (majorVersion >= 21) ? - direct.getClass().getDeclaredConstructor(long.class, long.class) : - direct.getClass().getDeclaredConstructor(long.class, int.class); - constructor.setAccessible(true); - logger.debug("Constructor for direct buffer found and made accessible"); - return constructor; - } catch (NoSuchMethodException e) { - logger.debug("Cannot get constructor for direct buffer allocation", e); - return e; - } catch (SecurityException e) { - logger.debug("Cannot get constructor for direct buffer allocation", e); - return e; - } - } - }); + AccessController.doPrivileged( + new PrivilegedAction() { + @Override + public Object run() { + try { + final Constructor constructor = + (majorVersion >= 21) + ? direct.getClass().getDeclaredConstructor(long.class, long.class) + : direct.getClass().getDeclaredConstructor(long.class, int.class); + constructor.setAccessible(true); + logger.debug("Constructor for direct buffer found and made accessible"); + return constructor; + } catch (NoSuchMethodException e) { + logger.debug("Cannot get constructor for direct buffer allocation", e); + return e; + } catch (SecurityException e) { + logger.debug("Cannot get constructor for direct buffer allocation", e); + return e; + } + } + }); if (maybeDirectBufferConstructor instanceof Constructor) { address = UNSAFE.allocateMemory(1); @@ -133,8 +126,7 @@ public Object run() { } } else { logger.debug( - "direct buffer constructor: unavailable", - (Throwable) maybeDirectBufferConstructor); + "direct buffer constructor: unavailable", (Throwable) maybeDirectBufferConstructor); directBufferConstructor = null; } } finally { @@ -146,10 +138,12 @@ public Object run() { } catch (Throwable e) { // This exception will get swallowed, but it's necessary for the static analysis that ensures // the static fields above get initialized - final RuntimeException failure = new RuntimeException( - "Failed to initialize MemoryUtil. You must start Java with " + - "`--add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED` " + - "(See https://arrow.apache.org/docs/java/install.html)", e); + final RuntimeException failure = + new RuntimeException( + "Failed to initialize MemoryUtil. You must start Java with " + + "`--add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED` " + + "(See https://arrow.apache.org/docs/java/install.html)", + e); failure.printStackTrace(); throw failure; } @@ -165,12 +159,9 @@ public static long getByteBufferAddress(ByteBuffer buf) { return UNSAFE.getLong(buf, BYTE_BUFFER_ADDRESS_OFFSET); } - private MemoryUtil() { - } + private MemoryUtil() {} - /** - * Create nio byte buffer. - */ + /** Create nio byte buffer. */ public static ByteBuffer directBuffer(long address, int capacity) { if (DIRECT_BUFFER_CONSTRUCTOR != null) { if (capacity < 0) { @@ -186,10 +177,14 @@ public static ByteBuffer directBuffer(long address, int capacity) { "sun.misc.Unsafe or java.nio.DirectByteBuffer.(long, int) not available"); } - @SuppressWarnings("nullness:argument") //to handle null assignment on third party dependency: Unsafe - public static void copyMemory(@Nullable Object srcBase, long srcOffset, - @Nullable Object destBase, long destOffset, - long bytes) { + @SuppressWarnings( + "nullness:argument") // to handle null assignment on third party dependency: Unsafe + public static void copyMemory( + @Nullable Object srcBase, + long srcOffset, + @Nullable Object destBase, + long destOffset, + long bytes) { UNSAFE.copyMemory(srcBase, srcOffset, destBase, destOffset, bytes); } } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/StackTrace.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/StackTrace.java index a533edd7937..a26d0338989 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/StackTrace.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/StackTrace.java @@ -18,20 +18,14 @@ package org.apache.arrow.memory.util; import java.util.Arrays; - import org.checkerframework.checker.nullness.qual.Nullable; - -/** - * Convenient way of obtaining and manipulating stack traces for debugging. - */ +/** Convenient way of obtaining and manipulating stack traces for debugging. */ public class StackTrace { - private final @Nullable StackTraceElement [] stackTraceElements; + private final @Nullable StackTraceElement[] stackTraceElements; - /** - * Constructor. Captures the current stack trace. - */ + /** Constructor. Captures the current stack trace. */ public StackTrace() { final StackTraceElement[] stack = Thread.currentThread().getStackTrace(); // Skip first two elements to remove getStackTrace/StackTrace. @@ -41,7 +35,7 @@ public StackTrace() { /** * Write the stack trace to a StringBuilder. * - * @param sb where to write it + * @param sb where to write it * @param indent how many double spaces to indent each line */ public void writeToBuilder(final StringBuilder sb, final int indent) { diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/hash/ArrowBufHasher.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/hash/ArrowBufHasher.java index 0de8e62a4a4..25425754e92 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/hash/ArrowBufHasher.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/hash/ArrowBufHasher.java @@ -20,16 +20,16 @@ import org.apache.arrow.memory.ArrowBuf; /** - * Utility for calculating the hash code for a consecutive memory region. - * This class provides the basic framework for efficiently calculating the hash code. - *

- * A default light-weight implementation is given in {@link SimpleHasher}. - *

+ * Utility for calculating the hash code for a consecutive memory region. This class provides the + * basic framework for efficiently calculating the hash code. + * + *

A default light-weight implementation is given in {@link SimpleHasher}. */ public interface ArrowBufHasher { /** * Calculates the hash code for a memory region. + * * @param address start address of the memory region. * @param length length of the memory region. * @return the hash code. @@ -38,6 +38,7 @@ public interface ArrowBufHasher { /** * Calculates the hash code for a memory region. + * * @param buf the buffer for the memory region. * @param offset offset within the buffer for the memory region. * @param length length of the memory region. diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/hash/MurmurHasher.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/hash/MurmurHasher.java index 5de98d23bb8..69355ea1412 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/hash/MurmurHasher.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/hash/MurmurHasher.java @@ -22,32 +22,28 @@ import org.checkerframework.checker.nullness.qual.Nullable; /** - * Implementation of the Murmur hashing algorithm. - * Details of the algorithm can be found in + * Implementation of the Murmur hashing algorithm. Details of the algorithm can be found in * https://en.wikipedia.org/wiki/MurmurHash - *

- * Murmur hashing is computationally expensive, as it involves several - * integer multiplications. However, the produced hash codes have - * good quality in the sense that they are uniformly distributed in the universe. - *

- *

- * Therefore, this algorithm is suitable for scenarios where uniform hashing - * is desired (e.g. in an open addressing hash table/hash set). - *

+ * + *

Murmur hashing is computationally expensive, as it involves several integer multiplications. + * However, the produced hash codes have good quality in the sense that they are uniformly + * distributed in the universe. + * + *

Therefore, this algorithm is suitable for scenarios where uniform hashing is desired (e.g. in + * an open addressing hash table/hash set). */ public class MurmurHasher implements ArrowBufHasher { private final int seed; - /** - * Creates a default Murmur hasher, with seed 0. - */ + /** Creates a default Murmur hasher, with seed 0. */ public MurmurHasher() { this(0); } /** * Creates a Murmur hasher. + * * @param seed the seed for the hasher. */ public MurmurHasher(int seed) { @@ -67,6 +63,7 @@ public int hashCode(ArrowBuf buf, long offset, long length) { /** * Calculates the hash code for a memory region. + * * @param buf the buffer for the memory region. * @param offset offset within the buffer for the memory region. * @param length length of the memory region. @@ -80,6 +77,7 @@ public static int hashCode(ArrowBuf buf, long offset, long length, int seed) { /** * Calculates the hash code for a memory region. + * * @param address start address of the memory region. * @param length length of the memory region. * @param seed the seed. @@ -108,8 +106,8 @@ public static int hashCode(long address, long length, int seed) { } /** - * Combine the current hash code and a new int value to calculate - * a new hash code. + * Combine the current hash code and a new int value to calculate a new hash code. + * * @param currentHashCode the current hash code. * @param intValue the new int value. * @return the new hah code. @@ -137,6 +135,7 @@ public static int combineHashCode(int currentHashCode, int intValue) { /** * Finalizing the hash code. + * * @param hashCode the current hash code. * @param length the length of the memory region. * @return the finalized hash code. diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/hash/SimpleHasher.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/hash/SimpleHasher.java index 3bf3c2a8283..23f7a665214 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/hash/SimpleHasher.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/hash/SimpleHasher.java @@ -17,38 +17,33 @@ package org.apache.arrow.memory.util.hash; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.util.MemoryUtil; import org.checkerframework.checker.nullness.qual.Nullable; /** - * A simple hasher that calculates the hash code of integers as is, - * and does not perform any finalization. So the computation is extremely - * efficient. - *

- * This algorithm only provides the most basic semantics for the hash code. That is, - * if two objects are equal, they must have equal hash code. However, the quality of the - * produced hash code may not be good. In other words, the generated hash codes are - * far from being uniformly distributed in the universe. - *

- *

- * Therefore, this algorithm is suitable only for scenarios where the most basic semantics - * of the hash code is required (e.g. in scenarios that require fast and proactive data pruning) - *

- *

- * An object of this class is stateless, so it can be shared between threads. - *

+ * A simple hasher that calculates the hash code of integers as is, and does not perform any + * finalization. So the computation is extremely efficient. + * + *

This algorithm only provides the most basic semantics for the hash code. That is, if two + * objects are equal, they must have equal hash code. However, the quality of the produced hash code + * may not be good. In other words, the generated hash codes are far from being uniformly + * distributed in the universe. + * + *

Therefore, this algorithm is suitable only for scenarios where the most basic semantics of the + * hash code is required (e.g. in scenarios that require fast and proactive data pruning) + * + *

An object of this class is stateless, so it can be shared between threads. */ public class SimpleHasher implements ArrowBufHasher { public static SimpleHasher INSTANCE = new SimpleHasher(); - protected SimpleHasher() { - } + protected SimpleHasher() {} /** * Calculates the hash code for a memory region. + * * @param address start address of the memory region. * @param length length of the memory region. * @return the hash code. @@ -82,6 +77,7 @@ public int hashCode(long address, long length) { /** * Calculates the hash code for a memory region. + * * @param buf the buffer for the memory region. * @param offset offset within the buffer for the memory region. * @param length length of the memory region. diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/util/AutoCloseables.java b/java/memory/memory-core/src/main/java/org/apache/arrow/util/AutoCloseables.java index d965f2aed41..4c96b1baed4 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/util/AutoCloseables.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/util/AutoCloseables.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. @@ -25,17 +25,14 @@ import java.util.List; import java.util.stream.StreamSupport; -/** - * Utilities for AutoCloseable classes. - */ +/** Utilities for AutoCloseable classes. */ public final class AutoCloseables { // Utility class. Should not be instantiated - private AutoCloseables() { - } + private AutoCloseables() {} /** - * Returns a new {@link AutoCloseable} that calls {@link #close(Iterable)} on autoCloseables - * when close is called. + * Returns a new {@link AutoCloseable} that calls {@link #close(Iterable)} on autoCloseables + * when close is called. */ public static AutoCloseable all(final Collection autoCloseables) { return new AutoCloseable() { @@ -48,6 +45,7 @@ public void close() throws Exception { /** * Closes all autoCloseables if not null and suppresses exceptions by adding them to t. + * * @param t the throwable to add suppressed exception to * @param autoCloseables the closeables to close */ @@ -57,6 +55,7 @@ public static void close(Throwable t, AutoCloseable... autoCloseables) { /** * Closes all autoCloseables if not null and suppresses exceptions by adding them to t. + * * @param t the throwable to add suppressed exception to * @param autoCloseables the closeables to close */ @@ -70,6 +69,7 @@ public static void close(Throwable t, Iterable autoClos /** * Closes all autoCloseables if not null and suppresses subsequent exceptions if more than one. + * * @param autoCloseables the closeables to close */ public static void close(AutoCloseable... autoCloseables) throws Exception { @@ -78,6 +78,7 @@ public static void close(AutoCloseable... autoCloseables) throws Exception { /** * Closes all autoCloseables if not null and suppresses subsequent exceptions if more than one. + * * @param ac the closeables to close */ public static void close(Iterable ac) throws Exception { @@ -109,33 +110,32 @@ public static void close(Iterable ac) throws Exception } } - /** - * Calls {@link #close(Iterable)} on the flattened list of closeables. - */ + /** Calls {@link #close(Iterable)} on the flattened list of closeables. */ @SafeVarargs - public static void close(Iterable...closeables) throws Exception { + public static void close(Iterable... closeables) throws Exception { close(flatten(closeables)); } @SafeVarargs private static Iterable flatten(Iterable... closeables) { return new Iterable() { - // Cast from Iterable to Iterable is safe in this context + // Cast from Iterable to Iterable is safe in this + // context // since there's no modification of the original collection @SuppressWarnings("unchecked") @Override public Iterator iterator() { return Arrays.stream(closeables) - .flatMap((Iterable i) - -> StreamSupport.stream(((Iterable) i).spliterator(), /*parallel=*/false)) + .flatMap( + (Iterable i) -> + StreamSupport.stream( + ((Iterable) i).spliterator(), /* parallel= */ false)) .iterator(); } }; } - /** - * Converts ac to a {@link Iterable} filtering out any null values. - */ + /** Converts ac to a {@link Iterable} filtering out any null values. */ public static Iterable iter(AutoCloseable... ac) { if (ac.length == 0) { return Collections.emptyList(); @@ -150,9 +150,7 @@ public static Iterable iter(AutoCloseable... ac) { } } - /** - * A closeable wrapper that will close the underlying closeables if a commit does not occur. - */ + /** A closeable wrapper that will close the underlying closeables if a commit does not occur. */ public static class RollbackCloseable implements AutoCloseable { private boolean commit = false; @@ -167,16 +165,12 @@ public T add(T t) { return t; } - /** - * Add all of list to the rollback list. - */ + /** Add all of list to the rollback list. */ public void addAll(AutoCloseable... list) { closeables.addAll(Arrays.asList(list)); } - /** - * Add all of list to the rollback list. - */ + /** Add all of list to the rollback list. */ public void addAll(Iterable list) { for (AutoCloseable ac : list) { closeables.add(ac); @@ -193,26 +187,22 @@ public void close() throws Exception { AutoCloseables.close(closeables); } } - } - /** - * Creates an {@link RollbackCloseable} from the given closeables. - */ + /** Creates an {@link RollbackCloseable} from the given closeables. */ public static RollbackCloseable rollbackable(AutoCloseable... closeables) { return new RollbackCloseable(closeables); } /** - * close() an {@link java.lang.AutoCloseable} without throwing a (checked) - * {@link java.lang.Exception}. This wraps the close() call with a - * try-catch that will rethrow an Exception wrapped with a - * {@link java.lang.RuntimeException}, providing a way to call close() + * close() an {@link java.lang.AutoCloseable} without throwing a (checked) {@link + * java.lang.Exception}. This wraps the close() call with a try-catch that will rethrow an + * Exception wrapped with a {@link java.lang.RuntimeException}, providing a way to call close() * without having to do the try-catch everywhere or propagate the Exception. * * @param autoCloseable the AutoCloseable to close; may be null - * @throws RuntimeException if an Exception occurs; the Exception is - * wrapped by the RuntimeException + * @throws RuntimeException if an Exception occurs; the Exception is wrapped by the + * RuntimeException */ public static void closeNoChecked(final AutoCloseable autoCloseable) { if (autoCloseable != null) { @@ -224,11 +214,11 @@ public static void closeNoChecked(final AutoCloseable autoCloseable) { } } - private static final AutoCloseable noOpAutocloseable = new AutoCloseable() { - @Override - public void close() { - } - }; + private static final AutoCloseable noOpAutocloseable = + new AutoCloseable() { + @Override + public void close() {} + }; /** * Get an AutoCloseable that does nothing. @@ -239,4 +229,3 @@ public static AutoCloseable noop() { return noOpAutocloseable; } } - diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/util/Collections2.java b/java/memory/memory-core/src/main/java/org/apache/arrow/util/Collections2.java index 6b01a61ebca..0a1afd38538 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/util/Collections2.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/util/Collections2.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. @@ -31,23 +31,20 @@ import java.util.stream.StreamSupport; /** - * Utility methods for manipulating {@link java.util.Collections} and their subclasses/implementations. + * Utility methods for manipulating {@link java.util.Collections} and their + * subclasses/implementations. */ public final class Collections2 { private Collections2() {} - /** - * Creates a {@link List} from the elements remaining in iterator. - */ + /** Creates a {@link List} from the elements remaining in iterator. */ public static List toList(Iterator iterator) { List target = new ArrayList<>(); iterator.forEachRemaining(target::add); return target; } - /** - * Converts the iterable into a new {@link List}. - */ + /** Converts the iterable into a new {@link List}. */ public static List toList(Iterable iterable) { if (iterable instanceof Collection) { // If iterable is a collection, take advantage of it for a more efficient copy @@ -56,14 +53,11 @@ public static List toList(Iterable iterable) { return toList(iterable.iterator()); } - /** - * Converts the iterable into a new immutable {@link List}. - */ + /** Converts the iterable into a new immutable {@link List}. */ public static List toImmutableList(Iterable iterable) { return Collections.unmodifiableList(toList(iterable)); } - /** Copies the elements of map to a new unmodifiable map. */ public static Map immutableMapCopy(Map map) { return Collections.unmodifiableMap(new HashMap<>(map)); @@ -75,17 +69,18 @@ public static List immutableListCopy(List list) { } /** Copies the values to a new unmodifiable list. */ - public static List asImmutableList(V...values) { + public static List asImmutableList(V... values) { return Collections.unmodifiableList(Arrays.asList(values)); } /** * Creates a human readable string from the remaining elements in iterator. * - * The output should be similar to {@code Arrays#toString(Object[])} + *

The output should be similar to {@code Arrays#toString(Object[])} */ public static String toString(Iterator iterator) { - return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false) + return StreamSupport.stream( + Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false) .map(String::valueOf) .collect(Collectors.joining(", ", "[", "]")); } diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/util/VisibleForTesting.java b/java/memory/memory-core/src/main/java/org/apache/arrow/util/VisibleForTesting.java index b6ed378bba6..0ad64bca713 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/util/VisibleForTesting.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/util/VisibleForTesting.java @@ -18,9 +18,7 @@ package org.apache.arrow.util; /** - * Annotation to indicate a class member or class is visible - * only for the purposes of testing and otherwise should not - * be referenced by other classes. + * Annotation to indicate a class member or class is visible only for the purposes of testing and + * otherwise should not be referenced by other classes. */ -public @interface VisibleForTesting { -} +public @interface VisibleForTesting {} diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/CountingAllocationListener.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/CountingAllocationListener.java index 38e1a582b8c..781ccbbe15a 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/CountingAllocationListener.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/CountingAllocationListener.java @@ -17,15 +17,12 @@ package org.apache.arrow.memory; -import org.apache.arrow.memory.AllocationListener; -import org.apache.arrow.memory.AllocationOutcome; -import org.apache.arrow.memory.BufferAllocator; import org.checkerframework.checker.nullness.qual.Nullable; /** - * Counting allocation listener. - * It counts the number of times it has been invoked, and how much memory allocation it has seen - * When set to 'expand on fail', it attempts to expand the associated allocator's limit. + * Counting allocation listener. It counts the number of times it has been invoked, and how much + * memory allocation it has seen When set to 'expand on fail', it attempts to expand the associated + * allocator's limit. */ final class CountingAllocationListener implements AllocationListener { private int numPreCalls; @@ -35,8 +32,7 @@ final class CountingAllocationListener implements AllocationListener { private long totalMem; private long currentMem; private boolean expandOnFail; - @Nullable - BufferAllocator expandAlloc; + @Nullable BufferAllocator expandAlloc; long expandLimit; CountingAllocationListener() { @@ -65,8 +61,8 @@ public void onAllocation(long size) { public boolean onFailedAllocation(long size, AllocationOutcome outcome) { if (expandOnFail) { if (expandAlloc == null) { - throw new IllegalStateException("expandAlloc must be non-null because this " + - "listener is set to expand on failure."); + throw new IllegalStateException( + "expandAlloc must be non-null because this " + "listener is set to expand on failure."); } expandAlloc.setLimit(expandLimit); return true; @@ -74,7 +70,6 @@ public boolean onFailedAllocation(long size, AllocationOutcome outcome) { return false; } - @Override public void onRelease(long size) { numReleaseCalls++; diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/DefaultAllocationManagerFactory.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/DefaultAllocationManagerFactory.java index bfe496532b1..58184d203d4 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/DefaultAllocationManagerFactory.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/DefaultAllocationManagerFactory.java @@ -22,16 +22,13 @@ /** * The default Allocation Manager Factory for a module. * - * This is only used by tests and contains only a simplistic allocator method. - * + *

This is only used by tests and contains only a simplistic allocator method. */ public class DefaultAllocationManagerFactory implements AllocationManager.Factory { public static final AllocationManager.Factory FACTORY = new DefaultAllocationManagerFactory(); - private static final ArrowBuf EMPTY = new ArrowBuf(ReferenceManager.NO_OP, - null, - 0, - MemoryUtil.UNSAFE.allocateMemory(0)); + private static final ArrowBuf EMPTY = + new ArrowBuf(ReferenceManager.NO_OP, null, 0, MemoryUtil.UNSAFE.allocateMemory(0)); @Override public AllocationManager create(BufferAllocator accountingAllocator, long size) { diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestAccountant.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestAccountant.java index 13fa4a64d0a..cfff53e6af3 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestAccountant.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestAccountant.java @@ -46,21 +46,21 @@ public void multiThread() throws InterruptedException { Thread[] threads = new Thread[numberOfThreads]; for (int i = 0; i < numberOfThreads; i++) { - Thread t = new Thread() { - - @Override - public void run() { - try { - for (int i = 0; i < loops; i++) { - ensureAccurateReservations(parent); + Thread t = + new Thread() { + + @Override + public void run() { + try { + for (int i = 0; i < loops; i++) { + ensureAccurateReservations(parent); + } + } catch (Exception ex) { + ex.printStackTrace(); + Assert.fail(ex.getMessage()); + } } - } catch (Exception ex) { - ex.printStackTrace(); - Assert.fail(ex.getMessage()); - } - } - - }; + }; threads[i] = t; t.start(); } diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestAllocationManager.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestAllocationManager.java index df28424b3a9..e3756cb6988 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestAllocationManager.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestAllocationManager.java @@ -21,9 +21,7 @@ import org.junit.Test; -/** - * Test cases for {@link AllocationManager}. - */ +/** Test cases for {@link AllocationManager}. */ public class TestAllocationManager { @Test diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestArrowBuf.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestArrowBuf.java index 9ba42abc1ce..eb2f34fab5f 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestArrowBuf.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestArrowBuf.java @@ -23,25 +23,22 @@ import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; - import org.junit.Test; import org.slf4j.LoggerFactory; -import ch.qos.logback.classic.Level; -import ch.qos.logback.classic.Logger; - public class TestArrowBuf { @Test(expected = IndexOutOfBoundsException.class) public void testSliceOutOfBoundsLength_RaisesIndexOutOfBoundsException() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(2) - ) { + ArrowBuf buf = allocator.buffer(2)) { assertEquals(2, buf.capacity()); buf.slice(0, 3); } @@ -50,8 +47,7 @@ public void testSliceOutOfBoundsLength_RaisesIndexOutOfBoundsException() { @Test(expected = IndexOutOfBoundsException.class) public void testSliceOutOfBoundsIndexPlusLength_RaisesIndexOutOfBoundsException() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(2) - ) { + ArrowBuf buf = allocator.buffer(2)) { assertEquals(2, buf.capacity()); buf.slice(1, 2); } @@ -60,8 +56,7 @@ public void testSliceOutOfBoundsIndexPlusLength_RaisesIndexOutOfBoundsException( @Test(expected = IndexOutOfBoundsException.class) public void testSliceOutOfBoundsIndex_RaisesIndexOutOfBoundsException() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(2) - ) { + ArrowBuf buf = allocator.buffer(2)) { assertEquals(2, buf.capacity()); buf.slice(3, 0); } @@ -70,8 +65,7 @@ public void testSliceOutOfBoundsIndex_RaisesIndexOutOfBoundsException() { @Test public void testSliceWithinBoundsLength_ReturnsSlice() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(2) - ) { + ArrowBuf buf = allocator.buffer(2)) { assertEquals(2, buf.capacity()); assertEquals(1, buf.slice(1, 1).capacity()); assertEquals(2, buf.slice(0, 2).capacity()); @@ -87,7 +81,7 @@ public void testSetBytesSliced() { } ByteBuffer data = ByteBuffer.wrap(expected); try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(expected.length)) { + ArrowBuf buf = allocator.buffer(expected.length)) { buf.setBytes(0, data, 0, data.capacity()); byte[] actual = new byte[expected.length]; @@ -125,7 +119,8 @@ public void testSetBytesBigEndian() { for (int i = 0; i < expected.length; i++) { expected[i] = (byte) i; } - // Only this code path is susceptible: others use unsafe or byte-by-byte copies, while this override copies longs. + // Only this code path is susceptible: others use unsafe or byte-by-byte copies, while this + // override copies longs. final ByteBuffer data = ByteBuffer.wrap(expected).asReadOnlyBuffer(); assertFalse(data.hasArray()); assertFalse(data.isDirect()); @@ -141,8 +136,8 @@ public void testSetBytesBigEndian() { @Test /** - * Test that allocation history is not recorded even though - * assertions are enabled in tests (GH-34338). + * Test that allocation history is not recorded even though assertions are enabled in tests + * (GH-34338). */ public void testEnabledAssertion() { ((Logger) LoggerFactory.getLogger("org.apache.arrow")).setLevel(Level.TRACE); @@ -168,13 +163,15 @@ public void testEnabledHistoricalLog() { try (BufferAllocator allocator = new RootAllocator(128)) { allocator.buffer(2); Exception e = assertThrows(IllegalStateException.class, allocator::close); - assertTrue("Exception had the following message: " + e.getMessage(), + assertTrue( + "Exception had the following message: " + e.getMessage(), e.getMessage().contains("event log for:")); // JDK8, JDK11 } finally { fieldDebug.set(null, false); } } catch (Exception e) { - assertTrue("Exception had the following toString(): " + e.toString(), + assertTrue( + "Exception had the following toString(): " + e.toString(), e.toString().contains("java.lang.NoSuchFieldException: modifiers")); // JDK17+ } finally { ((Logger) LoggerFactory.getLogger("org.apache.arrow")).setLevel(null); diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java index 535d5c15e89..5b37390c6eb 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java @@ -17,7 +17,6 @@ package org.apache.arrow.memory; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; @@ -31,7 +30,6 @@ import java.util.Collection; import java.util.Collections; import java.util.Iterator; - import org.apache.arrow.memory.AllocationOutcomeDetails.Entry; import org.apache.arrow.memory.rounding.RoundingPolicy; import org.apache.arrow.memory.rounding.SegmentRoundingPolicy; @@ -40,7 +38,6 @@ import org.junit.Ignore; import org.junit.Test; import org.junit.jupiter.api.Assertions; - import sun.misc.Unsafe; public class TestBaseAllocator { @@ -69,16 +66,14 @@ public void checkBuffers() { // ---------------------------------------- DEBUG ------------------------------------ */ - @Test public void test_privateMax() throws Exception { - try (final RootAllocator rootAllocator = - new RootAllocator(MAX_ALLOCATION)) { + try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { final ArrowBuf arrowBuf1 = rootAllocator.buffer(MAX_ALLOCATION / 2); assertNotNull("allocation failed", arrowBuf1); try (final BufferAllocator childAllocator = - rootAllocator.newChildAllocator("noLimits", 0, MAX_ALLOCATION)) { + rootAllocator.newChildAllocator("noLimits", 0, MAX_ALLOCATION)) { final ArrowBuf arrowBuf2 = childAllocator.buffer(MAX_ALLOCATION / 2); assertNotNull("allocation failed", arrowBuf2); arrowBuf2.getReferenceManager().release(); @@ -91,8 +86,7 @@ public void test_privateMax() throws Exception { @Test(expected = IllegalStateException.class) public void testRootAllocator_closeWithOutstanding() throws Exception { try { - try (final RootAllocator rootAllocator = - new RootAllocator(MAX_ALLOCATION)) { + try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { final ArrowBuf arrowBuf = rootAllocator.buffer(512); assertNotNull("allocation failed", arrowBuf); } @@ -114,8 +108,7 @@ public void testRootAllocator_closeWithOutstanding() throws Exception { @Test @Ignore public void testRootAllocator_getEmpty() throws Exception { - try (final RootAllocator rootAllocator = - new RootAllocator(MAX_ALLOCATION)) { + try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { final ArrowBuf arrowBuf = rootAllocator.buffer(0); assertNotNull("allocation failed", arrowBuf); assertEquals("capacity was non-zero", 0, arrowBuf.capacity()); @@ -127,8 +120,7 @@ public void testRootAllocator_getEmpty() throws Exception { @Ignore // TODO(DRILL-2740) @Test(expected = IllegalStateException.class) public void testAllocator_unreleasedEmpty() throws Exception { - try (final RootAllocator rootAllocator = - new RootAllocator(MAX_ALLOCATION)) { + try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { @SuppressWarnings("unused") final ArrowBuf arrowBuf = rootAllocator.buffer(0); } @@ -136,8 +128,7 @@ public void testAllocator_unreleasedEmpty() throws Exception { @Test public void testAllocator_transferOwnership() throws Exception { - try (final RootAllocator rootAllocator = - new RootAllocator(MAX_ALLOCATION)) { + try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { final BufferAllocator childAllocator1 = rootAllocator.newChildAllocator("changeOwnership1", 0, MAX_ALLOCATION); final BufferAllocator childAllocator2 = @@ -146,7 +137,8 @@ public void testAllocator_transferOwnership() throws Exception { final ArrowBuf arrowBuf1 = childAllocator1.buffer(MAX_ALLOCATION / 4); rootAllocator.verify(); final ReferenceManager referenceManager = arrowBuf1.getReferenceManager(); - OwnershipTransferResult transferOwnership = referenceManager.transferOwnership(arrowBuf1, childAllocator2); + OwnershipTransferResult transferOwnership = + referenceManager.transferOwnership(arrowBuf1, childAllocator2); assertEquiv(arrowBuf1, transferOwnership.getTransferredBuffer()); final boolean allocationFit = transferOwnership.getAllocationFit(); rootAllocator.verify(); @@ -179,14 +171,17 @@ public void testAllocator_getParentAndChild() throws Exception { try (final BufferAllocator childAllocator2 = rootAllocator.newChildAllocator("child2", 0, MAX_ALLOCATION)) { assertEquals(childAllocator2.getParentAllocator(), rootAllocator); - assertTrue(equalsIgnoreOrder(Arrays.asList(childAllocator1, childAllocator2), - rootAllocator.getChildAllocators())); + assertTrue( + equalsIgnoreOrder( + Arrays.asList(childAllocator1, childAllocator2), + rootAllocator.getChildAllocators())); try (final BufferAllocator grandChildAllocator = childAllocator1.newChildAllocator("grand-child", 0, MAX_ALLOCATION)) { assertEquals(grandChildAllocator.getParentAllocator(), childAllocator1); - assertTrue(equalsIgnoreOrder(Arrays.asList(grandChildAllocator), - childAllocator1.getChildAllocators())); + assertTrue( + equalsIgnoreOrder( + Arrays.asList(grandChildAllocator), childAllocator1.getChildAllocators())); } } } @@ -202,15 +197,18 @@ public void testAllocator_childRemovedOnClose() throws Exception { rootAllocator.newChildAllocator("child2", 0, MAX_ALLOCATION)) { // root has two child allocators - assertTrue(equalsIgnoreOrder(Arrays.asList(childAllocator1, childAllocator2), - rootAllocator.getChildAllocators())); + assertTrue( + equalsIgnoreOrder( + Arrays.asList(childAllocator1, childAllocator2), + rootAllocator.getChildAllocators())); try (final BufferAllocator grandChildAllocator = childAllocator1.newChildAllocator("grand-child", 0, MAX_ALLOCATION)) { // child1 has one allocator i.e grand-child - assertTrue(equalsIgnoreOrder(Arrays.asList(grandChildAllocator), - childAllocator1.getChildAllocators())); + assertTrue( + equalsIgnoreOrder( + Arrays.asList(grandChildAllocator), childAllocator1.getChildAllocators())); } // grand-child closed @@ -229,10 +227,10 @@ public void testAllocator_childRemovedOnClose() throws Exception { @Test public void testAllocator_shareOwnership() throws Exception { try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { - final BufferAllocator childAllocator1 = rootAllocator.newChildAllocator("shareOwnership1", 0, - MAX_ALLOCATION); - final BufferAllocator childAllocator2 = rootAllocator.newChildAllocator("shareOwnership2", 0, - MAX_ALLOCATION); + final BufferAllocator childAllocator1 = + rootAllocator.newChildAllocator("shareOwnership1", 0, MAX_ALLOCATION); + final BufferAllocator childAllocator2 = + rootAllocator.newChildAllocator("shareOwnership2", 0, MAX_ALLOCATION); final ArrowBuf arrowBuf1 = childAllocator1.buffer(MAX_ALLOCATION / 4); rootAllocator.verify(); @@ -250,8 +248,8 @@ public void testAllocator_shareOwnership() throws Exception { childAllocator1.close(); rootAllocator.verify(); - final BufferAllocator childAllocator3 = rootAllocator.newChildAllocator("shareOwnership3", 0, - MAX_ALLOCATION); + final BufferAllocator childAllocator3 = + rootAllocator.newChildAllocator("shareOwnership3", 0, MAX_ALLOCATION); final ArrowBuf arrowBuf3 = arrowBuf1.getReferenceManager().retain(arrowBuf1, childAllocator3); assertNotNull(arrowBuf3); assertNotEquals(arrowBuf3, arrowBuf1); @@ -273,8 +271,8 @@ public void testAllocator_shareOwnership() throws Exception { @Test public void testRootAllocator_createChildAndUse() throws Exception { try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { - try (final BufferAllocator childAllocator = rootAllocator.newChildAllocator( - "createChildAndUse", 0, MAX_ALLOCATION)) { + try (final BufferAllocator childAllocator = + rootAllocator.newChildAllocator("createChildAndUse", 0, MAX_ALLOCATION)) { final ArrowBuf arrowBuf = childAllocator.buffer(512); assertNotNull("allocation failed", arrowBuf); arrowBuf.getReferenceManager().release(); @@ -286,8 +284,8 @@ public void testRootAllocator_createChildAndUse() throws Exception { public void testRootAllocator_createChildDontClose() throws Exception { try { try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { - final BufferAllocator childAllocator = rootAllocator.newChildAllocator( - "createChildDontClose", 0, MAX_ALLOCATION); + final BufferAllocator childAllocator = + rootAllocator.newChildAllocator("createChildDontClose", 0, MAX_ALLOCATION); final ArrowBuf arrowBuf = childAllocator.buffer(512); assertNotNull("allocation failed", arrowBuf); } @@ -309,7 +307,8 @@ public void testRootAllocator_createChildDontClose() throws Exception { @Test public void testSegmentAllocator() { RoundingPolicy policy = new SegmentRoundingPolicy(1024); - try (RootAllocator allocator = new RootAllocator(AllocationListener.NOOP, 1024 * 1024, policy)) { + try (RootAllocator allocator = + new RootAllocator(AllocationListener.NOOP, 1024 * 1024, policy)) { ArrowBuf buf = allocator.buffer(798); assertEquals(1024, buf.capacity()); buf.setInt(333, 959); @@ -348,19 +347,17 @@ public void testSegmentAllocator_childAllocator() { @Test public void testSegmentAllocator_smallSegment() { - IllegalArgumentException e = Assertions.assertThrows( - IllegalArgumentException.class, - () -> new SegmentRoundingPolicy(128) - ); + IllegalArgumentException e = + Assertions.assertThrows( + IllegalArgumentException.class, () -> new SegmentRoundingPolicy(128)); assertEquals("The segment size cannot be smaller than 1024", e.getMessage()); } @Test public void testSegmentAllocator_segmentSizeNotPowerOf2() { - IllegalArgumentException e = Assertions.assertThrows( - IllegalArgumentException.class, - () -> new SegmentRoundingPolicy(4097) - ); + IllegalArgumentException e = + Assertions.assertThrows( + IllegalArgumentException.class, () -> new SegmentRoundingPolicy(4097)); assertEquals("The segment size must be a power of 2", e.getMessage()); } @@ -391,53 +388,57 @@ public void testCustomizedAllocationManager() { } private BaseAllocator createAllocatorWithCustomizedAllocationManager() { - return new RootAllocator(BaseAllocator.configBuilder() - .maxAllocation(MAX_ALLOCATION) - .allocationManagerFactory(new AllocationManager.Factory() { - @Override - public AllocationManager create(BufferAllocator accountingAllocator, long requestedSize) { - return new AllocationManager(accountingAllocator) { - private final Unsafe unsafe = getUnsafe(); - private final long address = unsafe.allocateMemory(requestedSize); - - @Override - protected long memoryAddress() { - return address; - } - - @Override - protected void release0() { - unsafe.setMemory(address, requestedSize, (byte) 0); - unsafe.freeMemory(address); - } - - @Override - public long getSize() { - return requestedSize; - } - - private Unsafe getUnsafe() { - Field f = null; - try { - f = Unsafe.class.getDeclaredField("theUnsafe"); - f.setAccessible(true); - return (Unsafe) f.get(null); - } catch (NoSuchFieldException | IllegalAccessException e) { - throw new RuntimeException(e); - } finally { - if (f != null) { - f.setAccessible(false); + return new RootAllocator( + BaseAllocator.configBuilder() + .maxAllocation(MAX_ALLOCATION) + .allocationManagerFactory( + new AllocationManager.Factory() { + @Override + public AllocationManager create( + BufferAllocator accountingAllocator, long requestedSize) { + return new AllocationManager(accountingAllocator) { + private final Unsafe unsafe = getUnsafe(); + private final long address = unsafe.allocateMemory(requestedSize); + + @Override + protected long memoryAddress() { + return address; + } + + @Override + protected void release0() { + unsafe.setMemory(address, requestedSize, (byte) 0); + unsafe.freeMemory(address); + } + + @Override + public long getSize() { + return requestedSize; + } + + private Unsafe getUnsafe() { + Field f = null; + try { + f = Unsafe.class.getDeclaredField("theUnsafe"); + f.setAccessible(true); + return (Unsafe) f.get(null); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new RuntimeException(e); + } finally { + if (f != null) { + f.setAccessible(false); + } + } + } + }; } - } - } - }; - } - @Override - public ArrowBuf empty() { - return null; - } - }).build()); + @Override + public ArrowBuf empty() { + return null; + } + }) + .build()); } @Test @@ -467,7 +468,8 @@ public void testRootAllocator_listeners() throws Exception { Assert.assertEquals(16, listener1.getTotalMem()); buf1.getReferenceManager().release(); try (final BufferAllocator c2 = c1.newChildAllocator("c2", listener2, 0, MAX_ALLOCATION)) { - Assert.assertEquals(2, listener1.getNumChildren()); // c1 got a new child, so listener1 is notified. + Assert.assertEquals( + 2, listener1.getNumChildren()); // c1 got a new child, so listener1 is notified. Assert.assertEquals(0, listener2.getNumChildren()); final ArrowBuf buf2 = c2.buffer(32); assertNotNull("allocation failed", buf2); @@ -514,8 +516,8 @@ public void testRootAllocator_listenerAllocationFail() throws Exception { // allocation. The listener's callback triggers, expanding the child allocator's limit, so then // the allocation succeeds. try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { - try (final BufferAllocator c1 = rootAllocator.newChildAllocator("c1", listener1, 0, - MAX_ALLOCATION / 2)) { + try (final BufferAllocator c1 = + rootAllocator.newChildAllocator("c1", listener1, 0, MAX_ALLOCATION / 2)) { try { c1.buffer(MAX_ALLOCATION); fail("allocated memory beyond max allowed"); @@ -558,10 +560,9 @@ private static void allocateAndFree(final BufferAllocator allocator) { @Test public void testAllocator_manyAllocations() throws Exception { - try (final RootAllocator rootAllocator = - new RootAllocator(MAX_ALLOCATION)) { + try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { try (final BufferAllocator childAllocator = - rootAllocator.newChildAllocator("manyAllocations", 0, MAX_ALLOCATION)) { + rootAllocator.newChildAllocator("manyAllocations", 0, MAX_ALLOCATION)) { allocateAndFree(childAllocator); } } @@ -569,10 +570,9 @@ public void testAllocator_manyAllocations() throws Exception { @Test public void testAllocator_overAllocate() throws Exception { - try (final RootAllocator rootAllocator = - new RootAllocator(MAX_ALLOCATION)) { + try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { try (final BufferAllocator childAllocator = - rootAllocator.newChildAllocator("overAllocate", 0, MAX_ALLOCATION)) { + rootAllocator.newChildAllocator("overAllocate", 0, MAX_ALLOCATION)) { allocateAndFree(childAllocator); try { @@ -587,10 +587,9 @@ public void testAllocator_overAllocate() throws Exception { @Test public void testAllocator_overAllocateParent() throws Exception { - try (final RootAllocator rootAllocator = - new RootAllocator(MAX_ALLOCATION)) { + try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { try (final BufferAllocator childAllocator = - rootAllocator.newChildAllocator("overAllocateParent", 0, MAX_ALLOCATION)) { + rootAllocator.newChildAllocator("overAllocateParent", 0, MAX_ALLOCATION)) { final ArrowBuf arrowBuf1 = rootAllocator.buffer(MAX_ALLOCATION / 2); assertNotNull("allocation failed", arrowBuf1); final ArrowBuf arrowBuf2 = childAllocator.buffer(MAX_ALLOCATION / 2); @@ -611,14 +610,14 @@ public void testAllocator_overAllocateParent() throws Exception { @Test public void testAllocator_failureAtParentLimitOutcomeDetails() throws Exception { - try (final RootAllocator rootAllocator = - new RootAllocator(MAX_ALLOCATION)) { + try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { try (final BufferAllocator childAllocator = rootAllocator.newChildAllocator("child", 0, MAX_ALLOCATION / 2)) { try (final BufferAllocator grandChildAllocator = childAllocator.newChildAllocator("grandchild", MAX_ALLOCATION / 4, MAX_ALLOCATION)) { - OutOfMemoryException e = assertThrows(OutOfMemoryException.class, - () -> grandChildAllocator.buffer(MAX_ALLOCATION)); + OutOfMemoryException e = + assertThrows( + OutOfMemoryException.class, () -> grandChildAllocator.buffer(MAX_ALLOCATION)); // expected assertTrue(e.getMessage().contains("Unable to allocate buffer")); @@ -648,14 +647,14 @@ public void testAllocator_failureAtParentLimitOutcomeDetails() throws Exception @Test public void testAllocator_failureAtRootLimitOutcomeDetails() throws Exception { - try (final RootAllocator rootAllocator = - new RootAllocator(MAX_ALLOCATION)) { + try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { try (final BufferAllocator childAllocator = rootAllocator.newChildAllocator("child", MAX_ALLOCATION / 2, Long.MAX_VALUE)) { try (final BufferAllocator grandChildAllocator = childAllocator.newChildAllocator("grandchild", MAX_ALLOCATION / 4, Long.MAX_VALUE)) { - OutOfMemoryException e = assertThrows(OutOfMemoryException.class, - () -> grandChildAllocator.buffer(MAX_ALLOCATION * 2)); + OutOfMemoryException e = + assertThrows( + OutOfMemoryException.class, () -> grandChildAllocator.buffer(MAX_ALLOCATION * 2)); assertTrue(e.getMessage().contains("Unable to allocate buffer")); assertTrue("missing outcome details", e.getOutcomeDetails().isPresent()); @@ -699,7 +698,9 @@ private static void testAllocator_sliceUpBufferAndRelease( final ArrowBuf arrowBuf4 = arrowBuf3.slice(16, arrowBuf3.capacity() - 32); rootAllocator.verify(); - arrowBuf3.getReferenceManager().release(); // since they share refcounts, one is enough to release them all + arrowBuf3 + .getReferenceManager() + .release(); // since they share refcounts, one is enough to release them all rootAllocator.verify(); } @@ -708,18 +709,18 @@ public void testAllocator_createSlices() throws Exception { try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { testAllocator_sliceUpBufferAndRelease(rootAllocator, rootAllocator); - try (final BufferAllocator childAllocator = rootAllocator.newChildAllocator("createSlices", 0, - MAX_ALLOCATION)) { + try (final BufferAllocator childAllocator = + rootAllocator.newChildAllocator("createSlices", 0, MAX_ALLOCATION)) { testAllocator_sliceUpBufferAndRelease(rootAllocator, childAllocator); } rootAllocator.verify(); testAllocator_sliceUpBufferAndRelease(rootAllocator, rootAllocator); - try (final BufferAllocator childAllocator = rootAllocator.newChildAllocator("createSlices", 0, - MAX_ALLOCATION)) { + try (final BufferAllocator childAllocator = + rootAllocator.newChildAllocator("createSlices", 0, MAX_ALLOCATION)) { try (final BufferAllocator childAllocator2 = - childAllocator.newChildAllocator("createSlices", 0, MAX_ALLOCATION)) { + childAllocator.newChildAllocator("createSlices", 0, MAX_ALLOCATION)) { final ArrowBuf arrowBuf1 = childAllocator2.buffer(MAX_ALLOCATION / 8); @SuppressWarnings("unused") final ArrowBuf arrowBuf2 = arrowBuf1.slice(MAX_ALLOCATION / 16, MAX_ALLOCATION / 16); @@ -738,8 +739,7 @@ public void testAllocator_createSlices() throws Exception { @Test public void testAllocator_sliceRanges() throws Exception { // final AllocatorOwner allocatorOwner = new NamedOwner("sliceRanges"); - try (final RootAllocator rootAllocator = - new RootAllocator(MAX_ALLOCATION)) { + try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { // Populate a buffer with byte values corresponding to their indices. final ArrowBuf arrowBuf = rootAllocator.buffer(256); assertEquals(256, arrowBuf.capacity()); @@ -797,8 +797,7 @@ public void testAllocator_sliceRanges() throws Exception { @Test public void testAllocator_slicesOfSlices() throws Exception { // final AllocatorOwner allocatorOwner = new NamedOwner("slicesOfSlices"); - try (final RootAllocator rootAllocator = - new RootAllocator(MAX_ALLOCATION)) { + try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { // Populate a buffer with byte values corresponding to their indices. final ArrowBuf arrowBuf = rootAllocator.buffer(256); for (int i = 0; i < 256; ++i) { @@ -833,8 +832,10 @@ public void testAllocator_slicesOfSlices() throws Exception { @Test public void testAllocator_transferSliced() throws Exception { try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { - final BufferAllocator childAllocator1 = rootAllocator.newChildAllocator("transferSliced1", 0, MAX_ALLOCATION); - final BufferAllocator childAllocator2 = rootAllocator.newChildAllocator("transferSliced2", 0, MAX_ALLOCATION); + final BufferAllocator childAllocator1 = + rootAllocator.newChildAllocator("transferSliced1", 0, MAX_ALLOCATION); + final BufferAllocator childAllocator2 = + rootAllocator.newChildAllocator("transferSliced2", 0, MAX_ALLOCATION); final ArrowBuf arrowBuf1 = childAllocator1.buffer(MAX_ALLOCATION / 8); final ArrowBuf arrowBuf2 = childAllocator2.buffer(MAX_ALLOCATION / 8); @@ -844,10 +845,12 @@ public void testAllocator_transferSliced() throws Exception { rootAllocator.verify(); - OwnershipTransferResult result1 = arrowBuf2s.getReferenceManager().transferOwnership(arrowBuf2s, childAllocator1); + OwnershipTransferResult result1 = + arrowBuf2s.getReferenceManager().transferOwnership(arrowBuf2s, childAllocator1); assertEquiv(arrowBuf2s, result1.getTransferredBuffer()); rootAllocator.verify(); - OwnershipTransferResult result2 = arrowBuf1s.getReferenceManager().transferOwnership(arrowBuf1s, childAllocator2); + OwnershipTransferResult result2 = + arrowBuf1s.getReferenceManager().transferOwnership(arrowBuf1s, childAllocator2); assertEquiv(arrowBuf1s, result2.getTransferredBuffer()); rootAllocator.verify(); @@ -865,8 +868,10 @@ public void testAllocator_transferSliced() throws Exception { @Test public void testAllocator_shareSliced() throws Exception { try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { - final BufferAllocator childAllocator1 = rootAllocator.newChildAllocator("transferSliced", 0, MAX_ALLOCATION); - final BufferAllocator childAllocator2 = rootAllocator.newChildAllocator("transferSliced", 0, MAX_ALLOCATION); + final BufferAllocator childAllocator1 = + rootAllocator.newChildAllocator("transferSliced", 0, MAX_ALLOCATION); + final BufferAllocator childAllocator2 = + rootAllocator.newChildAllocator("transferSliced", 0, MAX_ALLOCATION); final ArrowBuf arrowBuf1 = childAllocator1.buffer(MAX_ALLOCATION / 8); final ArrowBuf arrowBuf2 = childAllocator2.buffer(MAX_ALLOCATION / 8); @@ -876,9 +881,11 @@ public void testAllocator_shareSliced() throws Exception { rootAllocator.verify(); - final ArrowBuf arrowBuf2s1 = arrowBuf2s.getReferenceManager().retain(arrowBuf2s, childAllocator1); + final ArrowBuf arrowBuf2s1 = + arrowBuf2s.getReferenceManager().retain(arrowBuf2s, childAllocator1); assertEquiv(arrowBuf2s, arrowBuf2s1); - final ArrowBuf arrowBuf1s2 = arrowBuf1s.getReferenceManager().retain(arrowBuf1s, childAllocator2); + final ArrowBuf arrowBuf1s2 = + arrowBuf1s.getReferenceManager().retain(arrowBuf1s, childAllocator2); assertEquiv(arrowBuf1s, arrowBuf1s2); rootAllocator.verify(); @@ -897,9 +904,12 @@ public void testAllocator_shareSliced() throws Exception { @Test public void testAllocator_transferShared() throws Exception { try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { - final BufferAllocator childAllocator1 = rootAllocator.newChildAllocator("transferShared1", 0, MAX_ALLOCATION); - final BufferAllocator childAllocator2 = rootAllocator.newChildAllocator("transferShared2", 0, MAX_ALLOCATION); - final BufferAllocator childAllocator3 = rootAllocator.newChildAllocator("transferShared3", 0, MAX_ALLOCATION); + final BufferAllocator childAllocator1 = + rootAllocator.newChildAllocator("transferShared1", 0, MAX_ALLOCATION); + final BufferAllocator childAllocator2 = + rootAllocator.newChildAllocator("transferShared2", 0, MAX_ALLOCATION); + final BufferAllocator childAllocator3 = + rootAllocator.newChildAllocator("transferShared3", 0, MAX_ALLOCATION); final ArrowBuf arrowBuf1 = childAllocator1.buffer(MAX_ALLOCATION / 8); @@ -912,7 +922,8 @@ public void testAllocator_transferShared() throws Exception { assertEquiv(arrowBuf1, arrowBuf2); final ReferenceManager refManager1 = arrowBuf1.getReferenceManager(); - final OwnershipTransferResult result1 = refManager1.transferOwnership(arrowBuf1, childAllocator3); + final OwnershipTransferResult result1 = + refManager1.transferOwnership(arrowBuf1, childAllocator3); allocationFit = result1.getAllocationFit(); final ArrowBuf arrowBuf3 = result1.getTransferredBuffer(); assertTrue(allocationFit); @@ -928,9 +939,11 @@ public void testAllocator_transferShared() throws Exception { childAllocator2.close(); rootAllocator.verify(); - final BufferAllocator childAllocator4 = rootAllocator.newChildAllocator("transferShared4", 0, MAX_ALLOCATION); + final BufferAllocator childAllocator4 = + rootAllocator.newChildAllocator("transferShared4", 0, MAX_ALLOCATION); final ReferenceManager refManager3 = arrowBuf3.getReferenceManager(); - final OwnershipTransferResult result3 = refManager3.transferOwnership(arrowBuf3, childAllocator4); + final OwnershipTransferResult result3 = + refManager3.transferOwnership(arrowBuf3, childAllocator4); allocationFit = result3.getAllocationFit(); final ArrowBuf arrowBuf4 = result3.getTransferredBuffer(); assertTrue(allocationFit); @@ -951,7 +964,7 @@ public void testAllocator_transferShared() throws Exception { public void testAllocator_unclaimedReservation() throws Exception { try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { try (final BufferAllocator childAllocator1 = - rootAllocator.newChildAllocator("unclaimedReservation", 0, MAX_ALLOCATION)) { + rootAllocator.newChildAllocator("unclaimedReservation", 0, MAX_ALLOCATION)) { try (final AllocationReservation reservation = childAllocator1.newReservation()) { assertTrue(reservation.add(64)); } @@ -964,8 +977,8 @@ public void testAllocator_unclaimedReservation() throws Exception { public void testAllocator_claimedReservation() throws Exception { try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { - try (final BufferAllocator childAllocator1 = rootAllocator.newChildAllocator( - "claimedReservation", 0, MAX_ALLOCATION)) { + try (final BufferAllocator childAllocator1 = + rootAllocator.newChildAllocator("claimedReservation", 0, MAX_ALLOCATION)) { try (final AllocationReservation reservation = childAllocator1.newReservation()) { assertTrue(reservation.add(32)); @@ -986,8 +999,8 @@ public void testAllocator_claimedReservation() throws Exception { @Test public void testInitReservationAndLimit() throws Exception { try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { - try (final BufferAllocator childAllocator = rootAllocator.newChildAllocator( - "child", 2048, 4096)) { + try (final BufferAllocator childAllocator = + rootAllocator.newChildAllocator("child", 2048, 4096)) { assertEquals(2048, childAllocator.getInitReservation()); assertEquals(4096, childAllocator.getLimit()); } @@ -1055,11 +1068,11 @@ public void multiple() throws Exception { frag1.close(); frag2.close(); frag3.close(); - } } - // This test needs to run in non-debug mode. So disabling the assertion status through class loader for this. + // This test needs to run in non-debug mode. So disabling the assertion status through class + // loader for this. // The test passes if run individually with -Dtest=TestBaseAllocator#testMemoryLeakWithReservation // but fails generally since the assertion status cannot be changed once the class is initialized. // So setting the test to @ignore @@ -1067,27 +1080,35 @@ public void multiple() throws Exception { @Ignore public void testMemoryLeakWithReservation() throws Exception { // disabling assertion status - AssertionUtil.class.getClassLoader().setClassAssertionStatus(AssertionUtil.class.getName(), false); + AssertionUtil.class + .getClassLoader() + .setClassAssertionStatus(AssertionUtil.class.getName(), false); try (RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { - ChildAllocator childAllocator1 = (ChildAllocator) rootAllocator.newChildAllocator( - "child1", 1024, MAX_ALLOCATION); + ChildAllocator childAllocator1 = + (ChildAllocator) rootAllocator.newChildAllocator("child1", 1024, MAX_ALLOCATION); rootAllocator.verify(); - ChildAllocator childAllocator2 = (ChildAllocator) childAllocator1.newChildAllocator( - "child2", 1024, MAX_ALLOCATION); + ChildAllocator childAllocator2 = + (ChildAllocator) childAllocator1.newChildAllocator("child2", 1024, MAX_ALLOCATION); rootAllocator.verify(); ArrowBuf buff = childAllocator2.buffer(256); - Exception exception = assertThrows(IllegalStateException.class, () -> { - childAllocator2.close(); - }); + Exception exception = + assertThrows( + IllegalStateException.class, + () -> { + childAllocator2.close(); + }); String exMessage = exception.getMessage(); assertTrue(exMessage.contains("Memory leaked: (256)")); - exception = assertThrows(IllegalStateException.class, () -> { - childAllocator1.close(); - }); + exception = + assertThrows( + IllegalStateException.class, + () -> { + childAllocator1.close(); + }); exMessage = exception.getMessage(); assertTrue(exMessage.contains("Memory leaked: (256)")); } @@ -1097,11 +1118,13 @@ public void testMemoryLeakWithReservation() throws Exception { public void testOverlimit() { try (BufferAllocator allocator = new RootAllocator(1024)) { try (BufferAllocator child1 = allocator.newChildAllocator("ChildA", 0, 1024); - BufferAllocator child2 = allocator.newChildAllocator("ChildB", 1024, 1024)) { - assertThrows(OutOfMemoryException.class, () -> { - ArrowBuf buf1 = child1.buffer(8); - buf1.close(); - }); + BufferAllocator child2 = allocator.newChildAllocator("ChildB", 1024, 1024)) { + assertThrows( + OutOfMemoryException.class, + () -> { + ArrowBuf buf1 = child1.buffer(8); + buf1.close(); + }); assertEquals(0, child1.getAllocatedMemory()); assertEquals(0, child2.getAllocatedMemory()); assertEquals(1024, allocator.getAllocatedMemory()); @@ -1114,11 +1137,14 @@ public void testOverlimitOverflow() { // Regression test for https://github.com/apache/arrow/issues/35960 try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE)) { try (BufferAllocator child1 = allocator.newChildAllocator("ChildA", 0, Long.MAX_VALUE); - BufferAllocator child2 = allocator.newChildAllocator("ChildB", Long.MAX_VALUE, Long.MAX_VALUE)) { - assertThrows(OutOfMemoryException.class, () -> { - ArrowBuf buf1 = child1.buffer(1024); - buf1.close(); - }); + BufferAllocator child2 = + allocator.newChildAllocator("ChildB", Long.MAX_VALUE, Long.MAX_VALUE)) { + assertThrows( + OutOfMemoryException.class, + () -> { + ArrowBuf buf1 = child1.buffer(1024); + buf1.close(); + }); assertEquals(0, child1.getAllocatedMemory()); assertEquals(0, child2.getAllocatedMemory()); assertEquals(Long.MAX_VALUE, allocator.getAllocatedMemory()); diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBoundaryChecking.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBoundaryChecking.java index 5b86bed406b..ecf75be1a00 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBoundaryChecking.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBoundaryChecking.java @@ -19,17 +19,15 @@ import java.lang.reflect.Field; import java.net.URLClassLoader; - import org.junit.Assert; import org.junit.Test; -/** - * Test cases for evaluating the value of {@link BoundsChecking#BOUNDS_CHECKING_ENABLED}. - */ +/** Test cases for evaluating the value of {@link BoundsChecking#BOUNDS_CHECKING_ENABLED}. */ public class TestBoundaryChecking { /** * Get a copy of the current class loader. + * * @return the newly created class loader. */ private ClassLoader copyClassLoader() { @@ -44,7 +42,8 @@ private ClassLoader copyClassLoader() { } /** - * Get the value of flag {@link BoundsChecking#BOUNDS_CHECKING_ENABLED}. + * Get the value of flag {@link BoundsChecking#BOUNDS_CHECKING_ENABLED}. + * * @param classLoader the class loader from which to get the flag value. * @return value of the flag. */ @@ -55,8 +54,8 @@ private boolean getFlagValue(ClassLoader classLoader) throws Exception { } /** - * Ensure the flag for bounds checking is enabled by default. - * This will protect users from JVM crashes. + * Ensure the flag for bounds checking is enabled by default. This will protect users from JVM + * crashes. */ @Test public void testDefaultValue() throws Exception { @@ -69,6 +68,7 @@ public void testDefaultValue() throws Exception { /** * Test setting the bounds checking flag by the old property. + * * @throws Exception if loading class {@link BoundsChecking#BOUNDS_CHECKING_ENABLED} fails. */ @Test @@ -92,6 +92,7 @@ public void testEnableOldProperty() throws Exception { /** * Test setting the bounds checking flag by the new property. + * * @throws Exception if loading class {@link BoundsChecking#BOUNDS_CHECKING_ENABLED} fails. */ @Test @@ -115,8 +116,9 @@ public void testEnableNewProperty() throws Exception { } /** - * Test setting the bounds checking flag by both old and new properties. - * In this case, the new property should take precedence. + * Test setting the bounds checking flag by both old and new properties. In this case, the new + * property should take precedence. + * * @throws Exception if loading class {@link BoundsChecking#BOUNDS_CHECKING_ENABLED} fails. */ @Test diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestForeignAllocation.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestForeignAllocation.java index 46e94fad37b..6ed2c2b0c06 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestForeignAllocation.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestForeignAllocation.java @@ -22,14 +22,6 @@ import java.util.ArrayList; import java.util.List; - -import org.apache.arrow.memory.AllocationListener; -import org.apache.arrow.memory.AllocationOutcome; -import org.apache.arrow.memory.ArrowBuf; -import org.apache.arrow.memory.BufferAllocator; -import org.apache.arrow.memory.ForeignAllocation; -import org.apache.arrow.memory.OutOfMemoryException; -import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.memory.util.MemoryUtil; import org.junit.After; import org.junit.Before; @@ -118,13 +110,14 @@ public void wrapForeignAllocationWithAllocationListenerReclaimingSpace() { final long limit = 2 * bufferSize - 1; final List buffersToBeFreed = new ArrayList<>(); - final AllocationListener listener = new AllocationListener() { - @Override - public boolean onFailedAllocation(long size, AllocationOutcome outcome) { - buffersToBeFreed.forEach(ArrowBuf::close); - return true; - } - }; + final AllocationListener listener = + new AllocationListener() { + @Override + public boolean onFailedAllocation(long size, AllocationOutcome outcome) { + buffersToBeFreed.forEach(ArrowBuf::close); + return true; + } + }; try (BufferAllocator listenedAllocator = allocator.newChildAllocator("child", listener, 0L, limit)) { @@ -133,7 +126,8 @@ public boolean onFailedAllocation(long size, AllocationOutcome outcome) { UnsafeForeignAllocation allocation = new UnsafeForeignAllocation(bufferSize); try (final ArrowBuf buffer2 = listenedAllocator.wrapForeignAllocation(allocation)) { assertEquals(bufferSize, buffer2.capacity()); - assertEquals(0, buffer1.getReferenceManager().getRefCount()); // buffer1 was closed by listener + assertEquals( + 0, buffer1.getReferenceManager().getRefCount()); // buffer1 was closed by listener } } } diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestLowCostIdentityHashMap.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestLowCostIdentityHashMap.java index 0cabc4a0571..3507d6eadda 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestLowCostIdentityHashMap.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestLowCostIdentityHashMap.java @@ -24,9 +24,7 @@ import org.junit.Test; -/** - * To test simplified implementation of IdentityHashMap. - */ +/** To test simplified implementation of IdentityHashMap. */ public class TestLowCostIdentityHashMap { @Test @@ -61,8 +59,10 @@ public void testIdentityHashMap() throws Exception { assertNotNull(nextValue); - assertTrue((hashMap.get("s1key") == nextValue || hashMap.get("s2key") == nextValue || - hashMap.get("s5key") == nextValue)); + assertTrue( + (hashMap.get("s1key") == nextValue + || hashMap.get("s2key") == nextValue + || hashMap.get("s5key") == nextValue)); assertTrue(hashMap.containsValue(obj4)); assertTrue(hashMap.containsValue(obj2)); @@ -90,7 +90,7 @@ public void testIdentityHashMap() throws Exception { public void testLargeMap() throws Exception { LowCostIdentityHashMap hashMap = new LowCostIdentityHashMap<>(); - String [] keys = new String[200]; + String[] keys = new String[200]; for (int i = 0; i < 200; i++) { keys[i] = "s" + i + "key"; } diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestOpens.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestOpens.java index 7efc11a08ac..f71a535f4ee 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestOpens.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestOpens.java @@ -29,14 +29,18 @@ public void testMemoryUtilFailsLoudly() { // This test is configured by Maven to run WITHOUT add-opens. So this should fail on JDK16+ // (where JEP396 means that add-opens is required to access JDK internals). // The test will likely fail in your IDE if it doesn't correctly pick this up. - Throwable e = assertThrows(Throwable.class, () -> { - BufferAllocator allocator = new RootAllocator(); - allocator.close(); - }); + Throwable e = + assertThrows( + Throwable.class, + () -> { + BufferAllocator allocator = new RootAllocator(); + allocator.close(); + }); boolean found = false; while (e != null) { e = e.getCause(); - if (e instanceof RuntimeException && e.getMessage().contains("Failed to initialize MemoryUtil")) { + if (e instanceof RuntimeException + && e.getMessage().contains("Failed to initialize MemoryUtil")) { found = true; break; } diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestArrowBufPointer.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestArrowBufPointer.java index a1d5624a7e8..82c55df52dc 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestArrowBufPointer.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestArrowBufPointer.java @@ -31,9 +31,7 @@ import org.junit.Before; import org.junit.Test; -/** - * Test cases for {@link ArrowBufPointer}. - */ +/** Test cases for {@link ArrowBufPointer}. */ public class TestArrowBufPointer { private final int BUFFER_LENGTH = 1024; @@ -74,7 +72,7 @@ public void testArrowBufPointersEqual() { public void testArrowBufPointersHashCode() { final int vectorLength = 100; try (ArrowBuf buf1 = allocator.buffer(vectorLength * 4); - ArrowBuf buf2 = allocator.buffer(vectorLength * 4)) { + ArrowBuf buf2 = allocator.buffer(vectorLength * 4)) { for (int i = 0; i < vectorLength; i++) { buf1.setInt(i * 4, i); buf2.setInt(i * 4, i); @@ -155,7 +153,7 @@ public void testHashersForEquality() { public void testArrowBufPointersComparison() { final int vectorLength = 100; try (ArrowBuf buf1 = allocator.buffer(vectorLength); - ArrowBuf buf2 = allocator.buffer(vectorLength)) { + ArrowBuf buf2 = allocator.buffer(vectorLength)) { for (int i = 0; i < vectorLength; i++) { buf1.setByte(i, i); buf2.setByte(i, i); @@ -185,8 +183,8 @@ public void testArrowBufPointersComparison() { } /** - * Hasher with a counter that increments each time a hash code is calculated. - * This is to validate that the hash code in {@link ArrowBufPointer} is reused. + * Hasher with a counter that increments each time a hash code is calculated. This is to validate + * that the hash code in {@link ArrowBufPointer} is reused. */ class CounterHasher implements ArrowBufHasher { diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestByteFunctionHelpers.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestByteFunctionHelpers.java index 04a715962df..fbcc00a682a 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestByteFunctionHelpers.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestByteFunctionHelpers.java @@ -35,7 +35,6 @@ public class TestByteFunctionHelpers { @Before public void init() { allocator = new RootAllocator(Long.MAX_VALUE); - } @After @@ -53,28 +52,21 @@ public void testEquals() { buffer2.setByte(i, i); } - //test three cases, length>8, length>3, length<3 + // test three cases, length>8, length>3, length<3 - assertEquals(1, ByteFunctionHelpers.equal(buffer1, 0, SIZE - 1, - buffer2, 0, SIZE - 1)); - assertEquals(1, ByteFunctionHelpers.equal(buffer1, 0, 6, - buffer2, 0, 6)); - assertEquals(1, ByteFunctionHelpers.equal(buffer1, 0, 2, - buffer2, 0, 2)); + assertEquals(1, ByteFunctionHelpers.equal(buffer1, 0, SIZE - 1, buffer2, 0, SIZE - 1)); + assertEquals(1, ByteFunctionHelpers.equal(buffer1, 0, 6, buffer2, 0, 6)); + assertEquals(1, ByteFunctionHelpers.equal(buffer1, 0, 2, buffer2, 0, 2)); - //change value at index1 + // change value at index1 buffer1.setByte(1, 10); - assertEquals(0, ByteFunctionHelpers.equal(buffer1, 0, SIZE - 1, - buffer2, 0, SIZE - 1)); - assertEquals(0, ByteFunctionHelpers.equal(buffer1, 0, 6, - buffer2, 0, 6)); - assertEquals(0, ByteFunctionHelpers.equal(buffer1, 0, 2, - buffer2, 0, 2)); + assertEquals(0, ByteFunctionHelpers.equal(buffer1, 0, SIZE - 1, buffer2, 0, SIZE - 1)); + assertEquals(0, ByteFunctionHelpers.equal(buffer1, 0, 6, buffer2, 0, 6)); + assertEquals(0, ByteFunctionHelpers.equal(buffer1, 0, 2, buffer2, 0, 2)); buffer1.close(); buffer2.close(); - } @Test @@ -87,28 +79,21 @@ public void testCompare() { buffer2.setByte(i, i); } - //test three cases, length>8, length>3, length<3 + // test three cases, length>8, length>3, length<3 - assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, SIZE - 1, - buffer2, 0, SIZE - 1)); - assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, 6, - buffer2, 0, 6)); - assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, 2, - buffer2, 0, 2)); + assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, SIZE - 1, buffer2, 0, SIZE - 1)); + assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, 6, buffer2, 0, 6)); + assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, 2, buffer2, 0, 2)); - //change value at index 1 + // change value at index 1 buffer1.setByte(1, 0); - assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, SIZE - 1, - buffer2, 0, SIZE - 1)); - assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, 6, - buffer2, 0, 6)); - assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, 2, - buffer2, 0, 2)); + assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, SIZE - 1, buffer2, 0, SIZE - 1)); + assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, 6, buffer2, 0, 6)); + assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, 2, buffer2, 0, 2)); buffer1.close(); buffer2.close(); - } @Test @@ -125,7 +110,8 @@ public void testStringCompare() { ArrowBuf right = allocator.buffer(SIZE); right.setBytes(0, rightStr.getBytes()); - assertEquals(leftStr.compareTo(rightStr) < 0 ? -1 : 1, + assertEquals( + leftStr.compareTo(rightStr) < 0 ? -1 : 1, ByteFunctionHelpers.compare(left, 0, leftStr.length(), right, 0, rightStr.length())); left.close(); @@ -143,24 +129,18 @@ public void testCompareWithByteArray() { buffer2[i] = (byte) i; } - //test three cases, length>8, length>3, length<3 + // test three cases, length>8, length>3, length<3 - assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, SIZE - 1, - buffer2, 0, SIZE - 1)); - assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, 6, - buffer2, 0, 6)); - assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, 2, - buffer2, 0, 2)); + assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, SIZE - 1, buffer2, 0, SIZE - 1)); + assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, 6, buffer2, 0, 6)); + assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, 2, buffer2, 0, 2)); - //change value at index 1 + // change value at index 1 buffer1.setByte(1, 0); - assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, SIZE - 1, - buffer2, 0, SIZE - 1)); - assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, 6, - buffer2, 0, 6)); - assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, 2, - buffer2, 0, 2)); + assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, SIZE - 1, buffer2, 0, SIZE - 1)); + assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, 6, buffer2, 0, 6)); + assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, 2, buffer2, 0, 2)); buffer1.close(); } diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestLargeMemoryUtil.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestLargeMemoryUtil.java index 952fcb5f051..655e43179ff 100755 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestLargeMemoryUtil.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestLargeMemoryUtil.java @@ -1,105 +1,111 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.arrow.memory.util; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.net.URLClassLoader; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.jupiter.api.Assertions; - -public class TestLargeMemoryUtil { - - /** - * Get a copy of the current class loader. - * @return the newly created class loader. - */ - private ClassLoader copyClassLoader() { - ClassLoader curClassLoader = this.getClass().getClassLoader(); - if (curClassLoader instanceof URLClassLoader) { - // for Java 1.8 - return new URLClassLoader(((URLClassLoader) curClassLoader).getURLs(), null); - } - - // for Java 1.9 and Java 11. - return null; - } - - /** - * Use the checkedCastToInt method from the current classloader. - * @param classLoader the class loader from which to call the method. - * @return the return value of the method. - */ - private int checkedCastToInt(ClassLoader classLoader, long value) throws Exception { - Class clazz = classLoader.loadClass("org.apache.arrow.memory.util.LargeMemoryUtil"); - Method method = clazz.getMethod("checkedCastToInt", long.class); - return (int) method.invoke(null, value); - } - - private void checkExpectedOverflow(ClassLoader classLoader, long value) { - InvocationTargetException ex = Assertions.assertThrows(InvocationTargetException.class, () -> { - checkedCastToInt(classLoader, value); - }); - Assert.assertTrue(ex.getCause() instanceof ArithmeticException); - Assert.assertEquals("integer overflow", ex.getCause().getMessage()); - } - - @Test - public void testEnableLargeMemoryUtilCheck() throws Exception { - String savedNewProperty = System.getProperty("arrow.enable_unsafe_memory_access"); - System.setProperty("arrow.enable_unsafe_memory_access", "false"); - try { - ClassLoader classLoader = copyClassLoader(); - if (classLoader != null) { - Assert.assertEquals(Integer.MAX_VALUE, checkedCastToInt(classLoader, Integer.MAX_VALUE)); - checkExpectedOverflow(classLoader, Integer.MAX_VALUE + 1L); - checkExpectedOverflow(classLoader, Integer.MIN_VALUE - 1L); - } - } finally { - // restore system property - if (savedNewProperty != null) { - System.setProperty("arrow.enable_unsafe_memory_access", savedNewProperty); - } else { - System.clearProperty("arrow.enable_unsafe_memory_access"); - } - } - } - - @Test - public void testDisabledLargeMemoryUtilCheck() throws Exception { - String savedNewProperty = System.getProperty("arrow.enable_unsafe_memory_access"); - System.setProperty("arrow.enable_unsafe_memory_access", "true"); - try { - ClassLoader classLoader = copyClassLoader(); - if (classLoader != null) { - Assert.assertEquals(Integer.MAX_VALUE, checkedCastToInt(classLoader, Integer.MAX_VALUE)); - Assert.assertEquals(Integer.MIN_VALUE, checkedCastToInt(classLoader, Integer.MAX_VALUE + 1L)); - Assert.assertEquals(Integer.MAX_VALUE, checkedCastToInt(classLoader, Integer.MIN_VALUE - 1L)); - } - } finally { - // restore system property - if (savedNewProperty != null) { - System.setProperty("arrow.enable_unsafe_memory_access", savedNewProperty); - } else { - System.clearProperty("arrow.enable_unsafe_memory_access"); - } - } - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.arrow.memory.util; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URLClassLoader; +import org.junit.Assert; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +public class TestLargeMemoryUtil { + + /** + * Get a copy of the current class loader. + * + * @return the newly created class loader. + */ + private ClassLoader copyClassLoader() { + ClassLoader curClassLoader = this.getClass().getClassLoader(); + if (curClassLoader instanceof URLClassLoader) { + // for Java 1.8 + return new URLClassLoader(((URLClassLoader) curClassLoader).getURLs(), null); + } + + // for Java 1.9 and Java 11. + return null; + } + + /** + * Use the checkedCastToInt method from the current classloader. + * + * @param classLoader the class loader from which to call the method. + * @return the return value of the method. + */ + private int checkedCastToInt(ClassLoader classLoader, long value) throws Exception { + Class clazz = classLoader.loadClass("org.apache.arrow.memory.util.LargeMemoryUtil"); + Method method = clazz.getMethod("checkedCastToInt", long.class); + return (int) method.invoke(null, value); + } + + private void checkExpectedOverflow(ClassLoader classLoader, long value) { + InvocationTargetException ex = + Assertions.assertThrows( + InvocationTargetException.class, + () -> { + checkedCastToInt(classLoader, value); + }); + Assert.assertTrue(ex.getCause() instanceof ArithmeticException); + Assert.assertEquals("integer overflow", ex.getCause().getMessage()); + } + + @Test + public void testEnableLargeMemoryUtilCheck() throws Exception { + String savedNewProperty = System.getProperty("arrow.enable_unsafe_memory_access"); + System.setProperty("arrow.enable_unsafe_memory_access", "false"); + try { + ClassLoader classLoader = copyClassLoader(); + if (classLoader != null) { + Assert.assertEquals(Integer.MAX_VALUE, checkedCastToInt(classLoader, Integer.MAX_VALUE)); + checkExpectedOverflow(classLoader, Integer.MAX_VALUE + 1L); + checkExpectedOverflow(classLoader, Integer.MIN_VALUE - 1L); + } + } finally { + // restore system property + if (savedNewProperty != null) { + System.setProperty("arrow.enable_unsafe_memory_access", savedNewProperty); + } else { + System.clearProperty("arrow.enable_unsafe_memory_access"); + } + } + } + + @Test + public void testDisabledLargeMemoryUtilCheck() throws Exception { + String savedNewProperty = System.getProperty("arrow.enable_unsafe_memory_access"); + System.setProperty("arrow.enable_unsafe_memory_access", "true"); + try { + ClassLoader classLoader = copyClassLoader(); + if (classLoader != null) { + Assert.assertEquals(Integer.MAX_VALUE, checkedCastToInt(classLoader, Integer.MAX_VALUE)); + Assert.assertEquals( + Integer.MIN_VALUE, checkedCastToInt(classLoader, Integer.MAX_VALUE + 1L)); + Assert.assertEquals( + Integer.MAX_VALUE, checkedCastToInt(classLoader, Integer.MIN_VALUE - 1L)); + } + } finally { + // restore system property + if (savedNewProperty != null) { + System.setProperty("arrow.enable_unsafe_memory_access", savedNewProperty); + } else { + System.clearProperty("arrow.enable_unsafe_memory_access"); + } + } + } +} diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/hash/TestArrowBufHasher.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/hash/TestArrowBufHasher.java index 3da0602bdfd..bbc059b8927 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/hash/TestArrowBufHasher.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/hash/TestArrowBufHasher.java @@ -24,7 +24,6 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collection; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -34,9 +33,7 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -/** - * Test cases for {@link ArrowBufHasher} and its subclasses. - */ +/** Test cases for {@link ArrowBufHasher} and its subclasses. */ @RunWith(Parameterized.class) public class TestArrowBufHasher { @@ -63,7 +60,7 @@ public void shutdown() { @Test public void testHasher() { try (ArrowBuf buf1 = allocator.buffer(BUFFER_LENGTH); - ArrowBuf buf2 = allocator.buffer(BUFFER_LENGTH)) { + ArrowBuf buf2 = allocator.buffer(BUFFER_LENGTH)) { // prepare data for (int i = 0; i < BUFFER_LENGTH / 4; i++) { buf1.setFloat(i * 4, i / 10.0f); @@ -83,8 +80,8 @@ public void testHasher() { } } - private void verifyHashCodesEqual(ArrowBuf buf1, int offset1, int length1, - ArrowBuf buf2, int offset2, int length2) { + private void verifyHashCodesEqual( + ArrowBuf buf1, int offset1, int length1, ArrowBuf buf2, int offset2, int length2) { int hashCode1 = hasher.hashCode(buf1, offset1, length1); int hashCode2 = hasher.hashCode(buf2, offset2, length2); assertEquals(hashCode1, hashCode2); @@ -98,35 +95,41 @@ public void testHasherNegative() { buf.setFloat(i * 4, i / 10.0f); } - assertThrows(IllegalArgumentException.class, () -> { - hasher.hashCode(buf, 0, -1); - }); - - assertThrows(IndexOutOfBoundsException.class, () -> { - hasher.hashCode(buf, 0, 1028); - }); - - assertThrows(IndexOutOfBoundsException.class, () -> { - hasher.hashCode(buf, 500, 1000); - }); + assertThrows( + IllegalArgumentException.class, + () -> { + hasher.hashCode(buf, 0, -1); + }); + + assertThrows( + IndexOutOfBoundsException.class, + () -> { + hasher.hashCode(buf, 0, 1028); + }); + + assertThrows( + IndexOutOfBoundsException.class, + () -> { + hasher.hashCode(buf, 500, 1000); + }); } } @Test public void testHasherLessThanInt() { try (ArrowBuf buf1 = allocator.buffer(4); - ArrowBuf buf2 = allocator.buffer(4)) { + ArrowBuf buf2 = allocator.buffer(4)) { buf1.writeBytes("foo1".getBytes(StandardCharsets.UTF_8)); buf2.writeBytes("bar2".getBytes(StandardCharsets.UTF_8)); - for (int i = 1; i <= 4; i ++) { + for (int i = 1; i <= 4; i++) { verifyHashCodeNotEqual(buf1, 0, i, buf2, 0, i); } } } - private void verifyHashCodeNotEqual(ArrowBuf buf1, int offset1, int length1, - ArrowBuf buf2, int offset2, int length2) { + private void verifyHashCodeNotEqual( + ArrowBuf buf1, int offset1, int length1, ArrowBuf buf2, int offset2, int length2) { int hashCode1 = hasher.hashCode(buf1, 0, length1); int hashCode2 = hasher.hashCode(buf2, 0, length2); assertNotEquals(hashCode1, hashCode2); @@ -135,11 +138,7 @@ private void verifyHashCodeNotEqual(ArrowBuf buf1, int offset1, int length1, @Parameterized.Parameters(name = "hasher = {0}") public static Collection getHasher() { return Arrays.asList( - new Object[] {SimpleHasher.class.getSimpleName(), - SimpleHasher.INSTANCE}, - new Object[] {MurmurHasher.class.getSimpleName(), - new MurmurHasher() - } - ); + new Object[] {SimpleHasher.class.getSimpleName(), SimpleHasher.INSTANCE}, + new Object[] {MurmurHasher.class.getSimpleName(), new MurmurHasher()}); } } diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/util/TestCollections2.java b/java/memory/memory-core/src/test/java/org/apache/arrow/util/TestCollections2.java index c858ebe62a9..e5aa00acdb2 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/util/TestCollections2.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/util/TestCollections2.java @@ -25,15 +25,11 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; - import org.junit.Test; -/** - * Tests for {@code Collections2} class. - */ +/** Tests for {@code Collections2} class. */ public class TestCollections2 { - @Test public void testToImmutableListFromIterable() { final List source = new ArrayList<>(Arrays.asList("foo", "bar", "baz")); @@ -66,7 +62,6 @@ public void testToImmutableListFromIterable() { assertEquals(3, copy.size()); } - @Test public void testStringFromEmptyIterator() { assertEquals("[]", Collections2.toString(Collections.emptyIterator())); diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/util/TestStackTrace.java b/java/memory/memory-core/src/test/java/org/apache/arrow/util/TestStackTrace.java index 4418bec1872..630662d7625 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/util/TestStackTrace.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/util/TestStackTrace.java @@ -22,22 +22,19 @@ import org.junit.jupiter.api.Test; public class TestStackTrace { - /** - * Check that the stack trace includes the origin line. - */ + /** Check that the stack trace includes the origin line. */ @Test public void testStackTraceComplete() { final String stackTrace = new StackTrace().toString(); Assertions.assertTrue(stackTrace.contains("TestStackTrace.testStackTraceComplete"), stackTrace); } - /** - * Check that the stack trace doesn't include getStackTrace or StackTrace. - */ + /** Check that the stack trace doesn't include getStackTrace or StackTrace. */ @Test public void testStackTraceOmit() { final String stackTrace = new StackTrace().toString(); Assertions.assertFalse(stackTrace.contains("Thread.getStackTrace"), stackTrace); - Assertions.assertFalse(stackTrace.contains("org.apache.arrow.memory.util.StackTrace"), stackTrace); + Assertions.assertFalse( + stackTrace.contains("org.apache.arrow.memory.util.StackTrace"), stackTrace); } } diff --git a/java/memory/memory-core/src/test/resources/logback.xml b/java/memory/memory-core/src/test/resources/logback.xml index 4c54d18a210..9fa3f1e3b43 100644 --- a/java/memory/memory-core/src/test/resources/logback.xml +++ b/java/memory/memory-core/src/test/resources/logback.xml @@ -1,4 +1,4 @@ - + - diff --git a/java/memory/memory-netty-buffer-patch/pom.xml b/java/memory/memory-netty-buffer-patch/pom.xml index 97b224e9ccc..53e6ba6a9d2 100644 --- a/java/memory/memory-netty-buffer-patch/pom.xml +++ b/java/memory/memory-netty-buffer-patch/pom.xml @@ -9,15 +9,13 @@ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> - + + 4.0.0 - arrow-memory org.apache.arrow + arrow-memory 16.0.0-SNAPSHOT - 4.0.0 arrow-memory-netty-buffer-patch Arrow Memory - Netty Buffer diff --git a/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/ExpandableByteBuf.java b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/ExpandableByteBuf.java index 09b7300444c..0e203299dfb 100644 --- a/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/ExpandableByteBuf.java +++ b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/ExpandableByteBuf.java @@ -21,8 +21,7 @@ /** * Allows us to decorate ArrowBuf to make it expandable so that we can use them in the context of - * the Netty framework - * (thus supporting RPC level memory accounting). + * the Netty framework (thus supporting RPC level memory accounting). */ public class ExpandableByteBuf extends MutableWrappedByteBuf { @@ -52,5 +51,4 @@ public ByteBuf capacity(int newCapacity) { return super.capacity(newCapacity); } } - } diff --git a/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/MutableWrappedByteBuf.java b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/MutableWrappedByteBuf.java index 5221dd3c19d..ba40e49ac21 100644 --- a/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/MutableWrappedByteBuf.java +++ b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/MutableWrappedByteBuf.java @@ -17,6 +17,7 @@ package io.netty.buffer; +import io.netty.util.ByteProcessor; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -26,11 +27,9 @@ import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; -import io.netty.util.ByteProcessor; - /** - * This is basically a complete copy of netty's DuplicatedByteBuf. We copy because we want to override - * some behaviors and make buffer mutable. + * This is basically a complete copy of netty's DuplicatedByteBuf. We copy because we want to + * override some behaviors and make buffer mutable. */ abstract class MutableWrappedByteBuf extends AbstractByteBuf { @@ -343,40 +342,33 @@ public ByteBuf setBytes(int index, ByteBuffer src) { } @Override - public int setBytes(int index, FileChannel in, long position, int length) - throws IOException { + public int setBytes(int index, FileChannel in, long position, int length) throws IOException { return buffer.setBytes(index, in, position, length); } @Override - public ByteBuf getBytes(int index, OutputStream out, int length) - throws IOException { + public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { buffer.getBytes(index, out, length); return this; } @Override - public int getBytes(int index, GatheringByteChannel out, int length) - throws IOException { + public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { return buffer.getBytes(index, out, length); } @Override - public int setBytes(int index, InputStream in, int length) - throws IOException { + public int setBytes(int index, InputStream in, int length) throws IOException { return buffer.setBytes(index, in, length); } @Override - public int setBytes(int index, ScatteringByteChannel in, int length) - throws IOException { + public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { return buffer.setBytes(index, in, length); } - @Override - public int getBytes(int index, FileChannel out, long position, int length) - throws IOException { + public int getBytes(int index, FileChannel out, long position, int length) throws IOException { return buffer.getBytes(index, out, position, length); } @@ -444,5 +436,4 @@ public boolean release(int decrement) { boolean released = unwrap().release(decrement); return released; } - } diff --git a/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/NettyArrowBuf.java b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/NettyArrowBuf.java index 466444c7d53..5660c9e340c 100644 --- a/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/NettyArrowBuf.java +++ b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/NettyArrowBuf.java @@ -17,6 +17,7 @@ package io.netty.buffer; +import io.netty.util.internal.PlatformDependent; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -25,7 +26,6 @@ import java.nio.channels.FileChannel; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BoundsChecking; import org.apache.arrow.memory.BufferAllocator; @@ -34,11 +34,7 @@ import org.apache.arrow.util.Preconditions; import org.apache.arrow.util.VisibleForTesting; -import io.netty.util.internal.PlatformDependent; - -/** - * Netty specific wrapper over ArrowBuf for use in Netty framework. - */ +/** Netty specific wrapper over ArrowBuf for use in Netty framework. */ public class NettyArrowBuf extends AbstractByteBuf implements AutoCloseable { private final ArrowBuf arrowBuf; @@ -49,14 +45,12 @@ public class NettyArrowBuf extends AbstractByteBuf implements AutoCloseable { /** * Constructs a new instance. * - * @param arrowBuf The buffer to wrap. + * @param arrowBuf The buffer to wrap. * @param bufferAllocator The allocator for the buffer. - * @param length The length of this buffer. + * @param length The length of this buffer. */ public NettyArrowBuf( - final ArrowBuf arrowBuf, - final BufferAllocator bufferAllocator, - final int length) { + final ArrowBuf arrowBuf, final BufferAllocator bufferAllocator, final int length) { super(length); this.arrowBuf = arrowBuf; this.arrowByteBufAllocator = new ArrowByteBufAllocator(bufferAllocator); @@ -105,13 +99,15 @@ public synchronized ByteBuf capacity(int newCapacity) { length = newCapacity; return this; } - throw new UnsupportedOperationException("Buffers don't support resizing that increases the size."); + throw new UnsupportedOperationException( + "Buffers don't support resizing that increases the size."); } @Override public ByteBuf unwrap() { - // According to Netty's ByteBuf interface, unwrap() should return null if the buffer cannot be unwrapped + // According to Netty's ByteBuf interface, unwrap() should return null if the buffer cannot be + // unwrapped // https://github.com/netty/netty/blob/9fe796e10a433b6cd20ad78b2c39cd56b86ccd2e/buffer/src/main/java/io/netty/buffer/ByteBuf.java#L305 // Throwing here breaks toString() in AbstractByteBuf @@ -234,10 +230,9 @@ public ByteBuffer nioBuffer() { return nioBuffer(readerIndex(), readableBytes()); } - /** - * Returns a buffer that is zero positioned but points - * to a slice of the original buffer starting at given index. + * Returns a buffer that is zero positioned but points to a slice of the original buffer starting + * at given index. */ @Override public ByteBuffer nioBuffer(int index, int length) { @@ -248,8 +243,8 @@ public ByteBuffer nioBuffer(int index, int length) { } /** - * Returns a buffer that is zero positioned but points - * to a slice of the original buffer starting at given index. + * Returns a buffer that is zero positioned but points to a slice of the original buffer starting + * at given index. */ public ByteBuffer nioBuffer(long index, int length) { chk(index, length); @@ -264,7 +259,8 @@ public ByteBuffer nioBuffer(long index, int length) { * @return ByteBuffer */ private ByteBuffer getDirectBuffer(long index) { - return PlatformDependent.directBuffer(addr(index), LargeMemoryUtil.checkedCastToInt(length - index)); + return PlatformDependent.directBuffer( + addr(index), LargeMemoryUtil.checkedCastToInt(length - index)); } @Override @@ -294,11 +290,11 @@ public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { /** * Determine if the requested {@code index} and {@code length} will fit within {@code capacity}. * - * @param index The starting index. - * @param length The length which will be utilized (starting from {@code index}). + * @param index The starting index. + * @param length The length which will be utilized (starting from {@code index}). * @param capacity The capacity that {@code index + length} is allowed to be within. - * @return {@code true} if the requested {@code index} and {@code length} will fit within {@code capacity}. - * {@code false} if this would result in an index out of bounds exception. + * @return {@code true} if the requested {@code index} and {@code length} will fit within {@code + * capacity}. {@code false} if this would result in an index out of bounds exception. */ private static boolean isOutOfBounds(int index, int length, int capacity) { return (index | length | (index + length) | (capacity - (index + length))) < 0; @@ -413,18 +409,16 @@ protected int _getUnsignedMedium(int index) { protected int _getUnsignedMediumLE(int index) { this.chk(index, 3); long addr = this.addr(index); - return PlatformDependent.getByte(addr) & 255 | - (Short.reverseBytes(PlatformDependent.getShort(addr + 1L)) & '\uffff') << 8; + return PlatformDependent.getByte(addr) & 255 + | (Short.reverseBytes(PlatformDependent.getShort(addr + 1L)) & '\uffff') << 8; } - /*-------------------------------------------------* - | | - | get() APIs | - | | + | | + | get() APIs | + | | *-------------------------------------------------*/ - @Override protected byte _getByte(int index) { return getByte(index); @@ -483,14 +477,12 @@ public long getLong(int index) { return arrowBuf.getLong(index); } - /*-------------------------------------------------* - | | - | set() APIs | - | | + | | + | set() APIs | + | | *-------------------------------------------------*/ - @Override protected void _setByte(int index, int value) { setByte(index, value); @@ -524,10 +516,9 @@ private long addr(long index) { } /** - * Helper function to do bounds checking at a particular - * index for particular length of data. + * Helper function to do bounds checking at a particular index for particular length of data. * - * @param index index (0 based relative to this ArrowBuf) + * @param index index (0 based relative to this ArrowBuf) * @param fieldLength provided length of data for get/set */ private void chk(long index, long fieldLength) { @@ -539,8 +530,9 @@ private void chk(long index, long fieldLength) { throw new IllegalArgumentException("length: " + fieldLength + " (expected: >= 0)"); } if (index < 0 || index > capacity() - fieldLength) { - throw new IndexOutOfBoundsException(String.format( - "index: %d, length: %d (expected: range(0, %d))", index, fieldLength, capacity())); + throw new IndexOutOfBoundsException( + String.format( + "index: %d, length: %d (expected: range(0, %d))", index, fieldLength, capacity())); } } } @@ -615,17 +607,15 @@ public NettyArrowBuf setLong(int index, long value) { return this; } - /** - * unwrap arrow buffer into a netty buffer. - */ + /** unwrap arrow buffer into a netty buffer. */ public static NettyArrowBuf unwrapBuffer(ArrowBuf buf) { - final NettyArrowBuf nettyArrowBuf = new NettyArrowBuf( - buf, - buf.getReferenceManager().getAllocator(), - LargeMemoryUtil.checkedCastToInt(buf.capacity())); + final NettyArrowBuf nettyArrowBuf = + new NettyArrowBuf( + buf, + buf.getReferenceManager().getAllocator(), + LargeMemoryUtil.checkedCastToInt(buf.capacity())); nettyArrowBuf.readerIndex(LargeMemoryUtil.checkedCastToInt(buf.readerIndex())); nettyArrowBuf.writerIndex(LargeMemoryUtil.checkedCastToInt(buf.writerIndex())); return nettyArrowBuf; } - } diff --git a/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/PooledByteBufAllocatorL.java b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/PooledByteBufAllocatorL.java index ea84a3258e8..28393aaaef7 100644 --- a/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/PooledByteBufAllocatorL.java +++ b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/PooledByteBufAllocatorL.java @@ -17,24 +17,23 @@ package io.netty.buffer; +import io.netty.util.internal.OutOfDirectMemoryError; +import io.netty.util.internal.StringUtil; import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.util.concurrent.atomic.AtomicLong; - import org.apache.arrow.memory.OutOfMemoryException; import org.apache.arrow.memory.util.AssertionUtil; import org.apache.arrow.memory.util.LargeMemoryUtil; -import io.netty.util.internal.OutOfDirectMemoryError; -import io.netty.util.internal.StringUtil; - /** * The base allocator that we use for all of Arrow's memory management. Returns * UnsafeDirectLittleEndian buffers. */ public class PooledByteBufAllocatorL { - private static final org.slf4j.Logger memoryLogger = org.slf4j.LoggerFactory.getLogger("arrow.allocator"); + private static final org.slf4j.Logger memoryLogger = + org.slf4j.LoggerFactory.getLogger("arrow.allocator"); private static final int MEMORY_LOGGER_FREQUENCY_SECONDS = 60; public final UnsafeDirectLittleEndian empty; @@ -49,9 +48,7 @@ public PooledByteBufAllocatorL() { empty = new UnsafeDirectLittleEndian(new DuplicatedByteBuf(Unpooled.EMPTY_BUFFER)); } - /** - * Returns a {@linkplain UnsafeDirectLittleEndian} of the given size. - */ + /** Returns a {@linkplain UnsafeDirectLittleEndian} of the given size. */ public UnsafeDirectLittleEndian allocate(long size) { try { return allocator.directBuffer(LargeMemoryUtil.checkedCastToInt(size), Integer.MAX_VALUE); @@ -102,8 +99,8 @@ private AccountedUnsafeDirectLittleEndian(LargeBuffer buf, AtomicLong count, Ato this.size = size; } - private AccountedUnsafeDirectLittleEndian(PooledUnsafeDirectByteBuf buf, AtomicLong count, - AtomicLong size) { + private AccountedUnsafeDirectLittleEndian( + PooledUnsafeDirectByteBuf buf, AtomicLong count, AtomicLong size) { super(buf); this.initialCapacity = buf.capacity(); this.count = count; @@ -129,7 +126,6 @@ public boolean release(int decrement) { } return released; } - } private class InnerAllocator extends PooledByteBufAllocator { @@ -145,7 +141,8 @@ public InnerAllocator() { f.setAccessible(true); this.directArenas = (PoolArena[]) f.get(this); } catch (Exception e) { - throw new RuntimeException("Failure while initializing allocator. Unable to retrieve direct arenas field.", e); + throw new RuntimeException( + "Failure while initializing allocator. Unable to retrieve direct arenas field.", e); } if (memoryLogger.isTraceEnabled()) { @@ -170,8 +167,8 @@ private UnsafeDirectLittleEndian newDirectBufferL(int initialCapacity, int maxCa hugeBufferCount.incrementAndGet(); // logger.debug("Allocating huge buffer of size {}", initialCapacity, new Exception()); - return new AccountedUnsafeDirectLittleEndian(new LargeBuffer(buf), hugeBufferCount, - hugeBufferSize); + return new AccountedUnsafeDirectLittleEndian( + new LargeBuffer(buf), hugeBufferCount, hugeBufferSize); } else { // within chunk, use arena. ByteBuf buf = directArena.allocate(cache, initialCapacity, maxCapacity); @@ -186,8 +183,8 @@ private UnsafeDirectLittleEndian newDirectBufferL(int initialCapacity, int maxCa normalBufferSize.addAndGet(buf.capacity()); normalBufferCount.incrementAndGet(); - return new AccountedUnsafeDirectLittleEndian((PooledUnsafeDirectByteBuf) buf, - normalBufferCount, normalBufferSize); + return new AccountedUnsafeDirectLittleEndian( + (PooledUnsafeDirectByteBuf) buf, normalBufferCount, normalBufferSize); } } else { @@ -197,8 +194,8 @@ private UnsafeDirectLittleEndian newDirectBufferL(int initialCapacity, int maxCa private UnsupportedOperationException fail() { return new UnsupportedOperationException( - "Arrow requires that the JVM used supports access sun.misc.Unsafe. This platform " + - "didn't provide that functionality."); + "Arrow requires that the JVM used supports access sun.misc.Unsafe. This platform " + + "didn't provide that functionality."); } @Override @@ -215,15 +212,16 @@ public ByteBuf heapBuffer(int initialCapacity, int maxCapacity) { throw new UnsupportedOperationException("Arrow doesn't support using heap buffers."); } - private void validate(int initialCapacity, int maxCapacity) { if (initialCapacity < 0) { - throw new IllegalArgumentException("initialCapacity: " + initialCapacity + " (expected: 0+)"); + throw new IllegalArgumentException( + "initialCapacity: " + initialCapacity + " (expected: 0+)"); } if (initialCapacity > maxCapacity) { - throw new IllegalArgumentException(String.format( - "initialCapacity: %d (expected: not greater than maxCapacity(%d)", - initialCapacity, maxCapacity)); + throw new IllegalArgumentException( + String.format( + "initialCapacity: %d (expected: not greater than maxCapacity(%d)", + initialCapacity, maxCapacity)); } } @@ -272,7 +270,5 @@ public void run() { } } } - - } } diff --git a/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/UnsafeDirectLittleEndian.java b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/UnsafeDirectLittleEndian.java index e51c6c3d488..9405221f530 100644 --- a/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/UnsafeDirectLittleEndian.java +++ b/java/memory/memory-netty-buffer-patch/src/main/java/io/netty/buffer/UnsafeDirectLittleEndian.java @@ -17,18 +17,16 @@ package io.netty.buffer; +import io.netty.util.internal.PlatformDependent; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteOrder; import java.util.concurrent.atomic.AtomicLong; -import io.netty.util.internal.PlatformDependent; - /** - * The underlying class we use for little-endian access to memory. Is used underneath ArrowBufs - * to abstract away the - * Netty classes and underlying Netty memory management. + * The underlying class we use for little-endian access to memory. Is used underneath ArrowBufs to + * abstract away the Netty classes and underlying Netty memory management. */ public class UnsafeDirectLittleEndian extends WrappedByteBuf { private static final AtomicLong ID_GENERATOR = new AtomicLong(0); diff --git a/java/memory/memory-netty-buffer-patch/src/main/java/org/apache/arrow/memory/patch/ArrowByteBufAllocator.java b/java/memory/memory-netty-buffer-patch/src/main/java/org/apache/arrow/memory/patch/ArrowByteBufAllocator.java index 6ce08b5a590..26b3d41adfb 100644 --- a/java/memory/memory-netty-buffer-patch/src/main/java/org/apache/arrow/memory/patch/ArrowByteBufAllocator.java +++ b/java/memory/memory-netty-buffer-patch/src/main/java/org/apache/arrow/memory/patch/ArrowByteBufAllocator.java @@ -17,22 +17,18 @@ package org.apache.arrow.memory.patch; -import org.apache.arrow.memory.BufferAllocator; - import io.netty.buffer.AbstractByteBufAllocator; import io.netty.buffer.ByteBuf; import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.ExpandableByteBuf; import io.netty.buffer.NettyArrowBuf; +import org.apache.arrow.memory.BufferAllocator; /** * An implementation of ByteBufAllocator that wraps a Arrow BufferAllocator. This allows the RPC - * layer to be accounted - * and managed using Arrow's BufferAllocator infrastructure. The only thin different from a - * typical BufferAllocator is - * the signature and the fact that this Allocator returns ExpandableByteBufs which enable - * otherwise non-expandable - * ArrowBufs to be expandable. + * layer to be accounted and managed using Arrow's BufferAllocator infrastructure. The only thin + * different from a typical BufferAllocator is the signature and the fact that this Allocator + * returns ExpandableByteBufs which enable otherwise non-expandable ArrowBufs to be expandable. * * @deprecated This class may be removed in a future release. */ @@ -59,7 +55,8 @@ public ByteBuf buffer() { @Override public ByteBuf buffer(int initialCapacity) { - return new ExpandableByteBuf(NettyArrowBuf.unwrapBuffer(allocator.buffer(initialCapacity)), allocator); + return new ExpandableByteBuf( + NettyArrowBuf.unwrapBuffer(allocator.buffer(initialCapacity)), allocator); } @Override diff --git a/java/memory/memory-netty-buffer-patch/src/test/java/io/netty/buffer/TestUnsafeDirectLittleEndian.java b/java/memory/memory-netty-buffer-patch/src/test/java/io/netty/buffer/TestUnsafeDirectLittleEndian.java index 043c2c1605a..c9df9d6059a 100644 --- a/java/memory/memory-netty-buffer-patch/src/test/java/io/netty/buffer/TestUnsafeDirectLittleEndian.java +++ b/java/memory/memory-netty-buffer-patch/src/test/java/io/netty/buffer/TestUnsafeDirectLittleEndian.java @@ -17,7 +17,6 @@ package io.netty.buffer; - import static org.junit.Assert.assertEquals; import java.io.ByteArrayInputStream; @@ -25,14 +24,8 @@ import java.io.IOException; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; - import org.junit.Test; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.LargeBuffer; -import io.netty.buffer.Unpooled; -import io.netty.buffer.UnsafeDirectLittleEndian; - public class TestUnsafeDirectLittleEndian { private static final boolean LITTLE_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN; @@ -71,7 +64,7 @@ public void testPrimitiveGetSet() { byte[] inBytes = "1234567".getBytes(StandardCharsets.UTF_8); try (ByteArrayInputStream bais = new ByteArrayInputStream(inBytes); - ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + ByteArrayOutputStream baos = new ByteArrayOutputStream()) { assertEquals(5, unsafeDirect.setBytes(56, bais, 5)); unsafeDirect.getBytes(56, baos, 5); assertEquals("12345", new String(baos.toByteArray(), StandardCharsets.UTF_8)); diff --git a/java/memory/memory-netty/pom.xml b/java/memory/memory-netty/pom.xml index 9b20e1bde2a..1466adae41d 100644 --- a/java/memory/memory-netty/pom.xml +++ b/java/memory/memory-netty/pom.xml @@ -10,12 +10,12 @@ OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> + 4.0.0 - arrow-memory org.apache.arrow + arrow-memory 16.0.0-SNAPSHOT - 4.0.0 arrow-memory-netty Arrow Memory - Netty diff --git a/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/netty/DefaultAllocationManagerFactory.java b/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/netty/DefaultAllocationManagerFactory.java index 8ece77178f0..b81562a256f 100644 --- a/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/netty/DefaultAllocationManagerFactory.java +++ b/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/netty/DefaultAllocationManagerFactory.java @@ -21,10 +21,7 @@ import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; -/** - * The default Allocation Manager Factory for a module. - * - */ +/** The default Allocation Manager Factory for a module. */ public class DefaultAllocationManagerFactory implements AllocationManager.Factory { public static final AllocationManager.Factory FACTORY = NettyAllocationManager.FACTORY; @@ -38,5 +35,4 @@ public AllocationManager create(BufferAllocator accountingAllocator, long size) public ArrowBuf empty() { return FACTORY.empty(); } - } diff --git a/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/netty/NettyAllocationManager.java b/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/netty/NettyAllocationManager.java index 58354d0c2ee..1383d5518f2 100644 --- a/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/netty/NettyAllocationManager.java +++ b/java/memory/memory-netty/src/main/java/org/apache/arrow/memory/netty/NettyAllocationManager.java @@ -17,60 +17,56 @@ package org.apache.arrow.memory.netty; +import io.netty.buffer.PooledByteBufAllocatorL; +import io.netty.buffer.UnsafeDirectLittleEndian; +import io.netty.util.internal.PlatformDependent; import org.apache.arrow.memory.AllocationManager; import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.ReferenceManager; -import io.netty.buffer.PooledByteBufAllocatorL; -import io.netty.buffer.UnsafeDirectLittleEndian; -import io.netty.util.internal.PlatformDependent; - /** - * The default implementation of {@link AllocationManager}. The implementation is responsible for managing when memory - * is allocated and returned to the Netty-based PooledByteBufAllocatorL. + * The default implementation of {@link AllocationManager}. The implementation is responsible for + * managing when memory is allocated and returned to the Netty-based PooledByteBufAllocatorL. */ public class NettyAllocationManager extends AllocationManager { - public static final AllocationManager.Factory FACTORY = new AllocationManager.Factory() { + public static final AllocationManager.Factory FACTORY = + new AllocationManager.Factory() { - @Override - public AllocationManager create(BufferAllocator accountingAllocator, long size) { - return new NettyAllocationManager(accountingAllocator, size); - } + @Override + public AllocationManager create(BufferAllocator accountingAllocator, long size) { + return new NettyAllocationManager(accountingAllocator, size); + } - @Override - public ArrowBuf empty() { - return EMPTY_BUFFER; - } - }; + @Override + public ArrowBuf empty() { + return EMPTY_BUFFER; + } + }; /** - * The default cut-off value for switching allocation strategies. - * If the request size is not greater than the cut-off value, we will allocate memory by - * {@link PooledByteBufAllocatorL} APIs, - * otherwise, we will use {@link PlatformDependent} APIs. + * The default cut-off value for switching allocation strategies. If the request size is not + * greater than the cut-off value, we will allocate memory by {@link PooledByteBufAllocatorL} + * APIs, otherwise, we will use {@link PlatformDependent} APIs. */ public static final int DEFAULT_ALLOCATION_CUTOFF_VALUE = Integer.MAX_VALUE; private static final PooledByteBufAllocatorL INNER_ALLOCATOR = new PooledByteBufAllocatorL(); static final UnsafeDirectLittleEndian EMPTY = INNER_ALLOCATOR.empty; - static final ArrowBuf EMPTY_BUFFER = new ArrowBuf(ReferenceManager.NO_OP, - null, - 0, - NettyAllocationManager.EMPTY.memoryAddress()); + static final ArrowBuf EMPTY_BUFFER = + new ArrowBuf(ReferenceManager.NO_OP, null, 0, NettyAllocationManager.EMPTY.memoryAddress()); static final long CHUNK_SIZE = INNER_ALLOCATOR.getChunkSize(); private final long allocatedSize; private final UnsafeDirectLittleEndian memoryChunk; private final long allocatedAddress; - /** - * The cut-off value for switching allocation strategies. - */ + /** The cut-off value for switching allocation strategies. */ private final int allocationCutOffValue; - NettyAllocationManager(BufferAllocator accountingAllocator, long requestedSize, int allocationCutOffValue) { + NettyAllocationManager( + BufferAllocator accountingAllocator, long requestedSize, int allocationCutOffValue) { super(accountingAllocator); this.allocationCutOffValue = allocationCutOffValue; @@ -91,9 +87,9 @@ public ArrowBuf empty() { /** * Get the underlying memory chunk managed by this AllocationManager. - * @return the underlying memory chunk if the request size is not greater than the - * {@link NettyAllocationManager#allocationCutOffValue}, or null otherwise. * + * @return the underlying memory chunk if the request size is not greater than the {@link + * NettyAllocationManager#allocationCutOffValue}, or null otherwise. * @deprecated this method will be removed in a future release. */ @Deprecated @@ -124,5 +120,4 @@ protected void release0() { public long getSize() { return allocatedSize; } - } diff --git a/java/memory/memory-netty/src/test/java/io/netty/buffer/TestExpandableByteBuf.java b/java/memory/memory-netty/src/test/java/io/netty/buffer/TestExpandableByteBuf.java index 67557b65a62..5a5396444cf 100644 --- a/java/memory/memory-netty/src/test/java/io/netty/buffer/TestExpandableByteBuf.java +++ b/java/memory/memory-netty/src/test/java/io/netty/buffer/TestExpandableByteBuf.java @@ -23,17 +23,12 @@ import org.junit.Assert; import org.junit.Test; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ExpandableByteBuf; -import io.netty.buffer.NettyArrowBuf; - public class TestExpandableByteBuf { @Test public void testCapacity() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(20); - ) { + ArrowBuf buf = allocator.buffer(20); ) { NettyArrowBuf srcByteBuf = NettyArrowBuf.unwrapBuffer(buf); ExpandableByteBuf expandableByteBuf = new ExpandableByteBuf(srcByteBuf, allocator); ByteBuf newByteBuf = expandableByteBuf.capacity(31); @@ -45,8 +40,7 @@ public void testCapacity() { @Test public void testCapacity1() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(20); - ) { + ArrowBuf buf = allocator.buffer(20); ) { NettyArrowBuf srcByteBuf = NettyArrowBuf.unwrapBuffer(buf); ExpandableByteBuf expandableByteBuf = new ExpandableByteBuf(srcByteBuf, allocator); ByteBuf newByteBuf = expandableByteBuf.capacity(32); @@ -58,13 +52,20 @@ public void testCapacity1() { @Test public void testSetAndGetIntValues() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(20); - ) { + ArrowBuf buf = allocator.buffer(20); ) { NettyArrowBuf srcByteBuf = NettyArrowBuf.unwrapBuffer(buf); ExpandableByteBuf expandableByteBuf = new ExpandableByteBuf(srcByteBuf, allocator); - int [] intVals = new int[] {Integer.MIN_VALUE, Short.MIN_VALUE - 1, Short.MIN_VALUE, 0 , - Short.MAX_VALUE , Short.MAX_VALUE + 1, Integer.MAX_VALUE}; - for (int intValue :intVals) { + int[] intVals = + new int[] { + Integer.MIN_VALUE, + Short.MIN_VALUE - 1, + Short.MIN_VALUE, + 0, + Short.MAX_VALUE, + Short.MAX_VALUE + 1, + Integer.MAX_VALUE + }; + for (int intValue : intVals) { expandableByteBuf.setInt(0, intValue); Assert.assertEquals(expandableByteBuf.getInt(0), intValue); Assert.assertEquals(expandableByteBuf.getIntLE(0), Integer.reverseBytes(intValue)); @@ -75,12 +76,11 @@ public void testSetAndGetIntValues() { @Test public void testSetAndGetLongValues() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(20); - ) { + ArrowBuf buf = allocator.buffer(20); ) { NettyArrowBuf srcByteBuf = NettyArrowBuf.unwrapBuffer(buf); ExpandableByteBuf expandableByteBuf = new ExpandableByteBuf(srcByteBuf, allocator); - long [] longVals = new long[] {Long.MIN_VALUE, 0 , Long.MAX_VALUE}; - for (long longValue :longVals) { + long[] longVals = new long[] {Long.MIN_VALUE, 0, Long.MAX_VALUE}; + for (long longValue : longVals) { expandableByteBuf.setLong(0, longValue); Assert.assertEquals(expandableByteBuf.getLong(0), longValue); Assert.assertEquals(expandableByteBuf.getLongLE(0), Long.reverseBytes(longValue)); @@ -91,12 +91,11 @@ public void testSetAndGetLongValues() { @Test public void testSetAndGetShortValues() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(20); - ) { + ArrowBuf buf = allocator.buffer(20); ) { NettyArrowBuf srcByteBuf = NettyArrowBuf.unwrapBuffer(buf); ExpandableByteBuf expandableByteBuf = new ExpandableByteBuf(srcByteBuf, allocator); - short [] shortVals = new short[] {Short.MIN_VALUE, 0 , Short.MAX_VALUE}; - for (short shortValue :shortVals) { + short[] shortVals = new short[] {Short.MIN_VALUE, 0, Short.MAX_VALUE}; + for (short shortValue : shortVals) { expandableByteBuf.setShort(0, shortValue); Assert.assertEquals(expandableByteBuf.getShort(0), shortValue); Assert.assertEquals(expandableByteBuf.getShortLE(0), Short.reverseBytes(shortValue)); @@ -107,12 +106,11 @@ public void testSetAndGetShortValues() { @Test public void testSetAndGetByteValues() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(20); - ) { + ArrowBuf buf = allocator.buffer(20); ) { NettyArrowBuf srcByteBuf = NettyArrowBuf.unwrapBuffer(buf); ExpandableByteBuf expandableByteBuf = new ExpandableByteBuf(srcByteBuf, allocator); - byte [] byteVals = new byte[] {Byte.MIN_VALUE, 0 , Byte.MAX_VALUE}; - for (short byteValue :byteVals) { + byte[] byteVals = new byte[] {Byte.MIN_VALUE, 0, Byte.MAX_VALUE}; + for (short byteValue : byteVals) { expandableByteBuf.setByte(0, byteValue); Assert.assertEquals(expandableByteBuf.getByte(0), byteValue); } diff --git a/java/memory/memory-netty/src/test/java/io/netty/buffer/TestNettyArrowBuf.java b/java/memory/memory-netty/src/test/java/io/netty/buffer/TestNettyArrowBuf.java index f18bccb4c9d..3e7df95adc3 100644 --- a/java/memory/memory-netty/src/test/java/io/netty/buffer/TestNettyArrowBuf.java +++ b/java/memory/memory-netty/src/test/java/io/netty/buffer/TestNettyArrowBuf.java @@ -18,7 +18,6 @@ package io.netty.buffer; import java.nio.ByteBuffer; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -26,16 +25,12 @@ import org.junit.Assert; import org.junit.Test; -import io.netty.buffer.CompositeByteBuf; -import io.netty.buffer.NettyArrowBuf; - public class TestNettyArrowBuf { @Test public void testSliceWithoutArgs() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(20); - ) { + ArrowBuf buf = allocator.buffer(20); ) { NettyArrowBuf nettyBuf = NettyArrowBuf.unwrapBuffer(buf); nettyBuf.writerIndex(20); nettyBuf.readerIndex(10); @@ -48,8 +43,7 @@ public void testSliceWithoutArgs() { @Test public void testNioBuffer() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(20); - ) { + ArrowBuf buf = allocator.buffer(20); ) { NettyArrowBuf nettyBuf = NettyArrowBuf.unwrapBuffer(buf); ByteBuffer byteBuffer = nettyBuf.nioBuffer(4, 6); // Nio Buffers should always be 0 indexed @@ -57,46 +51,50 @@ public void testNioBuffer() { Assert.assertEquals(6, byteBuffer.limit()); // Underlying buffer has size 32 excluding 4 should have capacity of 28. Assert.assertEquals(28, byteBuffer.capacity()); - } } @Test public void testInternalNioBuffer() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(20); - ) { + ArrowBuf buf = allocator.buffer(20); ) { NettyArrowBuf nettyBuf = NettyArrowBuf.unwrapBuffer(buf); ByteBuffer byteBuffer = nettyBuf.internalNioBuffer(4, 6); Assert.assertEquals(0, byteBuffer.position()); Assert.assertEquals(6, byteBuffer.limit()); // Underlying buffer has size 32 excluding 4 should have capacity of 28. Assert.assertEquals(28, byteBuffer.capacity()); - } } @Test public void testSetLEValues() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(20); - ) { + ArrowBuf buf = allocator.buffer(20); ) { NettyArrowBuf nettyBuf = NettyArrowBuf.unwrapBuffer(buf); - int [] intVals = new int[] {Integer.MIN_VALUE, Short.MIN_VALUE - 1, Short.MIN_VALUE, 0 , - Short.MAX_VALUE , Short.MAX_VALUE + 1, Integer.MAX_VALUE}; - for (int intValue :intVals ) { + int[] intVals = + new int[] { + Integer.MIN_VALUE, + Short.MIN_VALUE - 1, + Short.MIN_VALUE, + 0, + Short.MAX_VALUE, + Short.MAX_VALUE + 1, + Integer.MAX_VALUE + }; + for (int intValue : intVals) { nettyBuf._setInt(0, intValue); Assert.assertEquals(nettyBuf._getIntLE(0), Integer.reverseBytes(intValue)); } - long [] longVals = new long[] {Long.MIN_VALUE, 0 , Long.MAX_VALUE}; - for (long longValue :longVals ) { + long[] longVals = new long[] {Long.MIN_VALUE, 0, Long.MAX_VALUE}; + for (long longValue : longVals) { nettyBuf._setLong(0, longValue); Assert.assertEquals(nettyBuf._getLongLE(0), Long.reverseBytes(longValue)); } - short [] shortVals = new short[] {Short.MIN_VALUE, 0 , Short.MAX_VALUE}; - for (short shortValue :shortVals ) { + short[] shortVals = new short[] {Short.MIN_VALUE, 0, Short.MAX_VALUE}; + for (short shortValue : shortVals) { nettyBuf._setShort(0, shortValue); Assert.assertEquals(nettyBuf._getShortLE(0), Short.reverseBytes(shortValue)); } @@ -106,11 +104,10 @@ public void testSetLEValues() { @Test public void testSetCompositeBuffer() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(20); - NettyArrowBuf buf2 = NettyArrowBuf.unwrapBuffer(allocator.buffer(20)); - ) { - CompositeByteBuf byteBufs = new CompositeByteBuf(new ArrowByteBufAllocator(allocator), - true, 1); + ArrowBuf buf = allocator.buffer(20); + NettyArrowBuf buf2 = NettyArrowBuf.unwrapBuffer(allocator.buffer(20)); ) { + CompositeByteBuf byteBufs = + new CompositeByteBuf(new ArrowByteBufAllocator(allocator), true, 1); int expected = 4; buf2.setInt(0, expected); buf2.writerIndex(4); @@ -124,10 +121,9 @@ public void testSetCompositeBuffer() { @Test public void testGetCompositeBuffer() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(20); - ) { - CompositeByteBuf byteBufs = new CompositeByteBuf(new ArrowByteBufAllocator(allocator), - true, 1); + ArrowBuf buf = allocator.buffer(20); ) { + CompositeByteBuf byteBufs = + new CompositeByteBuf(new ArrowByteBufAllocator(allocator), true, 1); int expected = 4; buf.setInt(0, expected); NettyArrowBuf buf2 = NettyArrowBuf.unwrapBuffer(allocator.buffer(20)); @@ -145,8 +141,7 @@ public void testGetCompositeBuffer() { @Test public void testUnwrapReturnsNull() { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(20); - ) { + ArrowBuf buf = allocator.buffer(20); ) { NettyArrowBuf nettyBuf = NettyArrowBuf.unwrapBuffer(buf); // NettyArrowBuf cannot be unwrapped, so unwrap() should return null per the Netty ByteBuf API Assert.assertNull(nettyBuf.unwrap()); diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/ITTestLargeArrowBuf.java b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/ITTestLargeArrowBuf.java index 71dba73d289..6ed686fdf2d 100644 --- a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/ITTestLargeArrowBuf.java +++ b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/ITTestLargeArrowBuf.java @@ -26,17 +26,16 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; - /** - * Integration test for large (more than 2GB) {@link org.apache.arrow.memory.ArrowBuf}. - * To run this test, please make sure there is at least 4GB memory in the system. + * Integration test for large (more than 2GB) {@link org.apache.arrow.memory.ArrowBuf}. To run this + * test, please make sure there is at least 4GB memory in the system. */ public class ITTestLargeArrowBuf { private static final Logger logger = LoggerFactory.getLogger(ITTestLargeArrowBuf.class); private void run(long bufSize) { try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - ArrowBuf largeBuf = allocator.buffer(bufSize)) { + ArrowBuf largeBuf = allocator.buffer(bufSize)) { assertEquals(bufSize, largeBuf.capacity()); logger.trace("Successfully allocated a buffer with capacity {}", largeBuf.capacity()); @@ -71,5 +70,4 @@ public void testLargeArrowBuf() { public void testMaxIntArrowBuf() { run(Integer.MAX_VALUE); } - } diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestAllocationManagerNetty.java b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestAllocationManagerNetty.java index 7f1e34ddc5f..50830b35ead 100644 --- a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestAllocationManagerNetty.java +++ b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestAllocationManagerNetty.java @@ -23,9 +23,7 @@ import org.apache.arrow.memory.DefaultAllocationManagerOption; import org.junit.Test; -/** - * Test cases for {@link AllocationManager}. - */ +/** Test cases for {@link AllocationManager}. */ public class TestAllocationManagerNetty { @Test diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestEmptyArrowBuf.java b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestEmptyArrowBuf.java index b9948083e6f..db3bbfb32c9 100644 --- a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestEmptyArrowBuf.java +++ b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestEmptyArrowBuf.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals; +import io.netty.buffer.PooledByteBufAllocatorL; import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.ReferenceManager; import org.apache.arrow.memory.RootAllocator; @@ -26,8 +27,6 @@ import org.junit.BeforeClass; import org.junit.Test; -import io.netty.buffer.PooledByteBufAllocatorL; - public class TestEmptyArrowBuf { private static final int MAX_ALLOCATION = 8 * 1024; @@ -37,7 +36,7 @@ public class TestEmptyArrowBuf { public static void beforeClass() { allocator = new RootAllocator(MAX_ALLOCATION); } - + /** Ensure the allocator is closed. */ @AfterClass public static void afterClass() { @@ -48,16 +47,22 @@ public static void afterClass() { @Test public void testZeroBuf() { - // Exercise the historical log inside the empty ArrowBuf. This is initialized statically, and there is a circular - // dependency between ArrowBuf and BaseAllocator, so if the initialization happens in the wrong order, the + // Exercise the historical log inside the empty ArrowBuf. This is initialized statically, and + // there is a circular + // dependency between ArrowBuf and BaseAllocator, so if the initialization happens in the wrong + // order, the // historical log will be null even though RootAllocator.DEBUG is true. allocator.getEmpty().print(new StringBuilder(), 0, RootAllocator.Verbosity.LOG_WITH_STACKTRACE); } @Test public void testEmptyArrowBuf() { - ArrowBuf buf = new ArrowBuf(ReferenceManager.NO_OP, null, - 1024, new PooledByteBufAllocatorL().empty.memoryAddress()); + ArrowBuf buf = + new ArrowBuf( + ReferenceManager.NO_OP, + null, + 1024, + new PooledByteBufAllocatorL().empty.memoryAddress()); buf.getReferenceManager().retain(); buf.getReferenceManager().retain(8); @@ -74,7 +79,8 @@ public void testEmptyArrowBuf() { assertEquals(false, buf.getReferenceManager().release()); assertEquals(false, buf.getReferenceManager().release(2)); assertEquals(0, buf.getReferenceManager().getAllocator().getLimit()); - assertEquals(buf, buf.getReferenceManager().transferOwnership(buf, allocator).getTransferredBuffer()); + assertEquals( + buf, buf.getReferenceManager().transferOwnership(buf, allocator).getTransferredBuffer()); assertEquals(0, buf.readerIndex()); assertEquals(0, buf.writerIndex()); assertEquals(1, buf.refCnt()); @@ -85,7 +91,5 @@ public void testEmptyArrowBuf() { assertEquals(1, derive.refCnt()); buf.close(); - } - } diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestEndianness.java b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestEndianness.java index a782523cbc6..2dbd1272e58 100644 --- a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestEndianness.java +++ b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestEndianness.java @@ -19,15 +19,13 @@ import static org.junit.Assert.assertEquals; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.NettyArrowBuf; import java.nio.ByteOrder; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.junit.Test; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.NettyArrowBuf; - public class TestEndianness { @Test @@ -49,5 +47,4 @@ public void testNativeEndian() { b.release(); a.close(); } - } diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocationManager.java b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocationManager.java index 39692c96ceb..907ce11474c 100644 --- a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocationManager.java +++ b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocationManager.java @@ -29,26 +29,28 @@ import org.apache.arrow.memory.RootAllocator; import org.junit.Test; -/** - * Test cases for {@link NettyAllocationManager}. - */ +/** Test cases for {@link NettyAllocationManager}. */ public class TestNettyAllocationManager { static int CUSTOMIZED_ALLOCATION_CUTOFF_VALUE = 1024; private RootAllocator createCustomizedAllocator() { - return new RootAllocator(RootAllocator.configBuilder() - .allocationManagerFactory(new AllocationManager.Factory() { - @Override - public AllocationManager create(BufferAllocator accountingAllocator, long size) { - return new NettyAllocationManager(accountingAllocator, size, CUSTOMIZED_ALLOCATION_CUTOFF_VALUE); - } - - @Override - public ArrowBuf empty() { - return null; - } - }).build()); + return new RootAllocator( + RootAllocator.configBuilder() + .allocationManagerFactory( + new AllocationManager.Factory() { + @Override + public AllocationManager create(BufferAllocator accountingAllocator, long size) { + return new NettyAllocationManager( + accountingAllocator, size, CUSTOMIZED_ALLOCATION_CUTOFF_VALUE); + } + + @Override + public ArrowBuf empty() { + return null; + } + }) + .build()); } private void readWriteArrowBuf(ArrowBuf buffer) { @@ -64,14 +66,12 @@ private void readWriteArrowBuf(ArrowBuf buffer) { } } - /** - * Test the allocation strategy for small buffers.. - */ + /** Test the allocation strategy for small buffers.. */ @Test public void testSmallBufferAllocation() { final long bufSize = CUSTOMIZED_ALLOCATION_CUTOFF_VALUE - 512L; try (RootAllocator allocator = createCustomizedAllocator(); - ArrowBuf buffer = allocator.buffer(bufSize)) { + ArrowBuf buffer = allocator.buffer(bufSize)) { assertTrue(buffer.getReferenceManager() instanceof BufferLedger); BufferLedger bufferLedger = (BufferLedger) buffer.getReferenceManager(); @@ -88,14 +88,12 @@ public void testSmallBufferAllocation() { } } - /** - * Test the allocation strategy for large buffers.. - */ + /** Test the allocation strategy for large buffers.. */ @Test public void testLargeBufferAllocation() { final long bufSize = CUSTOMIZED_ALLOCATION_CUTOFF_VALUE + 1024L; try (RootAllocator allocator = createCustomizedAllocator(); - ArrowBuf buffer = allocator.buffer(bufSize)) { + ArrowBuf buffer = allocator.buffer(bufSize)) { assertTrue(buffer.getReferenceManager() instanceof BufferLedger); BufferLedger bufferLedger = (BufferLedger) buffer.getReferenceManager(); diff --git a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocator.java b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocator.java index 07fdc3f784e..8c312b83ce6 100644 --- a/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocator.java +++ b/java/memory/memory-netty/src/test/java/org/apache/arrow/memory/netty/TestNettyAllocator.java @@ -19,23 +19,19 @@ import static org.junit.Assert.assertTrue; +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import io.netty.buffer.PooledByteBufAllocatorL; import java.util.Collections; import java.util.stream.Collectors; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.ReferenceManager; import org.junit.Test; import org.slf4j.LoggerFactory; -import ch.qos.logback.classic.Level; -import ch.qos.logback.classic.Logger; -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.read.ListAppender; -import io.netty.buffer.PooledByteBufAllocatorL; - -/** - * Test netty allocators. - */ +/** Test netty allocators. */ public class TestNettyAllocator { @Test @@ -47,30 +43,41 @@ public void testMemoryUsage() { logger.setLevel(Level.TRACE); logger.addAppender(memoryLogsAppender); memoryLogsAppender.start(); - try (ArrowBuf buf = new ArrowBuf(ReferenceManager.NO_OP, null, - 1024, new PooledByteBufAllocatorL().empty.memoryAddress())) { + try (ArrowBuf buf = + new ArrowBuf( + ReferenceManager.NO_OP, + null, + 1024, + new PooledByteBufAllocatorL().empty.memoryAddress())) { buf.memoryAddress(); } boolean result = false; long startTime = System.currentTimeMillis(); - while ((System.currentTimeMillis() - startTime) < 10000) { // 10 seconds maximum for time to read logs - // Lock on the list backing the appender since a background thread might try to add more logs - // while stream() is iterating over list elements. This would throw a flakey ConcurrentModificationException. + while ((System.currentTimeMillis() - startTime) + < 10000) { // 10 seconds maximum for time to read logs + // Lock on the list backing the appender since a background thread might try to add more + // logs + // while stream() is iterating over list elements. This would throw a flakey + // ConcurrentModificationException. synchronized (memoryLogsAppender.list) { - result = memoryLogsAppender.list.stream() - .anyMatch( - log -> log.toString().contains("Memory Usage: \n") && - log.toString().contains("Large buffers outstanding: ") && - log.toString().contains("Normal buffers outstanding: ") && - log.getLevel().equals(Level.TRACE) - ); + result = + memoryLogsAppender.list.stream() + .anyMatch( + log -> + log.toString().contains("Memory Usage: \n") + && log.toString().contains("Large buffers outstanding: ") + && log.toString().contains("Normal buffers outstanding: ") + && log.getLevel().equals(Level.TRACE)); } if (result) { break; } } - assertTrue("Log messages are:\n" + - memoryLogsAppender.list.stream().map(ILoggingEvent::toString).collect(Collectors.joining("\n")), + assertTrue( + "Log messages are:\n" + + memoryLogsAppender.list.stream() + .map(ILoggingEvent::toString) + .collect(Collectors.joining("\n")), result); } finally { memoryLogsAppender.stop(); diff --git a/java/memory/memory-netty/src/test/resources/logback.xml b/java/memory/memory-netty/src/test/resources/logback.xml index 4c54d18a210..9fa3f1e3b43 100644 --- a/java/memory/memory-netty/src/test/resources/logback.xml +++ b/java/memory/memory-netty/src/test/resources/logback.xml @@ -1,4 +1,4 @@ - + - diff --git a/java/memory/memory-unsafe/pom.xml b/java/memory/memory-unsafe/pom.xml index 07a140e5945..da4a5fe3223 100644 --- a/java/memory/memory-unsafe/pom.xml +++ b/java/memory/memory-unsafe/pom.xml @@ -10,18 +10,17 @@ OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> + 4.0.0 - arrow-memory org.apache.arrow + arrow-memory 16.0.0-SNAPSHOT - 4.0.0 arrow-memory-unsafe Arrow Memory - Unsafe Allocator and utils for allocating memory in Arrow based on sun.misc.Unsafe - org.apache.arrow diff --git a/java/memory/memory-unsafe/src/main/java/module-info.java b/java/memory/memory-unsafe/src/main/java/module-info.java index aa340d21716..526ebbdabbf 100644 --- a/java/memory/memory-unsafe/src/main/java/module-info.java +++ b/java/memory/memory-unsafe/src/main/java/module-info.java @@ -16,7 +16,8 @@ */ module org.apache.arrow.memory.unsafe { - exports org.apache.arrow.memory.unsafe to org.apache.arrow.memory.core; - + exports org.apache.arrow.memory.unsafe to + org.apache.arrow.memory.core; + requires org.apache.arrow.memory.core; } diff --git a/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/unsafe/DefaultAllocationManagerFactory.java b/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/unsafe/DefaultAllocationManagerFactory.java index dfb6c706856..7dc9db5df42 100644 --- a/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/unsafe/DefaultAllocationManagerFactory.java +++ b/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/unsafe/DefaultAllocationManagerFactory.java @@ -21,10 +21,7 @@ import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; -/** - * The default Allocation Manager Factory for a module. - * - */ +/** The default Allocation Manager Factory for a module. */ public class DefaultAllocationManagerFactory implements AllocationManager.Factory { public static final AllocationManager.Factory FACTORY = UnsafeAllocationManager.FACTORY; diff --git a/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/unsafe/UnsafeAllocationManager.java b/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/unsafe/UnsafeAllocationManager.java index 3468a6ec65c..02d79bd041a 100644 --- a/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/unsafe/UnsafeAllocationManager.java +++ b/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/unsafe/UnsafeAllocationManager.java @@ -23,28 +23,24 @@ import org.apache.arrow.memory.ReferenceManager; import org.apache.arrow.memory.util.MemoryUtil; -/** - * Allocation manager based on unsafe API. - */ +/** Allocation manager based on unsafe API. */ public final class UnsafeAllocationManager extends AllocationManager { - private static final ArrowBuf EMPTY = new ArrowBuf(ReferenceManager.NO_OP, - null, - 0, - MemoryUtil.UNSAFE.allocateMemory(0) - ); + private static final ArrowBuf EMPTY = + new ArrowBuf(ReferenceManager.NO_OP, null, 0, MemoryUtil.UNSAFE.allocateMemory(0)); - public static final AllocationManager.Factory FACTORY = new Factory() { - @Override - public AllocationManager create(BufferAllocator accountingAllocator, long size) { - return new UnsafeAllocationManager(accountingAllocator, size); - } + public static final AllocationManager.Factory FACTORY = + new Factory() { + @Override + public AllocationManager create(BufferAllocator accountingAllocator, long size) { + return new UnsafeAllocationManager(accountingAllocator, size); + } - @Override - public ArrowBuf empty() { - return EMPTY; - } - }; + @Override + public ArrowBuf empty() { + return EMPTY; + } + }; private final long allocatedSize; @@ -70,5 +66,4 @@ protected long memoryAddress() { protected void release0() { MemoryUtil.UNSAFE.freeMemory(allocatedAddress); } - } diff --git a/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/unsafe/TestAllocationManagerUnsafe.java b/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/unsafe/TestAllocationManagerUnsafe.java index f1ca96eea0f..25be2cc424f 100644 --- a/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/unsafe/TestAllocationManagerUnsafe.java +++ b/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/unsafe/TestAllocationManagerUnsafe.java @@ -23,9 +23,7 @@ import org.apache.arrow.memory.DefaultAllocationManagerOption; import org.junit.Test; -/** - * Test cases for {@link AllocationManager}. - */ +/** Test cases for {@link AllocationManager}. */ public class TestAllocationManagerUnsafe { @Test @@ -38,6 +36,5 @@ public void testAllocationManagerType() { DefaultAllocationManagerOption.getDefaultAllocationManagerType(); assertEquals(DefaultAllocationManagerOption.AllocationManagerType.Unsafe, mgrType); - } } diff --git a/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/unsafe/TestUnsafeAllocationManager.java b/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/unsafe/TestUnsafeAllocationManager.java index 77233e73cb3..67ae649335a 100644 --- a/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/unsafe/TestUnsafeAllocationManager.java +++ b/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/unsafe/TestUnsafeAllocationManager.java @@ -27,14 +27,14 @@ import org.apache.arrow.memory.RootAllocator; import org.junit.Test; -/** - * Test cases for {@link UnsafeAllocationManager}. - */ +/** Test cases for {@link UnsafeAllocationManager}. */ public class TestUnsafeAllocationManager { private BufferAllocator createUnsafeAllocator() { - return new RootAllocator(RootAllocator.configBuilder().allocationManagerFactory(UnsafeAllocationManager.FACTORY) - .build()); + return new RootAllocator( + RootAllocator.configBuilder() + .allocationManagerFactory(UnsafeAllocationManager.FACTORY) + .build()); } private void readWriteArrowBuf(ArrowBuf buffer) { @@ -50,14 +50,12 @@ private void readWriteArrowBuf(ArrowBuf buffer) { } } - /** - * Test the memory allocation for {@link UnsafeAllocationManager}. - */ + /** Test the memory allocation for {@link UnsafeAllocationManager}. */ @Test public void testBufferAllocation() { final long bufSize = 4096L; try (BufferAllocator allocator = createUnsafeAllocator(); - ArrowBuf buffer = allocator.buffer(bufSize)) { + ArrowBuf buffer = allocator.buffer(bufSize)) { assertTrue(buffer.getReferenceManager() instanceof BufferLedger); BufferLedger bufferLedger = (BufferLedger) buffer.getReferenceManager(); diff --git a/java/memory/memory-unsafe/src/test/resources/logback.xml b/java/memory/memory-unsafe/src/test/resources/logback.xml index 4c54d18a210..9fa3f1e3b43 100644 --- a/java/memory/memory-unsafe/src/test/resources/logback.xml +++ b/java/memory/memory-unsafe/src/test/resources/logback.xml @@ -1,4 +1,4 @@ - + - diff --git a/java/memory/pom.xml b/java/memory/pom.xml index 9e2d6127657..647a64bd075 100644 --- a/java/memory/pom.xml +++ b/java/memory/pom.xml @@ -1,4 +1,4 @@ - + - 4.0.0 - - arrow-java-root - org.apache.arrow - 16.0.0-SNAPSHOT - - arrow-performance - jar - Arrow Performance Benchmarks - JMH Performance benchmarks for other Arrow libraries. + 4.0.0 + + org.apache.arrow + arrow-java-root + 16.0.0-SNAPSHOT + + arrow-performance + jar + Arrow Performance Benchmarks + JMH Performance benchmarks for other Arrow libraries. - - - org.openjdk.jmh - jmh-core - ${jmh.version} - test - - - org.openjdk.jmh - jmh-generator-annprocess - ${jmh.version} - provided - - - org.apache.arrow - arrow-vector - ${arrow.vector.classifier} - test - - - org.apache.arrow - arrow-memory-core - test - - - org.apache.arrow - arrow-memory-netty - test - - - org.apache.avro - avro - ${dep.avro.version} - test - - - org.apache.arrow - arrow-avro - test - - - com.h2database - h2 - 2.2.224 - test - - - org.apache.arrow - arrow-jdbc - test - - - org.apache.arrow - arrow-algorithm - test - - + + UTF-8 + 1.21 + 1.8 + benchmarks + true + .* + 1 + + 5 + 5 + + jmh-result.json + json + - - UTF-8 - 1.21 - 1.8 - benchmarks - true - .* - 1 - - 5 - 5 - - jmh-result.json - json - + + + org.openjdk.jmh + jmh-core + ${jmh.version} + test + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + provided + + + org.apache.arrow + arrow-vector + ${arrow.vector.classifier} + test + + + org.apache.arrow + arrow-memory-core + test + + + org.apache.arrow + arrow-memory-netty + test + + + org.apache.avro + avro + ${dep.avro.version} + test + + + org.apache.arrow + arrow-avro + test + + + com.h2database + h2 + 2.2.224 + test + + + org.apache.arrow + arrow-jdbc + test + + + org.apache.arrow + arrow-algorithm + test + + - - - - org.apache.maven.plugins - maven-compiler-plugin - - ${javac.target} - ${javac.target} - ${javac.target} - - - - org.apache.maven.plugins - maven-shade-plugin - - - package - - shade - - - ${uberjar.name} - - - org.openjdk.jmh.Main - - - - - *:* - - META-INF/*.SF - META-INF/*.DSA - META-INF/*.RSA - - - - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - run-java-benchmarks - integration-test - - exec - - - - - ${skip.perf.benchmarks} - test - java - - -classpath - - org.openjdk.jmh.Main - ${benchmark.filter} - -f - ${benchmark.forks} - -jvmArgs - ${benchmark.jvmargs} - -wi - ${benchmark.warmups} - -i - ${benchmark.runs} - ${benchmark.list} - -rff - ${benchmark.resultfile} - -rf - ${benchmark.resultformat} - - - - - - - - maven-clean-plugin - 2.5 - - - maven-deploy-plugin - 3.1.1 - - - maven-install-plugin - 3.1.1 - - - maven-jar-plugin - 3.3.0 - - - maven-javadoc-plugin - 3.6.3 - - - maven-resources-plugin - 3.3.1 - - - maven-source-plugin - 2.2.1 - - - maven-surefire-plugin - 3.2.3 - - - - + + + + + maven-clean-plugin + 2.5 + + + maven-deploy-plugin + 3.1.1 + + + maven-install-plugin + 3.1.1 + + + maven-jar-plugin + 3.3.0 + + + maven-javadoc-plugin + 3.6.3 + + + maven-resources-plugin + 3.3.1 + + + maven-source-plugin + 2.2.1 + + + maven-surefire-plugin + 3.2.3 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${javac.target} + ${javac.target} + ${javac.target} + + + + org.apache.maven.plugins + maven-shade-plugin + + + + shade + + package + + ${uberjar.name} + + + org.openjdk.jmh.Main + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + ${skip.perf.benchmarks} + test + java + + -classpath + + org.openjdk.jmh.Main + ${benchmark.filter} + -f + ${benchmark.forks} + -jvmArgs + ${benchmark.jvmargs} + -wi + ${benchmark.warmups} + -i + ${benchmark.runs} + ${benchmark.list} + -rff + ${benchmark.resultfile} + -rf + ${benchmark.resultformat} + + + + + run-java-benchmarks + + exec + + integration-test + + + + + diff --git a/java/performance/src/test/java/org/apache/arrow/adapter/AvroAdapterBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/adapter/AvroAdapterBenchmarks.java index c07aeffafb1..c35f18e3b3d 100644 --- a/java/performance/src/test/java/org/apache/arrow/adapter/AvroAdapterBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/adapter/AvroAdapterBenchmarks.java @@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.concurrent.TimeUnit; - import org.apache.arrow.adapter.avro.AvroToArrow; import org.apache.arrow.adapter.avro.AvroToArrowConfig; import org.apache.arrow.adapter.avro.AvroToArrowConfigBuilder; @@ -52,9 +51,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for avro adapter. - */ +/** Benchmarks for avro adapter. */ @State(Scope.Benchmark) public class AvroAdapterBenchmarks { @@ -65,21 +62,25 @@ public class AvroAdapterBenchmarks { private Schema schema; private BinaryDecoder decoder; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup public void prepare() throws Exception { BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); config = new AvroToArrowConfigBuilder(allocator).build(); - String schemaStr = "{\n" + " \"namespace\": \"org.apache.arrow.avro\",\n" + - " \"type\": \"record\",\n" + " \"name\": \"testBenchmark\",\n" + " \"fields\": [\n" + - " {\"name\": \"f0\", \"type\": \"string\"},\n" + - " {\"name\": \"f1\", \"type\": \"int\"},\n" + - " {\"name\": \"f2\", \"type\": \"long\"},\n" + - " {\"name\": \"f3\", \"type\": \"boolean\"},\n" + - " {\"name\": \"f4\", \"type\": \"float\"}\n" + " ]\n" + "}"; + String schemaStr = + "{\n" + + " \"namespace\": \"org.apache.arrow.avro\",\n" + + " \"type\": \"record\",\n" + + " \"name\": \"testBenchmark\",\n" + + " \"fields\": [\n" + + " {\"name\": \"f0\", \"type\": \"string\"},\n" + + " {\"name\": \"f1\", \"type\": \"int\"},\n" + + " {\"name\": \"f2\", \"type\": \"long\"},\n" + + " {\"name\": \"f3\", \"type\": \"boolean\"},\n" + + " {\"name\": \"f4\", \"type\": \"float\"}\n" + + " ]\n" + + "}"; schema = new Schema.Parser().parse(schemaStr); ByteArrayOutputStream out = new ByteArrayOutputStream(); @@ -96,12 +97,11 @@ public void prepare() throws Exception { writer.write(record, encoder); } - decoder = new DecoderFactory().directBinaryDecoder(new ByteArrayInputStream(out.toByteArray()), null); + decoder = + new DecoderFactory().directBinaryDecoder(new ByteArrayInputStream(out.toByteArray()), null); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown public void tearDown() { config.getAllocator().close(); @@ -109,6 +109,7 @@ public void tearDown() { /** * Test {@link AvroToArrow#avroToArrowIterator(Schema, Decoder, AvroToArrowConfig)}. + * * @return useless. To avoid DCE by JIT. */ @Benchmark @@ -117,7 +118,8 @@ public void tearDown() { public int testAvroToArrow() throws Exception { decoder.inputStream().reset(); int sum = 0; - try (AvroToArrowVectorIterator iter = AvroToArrow.avroToArrowIterator(schema, decoder, config)) { + try (AvroToArrowVectorIterator iter = + AvroToArrow.avroToArrowIterator(schema, decoder, config)) { while (iter.hasNext()) { VectorSchemaRoot root = iter.next(); IntVector intVector = (IntVector) root.getVector("f1"); @@ -131,10 +133,8 @@ public int testAvroToArrow() throws Exception { } public static void main(String[] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(AvroAdapterBenchmarks.class.getSimpleName()) - .forks(1) - .build(); + Options opt = + new OptionsBuilder().include(AvroAdapterBenchmarks.class.getSimpleName()).forks(1).build(); new Runner(opt).run(); } diff --git a/java/performance/src/test/java/org/apache/arrow/adapter/jdbc/JdbcAdapterBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/adapter/jdbc/JdbcAdapterBenchmarks.java index fd3940b4c87..a96ae447e6e 100644 --- a/java/performance/src/test/java/org/apache/arrow/adapter/jdbc/JdbcAdapterBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/adapter/jdbc/JdbcAdapterBenchmarks.java @@ -23,7 +23,6 @@ import java.sql.ResultSet; import java.sql.Statement; import java.util.concurrent.TimeUnit; - import org.apache.arrow.adapter.jdbc.consumer.BigIntConsumer; import org.apache.arrow.adapter.jdbc.consumer.BitConsumer; import org.apache.arrow.adapter.jdbc.consumer.IntConsumer; @@ -50,26 +49,22 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for Jdbc adapter. - */ +/** Benchmarks for Jdbc adapter. */ public class JdbcAdapterBenchmarks { private static final int VALUE_COUNT = 3000; private static final String CREATE_STATEMENT = - "CREATE TABLE test_table (f0 INT, f1 LONG, f2 VARCHAR, f3 BOOLEAN);"; + "CREATE TABLE test_table (f0 INT, f1 LONG, f2 VARCHAR, f3 BOOLEAN);"; private static final String INSERT_STATEMENT = - "INSERT INTO test_table (f0, f1, f2, f3) VALUES (?, ?, ?, ?);"; + "INSERT INTO test_table (f0, f1, f2, f3) VALUES (?, ?, ?, ?);"; private static final String QUERY = "SELECT f0, f1, f2, f3 FROM test_table;"; private static final String DROP_STATEMENT = "DROP TABLE test_table;"; private static final String URL = "jdbc:h2:mem:JdbcAdapterBenchmarks"; private static final String DRIVER = "org.h2.Driver"; - /** - * State object for the jdbc e2e benchmark. - */ + /** State object for the jdbc e2e benchmark. */ @State(Scope.Benchmark) public static class JdbcState { @@ -86,7 +81,8 @@ public static class JdbcState { @Setup(Level.Trial) public void prepareState() throws Exception { allocator = new RootAllocator(Integer.MAX_VALUE); - config = new JdbcToArrowConfigBuilder().setAllocator(allocator).setTargetBatchSize(1024).build(); + config = + new JdbcToArrowConfigBuilder().setAllocator(allocator).setTargetBatchSize(1024).build(); Class.forName(DRIVER); conn = DriverManager.getConnection(URL); @@ -128,9 +124,7 @@ public void tearDownState() throws Exception { } } - /** - * State object for the consume benchmark. - */ + /** State object for the consume benchmark. */ @State(Scope.Benchmark) public static class ConsumeState { @@ -165,7 +159,8 @@ public static class ConsumeState { @Setup(Level.Trial) public void prepare() throws Exception { allocator = new RootAllocator(Integer.MAX_VALUE); - config = new JdbcToArrowConfigBuilder().setAllocator(allocator).setTargetBatchSize(1024).build(); + config = + new JdbcToArrowConfigBuilder().setAllocator(allocator).setTargetBatchSize(1024).build(); Class.forName(DRIVER); conn = DriverManager.getConnection(URL); @@ -232,9 +227,7 @@ public void tearDown() throws Exception { } } - /** - * State object for the jdbc row consume benchmark. - */ + /** State object for the jdbc row consume benchmark. */ @State(Scope.Benchmark) public static class RowConsumeState { @@ -255,7 +248,11 @@ public static class RowConsumeState { @Setup(Level.Trial) public void prepareState() throws Exception { allocator = new RootAllocator(Integer.MAX_VALUE); - config = new JdbcToArrowConfigBuilder().setAllocator(allocator).setTargetBatchSize(VALUE_COUNT).build(); + config = + new JdbcToArrowConfigBuilder() + .setAllocator(allocator) + .setTargetBatchSize(VALUE_COUNT) + .build(); Class.forName(DRIVER); conn = DriverManager.getConnection(URL); @@ -304,6 +301,7 @@ public void tearDownState() throws Exception { /** * Test {@link JdbcToArrow#sqlToArrowVectorIterator(ResultSet, JdbcToArrowConfig)}. + * * @return useless. To avoid DCE by JIT. */ @Benchmark @@ -311,7 +309,8 @@ public void tearDownState() throws Exception { @OutputTimeUnit(TimeUnit.MICROSECONDS) public int testJdbcToArrow(JdbcState state) throws Exception { int valueCount = 0; - try (ArrowVectorIterator iter = JdbcToArrow.sqlToArrowVectorIterator(state.resultSet, state.config)) { + try (ArrowVectorIterator iter = + JdbcToArrow.sqlToArrowVectorIterator(state.resultSet, state.config)) { while (iter.hasNext()) { VectorSchemaRoot root = iter.next(); IntVector intVector = (IntVector) root.getFieldVectors().get(0); @@ -348,12 +347,9 @@ public void consumeRowsBenchmark(RowConsumeState state) throws Exception { } public static void main(String[] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(JdbcAdapterBenchmarks.class.getSimpleName()) - .forks(1) - .build(); + Options opt = + new OptionsBuilder().include(JdbcAdapterBenchmarks.class.getSimpleName()).forks(1).build(); new Runner(opt).run(); } } - diff --git a/java/performance/src/test/java/org/apache/arrow/algorithm/search/ParallelSearcherBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/algorithm/search/ParallelSearcherBenchmarks.java index 1c3af77e73a..538ff9c0bf9 100644 --- a/java/performance/src/test/java/org/apache/arrow/algorithm/search/ParallelSearcherBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/algorithm/search/ParallelSearcherBenchmarks.java @@ -20,7 +20,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.IntVector; @@ -39,16 +38,12 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link ParallelSearcher}. - */ +/** Benchmarks for {@link ParallelSearcher}. */ public class ParallelSearcherBenchmarks { private static final int VECTOR_LENGTH = 1024 * 1024; - /** - * State object for the benchmarks. - */ + /** State object for the benchmarks. */ @State(Scope.Benchmark) public static class SearchState { @@ -105,7 +100,8 @@ public void searchBenchmark(SearchState state) throws Exception { } public static void main(String[] args) throws RunnerException { - Options opt = new OptionsBuilder() + Options opt = + new OptionsBuilder() .include(ParallelSearcherBenchmarks.class.getSimpleName()) .forks(1) .build(); diff --git a/java/performance/src/test/java/org/apache/arrow/memory/AllocatorBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/memory/AllocatorBenchmarks.java index 88fcf73f030..387cd6dc667 100644 --- a/java/performance/src/test/java/org/apache/arrow/memory/AllocatorBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/memory/AllocatorBenchmarks.java @@ -18,8 +18,6 @@ package org.apache.arrow.memory; import java.util.concurrent.TimeUnit; - -import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.rounding.RoundingPolicy; import org.apache.arrow.memory.rounding.SegmentRoundingPolicy; import org.openjdk.jmh.annotations.Benchmark; @@ -31,14 +29,10 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for allocators. - */ +/** Benchmarks for allocators. */ public class AllocatorBenchmarks { - /** - * Benchmark for the default allocator. - */ + /** Benchmark for the default allocator. */ @Benchmark @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) @@ -59,9 +53,7 @@ public void defaultAllocatorBenchmark() { } } - /** - * Benchmark for allocator with segment rounding policy. - */ + /** Benchmark for allocator with segment rounding policy. */ @Benchmark @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) @@ -71,7 +63,8 @@ public void segmentRoundingPolicyBenchmark() { final int segmentSize = 1024; RoundingPolicy policy = new SegmentRoundingPolicy(segmentSize); - try (RootAllocator allocator = new RootAllocator(AllocationListener.NOOP, bufferSize * numBuffers, policy)) { + try (RootAllocator allocator = + new RootAllocator(AllocationListener.NOOP, bufferSize * numBuffers, policy)) { ArrowBuf[] buffers = new ArrowBuf[numBuffers]; for (int i = 0; i < numBuffers; i++) { @@ -85,10 +78,8 @@ public void segmentRoundingPolicyBenchmark() { } public static void main(String[] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(AllocatorBenchmarks.class.getSimpleName()) - .forks(1) - .build(); + Options opt = + new OptionsBuilder().include(AllocatorBenchmarks.class.getSimpleName()).forks(1).build(); new Runner(opt).run(); } diff --git a/java/performance/src/test/java/org/apache/arrow/memory/ArrowBufBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/memory/ArrowBufBenchmarks.java index ef4da582829..4754a29b6ce 100644 --- a/java/performance/src/test/java/org/apache/arrow/memory/ArrowBufBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/memory/ArrowBufBenchmarks.java @@ -18,7 +18,6 @@ package org.apache.arrow.memory; import java.util.concurrent.TimeUnit; - import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Mode; @@ -32,9 +31,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link ArrowBuf}. - */ +/** Benchmarks for {@link ArrowBuf}. */ @State(Scope.Benchmark) public class ArrowBufBenchmarks { @@ -46,18 +43,14 @@ public class ArrowBufBenchmarks { private ArrowBuf buffer; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); buffer = allocator.buffer(BUFFER_CAPACITY); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown public void tearDown() { buffer.close(); @@ -72,10 +65,8 @@ public void setZero() { } public static void main(String[] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(ArrowBufBenchmarks.class.getSimpleName()) - .forks(1) - .build(); + Options opt = + new OptionsBuilder().include(ArrowBufBenchmarks.class.getSimpleName()).forks(1).build(); new Runner(opt).run(); } diff --git a/java/performance/src/test/java/org/apache/arrow/memory/util/ArrowBufPointerBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/memory/util/ArrowBufPointerBenchmarks.java index 8e2c9cc51a3..1a4eb3ea853 100644 --- a/java/performance/src/test/java/org/apache/arrow/memory/util/ArrowBufPointerBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/memory/util/ArrowBufPointerBenchmarks.java @@ -18,7 +18,6 @@ package org.apache.arrow.memory.util; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -35,9 +34,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link ArrowBufPointer}. - */ +/** Benchmarks for {@link ArrowBufPointer}. */ @State(Scope.Benchmark) public class ArrowBufPointerBenchmarks { @@ -55,9 +52,7 @@ public class ArrowBufPointerBenchmarks { private ArrowBufPointer pointer2; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); @@ -77,9 +72,7 @@ public void prepare() { pointer2 = new ArrowBufPointer(buffer2, 0, BUFFER_CAPACITY); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown public void tearDown() { buffer1.close(); @@ -95,7 +88,8 @@ public int compareBenchmark() { } public static void main(String[] args) throws RunnerException { - Options opt = new OptionsBuilder() + Options opt = + new OptionsBuilder() .include(ArrowBufPointerBenchmarks.class.getSimpleName()) .forks(1) .build(); @@ -103,5 +97,3 @@ public static void main(String[] args) throws RunnerException { new Runner(opt).run(); } } - - diff --git a/java/performance/src/test/java/org/apache/arrow/memory/util/ByteFunctionHelpersBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/memory/util/ByteFunctionHelpersBenchmarks.java index 4d0dfcb5da8..f797ea97674 100644 --- a/java/performance/src/test/java/org/apache/arrow/memory/util/ByteFunctionHelpersBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/memory/util/ByteFunctionHelpersBenchmarks.java @@ -18,7 +18,6 @@ package org.apache.arrow.memory.util; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -36,15 +35,14 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link ByteFunctionHelpers}. - */ +/** Benchmarks for {@link ByteFunctionHelpers}. */ public class ByteFunctionHelpersBenchmarks { private static final int ALLOCATOR_CAPACITY = 1024 * 1024; /** - * State object for the {@link ByteFunctionHelpersBenchmarks#arrowBufEquals(ArrowEqualState)} benchmark. + * State object for the {@link ByteFunctionHelpersBenchmarks#arrowBufEquals(ArrowEqualState)} + * benchmark. */ @State(Scope.Benchmark) public static class ArrowEqualState { @@ -81,12 +79,18 @@ public void tearDown() { @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public void arrowBufEquals(ArrowEqualState state) { - ByteFunctionHelpers.equal(state.buffer1, 0, ArrowEqualState.BUFFER_CAPACITY - 1, - state.buffer2, 0, ArrowEqualState.BUFFER_CAPACITY - 1); + ByteFunctionHelpers.equal( + state.buffer1, + 0, + ArrowEqualState.BUFFER_CAPACITY - 1, + state.buffer2, + 0, + ArrowEqualState.BUFFER_CAPACITY - 1); } /** - * State object for the {@link ByteFunctionHelpersBenchmarks#arrowBufArrayEquals(ArrowArrayEqualState)} benchmark. + * State object for the {@link + * ByteFunctionHelpersBenchmarks#arrowBufArrayEquals(ArrowArrayEqualState)} benchmark. */ @State(Scope.Benchmark) public static class ArrowArrayEqualState { @@ -123,15 +127,20 @@ public void tearDown() { @OutputTimeUnit(TimeUnit.NANOSECONDS) public int arrowBufArrayEquals(ArrowArrayEqualState state) { return ByteFunctionHelpers.compare( - state.buffer1, 0, ArrowArrayEqualState.BUFFER_CAPACITY, - state.buffer2, 0, ArrowArrayEqualState.BUFFER_CAPACITY); + state.buffer1, + 0, + ArrowArrayEqualState.BUFFER_CAPACITY, + state.buffer2, + 0, + ArrowArrayEqualState.BUFFER_CAPACITY); } public static void main(String[] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(ByteFunctionHelpersBenchmarks.class.getSimpleName()) - .forks(1) - .build(); + Options opt = + new OptionsBuilder() + .include(ByteFunctionHelpersBenchmarks.class.getSimpleName()) + .forks(1) + .build(); new Runner(opt).run(); } diff --git a/java/performance/src/test/java/org/apache/arrow/vector/BaseValueVectorBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/vector/BaseValueVectorBenchmarks.java index 5d6441cd5a7..500980e1d59 100644 --- a/java/performance/src/test/java/org/apache/arrow/vector/BaseValueVectorBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/BaseValueVectorBenchmarks.java @@ -18,7 +18,6 @@ package org.apache.arrow.vector; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.openjdk.jmh.annotations.Benchmark; @@ -34,9 +33,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link BaseValueVector}. - */ +/** Benchmarks for {@link BaseValueVector}. */ @State(Scope.Benchmark) public class BaseValueVectorBenchmarks { @@ -48,9 +45,7 @@ public class BaseValueVectorBenchmarks { private IntVector vector; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); @@ -58,9 +53,7 @@ public void prepare() { vector.allocateNew(VECTOR_LENGTH); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown public void tearDown() { vector.close(); @@ -69,6 +62,7 @@ public void tearDown() { /** * Test {@link BaseValueVector#computeCombinedBufferSize(int, int)}. + * * @return useless. To avoid DCE by JIT. */ @Benchmark @@ -82,14 +76,13 @@ public int testComputeCombinedBufferSize() { return totalSize; } - public static void main(String [] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(BaseValueVectorBenchmarks.class.getSimpleName()) - .forks(1) - .build(); + public static void main(String[] args) throws RunnerException { + Options opt = + new OptionsBuilder() + .include(BaseValueVectorBenchmarks.class.getSimpleName()) + .forks(1) + .build(); new Runner(opt).run(); } - - } diff --git a/java/performance/src/test/java/org/apache/arrow/vector/BitVectorHelperBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/vector/BitVectorHelperBenchmarks.java index 5f6e5ca28fb..7c86a83b974 100644 --- a/java/performance/src/test/java/org/apache/arrow/vector/BitVectorHelperBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/BitVectorHelperBenchmarks.java @@ -18,7 +18,6 @@ package org.apache.arrow.vector; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -37,14 +36,10 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link BitVectorHelper}. - */ +/** Benchmarks for {@link BitVectorHelper}. */ public class BitVectorHelperBenchmarks { - /** - * State object for general benchmarks. - */ + /** State object for general benchmarks. */ @State(Scope.Benchmark) public static class BenchmarkState { @@ -58,9 +53,7 @@ public static class BenchmarkState { private ArrowBuf oneBitValidityBuffer; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup(Level.Trial) public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); @@ -80,9 +73,7 @@ public void prepare() { BitVectorHelper.setBit(oneBitValidityBuffer, VALIDITY_BUFFER_CAPACITY / 2); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown(Level.Trial) public void tearDown() { validityBuffer.close(); @@ -95,7 +86,8 @@ public void tearDown() { @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public int getNullCountBenchmark(BenchmarkState state) { - return BitVectorHelper.getNullCount(state.validityBuffer, BenchmarkState.VALIDITY_BUFFER_CAPACITY); + return BitVectorHelper.getNullCount( + state.validityBuffer, BenchmarkState.VALIDITY_BUFFER_CAPACITY); } @Benchmark @@ -103,12 +95,10 @@ public int getNullCountBenchmark(BenchmarkState state) { @OutputTimeUnit(TimeUnit.NANOSECONDS) public boolean allBitsNullBenchmark(BenchmarkState state) { return BitVectorHelper.checkAllBitsEqualTo( - state.oneBitValidityBuffer, BenchmarkState.VALIDITY_BUFFER_CAPACITY, true); + state.oneBitValidityBuffer, BenchmarkState.VALIDITY_BUFFER_CAPACITY, true); } - /** - * State object for {@link #loadValidityBufferAllOne(NonNullableValidityBufferState)}.. - */ + /** State object for {@link #loadValidityBufferAllOne(NonNullableValidityBufferState)}.. */ @State(Scope.Benchmark) public static class NonNullableValidityBufferState { @@ -124,9 +114,7 @@ public static class NonNullableValidityBufferState { private ArrowFieldNode fieldNode; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup(Level.Trial) public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); @@ -144,9 +132,7 @@ public void tearDownInvoke() { loadResult.close(); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown(Level.Trial) public void tearDown() { validityBuffer.close(); @@ -155,19 +141,18 @@ public void tearDown() { } /** - * Benchmark for {@link BitVectorHelper#loadValidityBuffer(ArrowFieldNode, ArrowBuf, BufferAllocator)} - * when all elements are not null. + * Benchmark for {@link BitVectorHelper#loadValidityBuffer(ArrowFieldNode, ArrowBuf, + * BufferAllocator)} when all elements are not null. */ @Benchmark @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public void loadValidityBufferAllOne(NonNullableValidityBufferState state) { - state.loadResult = BitVectorHelper.loadValidityBuffer(state.fieldNode, state.validityBuffer, state.allocator); + state.loadResult = + BitVectorHelper.loadValidityBuffer(state.fieldNode, state.validityBuffer, state.allocator); } - /** - * State object for {@link #setValidityBitBenchmark(ClearBitStateState)}. - */ + /** State object for {@link #setValidityBitBenchmark(ClearBitStateState)}. */ @State(Scope.Benchmark) public static class ClearBitStateState { @@ -181,18 +166,14 @@ public static class ClearBitStateState { private int bitToSet = 0; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup(Level.Trial) public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); validityBuffer = allocator.buffer(VALIDITY_BUFFER_CAPACITY / 8); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown(Level.Trial) public void tearDown() { validityBuffer.close(); @@ -218,8 +199,9 @@ public void setValidityBitToZeroBenchmark(ClearBitStateState state) { } } - public static void main(String [] args) throws RunnerException { - Options opt = new OptionsBuilder() + public static void main(String[] args) throws RunnerException { + Options opt = + new OptionsBuilder() .include(BitVectorHelperBenchmarks.class.getSimpleName()) .forks(1) .build(); diff --git a/java/performance/src/test/java/org/apache/arrow/vector/DecimalVectorBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/vector/DecimalVectorBenchmarks.java index 72f5659907a..a9c1061800f 100644 --- a/java/performance/src/test/java/org/apache/arrow/vector/DecimalVectorBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/DecimalVectorBenchmarks.java @@ -19,7 +19,6 @@ import java.math.BigDecimal; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -36,9 +35,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link DecimalVector}. - */ +/** Benchmarks for {@link DecimalVector}. */ @State(Scope.Benchmark) public class DecimalVectorBenchmarks { @@ -54,9 +51,7 @@ public class DecimalVectorBenchmarks { byte[] fromByteArray; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); @@ -73,9 +68,7 @@ public void prepare() { fromBuf.getBytes(0, fromByteArray); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown public void tearDown() { fromBuf.close(); @@ -83,9 +76,7 @@ public void tearDown() { allocator.close(); } - /** - * Test writing on {@link DecimalVector} from arrow buf. - */ + /** Test writing on {@link DecimalVector} from arrow buf. */ @Benchmark @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) @@ -98,9 +89,7 @@ public void setBigEndianArrowBufBenchmark() { } } - /** - * Test writing on {@link DecimalVector} from byte array. - */ + /** Test writing on {@link DecimalVector} from byte array. */ @Benchmark @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) @@ -110,11 +99,12 @@ public void setBigEndianByteArrayBenchmark() { } } - public static void main(String [] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(DecimalVectorBenchmarks.class.getSimpleName()) - .forks(1) - .build(); + public static void main(String[] args) throws RunnerException { + Options opt = + new OptionsBuilder() + .include(DecimalVectorBenchmarks.class.getSimpleName()) + .forks(1) + .build(); new Runner(opt).run(); } diff --git a/java/performance/src/test/java/org/apache/arrow/vector/Float8Benchmarks.java b/java/performance/src/test/java/org/apache/arrow/vector/Float8Benchmarks.java index 874e0d9f82e..8eff3172481 100644 --- a/java/performance/src/test/java/org/apache/arrow/vector/Float8Benchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/Float8Benchmarks.java @@ -18,7 +18,6 @@ package org.apache.arrow.vector; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.BoundsChecking; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -35,9 +34,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link Float8Vector}. - */ +/** Benchmarks for {@link Float8Vector}. */ @State(Scope.Benchmark) public class Float8Benchmarks { @@ -51,9 +48,7 @@ public class Float8Benchmarks { private Float8Vector fromVector; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); @@ -73,9 +68,7 @@ public void prepare() { fromVector.setValueCount(VECTOR_LENGTH); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown public void tearDown() { vector.close(); @@ -84,10 +77,11 @@ public void tearDown() { } /** - * Test reading/writing on {@link Float8Vector}. - * The performance of this benchmark is influenced by the states of two flags: - * 1. The flag for boundary checking. For details, please see {@link BoundsChecking}. - * 2. The flag for null checking in get methods. For details, please see {@link NullCheckingForGet}. + * Test reading/writing on {@link Float8Vector}. The performance of this benchmark is influenced + * by the states of two flags: 1. The flag for boundary checking. For details, please see {@link + * BoundsChecking}. 2. The flag for null checking in get methods. For details, please see {@link + * NullCheckingForGet}. + * * @return useless. To avoid DCE by JIT. */ @Benchmark @@ -111,11 +105,9 @@ public void copyFromBenchmark() { } } - public static void main(String [] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(Float8Benchmarks.class.getSimpleName()) - .forks(1) - .build(); + public static void main(String[] args) throws RunnerException { + Options opt = + new OptionsBuilder().include(Float8Benchmarks.class.getSimpleName()).forks(1).build(); new Runner(opt).run(); } diff --git a/java/performance/src/test/java/org/apache/arrow/vector/FloatingPointBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/vector/FloatingPointBenchmarks.java index 079672e9f2a..83b6f475859 100644 --- a/java/performance/src/test/java/org/apache/arrow/vector/FloatingPointBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/FloatingPointBenchmarks.java @@ -18,7 +18,6 @@ package org.apache.arrow.vector; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.compare.ApproxEqualsVisitor; @@ -36,9 +35,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for floating point vectors. - */ +/** Benchmarks for floating point vectors. */ @State(Scope.Benchmark) public class FloatingPointBenchmarks { @@ -62,9 +59,7 @@ public class FloatingPointBenchmarks { private Range range; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); @@ -101,9 +96,7 @@ public void prepare() { range = new Range(0, 0, VECTOR_LENGTH); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown public void tearDown() { floatVector1.close(); @@ -122,8 +115,9 @@ public int approxEqualsBenchmark() { return (floatResult ? 1 : 0) + (doubleResult ? 1 : 0); } - public static void main(String [] args) throws RunnerException { - Options opt = new OptionsBuilder() + public static void main(String[] args) throws RunnerException { + Options opt = + new OptionsBuilder() .include(FloatingPointBenchmarks.class.getSimpleName()) .forks(1) .build(); @@ -131,4 +125,3 @@ public static void main(String [] args) throws RunnerException { new Runner(opt).run(); } } - diff --git a/java/performance/src/test/java/org/apache/arrow/vector/IntBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/vector/IntBenchmarks.java index 036768d445e..2db009eb46b 100644 --- a/java/performance/src/test/java/org/apache/arrow/vector/IntBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/IntBenchmarks.java @@ -18,7 +18,6 @@ package org.apache.arrow.vector; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.complex.impl.IntWriterImpl; @@ -36,9 +35,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link IntVector}. - */ +/** Benchmarks for {@link IntVector}. */ @State(Scope.Benchmark) public class IntBenchmarks { @@ -99,11 +96,9 @@ public void setWithWriter() { } } - public static void main(String [] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(IntBenchmarks.class.getSimpleName()) - .forks(1) - .build(); + public static void main(String[] args) throws RunnerException { + Options opt = + new OptionsBuilder().include(IntBenchmarks.class.getSimpleName()).forks(1).build(); new Runner(opt).run(); } diff --git a/java/performance/src/test/java/org/apache/arrow/vector/VarCharBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/vector/VarCharBenchmarks.java index 1ab4b7bc20d..b0d8359c60d 100644 --- a/java/performance/src/test/java/org/apache/arrow/vector/VarCharBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/VarCharBenchmarks.java @@ -18,7 +18,6 @@ package org.apache.arrow.vector; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.openjdk.jmh.annotations.Benchmark; @@ -34,9 +33,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link VarCharVector}. - */ +/** Benchmarks for {@link VarCharVector}. */ @State(Scope.Benchmark) public class VarCharBenchmarks { @@ -50,9 +47,7 @@ public class VarCharBenchmarks { private VarCharVector fromVector; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); @@ -72,9 +67,7 @@ public void prepare() { fromVector.setValueCount(VECTOR_LENGTH); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown public void tearDown() { vector.close(); @@ -91,11 +84,9 @@ public void copyFromBenchmark() { } } - public static void main(String [] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(VarCharBenchmarks.class.getSimpleName()) - .forks(1) - .build(); + public static void main(String[] args) throws RunnerException { + Options opt = + new OptionsBuilder().include(VarCharBenchmarks.class.getSimpleName()).forks(1).build(); new Runner(opt).run(); } diff --git a/java/performance/src/test/java/org/apache/arrow/vector/VariableWidthVectorBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/vector/VariableWidthVectorBenchmarks.java index 7eee981f133..e51f564f48d 100644 --- a/java/performance/src/test/java/org/apache/arrow/vector/VariableWidthVectorBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/VariableWidthVectorBenchmarks.java @@ -18,7 +18,6 @@ package org.apache.arrow.vector; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -36,9 +35,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link BaseVariableWidthVector}. - */ +/** Benchmarks for {@link BaseVariableWidthVector}. */ @State(Scope.Benchmark) public class VariableWidthVectorBenchmarks { @@ -55,9 +52,7 @@ public class VariableWidthVectorBenchmarks { private VarCharVector vector; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); @@ -67,9 +62,7 @@ public void prepare() { arrowBuff.setBytes(0, bytes, 0, bytes.length); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown public void tearDown() { arrowBuff.close(); @@ -79,6 +72,7 @@ public void tearDown() { /** * Test {@link BaseVariableWidthVector#getValueCapacity()}. + * * @return useless. To avoid DCE by JIT. */ @Benchmark @@ -118,9 +112,9 @@ public int setSafeFromNullableVarcharHolder() { return vector.getBufferSize(); } - - public static void main(String [] args) throws RunnerException { - Options opt = new OptionsBuilder() + public static void main(String[] args) throws RunnerException { + Options opt = + new OptionsBuilder() .include(VariableWidthVectorBenchmarks.class.getSimpleName()) .forks(1) .build(); diff --git a/java/performance/src/test/java/org/apache/arrow/vector/VectorLoaderBenchmark.java b/java/performance/src/test/java/org/apache/arrow/vector/VectorLoaderBenchmark.java index 416d126419e..4c9942d0563 100644 --- a/java/performance/src/test/java/org/apache/arrow/vector/VectorLoaderBenchmark.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/VectorLoaderBenchmark.java @@ -18,7 +18,6 @@ package org.apache.arrow.vector; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.ipc.message.ArrowRecordBatch; @@ -36,18 +35,14 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link VectorLoader}. - */ +/** Benchmarks for {@link VectorLoader}. */ public class VectorLoaderBenchmark { private static final int ALLOCATOR_CAPACITY = 1024 * 1024; private static final int VECTOR_COUNT = 10; - /** - * State for vector load benchmark. - */ + /** State for vector load benchmark. */ @State(Scope.Benchmark) public static class LoadState { @@ -61,9 +56,7 @@ public static class LoadState { private VectorLoader loader; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup(Level.Trial) public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); @@ -90,9 +83,7 @@ public void tearDownInvoke() { root.close(); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown(Level.Trial) public void tearDown() { allocator.close(); @@ -107,10 +98,8 @@ public void loadBenchmark(LoadState state) { } public static void main(String[] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(VectorLoaderBenchmark.class.getSimpleName()) - .forks(1) - .build(); + Options opt = + new OptionsBuilder().include(VectorLoaderBenchmark.class.getSimpleName()).forks(1).build(); new Runner(opt).run(); } diff --git a/java/performance/src/test/java/org/apache/arrow/vector/VectorUnloaderBenchmark.java b/java/performance/src/test/java/org/apache/arrow/vector/VectorUnloaderBenchmark.java index d1251724500..a369b8551b3 100644 --- a/java/performance/src/test/java/org/apache/arrow/vector/VectorUnloaderBenchmark.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/VectorUnloaderBenchmark.java @@ -18,7 +18,6 @@ package org.apache.arrow.vector; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.ipc.message.ArrowRecordBatch; @@ -36,9 +35,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link VectorUnloader}. - */ +/** Benchmarks for {@link VectorUnloader}. */ @State(Scope.Benchmark) public class VectorUnloaderBenchmark { @@ -48,15 +45,13 @@ public class VectorUnloaderBenchmark { private BufferAllocator allocator; - private VarCharVector [] vectors; + private VarCharVector[] vectors; private VectorUnloader unloader; private ArrowRecordBatch recordBatch; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup(Level.Trial) public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); @@ -83,9 +78,7 @@ public void tearDownInvoke() { } } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown(Level.Trial) public void tearDown() { allocator.close(); @@ -99,7 +92,8 @@ public void unloadBenchmark() { } public static void main(String[] args) throws RunnerException { - Options opt = new OptionsBuilder() + Options opt = + new OptionsBuilder() .include(VectorUnloaderBenchmark.class.getSimpleName()) .forks(1) .build(); diff --git a/java/performance/src/test/java/org/apache/arrow/vector/dictionary/DictionaryEncoderBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/vector/dictionary/DictionaryEncoderBenchmarks.java index 6dd887a3243..b6c7e9e5d8b 100644 --- a/java/performance/src/test/java/org/apache/arrow/vector/dictionary/DictionaryEncoderBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/dictionary/DictionaryEncoderBenchmarks.java @@ -22,7 +22,6 @@ import java.util.List; import java.util.Random; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.ValueVector; @@ -41,9 +40,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link DictionaryEncoder}. - */ +/** Benchmarks for {@link DictionaryEncoder}. */ @State(Scope.Benchmark) public class DictionaryEncoderBenchmarks { @@ -52,7 +49,6 @@ public class DictionaryEncoderBenchmarks { private static final int DATA_SIZE = 1000; private static final int KEY_SIZE = 100; - private static final int KEY_LENGTH = 10; private List keys = new ArrayList<>(); @@ -61,9 +57,7 @@ public class DictionaryEncoderBenchmarks { private VarCharVector dictionaryVector; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup public void prepare() { @@ -89,12 +83,9 @@ public void prepare() { byte[] value = keys.get(i).getBytes(StandardCharsets.UTF_8); dictionaryVector.setSafe(i, value, 0, value.length); } - } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown public void tearDown() { vector.close(); @@ -105,13 +96,15 @@ public void tearDown() { /** * Test encode for {@link DictionaryEncoder}. + * * @return useless. To avoid DCE by JIT. */ @Benchmark @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public int testEncode() { - Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); + Dictionary dictionary = + new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); final ValueVector encoded = DictionaryEncoder.encode(vector, dictionary); encoded.close(); return 0; @@ -137,10 +130,11 @@ private String generateUniqueKey(int length) { } public static void main(String[] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(DictionaryEncoderBenchmarks.class.getSimpleName()) - .forks(1) - .build(); + Options opt = + new OptionsBuilder() + .include(DictionaryEncoderBenchmarks.class.getSimpleName()) + .forks(1) + .build(); new Runner(opt).run(); } diff --git a/java/performance/src/test/java/org/apache/arrow/vector/ipc/WriteChannelBenchmark.java b/java/performance/src/test/java/org/apache/arrow/vector/ipc/WriteChannelBenchmark.java index 7a2537cbb88..457e981f414 100644 --- a/java/performance/src/test/java/org/apache/arrow/vector/ipc/WriteChannelBenchmark.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/ipc/WriteChannelBenchmark.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.nio.channels.Channels; import java.util.concurrent.TimeUnit; - import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Level; @@ -37,14 +36,10 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link WriteChannel}. - */ +/** Benchmarks for {@link WriteChannel}. */ public class WriteChannelBenchmark { - /** - * State object for align benchmark. - */ + /** State object for align benchmark. */ @State(Scope.Benchmark) public static class AlignState { @@ -77,10 +72,8 @@ public void alignBenchmark(AlignState state) throws IOException { } public static void main(String[] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(WriteChannelBenchmark.class.getSimpleName()) - .forks(1) - .build(); + Options opt = + new OptionsBuilder().include(WriteChannelBenchmark.class.getSimpleName()).forks(1).build(); new Runner(opt).run(); } diff --git a/java/performance/src/test/java/org/apache/arrow/vector/ipc/message/ArrowRecordBatchBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/vector/ipc/message/ArrowRecordBatchBenchmarks.java index c0882821e9c..b418534c839 100644 --- a/java/performance/src/test/java/org/apache/arrow/vector/ipc/message/ArrowRecordBatchBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/ipc/message/ArrowRecordBatchBenchmarks.java @@ -20,7 +20,6 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.VarCharVector; @@ -37,9 +36,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link ArrowRecordBatch}. - */ +/** Benchmarks for {@link ArrowRecordBatch}. */ @State(Scope.Benchmark) public class ArrowRecordBatchBenchmarks { @@ -55,9 +52,7 @@ public class ArrowRecordBatchBenchmarks { private List nodes; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); @@ -69,9 +64,7 @@ public void prepare() { nodes.add(new ArrowFieldNode(VECTOR_LENGTH, 0)); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown public void tearDown() { vector.close(); @@ -82,13 +75,15 @@ public void tearDown() { @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public long createAndGetLength() { - try (ArrowRecordBatch batch = new ArrowRecordBatch(VECTOR_LENGTH, nodes, vector.getFieldBuffers())) { + try (ArrowRecordBatch batch = + new ArrowRecordBatch(VECTOR_LENGTH, nodes, vector.getFieldBuffers())) { return batch.computeBodyLength(); } } - public static void main(String [] args) throws RunnerException { - Options opt = new OptionsBuilder() + public static void main(String[] args) throws RunnerException { + Options opt = + new OptionsBuilder() .include(ArrowRecordBatchBenchmarks.class.getSimpleName()) .forks(1) .build(); diff --git a/java/performance/src/test/java/org/apache/arrow/vector/util/TransferPairBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/vector/util/TransferPairBenchmarks.java index 5142f4bdb8d..c456b27fc7b 100644 --- a/java/performance/src/test/java/org/apache/arrow/vector/util/TransferPairBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/util/TransferPairBenchmarks.java @@ -19,7 +19,6 @@ import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.IntVector; @@ -37,9 +36,7 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; -/** - * Benchmarks for {@link TransferPair}. - */ +/** Benchmarks for {@link TransferPair}. */ @State(Scope.Benchmark) public class TransferPairBenchmarks { @@ -53,9 +50,7 @@ public class TransferPairBenchmarks { private VarCharVector varCharVector; - /** - * Setup benchmarks. - */ + /** Setup benchmarks. */ @Setup public void prepare() { allocator = new RootAllocator(ALLOCATOR_CAPACITY); @@ -78,9 +73,7 @@ public void prepare() { varCharVector.setValueCount(VECTOR_LENGTH); } - /** - * Tear down benchmarks. - */ + /** Tear down benchmarks. */ @TearDown public void tearDown() { intVector.close(); @@ -112,11 +105,9 @@ public int splitAndTransferVarcharVector() { return 0; } - public static void main(String [] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(TransferPairBenchmarks.class.getSimpleName()) - .forks(1) - .build(); + public static void main(String[] args) throws RunnerException { + Options opt = + new OptionsBuilder().include(TransferPairBenchmarks.class.getSimpleName()).forks(1).build(); new Runner(opt).run(); } diff --git a/java/pom.xml b/java/pom.xml index 3e595648ed0..8367e322350 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -27,37 +27,6 @@ Apache Arrow is open source, in-memory columnar data structures and low-overhead messaging https://arrow.apache.org/ - - ${project.build.directory}/generated-sources - 1.9.0 - 5.10.1 - 2.0.11 - 33.0.0-jre - 4.1.106.Final - 1.60.0 - 3.23.1 - 2.16.0 - 3.3.6 - 23.5.26 - 1.11.3 - - 2 - true - 9+181-r4173-1 - 2.24.0 - 3.11.0 - 5.5.0 - 5.2.0 - 3.42.0 - - - - scm:git:https://github.com/apache/arrow.git - scm:git:https://github.com/apache/arrow.git - https://github.com/apache/arrow - apache-arrow-2.0.0 - - Developer List @@ -81,328 +50,229 @@ - + + maven + bom + format + memory + vector + tools + adapter/jdbc + flight + performance + algorithm + adapter/avro + compression + - + + scm:git:https://github.com/apache/arrow.git + scm:git:https://github.com/apache/arrow.git + apache-arrow-2.0.0 + https://github.com/apache/arrow + Jira https://issues.apache.org/jira/browse/arrow - - - - - kr.motd.maven - os-maven-plugin - 1.7.0 - - + + ${project.build.directory}/generated-sources + 1.9.0 + 5.10.1 + 2.0.11 + 33.0.0-jre + 4.1.106.Final + 1.60.0 + 3.23.1 + 2.16.0 + 3.3.6 + 23.5.26 + 1.11.3 + + 2 + true + 9+181-r4173-1 + 2.24.0 + 3.11.0 + 5.5.0 + 5.2.0 + 3.42.0 + 2.42.0 + 2.30.0 + - - - org.apache.rat - apache-rat-plugin - - - rat-checks - validate - - check - - - - - false - - **/dependency-reduced-pom.xml - **/*.log - **/*.css - **/*.js - **/*.md - **/*.eps - **/*.json - **/*.seq - **/*.parquet - **/*.sql - **/arrow-git.properties - **/*.csv - **/*.csvh - **/*.csvh-test - **/*.tsv - **/*.txt - **/*.ssv - **/arrow-*.conf - **/.buildpath - **/*.proto - **/*.fmpp - **/target/** - **/*.tdd - **/*.project - **/TAGS - **/*.checkstyle - **/.classpath - **/.factorypath - **/.settings/** - .*/** - **/*.patch - **/*.pb.cc - **/*.pb.h - **/*.linux - **/client/build/** - **/*.tbl - **/*.iml - **/flight.properties - **/*.idea/** - - - + + + + + org.apache.arrow + arrow-bom + ${project.version} + pom + import + + + org.checkerframework + checker-qual + ${checker.framework.version} + + + com.google.flatbuffers + flatbuffers-java + ${dep.fbs.version} + + + com.google.code.findbugs + jsr305 + 3.0.2 + + + org.slf4j + slf4j-api + ${dep.slf4j.version} + + + javax.annotation + javax.annotation-api + 1.3.2 + + + org.assertj + assertj-core + 3.24.2 + test + + + org.immutables + value + 2.10.0 + provided + + + org.hamcrest + hamcrest + 2.2 + + + com.fasterxml.jackson + jackson-bom + ${dep.jackson-bom.version} + pom + import + + + com.google.guava + guava-bom + ${dep.guava-bom.version} + pom + import + + + io.netty + netty-bom + ${dep.netty-bom.version} + pom + import + + + io.grpc + grpc-bom + ${dep.grpc-bom.version} + pom + import + + + com.google.protobuf + protobuf-bom + ${dep.protobuf-bom.version} + pom + import + + + - - org.apache.maven.plugins - maven-jar-plugin - - - **/logging.properties - **/logback-test.xml - **/logback.out.xml - **/logback.xml - - - true - - true - true - - - org.apache.arrow - ${username} - https://arrow.apache.org/ - - - - - - - test-jar - - - true - - - - + + + + org.slf4j + jul-to-slf4j + ${dep.slf4j.version} + test + + + + org.slf4j + jcl-over-slf4j + ${dep.slf4j.version} + test + + + org.slf4j + log4j-over-slf4j + ${dep.slf4j.version} + test + - - org.apache.maven.plugins - maven-resources-plugin - - UTF-8 - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - 2048m - false - true - - - - maven-enforcer-plugin - - - validate_java_and_maven_version - verify - - enforce - - false - - - - [3.3.0,4) - - - - - - avoid_bad_dependencies - verify - - enforce - - - - - - commons-logging - javax.servlet:servlet-api - org.mortbay.jetty:servlet-api - org.mortbay.jetty:servlet-api-2.5 - log4j:log4j - - - - - - - - - pl.project13.maven - git-commit-id-plugin - 4.0.5 - - - for-jars - true - - revision - - - target/classes/arrow-git.properties - - - - for-source-tarball - - revision - - false - - ./arrow-git.properties - - - + + org.junit.jupiter + junit-jupiter-engine + ${dep.junit.jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-api + ${dep.junit.jupiter.version} + test + + + org.junit.vintage + junit-vintage-engine + ${dep.junit.jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-params + ${dep.junit.jupiter.version} + test + + + + junit + junit + 4.13.1 + test + + + org.mockito + mockito-junit-jupiter + 2.25.1 + test + + + ch.qos.logback + logback-classic + 1.3.14 + test + + + de.huxhorn.lilith + de.huxhorn.lilith.logback.appender.multiplex-classic + 0.9.44 + test + - - dd.MM.yyyy '@' HH:mm:ss z - false - false - true - false - - false - false - 7 - -dirty - true - - - + - - org.apache.maven.plugins - maven-checkstyle-plugin - 3.1.0 - - - com.puppycrawl.tools - checkstyle - 8.19 - - - org.slf4j - jcl-over-slf4j - ${dep.slf4j.version} - - - - - validate - validate - - check - - - - - **/module-info.java - dev/checkstyle/checkstyle.xml - dev/checkstyle/checkstyle.license - dev/checkstyle/suppressions.xml - true - UTF-8 - true - ${checkstyle.failOnViolation} - ${checkstyle.failOnViolation} - warning - xml - html - ${project.build.directory}/test/checkstyle-errors.xml - false - - - - org.apache.maven.plugins - maven-dependency-plugin - - - analyze - verify - - analyze-only - - - true - true - - - javax.annotation:javax.annotation-api:* - org.apache.hadoop:hadoop-client-api - org.checkerframework:checker-qual - - - - - - - org.cyclonedx - cyclonedx-maven-plugin - 2.7.10 - - - package - - makeBom - - - - - - org.apache.arrow.maven.plugins - module-info-compiler-maven-plugin - - - default-compile - - compile - - - - default-testCompile - - testCompile - - - - - - org.apache.maven.plugins - maven-project-info-reports-plugin - 3.0.0 - - - org.apache.maven.plugins - maven-site-plugin - 3.7.1 - - + + + + + @@ -453,20 +323,8 @@ 3.5.1 - maven-surefire-plugin - 3.2.3 - - - org.junit.jupiter - junit-jupiter-engine - ${dep.junit.jupiter.version} - - - org.apache.maven.surefire - surefire-junit-platform - 3.2.3 - - + maven-surefire-plugin + 3.2.3 true true @@ -481,6 +339,18 @@ 1048576 + + + org.junit.jupiter + junit-jupiter-engine + ${dep.junit.jupiter.version} + + + org.apache.maven.surefire + surefire-junit-platform + 3.2.3 + + maven-failsafe-plugin @@ -518,7 +388,7 @@ - + @@ -531,22 +401,20 @@ - + org.apache.maven.plugins - - maven-remote-resources-plugin - + maven-remote-resources-plugin [1.1,) process - + @@ -559,7 +427,7 @@ - + @@ -572,7 +440,7 @@ - + @@ -607,172 +475,275 @@ - - - - - - - org.apache.arrow - arrow-bom - ${project.version} - pom - import - - - org.checkerframework - checker-qual - ${checker.framework.version} - - - com.google.flatbuffers - flatbuffers-java - ${dep.fbs.version} - - - com.google.code.findbugs - jsr305 - 3.0.2 - - - org.slf4j - slf4j-api - ${dep.slf4j.version} - - - javax.annotation - javax.annotation-api - 1.3.2 - - - org.assertj - assertj-core - 3.24.2 - test - - - org.immutables - value - 2.10.0 - provided - - - org.hamcrest - hamcrest - 2.2 - - - com.fasterxml.jackson - jackson-bom - ${dep.jackson-bom.version} - pom - import - - - com.google.guava - guava-bom - ${dep.guava-bom.version} - pom - import - - - io.netty - netty-bom - ${dep.netty-bom.version} - pom - import - - - io.grpc - grpc-bom - ${dep.grpc-bom.version} - pom - import - - - com.google.protobuf - protobuf-bom - ${dep.protobuf-bom.version} - pom - import - - - - - - - - org.slf4j - jul-to-slf4j - ${dep.slf4j.version} - test - - - - org.slf4j - jcl-over-slf4j - ${dep.slf4j.version} - test - - - org.slf4j - log4j-over-slf4j - ${dep.slf4j.version} - test - + + + org.apache.rat + apache-rat-plugin + + false + + **/dependency-reduced-pom.xml + **/*.log + **/*.css + **/*.js + **/*.md + **/*.eps + **/*.json + **/*.seq + **/*.parquet + **/*.sql + **/arrow-git.properties + **/*.csv + **/*.csvh + **/*.csvh-test + **/*.tsv + **/*.txt + **/*.ssv + **/arrow-*.conf + **/.buildpath + **/*.proto + **/*.fmpp + **/target/** + **/*.tdd + **/*.project + **/TAGS + **/*.checkstyle + **/.classpath + **/.factorypath + **/.settings/** + .*/** + **/*.patch + **/*.pb.cc + **/*.pb.h + **/*.linux + **/client/build/** + **/*.tbl + **/*.iml + **/flight.properties + **/*.idea/** + + + + + rat-checks + + check + + validate + + + - - org.junit.jupiter - junit-jupiter-engine - ${dep.junit.jupiter.version} - test - - - org.junit.jupiter - junit-jupiter-api - ${dep.junit.jupiter.version} - test - - - org.junit.vintage - junit-vintage-engine - ${dep.junit.jupiter.version} - test - - - org.junit.jupiter - junit-jupiter-params - ${dep.junit.jupiter.version} - test - - - - junit - junit - 4.13.1 - test - - - org.mockito - mockito-junit-jupiter - 2.25.1 - test - - - ch.qos.logback - logback-classic - 1.3.14 - test - - - de.huxhorn.lilith - de.huxhorn.lilith.logback.appender.multiplex-classic - 0.9.44 - test - + + org.apache.maven.plugins + maven-jar-plugin + + + **/logging.properties + **/logback-test.xml + **/logback.out.xml + **/logback.xml + + + true + + true + true + + + org.apache.arrow + ${username} + https://arrow.apache.org/ + + + + + + + test-jar + + + true + + + + - + + org.apache.maven.plugins + maven-resources-plugin + + UTF-8 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + 2048m + false + true + + + + maven-enforcer-plugin + + + validate_java_and_maven_version + + enforce + + verify + false + + + + [3.3.0,4) + + + + + + avoid_bad_dependencies + + enforce + + verify + + + + + commons-logging + javax.servlet:servlet-api + org.mortbay.jetty:servlet-api + org.mortbay.jetty:servlet-api-2.5 + log4j:log4j + + + + + + + + + pl.project13.maven + git-commit-id-plugin + 4.0.5 + + + dd.MM.yyyy '@' HH:mm:ss z + false + false + true + false + + false + false + 7 + -dirty + true + + + + + for-jars + + revision + + true + + target/classes/arrow-git.properties + + + + for-source-tarball + + revision + + false + + ./arrow-git.properties + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + analyze + + analyze-only + + verify + + true + true + + + javax.annotation:javax.annotation-api:* + org.apache.hadoop:hadoop-client-api + org.checkerframework:checker-qual + + + + + + + org.cyclonedx + cyclonedx-maven-plugin + 2.7.10 + + + + makeBom + + package + + + + + org.apache.arrow.maven.plugins + module-info-compiler-maven-plugin + + + default-compile + + compile + + + + default-testCompile + + testCompile + + + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 3.0.0 + + + org.apache.maven.plugins + maven-site-plugin + 3.7.1 + + + + + + kr.motd.maven + os-maven-plugin + 1.7.0 + + + @@ -780,25 +751,29 @@ org.apache.maven.plugins maven-javadoc-plugin 3.6.3 + + + **/module-info.java + + - - + + + + javadoc - + + aggregate - false + aggregate + false - - - **/module-info.java - - org.apache.maven.plugins @@ -813,21 +788,6 @@ - - maven - bom - format - memory - vector - tools - adapter/jdbc - flight - performance - algorithm - adapter/avro - compression - - java-nodoclint @@ -871,9 +831,9 @@ error-prone-jdk8 1.8 @@ -996,11 +956,12 @@ 0.8.11 - false + report + false @@ -1042,35 +1003,31 @@ cdata-cmake - generate-resources exec + generate-resources cmake - - -S java + -S java -B java-jni -DARROW_JAVA_JNI_ENABLE_C=ON -DARROW_JAVA_JNI_ENABLE_DEFAULT=OFF -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release - -DCMAKE_INSTALL_PREFIX=${arrow.c.jni.dist.dir} - + -DCMAKE_INSTALL_PREFIX=${arrow.c.jni.dist.dir} ../ cdata-build - generate-resources exec + generate-resources cmake - - --build java-jni --target install --config Release - + --build java-jni --target install --config Release ../ @@ -1103,14 +1060,13 @@ jni-cpp-cmake - generate-resources exec + generate-resources cmake - - -S cpp + -S cpp -B cpp-jni -DARROW_BUILD_SHARED=OFF -DARROW_CSV=${ARROW_DATASET} @@ -1128,37 +1084,33 @@ -DARROW_USE_CCACHE=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=java-dist - -DCMAKE_UNITY_BUILD=ON - + -DCMAKE_UNITY_BUILD=ON ../ ${cpp.dependencies.builded} jni-cpp-build - generate-resources exec + generate-resources cmake - - --build cpp-jni --target install --config Release - + --build cpp-jni --target install --config Release ../ ${cpp.dependencies.builded} jni-cmake - generate-resources exec + generate-resources cmake - - -S java + -S java -B java-jni -DARROW_JAVA_JNI_ENABLE_C=${ARROW_JAVA_JNI_ENABLE_C} -DARROW_JAVA_JNI_ENABLE_DATASET=${ARROW_JAVA_JNI_ENABLE_DATASET} @@ -1170,22 +1122,19 @@ -DCMAKE_INSTALL_PREFIX=${arrow.dataset.jni.dist.dir} -DCMAKE_PREFIX_PATH=${project.basedir}/../java-dist/lib/${os.detected.arch}/cmake -DProtobuf_USE_STATIC_LIBS=ON - -DProtobuf_ROOT=${project.basedir}/../cpp-jni/protobuf_ep-install - + -DProtobuf_ROOT=${project.basedir}/../cpp-jni/protobuf_ep-install ../ jni-build - generate-resources exec + generate-resources cmake - - --build java-jni --target install --config Release - + --build java-jni --target install --config Release ../ @@ -1218,14 +1167,13 @@ jni-cpp-cmake - generate-resources exec + generate-resources cmake - - -S cpp + -S cpp -B cpp-jni -DARROW_BUILD_SHARED=OFF -DARROW_CSV=${ARROW_DATASET} @@ -1247,37 +1195,33 @@ -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=java-dist -DCMAKE_UNITY_BUILD=ON - -GNinja - + -GNinja ../ ${cpp.dependencies.builded} jni-cpp-build - generate-resources exec + generate-resources ninja - - install - + install ../cpp-jni ${cpp.dependencies.builded} jni-cmake - generate-resources exec + generate-resources cmake - - -S java + -S java -B java-jni -DARROW_JAVA_JNI_ENABLE_C=${ARROW_JAVA_JNI_ENABLE_C} -DARROW_JAVA_JNI_ENABLE_DATASET=${ARROW_JAVA_JNI_ENABLE_DATASET} @@ -1287,22 +1231,19 @@ -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${arrow.dataset.jni.dist.dir} - -DCMAKE_PREFIX_PATH=${project.basedir}/../java-dist/lib/${os.detected.arch}/cmake - + -DCMAKE_PREFIX_PATH=${project.basedir}/../java-dist/lib/${os.detected.arch}/cmake ../ jni-build - generate-resources exec + generate-resources cmake - - --build java-jni --target install --config Release - + --build java-jni --target install --config Release ../ @@ -1311,6 +1252,83 @@ + + + spotless-jdk11+ + + [11,] + + + + + com.diffplug.spotless + spotless-maven-plugin + ${spotless.version.jdk11} + + + + + + + + src/main/**/*.xml + src/test/**/*.xml + **/pom.xml + + + ${maven.multiModuleProjectDirectory}/spotless/asf-xml.license + (<configuration|<project) + + + + + + src/**/Preconditions.java + src/main/codegen/**/*.* + src/main/java/org/apache/arrow/flatbuf/*.java + + + true + 2 + + + true + 2 + + + + + ${maven.multiModuleProjectDirectory}/spotless/asf-java.license + (package|import) + + + + + true + 2 + + + true + 2 + + + false + + + + + + spotless-check + + check + + validate + + + + + + diff --git a/java/dev/checkstyle/checkstyle.license b/java/spotless/asf-java.license similarity index 99% rename from java/dev/checkstyle/checkstyle.license rename to java/spotless/asf-java.license index 3e7c6c26f55..29400e587da 100644 --- a/java/dev/checkstyle/checkstyle.license +++ b/java/spotless/asf-java.license @@ -14,3 +14,4 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + diff --git a/java/spotless/asf-xml.license b/java/spotless/asf-xml.license new file mode 100644 index 00000000000..a43b97bca8f --- /dev/null +++ b/java/spotless/asf-xml.license @@ -0,0 +1,11 @@ + + \ No newline at end of file diff --git a/java/tools/pom.xml b/java/tools/pom.xml index 0688fae1ab7..b13bf560e07 100644 --- a/java/tools/pom.xml +++ b/java/tools/pom.xml @@ -1,4 +1,4 @@ - + - 4.0.0 - - org.apache.arrow - arrow-java-root - 16.0.0-SNAPSHOT - - arrow-tools - Arrow Tools - Java applications for working with Arrow ValueVectors. + 4.0.0 + + org.apache.arrow + arrow-java-root + 16.0.0-SNAPSHOT + + arrow-tools + Arrow Tools + Java applications for working with Arrow ValueVectors. - - - org.apache.arrow - arrow-memory-core - - - org.apache.arrow - arrow-vector - ${arrow.vector.classifier} - - - org.apache.arrow - arrow-compression - - - org.immutables - value - - - com.google.guava - guava - test - - - commons-cli - commons-cli - 1.6.0 - - - ch.qos.logback - logback-classic - 1.3.14 - runtime - - - com.fasterxml.jackson.core - jackson-core - - - com.fasterxml.jackson.core - jackson-databind - - - org.slf4j - slf4j-api - - - org.apache.arrow - arrow-memory-netty - runtime - - - org.apache.arrow - arrow-vector - ${project.version} - tests - test-jar - test - - + + + org.apache.arrow + arrow-memory-core + + + org.apache.arrow + arrow-vector + ${arrow.vector.classifier} + + + org.apache.arrow + arrow-compression + + + org.immutables + value + + + com.google.guava + guava + test + + + commons-cli + commons-cli + 1.6.0 + + + ch.qos.logback + logback-classic + 1.3.14 + runtime + + + com.fasterxml.jackson.core + jackson-core + + + com.fasterxml.jackson.core + jackson-databind + + + org.slf4j + slf4j-api + + + org.apache.arrow + arrow-memory-netty + runtime + + + org.apache.arrow + arrow-vector + ${project.version} + tests + test-jar + test + + - - - - maven-assembly-plugin - 3.0.0 - - - jar-with-dependencies - - - - - make-assembly - package - - single - - - - - - + + + + maven-assembly-plugin + 3.0.0 + + + jar-with-dependencies + + + + + make-assembly + + single + + package + + + + + diff --git a/java/tools/src/main/java/org/apache/arrow/tools/EchoServer.java b/java/tools/src/main/java/org/apache/arrow/tools/EchoServer.java index 36ba24dbeec..0350d64e987 100644 --- a/java/tools/src/main/java/org/apache/arrow/tools/EchoServer.java +++ b/java/tools/src/main/java/org/apache/arrow/tools/EchoServer.java @@ -20,7 +20,6 @@ import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.util.Preconditions; @@ -30,26 +29,20 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * Simple server that echoes back data received. - */ +/** Simple server that echoes back data received. */ public class EchoServer { private static final Logger LOGGER = LoggerFactory.getLogger(EchoServer.class); private final ServerSocket serverSocket; private boolean closed = false; - /** - * Constructs a new instance that binds to the given port. - */ + /** Constructs a new instance that binds to the given port. */ public EchoServer(int port) throws IOException { LOGGER.debug("Starting echo server."); serverSocket = new ServerSocket(port); LOGGER.debug("Running echo server on port: " + port()); } - /** - * Main method to run the server, the first argument is an optional port number. - */ + /** Main method to run the server, the first argument is an optional port number. */ public static void main(String[] args) throws Exception { int port; if (args.length > 0) { @@ -64,9 +57,7 @@ public int port() { return serverSocket.getLocalPort(); } - /** - * Starts the main server event loop. - */ + /** Starts the main server event loop. */ public void run() throws IOException { try { Socket clientSocket = null; @@ -98,9 +89,7 @@ public void close() throws IOException { serverSocket.close(); } - /** - * Handler for each client connection to the server. - */ + /** Handler for each client connection to the server. */ public static class ClientConnection implements AutoCloseable { public final Socket socket; @@ -108,9 +97,7 @@ public ClientConnection(Socket socket) { this.socket = socket; } - /** - * Reads a record batch off the socket and writes it back out. - */ + /** Reads a record batch off the socket and writes it back out. */ public void run() throws IOException { // Read the entire input stream and write it back try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE)) { diff --git a/java/tools/src/main/java/org/apache/arrow/tools/FileRoundtrip.java b/java/tools/src/main/java/org/apache/arrow/tools/FileRoundtrip.java index 1201d0f7605..d67798da8b5 100644 --- a/java/tools/src/main/java/org/apache/arrow/tools/FileRoundtrip.java +++ b/java/tools/src/main/java/org/apache/arrow/tools/FileRoundtrip.java @@ -22,7 +22,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.VectorSchemaRoot; @@ -37,9 +36,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * Application that verifies data can be round-tripped through a file. - */ +/** Application that verifies data can be round-tripped through a file. */ public class FileRoundtrip { private static final Logger LOGGER = LoggerFactory.getLogger(FileRoundtrip.class); private final Options options; @@ -50,7 +47,6 @@ public class FileRoundtrip { this.options = new Options(); this.options.addOption("i", "in", true, "input file"); this.options.addOption("o", "out", true, "output file"); - } public static void main(String[] args) { @@ -80,9 +76,9 @@ int run(String[] args) { File outFile = validateFile("output", outFileName); try (BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - FileInputStream fileInputStream = new FileInputStream(inFile); - ArrowFileReader arrowReader = new ArrowFileReader(fileInputStream.getChannel(), - allocator)) { + FileInputStream fileInputStream = new FileInputStream(inFile); + ArrowFileReader arrowReader = + new ArrowFileReader(fileInputStream.getChannel(), allocator)) { VectorSchemaRoot root = arrowReader.getVectorSchemaRoot(); Schema schema = root.getSchema(); @@ -90,8 +86,8 @@ int run(String[] args) { LOGGER.debug("Found schema: " + schema); try (FileOutputStream fileOutputStream = new FileOutputStream(outFile); - ArrowFileWriter arrowWriter = new ArrowFileWriter(root, arrowReader, - fileOutputStream.getChannel())) { + ArrowFileWriter arrowWriter = + new ArrowFileWriter(root, arrowReader, fileOutputStream.getChannel())) { arrowWriter.start(); while (true) { if (!arrowReader.loadNextBatch()) { @@ -117,5 +113,4 @@ private int fatalError(String message, Throwable e) { LOGGER.error(message, e); return 1; } - } diff --git a/java/tools/src/main/java/org/apache/arrow/tools/FileToStream.java b/java/tools/src/main/java/org/apache/arrow/tools/FileToStream.java index bb7cedeb745..04508b8bbb2 100644 --- a/java/tools/src/main/java/org/apache/arrow/tools/FileToStream.java +++ b/java/tools/src/main/java/org/apache/arrow/tools/FileToStream.java @@ -22,7 +22,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.VectorSchemaRoot; @@ -30,15 +29,13 @@ import org.apache.arrow.vector.ipc.ArrowStreamWriter; /** - * Converts an Arrow file to an Arrow stream. The file should be specified as the - * first argument and the output is written to standard out. + * Converts an Arrow file to an Arrow stream. The file should be specified as the first argument and + * the output is written to standard out. */ public class FileToStream { private FileToStream() {} - /** - * Reads an Arrow file from in and writes it back to out. - */ + /** Reads an Arrow file from in and writes it back to out. */ public static void convert(FileInputStream in, OutputStream out) throws IOException { BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); try (ArrowFileReader reader = new ArrowFileReader(in.getChannel(), allocator)) { @@ -60,8 +57,8 @@ public static void convert(FileInputStream in, OutputStream out) throws IOExcept } /** - * Main method. The first arg is the file path. The second, optional argument, - * is an output file location (defaults to standard out). + * Main method. The first arg is the file path. The second, optional argument, is an output file + * location (defaults to standard out). */ public static void main(String[] args) throws IOException { if (args.length != 1 && args.length != 2) { @@ -70,8 +67,7 @@ public static void main(String[] args) throws IOException { } FileInputStream in = new FileInputStream(new File(args[0])); - OutputStream out = args.length == 1 ? - System.out : new FileOutputStream(new File(args[1])); + OutputStream out = args.length == 1 ? System.out : new FileOutputStream(new File(args[1])); convert(in, out); } diff --git a/java/tools/src/main/java/org/apache/arrow/tools/Integration.java b/java/tools/src/main/java/org/apache/arrow/tools/Integration.java index 1db3eeb6449..915693083de 100644 --- a/java/tools/src/main/java/org/apache/arrow/tools/Integration.java +++ b/java/tools/src/main/java/org/apache/arrow/tools/Integration.java @@ -25,7 +25,6 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; - import org.apache.arrow.compression.CommonsCompressionFactory; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -47,9 +46,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * Application for cross language integration testing. - */ +/** Application for cross language integration testing. */ public class Integration { private static final Logger LOGGER = LoggerFactory.getLogger(Integration.class); private final Options options; @@ -58,13 +55,11 @@ public class Integration { this.options = new Options(); this.options.addOption("a", "arrow", true, "arrow file"); this.options.addOption("j", "json", true, "json file"); - this.options.addOption("c", "command", true, "command to execute: " + Arrays.toString(Command - .values())); + this.options.addOption( + "c", "command", true, "command to execute: " + Arrays.toString(Command.values())); } - /** - * Main method. - */ + /** Main method. */ public static void main(String[] args) { try { new Integration().run(args); @@ -123,28 +118,29 @@ private Command toCommand(String commandName) { try { return Command.valueOf(commandName); } catch (IllegalArgumentException e) { - throw new IllegalArgumentException("Unknown command: " + commandName + " expected one of " + - Arrays.toString(Command.values())); + throw new IllegalArgumentException( + "Unknown command: " + + commandName + + " expected one of " + + Arrays.toString(Command.values())); } } - /** - * Commands (actions) the application can perform. - */ + /** Commands (actions) the application can perform. */ enum Command { ARROW_TO_JSON(true, false) { @Override public void execute(File arrowFile, File jsonFile) throws IOException { try (BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - FileInputStream fileInputStream = new FileInputStream(arrowFile); - ArrowFileReader arrowReader = new ArrowFileReader(fileInputStream.getChannel(), - allocator)) { + FileInputStream fileInputStream = new FileInputStream(arrowFile); + ArrowFileReader arrowReader = + new ArrowFileReader(fileInputStream.getChannel(), allocator)) { VectorSchemaRoot root = arrowReader.getVectorSchemaRoot(); Schema schema = root.getSchema(); LOGGER.debug("Input file size: " + arrowFile.length()); LOGGER.debug("Found schema: " + schema); - try (JsonFileWriter writer = new JsonFileWriter(jsonFile, JsonFileWriter.config() - .pretty(true))) { + try (JsonFileWriter writer = + new JsonFileWriter(jsonFile, JsonFileWriter.config().pretty(true))) { writer.start(schema, arrowReader); for (ArrowBlock rbBlock : arrowReader.getRecordBlocks()) { if (!arrowReader.loadRecordBatch(rbBlock)) { @@ -161,15 +157,15 @@ public void execute(File arrowFile, File jsonFile) throws IOException { @Override public void execute(File arrowFile, File jsonFile) throws IOException { try (BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - JsonFileReader reader = new JsonFileReader(jsonFile, allocator)) { + JsonFileReader reader = new JsonFileReader(jsonFile, allocator)) { Schema schema = reader.start(); LOGGER.debug("Input file size: " + jsonFile.length()); LOGGER.debug("Found schema: " + schema); try (FileOutputStream fileOutputStream = new FileOutputStream(arrowFile); - VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); - // TODO json dictionaries - ArrowFileWriter arrowWriter = new ArrowFileWriter(root, reader, fileOutputStream - .getChannel())) { + VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator); + // TODO json dictionaries + ArrowFileWriter arrowWriter = + new ArrowFileWriter(root, reader, fileOutputStream.getChannel())) { arrowWriter.start(); while (reader.read(root)) { arrowWriter.writeBatch(); @@ -184,10 +180,11 @@ public void execute(File arrowFile, File jsonFile) throws IOException { @Override public void execute(File arrowFile, File jsonFile) throws IOException { try (BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - JsonFileReader jsonReader = new JsonFileReader(jsonFile, allocator); - FileInputStream fileInputStream = new FileInputStream(arrowFile); - ArrowFileReader arrowReader = new ArrowFileReader(fileInputStream.getChannel(), - allocator, CommonsCompressionFactory.INSTANCE)) { + JsonFileReader jsonReader = new JsonFileReader(jsonFile, allocator); + FileInputStream fileInputStream = new FileInputStream(arrowFile); + ArrowFileReader arrowReader = + new ArrowFileReader( + fileInputStream.getChannel(), allocator, CommonsCompressionFactory.INSTANCE)) { Schema jsonSchema = jsonReader.start(); VectorSchemaRoot arrowRoot = arrowReader.getVectorSchemaRoot(); Schema arrowSchema = arrowRoot.getSchema(); @@ -221,9 +218,14 @@ public void execute(File arrowFile, File jsonFile) throws IOException { boolean hasMoreJSON = jsonRoot != null; boolean hasMoreArrow = iterator.hasNext(); if (hasMoreJSON || hasMoreArrow) { - throw new IllegalArgumentException("Unexpected RecordBatches. Total: " + totalBatches + - " J:" + hasMoreJSON + " " + - "A:" + hasMoreArrow); + throw new IllegalArgumentException( + "Unexpected RecordBatches. Total: " + + totalBatches + + " J:" + + hasMoreJSON + + " " + + "A:" + + hasMoreArrow); } } } @@ -238,7 +240,5 @@ public void execute(File arrowFile, File jsonFile) throws IOException { } public abstract void execute(File arrowFile, File jsonFile) throws IOException; - } - } diff --git a/java/tools/src/main/java/org/apache/arrow/tools/StreamToFile.java b/java/tools/src/main/java/org/apache/arrow/tools/StreamToFile.java index 6bd3c2fba2f..25806e95921 100644 --- a/java/tools/src/main/java/org/apache/arrow/tools/StreamToFile.java +++ b/java/tools/src/main/java/org/apache/arrow/tools/StreamToFile.java @@ -24,7 +24,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.channels.Channels; - import org.apache.arrow.compression.CommonsCompressionFactory; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -32,16 +31,13 @@ import org.apache.arrow.vector.ipc.ArrowFileWriter; import org.apache.arrow.vector.ipc.ArrowStreamReader; -/** - * Converts an Arrow stream to an Arrow file. - */ +/** Converts an Arrow stream to an Arrow file. */ public class StreamToFile { - /** - * Reads an Arrow stream from in and writes it to out. - */ + /** Reads an Arrow stream from in and writes it to out. */ public static void convert(InputStream in, OutputStream out) throws IOException { BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - try (ArrowStreamReader reader = new ArrowStreamReader(in, allocator, CommonsCompressionFactory.INSTANCE)) { + try (ArrowStreamReader reader = + new ArrowStreamReader(in, allocator, CommonsCompressionFactory.INSTANCE)) { VectorSchemaRoot root = reader.getVectorSchemaRoot(); // load the first batch before instantiating the writer so that we have any dictionaries. // Only writeBatches if we load the first one. @@ -60,9 +56,8 @@ public static void convert(InputStream in, OutputStream out) throws IOException } /** - * Main method. Defaults to reading from standard in and standard out. - * If there are two arguments the first is interpreted as the input file path, - * the second is the output file path. + * Main method. Defaults to reading from standard in and standard out. If there are two arguments + * the first is interpreted as the input file path, the second is the output file path. */ public static void main(String[] args) throws IOException { InputStream in = System.in; diff --git a/java/tools/src/test/java/org/apache/arrow/tools/ArrowFileTestFixtures.java b/java/tools/src/test/java/org/apache/arrow/tools/ArrowFileTestFixtures.java index 1bc7ead7b73..5cdb77ce70e 100644 --- a/java/tools/src/test/java/org/apache/arrow/tools/ArrowFileTestFixtures.java +++ b/java/tools/src/test/java/org/apache/arrow/tools/ArrowFileTestFixtures.java @@ -22,7 +22,6 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.VectorSchemaRoot; @@ -56,11 +55,11 @@ static void writeData(int count, NonNullableStructVector parent) { static void validateOutput(File testOutFile, BufferAllocator allocator) throws Exception { // read - try (BufferAllocator readerAllocator = allocator.newChildAllocator("reader", 0, Integer - .MAX_VALUE); - FileInputStream fileInputStream = new FileInputStream(testOutFile); - ArrowFileReader arrowReader = new ArrowFileReader(fileInputStream.getChannel(), - readerAllocator)) { + try (BufferAllocator readerAllocator = + allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); + FileInputStream fileInputStream = new FileInputStream(testOutFile); + ArrowFileReader arrowReader = + new ArrowFileReader(fileInputStream.getChannel(), readerAllocator)) { VectorSchemaRoot root = arrowReader.getVectorSchemaRoot(); for (ArrowBlock rbBlock : arrowReader.getRecordBlocks()) { if (!arrowReader.loadRecordBatch(rbBlock)) { @@ -82,19 +81,17 @@ static void validateContent(int count, VectorSchemaRoot root) { static void write(FieldVector parent, File file) throws FileNotFoundException, IOException { VectorSchemaRoot root = new VectorSchemaRoot(parent); try (FileOutputStream fileOutputStream = new FileOutputStream(file); - ArrowFileWriter arrowWriter = new ArrowFileWriter(root, null, fileOutputStream - .getChannel())) { + ArrowFileWriter arrowWriter = + new ArrowFileWriter(root, null, fileOutputStream.getChannel())) { arrowWriter.writeBatch(); } } - - static void writeInput(File testInFile, BufferAllocator allocator) throws - FileNotFoundException, IOException { + static void writeInput(File testInFile, BufferAllocator allocator) + throws FileNotFoundException, IOException { int count = ArrowFileTestFixtures.COUNT; - try ( - BufferAllocator vectorAllocator = allocator.newChildAllocator("original vectors", 0, - Integer.MAX_VALUE); + try (BufferAllocator vectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); NonNullableStructVector parent = NonNullableStructVector.empty("parent", vectorAllocator)) { writeData(count, parent); write(parent.getChild("root"), testInFile); diff --git a/java/tools/src/test/java/org/apache/arrow/tools/EchoServerTest.java b/java/tools/src/test/java/org/apache/arrow/tools/EchoServerTest.java index 9cf893ee5c2..1f7fb52923a 100644 --- a/java/tools/src/test/java/org/apache/arrow/tools/EchoServerTest.java +++ b/java/tools/src/test/java/org/apache/arrow/tools/EchoServerTest.java @@ -23,6 +23,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import com.google.common.collect.ImmutableList; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; @@ -30,7 +31,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.FieldVector; @@ -59,8 +59,6 @@ import org.junit.BeforeClass; import org.junit.Test; -import com.google.common.collect.ImmutableList; - public class EchoServerTest { private static EchoServer server; @@ -71,16 +69,17 @@ public class EchoServerTest { public static void startEchoServer() throws IOException { server = new EchoServer(0); serverPort = server.port(); - serverThread = new Thread() { - @Override - public void run() { - try { - server.run(); - } catch (IOException e) { - e.printStackTrace(); - } - } - }; + serverThread = + new Thread() { + @Override + public void run() { + try { + server.run(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }; serverThread.start(); } @@ -90,16 +89,13 @@ public static void stopEchoServer() throws IOException, InterruptedException { serverThread.join(); } - private void testEchoServer(int serverPort, - Field field, - TinyIntVector vector, - int batches) + private void testEchoServer(int serverPort, Field field, TinyIntVector vector, int batches) throws UnknownHostException, IOException { VectorSchemaRoot root = new VectorSchemaRoot(asList(field), asList((FieldVector) vector), 0); try (BufferAllocator alloc = new RootAllocator(Long.MAX_VALUE); - Socket socket = new Socket("localhost", serverPort); - ArrowStreamWriter writer = new ArrowStreamWriter(root, null, socket.getOutputStream()); - ArrowStreamReader reader = new ArrowStreamReader(socket.getInputStream(), alloc)) { + Socket socket = new Socket("localhost", serverPort); + ArrowStreamWriter writer = new ArrowStreamWriter(root, null, socket.getOutputStream()); + ArrowStreamReader reader = new ArrowStreamReader(socket.getInputStream(), alloc)) { writer.start(); for (int i = 0; i < batches; i++) { vector.allocateNew(16); @@ -115,8 +111,8 @@ private void testEchoServer(int serverPort, assertEquals(new Schema(asList(field)), reader.getVectorSchemaRoot().getSchema()); - TinyIntVector readVector = (TinyIntVector) reader.getVectorSchemaRoot() - .getFieldVectors().get(0); + TinyIntVector readVector = + (TinyIntVector) reader.getVectorSchemaRoot().getFieldVectors().get(0); for (int i = 0; i < batches; i++) { Assert.assertTrue(reader.loadNextBatch()); assertEquals(16, reader.getVectorSchemaRoot().getRowCount()); @@ -136,10 +132,11 @@ private void testEchoServer(int serverPort, public void basicTest() throws InterruptedException, IOException { BufferAllocator alloc = new RootAllocator(Long.MAX_VALUE); - Field field = new Field( - "testField", - new FieldType(true, new ArrowType.Int(8, true), null, null), - Collections.emptyList()); + Field field = + new Field( + "testField", + new FieldType(true, new ArrowType.Int(8, true), null, null), + Collections.emptyList()); TinyIntVector vector = new TinyIntVector("testField", FieldType.nullable(TINYINT.getType()), alloc); @@ -157,32 +154,32 @@ public void basicTest() throws InterruptedException, IOException { public void testFlatDictionary() throws IOException { DictionaryEncoding writeEncoding = new DictionaryEncoding(1L, false, null); try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - IntVector writeVector = - new IntVector( - "varchar", - new FieldType(true, MinorType.INT.getType(), writeEncoding, null), - allocator); - VarCharVector writeDictionaryVector = - new VarCharVector( - "dict", - FieldType.nullable(VARCHAR.getType()), - allocator)) { + IntVector writeVector = + new IntVector( + "varchar", + new FieldType(true, MinorType.INT.getType(), writeEncoding, null), + allocator); + VarCharVector writeDictionaryVector = + new VarCharVector("dict", FieldType.nullable(VARCHAR.getType()), allocator)) { ValueVectorDataPopulator.setVector(writeVector, 0, 1, null, 2, 1, 2); - ValueVectorDataPopulator.setVector(writeDictionaryVector, "foo".getBytes(StandardCharsets.UTF_8), - "bar".getBytes(StandardCharsets.UTF_8), "baz".getBytes(StandardCharsets.UTF_8)); + ValueVectorDataPopulator.setVector( + writeDictionaryVector, + "foo".getBytes(StandardCharsets.UTF_8), + "bar".getBytes(StandardCharsets.UTF_8), + "baz".getBytes(StandardCharsets.UTF_8)); List fields = ImmutableList.of(writeVector.getField()); List vectors = ImmutableList.of((FieldVector) writeVector); VectorSchemaRoot root = new VectorSchemaRoot(fields, vectors, 6); - DictionaryProvider writeProvider = new MapDictionaryProvider( - new Dictionary(writeDictionaryVector, writeEncoding)); + DictionaryProvider writeProvider = + new MapDictionaryProvider(new Dictionary(writeDictionaryVector, writeEncoding)); try (Socket socket = new Socket("localhost", serverPort); - ArrowStreamWriter writer = new ArrowStreamWriter(root, writeProvider, socket - .getOutputStream()); - ArrowStreamReader reader = new ArrowStreamReader(socket.getInputStream(), allocator)) { + ArrowStreamWriter writer = + new ArrowStreamWriter(root, writeProvider, socket.getOutputStream()); + ArrowStreamReader reader = new ArrowStreamReader(socket.getInputStream(), allocator)) { writer.start(); writer.writeBatch(); writer.end(); @@ -221,9 +218,9 @@ public void testFlatDictionary() throws IOException { public void testNestedDictionary() throws IOException { DictionaryEncoding writeEncoding = new DictionaryEncoding(2L, false, null); try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - VarCharVector writeDictionaryVector = - new VarCharVector("dictionary", FieldType.nullable(VARCHAR.getType()), allocator); - ListVector writeVector = ListVector.empty("list", allocator)) { + VarCharVector writeDictionaryVector = + new VarCharVector("dictionary", FieldType.nullable(VARCHAR.getType()), allocator); + ListVector writeVector = ListVector.empty("list", allocator)) { // data being written: // [['foo', 'bar'], ['foo'], ['bar']] -> [[0, 1], [0], [1]] @@ -252,13 +249,13 @@ public void testNestedDictionary() throws IOException { List vectors = ImmutableList.of((FieldVector) writeVector); VectorSchemaRoot root = new VectorSchemaRoot(fields, vectors, 3); - DictionaryProvider writeProvider = new MapDictionaryProvider( - new Dictionary(writeDictionaryVector, writeEncoding)); + DictionaryProvider writeProvider = + new MapDictionaryProvider(new Dictionary(writeDictionaryVector, writeEncoding)); try (Socket socket = new Socket("localhost", serverPort); - ArrowStreamWriter writer = new ArrowStreamWriter(root, writeProvider, socket - .getOutputStream()); - ArrowStreamReader reader = new ArrowStreamReader(socket.getInputStream(), allocator)) { + ArrowStreamWriter writer = + new ArrowStreamWriter(root, writeProvider, socket.getOutputStream()); + ArrowStreamReader reader = new ArrowStreamReader(socket.getInputStream(), allocator)) { writer.start(); writer.writeBatch(); writer.end(); @@ -271,8 +268,8 @@ public void testNestedDictionary() throws IOException { Assert.assertNotNull(readVector); Assert.assertNull(readVector.getField().getDictionary()); - DictionaryEncoding readEncoding = readVector.getField().getChildren().get(0) - .getDictionary(); + DictionaryEncoding readEncoding = + readVector.getField().getChildren().get(0).getDictionary(); Assert.assertNotNull(readEncoding); Assert.assertEquals(2L, readEncoding.getId()); diff --git a/java/tools/src/test/java/org/apache/arrow/tools/TestFileRoundtrip.java b/java/tools/src/test/java/org/apache/arrow/tools/TestFileRoundtrip.java index a5d6c9658fd..45b7370e554 100644 --- a/java/tools/src/test/java/org/apache/arrow/tools/TestFileRoundtrip.java +++ b/java/tools/src/test/java/org/apache/arrow/tools/TestFileRoundtrip.java @@ -22,7 +22,6 @@ import static org.junit.Assert.assertEquals; import java.io.File; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.junit.After; @@ -33,8 +32,7 @@ public class TestFileRoundtrip { - @Rule - public TemporaryFolder testFolder = new TemporaryFolder(); + @Rule public TemporaryFolder testFolder = new TemporaryFolder(); private BufferAllocator allocator; @@ -61,5 +59,4 @@ public void test() throws Exception { validateOutput(testOutFile, allocator); } - } diff --git a/java/tools/src/test/java/org/apache/arrow/tools/TestIntegration.java b/java/tools/src/test/java/org/apache/arrow/tools/TestIntegration.java index 1232c6c1d86..d3ff963b976 100644 --- a/java/tools/src/test/java/org/apache/arrow/tools/TestIntegration.java +++ b/java/tools/src/test/java/org/apache/arrow/tools/TestIntegration.java @@ -25,13 +25,16 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter.NopIndenter; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.StringReader; import java.util.Map; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.tools.Integration.Command; @@ -48,15 +51,9 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; -import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; -import com.fasterxml.jackson.core.util.DefaultPrettyPrinter.NopIndenter; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; - public class TestIntegration { - @Rule - public TemporaryFolder testFolder = new TemporaryFolder(); + @Rule public TemporaryFolder testFolder = new TemporaryFolder(); private BufferAllocator allocator; private ObjectMapper om = new ObjectMapper(); @@ -69,11 +66,10 @@ public class TestIntegration { om.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS); } - static void writeInputFloat(File testInFile, BufferAllocator allocator, double... f) throws - FileNotFoundException, IOException { - try ( - BufferAllocator vectorAllocator = allocator.newChildAllocator("original vectors", 0, - Integer.MAX_VALUE); + static void writeInputFloat(File testInFile, BufferAllocator allocator, double... f) + throws FileNotFoundException, IOException { + try (BufferAllocator vectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); NonNullableStructVector parent = NonNullableStructVector.empty("parent", vectorAllocator)) { ComplexWriter writer = new ComplexWriterImpl("root", parent); StructWriter rootWriter = writer.rootAsStruct(); @@ -87,12 +83,11 @@ static void writeInputFloat(File testInFile, BufferAllocator allocator, double.. } } - static void writeInput2(File testInFile, BufferAllocator allocator) throws - FileNotFoundException, IOException { + static void writeInput2(File testInFile, BufferAllocator allocator) + throws FileNotFoundException, IOException { int count = ArrowFileTestFixtures.COUNT; - try ( - BufferAllocator vectorAllocator = allocator.newChildAllocator("original vectors", 0, - Integer.MAX_VALUE); + try (BufferAllocator vectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); NonNullableStructVector parent = NonNullableStructVector.empty("parent", vectorAllocator)) { writeData(count, parent); ComplexWriter writer = new ComplexWriterImpl("root", parent); @@ -132,27 +127,47 @@ public void testValid() throws Exception { Integration integration = new Integration(); // convert it to json - String[] args1 = {"-arrow", testInFile.getAbsolutePath(), "-json", testJSONFile - .getAbsolutePath(), "-command", Command.ARROW_TO_JSON.name()}; + String[] args1 = { + "-arrow", + testInFile.getAbsolutePath(), + "-json", + testJSONFile.getAbsolutePath(), + "-command", + Command.ARROW_TO_JSON.name() + }; integration.run(args1); // convert back to arrow - String[] args2 = {"-arrow", testOutFile.getAbsolutePath(), "-json", testJSONFile - .getAbsolutePath(), "-command", Command.JSON_TO_ARROW.name()}; + String[] args2 = { + "-arrow", + testOutFile.getAbsolutePath(), + "-json", + testJSONFile.getAbsolutePath(), + "-command", + Command.JSON_TO_ARROW.name() + }; integration.run(args2); // check it is the same validateOutput(testOutFile, allocator); // validate arrow against json - String[] args3 = {"-arrow", testInFile.getAbsolutePath(), "-json", testJSONFile - .getAbsolutePath(), "-command", Command.VALIDATE.name()}; + String[] args3 = { + "-arrow", + testInFile.getAbsolutePath(), + "-json", + testJSONFile.getAbsolutePath(), + "-command", + Command.VALIDATE.name() + }; integration.run(args3); } @Test public void testJSONRoundTripWithVariableWidth() throws Exception { - File testJSONFile = new File("../../docs/source/format/integration_json_examples/simple.json").getCanonicalFile(); + File testJSONFile = + new File("../../docs/source/format/integration_json_examples/simple.json") + .getCanonicalFile(); if (!testJSONFile.exists()) { testJSONFile = new File("../docs/source/format/integration_json_examples/simple.json"); } @@ -164,13 +179,25 @@ public void testJSONRoundTripWithVariableWidth() throws Exception { Integration integration = new Integration(); // convert to arrow - String[] args1 = {"-arrow", testOutFile.getAbsolutePath(), "-json", testJSONFile - .getAbsolutePath(), "-command", Command.JSON_TO_ARROW.name()}; + String[] args1 = { + "-arrow", + testOutFile.getAbsolutePath(), + "-json", + testJSONFile.getAbsolutePath(), + "-command", + Command.JSON_TO_ARROW.name() + }; integration.run(args1); // convert back to json - String[] args2 = {"-arrow", testOutFile.getAbsolutePath(), "-json", testRoundTripJSONFile - .getAbsolutePath(), "-command", Command.ARROW_TO_JSON.name()}; + String[] args2 = { + "-arrow", + testOutFile.getAbsolutePath(), + "-json", + testRoundTripJSONFile.getAbsolutePath(), + "-command", + Command.ARROW_TO_JSON.name() + }; integration.run(args2); BufferedReader orig = readNormalized(testJSONFile); @@ -186,7 +213,9 @@ public void testJSONRoundTripWithVariableWidth() throws Exception { @Test public void testJSONRoundTripWithStruct() throws Exception { - File testJSONFile = new File("../../docs/source/format/integration_json_examples/struct.json").getCanonicalFile(); + File testJSONFile = + new File("../../docs/source/format/integration_json_examples/struct.json") + .getCanonicalFile(); if (!testJSONFile.exists()) { testJSONFile = new File("../docs/source/format/integration_json_examples/struct.json"); } @@ -198,13 +227,25 @@ public void testJSONRoundTripWithStruct() throws Exception { Integration integration = new Integration(); // convert to arrow - String[] args1 = {"-arrow", testOutFile.getAbsolutePath(), "-json", testJSONFile - .getAbsolutePath(), "-command", Command.JSON_TO_ARROW.name()}; + String[] args1 = { + "-arrow", + testOutFile.getAbsolutePath(), + "-json", + testJSONFile.getAbsolutePath(), + "-command", + Command.JSON_TO_ARROW.name() + }; integration.run(args1); // convert back to json - String[] args2 = {"-arrow", testOutFile.getAbsolutePath(), "-json", testRoundTripJSONFile - .getAbsolutePath(), "-command", Command.ARROW_TO_JSON.name()}; + String[] args2 = { + "-arrow", + testOutFile.getAbsolutePath(), + "-json", + testRoundTripJSONFile.getAbsolutePath(), + "-command", + Command.ARROW_TO_JSON.name() + }; integration.run(args2); BufferedReader orig = readNormalized(testJSONFile); @@ -224,9 +265,7 @@ private BufferedReader readNormalized(File f) throws IOException { return new BufferedReader(new StringReader(normalized)); } - /** - * The test should not be sensitive to small variations in float representation. - */ + /** The test should not be sensitive to small variations in float representation. */ @Test public void testFloat() throws Exception { File testValidInFile = testFolder.newFile("testValidFloatIn.arrow"); @@ -242,13 +281,25 @@ public void testFloat() throws Exception { Integration integration = new Integration(); // convert the "valid" file to json - String[] args1 = {"-arrow", testValidInFile.getAbsolutePath(), "-json", testJSONFile - .getAbsolutePath(), "-command", Command.ARROW_TO_JSON.name()}; + String[] args1 = { + "-arrow", + testValidInFile.getAbsolutePath(), + "-json", + testJSONFile.getAbsolutePath(), + "-command", + Command.ARROW_TO_JSON.name() + }; integration.run(args1); // compare the "invalid" file to the "valid" json - String[] args3 = {"-arrow", testInvalidInFile.getAbsolutePath(), "-json", testJSONFile - .getAbsolutePath(), "-command", Command.VALIDATE.name()}; + String[] args3 = { + "-arrow", + testInvalidInFile.getAbsolutePath(), + "-json", + testJSONFile.getAbsolutePath(), + "-command", + Command.VALIDATE.name() + }; // this should fail integration.run(args3); } @@ -268,13 +319,25 @@ public void testInvalid() throws Exception { Integration integration = new Integration(); // convert the "valid" file to json - String[] args1 = {"-arrow", testValidInFile.getAbsolutePath(), "-json", testJSONFile - .getAbsolutePath(), "-command", Command.ARROW_TO_JSON.name()}; + String[] args1 = { + "-arrow", + testValidInFile.getAbsolutePath(), + "-json", + testJSONFile.getAbsolutePath(), + "-command", + Command.ARROW_TO_JSON.name() + }; integration.run(args1); // compare the "invalid" file to the "valid" json - String[] args3 = {"-arrow", testInvalidInFile.getAbsolutePath(), "-json", testJSONFile - .getAbsolutePath(), "-command", Command.VALIDATE.name()}; + String[] args3 = { + "-arrow", + testInvalidInFile.getAbsolutePath(), + "-json", + testJSONFile.getAbsolutePath(), + "-command", + Command.VALIDATE.name() + }; // this should fail try { integration.run(args3); @@ -283,6 +346,5 @@ public void testInvalid() throws Exception { assertTrue(e.getMessage(), e.getMessage().contains("Different values in column")); assertTrue(e.getMessage(), e.getMessage().contains("999")); } - } } diff --git a/java/tools/src/test/resources/logback.xml b/java/tools/src/test/resources/logback.xml index ff848da2a8b..c3281b845c9 100644 --- a/java/tools/src/test/resources/logback.xml +++ b/java/tools/src/test/resources/logback.xml @@ -1,4 +1,4 @@ - + - diff --git a/java/vector/pom.xml b/java/vector/pom.xml index dc453963b62..596c3bcb682 100644 --- a/java/vector/pom.xml +++ b/java/vector/pom.xml @@ -1,4 +1,4 @@ - + ${basedir}/src/main/codegen - codegen + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + org.apache.drill.tools + drill-fmpp-maven-plugin + [1.0,) + + generate + + + + + false + true + + + + + + + + + @@ -122,10 +155,10 @@ run-unsafe - test test + test org.apache.arrow:arrow-memory-netty @@ -138,12 +171,13 @@ maven-resources-plugin - + + copy-fmpp-resources - initialize copy-resources + initialize ${project.build.directory}/codegen @@ -156,17 +190,18 @@ - + + org.apache.drill.tools drill-fmpp-maven-plugin 1.5.0 generate-fmpp - generate-sources generate + generate-sources src/main/codegen/config.fmpp ${project.build.directory}/generated-sources @@ -183,14 +218,14 @@ Using a newer version up to at least 3.5.1 will cause issues in the arrow-tools tests looking up FlatBuffer dependencies. - --> + --> 3.2.4 - package shade + package @@ -213,44 +248,9 @@ - - - - - org.eclipse.m2e - lifecycle-mapping - 1.0.0 - - - - - - org.apache.drill.tools - drill-fmpp-maven-plugin - [1.0,) - - generate - - - - - false - true - - - - - - - - - - - sourceVector index: " + (start + i) + - " toVector index: " + i, expected, actual); + assertEquals( + "different data values not expected --> sourceVector index: " + + (start + i) + + " toVector index: " + + i, + expected, + actual); } } } @@ -167,8 +171,13 @@ public void testSplitAndTransfer1() throws Exception { for (int i = 0; i < length; i++) { int actual = toVector.get(i); int expected = sourceVector.get(start + i); - assertEquals("different data values not expected --> sourceVector index: " + (start + i) + - " toVector index: " + i, expected, actual); + assertEquals( + "different data values not expected --> sourceVector index: " + + (start + i) + + " toVector index: " + + i, + expected, + actual); } } } @@ -206,7 +215,9 @@ public void testSplitAndTransfer2() throws Exception { try (final BitVector toVector = new BitVector("toVector", allocator)) { final TransferPair transferPair = sourceVector.makeTransferPair(toVector); - final int[][] transferLengths = {{5, 22}, {5, 24}, {5, 25}, {5, 27}, {0, 31}, {5, 7}, {2, 3}}; + final int[][] transferLengths = { + {5, 22}, {5, 24}, {5, 25}, {5, 27}, {0, 31}, {5, 7}, {2, 3} + }; for (final int[] transferLength : transferLengths) { final int start = transferLength[0]; @@ -218,8 +229,13 @@ public void testSplitAndTransfer2() throws Exception { for (int i = 0; i < length; i++) { int actual = toVector.get(i); int expected = sourceVector.get(start + i); - assertEquals("different data values not expected --> sourceVector index: " + (start + i) + - " toVector index: " + i, expected, actual); + assertEquals( + "different data values not expected --> sourceVector index: " + + (start + i) + + " toVector index: " + + i, + expected, + actual); } } } @@ -295,8 +311,10 @@ public void testReallocAfterVectorTransfer1() { for (int i = 0; i < toVector.getValueCapacity(); i++) { if (i <= valueCapacity * 4) { - if (((i & 1) == 1) || (i == valueCapacity) || - (i == valueCapacity * 2) || (i == valueCapacity * 4)) { + if (((i & 1) == 1) + || (i == valueCapacity) + || (i == valueCapacity * 2) + || (i == valueCapacity * 4)) { assertEquals("unexpected cleared bit at index: " + i, 1, toVector.get(i)); } else { assertTrue("unexpected set bit at index: " + i, toVector.isNull(i)); @@ -379,8 +397,10 @@ public void testReallocAfterVectorTransfer2() { for (int i = 0; i < toVector.getValueCapacity(); i++) { if (i <= valueCapacity * 4) { - if (((i & 1) == 1) || (i == valueCapacity) || - (i == valueCapacity * 2) || (i == valueCapacity * 4)) { + if (((i & 1) == 1) + || (i == valueCapacity) + || (i == valueCapacity * 2) + || (i == valueCapacity * 4)) { assertFalse("unexpected cleared bit at index: " + i, toVector.isNull(i)); } else { assertTrue("unexpected set bit at index: " + i, toVector.isNull(i)); @@ -547,7 +567,8 @@ public void testGetTransferPairWithField() { final BitVector fromVector = new BitVector(EMPTY_SCHEMA_PATH, allocator); final TransferPair transferPair = fromVector.getTransferPair(fromVector.getField(), allocator); final BitVector toVector = (BitVector) transferPair.getTo(); - // Field inside a new vector created by reusing a field should be the same in memory as the original field. + // Field inside a new vector created by reusing a field should be the same in memory as the + // original field. assertSame(fromVector.getField(), toVector.getField()); } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestBitVectorHelper.java b/java/vector/src/test/java/org/apache/arrow/vector/TestBitVectorHelper.java index 1da4a4c4914..8518cd3bd54 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestBitVectorHelper.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestBitVectorHelper.java @@ -114,7 +114,7 @@ public void testAllBitsNull() { public void testAllBitsSet() { final int bufferLength = 32 * 1024; try (RootAllocator allocator = new RootAllocator(bufferLength); - ArrowBuf validityBuffer = allocator.buffer(bufferLength)) { + ArrowBuf validityBuffer = allocator.buffer(bufferLength)) { MemoryUtil.UNSAFE.setMemory(validityBuffer.memoryAddress(), bufferLength, (byte) -1); int bitLength = 1024; @@ -155,8 +155,8 @@ public void testAllBitsSet() { public void testConcatBits() { try (RootAllocator allocator = new RootAllocator(1024 * 1024)) { try (ArrowBuf buf1 = allocator.buffer(1024); - ArrowBuf buf2 = allocator.buffer(1024); - ArrowBuf output = allocator.buffer(1024)) { + ArrowBuf buf2 = allocator.buffer(1024); + ArrowBuf output = allocator.buffer(1024)) { buf1.setZero(0, buf1.capacity()); buf2.setZero(0, buf2.capacity()); @@ -191,7 +191,7 @@ public void testConcatBits() { public void testConcatBitsInPlace() { try (RootAllocator allocator = new RootAllocator(1024 * 1024)) { try (ArrowBuf buf1 = allocator.buffer(1024); - ArrowBuf buf2 = allocator.buffer(1024)) { + ArrowBuf buf2 = allocator.buffer(1024)) { buf1.setZero(0, buf1.capacity()); buf2.setZero(0, buf2.capacity()); @@ -247,14 +247,14 @@ public void testLoadValidityBuffer() { // null fieldNode = new ArrowFieldNode(1024, 1024); try (ArrowBuf src = allocator.buffer(128); - ArrowBuf dst = BitVectorHelper.loadValidityBuffer(fieldNode, src, allocator)) { + ArrowBuf dst = BitVectorHelper.loadValidityBuffer(fieldNode, src, allocator)) { assertEquals(128, allocator.getAllocatedMemory()); } // ... similarly if all values are not null fieldNode = new ArrowFieldNode(1024, 0); try (ArrowBuf src = allocator.buffer(128); - ArrowBuf dst = BitVectorHelper.loadValidityBuffer(fieldNode, src, allocator)) { + ArrowBuf dst = BitVectorHelper.loadValidityBuffer(fieldNode, src, allocator)) { assertEquals(128, allocator.getAllocatedMemory()); } @@ -277,7 +277,8 @@ public void testLoadValidityBuffer() { } } - private void concatAndVerify(ArrowBuf buf1, int count1, ArrowBuf buf2, int count2, ArrowBuf output) { + private void concatAndVerify( + ArrowBuf buf1, int count1, ArrowBuf buf2, int count2, ArrowBuf output) { BitVectorHelper.concatBits(buf1, count1, buf2, count2, output); int outputIdx = 0; for (int i = 0; i < count1; i++, outputIdx++) { diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestBufferOwnershipTransfer.java b/java/vector/src/test/java/org/apache/arrow/vector/TestBufferOwnershipTransfer.java index 056b6bdd2b7..b2e0adfb46e 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestBufferOwnershipTransfer.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestBufferOwnershipTransfer.java @@ -22,7 +22,6 @@ import static org.junit.Assert.assertTrue; import java.nio.charset.StandardCharsets; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.ReferenceManager; import org.apache.arrow.memory.RootAllocator; @@ -105,12 +104,18 @@ public void emptyListTransferShouldNotTriggerSchemaChange() { final Pointer trigger1 = new Pointer<>(); final Pointer trigger2 = new Pointer<>(); - final ListVector v1 = new ListVector("v1", allocator, - FieldType.nullable(ArrowType.Null.INSTANCE), - newTriggerCallback(trigger1)); - final ListVector v2 = new ListVector("v2", allocator, - FieldType.nullable(ArrowType.Null.INSTANCE), - newTriggerCallback(trigger2)); + final ListVector v1 = + new ListVector( + "v1", + allocator, + FieldType.nullable(ArrowType.Null.INSTANCE), + newTriggerCallback(trigger1)); + final ListVector v2 = + new ListVector( + "v2", + allocator, + FieldType.nullable(ArrowType.Null.INSTANCE), + newTriggerCallback(trigger2)); try { // since we are working with empty vectors, their internal diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestCopyFrom.java b/java/vector/src/test/java/org/apache/arrow/vector/TestCopyFrom.java index 97de27bec82..d812a6e9aa8 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestCopyFrom.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestCopyFrom.java @@ -27,7 +27,6 @@ import java.time.Duration; import java.time.Period; import java.util.Objects; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.types.Types.MinorType; @@ -1072,16 +1071,15 @@ public void testCopyFromWithNulls14() { } } - @Test //https://issues.apache.org/jira/browse/ARROW-7837 + @Test // https://issues.apache.org/jira/browse/ARROW-7837 public void testCopySafeArrow7837() { // this test exposes a bug in `handleSafe` where // it reads a stale index and as a result missed a required resize of the value vector. try (VarCharVector vc1 = new VarCharVector("vc1", allocator); - VarCharVector vc2 = new VarCharVector("vc2", allocator); - ) { - //initial size is carefully set in order to force the second 'copyFromSafe' operation + VarCharVector vc2 = new VarCharVector("vc2", allocator); ) { + // initial size is carefully set in order to force the second 'copyFromSafe' operation // to trigger a reallocation of the vector. - vc2.setInitialCapacity(/*valueCount*/20, /*density*/0.5); + vc2.setInitialCapacity(/*valueCount*/ 20, /*density*/ 0.5); vc1.setSafe(0, "1234567890".getBytes(StandardCharsets.UTF_8)); assertFalse(vc1.isNull(0)); @@ -1100,6 +1098,4 @@ public void testCopySafeArrow7837() { assertEquals("1234567890", Objects.requireNonNull(vc2.getObject(5)).toString()); } } - - } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestDecimal256Vector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestDecimal256Vector.java index fc5dfc38587..2a5683f50c8 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestDecimal256Vector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestDecimal256Vector.java @@ -24,7 +24,6 @@ import java.math.BigDecimal; import java.math.BigInteger; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.types.pojo.ArrowType; @@ -61,10 +60,15 @@ public void terminate() throws Exception { @Test public void testValuesWriteRead() { - try (Decimal256Vector decimalVector = TestUtils.newVector(Decimal256Vector.class, "decimal", - new ArrowType.Decimal(10, scale, 256), allocator);) { - - try (Decimal256Vector oldConstructor = new Decimal256Vector("decimal", allocator, 10, scale);) { + try (Decimal256Vector decimalVector = + TestUtils.newVector( + Decimal256Vector.class, + "decimal", + new ArrowType.Decimal(10, scale, 256), + allocator); ) { + + try (Decimal256Vector oldConstructor = + new Decimal256Vector("decimal", allocator, 10, scale); ) { assertEquals(decimalVector.getField().getType(), oldConstructor.getField().getType()); } @@ -87,32 +91,39 @@ public void testValuesWriteRead() { @Test public void testDecimal256DifferentScaleAndPrecision() { - try (Decimal256Vector decimalVector = TestUtils.newVector(Decimal256Vector.class, "decimal", - new ArrowType.Decimal(4, 2, 256), allocator)) { + try (Decimal256Vector decimalVector = + TestUtils.newVector( + Decimal256Vector.class, "decimal", new ArrowType.Decimal(4, 2, 256), allocator)) { decimalVector.allocateNew(); // test Decimal256 with different scale { BigDecimal decimal = new BigDecimal(BigInteger.valueOf(0), 3); UnsupportedOperationException ue = - assertThrows(UnsupportedOperationException.class, () -> decimalVector.setSafe(0, decimal)); - assertEquals("BigDecimal scale must equal that in the Arrow vector: 3 != 2", ue.getMessage()); + assertThrows( + UnsupportedOperationException.class, () -> decimalVector.setSafe(0, decimal)); + assertEquals( + "BigDecimal scale must equal that in the Arrow vector: 3 != 2", ue.getMessage()); } // test BigDecimal with larger precision than initialized { BigDecimal decimal = new BigDecimal(BigInteger.valueOf(12345), 2); UnsupportedOperationException ue = - assertThrows(UnsupportedOperationException.class, () -> decimalVector.setSafe(0, decimal)); - assertEquals("BigDecimal precision cannot be greater than that in the Arrow vector: 5 > 4", ue.getMessage()); + assertThrows( + UnsupportedOperationException.class, () -> decimalVector.setSafe(0, decimal)); + assertEquals( + "BigDecimal precision cannot be greater than that in the Arrow vector: 5 > 4", + ue.getMessage()); } } } @Test public void testWriteBigEndian() { - try (Decimal256Vector decimalVector = TestUtils.newVector(Decimal256Vector.class, "decimal", - new ArrowType.Decimal(38, 18, 256), allocator);) { + try (Decimal256Vector decimalVector = + TestUtils.newVector( + Decimal256Vector.class, "decimal", new ArrowType.Decimal(38, 18, 256), allocator); ) { decimalVector.allocateNew(); BigDecimal decimal1 = new BigDecimal("123456789.000000000000000000"); BigDecimal decimal2 = new BigDecimal("11.123456789123456789"); @@ -156,8 +167,9 @@ public void testWriteBigEndian() { @Test public void testLongReadWrite() { - try (Decimal256Vector decimalVector = TestUtils.newVector(Decimal256Vector.class, "decimal", - new ArrowType.Decimal(38, 0, 256), allocator)) { + try (Decimal256Vector decimalVector = + TestUtils.newVector( + Decimal256Vector.class, "decimal", new ArrowType.Decimal(38, 0, 256), allocator)) { decimalVector.allocateNew(); long[] longValues = {0L, -2L, Long.MAX_VALUE, Long.MIN_VALUE, 187L}; @@ -174,11 +186,11 @@ public void testLongReadWrite() { } } - @Test public void testBigDecimalReadWrite() { - try (Decimal256Vector decimalVector = TestUtils.newVector(Decimal256Vector.class, "decimal", - new ArrowType.Decimal(38, 9, 256), allocator);) { + try (Decimal256Vector decimalVector = + TestUtils.newVector( + Decimal256Vector.class, "decimal", new ArrowType.Decimal(38, 9, 256), allocator); ) { decimalVector.allocateNew(); BigDecimal decimal1 = new BigDecimal("123456789.000000000"); BigDecimal decimal2 = new BigDecimal("11.123456789"); @@ -212,29 +224,33 @@ public void testBigDecimalReadWrite() { } /** - * Test {@link Decimal256Vector#setBigEndian(int, byte[])} which takes BE layout input and stores in native-endian - * (NE) layout. - * Cases to cover: input byte array in different lengths in range [1-16] and negative values. + * Test {@link Decimal256Vector#setBigEndian(int, byte[])} which takes BE layout input and stores + * in native-endian (NE) layout. Cases to cover: input byte array in different lengths in range + * [1-16] and negative values. */ @Test public void decimalBE2NE() { - try (Decimal256Vector decimalVector = TestUtils.newVector(Decimal256Vector.class, "decimal", - new ArrowType.Decimal(23, 2, 256), allocator)) { + try (Decimal256Vector decimalVector = + TestUtils.newVector( + Decimal256Vector.class, "decimal", new ArrowType.Decimal(23, 2, 256), allocator)) { decimalVector.allocateNew(); - BigInteger[] testBigInts = new BigInteger[] { - new BigInteger("0"), - new BigInteger("-1"), - new BigInteger("23"), - new BigInteger("234234"), - new BigInteger("-234234234"), - new BigInteger("234234234234"), - new BigInteger("-56345345345345"), - new BigInteger("2982346298346289346293467923465345634500"), // converts to 16+ byte array - new BigInteger("-389457298347598237459832459823434653600"), // converts to 16+ byte array - new BigInteger("-345345"), - new BigInteger("754533") - }; + BigInteger[] testBigInts = + new BigInteger[] { + new BigInteger("0"), + new BigInteger("-1"), + new BigInteger("23"), + new BigInteger("234234"), + new BigInteger("-234234234"), + new BigInteger("234234234234"), + new BigInteger("-56345345345345"), + new BigInteger( + "2982346298346289346293467923465345634500"), // converts to 16+ byte array + new BigInteger( + "-389457298347598237459832459823434653600"), // converts to 16+ byte array + new BigInteger("-345345"), + new BigInteger("754533") + }; int insertionIdx = 0; insertionIdx++; // insert a null @@ -247,9 +263,12 @@ public void decimalBE2NE() { // Try inserting a buffer larger than 33 bytes and expect a failure final int insertionIdxCapture = insertionIdx; - IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, - () -> decimalVector.setBigEndian(insertionIdxCapture, new byte[33])); - assertTrue(ex.getMessage().equals("Invalid decimal value length. Valid length in [1 - 32], got 33")); + IllegalArgumentException ex = + assertThrows( + IllegalArgumentException.class, + () -> decimalVector.setBigEndian(insertionIdxCapture, new byte[33])); + assertTrue( + ex.getMessage().equals("Invalid decimal value length. Valid length in [1 - 32], got 33")); decimalVector.setValueCount(insertionIdx); // retrieve values and check if they are correct @@ -266,9 +285,10 @@ public void decimalBE2NE() { @Test public void setUsingArrowBufOfLEInts() { - try (Decimal256Vector decimalVector = TestUtils.newVector(Decimal256Vector.class, "decimal", - new ArrowType.Decimal(5, 2, 256), allocator); - ArrowBuf buf = allocator.buffer(8);) { + try (Decimal256Vector decimalVector = + TestUtils.newVector( + Decimal256Vector.class, "decimal", new ArrowType.Decimal(5, 2, 256), allocator); + ArrowBuf buf = allocator.buffer(8); ) { decimalVector.allocateNew(); // add a positive value equivalent to 705.32 @@ -283,21 +303,21 @@ public void setUsingArrowBufOfLEInts() { decimalVector.setValueCount(2); - BigDecimal [] expectedValues = new BigDecimal[] {BigDecimal.valueOf(705.32), BigDecimal - .valueOf(-705.32)}; - for (int i = 0; i < 2; i ++) { + BigDecimal[] expectedValues = + new BigDecimal[] {BigDecimal.valueOf(705.32), BigDecimal.valueOf(-705.32)}; + for (int i = 0; i < 2; i++) { BigDecimal value = decimalVector.getObject(i); assertEquals(expectedValues[i], value); } } - } @Test public void setUsingArrowLongLEBytes() { - try (Decimal256Vector decimalVector = TestUtils.newVector(Decimal256Vector.class, "decimal", - new ArrowType.Decimal(18, 0, 256), allocator); - ArrowBuf buf = allocator.buffer(16);) { + try (Decimal256Vector decimalVector = + TestUtils.newVector( + Decimal256Vector.class, "decimal", new ArrowType.Decimal(18, 0, 256), allocator); + ArrowBuf buf = allocator.buffer(16); ) { decimalVector.allocateNew(); long val = Long.MAX_VALUE; @@ -310,9 +330,9 @@ public void setUsingArrowLongLEBytes() { decimalVector.setValueCount(2); - BigDecimal [] expectedValues = new BigDecimal[] {BigDecimal.valueOf(Long.MAX_VALUE), BigDecimal - .valueOf(Long.MIN_VALUE)}; - for (int i = 0; i < 2; i ++) { + BigDecimal[] expectedValues = + new BigDecimal[] {BigDecimal.valueOf(Long.MAX_VALUE), BigDecimal.valueOf(Long.MIN_VALUE)}; + for (int i = 0; i < 2; i++) { BigDecimal value = decimalVector.getObject(i); assertEquals(expectedValues[i], value); } @@ -321,20 +341,27 @@ public void setUsingArrowLongLEBytes() { @Test public void setUsingArrowBufOfBEBytes() { - try (Decimal256Vector decimalVector = TestUtils.newVector(Decimal256Vector.class, "decimal", - new ArrowType.Decimal(5, 2, 256), allocator); - ArrowBuf buf = allocator.buffer(9);) { - BigDecimal [] expectedValues = new BigDecimal[] {BigDecimal.valueOf(705.32), BigDecimal - .valueOf(-705.32), BigDecimal.valueOf(705.32)}; + try (Decimal256Vector decimalVector = + TestUtils.newVector( + Decimal256Vector.class, "decimal", new ArrowType.Decimal(5, 2, 256), allocator); + ArrowBuf buf = allocator.buffer(9); ) { + BigDecimal[] expectedValues = + new BigDecimal[] { + BigDecimal.valueOf(705.32), BigDecimal.valueOf(-705.32), BigDecimal.valueOf(705.32) + }; verifyWritingArrowBufWithBigEndianBytes(decimalVector, buf, expectedValues, 3); } - try (Decimal256Vector decimalVector = TestUtils.newVector(Decimal256Vector.class, "decimal", - new ArrowType.Decimal(43, 2, 256), allocator); - ArrowBuf buf = allocator.buffer(45);) { - BigDecimal[] expectedValues = new BigDecimal[] {new BigDecimal("29823462983462893462934679234653450000000.63"), - new BigDecimal("-2982346298346289346293467923465345.63"), - new BigDecimal("2982346298346289346293467923465345.63")}; + try (Decimal256Vector decimalVector = + TestUtils.newVector( + Decimal256Vector.class, "decimal", new ArrowType.Decimal(43, 2, 256), allocator); + ArrowBuf buf = allocator.buffer(45); ) { + BigDecimal[] expectedValues = + new BigDecimal[] { + new BigDecimal("29823462983462893462934679234653450000000.63"), + new BigDecimal("-2982346298346289346293467923465345.63"), + new BigDecimal("2982346298346289346293467923465345.63") + }; verifyWritingArrowBufWithBigEndianBytes(decimalVector, buf, expectedValues, 15); } } @@ -344,23 +371,23 @@ public void testGetTransferPairWithField() { final Decimal256Vector fromVector = new Decimal256Vector("decimal", allocator, 10, scale); final TransferPair transferPair = fromVector.getTransferPair(fromVector.getField(), allocator); final Decimal256Vector toVector = (Decimal256Vector) transferPair.getTo(); - // Field inside a new vector created by reusing a field should be the same in memory as the original field. + // Field inside a new vector created by reusing a field should be the same in memory as the + // original field. assertSame(fromVector.getField(), toVector.getField()); } - private void verifyWritingArrowBufWithBigEndianBytes(Decimal256Vector decimalVector, - ArrowBuf buf, BigDecimal[] expectedValues, - int length) { + private void verifyWritingArrowBufWithBigEndianBytes( + Decimal256Vector decimalVector, ArrowBuf buf, BigDecimal[] expectedValues, int length) { decimalVector.allocateNew(); for (int i = 0; i < expectedValues.length; i++) { byte[] bigEndianBytes = expectedValues[i].unscaledValue().toByteArray(); - buf.setBytes(length * i , bigEndianBytes, 0 , bigEndianBytes.length); + buf.setBytes(length * i, bigEndianBytes, 0, bigEndianBytes.length); decimalVector.setBigEndianSafe(i, length * i, buf, bigEndianBytes.length); } decimalVector.setValueCount(3); - for (int i = 0; i < expectedValues.length; i ++) { + for (int i = 0; i < expectedValues.length; i++) { BigDecimal value = decimalVector.getObject(i); assertEquals(expectedValues[i], value); } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestDecimalVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestDecimalVector.java index 572f13fea1e..434573e6e1e 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestDecimalVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestDecimalVector.java @@ -24,7 +24,6 @@ import java.math.BigDecimal; import java.math.BigInteger; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.types.pojo.ArrowType; @@ -61,10 +60,11 @@ public void terminate() throws Exception { @Test public void testValuesWriteRead() { - try (DecimalVector decimalVector = TestUtils.newVector(DecimalVector.class, "decimal", - new ArrowType.Decimal(10, scale, 128), allocator);) { + try (DecimalVector decimalVector = + TestUtils.newVector( + DecimalVector.class, "decimal", new ArrowType.Decimal(10, scale, 128), allocator); ) { - try (DecimalVector oldConstructor = new DecimalVector("decimal", allocator, 10, scale);) { + try (DecimalVector oldConstructor = new DecimalVector("decimal", allocator, 10, scale); ) { assertEquals(decimalVector.getField().getType(), oldConstructor.getField().getType()); } @@ -87,8 +87,9 @@ public void testValuesWriteRead() { @Test public void testBigDecimalDifferentScaleAndPrecision() { - try (DecimalVector decimalVector = TestUtils.newVector(DecimalVector.class, "decimal", - new ArrowType.Decimal(4, 2, 128), allocator);) { + try (DecimalVector decimalVector = + TestUtils.newVector( + DecimalVector.class, "decimal", new ArrowType.Decimal(4, 2, 128), allocator); ) { decimalVector.allocateNew(); // test BigDecimal with different scale @@ -117,8 +118,9 @@ public void testBigDecimalDifferentScaleAndPrecision() { @Test public void testWriteBigEndian() { - try (DecimalVector decimalVector = TestUtils.newVector(DecimalVector.class, "decimal", - new ArrowType.Decimal(38, 9, 128), allocator);) { + try (DecimalVector decimalVector = + TestUtils.newVector( + DecimalVector.class, "decimal", new ArrowType.Decimal(38, 9, 128), allocator); ) { decimalVector.allocateNew(); BigDecimal decimal1 = new BigDecimal("123456789.000000000"); BigDecimal decimal2 = new BigDecimal("11.123456789"); @@ -162,8 +164,9 @@ public void testWriteBigEndian() { @Test public void testLongReadWrite() { - try (DecimalVector decimalVector = TestUtils.newVector(DecimalVector.class, "decimal", - new ArrowType.Decimal(38, 0, 128), allocator)) { + try (DecimalVector decimalVector = + TestUtils.newVector( + DecimalVector.class, "decimal", new ArrowType.Decimal(38, 0, 128), allocator)) { decimalVector.allocateNew(); long[] longValues = {0L, -2L, Long.MAX_VALUE, Long.MIN_VALUE, 187L}; @@ -180,11 +183,11 @@ public void testLongReadWrite() { } } - @Test public void testBigDecimalReadWrite() { - try (DecimalVector decimalVector = TestUtils.newVector(DecimalVector.class, "decimal", - new ArrowType.Decimal(38, 9, 128), allocator);) { + try (DecimalVector decimalVector = + TestUtils.newVector( + DecimalVector.class, "decimal", new ArrowType.Decimal(38, 9, 128), allocator); ) { decimalVector.allocateNew(); BigDecimal decimal1 = new BigDecimal("123456789.000000000"); BigDecimal decimal2 = new BigDecimal("11.123456789"); @@ -218,29 +221,31 @@ public void testBigDecimalReadWrite() { } /** - * Test {@link DecimalVector#setBigEndian(int, byte[])} which takes BE layout input and stores in native-endian (NE) - * layout. - * Cases to cover: input byte array in different lengths in range [1-16] and negative values. + * Test {@link DecimalVector#setBigEndian(int, byte[])} which takes BE layout input and stores in + * native-endian (NE) layout. Cases to cover: input byte array in different lengths in range + * [1-16] and negative values. */ @Test public void decimalBE2NE() { - try (DecimalVector decimalVector = TestUtils.newVector(DecimalVector.class, "decimal", - new ArrowType.Decimal(21, 2, 128), allocator)) { + try (DecimalVector decimalVector = + TestUtils.newVector( + DecimalVector.class, "decimal", new ArrowType.Decimal(21, 2, 128), allocator)) { decimalVector.allocateNew(); - BigInteger[] testBigInts = new BigInteger[] { - new BigInteger("0"), - new BigInteger("-1"), - new BigInteger("23"), - new BigInteger("234234"), - new BigInteger("-234234234"), - new BigInteger("234234234234"), - new BigInteger("-56345345345345"), - new BigInteger("29823462983462893462934679234653456345"), // converts to 16 byte array - new BigInteger("-3894572983475982374598324598234346536"), // converts to 16 byte array - new BigInteger("-345345"), - new BigInteger("754533") - }; + BigInteger[] testBigInts = + new BigInteger[] { + new BigInteger("0"), + new BigInteger("-1"), + new BigInteger("23"), + new BigInteger("234234"), + new BigInteger("-234234234"), + new BigInteger("234234234234"), + new BigInteger("-56345345345345"), + new BigInteger("29823462983462893462934679234653456345"), // converts to 16 byte array + new BigInteger("-3894572983475982374598324598234346536"), // converts to 16 byte array + new BigInteger("-345345"), + new BigInteger("754533") + }; int insertionIdx = 0; insertionIdx++; // insert a null @@ -256,7 +261,9 @@ public void decimalBE2NE() { decimalVector.setBigEndian(insertionIdx, new byte[17]); fail("above statement should have failed"); } catch (IllegalArgumentException ex) { - assertTrue(ex.getMessage().equals("Invalid decimal value length. Valid length in [1 - 16], got 17")); + assertTrue( + ex.getMessage() + .equals("Invalid decimal value length. Valid length in [1 - 16], got 17")); } decimalVector.setValueCount(insertionIdx); @@ -274,9 +281,10 @@ public void decimalBE2NE() { @Test public void setUsingArrowBufOfInts() { - try (DecimalVector decimalVector = TestUtils.newVector(DecimalVector.class, "decimal", - new ArrowType.Decimal(5, 2, 128), allocator); - ArrowBuf buf = allocator.buffer(8);) { + try (DecimalVector decimalVector = + TestUtils.newVector( + DecimalVector.class, "decimal", new ArrowType.Decimal(5, 2, 128), allocator); + ArrowBuf buf = allocator.buffer(8); ) { decimalVector.allocateNew(); // add a positive value equivalent to 705.32 @@ -291,21 +299,21 @@ public void setUsingArrowBufOfInts() { decimalVector.setValueCount(2); - BigDecimal [] expectedValues = new BigDecimal[] {BigDecimal.valueOf(705.32), BigDecimal - .valueOf(-705.32)}; - for (int i = 0; i < 2; i ++) { + BigDecimal[] expectedValues = + new BigDecimal[] {BigDecimal.valueOf(705.32), BigDecimal.valueOf(-705.32)}; + for (int i = 0; i < 2; i++) { BigDecimal value = decimalVector.getObject(i); assertEquals(expectedValues[i], value); } } - } @Test public void setUsingArrowLongBytes() { - try (DecimalVector decimalVector = TestUtils.newVector(DecimalVector.class, "decimal", - new ArrowType.Decimal(18, 0, 128), allocator); - ArrowBuf buf = allocator.buffer(16);) { + try (DecimalVector decimalVector = + TestUtils.newVector( + DecimalVector.class, "decimal", new ArrowType.Decimal(18, 0, 128), allocator); + ArrowBuf buf = allocator.buffer(16); ) { decimalVector.allocateNew(); long val = Long.MAX_VALUE; @@ -318,9 +326,9 @@ public void setUsingArrowLongBytes() { decimalVector.setValueCount(2); - BigDecimal [] expectedValues = new BigDecimal[] {BigDecimal.valueOf(Long.MAX_VALUE), BigDecimal - .valueOf(Long.MIN_VALUE)}; - for (int i = 0; i < 2; i ++) { + BigDecimal[] expectedValues = + new BigDecimal[] {BigDecimal.valueOf(Long.MAX_VALUE), BigDecimal.valueOf(Long.MIN_VALUE)}; + for (int i = 0; i < 2; i++) { BigDecimal value = decimalVector.getObject(i); assertEquals(expectedValues[i], value); } @@ -329,20 +337,27 @@ public void setUsingArrowLongBytes() { @Test public void setUsingArrowBufOfBEBytes() { - try (DecimalVector decimalVector = TestUtils.newVector(DecimalVector.class, "decimal", - new ArrowType.Decimal(5, 2, 128), allocator); - ArrowBuf buf = allocator.buffer(9);) { - BigDecimal [] expectedValues = new BigDecimal[] {BigDecimal.valueOf(705.32), BigDecimal - .valueOf(-705.32), BigDecimal.valueOf(705.32)}; + try (DecimalVector decimalVector = + TestUtils.newVector( + DecimalVector.class, "decimal", new ArrowType.Decimal(5, 2, 128), allocator); + ArrowBuf buf = allocator.buffer(9); ) { + BigDecimal[] expectedValues = + new BigDecimal[] { + BigDecimal.valueOf(705.32), BigDecimal.valueOf(-705.32), BigDecimal.valueOf(705.32) + }; verifyWritingArrowBufWithBigEndianBytes(decimalVector, buf, expectedValues, 3); } - try (DecimalVector decimalVector = TestUtils.newVector(DecimalVector.class, "decimal", - new ArrowType.Decimal(36, 2, 128), allocator); - ArrowBuf buf = allocator.buffer(45);) { - BigDecimal[] expectedValues = new BigDecimal[] {new BigDecimal("2982346298346289346293467923465345.63"), - new BigDecimal("-2982346298346289346293467923465345.63"), - new BigDecimal("2982346298346289346293467923465345.63")}; + try (DecimalVector decimalVector = + TestUtils.newVector( + DecimalVector.class, "decimal", new ArrowType.Decimal(36, 2, 128), allocator); + ArrowBuf buf = allocator.buffer(45); ) { + BigDecimal[] expectedValues = + new BigDecimal[] { + new BigDecimal("2982346298346289346293467923465345.63"), + new BigDecimal("-2982346298346289346293467923465345.63"), + new BigDecimal("2982346298346289346293467923465345.63") + }; verifyWritingArrowBufWithBigEndianBytes(decimalVector, buf, expectedValues, 15); } } @@ -352,23 +367,23 @@ public void testGetTransferPairWithField() { final DecimalVector fromVector = new DecimalVector("decimal", allocator, 10, scale); final TransferPair transferPair = fromVector.getTransferPair(fromVector.getField(), allocator); final DecimalVector toVector = (DecimalVector) transferPair.getTo(); - // Field inside a new vector created by reusing a field should be the same in memory as the original field. + // Field inside a new vector created by reusing a field should be the same in memory as the + // original field. assertSame(fromVector.getField(), toVector.getField()); } - private void verifyWritingArrowBufWithBigEndianBytes(DecimalVector decimalVector, - ArrowBuf buf, BigDecimal[] expectedValues, - int length) { + private void verifyWritingArrowBufWithBigEndianBytes( + DecimalVector decimalVector, ArrowBuf buf, BigDecimal[] expectedValues, int length) { decimalVector.allocateNew(); for (int i = 0; i < expectedValues.length; i++) { - byte []bigEndianBytes = expectedValues[i].unscaledValue().toByteArray(); - buf.setBytes(length * i , bigEndianBytes, 0 , bigEndianBytes.length); + byte[] bigEndianBytes = expectedValues[i].unscaledValue().toByteArray(); + buf.setBytes(length * i, bigEndianBytes, 0, bigEndianBytes.length); decimalVector.setBigEndianSafe(i, length * i, buf, bigEndianBytes.length); } decimalVector.setValueCount(3); - for (int i = 0; i < expectedValues.length; i ++) { + for (int i = 0; i < expectedValues.length; i++) { BigDecimal value = decimalVector.getObject(i); assertEquals(expectedValues[i], value); } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestDenseUnionVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestDenseUnionVector.java index 8fd33eb5a84..e5578b67787 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestDenseUnionVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestDenseUnionVector.java @@ -26,7 +26,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.complex.DenseUnionVector; @@ -73,11 +72,13 @@ public void testDenseUnionVector() throws Exception { uInt4Holder.value = 100; uInt4Holder.isSet = 1; - try (DenseUnionVector unionVector = new DenseUnionVector(EMPTY_SCHEMA_PATH, allocator, null, null)) { + try (DenseUnionVector unionVector = + new DenseUnionVector(EMPTY_SCHEMA_PATH, allocator, null, null)) { unionVector.allocateNew(); // write some data - byte uint4TypeId = unionVector.registerNewTypeId(Field.nullable("", MinorType.UINT4.getType())); + byte uint4TypeId = + unionVector.registerNewTypeId(Field.nullable("", MinorType.UINT4.getType())); unionVector.setTypeId(0, uint4TypeId); unionVector.setSafe(0, uInt4Holder); unionVector.setTypeId(2, uint4TypeId); @@ -101,7 +102,8 @@ public void testDenseUnionVector() throws Exception { @Test public void testTransfer() throws Exception { - try (DenseUnionVector srcVector = new DenseUnionVector(EMPTY_SCHEMA_PATH, allocator, null, null)) { + try (DenseUnionVector srcVector = + new DenseUnionVector(EMPTY_SCHEMA_PATH, allocator, null, null)) { srcVector.allocateNew(); // write some data @@ -117,7 +119,8 @@ public void testTransfer() throws Exception { srcVector.setSafe(5, newBitHolder(false)); srcVector.setValueCount(6); - try (DenseUnionVector destVector = new DenseUnionVector(EMPTY_SCHEMA_PATH, allocator, null, null)) { + try (DenseUnionVector destVector = + new DenseUnionVector(EMPTY_SCHEMA_PATH, allocator, null, null)) { TransferPair pair = srcVector.makeTransferPair(destVector); // Creating the transfer should transfer the type of the field at least. @@ -152,7 +155,8 @@ public void testTransfer() throws Exception { @Test public void testSplitAndTransfer() throws Exception { - try (DenseUnionVector sourceVector = new DenseUnionVector(EMPTY_SCHEMA_PATH, allocator, null, null)) { + try (DenseUnionVector sourceVector = + new DenseUnionVector(EMPTY_SCHEMA_PATH, allocator, null, null)) { sourceVector.allocateNew(); @@ -203,18 +207,13 @@ public void testSplitAndTransfer() throws Exception { assertEquals(false, sourceVector.isNull(9)); assertEquals(50, sourceVector.getObject(9)); - try (DenseUnionVector toVector = new DenseUnionVector(EMPTY_SCHEMA_PATH, allocator, null, null)) { + try (DenseUnionVector toVector = + new DenseUnionVector(EMPTY_SCHEMA_PATH, allocator, null, null)) { toVector.registerNewTypeId(Field.nullable("", MinorType.INT.getType())); final TransferPair transferPair = sourceVector.makeTransferPair(toVector); - final int[][] transferLengths = {{0, 3}, - {3, 1}, - {4, 2}, - {6, 1}, - {7, 1}, - {8, 2} - }; + final int[][] transferLengths = {{0, 3}, {3, 1}, {4, 2}, {6, 1}, {7, 1}, {8, 2}}; for (final int[] transferLength : transferLengths) { final int start = transferLength[0]; @@ -224,8 +223,10 @@ public void testSplitAndTransfer() throws Exception { /* check the toVector output after doing the splitAndTransfer */ for (int i = 0; i < length; i++) { - assertEquals("Different data at indexes: " + (start + i) + "and " + i, sourceVector.getObject(start + i), - toVector.getObject(i)); + assertEquals( + "Different data at indexes: " + (start + i) + "and " + i, + sourceVector.getObject(start + i), + toVector.getObject(i)); } } } @@ -234,7 +235,8 @@ public void testSplitAndTransfer() throws Exception { @Test public void testSplitAndTransferWithMixedVectors() throws Exception { - try (DenseUnionVector sourceVector = new DenseUnionVector(EMPTY_SCHEMA_PATH, allocator, null, null)) { + try (DenseUnionVector sourceVector = + new DenseUnionVector(EMPTY_SCHEMA_PATH, allocator, null, null)) { sourceVector.allocateNew(); @@ -244,7 +246,8 @@ public void testSplitAndTransferWithMixedVectors() throws Exception { sourceVector.setTypeId(0, intTypeId); sourceVector.setSafe(0, newIntHolder(5)); - byte float4TypeId = sourceVector.registerNewTypeId(Field.nullable("", MinorType.FLOAT4.getType())); + byte float4TypeId = + sourceVector.registerNewTypeId(Field.nullable("", MinorType.FLOAT4.getType())); sourceVector.setTypeId(1, float4TypeId); sourceVector.setSafe(1, newFloat4Holder(5.5f)); @@ -297,18 +300,14 @@ public void testSplitAndTransferWithMixedVectors() throws Exception { assertEquals(false, sourceVector.isNull(9)); assertEquals(30.5f, sourceVector.getObject(9)); - try (DenseUnionVector toVector = new DenseUnionVector(EMPTY_SCHEMA_PATH, allocator, null, null)) { + try (DenseUnionVector toVector = + new DenseUnionVector(EMPTY_SCHEMA_PATH, allocator, null, null)) { toVector.registerNewTypeId(Field.nullable("", MinorType.INT.getType())); toVector.registerNewTypeId(Field.nullable("", MinorType.FLOAT4.getType())); final TransferPair transferPair = sourceVector.makeTransferPair(toVector); - final int[][] transferLengths = {{0, 2}, - {2, 1}, - {3, 2}, - {5, 3}, - {8, 2} - }; + final int[][] transferLengths = {{0, 2}, {2, 1}, {3, 2}, {5, 3}, {8, 2}}; for (final int[] transferLength : transferLengths) { final int start = transferLength[0]; @@ -318,7 +317,10 @@ public void testSplitAndTransferWithMixedVectors() throws Exception { /* check the toVector output after doing the splitAndTransfer */ for (int i = 0; i < length; i++) { - assertEquals("Different values at index: " + i, sourceVector.getObject(start + i), toVector.getObject(i)); + assertEquals( + "Different values at index: " + i, + sourceVector.getObject(start + i), + toVector.getObject(i)); } } } @@ -338,8 +340,9 @@ public void testGetFieldTypeInfo() throws Exception { children.add(new Field("int", FieldType.nullable(MinorType.INT.getType()), null)); children.add(new Field("varchar", FieldType.nullable(MinorType.VARCHAR.getType()), null)); - final FieldType fieldType = new FieldType(false, new ArrowType.Union(UnionMode.Dense, typeIds), - /*dictionary=*/null, metadata); + final FieldType fieldType = + new FieldType( + false, new ArrowType.Union(UnionMode.Dense, typeIds), /* dictionary= */ null, metadata); final Field field = new Field("union", fieldType, children); MinorType minorType = MinorType.DENSEUNION; @@ -414,26 +417,26 @@ public void testGetBufferAddress() throws Exception { } } - /** - * Test adding two struct vectors to the dense union vector. - */ + /** Test adding two struct vectors to the dense union vector. */ @Test public void testMultipleStructs() { FieldType type = new FieldType(true, ArrowType.Struct.INSTANCE, null, null); try (StructVector structVector1 = new StructVector("struct1", allocator, type, null); - StructVector structVector2 = new StructVector("struct2", allocator, type, null); - DenseUnionVector unionVector = DenseUnionVector.empty("union", allocator)) { + StructVector structVector2 = new StructVector("struct2", allocator, type, null); + DenseUnionVector unionVector = DenseUnionVector.empty("union", allocator)) { // prepare sub vectors // first struct vector: (int, int) - IntVector subVector11 = structVector1 - .addOrGet("sub11", FieldType.nullable(MinorType.INT.getType()), IntVector.class); + IntVector subVector11 = + structVector1.addOrGet( + "sub11", FieldType.nullable(MinorType.INT.getType()), IntVector.class); subVector11.allocateNew(); ValueVectorDataPopulator.setVector(subVector11, 0, 1); - IntVector subVector12 = structVector1 - .addOrGet("sub12", FieldType.nullable(MinorType.INT.getType()), IntVector.class); + IntVector subVector12 = + structVector1.addOrGet( + "sub12", FieldType.nullable(MinorType.INT.getType()), IntVector.class); subVector12.allocateNew(); ValueVectorDataPopulator.setVector(subVector12, 0, 10); @@ -442,13 +445,15 @@ public void testMultipleStructs() { structVector1.setValueCount(2); // second struct vector: (string, string) - VarCharVector subVector21 = structVector2 - .addOrGet("sub21", FieldType.nullable(MinorType.VARCHAR.getType()), VarCharVector.class); + VarCharVector subVector21 = + structVector2.addOrGet( + "sub21", FieldType.nullable(MinorType.VARCHAR.getType()), VarCharVector.class); subVector21.allocateNew(); ValueVectorDataPopulator.setVector(subVector21, "a0"); - VarCharVector subVector22 = structVector2 - .addOrGet("sub22", FieldType.nullable(MinorType.VARCHAR.getType()), VarCharVector.class); + VarCharVector subVector22 = + structVector2.addOrGet( + "sub22", FieldType.nullable(MinorType.VARCHAR.getType()), VarCharVector.class); subVector22.allocateNew(); ValueVectorDataPopulator.setVector(subVector22, "b0"); @@ -502,14 +507,12 @@ public void testMultipleStructs() { } } - /** - * Test adding two varchar vectors to the dense union vector. - */ + /** Test adding two varchar vectors to the dense union vector. */ @Test public void testMultipleVarChars() { try (VarCharVector childVector1 = new VarCharVector("child1", allocator); - VarCharVector childVector2 = new VarCharVector("child2", allocator); - DenseUnionVector unionVector = DenseUnionVector.empty("union", allocator)) { + VarCharVector childVector2 = new VarCharVector("child2", allocator); + DenseUnionVector unionVector = DenseUnionVector.empty("union", allocator)) { // prepare sub vectors ValueVectorDataPopulator.setVector(childVector1, "a0", "a4"); @@ -544,7 +547,6 @@ public void testMultipleVarChars() { unionVector.setTypeId(2, typeId2); offsetBuf.setInt(DenseUnionVector.OFFSET_WIDTH * 2, 1); - // slot 4 points to child1 unionVector.setTypeId(4, typeId1); offsetBuf.setInt(DenseUnionVector.OFFSET_WIDTH * 4, 1); @@ -581,11 +583,13 @@ public void testChildVectorValueCounts() { vector.setTypeId(0, intTypeId); intHolder.value = 7; vector.setSafe(0, intHolder); - byte longTypeId = vector.registerNewTypeId(Field.nullable("", Types.MinorType.BIGINT.getType())); + byte longTypeId = + vector.registerNewTypeId(Field.nullable("", Types.MinorType.BIGINT.getType())); vector.setTypeId(2, longTypeId); longHolder.value = 8L; vector.setSafe(2, longHolder); - byte floatTypeId = vector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT4.getType())); + byte floatTypeId = + vector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT4.getType())); vector.setTypeId(3, floatTypeId); floatHolder.value = 9.0f; vector.setSafe(3, floatHolder); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestDictionaryVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestDictionaryVector.java index 9ffa79470ee..2eb5ed1a104 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestDictionaryVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestDictionaryVector.java @@ -28,7 +28,6 @@ import java.util.List; import java.util.Map; import java.util.function.ToIntBiFunction; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.complex.FixedSizeListVector; @@ -77,7 +76,7 @@ public void terminate() throws Exception { public void testEncodeStrings() { // Create a new value vector try (final VarCharVector vector = newVarCharVector("foo", allocator); - final VarCharVector dictionaryVector = newVarCharVector("dict", allocator);) { + final VarCharVector dictionaryVector = newVarCharVector("dict", allocator); ) { setVector(vector, zero, one, one, two, zero); setVector(dictionaryVector, zero, one, two); @@ -113,7 +112,7 @@ public void testEncodeStrings() { public void testEncodeLargeVector() { // Create a new value vector try (final VarCharVector vector = newVarCharVector("foo", allocator); - final VarCharVector dictionaryVector = newVarCharVector("dict", allocator);) { + final VarCharVector dictionaryVector = newVarCharVector("dict", allocator); ) { vector.allocateNew(); int count = 10000; @@ -154,30 +153,31 @@ public void testEncodeLargeVector() { public void testEncodeList() { // Create a new value vector try (final ListVector vector = ListVector.empty("vector", allocator); - final ListVector dictionaryVector = ListVector.empty("dict", allocator);) { + final ListVector dictionaryVector = ListVector.empty("dict", allocator); ) { UnionListWriter writer = vector.getWriter(); writer.allocate(); - //set some values - writeListVector(writer, new int[]{10, 20}); - writeListVector(writer, new int[]{10, 20}); - writeListVector(writer, new int[]{10, 20}); - writeListVector(writer, new int[]{30, 40, 50}); - writeListVector(writer, new int[]{30, 40, 50}); - writeListVector(writer, new int[]{10, 20}); + // set some values + writeListVector(writer, new int[] {10, 20}); + writeListVector(writer, new int[] {10, 20}); + writeListVector(writer, new int[] {10, 20}); + writeListVector(writer, new int[] {30, 40, 50}); + writeListVector(writer, new int[] {30, 40, 50}); + writeListVector(writer, new int[] {10, 20}); writer.setValueCount(6); UnionListWriter dictWriter = dictionaryVector.getWriter(); dictWriter.allocate(); - writeListVector(dictWriter, new int[]{10, 20}); - writeListVector(dictWriter, new int[]{30, 40, 50}); + writeListVector(dictWriter, new int[] {10, 20}); + writeListVector(dictWriter, new int[] {30, 40, 50}); dictWriter.setValueCount(2); - Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); + Dictionary dictionary = + new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); try (final ValueVector encoded = DictionaryEncoder.encode(vector, dictionary)) { // verify indices @@ -208,11 +208,13 @@ public void testEncodeList() { public void testEncodeStruct() { // Create a new value vector try (final StructVector vector = StructVector.empty("vector", allocator); - final StructVector dictionaryVector = StructVector.empty("dict", allocator);) { + final StructVector dictionaryVector = StructVector.empty("dict", allocator); ) { vector.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); vector.addOrGet("f1", FieldType.nullable(new ArrowType.Int(64, true)), BigIntVector.class); - dictionaryVector.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); - dictionaryVector.addOrGet("f1", FieldType.nullable(new ArrowType.Int(64, true)), BigIntVector.class); + dictionaryVector.addOrGet( + "f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); + dictionaryVector.addOrGet( + "f1", FieldType.nullable(new ArrowType.Int(64, true)), BigIntVector.class); NullableStructWriter writer = vector.getWriter(); writer.allocate(); @@ -233,10 +235,10 @@ public void testEncodeStruct() { writeStructVector(dictWriter, 1, 10L); writeStructVector(dictWriter, 2, 20L); - dictionaryVector.setValueCount(2); - Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); + Dictionary dictionary = + new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); try (final ValueVector encoded = DictionaryEncoder.encode(vector, dictionary)) { // verify indices @@ -268,12 +270,13 @@ public void testEncodeStruct() { public void testEncodeBinaryVector() { // Create a new value vector try (final VarBinaryVector vector = newVarBinaryVector("foo", allocator); - final VarBinaryVector dictionaryVector = newVarBinaryVector("dict", allocator)) { + final VarBinaryVector dictionaryVector = newVarBinaryVector("dict", allocator)) { setVector(vector, zero, one, one, two, zero); setVector(dictionaryVector, zero, one, two); - Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); + Dictionary dictionary = + new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); try (final ValueVector encoded = DictionaryEncoder.encode(vector, dictionary)) { // verify indices @@ -288,7 +291,8 @@ public void testEncodeBinaryVector() { assertEquals(0, index.get(4)); // now run through the decoder and verify we get the original back - try (VarBinaryVector decoded = (VarBinaryVector) DictionaryEncoder.decode(encoded, dictionary)) { + try (VarBinaryVector decoded = + (VarBinaryVector) DictionaryEncoder.decode(encoded, dictionary)) { assertEquals(vector.getClass(), decoded.getClass()); assertEquals(vector.getValueCount(), decoded.getValueCount()); for (int i = 0; i < 5; i++) { @@ -302,9 +306,10 @@ public void testEncodeBinaryVector() { @Test public void testEncodeUnion() { // Create a new value vector - try (final UnionVector vector = new UnionVector("vector", allocator, /* field type */ null, /* call-back */ null); - final UnionVector dictionaryVector = - new UnionVector("dict", allocator, /* field type */ null, /* call-back */ null);) { + try (final UnionVector vector = + new UnionVector("vector", allocator, /* field type */ null, /* call-back */ null); + final UnionVector dictionaryVector = + new UnionVector("dict", allocator, /* field type */ null, /* call-back */ null); ) { final NullableUInt4Holder uintHolder1 = new NullableUInt4Holder(); uintHolder1.value = 10; @@ -318,7 +323,7 @@ public void testEncodeUnion() { intHolder2.value = 20; intHolder2.isSet = 1; - //write data + // write data vector.setType(0, Types.MinorType.UINT4); vector.setSafe(0, uintHolder1); @@ -336,7 +341,7 @@ public void testEncodeUnion() { vector.setValueCount(5); - //write dictionary + // write dictionary dictionaryVector.setType(0, Types.MinorType.UINT4); dictionaryVector.setSafe(0, uintHolder1); @@ -348,7 +353,8 @@ public void testEncodeUnion() { dictionaryVector.setValueCount(3); - Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); + Dictionary dictionary = + new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); try (final ValueVector encoded = DictionaryEncoder.encode(vector, dictionary)) { // verify indices @@ -376,9 +382,9 @@ public void testEncodeUnion() { @Test public void testIntEquals() { - //test Int + // test Int try (final IntVector vector1 = new IntVector("int", allocator); - final IntVector vector2 = new IntVector("int", allocator)) { + final IntVector vector2 = new IntVector("int", allocator)) { Dictionary dict1 = new Dictionary(vector1, new DictionaryEncoding(1L, false, null)); Dictionary dict2 = new Dictionary(vector2, new DictionaryEncoding(1L, false, null)); @@ -396,7 +402,7 @@ public void testIntEquals() { @Test public void testVarcharEquals() { try (final VarCharVector vector1 = new VarCharVector("varchar", allocator); - final VarCharVector vector2 = new VarCharVector("varchar", allocator)) { + final VarCharVector vector2 = new VarCharVector("varchar", allocator)) { Dictionary dict1 = new Dictionary(vector1, new DictionaryEncoding(1L, false, null)); Dictionary dict2 = new Dictionary(vector2, new DictionaryEncoding(1L, false, null)); @@ -414,7 +420,7 @@ public void testVarcharEquals() { @Test public void testVarBinaryEquals() { try (final VarBinaryVector vector1 = new VarBinaryVector("binary", allocator); - final VarBinaryVector vector2 = new VarBinaryVector("binary", allocator)) { + final VarBinaryVector vector2 = new VarBinaryVector("binary", allocator)) { Dictionary dict1 = new Dictionary(vector1, new DictionaryEncoding(1L, false, null)); Dictionary dict2 = new Dictionary(vector2, new DictionaryEncoding(1L, false, null)); @@ -432,7 +438,7 @@ public void testVarBinaryEquals() { @Test public void testListEquals() { try (final ListVector vector1 = ListVector.empty("list", allocator); - final ListVector vector2 = ListVector.empty("list", allocator);) { + final ListVector vector2 = ListVector.empty("list", allocator); ) { Dictionary dict1 = new Dictionary(vector1, new DictionaryEncoding(1L, false, null)); Dictionary dict2 = new Dictionary(vector2, new DictionaryEncoding(1L, false, null)); @@ -440,7 +446,7 @@ public void testListEquals() { UnionListWriter writer1 = vector1.getWriter(); writer1.allocate(); - //set some values + // set some values writeListVector(writer1, new int[] {1, 2}); writeListVector(writer1, new int[] {3, 4}); writeListVector(writer1, new int[] {5, 6}); @@ -449,7 +455,7 @@ public void testListEquals() { UnionListWriter writer2 = vector2.getWriter(); writer2.allocate(); - //set some values + // set some values writeListVector(writer2, new int[] {1, 2}); writeListVector(writer2, new int[] {3, 4}); writeListVector(writer2, new int[] {5, 6}); @@ -462,7 +468,7 @@ public void testListEquals() { @Test public void testStructEquals() { try (final StructVector vector1 = StructVector.empty("struct", allocator); - final StructVector vector2 = StructVector.empty("struct", allocator);) { + final StructVector vector2 = StructVector.empty("struct", allocator); ) { vector1.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); vector1.addOrGet("f1", FieldType.nullable(new ArrowType.Int(64, true)), BigIntVector.class); vector2.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); @@ -491,9 +497,10 @@ public void testStructEquals() { @Test public void testUnionEquals() { - try (final UnionVector vector1 = new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); - final UnionVector vector2 = - new UnionVector("union", allocator, /* field type */ null, /* call-back */ null);) { + try (final UnionVector vector1 = + new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); + final UnionVector vector2 = + new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); ) { final NullableUInt4Holder uInt4Holder = new NullableUInt4Holder(); uInt4Holder.value = 10; @@ -528,7 +535,7 @@ public void testUnionEquals() { public void testEncodeWithEncoderInstance() { // Create a new value vector try (final VarCharVector vector = newVarCharVector("vector", allocator); - final VarCharVector dictionaryVector = newVarCharVector("dict", allocator);) { + final VarCharVector dictionaryVector = newVarCharVector("dict", allocator); ) { setVector(vector, zero, one, one, two, zero); setVector(dictionaryVector, zero, one, two); @@ -565,8 +572,8 @@ public void testEncodeWithEncoderInstance() { public void testEncodeMultiVectors() { // Create a new value vector try (final VarCharVector vector1 = newVarCharVector("vector1", allocator); - final VarCharVector vector2 = newVarCharVector("vector2", allocator); - final VarCharVector dictionaryVector = newVarCharVector("dict", allocator);) { + final VarCharVector vector2 = newVarCharVector("vector2", allocator); + final VarCharVector dictionaryVector = newVarCharVector("dict", allocator); ) { setVector(vector1, zero, one, one, two, zero); setVector(vector2, zero, one, one); @@ -624,26 +631,27 @@ public void testEncodeMultiVectors() { public void testEncodeListSubField() { // Create a new value vector try (final ListVector vector = ListVector.empty("vector", allocator); - final ListVector dictionaryVector = ListVector.empty("dict", allocator);) { + final ListVector dictionaryVector = ListVector.empty("dict", allocator); ) { UnionListWriter writer = vector.getWriter(); writer.allocate(); - //set some values - writeListVector(writer, new int[]{10, 20}); - writeListVector(writer, new int[]{10, 20}); - writeListVector(writer, new int[]{10, 20}); - writeListVector(writer, new int[]{30, 40, 50}); - writeListVector(writer, new int[]{30, 40, 50}); - writeListVector(writer, new int[]{10, 20}); + // set some values + writeListVector(writer, new int[] {10, 20}); + writeListVector(writer, new int[] {10, 20}); + writeListVector(writer, new int[] {10, 20}); + writeListVector(writer, new int[] {30, 40, 50}); + writeListVector(writer, new int[] {30, 40, 50}); + writeListVector(writer, new int[] {10, 20}); writer.setValueCount(6); UnionListWriter dictWriter = dictionaryVector.getWriter(); dictWriter.allocate(); - writeListVector(dictWriter, new int[]{10, 20, 30, 40, 50}); + writeListVector(dictWriter, new int[] {10, 20, 30, 40, 50}); dictionaryVector.setValueCount(1); - Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); + Dictionary dictionary = + new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); ListSubfieldEncoder encoder = new ListSubfieldEncoder(dictionary, allocator); try (final ListVector encoded = (ListVector) encoder.encodeListSubField(vector)) { @@ -680,13 +688,15 @@ public void testEncodeListSubField() { public void testEncodeFixedSizeListSubField() { // Create a new value vector try (final FixedSizeListVector vector = FixedSizeListVector.empty("vector", 2, allocator); - final FixedSizeListVector dictionaryVector = FixedSizeListVector.empty("dict", 2, allocator)) { + final FixedSizeListVector dictionaryVector = + FixedSizeListVector.empty("dict", 2, allocator)) { vector.allocateNew(); vector.setValueCount(4); IntVector dataVector = - (IntVector) vector.addOrGetVector(FieldType.nullable(Types.MinorType.INT.getType())).getVector(); + (IntVector) + vector.addOrGetVector(FieldType.nullable(Types.MinorType.INT.getType())).getVector(); dataVector.allocateNew(8); dataVector.setValueCount(8); // set value at index 0 @@ -709,7 +719,10 @@ public void testEncodeFixedSizeListSubField() { dictionaryVector.allocateNew(); dictionaryVector.setValueCount(2); IntVector dictDataVector = - (IntVector) dictionaryVector.addOrGetVector(FieldType.nullable(Types.MinorType.INT.getType())).getVector(); + (IntVector) + dictionaryVector + .addOrGetVector(FieldType.nullable(Types.MinorType.INT.getType())) + .getVector(); dictDataVector.allocateNew(4); dictDataVector.setValueCount(4); @@ -720,7 +733,8 @@ public void testEncodeFixedSizeListSubField() { dictDataVector.set(2, 30); dictDataVector.set(3, 40); - Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); + Dictionary dictionary = + new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); ListSubfieldEncoder encoder = new ListSubfieldEncoder(dictionary, allocator); try (final FixedSizeListVector encoded = @@ -753,15 +767,15 @@ public void testEncodeFixedSizeListSubField() { @Test public void testEncodeStructSubField() { try (final StructVector vector = StructVector.empty("vector", allocator); - final VarCharVector dictVector1 = new VarCharVector("f0", allocator); - final VarCharVector dictVector2 = new VarCharVector("f1", allocator)) { + final VarCharVector dictVector1 = new VarCharVector("f0", allocator); + final VarCharVector dictVector2 = new VarCharVector("f1", allocator)) { vector.addOrGet("f0", FieldType.nullable(ArrowType.Utf8.INSTANCE), VarCharVector.class); vector.addOrGet("f1", FieldType.nullable(ArrowType.Utf8.INSTANCE), VarCharVector.class); NullableStructWriter writer = vector.getWriter(); writer.allocate(); - //set some values + // set some values writeStructVector(writer, "aa", "baz"); writeStructVector(writer, "bb", "bar"); writeStructVector(writer, "cc", "foo"); @@ -770,15 +784,17 @@ public void testEncodeStructSubField() { writer.setValueCount(5); // initialize dictionaries - DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); - + DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); - setVector(dictVector1, + setVector( + dictVector1, "aa".getBytes(StandardCharsets.UTF_8), "bb".getBytes(StandardCharsets.UTF_8), "cc".getBytes(StandardCharsets.UTF_8), "dd".getBytes(StandardCharsets.UTF_8)); - setVector(dictVector2, + setVector( + dictVector2, "foo".getBytes(StandardCharsets.UTF_8), "baz".getBytes(StandardCharsets.UTF_8), "bar".getBytes(StandardCharsets.UTF_8)); @@ -791,7 +807,8 @@ public void testEncodeStructSubField() { columnToDictionaryId.put(0, 1L); columnToDictionaryId.put(1, 2L); - try (final StructVector encoded = (StructVector) encoder.encode(vector, columnToDictionaryId)) { + try (final StructVector encoded = + (StructVector) encoder.encode(vector, columnToDictionaryId)) { // verify indices assertEquals(StructVector.class, encoded.getClass()); @@ -823,14 +840,14 @@ public void testEncodeStructSubField() { public void testEncodeStructSubFieldWithCertainColumns() { // in this case, some child vector is encoded and others are not try (final StructVector vector = StructVector.empty("vector", allocator); - final VarCharVector dictVector1 = new VarCharVector("f0", allocator)) { + final VarCharVector dictVector1 = new VarCharVector("f0", allocator)) { vector.addOrGet("f0", FieldType.nullable(ArrowType.Utf8.INSTANCE), VarCharVector.class); vector.addOrGet("f1", FieldType.nullable(ArrowType.Utf8.INSTANCE), VarCharVector.class); NullableStructWriter writer = vector.getWriter(); writer.allocate(); - //set some values + // set some values writeStructVector(writer, "aa", "baz"); writeStructVector(writer, "bb", "bar"); writeStructVector(writer, "cc", "foo"); @@ -839,17 +856,23 @@ public void testEncodeStructSubFieldWithCertainColumns() { writer.setValueCount(5); // initialize dictionaries - DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); + DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); - setVector(dictVector1, "aa".getBytes(StandardCharsets.UTF_8), "bb".getBytes(StandardCharsets.UTF_8), - "cc".getBytes(StandardCharsets.UTF_8), "dd".getBytes(StandardCharsets.UTF_8)); + setVector( + dictVector1, + "aa".getBytes(StandardCharsets.UTF_8), + "bb".getBytes(StandardCharsets.UTF_8), + "cc".getBytes(StandardCharsets.UTF_8), + "dd".getBytes(StandardCharsets.UTF_8)); provider.put(new Dictionary(dictVector1, new DictionaryEncoding(1L, false, null))); StructSubfieldEncoder encoder = new StructSubfieldEncoder(allocator, provider); Map columnToDictionaryId = new HashMap<>(); columnToDictionaryId.put(0, 1L); - try (final StructVector encoded = (StructVector) encoder.encode(vector, columnToDictionaryId)) { + try (final StructVector encoded = + (StructVector) encoder.encode(vector, columnToDictionaryId)) { // verify indices assertEquals(StructVector.class, encoded.getClass()); @@ -874,7 +897,6 @@ public void testEncodeStructSubFieldWithCertainColumns() { } } } - } } @@ -882,13 +904,13 @@ public void testEncodeStructSubFieldWithCertainColumns() { public void testNoMemoryLeak() { // test no memory leak when encode try (final VarCharVector vector = newVarCharVector("foo", allocator); - final VarCharVector dictionaryVector = newVarCharVector("dict", allocator)) { + final VarCharVector dictionaryVector = newVarCharVector("dict", allocator)) { setVector(vector, zero, one, two); setVector(dictionaryVector, zero, one); Dictionary dictionary = - new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); + new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); try (final ValueVector encoded = DictionaryEncoder.encode(vector, dictionary)) { fail("There should be an exception when encoding"); @@ -900,13 +922,13 @@ public void testNoMemoryLeak() { // test no memory leak when decode try (final IntVector indices = newVector(IntVector.class, "", Types.MinorType.INT, allocator); - final VarCharVector dictionaryVector = newVarCharVector("dict", allocator)) { + final VarCharVector dictionaryVector = newVarCharVector("dict", allocator)) { setVector(indices, 3); setVector(dictionaryVector, zero, one); Dictionary dictionary = - new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); + new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); try (final ValueVector decoded = DictionaryEncoder.decode(indices, dictionary, allocator)) { fail("There should be an exception when decoding"); @@ -921,19 +943,20 @@ public void testNoMemoryLeak() { public void testListNoMemoryLeak() { // Create a new value vector try (final ListVector vector = ListVector.empty("vector", allocator); - final ListVector dictionaryVector = ListVector.empty("dict", allocator)) { + final ListVector dictionaryVector = ListVector.empty("dict", allocator)) { UnionListWriter writer = vector.getWriter(); writer.allocate(); - writeListVector(writer, new int[]{10, 20}); + writeListVector(writer, new int[] {10, 20}); writer.setValueCount(1); UnionListWriter dictWriter = dictionaryVector.getWriter(); dictWriter.allocate(); - writeListVector(dictWriter, new int[]{10}); + writeListVector(dictWriter, new int[] {10}); dictionaryVector.setValueCount(1); - Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); + Dictionary dictionary = + new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); ListSubfieldEncoder encoder = new ListSubfieldEncoder(dictionary, allocator); try (final ListVector encoded = (ListVector) encoder.encodeListSubField(vector)) { @@ -945,22 +968,23 @@ public void testListNoMemoryLeak() { assertEquals("list encode memory leak", 0, allocator.getAllocatedMemory()); try (final ListVector indices = ListVector.empty("indices", allocator); - final ListVector dictionaryVector = ListVector.empty("dict", allocator)) { + final ListVector dictionaryVector = ListVector.empty("dict", allocator)) { UnionListWriter writer = indices.getWriter(); writer.allocate(); - writeListVector(writer, new int[]{3}); + writeListVector(writer, new int[] {3}); writer.setValueCount(1); UnionListWriter dictWriter = dictionaryVector.getWriter(); dictWriter.allocate(); - writeListVector(dictWriter, new int[]{10, 20}); + writeListVector(dictWriter, new int[] {10, 20}); dictionaryVector.setValueCount(1); Dictionary dictionary = - new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); + new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); - try (final ValueVector decoded = ListSubfieldEncoder.decodeListSubField(indices, dictionary, allocator)) { + try (final ValueVector decoded = + ListSubfieldEncoder.decodeListSubField(indices, dictionary, allocator)) { fail("There should be an exception when decoding"); } catch (Exception e) { assertEquals("Provided dictionary does not contain value for index 3", e.getMessage()); @@ -972,8 +996,8 @@ public void testListNoMemoryLeak() { @Test public void testStructNoMemoryLeak() { try (final StructVector vector = StructVector.empty("vector", allocator); - final VarCharVector dictVector1 = new VarCharVector("f0", allocator); - final VarCharVector dictVector2 = new VarCharVector("f1", allocator)) { + final VarCharVector dictVector1 = new VarCharVector("f0", allocator); + final VarCharVector dictVector2 = new VarCharVector("f1", allocator)) { vector.addOrGet("f0", FieldType.nullable(ArrowType.Utf8.INSTANCE), VarCharVector.class); vector.addOrGet("f1", FieldType.nullable(ArrowType.Utf8.INSTANCE), VarCharVector.class); @@ -983,11 +1007,10 @@ public void testStructNoMemoryLeak() { writeStructVector(writer, "aa", "baz"); writer.setValueCount(1); - DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); - setVector(dictVector1, - "aa".getBytes(StandardCharsets.UTF_8)); - setVector(dictVector2, - "foo".getBytes(StandardCharsets.UTF_8)); + DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); + setVector(dictVector1, "aa".getBytes(StandardCharsets.UTF_8)); + setVector(dictVector2, "foo".getBytes(StandardCharsets.UTF_8)); provider.put(new Dictionary(dictVector1, new DictionaryEncoding(1L, false, null))); provider.put(new Dictionary(dictVector2, new DictionaryEncoding(2L, false, null))); @@ -997,7 +1020,8 @@ public void testStructNoMemoryLeak() { columnToDictionaryId.put(0, 1L); columnToDictionaryId.put(1, 2L); - try (final StructVector encoded = (StructVector) encoder.encode(vector, columnToDictionaryId)) { + try (final StructVector encoded = + (StructVector) encoder.encode(vector, columnToDictionaryId)) { fail("There should be an exception when encoding"); } catch (Exception e) { assertEquals("Dictionary encoding not defined for value:baz", e.getMessage()); @@ -1006,25 +1030,22 @@ public void testStructNoMemoryLeak() { assertEquals("struct encode memory leak", 0, allocator.getAllocatedMemory()); try (final StructVector indices = StructVector.empty("indices", allocator); - final VarCharVector dictVector1 = new VarCharVector("f0", allocator); - final VarCharVector dictVector2 = new VarCharVector("f1", allocator)) { + final VarCharVector dictVector1 = new VarCharVector("f0", allocator); + final VarCharVector dictVector2 = new VarCharVector("f1", allocator)) { - DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); - setVector(dictVector1, - "aa".getBytes(StandardCharsets.UTF_8)); - setVector(dictVector2, - "foo".getBytes(StandardCharsets.UTF_8)); + DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); + setVector(dictVector1, "aa".getBytes(StandardCharsets.UTF_8)); + setVector(dictVector2, "foo".getBytes(StandardCharsets.UTF_8)); provider.put(new Dictionary(dictVector1, new DictionaryEncoding(1L, false, null))); provider.put(new Dictionary(dictVector2, new DictionaryEncoding(2L, false, null))); ArrowType int32 = new ArrowType.Int(32, true); - indices.addOrGet("f0", - new FieldType(true, int32, provider.lookup(1L).getEncoding()), - IntVector.class); - indices.addOrGet("f1", - new FieldType(true, int32, provider.lookup(2L).getEncoding()), - IntVector.class); + indices.addOrGet( + "f0", new FieldType(true, int32, provider.lookup(1L).getEncoding()), IntVector.class); + indices.addOrGet( + "f1", new FieldType(true, int32, provider.lookup(2L).getEncoding()), IntVector.class); NullableStructWriter writer = indices.getWriter(); writer.allocate(); @@ -1043,7 +1064,8 @@ public void testStructNoMemoryLeak() { assertEquals("struct decode memory leak", 0, allocator.getAllocatedMemory()); } - private void testDictionary(Dictionary dictionary, ToIntBiFunction valGetter) { + private void testDictionary( + Dictionary dictionary, ToIntBiFunction valGetter) { try (VarCharVector vector = new VarCharVector("vector", allocator)) { setVector(vector, "1", "3", "5", "7", "9"); try (ValueVector encodedVector = DictionaryEncoder.encode(vector, dictionary)) { @@ -1059,11 +1081,16 @@ private void testDictionary(Dictionary dictionary, ToIntBiFunction ((UInt1Vector) vector).get(index)); } } @@ -1084,9 +1115,13 @@ public void testDictionaryUInt1() { public void testDictionaryUInt2() { try (VarCharVector dictionaryVector = new VarCharVector("dict vector", allocator)) { setVector(dictionaryVector, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); - Dictionary dictionary2 = new Dictionary(dictionaryVector, - new DictionaryEncoding(/*id=*/20L, /*ordered=*/false, - /*indexType=*/new ArrowType.Int(/*bitWidth=*/16, /*isSigned*/false))); + Dictionary dictionary2 = + new Dictionary( + dictionaryVector, + new DictionaryEncoding( + /* id= */ 20L, + /* ordered= */ false, + /* indexType= */ new ArrowType.Int(/* bitWidth= */ 16, /*isSigned*/ false))); testDictionary(dictionary2, (vector, index) -> ((UInt2Vector) vector).get(index)); } } @@ -1095,9 +1130,13 @@ public void testDictionaryUInt2() { public void testDictionaryUInt4() { try (VarCharVector dictionaryVector = new VarCharVector("dict vector", allocator)) { setVector(dictionaryVector, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); - Dictionary dictionary4 = new Dictionary(dictionaryVector, - new DictionaryEncoding(/*id=*/30L, /*ordered=*/false, - /*indexType=*/new ArrowType.Int(/*bitWidth=*/32, /*isSigned*/false))); + Dictionary dictionary4 = + new Dictionary( + dictionaryVector, + new DictionaryEncoding( + /* id= */ 30L, + /* ordered= */ false, + /* indexType= */ new ArrowType.Int(/* bitWidth= */ 32, /*isSigned*/ false))); testDictionary(dictionary4, (vector, index) -> ((UInt4Vector) vector).get(index)); } } @@ -1106,9 +1145,13 @@ public void testDictionaryUInt4() { public void testDictionaryUInt8() { try (VarCharVector dictionaryVector = new VarCharVector("dict vector", allocator)) { setVector(dictionaryVector, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); - Dictionary dictionary8 = new Dictionary(dictionaryVector, - new DictionaryEncoding(/*id=*/40L, /*ordered=*/false, - /*indexType=*/new ArrowType.Int(/*bitWidth=*/64, /*isSigned*/false))); + Dictionary dictionary8 = + new Dictionary( + dictionaryVector, + new DictionaryEncoding( + /* id= */ 40L, + /* ordered= */ false, + /* indexType= */ new ArrowType.Int(/* bitWidth= */ 64, /*isSigned*/ false))); testDictionary(dictionary8, (vector, index) -> (int) ((UInt8Vector) vector).get(index)); } } @@ -1124,19 +1167,25 @@ public void testDictionaryUIntOverflow() { } dictionaryVector.setValueCount(vecLength); - Dictionary dictionary = new Dictionary(dictionaryVector, - new DictionaryEncoding(/*id=*/10L, /*ordered=*/false, - /*indexType=*/new ArrowType.Int(/*bitWidth=*/8, /*isSigned*/false))); + Dictionary dictionary = + new Dictionary( + dictionaryVector, + new DictionaryEncoding( + /* id= */ 10L, + /* ordered= */ false, + /* indexType= */ new ArrowType.Int(/* bitWidth= */ 8, /*isSigned*/ false))); try (VarCharVector vector = new VarCharVector("vector", allocator)) { setVector(vector, "255"); - try (UInt1Vector encodedVector = (UInt1Vector) DictionaryEncoder.encode(vector, dictionary)) { + try (UInt1Vector encodedVector = + (UInt1Vector) DictionaryEncoder.encode(vector, dictionary)) { // verify encoded result assertEquals(1, encodedVector.getValueCount()); assertEquals(255, encodedVector.getValueAsLong(0)); - try (VarCharVector decodedVector = (VarCharVector) DictionaryEncoder.decode(encodedVector, dictionary)) { + try (VarCharVector decodedVector = + (VarCharVector) DictionaryEncoder.decode(encodedVector, dictionary)) { assertEquals(1, decodedVector.getValueCount()); assertArrayEquals("255".getBytes(StandardCharsets.UTF_8), decodedVector.get(0)); } @@ -1186,7 +1235,7 @@ private void writeStructVector(NullableStructWriter writer, int value1, long val private void writeListVector(UnionListWriter writer, int[] values) { writer.startList(); - for (int v: values) { + for (int v : values) { writer.integer().writeInt(v); } writer.endList(); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestDurationVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestDurationVector.java index c5d4d296cc0..131073ece41 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestDurationVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestDurationVector.java @@ -22,7 +22,6 @@ import static org.junit.Assert.assertSame; import java.time.Duration; - import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.holders.NullableDurationHolder; import org.apache.arrow.vector.types.TimeUnit; @@ -47,8 +46,9 @@ public void terminate() { @Test public void testSecBasics() { - try (DurationVector secVector = TestUtils.newVector(DurationVector.class, "second", - new ArrowType.Duration(TimeUnit.SECOND), allocator)) { + try (DurationVector secVector = + TestUtils.newVector( + DurationVector.class, "second", new ArrowType.Duration(TimeUnit.SECOND), allocator)) { secVector.allocateNew(); secVector.setNull(0); @@ -63,15 +63,19 @@ public void testSecBasics() { secVector.get(0, holder); assertEquals(0, holder.isSet); secVector.get(1, holder); - assertEquals(1 , holder.isSet); - assertEquals(1000 , holder.value); + assertEquals(1, holder.isSet); + assertEquals(1000, holder.value); } } @Test public void testMilliBasics() { - try (DurationVector milliVector = TestUtils.newVector(DurationVector.class, "nanos", - new ArrowType.Duration(TimeUnit.MILLISECOND), allocator)) { + try (DurationVector milliVector = + TestUtils.newVector( + DurationVector.class, + "nanos", + new ArrowType.Duration(TimeUnit.MILLISECOND), + allocator)) { milliVector.allocateNew(); milliVector.setNull(0); @@ -86,15 +90,19 @@ public void testMilliBasics() { milliVector.get(0, holder); assertEquals(0, holder.isSet); milliVector.get(1, holder); - assertEquals(1 , holder.isSet); - assertEquals(1000 , holder.value); + assertEquals(1, holder.isSet); + assertEquals(1000, holder.value); } } @Test public void testMicroBasics() { - try (DurationVector microVector = TestUtils.newVector(DurationVector.class, "micro", - new ArrowType.Duration(TimeUnit.MICROSECOND), allocator)) { + try (DurationVector microVector = + TestUtils.newVector( + DurationVector.class, + "micro", + new ArrowType.Duration(TimeUnit.MICROSECOND), + allocator)) { microVector.allocateNew(); microVector.setNull(0); @@ -109,15 +117,19 @@ public void testMicroBasics() { microVector.get(0, holder); assertEquals(0, holder.isSet); microVector.get(1, holder); - assertEquals(1 , holder.isSet); - assertEquals(1000 , holder.value); + assertEquals(1, holder.isSet); + assertEquals(1000, holder.value); } } @Test public void testNanosBasics() { - try (DurationVector nanoVector = TestUtils.newVector(DurationVector.class, "nanos", - new ArrowType.Duration(TimeUnit.NANOSECOND), allocator)) { + try (DurationVector nanoVector = + TestUtils.newVector( + DurationVector.class, + "nanos", + new ArrowType.Duration(TimeUnit.NANOSECOND), + allocator)) { nanoVector.allocateNew(); nanoVector.setNull(0); @@ -132,18 +144,20 @@ public void testNanosBasics() { nanoVector.get(0, holder); assertEquals(0, holder.isSet); nanoVector.get(1, holder); - assertEquals(1 , holder.isSet); - assertEquals(1000000 , holder.value); + assertEquals(1, holder.isSet); + assertEquals(1000000, holder.value); } } @Test public void testGetTransferPairWithField() { - final DurationVector fromVector = TestUtils.newVector(DurationVector.class, "nanos", - new ArrowType.Duration(TimeUnit.NANOSECOND), allocator); + final DurationVector fromVector = + TestUtils.newVector( + DurationVector.class, "nanos", new ArrowType.Duration(TimeUnit.NANOSECOND), allocator); final TransferPair transferPair = fromVector.getTransferPair(fromVector.getField(), allocator); final DurationVector toVector = (DurationVector) transferPair.getTo(); - // Field inside a new vector created by reusing a field should be the same in memory as the original field. + // Field inside a new vector created by reusing a field should be the same in memory as the + // original field. assertSame(fromVector.getField(), toVector.getField()); } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestFixedSizeBinaryVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestFixedSizeBinaryVector.java index b9cd89e4ad7..7330e46f7b5 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestFixedSizeBinaryVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestFixedSizeBinaryVector.java @@ -49,7 +49,8 @@ public class TestFixedSizeBinaryVector { private ArrowBuf[] bufs = new ArrowBuf[numValues]; private FixedSizeBinaryHolder[] holders = new FixedSizeBinaryHolder[numValues]; - private NullableFixedSizeBinaryHolder[] nullableHolders = new NullableFixedSizeBinaryHolder[numValues]; + private NullableFixedSizeBinaryHolder[] nullableHolders = + new NullableFixedSizeBinaryHolder[numValues]; private static byte[] smallValue; @@ -84,7 +85,6 @@ private static void failWithException(String message) throws Exception { throw new Exception(message); } - @Before public void init() throws Exception { allocator = new DirtyRootAllocator(Integer.MAX_VALUE, (byte) 100); @@ -156,9 +156,12 @@ public void testSetUsingNull() { final byte[] value = null; for (int i = 0; i < numValues; i++) { final int index = i; - Exception e = assertThrows(NullPointerException.class, () -> { - vector.set(index, value); - }); + Exception e = + assertThrows( + NullPointerException.class, + () -> { + vector.set(index, value); + }); assertEquals("expecting a valid byte array", e.getMessage()); } } @@ -281,10 +284,12 @@ public void testGetNull() { @Test public void testGetTransferPairWithField() { - final FixedSizeBinaryVector fromVector = new FixedSizeBinaryVector("fixedSizeBinary", allocator, typeWidth); + final FixedSizeBinaryVector fromVector = + new FixedSizeBinaryVector("fixedSizeBinary", allocator, typeWidth); final TransferPair transferPair = fromVector.getTransferPair(fromVector.getField(), allocator); final FixedSizeBinaryVector toVector = (FixedSizeBinaryVector) transferPair.getTo(); - // Field inside a new vector created by reusing a field should be the same in memory as the original field. + // Field inside a new vector created by reusing a field should be the same in memory as the + // original field. assertSame(fromVector.getField(), toVector.getField()); } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestFixedSizeListVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestFixedSizeListVector.java index bde6dd491dd..644f165053c 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestFixedSizeListVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestFixedSizeListVector.java @@ -28,7 +28,6 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.complex.FixedSizeListVector; import org.apache.arrow.vector.complex.ListVector; @@ -62,8 +61,10 @@ public void terminate() throws Exception { @Test public void testIntType() { - try (FixedSizeListVector vector = FixedSizeListVector.empty("list", /*size=*/2, allocator)) { - IntVector nested = (IntVector) vector.addOrGetVector(FieldType.nullable(MinorType.INT.getType())).getVector(); + try (FixedSizeListVector vector = FixedSizeListVector.empty("list", /* size= */ 2, allocator)) { + IntVector nested = + (IntVector) + vector.addOrGetVector(FieldType.nullable(MinorType.INT.getType())).getVector(); vector.allocateNew(); for (int i = 0; i < 10; i++) { @@ -89,9 +90,10 @@ public void testIntType() { @Test public void testFloatTypeNullable() { - try (FixedSizeListVector vector = FixedSizeListVector.empty("list", /*size=*/2, allocator)) { - Float4Vector nested = (Float4Vector) vector.addOrGetVector(FieldType.nullable(MinorType.FLOAT4.getType())) - .getVector(); + try (FixedSizeListVector vector = FixedSizeListVector.empty("list", /* size= */ 2, allocator)) { + Float4Vector nested = + (Float4Vector) + vector.addOrGetVector(FieldType.nullable(MinorType.FLOAT4.getType())).getVector(); vector.allocateNew(); for (int i = 0; i < 10; i++) { @@ -125,10 +127,12 @@ public void testFloatTypeNullable() { @Test public void testNestedInList() { try (ListVector vector = ListVector.empty("list", allocator)) { - FixedSizeListVector tuples = (FixedSizeListVector) vector.addOrGetVector( - FieldType.nullable(new ArrowType.FixedSizeList(2))).getVector(); - IntVector innerVector = (IntVector) tuples.addOrGetVector(FieldType.nullable(MinorType.INT.getType())) - .getVector(); + FixedSizeListVector tuples = + (FixedSizeListVector) + vector.addOrGetVector(FieldType.nullable(new ArrowType.FixedSizeList(2))).getVector(); + IntVector innerVector = + (IntVector) + tuples.addOrGetVector(FieldType.nullable(MinorType.INT.getType())).getVector(); vector.allocateNew(); for (int i = 0; i < 10; i++) { @@ -168,12 +172,18 @@ public void testNestedInList() { @Test public void testTransferPair() { - try (FixedSizeListVector from = new FixedSizeListVector( - "from", allocator, new FieldType(true, new ArrowType.FixedSizeList(2), null), null); - FixedSizeListVector to = new FixedSizeListVector( - "to", allocator, new FieldType(true, new ArrowType.FixedSizeList(2), null), null)) { - Float4Vector nested = (Float4Vector) from.addOrGetVector(FieldType.nullable(MinorType.FLOAT4.getType())) - .getVector(); + try (FixedSizeListVector from = + new FixedSizeListVector( + "from", + allocator, + new FieldType(true, new ArrowType.FixedSizeList(2), null), + null); + FixedSizeListVector to = + new FixedSizeListVector( + "to", allocator, new FieldType(true, new ArrowType.FixedSizeList(2), null), null)) { + Float4Vector nested = + (Float4Vector) + from.addOrGetVector(FieldType.nullable(MinorType.FLOAT4.getType())).getVector(); from.allocateNew(); for (int i = 0; i < 10; i++) { @@ -236,7 +246,8 @@ public void testTransferPair() { @Test public void testConsistentChildName() throws Exception { - try (FixedSizeListVector listVector = FixedSizeListVector.empty("sourceVector", /*size=*/2, allocator)) { + try (FixedSizeListVector listVector = + FixedSizeListVector.empty("sourceVector", /* size= */ 2, allocator)) { String emptyListStr = listVector.getField().toString(); Assert.assertTrue(emptyListStr.contains(ListVector.DATA_VECTOR_NAME)); @@ -252,7 +263,8 @@ public void testUnionFixedSizeListWriterWithNulls() throws Exception { * each list of size 3 and having its data values alternating between null and a non-null. * Read and verify */ - try (final FixedSizeListVector vector = FixedSizeListVector.empty("vector", /*size=*/3, allocator)) { + try (final FixedSizeListVector vector = + FixedSizeListVector.empty("vector", /* size= */ 3, allocator)) { UnionFixedSizeListWriter writer = vector.getWriter(); writer.allocate(); @@ -280,7 +292,8 @@ public void testUnionFixedSizeListWriterWithNulls() throws Exception { @Test public void testUnionFixedSizeListWriter() throws Exception { - try (final FixedSizeListVector vector1 = FixedSizeListVector.empty("vector", /*size=*/3, allocator)) { + try (final FixedSizeListVector vector1 = + FixedSizeListVector.empty("vector", /* size= */ 3, allocator)) { UnionFixedSizeListWriter writer1 = vector1.getWriter(); writer1.allocate(); @@ -289,7 +302,7 @@ public void testUnionFixedSizeListWriter() throws Exception { int[] values2 = new int[] {4, 5, 6}; int[] values3 = new int[] {7, 8, 9}; - //set some values + // set some values writeListVector(vector1, writer1, values1); writeListVector(vector1, writer1, values2); writeListVector(vector1, writer1, values3); @@ -308,7 +321,8 @@ public void testUnionFixedSizeListWriter() throws Exception { @Test public void testWriteDecimal() throws Exception { - try (final FixedSizeListVector vector = FixedSizeListVector.empty("vector", /*size=*/3, allocator)) { + try (final FixedSizeListVector vector = + FixedSizeListVector.empty("vector", /* size= */ 3, allocator)) { UnionFixedSizeListWriter writer = vector.getWriter(); writer.allocate(); @@ -336,27 +350,31 @@ public void testWriteDecimal() throws Exception { @Test public void testDecimalIndexCheck() throws Exception { - try (final FixedSizeListVector vector = FixedSizeListVector.empty("vector", /*size=*/3, allocator)) { + try (final FixedSizeListVector vector = + FixedSizeListVector.empty("vector", /* size= */ 3, allocator)) { UnionFixedSizeListWriter writer = vector.getWriter(); writer.allocate(); - IllegalStateException e = assertThrows(IllegalStateException.class, () -> { - writer.startList(); - writer.decimal().writeDecimal(new BigDecimal(1)); - writer.decimal().writeDecimal(new BigDecimal(2)); - writer.decimal().writeDecimal(new BigDecimal(3)); - writer.decimal().writeDecimal(new BigDecimal(4)); - writer.endList(); - }); + IllegalStateException e = + assertThrows( + IllegalStateException.class, + () -> { + writer.startList(); + writer.decimal().writeDecimal(new BigDecimal(1)); + writer.decimal().writeDecimal(new BigDecimal(2)); + writer.decimal().writeDecimal(new BigDecimal(3)); + writer.decimal().writeDecimal(new BigDecimal(4)); + writer.endList(); + }); assertEquals("values at index 0 is greater than listSize 3", e.getMessage()); } } - @Test(expected = IllegalStateException.class) public void testWriteIllegalData() throws Exception { - try (final FixedSizeListVector vector1 = FixedSizeListVector.empty("vector", /*size=*/3, allocator)) { + try (final FixedSizeListVector vector1 = + FixedSizeListVector.empty("vector", /* size= */ 3, allocator)) { UnionFixedSizeListWriter writer1 = vector1.getWriter(); writer1.allocate(); @@ -364,7 +382,7 @@ public void testWriteIllegalData() throws Exception { int[] values1 = new int[] {1, 2, 3}; int[] values2 = new int[] {4, 5, 6, 7, 8}; - //set some values + // set some values writeListVector(vector1, writer1, values1); writeListVector(vector1, writer1, values2); writer1.setValueCount(3); @@ -379,7 +397,8 @@ public void testWriteIllegalData() throws Exception { @Test public void testSplitAndTransfer() throws Exception { - try (final FixedSizeListVector vector1 = FixedSizeListVector.empty("vector", /*size=*/3, allocator)) { + try (final FixedSizeListVector vector1 = + FixedSizeListVector.empty("vector", /* size= */ 3, allocator)) { UnionFixedSizeListWriter writer1 = vector1.getWriter(); writer1.allocate(); @@ -388,7 +407,7 @@ public void testSplitAndTransfer() throws Exception { int[] values2 = new int[] {4, 5, 6}; int[] values3 = new int[] {7, 8, 9}; - //set some values + // set some values writeListVector(vector1, writer1, values1); writeListVector(vector1, writer1, values2); writeListVector(vector1, writer1, values3); @@ -410,7 +429,8 @@ public void testSplitAndTransfer() throws Exception { @Test public void testZeroWidthVector() { - try (final FixedSizeListVector vector1 = FixedSizeListVector.empty("vector", /*size=*/0, allocator)) { + try (final FixedSizeListVector vector1 = + FixedSizeListVector.empty("vector", /* size= */ 0, allocator)) { UnionFixedSizeListWriter writer1 = vector1.getWriter(); writer1.allocate(); @@ -420,7 +440,7 @@ public void testZeroWidthVector() { int[] values3 = null; int[] values4 = new int[] {}; - //set some values + // set some values writeListVector(vector1, writer1, values1); writeListVector(vector1, writer1, values2); writeListVector(vector1, writer1, values3); @@ -441,7 +461,8 @@ public void testZeroWidthVector() { @Test public void testVectorWithNulls() { - try (final FixedSizeListVector vector1 = FixedSizeListVector.empty("vector", /*size=*/4, allocator)) { + try (final FixedSizeListVector vector1 = + FixedSizeListVector.empty("vector", /* size= */ 4, allocator)) { UnionFixedSizeListWriter writer1 = vector1.getWriter(); writer1.allocate(); @@ -451,7 +472,7 @@ public void testVectorWithNulls() { List values3 = null; List values4 = Arrays.asList(7, 8, null, 9); - //set some values + // set some values writeListVector(vector1, writer1, values1); writeListVector(vector1, writer1, values2); writeListVector(vector1, writer1, values3); @@ -473,7 +494,8 @@ public void testVectorWithNulls() { @Test public void testWriteVarCharHelpers() throws Exception { - try (final FixedSizeListVector vector = FixedSizeListVector.empty("vector", /*size=*/4, allocator)) { + try (final FixedSizeListVector vector = + FixedSizeListVector.empty("vector", /* size= */ 4, allocator)) { UnionFixedSizeListWriter writer = vector.getWriter(); writer.allocate(); @@ -492,7 +514,8 @@ public void testWriteVarCharHelpers() throws Exception { @Test public void testWriteLargeVarCharHelpers() throws Exception { - try (final FixedSizeListVector vector = FixedSizeListVector.empty("vector", /*size=*/4, allocator)) { + try (final FixedSizeListVector vector = + FixedSizeListVector.empty("vector", /* size= */ 4, allocator)) { UnionFixedSizeListWriter writer = vector.getWriter(); writer.allocate(); @@ -511,47 +534,65 @@ public void testWriteLargeVarCharHelpers() throws Exception { @Test public void testWriteVarBinaryHelpers() throws Exception { - try (final FixedSizeListVector vector = FixedSizeListVector.empty("vector", /*size=*/4, allocator)) { + try (final FixedSizeListVector vector = + FixedSizeListVector.empty("vector", /* size= */ 4, allocator)) { UnionFixedSizeListWriter writer = vector.getWriter(); writer.allocate(); writer.startList(); writer.writeVarBinary("row1,1".getBytes(StandardCharsets.UTF_8)); - writer.writeVarBinary("row1,2".getBytes(StandardCharsets.UTF_8), 0, + writer.writeVarBinary( + "row1,2".getBytes(StandardCharsets.UTF_8), + 0, "row1,2".getBytes(StandardCharsets.UTF_8).length); writer.writeVarBinary(ByteBuffer.wrap("row1,3".getBytes(StandardCharsets.UTF_8))); - writer.writeVarBinary(ByteBuffer.wrap("row1,4".getBytes(StandardCharsets.UTF_8)), 0, + writer.writeVarBinary( + ByteBuffer.wrap("row1,4".getBytes(StandardCharsets.UTF_8)), + 0, "row1,4".getBytes(StandardCharsets.UTF_8).length); writer.endList(); - assertEquals("row1,1", new String((byte[]) vector.getObject(0).get(0), StandardCharsets.UTF_8)); - assertEquals("row1,2", new String((byte[]) vector.getObject(0).get(1), StandardCharsets.UTF_8)); - assertEquals("row1,3", new String((byte[]) vector.getObject(0).get(2), StandardCharsets.UTF_8)); - assertEquals("row1,4", new String((byte[]) vector.getObject(0).get(3), StandardCharsets.UTF_8)); + assertEquals( + "row1,1", new String((byte[]) vector.getObject(0).get(0), StandardCharsets.UTF_8)); + assertEquals( + "row1,2", new String((byte[]) vector.getObject(0).get(1), StandardCharsets.UTF_8)); + assertEquals( + "row1,3", new String((byte[]) vector.getObject(0).get(2), StandardCharsets.UTF_8)); + assertEquals( + "row1,4", new String((byte[]) vector.getObject(0).get(3), StandardCharsets.UTF_8)); } } @Test public void testWriteLargeVarBinaryHelpers() throws Exception { - try (final FixedSizeListVector vector = FixedSizeListVector.empty("vector", /*size=*/4, allocator)) { + try (final FixedSizeListVector vector = + FixedSizeListVector.empty("vector", /* size= */ 4, allocator)) { UnionFixedSizeListWriter writer = vector.getWriter(); writer.allocate(); writer.startList(); writer.writeLargeVarBinary("row1,1".getBytes(StandardCharsets.UTF_8)); - writer.writeLargeVarBinary("row1,2".getBytes(StandardCharsets.UTF_8), 0, + writer.writeLargeVarBinary( + "row1,2".getBytes(StandardCharsets.UTF_8), + 0, "row1,2".getBytes(StandardCharsets.UTF_8).length); writer.writeLargeVarBinary(ByteBuffer.wrap("row1,3".getBytes(StandardCharsets.UTF_8))); - writer.writeLargeVarBinary(ByteBuffer.wrap("row1,4".getBytes(StandardCharsets.UTF_8)), 0, + writer.writeLargeVarBinary( + ByteBuffer.wrap("row1,4".getBytes(StandardCharsets.UTF_8)), + 0, "row1,4".getBytes(StandardCharsets.UTF_8).length); writer.endList(); - assertEquals("row1,1", new String((byte[]) vector.getObject(0).get(0), StandardCharsets.UTF_8)); - assertEquals("row1,2", new String((byte[]) vector.getObject(0).get(1), StandardCharsets.UTF_8)); - assertEquals("row1,3", new String((byte[]) vector.getObject(0).get(2), StandardCharsets.UTF_8)); - assertEquals("row1,4", new String((byte[]) vector.getObject(0).get(3), StandardCharsets.UTF_8)); + assertEquals( + "row1,1", new String((byte[]) vector.getObject(0).get(0), StandardCharsets.UTF_8)); + assertEquals( + "row1,2", new String((byte[]) vector.getObject(0).get(1), StandardCharsets.UTF_8)); + assertEquals( + "row1,3", new String((byte[]) vector.getObject(0).get(2), StandardCharsets.UTF_8)); + assertEquals( + "row1,4", new String((byte[]) vector.getObject(0).get(3), StandardCharsets.UTF_8)); } } @@ -563,7 +604,8 @@ private int[] convertListToIntArray(List list) { return values; } - private void writeListVector(FixedSizeListVector vector, UnionFixedSizeListWriter writer, int[] values) { + private void writeListVector( + FixedSizeListVector vector, UnionFixedSizeListWriter writer, int[] values) { writer.startList(); if (values != null) { for (int v : values) { @@ -575,7 +617,8 @@ private void writeListVector(FixedSizeListVector vector, UnionFixedSizeListWrite writer.endList(); } - private void writeListVector(FixedSizeListVector vector, UnionFixedSizeListWriter writer, List values) { + private void writeListVector( + FixedSizeListVector vector, UnionFixedSizeListWriter writer, List values) { writer.startList(); if (values != null) { for (Integer v : values) { @@ -590,5 +633,4 @@ private void writeListVector(FixedSizeListVector vector, UnionFixedSizeListWrite } writer.endList(); } - } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestIntervalMonthDayNanoVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestIntervalMonthDayNanoVector.java index 681897b93c1..d43590e5c41 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestIntervalMonthDayNanoVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestIntervalMonthDayNanoVector.java @@ -19,10 +19,8 @@ import static org.junit.Assert.assertEquals; - import java.time.Duration; import java.time.Period; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.holders.IntervalMonthDayNanoHolder; import org.apache.arrow.vector.holders.NullableIntervalMonthDayNanoHolder; @@ -49,7 +47,8 @@ public void terminate() throws Exception { @Test public void testBasics() { - try (final IntervalMonthDayNanoVector vector = new IntervalMonthDayNanoVector(/*name=*/"", allocator)) { + try (final IntervalMonthDayNanoVector vector = + new IntervalMonthDayNanoVector(/* name= */ "", allocator)) { int valueCount = 100; vector.setInitialCapacity(valueCount); vector.allocateNew(); @@ -63,20 +62,21 @@ public void testBasics() { holder.days = Integer.MIN_VALUE; holder.nanoseconds = Long.MIN_VALUE; - - vector.set(0, /*months=*/1, /*days=*/2, /*nanoseconds=*/-2); - vector.setSafe(2, /*months=*/1, /*days=*/2, /*nanoseconds=*/-3); - vector.setSafe(/*index=*/4, nullableHolder); + vector.set(0, /* months= */ 1, /* days= */ 2, /* nanoseconds= */ -2); + vector.setSafe(2, /* months= */ 1, /* days= */ 2, /* nanoseconds= */ -3); + vector.setSafe(/* index= */ 4, nullableHolder); vector.set(3, holder); nullableHolder.isSet = 0; - vector.setSafe(/*index=*/5, nullableHolder); + vector.setSafe(/* index= */ 5, nullableHolder); vector.setValueCount(5); assertEquals("P1M2D PT-0.000000002S ", vector.getAsStringBuilder(0).toString()); assertEquals(null, vector.getAsStringBuilder(1)); assertEquals("P1M2D PT-0.000000003S ", vector.getAsStringBuilder(2).toString()); - assertEquals(new PeriodDuration(Period.of(0, Integer.MIN_VALUE, Integer.MIN_VALUE), - Duration.ofNanos(Long.MIN_VALUE)), vector.getObject(3)); + assertEquals( + new PeriodDuration( + Period.of(0, Integer.MIN_VALUE, Integer.MIN_VALUE), Duration.ofNanos(Long.MIN_VALUE)), + vector.getObject(3)); assertEquals("P2M20D PT0.000000123S ", vector.getAsStringBuilder(4).toString()); assertEquals(null, vector.getObject(5)); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestIntervalYearVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestIntervalYearVector.java index 4b2ae2eb3d4..0d7d8ca6a9e 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestIntervalYearVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestIntervalYearVector.java @@ -71,7 +71,8 @@ public void testGetTransferPairWithField() { final IntervalYearVector fromVector = new IntervalYearVector("", allocator); final TransferPair transferPair = fromVector.getTransferPair(fromVector.getField(), allocator); final IntervalYearVector toVector = (IntervalYearVector) transferPair.getTo(); - // Field inside a new vector created by reusing a field should be the same in memory as the original field. + // Field inside a new vector created by reusing a field should be the same in memory as the + // original field. assertSame(fromVector.getField(), toVector.getField()); } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestLargeListVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestLargeListVector.java index ffd87c99d50..ca76dcc848c 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestLargeListVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestLargeListVector.java @@ -26,7 +26,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.complex.BaseRepeatedValueVector; @@ -61,7 +60,7 @@ public void terminate() throws Exception { @Test public void testCopyFrom() throws Exception { try (LargeListVector inVector = LargeListVector.empty("input", allocator); - LargeListVector outVector = LargeListVector.empty("output", allocator)) { + LargeListVector outVector = LargeListVector.empty("output", allocator)) { UnionLargeListWriter writer = inVector.getWriter(); writer.allocate(); @@ -97,7 +96,6 @@ public void testCopyFrom() throws Exception { reader.setPosition(2); Assert.assertTrue("shouldn't be null", reader.isSet()); - /* index 0 */ Object result = outVector.getObject(0); ArrayList resultSet = (ArrayList) result; @@ -428,20 +426,26 @@ public void testSplitAndTransfer() throws Exception { BigIntVector dataVector1 = (BigIntVector) toVector.getDataVector(); for (int i = 0; i < splitLength; i++) { - dataLength1 = (int) offsetBuffer.getLong((start + i + 1) * LargeListVector.OFFSET_WIDTH) - - (int) offsetBuffer.getLong((start + i) * LargeListVector.OFFSET_WIDTH); - dataLength2 = (int) toOffsetBuffer.getLong((i + 1) * LargeListVector.OFFSET_WIDTH) - - (int) toOffsetBuffer.getLong(i * LargeListVector.OFFSET_WIDTH); - - assertEquals("Different data lengths at index: " + i + " and start: " + start, - dataLength1, dataLength2); + dataLength1 = + (int) offsetBuffer.getLong((start + i + 1) * LargeListVector.OFFSET_WIDTH) + - (int) offsetBuffer.getLong((start + i) * LargeListVector.OFFSET_WIDTH); + dataLength2 = + (int) toOffsetBuffer.getLong((i + 1) * LargeListVector.OFFSET_WIDTH) + - (int) toOffsetBuffer.getLong(i * LargeListVector.OFFSET_WIDTH); + + assertEquals( + "Different data lengths at index: " + i + " and start: " + start, + dataLength1, + dataLength2); offset1 = (int) offsetBuffer.getLong((start + i) * LargeListVector.OFFSET_WIDTH); offset2 = (int) toOffsetBuffer.getLong(i * LargeListVector.OFFSET_WIDTH); for (int j = 0; j < dataLength1; j++) { - assertEquals("Different data at indexes: " + offset1 + " and " + offset2, - dataVector.getObject(offset1), dataVector1.getObject(offset2)); + assertEquals( + "Different data at indexes: " + offset1 + " and " + offset2, + dataVector.getObject(offset1), + dataVector1.getObject(offset2)); offset1++; offset2++; @@ -772,8 +776,8 @@ public void testSetInitialCapacity() { vector.addOrGetVector(FieldType.nullable(MinorType.INT.getType())); /** - * use the default multiplier of 5, - * 512 * 5 => 2560 * 4 => 10240 bytes => 16KB => 4096 value capacity. + * use the default multiplier of 5, 512 * 5 => 2560 * 4 => 10240 bytes => 16KB => 4096 value + * capacity. */ vector.setInitialCapacity(512); vector.allocateNew(); @@ -787,12 +791,10 @@ public void testSetInitialCapacity() { assertTrue(vector.getDataVector().getValueCapacity() >= 512 * 4); /** - * inner value capacity we pass to data vector is 512 * 0.1 => 51 - * For an int vector this is 204 bytes of memory for data buffer - * and 7 bytes for validity buffer. - * and with power of 2 allocation, we allocate 256 bytes and 8 bytes - * for the data buffer and validity buffer of the inner vector. Thus - * value capacity of inner vector is 64 + * inner value capacity we pass to data vector is 512 * 0.1 => 51 For an int vector this is + * 204 bytes of memory for data buffer and 7 bytes for validity buffer. and with power of 2 + * allocation, we allocate 256 bytes and 8 bytes for the data buffer and validity buffer of + * the inner vector. Thus value capacity of inner vector is 64 */ vector.setInitialCapacity(512, 0.1); vector.allocateNew(); @@ -800,12 +802,10 @@ public void testSetInitialCapacity() { assertTrue(vector.getDataVector().getValueCapacity() >= 51); /** - * inner value capacity we pass to data vector is 512 * 0.01 => 5 - * For an int vector this is 20 bytes of memory for data buffer - * and 1 byte for validity buffer. - * and with power of 2 allocation, we allocate 32 bytes and 1 bytes - * for the data buffer and validity buffer of the inner vector. Thus - * value capacity of inner vector is 8 + * inner value capacity we pass to data vector is 512 * 0.01 => 5 For an int vector this is 20 + * bytes of memory for data buffer and 1 byte for validity buffer. and with power of 2 + * allocation, we allocate 32 bytes and 1 bytes for the data buffer and validity buffer of the + * inner vector. Thus value capacity of inner vector is 8 */ vector.setInitialCapacity(512, 0.01); vector.allocateNew(); @@ -813,14 +813,11 @@ public void testSetInitialCapacity() { assertTrue(vector.getDataVector().getValueCapacity() >= 5); /** - * inner value capacity we pass to data vector is 5 * 0.1 => 0 - * which is then rounded off to 1. So we pass value count as 1 - * to the inner int vector. - * the offset buffer of the list vector is allocated for 6 values - * which is 24 bytes and then rounded off to 32 bytes (8 values) - * the validity buffer of the list vector is allocated for 5 - * values which is 1 byte. This is why value capacity of the list - * vector is 7 as we take the min of validity buffer value capacity + * inner value capacity we pass to data vector is 5 * 0.1 => 0 which is then rounded off to 1. + * So we pass value count as 1 to the inner int vector. the offset buffer of the list vector + * is allocated for 6 values which is 24 bytes and then rounded off to 32 bytes (8 values) the + * validity buffer of the list vector is allocated for 5 values which is 1 byte. This is why + * value capacity of the list vector is 7 as we take the min of validity buffer value capacity * and offset buffer value capacity. */ vector.setInitialCapacity(5, 0.1); @@ -834,7 +831,8 @@ public void testSetInitialCapacity() { public void testClearAndReuse() { try (final LargeListVector vector = LargeListVector.empty("list", allocator)) { BigIntVector bigIntVector = - (BigIntVector) vector.addOrGetVector(FieldType.nullable(MinorType.BIGINT.getType())).getVector(); + (BigIntVector) + vector.addOrGetVector(FieldType.nullable(MinorType.BIGINT.getType())).getVector(); vector.setInitialCapacity(10); vector.allocateNew(); @@ -883,17 +881,23 @@ public void testWriterGetField() { UnionLargeListWriter writer = vector.getWriter(); writer.allocate(); - //set some values + // set some values writer.startList(); writer.integer().writeInt(1); writer.integer().writeInt(2); writer.endList(); vector.setValueCount(2); - Field expectedDataField = new Field(BaseRepeatedValueVector.DATA_VECTOR_NAME, - FieldType.nullable(new ArrowType.Int(32, true)), null); - Field expectedField = new Field(vector.getName(), FieldType.nullable(ArrowType.LargeList.INSTANCE), - Arrays.asList(expectedDataField)); + Field expectedDataField = + new Field( + BaseRepeatedValueVector.DATA_VECTOR_NAME, + FieldType.nullable(new ArrowType.Int(32, true)), + null); + Field expectedField = + new Field( + vector.getName(), + FieldType.nullable(ArrowType.LargeList.INSTANCE), + Arrays.asList(expectedDataField)); assertEquals(expectedField, writer.getField()); } @@ -906,7 +910,7 @@ public void testClose() throws Exception { UnionLargeListWriter writer = vector.getWriter(); writer.allocate(); - //set some values + // set some values writer.startList(); writer.integer().writeInt(1); writer.integer().writeInt(2); @@ -929,7 +933,7 @@ public void testGetBufferSizeFor() { UnionLargeListWriter writer = vector.getWriter(); writer.allocate(); - //set some values + // set some values writeIntValues(writer, new int[] {1, 2}); writeIntValues(writer, new int[] {3, 4}); writeIntValues(writer, new int[] {5, 6}); @@ -944,7 +948,10 @@ public void testGetBufferSizeFor() { int validityBufferSize = BitVectorHelper.getValidityBufferSize(valueCount); int offsetBufferSize = (valueCount + 1) * LargeListVector.OFFSET_WIDTH; - int expectedSize = validityBufferSize + offsetBufferSize + dataVector.getBufferSizeFor(indices[valueCount]); + int expectedSize = + validityBufferSize + + offsetBufferSize + + dataVector.getBufferSizeFor(indices[valueCount]); assertEquals(expectedSize, vector.getBufferSizeFor(valueCount)); } } @@ -999,24 +1006,25 @@ public void testGetTransferPairWithField() throws Exception { UnionLargeListWriter writer = fromVector.getWriter(); writer.allocate(); - //set some values + // set some values writer.startList(); writer.integer().writeInt(1); writer.integer().writeInt(2); writer.endList(); fromVector.setValueCount(2); - final TransferPair transferPair = fromVector.getTransferPair(fromVector.getField(), - allocator); + final TransferPair transferPair = + fromVector.getTransferPair(fromVector.getField(), allocator); final LargeListVector toVector = (LargeListVector) transferPair.getTo(); - // Field inside a new vector created by reusing a field should be the same in memory as the original field. + // Field inside a new vector created by reusing a field should be the same in memory as the + // original field. assertSame(toVector.getField(), fromVector.getField()); } } private void writeIntValues(UnionLargeListWriter writer, int[] values) { writer.startList(); - for (int v: values) { + for (int v : values) { writer.integer().writeInt(v); } writer.endList(); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestLargeVarBinaryVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestLargeVarBinaryVector.java index 36607903b01..fda6f3b2388 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestLargeVarBinaryVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestLargeVarBinaryVector.java @@ -25,7 +25,6 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Objects; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -74,7 +73,8 @@ public void testSetNullableLargeVarBinaryHolder() { // verify results assertTrue(vector.isNull(0)); - assertEquals(str, new String(Objects.requireNonNull(vector.get(1)), StandardCharsets.UTF_8)); + assertEquals( + str, new String(Objects.requireNonNull(vector.get(1)), StandardCharsets.UTF_8)); } } } @@ -102,7 +102,8 @@ public void testSetNullableLargeVarBinaryHolderSafe() { vector.setSafe(1, nullHolder); // verify results - assertEquals(str, new String(Objects.requireNonNull(vector.get(0)), StandardCharsets.UTF_8)); + assertEquals( + str, new String(Objects.requireNonNull(vector.get(0)), StandardCharsets.UTF_8)); assertTrue(vector.isNull(1)); } } @@ -122,12 +123,16 @@ public void testGetBytesRepeatedly() { ReusableByteArray reusableByteArray = new ReusableByteArray(); vector.read(0, reusableByteArray); byte[] oldBuffer = reusableByteArray.getBuffer(); - assertArrayEquals(str.getBytes(StandardCharsets.UTF_8), Arrays.copyOfRange(reusableByteArray.getBuffer(), - 0, (int) reusableByteArray.getLength())); + assertArrayEquals( + str.getBytes(StandardCharsets.UTF_8), + Arrays.copyOfRange( + reusableByteArray.getBuffer(), 0, (int) reusableByteArray.getLength())); vector.read(1, reusableByteArray); - assertArrayEquals(str2.getBytes(StandardCharsets.UTF_8), Arrays.copyOfRange(reusableByteArray.getBuffer(), - 0, (int) reusableByteArray.getLength())); + assertArrayEquals( + str2.getBytes(StandardCharsets.UTF_8), + Arrays.copyOfRange( + reusableByteArray.getBuffer(), 0, (int) reusableByteArray.getLength())); // There should not have been any reallocation since the newer value is smaller in length. assertSame(oldBuffer, reusableByteArray.getBuffer()); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestLargeVarCharVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestLargeVarCharVector.java index 62d09da86d6..c64a564281e 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestLargeVarCharVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestLargeVarCharVector.java @@ -28,7 +28,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.OutOfMemoryException; @@ -71,9 +70,9 @@ public void shutdown() { @Test public void testTransfer() { try (BufferAllocator childAllocator1 = allocator.newChildAllocator("child1", 1000000, 1000000); - BufferAllocator childAllocator2 = allocator.newChildAllocator("child2", 1000000, 1000000); - LargeVarCharVector v1 = new LargeVarCharVector("v1", childAllocator1); - LargeVarCharVector v2 = new LargeVarCharVector("v2", childAllocator2);) { + BufferAllocator childAllocator2 = allocator.newChildAllocator("child2", 1000000, 1000000); + LargeVarCharVector v1 = new LargeVarCharVector("v1", childAllocator1); + LargeVarCharVector v2 = new LargeVarCharVector("v2", childAllocator2); ) { v1.allocateNew(); v1.setSafe(4094, "hello world".getBytes(StandardCharsets.UTF_8), 0, 11); v1.setValueCount(4001); @@ -89,8 +88,10 @@ public void testTransfer() { @Test public void testCopyValueSafe() { - try (final LargeVarCharVector largeVarCharVector = new LargeVarCharVector("myvector", allocator); - final LargeVarCharVector newLargeVarCharVector = new LargeVarCharVector("newvector", allocator)) { + try (final LargeVarCharVector largeVarCharVector = + new LargeVarCharVector("myvector", allocator); + final LargeVarCharVector newLargeVarCharVector = + new LargeVarCharVector("newvector", allocator)) { largeVarCharVector.allocateNew(10000, 1000); final int valueCount = 500; @@ -119,7 +120,8 @@ public void testCopyValueSafe() { @Test public void testSplitAndTransferNon() { - try (final LargeVarCharVector largeVarCharVector = new LargeVarCharVector("myvector", allocator)) { + try (final LargeVarCharVector largeVarCharVector = + new LargeVarCharVector("myvector", allocator)) { largeVarCharVector.allocateNew(10000, 1000); final int valueCount = 500; @@ -136,7 +138,8 @@ public void testSplitAndTransferNon() { @Test public void testSplitAndTransferAll() { - try (final LargeVarCharVector largeVarCharVector = new LargeVarCharVector("myvector", allocator)) { + try (final LargeVarCharVector largeVarCharVector = + new LargeVarCharVector("myvector", allocator)) { largeVarCharVector.allocateNew(10000, 1000); final int valueCount = 500; @@ -153,8 +156,10 @@ public void testSplitAndTransferAll() { @Test public void testInvalidStartIndex() { - try (final LargeVarCharVector largeVarCharVector = new LargeVarCharVector("myvector", allocator); - final LargeVarCharVector newLargeVarCharVector = new LargeVarCharVector("newvector", allocator)) { + try (final LargeVarCharVector largeVarCharVector = + new LargeVarCharVector("myvector", allocator); + final LargeVarCharVector newLargeVarCharVector = + new LargeVarCharVector("newvector", allocator)) { largeVarCharVector.allocateNew(10000, 1000); final int valueCount = 500; @@ -162,9 +167,9 @@ public void testInvalidStartIndex() { final TransferPair tp = largeVarCharVector.makeTransferPair(newLargeVarCharVector); - IllegalArgumentException e = Assertions.assertThrows( - IllegalArgumentException.class, - () -> tp.splitAndTransfer(valueCount, 10)); + IllegalArgumentException e = + Assertions.assertThrows( + IllegalArgumentException.class, () -> tp.splitAndTransfer(valueCount, 10)); assertEquals("Invalid startIndex: 500", e.getMessage()); } @@ -172,8 +177,10 @@ public void testInvalidStartIndex() { @Test public void testInvalidLength() { - try (final LargeVarCharVector largeVarCharVector = new LargeVarCharVector("myvector", allocator); - final LargeVarCharVector newLargeVarCharVector = new LargeVarCharVector("newvector", allocator)) { + try (final LargeVarCharVector largeVarCharVector = + new LargeVarCharVector("myvector", allocator); + final LargeVarCharVector newLargeVarCharVector = + new LargeVarCharVector("newvector", allocator)) { largeVarCharVector.allocateNew(10000, 1000); final int valueCount = 500; @@ -181,9 +188,9 @@ public void testInvalidLength() { final TransferPair tp = largeVarCharVector.makeTransferPair(newLargeVarCharVector); - IllegalArgumentException e = Assertions.assertThrows( - IllegalArgumentException.class, - () -> tp.splitAndTransfer(0, valueCount * 2)); + IllegalArgumentException e = + Assertions.assertThrows( + IllegalArgumentException.class, () -> tp.splitAndTransfer(0, valueCount * 2)); assertEquals("Invalid length: 1000", e.getMessage()); } @@ -290,7 +297,7 @@ public void testSetLastSetUsage() { assertEquals(40, vector.offsetBuffer.getLong(17 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); assertEquals(40, vector.offsetBuffer.getLong(18 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); assertEquals(40, vector.offsetBuffer.getLong(19 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - + vector.set(19, STR6); assertArrayEquals(STR6, vector.get(19)); assertEquals(40, vector.offsetBuffer.getLong(19 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); @@ -335,7 +342,8 @@ public void testLargeVariableVectorReallocation() { @Test public void testSplitAndTransfer() { - try (final LargeVarCharVector largeVarCharVector = new LargeVarCharVector("myvector", allocator)) { + try (final LargeVarCharVector largeVarCharVector = + new LargeVarCharVector("myvector", allocator)) { largeVarCharVector.allocateNew(10000, 1000); final int valueCount = 500; @@ -496,13 +504,12 @@ public void testVectorLoadUnload() { Schema schema = new Schema(fields); - VectorSchemaRoot schemaRoot1 = new VectorSchemaRoot(schema, fieldVectors, vector1.getValueCount()); + VectorSchemaRoot schemaRoot1 = + new VectorSchemaRoot(schema, fieldVectors, vector1.getValueCount()); VectorUnloader vectorUnloader = new VectorUnloader(schemaRoot1); - try ( - ArrowRecordBatch recordBatch = vectorUnloader.getRecordBatch(); - VectorSchemaRoot schemaRoot2 = VectorSchemaRoot.create(schema, allocator); - ) { + try (ArrowRecordBatch recordBatch = vectorUnloader.getRecordBatch(); + VectorSchemaRoot schemaRoot2 = VectorSchemaRoot.create(schema, allocator); ) { VectorLoader vectorLoader = new VectorLoader(schemaRoot2); vectorLoader.load(recordBatch); @@ -598,41 +605,25 @@ public void testFillEmptiesUsage() { assertEquals(0, vector.getValueLength(14)); /* Check offsets */ - assertEquals(0, - vector.offsetBuffer.getLong(0 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - assertEquals(6, - vector.offsetBuffer.getLong(1 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - assertEquals(16, - vector.offsetBuffer.getLong(2 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - assertEquals(21, - vector.offsetBuffer.getLong(3 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - assertEquals(30, - vector.offsetBuffer.getLong(4 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - assertEquals(34, - vector.offsetBuffer.getLong(5 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - - assertEquals(40, - vector.offsetBuffer.getLong(6 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - assertEquals(40, - vector.offsetBuffer.getLong(7 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - assertEquals(40, - vector.offsetBuffer.getLong(8 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - assertEquals(40, - vector.offsetBuffer.getLong(9 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - assertEquals(40, - vector.offsetBuffer.getLong(10 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - - assertEquals(46, - vector.offsetBuffer.getLong(11 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - assertEquals(56, - vector.offsetBuffer.getLong(12 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - - assertEquals(56, - vector.offsetBuffer.getLong(13 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - assertEquals(56, - vector.offsetBuffer.getLong(14 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); - assertEquals(56, - vector.offsetBuffer.getLong(15 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + assertEquals(0, vector.offsetBuffer.getLong(0 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + assertEquals(6, vector.offsetBuffer.getLong(1 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + assertEquals(16, vector.offsetBuffer.getLong(2 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + assertEquals(21, vector.offsetBuffer.getLong(3 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + assertEquals(30, vector.offsetBuffer.getLong(4 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + assertEquals(34, vector.offsetBuffer.getLong(5 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + + assertEquals(40, vector.offsetBuffer.getLong(6 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + assertEquals(40, vector.offsetBuffer.getLong(7 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + assertEquals(40, vector.offsetBuffer.getLong(8 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + assertEquals(40, vector.offsetBuffer.getLong(9 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + assertEquals(40, vector.offsetBuffer.getLong(10 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + + assertEquals(46, vector.offsetBuffer.getLong(11 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + assertEquals(56, vector.offsetBuffer.getLong(12 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + + assertEquals(56, vector.offsetBuffer.getLong(13 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + assertEquals(56, vector.offsetBuffer.getLong(14 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); + assertEquals(56, vector.offsetBuffer.getLong(15 * BaseLargeVariableWidthVector.OFFSET_WIDTH)); } } @@ -725,8 +716,10 @@ public void testSetNullableLargeVarCharHolderSafe() { @Test public void testGetNullFromLargeVariableWidthVector() { - try (final LargeVarCharVector largeVarCharVector = new LargeVarCharVector("largevarcharvec", allocator); - final LargeVarBinaryVector largeVarBinaryVector = new LargeVarBinaryVector("largevarbinary", allocator)) { + try (final LargeVarCharVector largeVarCharVector = + new LargeVarCharVector("largevarcharvec", allocator); + final LargeVarBinaryVector largeVarBinaryVector = + new LargeVarBinaryVector("largevarbinary", allocator)) { largeVarCharVector.allocateNew(10, 1); largeVarBinaryVector.allocateNew(10, 1); @@ -737,10 +730,11 @@ public void testGetNullFromLargeVariableWidthVector() { assertNull(largeVarBinaryVector.get(0)); } } - + @Test public void testLargeVariableWidthVectorNullHashCode() { - try (LargeVarCharVector largeVarChVec = new LargeVarCharVector("large var char vector", allocator)) { + try (LargeVarCharVector largeVarChVec = + new LargeVarCharVector("large var char vector", allocator)) { largeVarChVec.allocateNew(100, 1); largeVarChVec.setValueCount(1); @@ -753,7 +747,8 @@ public void testLargeVariableWidthVectorNullHashCode() { @Test public void testUnloadLargeVariableWidthVector() { - try (final LargeVarCharVector largeVarCharVector = new LargeVarCharVector("var char", allocator)) { + try (final LargeVarCharVector largeVarCharVector = + new LargeVarCharVector("var char", allocator)) { largeVarCharVector.allocateNew(5, 2); largeVarCharVector.setValueCount(2); @@ -833,7 +828,8 @@ public void testGetTransferPairWithField() { } } - private void populateLargeVarcharVector(final LargeVarCharVector vector, int valueCount, String[] values) { + private void populateLargeVarcharVector( + final LargeVarCharVector vector, int valueCount, String[] values) { for (int i = 0; i < valueCount; i += 3) { final String s = String.format("%010d", i); vector.set(i, s.getBytes(StandardCharsets.UTF_8)); @@ -845,11 +841,13 @@ private void populateLargeVarcharVector(final LargeVarCharVector vector, int val } public static void setBytes(int index, byte[] bytes, LargeVarCharVector vector) { - final long currentOffset = vector.offsetBuffer.getLong((long) index * BaseLargeVariableWidthVector.OFFSET_WIDTH); + final long currentOffset = + vector.offsetBuffer.getLong((long) index * BaseLargeVariableWidthVector.OFFSET_WIDTH); BitVectorHelper.setBit(vector.validityBuffer, index); vector.offsetBuffer.setLong( - (long) (index + 1) * BaseLargeVariableWidthVector.OFFSET_WIDTH, currentOffset + bytes.length); + (long) (index + 1) * BaseLargeVariableWidthVector.OFFSET_WIDTH, + currentOffset + bytes.length); vector.valueBuffer.setBytes(currentOffset, bytes, 0, bytes.length); } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestListVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestListVector.java index 97f2d9fd6de..8697f925f42 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestListVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestListVector.java @@ -27,7 +27,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.util.AutoCloseables; @@ -66,7 +65,7 @@ public void terminate() throws Exception { @Test public void testCopyFrom() throws Exception { try (ListVector inVector = ListVector.empty("input", allocator); - ListVector outVector = ListVector.empty("output", allocator)) { + ListVector outVector = ListVector.empty("output", allocator)) { UnionListWriter writer = inVector.getWriter(); writer.allocate(); @@ -102,7 +101,6 @@ public void testCopyFrom() throws Exception { reader.setPosition(2); Assert.assertTrue("shouldn't be null", reader.isSet()); - /* index 0 */ Object result = outVector.getObject(0); ArrayList resultSet = (ArrayList) result; @@ -434,20 +432,26 @@ public void testSplitAndTransfer() throws Exception { BigIntVector dataVector1 = (BigIntVector) toVector.getDataVector(); for (int i = 0; i < splitLength; i++) { - dataLength1 = offsetBuffer.getInt((start + i + 1) * ListVector.OFFSET_WIDTH) - - offsetBuffer.getInt((start + i) * ListVector.OFFSET_WIDTH); - dataLength2 = toOffsetBuffer.getInt((i + 1) * ListVector.OFFSET_WIDTH) - - toOffsetBuffer.getInt(i * ListVector.OFFSET_WIDTH); - - assertEquals("Different data lengths at index: " + i + " and start: " + start, - dataLength1, dataLength2); + dataLength1 = + offsetBuffer.getInt((start + i + 1) * ListVector.OFFSET_WIDTH) + - offsetBuffer.getInt((start + i) * ListVector.OFFSET_WIDTH); + dataLength2 = + toOffsetBuffer.getInt((i + 1) * ListVector.OFFSET_WIDTH) + - toOffsetBuffer.getInt(i * ListVector.OFFSET_WIDTH); + + assertEquals( + "Different data lengths at index: " + i + " and start: " + start, + dataLength1, + dataLength2); offset1 = offsetBuffer.getInt((start + i) * ListVector.OFFSET_WIDTH); offset2 = toOffsetBuffer.getInt(i * ListVector.OFFSET_WIDTH); for (int j = 0; j < dataLength1; j++) { - assertEquals("Different data at indexes: " + offset1 + " and " + offset2, - dataVector.getObject(offset1), dataVector1.getObject(offset2)); + assertEquals( + "Different data at indexes: " + offset1 + " and " + offset2, + dataVector.getObject(offset1), + dataVector1.getObject(offset2)); offset1++; offset2++; @@ -840,7 +844,8 @@ public void testSetInitialCapacity() { public void testClearAndReuse() { try (final ListVector vector = ListVector.empty("list", allocator)) { BigIntVector bigIntVector = - (BigIntVector) vector.addOrGetVector(FieldType.nullable(MinorType.BIGINT.getType())).getVector(); + (BigIntVector) + vector.addOrGetVector(FieldType.nullable(MinorType.BIGINT.getType())).getVector(); vector.setInitialCapacity(10); vector.allocateNew(); @@ -889,17 +894,23 @@ public void testWriterGetField() { UnionListWriter writer = vector.getWriter(); writer.allocate(); - //set some values + // set some values writer.startList(); writer.integer().writeInt(1); writer.integer().writeInt(2); writer.endList(); vector.setValueCount(2); - Field expectedDataField = new Field(BaseRepeatedValueVector.DATA_VECTOR_NAME, - FieldType.nullable(new ArrowType.Int(32, true)), null); - Field expectedField = new Field(vector.getName(), FieldType.nullable(ArrowType.List.INSTANCE), - Arrays.asList(expectedDataField)); + Field expectedDataField = + new Field( + BaseRepeatedValueVector.DATA_VECTOR_NAME, + FieldType.nullable(new ArrowType.Int(32, true)), + null); + Field expectedField = + new Field( + vector.getName(), + FieldType.nullable(ArrowType.List.INSTANCE), + Arrays.asList(expectedDataField)); assertEquals(expectedField, writer.getField()); } @@ -917,10 +928,16 @@ public void testWriterGetTimestampMilliTZField() { writer.endList(); vector.setValueCount(1); - Field expectedDataField = new Field(BaseRepeatedValueVector.DATA_VECTOR_NAME, - FieldType.nullable(new ArrowType.Timestamp(TimeUnit.MILLISECOND, "UTC")), null); - Field expectedField = new Field(vector.getName(), FieldType.nullable(ArrowType.List.INSTANCE), - Arrays.asList(expectedDataField)); + Field expectedDataField = + new Field( + BaseRepeatedValueVector.DATA_VECTOR_NAME, + FieldType.nullable(new ArrowType.Timestamp(TimeUnit.MILLISECOND, "UTC")), + null); + Field expectedField = + new Field( + vector.getName(), + FieldType.nullable(ArrowType.List.INSTANCE), + Arrays.asList(expectedDataField)); assertEquals(expectedField, writer.getField()); } @@ -943,17 +960,26 @@ public void testWriterUsingHolderGetTimestampMilliTZField() { // Writing with a different timezone should throw holder.timezone = "AsdfTimeZone"; holder.value = 77777; - IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, - () -> writer.timeStampMilliTZ().write(holder)); - assertEquals("holder.timezone: AsdfTimeZone not equal to vector timezone: SomeFakeTimeZone", ex.getMessage()); + IllegalArgumentException ex = + assertThrows( + IllegalArgumentException.class, () -> writer.timeStampMilliTZ().write(holder)); + assertEquals( + "holder.timezone: AsdfTimeZone not equal to vector timezone: SomeFakeTimeZone", + ex.getMessage()); writer.endList(); vector.setValueCount(1); - Field expectedDataField = new Field(BaseRepeatedValueVector.DATA_VECTOR_NAME, - FieldType.nullable(new ArrowType.Timestamp(TimeUnit.MILLISECOND, "SomeFakeTimeZone")), null); - Field expectedField = new Field(vector.getName(), FieldType.nullable(ArrowType.List.INSTANCE), - Arrays.asList(expectedDataField)); + Field expectedDataField = + new Field( + BaseRepeatedValueVector.DATA_VECTOR_NAME, + FieldType.nullable(new ArrowType.Timestamp(TimeUnit.MILLISECOND, "SomeFakeTimeZone")), + null); + Field expectedField = + new Field( + vector.getName(), + FieldType.nullable(ArrowType.List.INSTANCE), + Arrays.asList(expectedDataField)); assertEquals(expectedField, writer.getField()); } @@ -977,17 +1003,24 @@ public void testWriterGetDurationField() { // Writing with a different unit should throw durationHolder.unit = TimeUnit.SECOND; durationHolder.value = 8888888; - IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, - () -> writer.duration().write(durationHolder)); + IllegalArgumentException ex = + assertThrows( + IllegalArgumentException.class, () -> writer.duration().write(durationHolder)); assertEquals("holder.unit: SECOND not equal to vector unit: MILLISECOND", ex.getMessage()); writer.endList(); vector.setValueCount(1); - Field expectedDataField = new Field(BaseRepeatedValueVector.DATA_VECTOR_NAME, - FieldType.nullable(new ArrowType.Duration(TimeUnit.MILLISECOND)), null); - Field expectedField = new Field(vector.getName(), FieldType.nullable(ArrowType.List.INSTANCE), - Arrays.asList(expectedDataField)); + Field expectedDataField = + new Field( + BaseRepeatedValueVector.DATA_VECTOR_NAME, + FieldType.nullable(new ArrowType.Duration(TimeUnit.MILLISECOND)), + null); + Field expectedField = + new Field( + vector.getName(), + FieldType.nullable(ArrowType.List.INSTANCE), + Arrays.asList(expectedDataField)); assertEquals(expectedField, writer.getField()); } @@ -1026,17 +1059,24 @@ public void testWriterGetFixedSizeBinaryField() throws Exception { // Writing with a different byteWidth should throw // Note just reusing the last buffer value since that won't matter here anyway binHolder.byteWidth = 3; - IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, - () -> writer.fixedSizeBinary().write(binHolder)); + IllegalArgumentException ex = + assertThrows( + IllegalArgumentException.class, () -> writer.fixedSizeBinary().write(binHolder)); assertEquals("holder.byteWidth: 3 not equal to vector byteWidth: 9", ex.getMessage()); writer.endList(); vector.setValueCount(1); - Field expectedDataField = new Field(BaseRepeatedValueVector.DATA_VECTOR_NAME, - FieldType.nullable(new ArrowType.FixedSizeBinary(byteWidth)), null); - Field expectedField = new Field(vector.getName(), FieldType.nullable(ArrowType.List.INSTANCE), - Arrays.asList(expectedDataField)); + Field expectedDataField = + new Field( + BaseRepeatedValueVector.DATA_VECTOR_NAME, + FieldType.nullable(new ArrowType.FixedSizeBinary(byteWidth)), + null); + Field expectedField = + new Field( + vector.getName(), + FieldType.nullable(ArrowType.List.INSTANCE), + Arrays.asList(expectedDataField)); assertEquals(expectedField, writer.getField()); } @@ -1050,7 +1090,7 @@ public void testClose() throws Exception { UnionListWriter writer = vector.getWriter(); writer.allocate(); - //set some values + // set some values writer.startList(); writer.integer().writeInt(1); writer.integer().writeInt(2); @@ -1073,7 +1113,7 @@ public void testGetBufferSizeFor() { UnionListWriter writer = vector.getWriter(); writer.allocate(); - //set some values + // set some values writeIntValues(writer, new int[] {1, 2}); writeIntValues(writer, new int[] {3, 4}); writeIntValues(writer, new int[] {5, 6}); @@ -1088,7 +1128,10 @@ public void testGetBufferSizeFor() { int validityBufferSize = BitVectorHelper.getValidityBufferSize(valueCount); int offsetBufferSize = (valueCount + 1) * BaseRepeatedValueVector.OFFSET_WIDTH; - int expectedSize = validityBufferSize + offsetBufferSize + dataVector.getBufferSizeFor(indices[valueCount]); + int expectedSize = + validityBufferSize + + offsetBufferSize + + dataVector.getBufferSizeFor(indices[valueCount]); assertEquals(expectedSize, vector.getBufferSizeFor(valueCount)); } } @@ -1148,17 +1191,18 @@ public void testGetTransferPairWithField() { writer.bigInt().writeBigInt(3); writer.endList(); writer.setValueCount(1); - final TransferPair transferPair = fromVector.getTransferPair(fromVector.getField(), - allocator); + final TransferPair transferPair = + fromVector.getTransferPair(fromVector.getField(), allocator); final ListVector toVector = (ListVector) transferPair.getTo(); - // Field inside a new vector created by reusing a field should be the same in memory as the original field. + // Field inside a new vector created by reusing a field should be the same in memory as the + // original field. assertSame(toVector.getField(), fromVector.getField()); } } private void writeIntValues(UnionListWriter writer, int[] values) { writer.startList(); - for (int v: values) { + for (int v : values) { writer.integer().writeInt(v); } writer.endList(); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestMapVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestMapVector.java index 43f4c3b536f..a4cfd76e456 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestMapVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestMapVector.java @@ -25,7 +25,6 @@ import java.util.ArrayList; import java.util.Map; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.complex.MapVector; @@ -152,7 +151,7 @@ public void testBasicOperationNulls() { @Test public void testCopyFrom() throws Exception { try (MapVector inVector = MapVector.empty("input", allocator, false); - MapVector outVector = MapVector.empty("output", allocator, false)) { + MapVector outVector = MapVector.empty("output", allocator, false)) { UnionMapWriter writer = inVector.getWriter(); writer.allocate(); @@ -200,7 +199,6 @@ public void testCopyFrom() throws Exception { reader.setPosition(2); assertTrue("shouldn't be null", reader.isSet()); - /* index 0 */ Object result = outVector.getObject(0); ArrayList resultSet = (ArrayList) result; @@ -455,20 +453,26 @@ public void testSplitAndTransfer() throws Exception { StructVector dataVector1 = (StructVector) toVector.getDataVector(); for (int i = 0; i < splitLength; i++) { - dataLength1 = offsetBuffer.getInt((start + i + 1) * MapVector.OFFSET_WIDTH) - - offsetBuffer.getInt((start + i) * MapVector.OFFSET_WIDTH); - dataLength2 = toOffsetBuffer.getInt((i + 1) * MapVector.OFFSET_WIDTH) - - toOffsetBuffer.getInt(i * MapVector.OFFSET_WIDTH); - - assertEquals("Different data lengths at index: " + i + " and start: " + start, - dataLength1, dataLength2); + dataLength1 = + offsetBuffer.getInt((start + i + 1) * MapVector.OFFSET_WIDTH) + - offsetBuffer.getInt((start + i) * MapVector.OFFSET_WIDTH); + dataLength2 = + toOffsetBuffer.getInt((i + 1) * MapVector.OFFSET_WIDTH) + - toOffsetBuffer.getInt(i * MapVector.OFFSET_WIDTH); + + assertEquals( + "Different data lengths at index: " + i + " and start: " + start, + dataLength1, + dataLength2); offset1 = offsetBuffer.getInt((start + i) * MapVector.OFFSET_WIDTH); offset2 = toOffsetBuffer.getInt(i * MapVector.OFFSET_WIDTH); for (int j = 0; j < dataLength1; j++) { - assertEquals("Different data at indexes: " + offset1 + " and " + offset2, - dataVector.getObject(offset1), dataVector1.getObject(offset2)); + assertEquals( + "Different data at indexes: " + offset1 + " and " + offset2, + dataVector.getObject(offset1), + dataVector1.getObject(offset2)); offset1++; offset2++; @@ -806,7 +810,8 @@ public void testMapWithMapKeyAndMapValue() throws Exception { // populate map vector with the following two records // [ // [[5: 10, 20: 40]:[50: 100, 200: 400], [50: 100]:[75: 175, 150: 250]], - // [[1: 2]:[10: 20], [30: 40]:[15: 20], [50: 60, 70: null]:[25: 30, 35: null], [5: null]: null] + // [[1: 2]:[10: 20], [30: 40]:[15: 20], [50: 60, 70: null]:[25: 30, 35: null], [5: null]: + // null] // ] mapWriter.setPosition(0); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestNullCheckingForGet.java b/java/vector/src/test/java/org/apache/arrow/vector/TestNullCheckingForGet.java index f1345e88ab8..ae4326c0b4f 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestNullCheckingForGet.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestNullCheckingForGet.java @@ -19,17 +19,15 @@ import java.lang.reflect.Field; import java.net.URLClassLoader; - import org.junit.Assert; import org.junit.Test; -/** - * Test cases for {@link NullCheckingForGet}. - */ +/** Test cases for {@link NullCheckingForGet}. */ public class TestNullCheckingForGet { /** * Get a copy of the current class loader. + * * @return the newly created class loader. */ private ClassLoader copyClassLoader() { @@ -44,7 +42,8 @@ private ClassLoader copyClassLoader() { } /** - * Get the value of flag {@link NullCheckingForGet#NULL_CHECKING_ENABLED}. + * Get the value of flag {@link NullCheckingForGet#NULL_CHECKING_ENABLED}. + * * @param classLoader the class loader from which to get the flag value. * @return value of the flag. */ @@ -55,8 +54,8 @@ private boolean getFlagValue(ClassLoader classLoader) throws Exception { } /** - * Ensure the flag for null checking is enabled by default. - * This will protect users from JVM crashes. + * Ensure the flag for null checking is enabled by default. This will protect users from JVM + * crashes. */ @Test public void testDefaultValue() throws Exception { @@ -69,6 +68,7 @@ public void testDefaultValue() throws Exception { /** * Test setting the null checking flag by the system property. + * * @throws Exception if loading class {@link NullCheckingForGet#NULL_CHECKING_ENABLED} fails. */ @Test diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestOutOfMemoryForValueVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestOutOfMemoryForValueVector.java index 7f26b5c1b79..5b58b4d2e82 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestOutOfMemoryForValueVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestOutOfMemoryForValueVector.java @@ -24,9 +24,7 @@ import org.junit.Before; import org.junit.Test; -/** - * This class tests cases where we expect to receive {@link OutOfMemoryException}. - */ +/** This class tests cases where we expect to receive {@link OutOfMemoryException}. */ public class TestOutOfMemoryForValueVector { private static final String EMPTY_SCHEMA_PATH = ""; diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestOversizedAllocationForValueVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestOversizedAllocationForValueVector.java index 23414e9f5df..a88cda8b7c2 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestOversizedAllocationForValueVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestOversizedAllocationForValueVector.java @@ -29,9 +29,10 @@ import org.junit.Test; /** - * This class tests that OversizedAllocationException occurs when a large memory is allocated for a vector. - * Typically, arrow allows the allocation of the size of at most Integer.MAX_VALUE, but this might cause OOM in tests. - * Thus, the max allocation size is limited to 1 KB in this class. Please see the surefire option in pom.xml. + * This class tests that OversizedAllocationException occurs when a large memory is allocated for a + * vector. Typically, arrow allows the allocation of the size of at most Integer.MAX_VALUE, but this + * might cause OOM in tests. Thus, the max allocation size is limited to 1 KB in this class. Please + * see the surefire option in pom.xml. */ public class TestOversizedAllocationForValueVector { @@ -102,7 +103,6 @@ public void testBitVectorReallocation() { } } - @Test(expected = OversizedAllocationException.class) public void testVariableVectorReallocation() { final VarCharVector vector = new VarCharVector(EMPTY_SCHEMA_PATH, allocator); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestPeriodDuration.java b/java/vector/src/test/java/org/apache/arrow/vector/TestPeriodDuration.java index c8965dec3b8..1994ea38a92 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestPeriodDuration.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestPeriodDuration.java @@ -22,7 +22,6 @@ import java.time.Duration; import java.time.Period; - import org.junit.Test; public class TestPeriodDuration { @@ -42,5 +41,4 @@ public void testBasics() { assertNotEquals(pd1, pd3); assertNotEquals(pd1.hashCode(), pd3.hashCode()); } - } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestSplitAndTransfer.java b/java/vector/src/test/java/org/apache/arrow/vector/TestSplitAndTransfer.java index 3580a321f01..ee83a7db628 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestSplitAndTransfer.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestSplitAndTransfer.java @@ -25,7 +25,6 @@ import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.complex.FixedSizeListVector; @@ -49,13 +48,14 @@ public class TestSplitAndTransfer { public void init() { allocator = new RootAllocator(Long.MAX_VALUE); } - + @After public void terminate() throws Exception { allocator.close(); } - private void populateVarcharVector(final VarCharVector vector, int valueCount, String[] compareArray) { + private void populateVarcharVector( + final VarCharVector vector, int valueCount, String[] compareArray) { for (int i = 0; i < valueCount; i += 3) { final String s = String.format("%010d", i); vector.set(i, s.getBytes(StandardCharsets.UTF_8)); @@ -65,21 +65,21 @@ private void populateVarcharVector(final VarCharVector vector, int valueCount, S } vector.setValueCount(valueCount); } - + @Test /* VarCharVector */ public void test() throws Exception { try (final VarCharVector varCharVector = new VarCharVector("myvector", allocator)) { varCharVector.allocateNew(10000, 1000); - + final int valueCount = 500; final String[] compareArray = new String[valueCount]; - + populateVarcharVector(varCharVector, valueCount, compareArray); - + final TransferPair tp = varCharVector.getTransferPair(allocator); final VarCharVector newVarCharVector = (VarCharVector) tp.getTo(); final int[][] startLengths = {{0, 201}, {201, 0}, {201, 200}, {401, 99}}; - + for (final int[] startLength : startLengths) { final int start = startLength[0]; final int length = startLength[1]; @@ -157,7 +157,7 @@ public void testTransfer() { @Test public void testCopyValueSafe() { try (final VarCharVector varCharVector = new VarCharVector("myvector", allocator); - final VarCharVector newVarCharVector = new VarCharVector("newvector", allocator)) { + final VarCharVector newVarCharVector = new VarCharVector("newvector", allocator)) { varCharVector.allocateNew(10000, 1000); final int valueCount = 500; @@ -231,11 +231,12 @@ public void testInvalidStartIndex() { final TransferPair tp = varCharVector.makeTransferPair(newVarCharVector); - IllegalArgumentException e = Assertions.assertThrows( - IllegalArgumentException.class, - () -> tp.splitAndTransfer(valueCount, 10)); + IllegalArgumentException e = + Assertions.assertThrows( + IllegalArgumentException.class, () -> tp.splitAndTransfer(valueCount, 10)); - assertEquals("Invalid parameters startIndex: 500, length: 10 for valueCount: 500", e.getMessage()); + assertEquals( + "Invalid parameters startIndex: 500, length: 10 for valueCount: 500", e.getMessage()); newVarCharVector.clear(); } @@ -252,11 +253,12 @@ public void testInvalidLength() { final TransferPair tp = varCharVector.makeTransferPair(newVarCharVector); - IllegalArgumentException e = Assertions.assertThrows( - IllegalArgumentException.class, - () -> tp.splitAndTransfer(0, valueCount * 2)); + IllegalArgumentException e = + Assertions.assertThrows( + IllegalArgumentException.class, () -> tp.splitAndTransfer(0, valueCount * 2)); - assertEquals("Invalid parameters startIndex: 0, length: 1000 for valueCount: 500", e.getMessage()); + assertEquals( + "Invalid parameters startIndex: 0, length: 1000 for valueCount: 500", e.getMessage()); newVarCharVector.clear(); } @@ -265,7 +267,7 @@ public void testInvalidLength() { @Test public void testZeroStartIndexAndLength() { try (final VarCharVector varCharVector = new VarCharVector("myvector", allocator); - final VarCharVector newVarCharVector = new VarCharVector("newvector", allocator)) { + final VarCharVector newVarCharVector = new VarCharVector("newvector", allocator)) { varCharVector.allocateNew(0, 0); final int valueCount = 0; @@ -283,7 +285,7 @@ public void testZeroStartIndexAndLength() { @Test public void testZeroLength() { try (final VarCharVector varCharVector = new VarCharVector("myvector", allocator); - final VarCharVector newVarCharVector = new VarCharVector("newvector", allocator)) { + final VarCharVector newVarCharVector = new VarCharVector("newvector", allocator)) { varCharVector.allocateNew(10000, 1000); final int valueCount = 500; @@ -301,7 +303,7 @@ public void testZeroLength() { @Test public void testUnionVectorZeroStartIndexAndLength() { try (final UnionVector unionVector = UnionVector.empty("myvector", allocator); - final UnionVector newUnionVector = UnionVector.empty("newvector", allocator)) { + final UnionVector newUnionVector = UnionVector.empty("newvector", allocator)) { unionVector.allocateNew(); final int valueCount = 0; @@ -319,7 +321,7 @@ public void testUnionVectorZeroStartIndexAndLength() { @Test public void testFixedWidthVectorZeroStartIndexAndLength() { try (final IntVector intVector = new IntVector("myvector", allocator); - final IntVector newIntVector = new IntVector("newvector", allocator)) { + final IntVector newIntVector = new IntVector("newvector", allocator)) { intVector.allocateNew(0); final int valueCount = 0; @@ -337,7 +339,7 @@ public void testFixedWidthVectorZeroStartIndexAndLength() { @Test public void testBitVectorZeroStartIndexAndLength() { try (final BitVector bitVector = new BitVector("myvector", allocator); - final BitVector newBitVector = new BitVector("newvector", allocator)) { + final BitVector newBitVector = new BitVector("newvector", allocator)) { bitVector.allocateNew(0); final int valueCount = 0; @@ -355,7 +357,8 @@ public void testBitVectorZeroStartIndexAndLength() { @Test public void testFixedSizeListVectorZeroStartIndexAndLength() { try (final FixedSizeListVector listVector = FixedSizeListVector.empty("list", 4, allocator); - final FixedSizeListVector newListVector = FixedSizeListVector.empty("newList", 4, allocator)) { + final FixedSizeListVector newListVector = + FixedSizeListVector.empty("newList", 4, allocator)) { listVector.allocateNew(); final int valueCount = 0; @@ -373,7 +376,7 @@ public void testFixedSizeListVectorZeroStartIndexAndLength() { @Test public void testListVectorZeroStartIndexAndLength() { try (final ListVector listVector = ListVector.empty("list", allocator); - final ListVector newListVector = ListVector.empty("newList", allocator)) { + final ListVector newListVector = ListVector.empty("newList", allocator)) { listVector.allocateNew(); final int valueCount = 0; @@ -394,7 +397,8 @@ public void testStructVectorZeroStartIndexAndLength() { metadata.put("k1", "v1"); FieldType type = new FieldType(true, Struct.INSTANCE, null, metadata); try (final StructVector structVector = new StructVector("structvec", allocator, type, null); - final StructVector newStructVector = new StructVector("newStructvec", allocator, type, null)) { + final StructVector newStructVector = + new StructVector("newStructvec", allocator, type, null)) { structVector.allocateNew(); final int valueCount = 0; @@ -415,7 +419,7 @@ public void testMapVectorZeroStartIndexAndLength() { metadata.put("k1", "v1"); FieldType type = new FieldType(true, new ArrowType.Map(false), null, metadata); try (final MapVector mapVector = new MapVector("mapVec", allocator, type, null); - final MapVector newMapVector = new MapVector("newMapVec", allocator, type, null)) { + final MapVector newMapVector = new MapVector("newMapVec", allocator, type, null)) { mapVector.allocateNew(); final int valueCount = 0; @@ -429,5 +433,4 @@ public void testMapVectorZeroStartIndexAndLength() { newMapVector.clear(); } } - } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestStructVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestStructVector.java index 68f5e14dabb..c1fe07ed9a8 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestStructVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestStructVector.java @@ -23,7 +23,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.complex.AbstractStructVector; import org.apache.arrow.vector.complex.ListVector; @@ -71,11 +70,12 @@ public void testFieldMetadata() throws Exception { @Test public void testMakeTransferPair() { try (final StructVector s1 = StructVector.empty("s1", allocator); - final StructVector s2 = StructVector.empty("s2", allocator)) { + final StructVector s2 = StructVector.empty("s2", allocator)) { s1.addOrGet("struct_child", FieldType.nullable(MinorType.INT.getType()), IntVector.class); s1.makeTransferPair(s2); final FieldVector child = s1.getChild("struct_child"); - final FieldVector toChild = s2.addOrGet("struct_child", child.getField().getFieldType(), child.getClass()); + final FieldVector toChild = + s2.addOrGet("struct_child", child.getField().getFieldType(), child.getClass()); assertEquals(0, toChild.getValueCapacity()); assertEquals(0, toChild.getDataBuffer().capacity()); assertEquals(0, toChild.getValidityBuffer().capacity()); @@ -157,7 +157,8 @@ public void testGetPrimitiveVectors() { unionVector.addVector(new SmallIntVector("smallInt", allocator)); // add varchar vector - vector.addOrGet("varchar", FieldType.nullable(MinorType.VARCHAR.getType()), VarCharVector.class); + vector.addOrGet( + "varchar", FieldType.nullable(MinorType.VARCHAR.getType()), VarCharVector.class); List primitiveVectors = vector.getPrimitiveVectors(); assertEquals(4, primitiveVectors.size()); @@ -192,8 +193,14 @@ public void testAddOrGetComplexChildVectors() { @Test public void testAddChildVectorsWithDuplicatedFieldNamesForConflictPolicyAppend() { final FieldType type = new FieldType(true, Struct.INSTANCE, null, null); - try (StructVector vector = new StructVector("struct", allocator, type, null, - AbstractStructVector.ConflictPolicy.CONFLICT_APPEND, true)) { + try (StructVector vector = + new StructVector( + "struct", + allocator, + type, + null, + AbstractStructVector.ConflictPolicy.CONFLICT_APPEND, + true)) { final List initFields = new ArrayList<>(); // Add a bit more fields to test against stability of the internal field @@ -245,8 +252,14 @@ public void testAddChildVectorsWithDuplicatedFieldNamesForConflictPolicyAppend() @Test public void testAddChildVectorsWithDuplicatedFieldNamesForConflictPolicyReplace() { final FieldType type = new FieldType(true, Struct.INSTANCE, null, null); - try (StructVector vector = new StructVector("struct", allocator, type, null, - AbstractStructVector.ConflictPolicy.CONFLICT_REPLACE, true)) { + try (StructVector vector = + new StructVector( + "struct", + allocator, + type, + null, + AbstractStructVector.ConflictPolicy.CONFLICT_REPLACE, + true)) { final List initFields = new ArrayList<>(); // Add a bit more fields to test against stability of the internal field @@ -301,7 +314,8 @@ public void testGetTransferPair() { try (final StructVector fromVector = simpleStructVector("s1", allocator)) { TransferPair tp = fromVector.getTransferPair(fromVector.getField(), allocator); final StructVector toVector = (StructVector) tp.getTo(); - // Field inside a new vector created by reusing a field should be the same in memory as the original field. + // Field inside a new vector created by reusing a field should be the same in memory as the + // original field. assertSame(toVector.getField(), fromVector.getField()); toVector.clear(); } @@ -313,7 +327,8 @@ public void testGetTransferPairWithFieldAndCallBack() { try (final StructVector fromVector = simpleStructVector("s1", allocator)) { TransferPair tp = fromVector.getTransferPair(fromVector.getField(), allocator, callBack); final StructVector toVector = (StructVector) tp.getTo(); - // Field inside a new vector created by reusing a field should be the same in memory as the original field. + // Field inside a new vector created by reusing a field should be the same in memory as the + // original field. assertSame(toVector.getField(), fromVector.getField()); toVector.clear(); } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestTypeLayout.java b/java/vector/src/test/java/org/apache/arrow/vector/TestTypeLayout.java index 97930f433d3..0750b483d92 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestTypeLayout.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestTypeLayout.java @@ -32,67 +32,108 @@ public class TestTypeLayout { @Test public void testTypeBufferCount() { ArrowType type = new ArrowType.Int(8, true); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Union(UnionMode.Sparse, new int[2]); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Union(UnionMode.Dense, new int[1]); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Struct(); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Timestamp(TimeUnit.MILLISECOND, null); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.List(); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.FixedSizeList(5); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Map(false); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Decimal(10, 10, 128); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Decimal(10, 10, 256); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); - + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.FixedSizeBinary(5); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Bool(); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Binary(); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Utf8(); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Null(); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Date(DateUnit.DAY); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Time(TimeUnit.MILLISECOND, 32); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Interval(IntervalUnit.DAY_TIME); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); type = new ArrowType.Duration(TimeUnit.MILLISECOND); - assertEquals(TypeLayout.getTypeBufferCount(type), TypeLayout.getTypeLayout(type).getBufferLayouts().size()); + assertEquals( + TypeLayout.getTypeBufferCount(type), + TypeLayout.getTypeLayout(type).getBufferLayouts().size()); } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestUnionVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestUnionVector.java index 1b0387feb73..cb794b5ae04 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestUnionVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestUnionVector.java @@ -27,7 +27,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.complex.MapVector; @@ -71,7 +70,8 @@ public void testUnionVector() throws Exception { uInt4Holder.isSet = 1; try (UnionVector unionVector = - new UnionVector(EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { + new UnionVector( + EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { unionVector.allocateNew(); // write some data @@ -99,7 +99,8 @@ public void testUnionVector() throws Exception { @Test public void testUnionVectorMapValue() throws Exception { try (UnionVector unionVector = - new UnionVector(EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { + new UnionVector( + EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { unionVector.allocateNew(); UnionWriter writer = (UnionWriter) unionVector.getWriter(); @@ -161,7 +162,8 @@ public void testUnionVectorMapValue() throws Exception { @Test public void testTransfer() throws Exception { try (UnionVector srcVector = - new UnionVector(EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { + new UnionVector( + EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { srcVector.allocateNew(); // write some data @@ -176,7 +178,8 @@ public void testTransfer() throws Exception { srcVector.setValueCount(6); try (UnionVector destVector = - new UnionVector(EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { + new UnionVector( + EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { TransferPair pair = srcVector.makeTransferPair(destVector); // Creating the transfer should transfer the type of the field at least. @@ -212,7 +215,8 @@ public void testTransfer() throws Exception { @Test public void testSplitAndTransfer() throws Exception { try (UnionVector sourceVector = - new UnionVector(EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { + new UnionVector( + EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { sourceVector.allocateNew(); @@ -263,17 +267,12 @@ public void testSplitAndTransfer() throws Exception { assertEquals(50, sourceVector.getObject(9)); try (UnionVector toVector = - new UnionVector(EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { + new UnionVector( + EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { final TransferPair transferPair = sourceVector.makeTransferPair(toVector); - final int[][] transferLengths = {{0, 3}, - {3, 1}, - {4, 2}, - {6, 1}, - {7, 1}, - {8, 2} - }; + final int[][] transferLengths = {{0, 3}, {3, 1}, {4, 2}, {6, 1}, {7, 1}, {8, 2}}; for (final int[] transferLength : transferLengths) { final int start = transferLength[0]; @@ -283,7 +282,9 @@ public void testSplitAndTransfer() throws Exception { /* check the toVector output after doing the splitAndTransfer */ for (int i = 0; i < length; i++) { - assertEquals("Different data at indexes: " + (start + i) + "and " + i, sourceVector.getObject(start + i), + assertEquals( + "Different data at indexes: " + (start + i) + "and " + i, + sourceVector.getObject(start + i), toVector.getObject(i)); } } @@ -294,7 +295,8 @@ public void testSplitAndTransfer() throws Exception { @Test public void testSplitAndTransferWithMixedVectors() throws Exception { try (UnionVector sourceVector = - new UnionVector(EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { + new UnionVector( + EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { sourceVector.allocateNew(); @@ -354,16 +356,12 @@ public void testSplitAndTransferWithMixedVectors() throws Exception { assertEquals(30.5f, sourceVector.getObject(9)); try (UnionVector toVector = - new UnionVector(EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { + new UnionVector( + EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { final TransferPair transferPair = sourceVector.makeTransferPair(toVector); - final int[][] transferLengths = {{0, 2}, - {2, 1}, - {3, 2}, - {5, 3}, - {8, 2} - }; + final int[][] transferLengths = {{0, 2}, {2, 1}, {3, 2}, {5, 3}, {8, 2}}; for (final int[] transferLength : transferLengths) { final int start = transferLength[0]; @@ -373,7 +371,10 @@ public void testSplitAndTransferWithMixedVectors() throws Exception { /* check the toVector output after doing the splitAndTransfer */ for (int i = 0; i < length; i++) { - assertEquals("Different values at index: " + i, sourceVector.getObject(start + i), toVector.getObject(i)); + assertEquals( + "Different values at index: " + i, + sourceVector.getObject(start + i), + toVector.getObject(i)); } } } @@ -393,8 +394,12 @@ public void testGetFieldTypeInfo() throws Exception { children.add(new Field("int", FieldType.nullable(MinorType.INT.getType()), null)); children.add(new Field("varchar", FieldType.nullable(MinorType.VARCHAR.getType()), null)); - final FieldType fieldType = new FieldType(false, new ArrowType.Union(UnionMode.Sparse, typeIds), - /*dictionary=*/null, metadata); + final FieldType fieldType = + new FieldType( + false, + new ArrowType.Union(UnionMode.Sparse, typeIds), + /* dictionary= */ null, + metadata); final Field field = new Field("union", fieldType, children); MinorType minorType = MinorType.UNION; @@ -420,7 +425,8 @@ public void testGetFieldTypeInfo() throws Exception { @Test public void testGetBufferAddress() throws Exception { try (UnionVector vector = - new UnionVector(EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { + new UnionVector( + EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { boolean error = false; vector.allocateNew(); @@ -453,7 +459,6 @@ public void testGetBufferAddress() throws Exception { List buffers = vector.getFieldBuffers(); - try { vector.getOffsetBufferAddress(); } catch (UnsupportedOperationException ue) { @@ -478,7 +483,8 @@ public void testGetBufferAddress() throws Exception { @Test public void testSetGetNull() { try (UnionVector srcVector = - new UnionVector(EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { + new UnionVector( + EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { srcVector.allocateNew(); final NullableIntHolder holder = new NullableIntHolder(); @@ -501,22 +507,30 @@ public void testSetGetNull() { @Test public void testCreateNewVectorWithoutTypeExceptionThrown() { try (UnionVector vector = - new UnionVector(EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { - IllegalArgumentException e1 = assertThrows(IllegalArgumentException.class, - () -> vector.getTimeStampMilliTZVector()); - assertEquals("No TimeStampMilliTZ present. Provide ArrowType argument to create a new vector", e1.getMessage()); - - IllegalArgumentException e2 = assertThrows(IllegalArgumentException.class, - () -> vector.getDurationVector()); - assertEquals("No Duration present. Provide ArrowType argument to create a new vector", e2.getMessage()); - - IllegalArgumentException e3 = assertThrows(IllegalArgumentException.class, - () -> vector.getFixedSizeBinaryVector()); - assertEquals("No FixedSizeBinary present. Provide ArrowType argument to create a new vector", e3.getMessage()); - - IllegalArgumentException e4 = assertThrows(IllegalArgumentException.class, - () -> vector.getDecimalVector()); - assertEquals("No Decimal present. Provide ArrowType argument to create a new vector", e4.getMessage()); + new UnionVector( + EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { + IllegalArgumentException e1 = + assertThrows(IllegalArgumentException.class, () -> vector.getTimeStampMilliTZVector()); + assertEquals( + "No TimeStampMilliTZ present. Provide ArrowType argument to create a new vector", + e1.getMessage()); + + IllegalArgumentException e2 = + assertThrows(IllegalArgumentException.class, () -> vector.getDurationVector()); + assertEquals( + "No Duration present. Provide ArrowType argument to create a new vector", + e2.getMessage()); + + IllegalArgumentException e3 = + assertThrows(IllegalArgumentException.class, () -> vector.getFixedSizeBinaryVector()); + assertEquals( + "No FixedSizeBinary present. Provide ArrowType argument to create a new vector", + e3.getMessage()); + + IllegalArgumentException e4 = + assertThrows(IllegalArgumentException.class, () -> vector.getDecimalVector()); + assertEquals( + "No Decimal present. Provide ArrowType argument to create a new vector", e4.getMessage()); } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestUtils.java b/java/vector/src/test/java/org/apache/arrow/vector/TestUtils.java index 7e64dd38646..d7cc59b4e23 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestUtils.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestUtils.java @@ -34,12 +34,13 @@ public static VarBinaryVector newVarBinaryVector(String name, BufferAllocator al FieldType.nullable(new ArrowType.Binary()).createNewSingleVector(name, allocator, null); } - public static T newVector(Class c, String name, ArrowType type, BufferAllocator allocator) { + public static T newVector( + Class c, String name, ArrowType type, BufferAllocator allocator) { return c.cast(FieldType.nullable(type).createNewSingleVector(name, allocator, null)); } - public static T newVector(Class c, String name, MinorType type, BufferAllocator allocator) { + public static T newVector( + Class c, String name, MinorType type, BufferAllocator allocator) { return c.cast(FieldType.nullable(type.getType()).createNewSingleVector(name, allocator, null)); } - } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestValueVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestValueVector.java index 614aff18d45..b1a34ddf332 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestValueVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestValueVector.java @@ -35,7 +35,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -195,10 +194,10 @@ public void testFixedType1() { @Test public void testNoOverFlowWithUINT() { try (final UInt8Vector uInt8Vector = new UInt8Vector("uint8", allocator); - final UInt4Vector uInt4Vector = new UInt4Vector("uint4", allocator); - final UInt1Vector uInt1Vector = new UInt1Vector("uint1", allocator)) { + final UInt4Vector uInt4Vector = new UInt4Vector("uint4", allocator); + final UInt1Vector uInt1Vector = new UInt1Vector("uint1", allocator)) { - long[] longValues = new long[]{Long.MIN_VALUE, Long.MAX_VALUE, -1L}; + long[] longValues = new long[] {Long.MIN_VALUE, Long.MAX_VALUE, -1L}; uInt8Vector.allocateNew(3); uInt8Vector.setValueCount(3); for (int i = 0; i < longValues.length; i++) { @@ -207,7 +206,7 @@ public void testNoOverFlowWithUINT() { assertEquals(readValue, longValues[i]); } - int[] intValues = new int[]{Integer.MIN_VALUE, Integer.MAX_VALUE, -1}; + int[] intValues = new int[] {Integer.MIN_VALUE, Integer.MAX_VALUE, -1}; uInt4Vector.allocateNew(3); uInt4Vector.setValueCount(3); for (int i = 0; i < intValues.length; i++) { @@ -216,7 +215,7 @@ public void testNoOverFlowWithUINT() { assertEquals(intValues[i], actualValue); } - byte[] byteValues = new byte[]{Byte.MIN_VALUE, Byte.MAX_VALUE, -1}; + byte[] byteValues = new byte[] {Byte.MIN_VALUE, Byte.MAX_VALUE, -1}; uInt1Vector.allocateNew(3); uInt1Vector.setValueCount(3); for (int i = 0; i < byteValues.length; i++) { @@ -534,8 +533,9 @@ public void testFixedType4() { public void testNullableFixedType1() { // Create a new value vector for 1024 integers. - try (final UInt4Vector vector = newVector(UInt4Vector.class, EMPTY_SCHEMA_PATH, new ArrowType.Int(32, false), - allocator);) { + try (final UInt4Vector vector = + newVector( + UInt4Vector.class, EMPTY_SCHEMA_PATH, new ArrowType.Int(32, false), allocator); ) { boolean error = false; int initialCapacity = 1024; @@ -630,7 +630,8 @@ public void testNullableFixedType1() { @Test /* Float4Vector */ public void testNullableFixedType2() { // Create a new value vector for 1024 integers - try (final Float4Vector vector = newVector(Float4Vector.class, EMPTY_SCHEMA_PATH, MinorType.FLOAT4, allocator);) { + try (final Float4Vector vector = + newVector(Float4Vector.class, EMPTY_SCHEMA_PATH, MinorType.FLOAT4, allocator); ) { boolean error = false; int initialCapacity = 16; @@ -729,7 +730,8 @@ public void testNullableFixedType2() { @Test /* IntVector */ public void testNullableFixedType3() { // Create a new value vector for 1024 integers - try (final IntVector vector = newVector(IntVector.class, EMPTY_SCHEMA_PATH, MinorType.INT, allocator)) { + try (final IntVector vector = + newVector(IntVector.class, EMPTY_SCHEMA_PATH, MinorType.INT, allocator)) { int initialCapacity = 1024; /* no memory allocation has happened yet so capacity of underlying buffer should be 0 */ @@ -818,7 +820,8 @@ public void testNullableFixedType3() { @Test /* IntVector */ public void testNullableFixedType4() { - try (final IntVector vector = newVector(IntVector.class, EMPTY_SCHEMA_PATH, MinorType.INT, allocator)) { + try (final IntVector vector = + newVector(IntVector.class, EMPTY_SCHEMA_PATH, MinorType.INT, allocator)) { /* no memory allocation has happened yet */ assertEquals(0, vector.getValueCapacity()); @@ -921,7 +924,8 @@ public void testNullableFixedType4() { */ /** - * ARROW-7831: this checks that a slice taken off a buffer is still readable after that buffer's allocator is closed. + * ARROW-7831: this checks that a slice taken off a buffer is still readable after that buffer's + * allocator is closed. */ @Test /* VarCharVector */ public void testSplitAndTransfer1() { @@ -939,11 +943,14 @@ public void testSplitAndTransfer1() { final int offsetRefCnt = sourceVector.getOffsetBuffer().refCnt(); final int dataRefCnt = sourceVector.getDataBuffer().refCnt(); - // split and transfer with slice starting at the beginning: this should not allocate anything new + // split and transfer with slice starting at the beginning: this should not allocate + // anything new sourceVector.splitAndTransferTo(0, 2, targetVector); assertEquals(allocatedMem, allocator.getAllocatedMemory()); - // The validity and offset buffers are sliced from a same buffer.See BaseFixedWidthVector#allocateBytes. - // Therefore, the refcnt of the validity buffer is increased once since the startIndex is 0. The refcnt of the + // The validity and offset buffers are sliced from a same buffer.See + // BaseFixedWidthVector#allocateBytes. + // Therefore, the refcnt of the validity buffer is increased once since the startIndex is 0. + // The refcnt of the // offset buffer is increased as well for the same reason. This amounts to a total of 2. assertEquals(validityRefCnt + 2, sourceVector.getValidityBuffer().refCnt()); assertEquals(offsetRefCnt + 2, sourceVector.getOffsetBuffer().refCnt()); @@ -955,7 +962,8 @@ public void testSplitAndTransfer1() { } /** - * ARROW-7831: this checks that a vector that got sliced is still readable after the slice's allocator got closed. + * ARROW-7831: this checks that a vector that got sliced is still readable after the slice's + * allocator got closed. */ @Test /* VarCharVector */ public void testSplitAndTransfer2() { @@ -973,11 +981,14 @@ public void testSplitAndTransfer2() { final int offsetRefCnt = sourceVector.getOffsetBuffer().refCnt(); final int dataRefCnt = sourceVector.getDataBuffer().refCnt(); - // split and transfer with slice starting at the beginning: this should not allocate anything new + // split and transfer with slice starting at the beginning: this should not allocate + // anything new sourceVector.splitAndTransferTo(0, 2, targetVector); assertEquals(allocatedMem, allocator.getAllocatedMemory()); - // The validity and offset buffers are sliced from a same buffer.See BaseFixedWidthVector#allocateBytes. - // Therefore, the refcnt of the validity buffer is increased once since the startIndex is 0. The refcnt of the + // The validity and offset buffers are sliced from a same buffer.See + // BaseFixedWidthVector#allocateBytes. + // Therefore, the refcnt of the validity buffer is increased once since the startIndex is 0. + // The refcnt of the // offset buffer is increased as well for the same reason. This amounts to a total of 2. assertEquals(validityRefCnt + 2, sourceVector.getValidityBuffer().refCnt()); assertEquals(offsetRefCnt + 2, sourceVector.getOffsetBuffer().refCnt()); @@ -990,13 +1001,13 @@ public void testSplitAndTransfer2() { } /** - * ARROW-7831: this checks an offset splitting optimization, in the case where all the values up to the start of the - * slice are null/empty, which avoids allocation for the offset buffer. + * ARROW-7831: this checks an offset splitting optimization, in the case where all the values up + * to the start of the slice are null/empty, which avoids allocation for the offset buffer. */ @Test /* VarCharVector */ public void testSplitAndTransfer3() { try (final VarCharVector targetVector = newVarCharVector("split-target", allocator); - final VarCharVector sourceVector = newVarCharVector(EMPTY_SCHEMA_PATH, allocator)) { + final VarCharVector sourceVector = newVarCharVector(EMPTY_SCHEMA_PATH, allocator)) { sourceVector.allocateNew(1024 * 10, 1024); sourceVector.set(0, new byte[0]); @@ -1012,14 +1023,17 @@ public void testSplitAndTransfer3() { final int dataRefCnt = sourceVector.getDataBuffer().refCnt(); sourceVector.splitAndTransferTo(2, 2, targetVector); - // because the offset starts at 0 since the first 2 values are empty/null, the allocation only consists in + // because the offset starts at 0 since the first 2 values are empty/null, the allocation only + // consists in // the size needed for the validity buffer final long validitySize = DefaultRoundingPolicy.DEFAULT_ROUNDING_POLICY.getRoundedSize( BaseValueVector.getValidityBufferSizeFromCount(2)); assertEquals(allocatedMem + validitySize, allocator.getAllocatedMemory()); - // The validity and offset buffers are sliced from a same buffer.See BaseFixedWidthVector#allocateBytes. - // Since values up to the startIndex are empty/null, the offset buffer doesn't need to be reallocated and + // The validity and offset buffers are sliced from a same buffer.See + // BaseFixedWidthVector#allocateBytes. + // Since values up to the startIndex are empty/null, the offset buffer doesn't need to be + // reallocated and // therefore its refcnt is increased by 1. assertEquals(validityRefCnt + 1, sourceVector.getValidityBuffer().refCnt()); assertEquals(offsetRefCnt + 1, sourceVector.getOffsetBuffer().refCnt()); @@ -1031,14 +1045,17 @@ public void testSplitAndTransfer3() { } /** - * ARROW-7831: ensures that data is transferred from one allocator to another in case of 0-index start special cases. + * ARROW-7831: ensures that data is transferred from one allocator to another in case of 0-index + * start special cases. */ @Test /* VarCharVector */ public void testSplitAndTransfer4() { - try (final BufferAllocator targetAllocator = allocator.newChildAllocator("target-alloc", 256, 256); - final VarCharVector targetVector = newVarCharVector("split-target", targetAllocator)) { - try (final BufferAllocator sourceAllocator = allocator.newChildAllocator("source-alloc", 256, 256); - final VarCharVector sourceVector = newVarCharVector(EMPTY_SCHEMA_PATH, sourceAllocator)) { + try (final BufferAllocator targetAllocator = + allocator.newChildAllocator("target-alloc", 256, 256); + final VarCharVector targetVector = newVarCharVector("split-target", targetAllocator)) { + try (final BufferAllocator sourceAllocator = + allocator.newChildAllocator("source-alloc", 256, 256); + final VarCharVector sourceVector = newVarCharVector(EMPTY_SCHEMA_PATH, sourceAllocator)) { sourceVector.allocateNew(50, 3); sourceVector.set(0, STR1); @@ -1051,10 +1068,12 @@ public void testSplitAndTransfer4() { final int offsetRefCnt = sourceVector.getOffsetBuffer().refCnt(); final int dataRefCnt = sourceVector.getDataBuffer().refCnt(); - // split and transfer with slice starting at the beginning: this should not allocate anything new + // split and transfer with slice starting at the beginning: this should not allocate + // anything new sourceVector.splitAndTransferTo(0, 2, targetVector); assertEquals(allocatedMem, allocator.getAllocatedMemory()); - // Unlike testSplitAndTransfer1 where the buffers originated from the same allocator, the refcnts of each + // Unlike testSplitAndTransfer1 where the buffers originated from the same allocator, the + // refcnts of each // buffers for this test should be the same as what the source allocator ended up with. assertEquals(validityRefCnt, sourceVector.getValidityBuffer().refCnt()); assertEquals(offsetRefCnt, sourceVector.getOffsetBuffer().refCnt()); @@ -1180,13 +1199,17 @@ public void testGetBytesRepeatedly() { // verify results ReusableByteArray reusableByteArray = new ReusableByteArray(); vector.read(0, reusableByteArray); - assertArrayEquals(str.getBytes(StandardCharsets.UTF_8), Arrays.copyOfRange(reusableByteArray.getBuffer(), - 0, (int) reusableByteArray.getLength())); + assertArrayEquals( + str.getBytes(StandardCharsets.UTF_8), + Arrays.copyOfRange( + reusableByteArray.getBuffer(), 0, (int) reusableByteArray.getLength())); byte[] oldBuffer = reusableByteArray.getBuffer(); vector.read(1, reusableByteArray); - assertArrayEquals(str2.getBytes(StandardCharsets.UTF_8), Arrays.copyOfRange(reusableByteArray.getBuffer(), - 0, (int) reusableByteArray.getLength())); + assertArrayEquals( + str2.getBytes(StandardCharsets.UTF_8), + Arrays.copyOfRange( + reusableByteArray.getBuffer(), 0, (int) reusableByteArray.getLength())); // There should not have been any reallocation since the newer value is smaller in length. assertSame(oldBuffer, reusableByteArray.getBuffer()); @@ -1568,7 +1591,8 @@ public void testReallocAfterVectorTransfer4() { @Test public void testReAllocFixedWidthVector() { // Create a new value vector for 1024 integers - try (final Float4Vector vector = newVector(Float4Vector.class, EMPTY_SCHEMA_PATH, MinorType.FLOAT4, allocator)) { + try (final Float4Vector vector = + newVector(Float4Vector.class, EMPTY_SCHEMA_PATH, MinorType.FLOAT4, allocator)) { vector.allocateNew(1024); assertTrue(vector.getValueCapacity() >= 1024); @@ -1590,8 +1614,10 @@ public void testReAllocFixedWidthVector() { assertEquals(104.5f, vector.get(1023), 0); assertEquals(105.5f, vector.get(2000), 0); - // Set the valueCount to be more than valueCapacity of current allocation. This is possible for ValueVectors - // as we don't call setSafe for null values, but we do call setValueCount when all values are inserted into the + // Set the valueCount to be more than valueCapacity of current allocation. This is possible + // for ValueVectors + // as we don't call setSafe for null values, but we do call setValueCount when all values are + // inserted into the // vector vector.setValueCount(vector.getValueCapacity() + 200); } @@ -1599,7 +1625,8 @@ public void testReAllocFixedWidthVector() { @Test public void testReAllocVariableWidthVector() { - try (final VarCharVector vector = newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator)) { + try (final VarCharVector vector = + newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator)) { vector.setInitialCapacity(4095); vector.allocateNew(); @@ -1623,15 +1650,18 @@ public void testReAllocVariableWidthVector() { assertArrayEquals(STR2, vector.get(initialCapacity - 1)); assertArrayEquals(STR3, vector.get(initialCapacity + 200)); - // Set the valueCount to be more than valueCapacity of current allocation. This is possible for ValueVectors - // as we don't call setSafe for null values, but we do call setValueCount when the current batch is processed. + // Set the valueCount to be more than valueCapacity of current allocation. This is possible + // for ValueVectors + // as we don't call setSafe for null values, but we do call setValueCount when the current + // batch is processed. vector.setValueCount(vector.getValueCapacity() + 200); } } @Test public void testFillEmptiesNotOverfill() { - try (final VarCharVector vector = newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator)) { + try (final VarCharVector vector = + newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator)) { vector.setInitialCapacity(4095); vector.allocateNew(); @@ -1656,11 +1686,10 @@ public void testSetSafeWithArrowBufNoExcessAllocs() { final int valueBytesLength = valueBytes.length; final int isSet = 1; - try ( - final VarCharVector fromVector = newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, - MinorType.VARCHAR, allocator); - final VarCharVector toVector = newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, - MinorType.VARCHAR, allocator)) { + try (final VarCharVector fromVector = + newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator); + final VarCharVector toVector = + newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator)) { /* * Populate the from vector with 'numValues' with byte-arrays, each of size 'valueBytesLength'. */ @@ -1696,9 +1725,10 @@ public void testSetSafeWithArrowBufNoExcessAllocs() { @Test public void testCopyFromWithNulls() { - try (final VarCharVector vector = newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator); - final VarCharVector vector2 = - newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator)) { + try (final VarCharVector vector = + newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator); + final VarCharVector vector2 = + newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator)) { vector.setInitialCapacity(4095); vector.allocateNew(); @@ -1722,7 +1752,10 @@ public void testCopyFromWithNulls() { if (i % 3 == 0) { assertNull(vector.getObject(i)); } else { - assertEquals("unexpected value at index: " + i, Integer.toString(i), vector.getObject(i).toString()); + assertEquals( + "unexpected value at index: " + i, + Integer.toString(i), + vector.getObject(i).toString()); } } @@ -1736,7 +1769,10 @@ public void testCopyFromWithNulls() { if (i % 3 == 0) { assertNull(vector2.getObject(i)); } else { - assertEquals("unexpected value at index: " + i, Integer.toString(i), vector2.getObject(i).toString()); + assertEquals( + "unexpected value at index: " + i, + Integer.toString(i), + vector2.getObject(i).toString()); } } @@ -1749,7 +1785,10 @@ public void testCopyFromWithNulls() { if (i % 3 == 0) { assertNull(vector2.getObject(i)); } else { - assertEquals("unexpected value at index: " + i, Integer.toString(i), vector2.getObject(i).toString()); + assertEquals( + "unexpected value at index: " + i, + Integer.toString(i), + vector2.getObject(i).toString()); } } } @@ -1757,9 +1796,10 @@ public void testCopyFromWithNulls() { @Test public void testCopyFromWithNulls1() { - try (final VarCharVector vector = newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator); - final VarCharVector vector2 = - newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator)) { + try (final VarCharVector vector = + newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator); + final VarCharVector vector2 = + newVector(VarCharVector.class, EMPTY_SCHEMA_PATH, MinorType.VARCHAR, allocator)) { vector.setInitialCapacity(4095); vector.allocateNew(); @@ -1783,7 +1823,10 @@ public void testCopyFromWithNulls1() { if (i % 3 == 0) { assertNull(vector.getObject(i)); } else { - assertEquals("unexpected value at index: " + i, Integer.toString(i), vector.getObject(i).toString()); + assertEquals( + "unexpected value at index: " + i, + Integer.toString(i), + vector.getObject(i).toString()); } } @@ -1801,7 +1844,10 @@ public void testCopyFromWithNulls1() { if (i % 3 == 0) { assertNull(vector2.getObject(i)); } else { - assertEquals("unexpected value at index: " + i, Integer.toString(i), vector2.getObject(i).toString()); + assertEquals( + "unexpected value at index: " + i, + Integer.toString(i), + vector2.getObject(i).toString()); } } @@ -1814,7 +1860,10 @@ public void testCopyFromWithNulls1() { if (i % 3 == 0) { assertNull(vector2.getObject(i)); } else { - assertEquals("unexpected value at index: " + i, Integer.toString(i), vector2.getObject(i).toString()); + assertEquals( + "unexpected value at index: " + i, + Integer.toString(i), + vector2.getObject(i).toString()); } } } @@ -1898,7 +1947,7 @@ public void testSetLastSetUsage() { assertEquals(40, vector.offsetBuffer.getInt(17 * BaseVariableWidthVector.OFFSET_WIDTH)); assertEquals(40, vector.offsetBuffer.getInt(18 * BaseVariableWidthVector.OFFSET_WIDTH)); assertEquals(40, vector.offsetBuffer.getInt(19 * BaseVariableWidthVector.OFFSET_WIDTH)); - + vector.set(19, STR6); assertArrayEquals(STR6, vector.get(19)); assertEquals(40, vector.offsetBuffer.getInt(19 * BaseVariableWidthVector.OFFSET_WIDTH)); @@ -1936,14 +1985,14 @@ public void testVectorLoadUnload() { Schema schema = new Schema(fields); - VectorSchemaRoot schemaRoot1 = new VectorSchemaRoot(schema, fieldVectors, vector1.getValueCount()); + VectorSchemaRoot schemaRoot1 = + new VectorSchemaRoot(schema, fieldVectors, vector1.getValueCount()); VectorUnloader vectorUnloader = new VectorUnloader(schemaRoot1); - try ( - ArrowRecordBatch recordBatch = vectorUnloader.getRecordBatch(); - BufferAllocator finalVectorsAllocator = allocator.newChildAllocator("new vector", 0, Long.MAX_VALUE); - VectorSchemaRoot schemaRoot2 = VectorSchemaRoot.create(schema, finalVectorsAllocator); - ) { + try (ArrowRecordBatch recordBatch = vectorUnloader.getRecordBatch(); + BufferAllocator finalVectorsAllocator = + allocator.newChildAllocator("new vector", 0, Long.MAX_VALUE); + VectorSchemaRoot schemaRoot2 = VectorSchemaRoot.create(schema, finalVectorsAllocator); ) { VectorLoader vectorLoader = new VectorLoader(schemaRoot2); vectorLoader.load(recordBatch); @@ -2039,41 +2088,25 @@ public void testFillEmptiesUsage() { assertEquals(0, vector.getValueLength(14)); /* Check offsets */ - assertEquals(0, - vector.offsetBuffer.getInt(0 * BaseVariableWidthVector.OFFSET_WIDTH)); - assertEquals(6, - vector.offsetBuffer.getInt(1 * BaseVariableWidthVector.OFFSET_WIDTH)); - assertEquals(16, - vector.offsetBuffer.getInt(2 * BaseVariableWidthVector.OFFSET_WIDTH)); - assertEquals(21, - vector.offsetBuffer.getInt(3 * BaseVariableWidthVector.OFFSET_WIDTH)); - assertEquals(30, - vector.offsetBuffer.getInt(4 * BaseVariableWidthVector.OFFSET_WIDTH)); - assertEquals(34, - vector.offsetBuffer.getInt(5 * BaseVariableWidthVector.OFFSET_WIDTH)); - - assertEquals(40, - vector.offsetBuffer.getInt(6 * BaseVariableWidthVector.OFFSET_WIDTH)); - assertEquals(40, - vector.offsetBuffer.getInt(7 * BaseVariableWidthVector.OFFSET_WIDTH)); - assertEquals(40, - vector.offsetBuffer.getInt(8 * BaseVariableWidthVector.OFFSET_WIDTH)); - assertEquals(40, - vector.offsetBuffer.getInt(9 * BaseVariableWidthVector.OFFSET_WIDTH)); - assertEquals(40, - vector.offsetBuffer.getInt(10 * BaseVariableWidthVector.OFFSET_WIDTH)); - - assertEquals(46, - vector.offsetBuffer.getInt(11 * BaseVariableWidthVector.OFFSET_WIDTH)); - assertEquals(56, - vector.offsetBuffer.getInt(12 * BaseVariableWidthVector.OFFSET_WIDTH)); - - assertEquals(56, - vector.offsetBuffer.getInt(13 * BaseVariableWidthVector.OFFSET_WIDTH)); - assertEquals(56, - vector.offsetBuffer.getInt(14 * BaseVariableWidthVector.OFFSET_WIDTH)); - assertEquals(56, - vector.offsetBuffer.getInt(15 * BaseVariableWidthVector.OFFSET_WIDTH)); + assertEquals(0, vector.offsetBuffer.getInt(0 * BaseVariableWidthVector.OFFSET_WIDTH)); + assertEquals(6, vector.offsetBuffer.getInt(1 * BaseVariableWidthVector.OFFSET_WIDTH)); + assertEquals(16, vector.offsetBuffer.getInt(2 * BaseVariableWidthVector.OFFSET_WIDTH)); + assertEquals(21, vector.offsetBuffer.getInt(3 * BaseVariableWidthVector.OFFSET_WIDTH)); + assertEquals(30, vector.offsetBuffer.getInt(4 * BaseVariableWidthVector.OFFSET_WIDTH)); + assertEquals(34, vector.offsetBuffer.getInt(5 * BaseVariableWidthVector.OFFSET_WIDTH)); + + assertEquals(40, vector.offsetBuffer.getInt(6 * BaseVariableWidthVector.OFFSET_WIDTH)); + assertEquals(40, vector.offsetBuffer.getInt(7 * BaseVariableWidthVector.OFFSET_WIDTH)); + assertEquals(40, vector.offsetBuffer.getInt(8 * BaseVariableWidthVector.OFFSET_WIDTH)); + assertEquals(40, vector.offsetBuffer.getInt(9 * BaseVariableWidthVector.OFFSET_WIDTH)); + assertEquals(40, vector.offsetBuffer.getInt(10 * BaseVariableWidthVector.OFFSET_WIDTH)); + + assertEquals(46, vector.offsetBuffer.getInt(11 * BaseVariableWidthVector.OFFSET_WIDTH)); + assertEquals(56, vector.offsetBuffer.getInt(12 * BaseVariableWidthVector.OFFSET_WIDTH)); + + assertEquals(56, vector.offsetBuffer.getInt(13 * BaseVariableWidthVector.OFFSET_WIDTH)); + assertEquals(56, vector.offsetBuffer.getInt(14 * BaseVariableWidthVector.OFFSET_WIDTH)); + assertEquals(56, vector.offsetBuffer.getInt(15 * BaseVariableWidthVector.OFFSET_WIDTH)); } } @@ -2141,8 +2174,10 @@ public void testGetBufferAddress2() { @Test public void testMultipleClose() { - BufferAllocator vectorAllocator = allocator.newChildAllocator("vector_allocator", 0, Long.MAX_VALUE); - IntVector vector = newVector(IntVector.class, EMPTY_SCHEMA_PATH, MinorType.INT, vectorAllocator); + BufferAllocator vectorAllocator = + allocator.newChildAllocator("vector_allocator", 0, Long.MAX_VALUE); + IntVector vector = + newVector(IntVector.class, EMPTY_SCHEMA_PATH, MinorType.INT, vectorAllocator); vector.close(); vectorAllocator.close(); vector.close(); @@ -2154,10 +2189,12 @@ public void testMultipleClose() { * in a way that lastSet is not set automatically. */ public static void setBytes(int index, byte[] bytes, VarCharVector vector) { - final int currentOffset = vector.offsetBuffer.getInt(index * BaseVariableWidthVector.OFFSET_WIDTH); + final int currentOffset = + vector.offsetBuffer.getInt(index * BaseVariableWidthVector.OFFSET_WIDTH); BitVectorHelper.setBit(vector.validityBuffer, index); - vector.offsetBuffer.setInt((index + 1) * BaseVariableWidthVector.OFFSET_WIDTH, currentOffset + bytes.length); + vector.offsetBuffer.setInt( + (index + 1) * BaseVariableWidthVector.OFFSET_WIDTH, currentOffset + bytes.length); vector.valueBuffer.setBytes(currentOffset, bytes, 0, bytes.length); } @@ -2170,7 +2207,8 @@ public void testSetInitialCapacity() { vector.setInitialCapacity(defaultCapacity); vector.allocateNew(); assertEquals(defaultCapacity, vector.getValueCapacity()); - assertEquals(CommonUtil.nextPowerOfTwo(defaultCapacity * 8), vector.getDataBuffer().capacity()); + assertEquals( + CommonUtil.nextPowerOfTwo(defaultCapacity * 8), vector.getDataBuffer().capacity()); vector.setInitialCapacity(defaultCapacity, 1); vector.allocateNew(); @@ -2180,12 +2218,16 @@ public void testSetInitialCapacity() { vector.setInitialCapacity(defaultCapacity, 0.1); vector.allocateNew(); assertEquals(defaultCapacity, vector.getValueCapacity()); - assertEquals(CommonUtil.nextPowerOfTwo((int) (defaultCapacity * 0.1)), vector.getDataBuffer().capacity()); + assertEquals( + CommonUtil.nextPowerOfTwo((int) (defaultCapacity * 0.1)), + vector.getDataBuffer().capacity()); vector.setInitialCapacity(defaultCapacity, 0.01); vector.allocateNew(); assertEquals(defaultCapacity, vector.getValueCapacity()); - assertEquals(CommonUtil.nextPowerOfTwo((int) (defaultCapacity * 0.01)), vector.getDataBuffer().capacity()); + assertEquals( + CommonUtil.nextPowerOfTwo((int) (defaultCapacity * 0.01)), + vector.getDataBuffer().capacity()); vector.setInitialCapacity(5, 0.01); vector.allocateNew(); @@ -2199,35 +2241,40 @@ public void testDefaultAllocNewAll() { int defaultCapacity = BaseValueVector.INITIAL_VALUE_ALLOCATION; int expectedSize; long beforeSize; - try (BufferAllocator childAllocator = allocator.newChildAllocator("defaultAllocs", 0, Long.MAX_VALUE); + try (BufferAllocator childAllocator = + allocator.newChildAllocator("defaultAllocs", 0, Long.MAX_VALUE); final IntVector intVector = new IntVector(EMPTY_SCHEMA_PATH, childAllocator); final BigIntVector bigIntVector = new BigIntVector(EMPTY_SCHEMA_PATH, childAllocator); final BitVector bitVector = new BitVector(EMPTY_SCHEMA_PATH, childAllocator); - final DecimalVector decimalVector = new DecimalVector(EMPTY_SCHEMA_PATH, childAllocator, 38, 6); + final DecimalVector decimalVector = + new DecimalVector(EMPTY_SCHEMA_PATH, childAllocator, 38, 6); final VarCharVector varCharVector = new VarCharVector(EMPTY_SCHEMA_PATH, childAllocator)) { // verify that the wastage is within bounds for IntVector. beforeSize = childAllocator.getAllocatedMemory(); intVector.allocateNew(); assertTrue(intVector.getValueCapacity() >= defaultCapacity); - expectedSize = (defaultCapacity * IntVector.TYPE_WIDTH) + - BaseFixedWidthVector.getValidityBufferSizeFromCount(defaultCapacity); + expectedSize = + (defaultCapacity * IntVector.TYPE_WIDTH) + + BaseFixedWidthVector.getValidityBufferSizeFromCount(defaultCapacity); assertTrue(childAllocator.getAllocatedMemory() - beforeSize <= expectedSize * 1.05); // verify that the wastage is within bounds for BigIntVector. beforeSize = childAllocator.getAllocatedMemory(); bigIntVector.allocateNew(); assertTrue(bigIntVector.getValueCapacity() >= defaultCapacity); - expectedSize = (defaultCapacity * bigIntVector.TYPE_WIDTH) + - BaseFixedWidthVector.getValidityBufferSizeFromCount(defaultCapacity); + expectedSize = + (defaultCapacity * bigIntVector.TYPE_WIDTH) + + BaseFixedWidthVector.getValidityBufferSizeFromCount(defaultCapacity); assertTrue(childAllocator.getAllocatedMemory() - beforeSize <= expectedSize * 1.05); // verify that the wastage is within bounds for DecimalVector. beforeSize = childAllocator.getAllocatedMemory(); decimalVector.allocateNew(); assertTrue(decimalVector.getValueCapacity() >= defaultCapacity); - expectedSize = (defaultCapacity * decimalVector.TYPE_WIDTH) + - BaseFixedWidthVector.getValidityBufferSizeFromCount(defaultCapacity); + expectedSize = + (defaultCapacity * decimalVector.TYPE_WIDTH) + + BaseFixedWidthVector.getValidityBufferSizeFromCount(defaultCapacity); assertTrue(childAllocator.getAllocatedMemory() - beforeSize <= expectedSize * 1.05); // verify that the wastage is within bounds for VarCharVector. @@ -2235,9 +2282,10 @@ public void testDefaultAllocNewAll() { beforeSize = childAllocator.getAllocatedMemory(); varCharVector.allocateNew(); assertTrue(varCharVector.getValueCapacity() >= defaultCapacity - 1); - expectedSize = (defaultCapacity * VarCharVector.OFFSET_WIDTH) + - BaseFixedWidthVector.getValidityBufferSizeFromCount(defaultCapacity) + - defaultCapacity * 8; + expectedSize = + (defaultCapacity * VarCharVector.OFFSET_WIDTH) + + BaseFixedWidthVector.getValidityBufferSizeFromCount(defaultCapacity) + + defaultCapacity * 8; // wastage should be less than 5%. assertTrue(childAllocator.getAllocatedMemory() - beforeSize <= expectedSize * 1.05); @@ -2247,7 +2295,6 @@ public void testDefaultAllocNewAll() { assertTrue(bitVector.getValueCapacity() >= defaultCapacity); expectedSize = BaseFixedWidthVector.getValidityBufferSizeFromCount(defaultCapacity) * 2; assertTrue(childAllocator.getAllocatedMemory() - beforeSize <= expectedSize * 1.05); - } } @@ -2375,7 +2422,7 @@ public void testSetNullableVarBinaryHolderSafe() { public void testGetPointerFixedWidth() { final int vectorLength = 100; try (IntVector vec1 = new IntVector("vec1", allocator); - IntVector vec2 = new IntVector("vec2", allocator)) { + IntVector vec2 = new IntVector("vec2", allocator)) { vec1.allocateNew(vectorLength); vec2.allocateNew(vectorLength); @@ -2409,11 +2456,11 @@ public void testGetPointerFixedWidth() { @Test public void testGetPointerVariableWidth() { - final String[] sampleData = new String[]{ - "abc", "123", "def", null, "hello", "aaaaa", "world", "2019", null, "0717"}; + final String[] sampleData = + new String[] {"abc", "123", "def", null, "hello", "aaaaa", "world", "2019", null, "0717"}; try (VarCharVector vec1 = new VarCharVector("vec1", allocator); - VarCharVector vec2 = new VarCharVector("vec2", allocator)) { + VarCharVector vec2 = new VarCharVector("vec2", allocator)) { vec1.allocateNew(sampleData.length * 10, sampleData.length); vec2.allocateNew(sampleData.length * 10, sampleData.length); @@ -2444,7 +2491,7 @@ public void testGetPointerVariableWidth() { @Test public void testGetNullFromVariableWidthVector() { try (final VarCharVector varCharVector = new VarCharVector("varcharvec", allocator); - final VarBinaryVector varBinaryVector = new VarBinaryVector("varbinary", allocator)) { + final VarBinaryVector varBinaryVector = new VarBinaryVector("varbinary", allocator)) { varCharVector.allocateNew(10, 1); varBinaryVector.allocateNew(10, 1); @@ -2504,7 +2551,7 @@ public void testBitVectorEquals() { @Test public void testIntVectorEqualsWithNull() { try (final IntVector vector1 = new IntVector("int", allocator); - final IntVector vector2 = new IntVector("int", allocator)) { + final IntVector vector2 = new IntVector("int", allocator)) { setVector(vector1, 1, 2); setVector(vector2, 1, null); @@ -2518,7 +2565,7 @@ public void testIntVectorEqualsWithNull() { @Test public void testIntVectorEquals() { try (final IntVector vector1 = new IntVector("int", allocator); - final IntVector vector2 = new IntVector("int", allocator)) { + final IntVector vector2 = new IntVector("int", allocator)) { setVector(vector1, 1, 2, 3); setVector(vector2, 1, 2, null); @@ -2539,8 +2586,8 @@ public void testIntVectorEquals() { @Test public void testDecimalVectorEquals() { try (final DecimalVector vector1 = new DecimalVector("decimal", allocator, 3, 3); - final DecimalVector vector2 = new DecimalVector("decimal", allocator, 3, 3); - final DecimalVector vector3 = new DecimalVector("decimal", allocator, 3, 2)) { + final DecimalVector vector2 = new DecimalVector("decimal", allocator, 3, 3); + final DecimalVector vector3 = new DecimalVector("decimal", allocator, 3, 2)) { setVector(vector1, 100L, 200L); setVector(vector2, 100L, 200L); @@ -2557,7 +2604,7 @@ public void testDecimalVectorEquals() { @Test public void testVarcharVectorEqualsWithNull() { try (final VarCharVector vector1 = new VarCharVector("varchar", allocator); - final VarCharVector vector2 = new VarCharVector("varchar", allocator)) { + final VarCharVector vector2 = new VarCharVector("varchar", allocator)) { setVector(vector1, STR1, STR2); setVector(vector2, STR1, null); @@ -2570,7 +2617,7 @@ public void testVarcharVectorEqualsWithNull() { @Test public void testVarcharVectorEquals() { try (final VarCharVector vector1 = new VarCharVector("varchar", allocator); - final VarCharVector vector2 = new VarCharVector("varchar", allocator)) { + final VarCharVector vector2 = new VarCharVector("varchar", allocator)) { setVector(vector1, STR1, STR2, STR3); setVector(vector2, STR1, STR2); @@ -2587,7 +2634,7 @@ public void testVarcharVectorEquals() { @Test public void testVarBinaryVectorEquals() { try (final VarBinaryVector vector1 = new VarBinaryVector("binary", allocator); - final VarBinaryVector vector2 = new VarBinaryVector("binary", allocator)) { + final VarBinaryVector vector2 = new VarBinaryVector("binary", allocator)) { setVector(vector1, STR1, STR2, STR3); setVector(vector2, STR1, STR2); @@ -2604,12 +2651,12 @@ public void testVarBinaryVectorEquals() { @Test public void testListVectorEqualsWithNull() { try (final ListVector vector1 = ListVector.empty("list", allocator); - final ListVector vector2 = ListVector.empty("list", allocator);) { + final ListVector vector2 = ListVector.empty("list", allocator); ) { UnionListWriter writer1 = vector1.getWriter(); writer1.allocate(); - //set some values + // set some values writeListVector(writer1, new int[] {1, 2}); writeListVector(writer1, new int[] {3, 4}); writeListVector(writer1, new int[] {}); @@ -2618,7 +2665,7 @@ public void testListVectorEqualsWithNull() { UnionListWriter writer2 = vector2.getWriter(); writer2.allocate(); - //set some values + // set some values writeListVector(writer2, new int[] {1, 2}); writeListVector(writer2, new int[] {3, 4}); writer2.setValueCount(3); @@ -2632,12 +2679,12 @@ public void testListVectorEqualsWithNull() { @Test public void testListVectorEquals() { try (final ListVector vector1 = ListVector.empty("list", allocator); - final ListVector vector2 = ListVector.empty("list", allocator);) { + final ListVector vector2 = ListVector.empty("list", allocator); ) { UnionListWriter writer1 = vector1.getWriter(); writer1.allocate(); - //set some values + // set some values writeListVector(writer1, new int[] {1, 2}); writeListVector(writer1, new int[] {3, 4}); writeListVector(writer1, new int[] {5, 6}); @@ -2646,7 +2693,7 @@ public void testListVectorEquals() { UnionListWriter writer2 = vector2.getWriter(); writer2.allocate(); - //set some values + // set some values writeListVector(writer2, new int[] {1, 2}); writeListVector(writer2, new int[] {3, 4}); writer2.setValueCount(2); @@ -2665,7 +2712,7 @@ public void testListVectorEquals() { public void testStructVectorEqualsWithNull() { try (final StructVector vector1 = StructVector.empty("struct", allocator); - final StructVector vector2 = StructVector.empty("struct", allocator);) { + final StructVector vector2 = StructVector.empty("struct", allocator); ) { vector1.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); vector1.addOrGet("f1", FieldType.nullable(new ArrowType.Int(64, true)), BigIntVector.class); vector2.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); @@ -2694,7 +2741,7 @@ public void testStructVectorEqualsWithNull() { @Test public void testStructVectorEquals() { try (final StructVector vector1 = StructVector.empty("struct", allocator); - final StructVector vector2 = StructVector.empty("struct", allocator);) { + final StructVector vector2 = StructVector.empty("struct", allocator); ) { vector1.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); vector1.addOrGet("f1", FieldType.nullable(new ArrowType.Int(64, true)), BigIntVector.class); vector2.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); @@ -2728,7 +2775,7 @@ public void testStructVectorEquals() { @Test public void testStructVectorEqualsWithDiffChild() { try (final StructVector vector1 = StructVector.empty("struct", allocator); - final StructVector vector2 = StructVector.empty("struct", allocator);) { + final StructVector vector2 = StructVector.empty("struct", allocator); ) { vector1.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); vector1.addOrGet("f1", FieldType.nullable(new ArrowType.Int(64, true)), BigIntVector.class); vector2.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); @@ -2755,8 +2802,10 @@ public void testStructVectorEqualsWithDiffChild() { @Test public void testUnionVectorEquals() { - try (final UnionVector vector1 = new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); - final UnionVector vector2 = new UnionVector("union", allocator, /* field type */ null, /* call-back */ null);) { + try (final UnionVector vector1 = + new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); + final UnionVector vector2 = + new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); ) { final NullableUInt4Holder uInt4Holder = new NullableUInt4Holder(); uInt4Holder.value = 10; @@ -2788,7 +2837,7 @@ public void testUnionVectorEquals() { @Test(expected = IllegalArgumentException.class) public void testEqualsWithIndexOutOfRange() { try (final IntVector vector1 = new IntVector("int", allocator); - final IntVector vector2 = new IntVector("int", allocator)) { + final IntVector vector2 = new IntVector("int", allocator)) { setVector(vector1, 1, 2); setVector(vector2, 1, 2); @@ -2826,7 +2875,8 @@ public void testVariableWidthVectorNullHashCode() { @Test public void testUnionNullHashCode() { try (UnionVector srcVector = - new UnionVector(EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { + new UnionVector( + EMPTY_SCHEMA_PATH, allocator, /* field type */ null, /* call-back */ null)) { srcVector.allocateNew(); final NullableIntHolder holder = new NullableIntHolder(); @@ -2843,8 +2893,8 @@ public void testUnionNullHashCode() { @Test public void testToString() { try (final IntVector intVector = new IntVector("intVector", allocator); - final ListVector listVector = ListVector.empty("listVector", allocator); - final StructVector structVector = StructVector.empty("structVector", allocator)) { + final ListVector listVector = ListVector.empty("listVector", allocator); + final StructVector structVector = StructVector.empty("structVector", allocator)) { // validate intVector toString assertEquals("[]", intVector.toString()); @@ -2859,7 +2909,8 @@ public void testToString() { for (int i = 0; i < 100; i++) { intVector.setSafe(i, i); } - assertEquals("[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]", + assertEquals( + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]", intVector.toString()); // validate listVector toString @@ -2883,7 +2934,8 @@ public void testToString() { // validate structVector toString structVector.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); - structVector.addOrGet("f1", FieldType.nullable(new ArrowType.Int(64, true)), BigIntVector.class); + structVector.addOrGet( + "f1", FieldType.nullable(new ArrowType.Int(64, true)), BigIntVector.class); NullableStructWriter structWriter = structVector.getWriter(); structWriter.allocate(); @@ -2959,7 +3011,7 @@ private void writeStructVector(NullableStructWriter writer, int value1, long val private void writeListVector(UnionListWriter writer, int[] values) { writer.startList(); - for (int v: values) { + for (int v : values) { writer.integer().writeInt(v); } writer.endList(); @@ -2968,7 +3020,7 @@ private void writeListVector(UnionListWriter writer, int[] values) { @Test public void testVariableVectorGetEndOffset() { try (final VarCharVector vector1 = new VarCharVector("v1", allocator); - final VarBinaryVector vector2 = new VarBinaryVector("v2", allocator)) { + final VarBinaryVector vector2 = new VarBinaryVector("v2", allocator)) { setVector(vector1, STR1, null, STR2); setVector(vector2, STR1, STR2, STR3); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestVarCharListVector.java b/java/vector/src/test/java/org/apache/arrow/vector/TestVarCharListVector.java index bfe489fa5af..71c5691d926 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestVarCharListVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestVarCharListVector.java @@ -18,7 +18,6 @@ package org.apache.arrow.vector; import java.nio.charset.StandardCharsets; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.complex.ListVector; @@ -47,9 +46,10 @@ public void terminate() throws Exception { @Test public void testVarCharListWithNulls() { byte[] bytes = "a".getBytes(StandardCharsets.UTF_8); - try (ListVector vector = new ListVector("VarList", allocator, FieldType.nullable(Types - .MinorType.VARCHAR.getType()), null); - ArrowBuf tempBuf = allocator.buffer(bytes.length)) { + try (ListVector vector = + new ListVector( + "VarList", allocator, FieldType.nullable(Types.MinorType.VARCHAR.getType()), null); + ArrowBuf tempBuf = allocator.buffer(bytes.length)) { UnionListWriter writer = vector.getWriter(); writer.allocate(); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestVectorAlloc.java b/java/vector/src/test/java/org/apache/arrow/vector/TestVectorAlloc.java index b96f6ab6afe..51eeb10bc60 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestVectorAlloc.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestVectorAlloc.java @@ -22,7 +22,6 @@ import java.util.Arrays; import java.util.Collections; - import org.apache.arrow.memory.AllocationListener; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -67,45 +66,47 @@ private static Field field(String name, ArrowType type) { @Test public void testVectorAllocWithField() { - Schema schema = new Schema(Arrays.asList( - field("TINYINT", MinorType.TINYINT.getType()), - field("SMALLINT", MinorType.SMALLINT.getType()), - field("INT", MinorType.INT.getType()), - field("BIGINT", MinorType.BIGINT.getType()), - field("UINT1", MinorType.UINT1.getType()), - field("UINT2", MinorType.UINT2.getType()), - field("UINT4", MinorType.UINT4.getType()), - field("UINT8", MinorType.UINT8.getType()), - field("FLOAT4", MinorType.FLOAT4.getType()), - field("FLOAT8", MinorType.FLOAT8.getType()), - field("UTF8", MinorType.VARCHAR.getType()), - field("VARBINARY", MinorType.VARBINARY.getType()), - field("BIT", MinorType.BIT.getType()), - field("DECIMAL", new Decimal(38, 5, 128)), - field("FIXEDSIZEBINARY", new FixedSizeBinary(50)), - field("DATEDAY", MinorType.DATEDAY.getType()), - field("DATEMILLI", MinorType.DATEMILLI.getType()), - field("TIMESEC", MinorType.TIMESEC.getType()), - field("TIMEMILLI", MinorType.TIMEMILLI.getType()), - field("TIMEMICRO", MinorType.TIMEMICRO.getType()), - field("TIMENANO", MinorType.TIMENANO.getType()), - field("TIMESTAMPSEC", MinorType.TIMESTAMPSEC.getType()), - field("TIMESTAMPMILLI", MinorType.TIMESTAMPMILLI.getType()), - field("TIMESTAMPMICRO", MinorType.TIMESTAMPMICRO.getType()), - field("TIMESTAMPNANO", MinorType.TIMESTAMPNANO.getType()), - field("TIMESTAMPSECTZ", new Timestamp(TimeUnit.SECOND, "PST")), - field("TIMESTAMPMILLITZ", new Timestamp(TimeUnit.MILLISECOND, "PST")), - field("TIMESTAMPMICROTZ", new Timestamp(TimeUnit.MICROSECOND, "PST")), - field("TIMESTAMPNANOTZ", new Timestamp(TimeUnit.NANOSECOND, "PST")), - field("INTERVALDAY", MinorType.INTERVALDAY.getType()), - field("INTERVALYEAR", MinorType.INTERVALYEAR.getType()), - field("DURATION", new Duration(TimeUnit.MILLISECOND)) - )); + Schema schema = + new Schema( + Arrays.asList( + field("TINYINT", MinorType.TINYINT.getType()), + field("SMALLINT", MinorType.SMALLINT.getType()), + field("INT", MinorType.INT.getType()), + field("BIGINT", MinorType.BIGINT.getType()), + field("UINT1", MinorType.UINT1.getType()), + field("UINT2", MinorType.UINT2.getType()), + field("UINT4", MinorType.UINT4.getType()), + field("UINT8", MinorType.UINT8.getType()), + field("FLOAT4", MinorType.FLOAT4.getType()), + field("FLOAT8", MinorType.FLOAT8.getType()), + field("UTF8", MinorType.VARCHAR.getType()), + field("VARBINARY", MinorType.VARBINARY.getType()), + field("BIT", MinorType.BIT.getType()), + field("DECIMAL", new Decimal(38, 5, 128)), + field("FIXEDSIZEBINARY", new FixedSizeBinary(50)), + field("DATEDAY", MinorType.DATEDAY.getType()), + field("DATEMILLI", MinorType.DATEMILLI.getType()), + field("TIMESEC", MinorType.TIMESEC.getType()), + field("TIMEMILLI", MinorType.TIMEMILLI.getType()), + field("TIMEMICRO", MinorType.TIMEMICRO.getType()), + field("TIMENANO", MinorType.TIMENANO.getType()), + field("TIMESTAMPSEC", MinorType.TIMESTAMPSEC.getType()), + field("TIMESTAMPMILLI", MinorType.TIMESTAMPMILLI.getType()), + field("TIMESTAMPMICRO", MinorType.TIMESTAMPMICRO.getType()), + field("TIMESTAMPNANO", MinorType.TIMESTAMPNANO.getType()), + field("TIMESTAMPSECTZ", new Timestamp(TimeUnit.SECOND, "PST")), + field("TIMESTAMPMILLITZ", new Timestamp(TimeUnit.MILLISECOND, "PST")), + field("TIMESTAMPMICROTZ", new Timestamp(TimeUnit.MICROSECOND, "PST")), + field("TIMESTAMPNANOTZ", new Timestamp(TimeUnit.NANOSECOND, "PST")), + field("INTERVALDAY", MinorType.INTERVALDAY.getType()), + field("INTERVALYEAR", MinorType.INTERVALYEAR.getType()), + field("DURATION", new Duration(TimeUnit.MILLISECOND)))); try (BufferAllocator allocator = rootAllocator.newChildAllocator("child", 0, Long.MAX_VALUE)) { for (Field field : schema.getFields()) { try (FieldVector vector = field.createVector(allocator)) { - assertEquals(vector.getMinorType(), + assertEquals( + vector.getMinorType(), Types.getMinorTypeForArrowType(field.getFieldType().getType())); vector.allocateNew(); } @@ -115,10 +116,7 @@ public void testVectorAllocWithField() { private static final int CUSTOM_SEGMENT_SIZE = 200; - /** - * A custom rounding policy that rounds the size to - * the next multiple of 200. - */ + /** A custom rounding policy that rounds the size to the next multiple of 200. */ private static class CustomPolicy implements RoundingPolicy { @Override @@ -150,7 +148,7 @@ public void testFixedWidthVectorAllocation() { @Test public void testVariableWidthVectorAllocation() { try (VarCharVector vec1 = new VarCharVector("vec", policyAllocator); - VarCharVector vec2 = new VarCharVector("vec", rootAllocator)) { + VarCharVector vec2 = new VarCharVector("vec", rootAllocator)) { assertTrue(vec1.getAllocator().getRoundingPolicy() instanceof CustomPolicy); vec1.allocateNew(50); long totalCapacity = vec1.getValidityBuffer().capacity() + vec1.getOffsetBuffer().capacity(); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestVectorReAlloc.java b/java/vector/src/test/java/org/apache/arrow/vector/TestVectorReAlloc.java index 9043bd4f8f2..9900d263f96 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestVectorReAlloc.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestVectorReAlloc.java @@ -20,7 +20,6 @@ import static org.junit.Assert.*; import java.nio.charset.StandardCharsets; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.complex.DenseUnionVector; @@ -42,7 +41,6 @@ import org.junit.Before; import org.junit.Test; - public class TestVectorReAlloc { private BufferAllocator allocator; @@ -254,10 +252,14 @@ public void testVarCharAllocateNew() throws Exception { try (final VarCharVector vector = new VarCharVector("", allocator)) { vector.allocateNew(count); - - // verify that the validity buffer and value buffer have capacity for at least 'count' elements. - Assert.assertTrue(vector.getValidityBuffer().capacity() >= DataSizeRoundingUtil.divideBy8Ceil(count)); - Assert.assertTrue(vector.getOffsetBuffer().capacity() >= (count + 1) * BaseVariableWidthVector.OFFSET_WIDTH); + + // verify that the validity buffer and value buffer have capacity for at least 'count' + // elements. + Assert.assertTrue( + vector.getValidityBuffer().capacity() >= DataSizeRoundingUtil.divideBy8Ceil(count)); + Assert.assertTrue( + vector.getOffsetBuffer().capacity() + >= (count + 1) * BaseVariableWidthVector.OFFSET_WIDTH); } } @@ -267,10 +269,14 @@ public void testLargeVarCharAllocateNew() throws Exception { try (final LargeVarCharVector vector = new LargeVarCharVector("", allocator)) { vector.allocateNew(count); - - // verify that the validity buffer and value buffer have capacity for at least 'count' elements. - Assert.assertTrue(vector.getValidityBuffer().capacity() >= DataSizeRoundingUtil.divideBy8Ceil(count)); - Assert.assertTrue(vector.getOffsetBuffer().capacity() >= (count + 1) * BaseLargeVariableWidthVector.OFFSET_WIDTH); + + // verify that the validity buffer and value buffer have capacity for at least 'count' + // elements. + Assert.assertTrue( + vector.getValidityBuffer().capacity() >= DataSizeRoundingUtil.divideBy8Ceil(count)); + Assert.assertTrue( + vector.getOffsetBuffer().capacity() + >= (count + 1) * BaseLargeVariableWidthVector.OFFSET_WIDTH); } } @@ -281,9 +287,13 @@ public void testVarCharAllocateNewUsingHelper() throws Exception { try (final VarCharVector vector = new VarCharVector("", allocator)) { AllocationHelper.allocateNew(vector, count); - // verify that the validity buffer and value buffer have capacity for at least 'count' elements. - Assert.assertTrue(vector.getValidityBuffer().capacity() >= DataSizeRoundingUtil.divideBy8Ceil(count)); - Assert.assertTrue(vector.getOffsetBuffer().capacity() >= (count + 1) * BaseVariableWidthVector.OFFSET_WIDTH); + // verify that the validity buffer and value buffer have capacity for at least 'count' + // elements. + Assert.assertTrue( + vector.getValidityBuffer().capacity() >= DataSizeRoundingUtil.divideBy8Ceil(count)); + Assert.assertTrue( + vector.getOffsetBuffer().capacity() + >= (count + 1) * BaseVariableWidthVector.OFFSET_WIDTH); } } @@ -294,9 +304,13 @@ public void testLargeVarCharAllocateNewUsingHelper() throws Exception { try (final LargeVarCharVector vector = new LargeVarCharVector("", allocator)) { AllocationHelper.allocateNew(vector, count); - // verify that the validity buffer and value buffer have capacity for at least 'count' elements. - Assert.assertTrue(vector.getValidityBuffer().capacity() >= DataSizeRoundingUtil.divideBy8Ceil(count)); - Assert.assertTrue(vector.getOffsetBuffer().capacity() >= (count + 1) * BaseLargeVariableWidthVector.OFFSET_WIDTH); + // verify that the validity buffer and value buffer have capacity for at least 'count' + // elements. + Assert.assertTrue( + vector.getValidityBuffer().capacity() >= DataSizeRoundingUtil.divideBy8Ceil(count)); + Assert.assertTrue( + vector.getOffsetBuffer().capacity() + >= (count + 1) * BaseLargeVariableWidthVector.OFFSET_WIDTH); } } @@ -339,7 +353,8 @@ public void testVariableRepeatedClearAndSet() throws Exception { @Test public void testRepeatedValueVectorClearAndSet() throws Exception { - try (final ListVector vector = new ListVector("", allocator, FieldType.nullable(MinorType.INT.getType()), null)) { + try (final ListVector vector = + new ListVector("", allocator, FieldType.nullable(MinorType.INT.getType()), null)) { vector.allocateNewSafe(); // Initial allocation UnionListWriter writer = vector.getWriter(); @@ -392,8 +407,9 @@ public void testStructVectorClearAndSet() throws Exception { @Test public void testFixedSizeListVectorClearAndSet() { - try (final FixedSizeListVector vector = new FixedSizeListVector("", allocator, - FieldType.nullable(new ArrowType.FixedSizeList(2)), null)) { + try (final FixedSizeListVector vector = + new FixedSizeListVector( + "", allocator, FieldType.nullable(new ArrowType.FixedSizeList(2)), null)) { vector.allocateNewSafe(); // Initial allocation UnionFixedSizeListWriter writer = vector.getWriter(); @@ -421,7 +437,8 @@ public void testFixedSizeListVectorClearAndSet() { @Test public void testUnionVectorClearAndSet() { - try (final UnionVector vector = new UnionVector("", allocator, /* field type */ null, /* call-back */ null)) { + try (final UnionVector vector = + new UnionVector("", allocator, /* field type */ null, /* call-back */ null)) { vector.allocateNewSafe(); // Initial allocation NullableIntHolder holder = new NullableIntHolder(); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestVectorReset.java b/java/vector/src/test/java/org/apache/arrow/vector/TestVectorReset.java index 71009a33375..4b8afa1cba9 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestVectorReset.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestVectorReset.java @@ -21,7 +21,6 @@ import static org.junit.Assert.assertTrue; import java.nio.charset.StandardCharsets; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -108,10 +107,11 @@ public void testLargeVariableTypeReset() { @Test public void testListTypeReset() { try (final ListVector variableList = - new ListVector("VarList", allocator, FieldType.nullable(MinorType.INT.getType()), null); - final FixedSizeListVector fixedList = - new FixedSizeListVector("FixedList", allocator, FieldType.nullable(new FixedSizeList(2)), null) - ) { + new ListVector( + "VarList", allocator, FieldType.nullable(MinorType.INT.getType()), null); + final FixedSizeListVector fixedList = + new FixedSizeListVector( + "FixedList", allocator, FieldType.nullable(new FixedSizeList(2)), null)) { // ListVector variableList.allocateNewSafe(); variableList.startNewValue(0); @@ -131,14 +131,16 @@ public void testListTypeReset() { @Test public void testStructTypeReset() { try (final NonNullableStructVector nonNullableStructVector = - new NonNullableStructVector("Struct", allocator, FieldType.nullable(MinorType.INT.getType()), null); - final StructVector structVector = - new StructVector("NullableStruct", allocator, FieldType.nullable(MinorType.INT.getType()), null) - ) { + new NonNullableStructVector( + "Struct", allocator, FieldType.nullable(MinorType.INT.getType()), null); + final StructVector structVector = + new StructVector( + "NullableStruct", allocator, FieldType.nullable(MinorType.INT.getType()), null)) { // NonNullableStructVector nonNullableStructVector.allocateNewSafe(); - IntVector structChild = nonNullableStructVector - .addOrGet("child", FieldType.nullable(new Int(32, true)), IntVector.class); + IntVector structChild = + nonNullableStructVector.addOrGet( + "child", FieldType.nullable(new Int(32, true)), IntVector.class); structChild.setNull(0); nonNullableStructVector.setValueCount(1); resetVectorAndVerify(nonNullableStructVector, nonNullableStructVector.getBuffers(false)); @@ -153,9 +155,9 @@ public void testStructTypeReset() { @Test public void testUnionTypeReset() { - try (final UnionVector vector = new UnionVector("Union", allocator, /* field type */ null, /* call-back */ null); - final IntVector dataVector = new IntVector("Int", allocator) - ) { + try (final UnionVector vector = + new UnionVector("Union", allocator, /* field type */ null, /* call-back */ null); + final IntVector dataVector = new IntVector("Int", allocator)) { vector.getBufferSize(); vector.allocateNewSafe(); dataVector.allocateNewSafe(); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestVectorSchemaRoot.java b/java/vector/src/test/java/org/apache/arrow/vector/TestVectorSchemaRoot.java index 207962eb45b..9a48845aa70 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestVectorSchemaRoot.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestVectorSchemaRoot.java @@ -26,7 +26,6 @@ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.complex.ListVector; @@ -57,7 +56,7 @@ public void terminate() { public void testResetRowCount() { final int size = 20; try (final BitVector vec1 = new BitVector("bit", allocator); - final IntVector vec2 = new IntVector("int", allocator)) { + final IntVector vec2 = new IntVector("int", allocator)) { VectorSchemaRoot vsr = VectorSchemaRoot.of(vec1, vec2); vsr.allocateNew(); @@ -92,8 +91,8 @@ private void checkCount(BitVector vec1, IntVector vec2, VectorSchemaRoot vsr, in } private VectorSchemaRoot createBatch() { - FieldType varCharType = new FieldType(true, new ArrowType.Utf8(), /*dictionary=*/null); - FieldType listType = new FieldType(true, new ArrowType.List(), /*dictionary=*/null); + FieldType varCharType = new FieldType(true, new ArrowType.Utf8(), /* dictionary= */ null); + FieldType listType = new FieldType(true, new ArrowType.List(), /* dictionary= */ null); // create the schema List schemaFields = new ArrayList<>(); @@ -157,8 +156,8 @@ private VectorSchemaRoot createBatch() { @Test public void testAddVector() { try (final IntVector intVector1 = new IntVector("intVector1", allocator); - final IntVector intVector2 = new IntVector("intVector2", allocator); - final IntVector intVector3 = new IntVector("intVector3", allocator);) { + final IntVector intVector2 = new IntVector("intVector2", allocator); + final IntVector intVector3 = new IntVector("intVector3", allocator); ) { VectorSchemaRoot original = new VectorSchemaRoot(Arrays.asList(intVector1, intVector2)); assertEquals(2, original.getFieldVectors().size()); @@ -176,7 +175,7 @@ public void testAddVector() { public void testRemoveVector() { try (final IntVector intVector1 = new IntVector("intVector1", allocator); final IntVector intVector2 = new IntVector("intVector2", allocator); - final IntVector intVector3 = new IntVector("intVector3", allocator);) { + final IntVector intVector3 = new IntVector("intVector3", allocator); ) { VectorSchemaRoot original = new VectorSchemaRoot(Arrays.asList(intVector1, intVector2, intVector3)); @@ -195,7 +194,7 @@ public void testRemoveVector() { @Test public void testSlice() { try (final IntVector intVector = new IntVector("intVector", allocator); - final Float4Vector float4Vector = new Float4Vector("float4Vector", allocator)) { + final Float4Vector float4Vector = new Float4Vector("float4Vector", allocator)) { final int numRows = 10; intVector.setValueCount(numRows); float4Vector.setValueCount(numRows); @@ -204,7 +203,8 @@ public void testSlice() { float4Vector.setSafe(i, i + 0.1f); } - final VectorSchemaRoot original = new VectorSchemaRoot(Arrays.asList(intVector, float4Vector)); + final VectorSchemaRoot original = + new VectorSchemaRoot(Arrays.asList(intVector, float4Vector)); for (int sliceIndex = 0; sliceIndex < numRows; sliceIndex++) { for (int sliceLength = 0; sliceIndex + sliceLength <= numRows; sliceLength++) { @@ -229,14 +229,15 @@ public void testSlice() { @Test(expected = IllegalArgumentException.class) public void testSliceWithInvalidParam() { try (final IntVector intVector = new IntVector("intVector", allocator); - final Float4Vector float4Vector = new Float4Vector("float4Vector", allocator)) { + final Float4Vector float4Vector = new Float4Vector("float4Vector", allocator)) { intVector.setValueCount(10); float4Vector.setValueCount(10); for (int i = 0; i < 10; i++) { intVector.setSafe(i, i); float4Vector.setSafe(i, i + 0.1f); } - final VectorSchemaRoot original = new VectorSchemaRoot(Arrays.asList(intVector, float4Vector)); + final VectorSchemaRoot original = + new VectorSchemaRoot(Arrays.asList(intVector, float4Vector)); original.slice(0, 20); } @@ -245,8 +246,8 @@ public void testSliceWithInvalidParam() { @Test public void testEquals() { try (final IntVector intVector1 = new IntVector("intVector1", allocator); - final IntVector intVector2 = new IntVector("intVector2", allocator); - final IntVector intVector3 = new IntVector("intVector3", allocator);) { + final IntVector intVector2 = new IntVector("intVector2", allocator); + final IntVector intVector3 = new IntVector("intVector3", allocator); ) { intVector1.setValueCount(5); for (int i = 0; i < 5; i++) { @@ -256,8 +257,7 @@ public void testEquals() { VectorSchemaRoot root1 = new VectorSchemaRoot(Arrays.asList(intVector1, intVector2, intVector3)); - VectorSchemaRoot root2 = - new VectorSchemaRoot(Arrays.asList(intVector1, intVector2)); + VectorSchemaRoot root2 = new VectorSchemaRoot(Arrays.asList(intVector1, intVector2)); VectorSchemaRoot root3 = new VectorSchemaRoot(Arrays.asList(intVector1, intVector2, intVector3)); @@ -274,8 +274,8 @@ public void testEquals() { @Test public void testApproxEquals() { try (final Float4Vector float4Vector1 = new Float4Vector("floatVector", allocator); - final Float4Vector float4Vector2 = new Float4Vector("floatVector", allocator); - final Float4Vector float4Vector3 = new Float4Vector("floatVector", allocator);) { + final Float4Vector float4Vector2 = new Float4Vector("floatVector", allocator); + final Float4Vector float4Vector3 = new Float4Vector("floatVector", allocator); ) { float4Vector1.setValueCount(5); float4Vector2.setValueCount(5); @@ -287,14 +287,11 @@ public void testApproxEquals() { float4Vector3.set(i, i + epsilon / 2); } - VectorSchemaRoot root1 = - new VectorSchemaRoot(Arrays.asList(float4Vector1)); + VectorSchemaRoot root1 = new VectorSchemaRoot(Arrays.asList(float4Vector1)); - VectorSchemaRoot root2 = - new VectorSchemaRoot(Arrays.asList(float4Vector2)); + VectorSchemaRoot root2 = new VectorSchemaRoot(Arrays.asList(float4Vector2)); - VectorSchemaRoot root3 = - new VectorSchemaRoot(Arrays.asList(float4Vector3)); + VectorSchemaRoot root3 = new VectorSchemaRoot(Arrays.asList(float4Vector3)); assertFalse(root1.approxEquals(root2)); assertTrue(root1.approxEquals(root3)); @@ -307,10 +304,13 @@ public void testApproxEquals() { @Test public void testSchemaSync() { - //create vector schema root + // create vector schema root try (VectorSchemaRoot schemaRoot = createBatch()) { - Schema newSchema = new Schema( - schemaRoot.getFieldVectors().stream().map(vec -> vec.getField()).collect(Collectors.toList())); + Schema newSchema = + new Schema( + schemaRoot.getFieldVectors().stream() + .map(vec -> vec.getField()) + .collect(Collectors.toList())); assertNotEquals(newSchema, schemaRoot.getSchema()); assertTrue(schemaRoot.syncSchema()); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/TestVectorUnloadLoad.java b/java/vector/src/test/java/org/apache/arrow/vector/TestVectorUnloadLoad.java index eac72f4b2c8..829afa7fc12 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/TestVectorUnloadLoad.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/TestVectorUnloadLoad.java @@ -26,7 +26,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -68,8 +67,8 @@ public void testNullCodec() { final Schema schema = new Schema(Collections.emptyList()); try (final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { root.setRowCount(1); - final VectorUnloader unloader = new VectorUnloader( - root, /*includeNulls*/ true, /*codec*/ null, /*alignBuffers*/ true); + final VectorUnloader unloader = + new VectorUnloader(root, /*includeNulls*/ true, /*codec*/ null, /*alignBuffers*/ true); unloader.getRecordBatch().close(); } } @@ -79,10 +78,10 @@ public void testUnloadLoad() throws IOException { int count = 10000; Schema schema; - try ( - BufferAllocator originalVectorsAllocator = + try (BufferAllocator originalVectorsAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); - NonNullableStructVector parent = NonNullableStructVector.empty("parent", originalVectorsAllocator)) { + NonNullableStructVector parent = + NonNullableStructVector.empty("parent", originalVectorsAllocator)) { // write some data ComplexWriter writer = new ComplexWriterImpl("root", parent); @@ -101,11 +100,10 @@ public void testUnloadLoad() throws IOException { FieldVector root = parent.getChild("root"); schema = new Schema(root.getField().getChildren()); VectorUnloader vectorUnloader = newVectorUnloader(root); - try ( - ArrowRecordBatch recordBatch = vectorUnloader.getRecordBatch(); - BufferAllocator finalVectorsAllocator = allocator.newChildAllocator("final vectors", 0, Integer.MAX_VALUE); - VectorSchemaRoot newRoot = VectorSchemaRoot.create(schema, finalVectorsAllocator); - ) { + try (ArrowRecordBatch recordBatch = vectorUnloader.getRecordBatch(); + BufferAllocator finalVectorsAllocator = + allocator.newChildAllocator("final vectors", 0, Integer.MAX_VALUE); + VectorSchemaRoot newRoot = VectorSchemaRoot.create(schema, finalVectorsAllocator); ) { // load it VectorLoader vectorLoader = new VectorLoader(newRoot); @@ -128,10 +126,10 @@ public void testUnloadLoad() throws IOException { public void testUnloadLoadAddPadding() throws IOException { int count = 10000; Schema schema; - try ( - BufferAllocator originalVectorsAllocator = + try (BufferAllocator originalVectorsAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); - NonNullableStructVector parent = NonNullableStructVector.empty("parent", originalVectorsAllocator)) { + NonNullableStructVector parent = + NonNullableStructVector.empty("parent", originalVectorsAllocator)) { // write some data ComplexWriter writer = new ComplexWriterImpl("root", parent); @@ -152,11 +150,10 @@ public void testUnloadLoadAddPadding() throws IOException { FieldVector root = parent.getChild("root"); schema = new Schema(root.getField().getChildren()); VectorUnloader vectorUnloader = newVectorUnloader(root); - try ( - ArrowRecordBatch recordBatch = vectorUnloader.getRecordBatch(); - BufferAllocator finalVectorsAllocator = allocator.newChildAllocator("final vectors", 0, Integer.MAX_VALUE); - VectorSchemaRoot newRoot = VectorSchemaRoot.create(schema, finalVectorsAllocator); - ) { + try (ArrowRecordBatch recordBatch = vectorUnloader.getRecordBatch(); + BufferAllocator finalVectorsAllocator = + allocator.newChildAllocator("final vectors", 0, Integer.MAX_VALUE); + VectorSchemaRoot newRoot = VectorSchemaRoot.create(schema, finalVectorsAllocator); ) { List oldBuffers = recordBatch.getBuffers(); List newBuffers = new ArrayList<>(); for (ArrowBuf oldBuffer : oldBuffers) { @@ -175,7 +172,7 @@ public void testUnloadLoadAddPadding() throws IOException { } try (ArrowRecordBatch newBatch = - new ArrowRecordBatch(recordBatch.getLength(), recordBatch.getNodes(), newBuffers);) { + new ArrowRecordBatch(recordBatch.getLength(), recordBatch.getNodes(), newBuffers); ) { // load it VectorLoader vectorLoader = new VectorLoader(newRoot); @@ -200,18 +197,23 @@ public void testUnloadLoadAddPadding() throws IOException { } /** - * The validity buffer can be empty if: - * - all values are defined. - * - all values are null. + * The validity buffer can be empty if: - all values are defined. - all values are null. * * @throws IOException on error */ @Test public void testLoadValidityBuffer() throws IOException { - Schema schema = new Schema(asList( - new Field("intDefined", FieldType.nullable(new ArrowType.Int(32, true)), Collections.emptyList()), - new Field("intNull", FieldType.nullable(new ArrowType.Int(32, true)), Collections.emptyList()) - )); + Schema schema = + new Schema( + asList( + new Field( + "intDefined", + FieldType.nullable(new ArrowType.Int(32, true)), + Collections.emptyList()), + new Field( + "intNull", + FieldType.nullable(new ArrowType.Int(32, true)), + Collections.emptyList()))); int count = 10; ArrowBuf[] values = new ArrowBuf[4]; for (int i = 0; i < 4; i += 2) { @@ -241,12 +243,14 @@ public void testLoadValidityBuffer() throws IOException { * values[3] - data buffer for second vector */ - try ( - ArrowRecordBatch recordBatch = new ArrowRecordBatch(count, asList(new ArrowFieldNode(count, 0), - new ArrowFieldNode(count, count)), asList(values[0], values[1], values[2], values[3])); - BufferAllocator finalVectorsAllocator = allocator.newChildAllocator("final vectors", 0, Integer.MAX_VALUE); - VectorSchemaRoot newRoot = VectorSchemaRoot.create(schema, finalVectorsAllocator); - ) { + try (ArrowRecordBatch recordBatch = + new ArrowRecordBatch( + count, + asList(new ArrowFieldNode(count, 0), new ArrowFieldNode(count, count)), + asList(values[0], values[1], values[2], values[3])); + BufferAllocator finalVectorsAllocator = + allocator.newChildAllocator("final vectors", 0, Integer.MAX_VALUE); + VectorSchemaRoot newRoot = VectorSchemaRoot.create(schema, finalVectorsAllocator); ) { // load it VectorLoader vectorLoader = new VectorLoader(newRoot); @@ -287,15 +291,20 @@ public void testLoadValidityBuffer() throws IOException { @Test public void testUnloadLoadDuplicates() throws IOException { int count = 10; - Schema schema = new Schema(asList( - new Field("duplicate", FieldType.nullable(new ArrowType.Int(32, true)), Collections.emptyList()), - new Field("duplicate", FieldType.nullable(new ArrowType.Int(32, true)), Collections.emptyList()) - )); - - try ( - BufferAllocator originalVectorsAllocator = - allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); - ) { + Schema schema = + new Schema( + asList( + new Field( + "duplicate", + FieldType.nullable(new ArrowType.Int(32, true)), + Collections.emptyList()), + new Field( + "duplicate", + FieldType.nullable(new ArrowType.Int(32, true)), + Collections.emptyList()))); + + try (BufferAllocator originalVectorsAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); ) { List sources = new ArrayList<>(); for (Field field : schema.getFields()) { FieldVector vector = field.createVector(originalVectorsAllocator); @@ -311,9 +320,9 @@ public void testUnloadLoadDuplicates() throws IOException { try (VectorSchemaRoot root = new VectorSchemaRoot(schema.getFields(), sources, count)) { VectorUnloader vectorUnloader = new VectorUnloader(root); try (ArrowRecordBatch recordBatch = vectorUnloader.getRecordBatch(); - BufferAllocator finalVectorsAllocator = + BufferAllocator finalVectorsAllocator = allocator.newChildAllocator("final vectors", 0, Integer.MAX_VALUE); - VectorSchemaRoot newRoot = VectorSchemaRoot.create(schema, finalVectorsAllocator);) { + VectorSchemaRoot newRoot = VectorSchemaRoot.create(schema, finalVectorsAllocator); ) { // load it VectorLoader vectorLoader = new VectorLoader(newRoot); vectorLoader.load(recordBatch); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/compare/TestRangeEqualsVisitor.java b/java/vector/src/test/java/org/apache/arrow/vector/compare/TestRangeEqualsVisitor.java index ab8c6c63489..57b2647e964 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/compare/TestRangeEqualsVisitor.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/compare/TestRangeEqualsVisitor.java @@ -23,7 +23,6 @@ import java.nio.charset.Charset; import java.util.Arrays; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.BigIntVector; @@ -80,7 +79,7 @@ public void terminate() throws Exception { @Test public void testIntVectorEqualsWithNull() { try (final IntVector vector1 = new IntVector("int", allocator); - final IntVector vector2 = new IntVector("int", allocator)) { + final IntVector vector2 = new IntVector("int", allocator)) { setVector(vector1, 1, 2); setVector(vector2, 1, null); @@ -92,8 +91,8 @@ public void testIntVectorEqualsWithNull() { @Test public void testEqualsWithTypeChange() { try (final IntVector vector1 = new IntVector("vector", allocator); - final IntVector vector2 = new IntVector("vector", allocator); - final BigIntVector vector3 = new BigIntVector("vector", allocator)) { + final IntVector vector2 = new IntVector("vector", allocator); + final BigIntVector vector3 = new BigIntVector("vector", allocator)) { setVector(vector1, 1, 2); setVector(vector2, 1, 2); @@ -109,7 +108,7 @@ public void testEqualsWithTypeChange() { @Test public void testBaseFixedWidthVectorRangeEqual() { try (final IntVector vector1 = new IntVector("int", allocator); - final IntVector vector2 = new IntVector("int", allocator)) { + final IntVector vector2 = new IntVector("int", allocator)) { setVector(vector1, 1, 2, 3, 4, 5); setVector(vector2, 11, 2, 3, 4, 55); @@ -122,7 +121,7 @@ public void testBaseFixedWidthVectorRangeEqual() { @Test public void testBaseVariableVectorRangeEquals() { try (final VarCharVector vector1 = new VarCharVector("varchar", allocator); - final VarCharVector vector2 = new VarCharVector("varchar", allocator)) { + final VarCharVector vector2 = new VarCharVector("varchar", allocator)) { setVector(vector1, STR1, STR2, STR3, STR2, STR1); setVector(vector2, STR1, STR2, STR3, STR2, STR1); @@ -135,7 +134,7 @@ public void testBaseVariableVectorRangeEquals() { @Test public void testListVectorWithDifferentChild() { try (final ListVector vector1 = ListVector.empty("list", allocator); - final ListVector vector2 = ListVector.empty("list", allocator);) { + final ListVector vector2 = ListVector.empty("list", allocator); ) { vector1.allocateNew(); vector1.initializeChildrenFromFields( @@ -153,12 +152,12 @@ public void testListVectorWithDifferentChild() { @Test public void testListVectorRangeEquals() { try (final ListVector vector1 = ListVector.empty("list", allocator); - final ListVector vector2 = ListVector.empty("list", allocator);) { + final ListVector vector2 = ListVector.empty("list", allocator); ) { UnionListWriter writer1 = vector1.getWriter(); writer1.allocate(); - //set some values + // set some values writeListVector(writer1, new int[] {1, 2}); writeListVector(writer1, new int[] {3, 4}); writeListVector(writer1, new int[] {5, 6}); @@ -169,7 +168,7 @@ public void testListVectorRangeEquals() { UnionListWriter writer2 = vector2.getWriter(); writer2.allocate(); - //set some values + // set some values writeListVector(writer2, new int[] {0, 0}); writeListVector(writer2, new int[] {3, 4}); writeListVector(writer2, new int[] {5, 6}); @@ -185,16 +184,16 @@ public void testListVectorRangeEquals() { @Test public void testBitVectorRangeEquals() { try (final BitVector vector1 = new BitVector("v1", allocator); - final BitVector vector2 = new BitVector("v2", allocator);) { + final BitVector vector2 = new BitVector("v2", allocator); ) { - boolean[] v1 = new boolean[]{true, false, true, true, true}; - boolean[] v2 = new boolean[]{false, true, true, true, false}; + boolean[] v1 = new boolean[] {true, false, true, true, true}; + boolean[] v2 = new boolean[] {false, true, true, true, false}; vector1.setValueCount(5); - for (int i = 0; i < 5; i ++) { + for (int i = 0; i < 5; i++) { vector1.set(i, v1[i] ? 1 : 0); } vector2.setValueCount(5); - for (int i = 0; i < 5; i ++) { + for (int i = 0; i < 5; i++) { vector2.set(i, v2[i] ? 1 : 0); } @@ -207,12 +206,12 @@ public void testBitVectorRangeEquals() { @Test public void testFixedSizeListVectorRangeEquals() { try (final FixedSizeListVector vector1 = FixedSizeListVector.empty("list", 2, allocator); - final FixedSizeListVector vector2 = FixedSizeListVector.empty("list", 2, allocator);) { + final FixedSizeListVector vector2 = FixedSizeListVector.empty("list", 2, allocator); ) { UnionFixedSizeListWriter writer1 = vector1.getWriter(); writer1.allocate(); - //set some values + // set some values writeFixedSizeListVector(writer1, new int[] {1, 2}); writeFixedSizeListVector(writer1, new int[] {3, 4}); writeFixedSizeListVector(writer1, new int[] {5, 6}); @@ -223,7 +222,7 @@ public void testFixedSizeListVectorRangeEquals() { UnionFixedSizeListWriter writer2 = vector2.getWriter(); writer2.allocate(); - //set some values + // set some values writeFixedSizeListVector(writer2, new int[] {0, 0}); writeFixedSizeListVector(writer2, new int[] {3, 4}); writeFixedSizeListVector(writer2, new int[] {5, 6}); @@ -240,27 +239,35 @@ public void testFixedSizeListVectorRangeEquals() { @Test public void testLargeVariableWidthVectorRangeEquals() { try (final LargeVarCharVector vector1 = new LargeVarCharVector("vector1", allocator); - final LargeVarCharVector vector2 = new LargeVarCharVector("vector2", allocator)) { + final LargeVarCharVector vector2 = new LargeVarCharVector("vector2", allocator)) { setVector(vector1, "aaa", "bbb", "ccc", null, "ddd"); setVector(vector2, "ccc", "aaa", "bbb", null, "ddd"); - RangeEqualsVisitor visitor = new RangeEqualsVisitor(vector1, vector2, - (v1, v2) -> new TypeEqualsVisitor(v2, /*check name*/ false, /*check metadata*/ false).equals(v1)); + RangeEqualsVisitor visitor = + new RangeEqualsVisitor( + vector1, + vector2, + (v1, v2) -> + new TypeEqualsVisitor(v2, /*check name*/ false, /*check metadata*/ false) + .equals(v1)); - assertFalse(visitor.rangeEquals(new Range(/*left start*/ 0, /*right start*/ 0, /*length*/ 1))); + assertFalse( + visitor.rangeEquals(new Range(/*left start*/ 0, /*right start*/ 0, /*length*/ 1))); assertTrue(visitor.rangeEquals(new Range(/*left start*/ 0, /*right start*/ 1, /*length*/ 1))); - assertFalse(visitor.rangeEquals(new Range(/*left start*/ 0, /*right start*/ 0, /*length*/ 3))); + assertFalse( + visitor.rangeEquals(new Range(/*left start*/ 0, /*right start*/ 0, /*length*/ 3))); assertTrue(visitor.rangeEquals(new Range(/*left start*/ 0, /*right start*/ 1, /*length*/ 2))); assertTrue(visitor.rangeEquals(new Range(/*left start*/ 3, /*right start*/ 3, /*length*/ 1))); assertTrue(visitor.rangeEquals(new Range(/*left start*/ 3, /*right start*/ 3, /*length*/ 2))); - assertFalse(visitor.rangeEquals(new Range(/*left start*/ 2, /*right start*/ 2, /*length*/ 2))); + assertFalse( + visitor.rangeEquals(new Range(/*left start*/ 2, /*right start*/ 2, /*length*/ 2))); } } @Test public void testStructVectorRangeEquals() { try (final StructVector vector1 = StructVector.empty("struct", allocator); - final StructVector vector2 = StructVector.empty("struct", allocator);) { + final StructVector vector2 = StructVector.empty("struct", allocator); ) { vector1.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); vector1.addOrGet("f1", FieldType.nullable(new ArrowType.Int(64, true)), BigIntVector.class); vector2.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); @@ -293,9 +300,10 @@ public void testStructVectorRangeEquals() { @Test public void testUnionVectorRangeEquals() { - try (final UnionVector vector1 = new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); - final UnionVector vector2 = - new UnionVector("union", allocator, /* field type */ null, /* call-back */ null);) { + try (final UnionVector vector1 = + new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); + final UnionVector vector2 = + new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); ) { final NullableUInt4Holder uInt4Holder = new NullableUInt4Holder(); uInt4Holder.value = 10; @@ -331,13 +339,13 @@ public void testUnionVectorRangeEquals() { } /** - * Test comparing two union vectors. - * The two vectors are different in total, but have a range with equal values. + * Test comparing two union vectors. The two vectors are different in total, but have a range with + * equal values. */ @Test public void testUnionVectorSubRangeEquals() { try (final UnionVector vector1 = new UnionVector("union", allocator, null, null); - final UnionVector vector2 = new UnionVector("union", allocator, null, null);) { + final UnionVector vector2 = new UnionVector("union", allocator, null, null); ) { final NullableUInt4Holder uInt4Holder = new NullableUInt4Holder(); uInt4Holder.value = 10; @@ -400,15 +408,19 @@ public void testDenseUnionVectorEquals() { float8Holder.value = 800D; try (DenseUnionVector vector1 = new DenseUnionVector("vector1", allocator, null, null); - DenseUnionVector vector2 = new DenseUnionVector("vector2", allocator, null, null)) { + DenseUnionVector vector2 = new DenseUnionVector("vector2", allocator, null, null)) { vector1.allocateNew(); vector2.allocateNew(); // populate vector1: {100, 200L, null, 400F, 800D} - byte intTypeId = vector1.registerNewTypeId(Field.nullable("int", Types.MinorType.INT.getType())); - byte longTypeId = vector1.registerNewTypeId(Field.nullable("long", Types.MinorType.BIGINT.getType())); - byte floatTypeId = vector1.registerNewTypeId(Field.nullable("float", Types.MinorType.FLOAT4.getType())); - byte doubleTypeId = vector1.registerNewTypeId(Field.nullable("double", Types.MinorType.FLOAT8.getType())); + byte intTypeId = + vector1.registerNewTypeId(Field.nullable("int", Types.MinorType.INT.getType())); + byte longTypeId = + vector1.registerNewTypeId(Field.nullable("long", Types.MinorType.BIGINT.getType())); + byte floatTypeId = + vector1.registerNewTypeId(Field.nullable("float", Types.MinorType.FLOAT4.getType())); + byte doubleTypeId = + vector1.registerNewTypeId(Field.nullable("double", Types.MinorType.FLOAT8.getType())); vector1.setTypeId(0, intTypeId); vector1.setSafe(0, intHolder); @@ -426,9 +438,12 @@ public void testDenseUnionVectorEquals() { // populate vector2: {400F, null, 200L, null, 400F, 800D, 100} intTypeId = vector2.registerNewTypeId(Field.nullable("int", Types.MinorType.INT.getType())); - longTypeId = vector2.registerNewTypeId(Field.nullable("long", Types.MinorType.BIGINT.getType())); - floatTypeId = vector2.registerNewTypeId(Field.nullable("float", Types.MinorType.FLOAT4.getType())); - doubleTypeId = vector2.registerNewTypeId(Field.nullable("double", Types.MinorType.FLOAT8.getType())); + longTypeId = + vector2.registerNewTypeId(Field.nullable("long", Types.MinorType.BIGINT.getType())); + floatTypeId = + vector2.registerNewTypeId(Field.nullable("float", Types.MinorType.FLOAT4.getType())); + doubleTypeId = + vector2.registerNewTypeId(Field.nullable("double", Types.MinorType.FLOAT8.getType())); vector2.setTypeId(0, floatTypeId); vector2.setSafe(0, float4Holder); @@ -480,7 +495,7 @@ public void testDenseUnionVectorEquals() { @Test public void testEqualsWithOutTypeCheck() { try (final IntVector intVector = new IntVector("int", allocator); - final ZeroVector zeroVector = new ZeroVector("zero")) { + final ZeroVector zeroVector = new ZeroVector("zero")) { assertTrue(VectorEqualsVisitor.vectorEquals(intVector, zeroVector, null)); assertTrue(VectorEqualsVisitor.vectorEquals(zeroVector, intVector, null)); @@ -490,8 +505,8 @@ public void testEqualsWithOutTypeCheck() { @Test public void testFloat4ApproxEquals() { try (final Float4Vector vector1 = new Float4Vector("float", allocator); - final Float4Vector vector2 = new Float4Vector("float", allocator); - final Float4Vector vector3 = new Float4Vector("float", allocator)) { + final Float4Vector vector2 = new Float4Vector("float", allocator); + final Float4Vector vector3 = new Float4Vector("float", allocator)) { final float epsilon = 1.0E-6f; setVector(vector1, 1.1f, 2.2f); @@ -511,8 +526,8 @@ public void testFloat4ApproxEquals() { @Test public void testFloat8ApproxEquals() { try (final Float8Vector vector1 = new Float8Vector("float", allocator); - final Float8Vector vector2 = new Float8Vector("float", allocator); - final Float8Vector vector3 = new Float8Vector("float", allocator)) { + final Float8Vector vector2 = new Float8Vector("float", allocator); + final Float8Vector vector3 = new Float8Vector("float", allocator)) { final float epsilon = 1.0E-6f; setVector(vector1, 1.1, 2.2); @@ -530,18 +545,30 @@ public void testStructVectorApproxEquals() { try (final StructVector right = StructVector.empty("struct", allocator); final StructVector left1 = StructVector.empty("struct", allocator); final StructVector left2 = StructVector.empty("struct", allocator)) { - right.addOrGet("f0", - FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)), Float4Vector.class); - right.addOrGet("f1", - FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), Float8Vector.class); - left1.addOrGet("f0", - FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)), Float4Vector.class); - left1.addOrGet("f1", - FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), Float8Vector.class); - left2.addOrGet("f0", - FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)), Float4Vector.class); - left2.addOrGet("f1", - FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), Float8Vector.class); + right.addOrGet( + "f0", + FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)), + Float4Vector.class); + right.addOrGet( + "f1", + FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), + Float8Vector.class); + left1.addOrGet( + "f0", + FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)), + Float4Vector.class); + left1.addOrGet( + "f1", + FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), + Float8Vector.class); + left2.addOrGet( + "f0", + FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)), + Float4Vector.class); + left2.addOrGet( + "f1", + FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), + Float8Vector.class); final float epsilon = 1.0E-6f; @@ -571,9 +598,12 @@ public void testStructVectorApproxEquals() { @Test public void testUnionVectorApproxEquals() { - try (final UnionVector right = new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); - final UnionVector left1 = new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); - final UnionVector left2 = new UnionVector("union", allocator, /* field type */ null, /* call-back */ null);) { + try (final UnionVector right = + new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); + final UnionVector left1 = + new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); + final UnionVector left2 = + new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); ) { final NullableFloat4Holder float4Holder = new NullableFloat4Holder(); float4Holder.value = 1.01f; @@ -627,16 +657,18 @@ public void testDenseUnionVectorApproxEquals() { final double doubleEpsilon = 0.02; try (final DenseUnionVector vector1 = new DenseUnionVector("vector1", allocator, null, null); - final DenseUnionVector vector2 = new DenseUnionVector("vector2", allocator, null, null); - final DenseUnionVector vector3 = new DenseUnionVector("vector2", allocator, null, null)) { + final DenseUnionVector vector2 = new DenseUnionVector("vector2", allocator, null, null); + final DenseUnionVector vector3 = new DenseUnionVector("vector2", allocator, null, null)) { vector1.allocateNew(); vector2.allocateNew(); vector3.allocateNew(); // populate vector1: {1.0f, 2.0D} - byte floatTypeId = vector1.registerNewTypeId(Field.nullable("float", Types.MinorType.FLOAT4.getType())); - byte doubleTypeId = vector1.registerNewTypeId(Field.nullable("double", Types.MinorType.FLOAT8.getType())); + byte floatTypeId = + vector1.registerNewTypeId(Field.nullable("float", Types.MinorType.FLOAT4.getType())); + byte doubleTypeId = + vector1.registerNewTypeId(Field.nullable("double", Types.MinorType.FLOAT8.getType())); float4Holder.value = 1.0f; vector1.setTypeId(0, floatTypeId); @@ -647,8 +679,10 @@ public void testDenseUnionVectorApproxEquals() { vector1.setValueCount(2); // populate vector2: {1.01f, 2.01D} - floatTypeId = vector2.registerNewTypeId(Field.nullable("float", Types.MinorType.FLOAT4.getType())); - doubleTypeId = vector2.registerNewTypeId(Field.nullable("double", Types.MinorType.FLOAT8.getType())); + floatTypeId = + vector2.registerNewTypeId(Field.nullable("float", Types.MinorType.FLOAT4.getType())); + doubleTypeId = + vector2.registerNewTypeId(Field.nullable("double", Types.MinorType.FLOAT8.getType())); float4Holder.value = 1.01f; vector2.setTypeId(0, floatTypeId); @@ -659,8 +693,10 @@ public void testDenseUnionVectorApproxEquals() { vector2.setValueCount(2); // populate vector3: {1.05f, 2.05D} - floatTypeId = vector3.registerNewTypeId(Field.nullable("float", Types.MinorType.FLOAT4.getType())); - doubleTypeId = vector3.registerNewTypeId(Field.nullable("double", Types.MinorType.FLOAT8.getType())); + floatTypeId = + vector3.registerNewTypeId(Field.nullable("float", Types.MinorType.FLOAT4.getType())); + doubleTypeId = + vector3.registerNewTypeId(Field.nullable("double", Types.MinorType.FLOAT8.getType())); float4Holder.value = 1.05f; vector3.setTypeId(0, floatTypeId); @@ -674,19 +710,27 @@ public void testDenseUnionVectorApproxEquals() { Range range = new Range(0, 0, 2); // compare vector1 and vector2 - ApproxEqualsVisitor approxEqualsVisitor = new ApproxEqualsVisitor( - vector1, vector2, - new ValueEpsilonEqualizers.Float4EpsilonEqualizer(floatEpsilon), - new ValueEpsilonEqualizers.Float8EpsilonEqualizer(doubleEpsilon), - (v1, v2) -> new TypeEqualsVisitor(v2, /* check name */ false, /* check meta */ true).equals(v1)); + ApproxEqualsVisitor approxEqualsVisitor = + new ApproxEqualsVisitor( + vector1, + vector2, + new ValueEpsilonEqualizers.Float4EpsilonEqualizer(floatEpsilon), + new ValueEpsilonEqualizers.Float8EpsilonEqualizer(doubleEpsilon), + (v1, v2) -> + new TypeEqualsVisitor(v2, /* check name */ false, /* check meta */ true) + .equals(v1)); assertTrue(approxEqualsVisitor.rangeEquals(range)); // compare vector1 and vector3 - approxEqualsVisitor = new ApproxEqualsVisitor( - vector1, vector3, - new ValueEpsilonEqualizers.Float4EpsilonEqualizer(floatEpsilon), - new ValueEpsilonEqualizers.Float8EpsilonEqualizer(doubleEpsilon), - (v1, v2) -> new TypeEqualsVisitor(v2, /* check name */ false, /* check meta */ true).equals(v1)); + approxEqualsVisitor = + new ApproxEqualsVisitor( + vector1, + vector3, + new ValueEpsilonEqualizers.Float4EpsilonEqualizer(floatEpsilon), + new ValueEpsilonEqualizers.Float8EpsilonEqualizer(doubleEpsilon), + (v1, v2) -> + new TypeEqualsVisitor(v2, /* check name */ false, /* check meta */ true) + .equals(v1)); assertFalse(approxEqualsVisitor.rangeEquals(range)); } } @@ -694,8 +738,8 @@ public void testDenseUnionVectorApproxEquals() { @Test public void testListVectorApproxEquals() { try (final ListVector right = ListVector.empty("list", allocator); - final ListVector left1 = ListVector.empty("list", allocator); - final ListVector left2 = ListVector.empty("list", allocator);) { + final ListVector left1 = ListVector.empty("list", allocator); + final ListVector left2 = ListVector.empty("list", allocator); ) { final float epsilon = 1.0E-6f; @@ -739,7 +783,7 @@ private void writeStructVector(NullableStructWriter writer, float value1, double private void writeListVector(UnionListWriter writer, int[] values) { writer.startList(); - for (int v: values) { + for (int v : values) { writer.integer().writeInt(v); } writer.endList(); @@ -747,7 +791,7 @@ private void writeListVector(UnionListWriter writer, int[] values) { private void writeFixedSizeListVector(UnionFixedSizeListWriter writer, int[] values) { writer.startList(); - for (int v: values) { + for (int v : values) { writer.integer().writeInt(v); } writer.endList(); @@ -755,7 +799,7 @@ private void writeFixedSizeListVector(UnionFixedSizeListWriter writer, int[] val private void writeListVector(UnionListWriter writer, double[] values) { writer.startList(); - for (double v: values) { + for (double v : values) { writer.float8().writeFloat8(v); } writer.endList(); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/compare/TestTypeEqualsVisitor.java b/java/vector/src/test/java/org/apache/arrow/vector/compare/TestTypeEqualsVisitor.java index 62fa0336ea9..5e77d557b50 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/compare/TestTypeEqualsVisitor.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/compare/TestTypeEqualsVisitor.java @@ -22,7 +22,6 @@ import java.util.HashMap; import java.util.Map; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.BigIntVector; @@ -59,7 +58,7 @@ public void terminate() throws Exception { @Test public void testTypeEqualsWithName() { try (final IntVector right = new IntVector("int", allocator); - final IntVector left1 = new IntVector("int", allocator); + final IntVector left1 = new IntVector("int", allocator); final IntVector left2 = new IntVector("int2", allocator)) { TypeEqualsVisitor visitor = new TypeEqualsVisitor(right); @@ -72,14 +71,14 @@ public void testTypeEqualsWithName() { public void testTypeEqualsWithMetadata() { Map metadata = new HashMap<>(); metadata.put("key1", "value1"); - FieldType typeWithoutMeta = new FieldType(true, new ArrowType.Int(32, true), - null, null); - FieldType typeWithMeta = new FieldType(true, new ArrowType.Int(32, true), - null, metadata); + FieldType typeWithoutMeta = new FieldType(true, new ArrowType.Int(32, true), null, null); + FieldType typeWithMeta = new FieldType(true, new ArrowType.Int(32, true), null, metadata); - try (IntVector right = (IntVector) typeWithoutMeta.createNewSingleVector("int", allocator, null); - IntVector left1 = (IntVector) typeWithoutMeta.createNewSingleVector("int", allocator, null); - IntVector left2 = (IntVector) typeWithMeta.createNewSingleVector("int", allocator, null)) { + try (IntVector right = + (IntVector) typeWithoutMeta.createNewSingleVector("int", allocator, null); + IntVector left1 = + (IntVector) typeWithoutMeta.createNewSingleVector("int", allocator, null); + IntVector left2 = (IntVector) typeWithMeta.createNewSingleVector("int", allocator, null)) { TypeEqualsVisitor visitor = new TypeEqualsVisitor(right); assertTrue(visitor.equals(left1)); @@ -90,8 +89,8 @@ public void testTypeEqualsWithMetadata() { @Test public void testListTypeEquals() { try (final ListVector right = ListVector.empty("list", allocator); - final ListVector left1 = ListVector.empty("list", allocator); - final ListVector left2 = ListVector.empty("list", allocator)) { + final ListVector left1 = ListVector.empty("list", allocator); + final ListVector left2 = ListVector.empty("list", allocator)) { right.addOrGetVector(FieldType.nullable(new ArrowType.Utf8())); left1.addOrGetVector(FieldType.nullable(new ArrowType.Utf8())); @@ -106,8 +105,8 @@ public void testListTypeEquals() { @Test public void testStructTypeEquals() { try (final StructVector right = StructVector.empty("struct", allocator); - final StructVector left1 = StructVector.empty("struct", allocator); - final StructVector left2 = StructVector.empty("struct", allocator)) { + final StructVector left1 = StructVector.empty("struct", allocator); + final StructVector left2 = StructVector.empty("struct", allocator)) { right.addOrGet("child", FieldType.nullable(new ArrowType.Utf8()), VarCharVector.class); left1.addOrGet("child", FieldType.nullable(new ArrowType.Utf8()), VarCharVector.class); @@ -121,9 +120,12 @@ public void testStructTypeEquals() { @Test public void testUnionTypeEquals() { - try (final UnionVector right = new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); - final UnionVector left1 = new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); - final UnionVector left2 = new UnionVector("union", allocator, /* field type */ null, /* call-back */ null)) { + try (final UnionVector right = + new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); + final UnionVector left1 = + new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); + final UnionVector left2 = + new UnionVector("union", allocator, /* field type */ null, /* call-back */ null)) { right.addVector(new IntVector("int", allocator)); left1.addVector(new IntVector("int", allocator)); @@ -138,15 +140,19 @@ public void testUnionTypeEquals() { @Test public void testDenseUnionTypeEquals() { try (DenseUnionVector vector1 = new DenseUnionVector("vector1", allocator, null, null); - DenseUnionVector vector2 = new DenseUnionVector("vector2", allocator, null, null)) { + DenseUnionVector vector2 = new DenseUnionVector("vector2", allocator, null, null)) { vector1.allocateNew(); vector2.allocateNew(); // set children for vector1 - byte intTypeId = vector1.registerNewTypeId(Field.nullable("int", Types.MinorType.INT.getType())); - byte longTypeId = vector1.registerNewTypeId(Field.nullable("long", Types.MinorType.BIGINT.getType())); - byte floatTypeId = vector1.registerNewTypeId(Field.nullable("float", Types.MinorType.FLOAT4.getType())); - byte doubleTypeId = vector1.registerNewTypeId(Field.nullable("double", Types.MinorType.FLOAT8.getType())); + byte intTypeId = + vector1.registerNewTypeId(Field.nullable("int", Types.MinorType.INT.getType())); + byte longTypeId = + vector1.registerNewTypeId(Field.nullable("long", Types.MinorType.BIGINT.getType())); + byte floatTypeId = + vector1.registerNewTypeId(Field.nullable("float", Types.MinorType.FLOAT4.getType())); + byte doubleTypeId = + vector1.registerNewTypeId(Field.nullable("double", Types.MinorType.FLOAT8.getType())); vector1.addVector(floatTypeId, new Float4Vector("", allocator)); vector1.addVector(longTypeId, new BigIntVector("", allocator)); @@ -155,9 +161,12 @@ public void testDenseUnionTypeEquals() { // set children for vector2 intTypeId = vector2.registerNewTypeId(Field.nullable("int", Types.MinorType.INT.getType())); - longTypeId = vector2.registerNewTypeId(Field.nullable("long", Types.MinorType.BIGINT.getType())); - floatTypeId = vector2.registerNewTypeId(Field.nullable("float", Types.MinorType.FLOAT4.getType())); - doubleTypeId = vector2.registerNewTypeId(Field.nullable("double", Types.MinorType.FLOAT8.getType())); + longTypeId = + vector2.registerNewTypeId(Field.nullable("long", Types.MinorType.BIGINT.getType())); + floatTypeId = + vector2.registerNewTypeId(Field.nullable("float", Types.MinorType.FLOAT4.getType())); + doubleTypeId = + vector2.registerNewTypeId(Field.nullable("double", Types.MinorType.FLOAT8.getType())); // add vectors in a different order vector2.addVector(intTypeId, new IntVector("", allocator)); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/complex/TestDenseUnionBufferSize.java b/java/vector/src/test/java/org/apache/arrow/vector/complex/TestDenseUnionBufferSize.java index 82ef7a479d0..d98677e898e 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/complex/TestDenseUnionBufferSize.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/complex/TestDenseUnionBufferSize.java @@ -35,14 +35,22 @@ public class TestDenseUnionBufferSize { @Test public void testBufferSize() { try (BufferAllocator allocator = new RootAllocator(); - DenseUnionVector duv = new DenseUnionVector("duv", allocator, - FieldType.nullable(new ArrowType.Union(UnionMode.Dense, null)), null)) { + DenseUnionVector duv = + new DenseUnionVector( + "duv", + allocator, + FieldType.nullable(new ArrowType.Union(UnionMode.Dense, null)), + null)) { byte aTypeId = 42; byte bTypeId = 7; - duv.addVector(aTypeId, new IntVector("a", FieldType.notNullable(new ArrowType.Int(32, true)), allocator)); - duv.addVector(bTypeId, new VarBinaryVector("b", FieldType.notNullable(new ArrowType.Binary()), allocator)); + duv.addVector( + aTypeId, + new IntVector("a", FieldType.notNullable(new ArrowType.Int(32, true)), allocator)); + duv.addVector( + bTypeId, + new VarBinaryVector("b", FieldType.notNullable(new ArrowType.Binary()), allocator)); NullableIntHolder intHolder = new NullableIntHolder(); NullableVarBinaryHolder varBinaryHolder = new NullableVarBinaryHolder(); @@ -70,12 +78,15 @@ public void testBufferSize() { long overhead = DenseUnionVector.TYPE_WIDTH + DenseUnionVector.OFFSET_WIDTH; - assertEquals(overhead * count + intVector.getBufferSize() + varBinaryVector.getBufferSize(), - duv.getBufferSize()); - - assertEquals(overhead * (aCount + 1) + intVector.getBufferSizeFor(aCount) + varBinaryVector.getBufferSizeFor(1), - duv.getBufferSizeFor(aCount + 1)); + assertEquals( + overhead * count + intVector.getBufferSize() + varBinaryVector.getBufferSize(), + duv.getBufferSize()); + assertEquals( + overhead * (aCount + 1) + + intVector.getBufferSizeFor(aCount) + + varBinaryVector.getBufferSizeFor(1), + duv.getBufferSizeFor(aCount + 1)); } } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestComplexCopier.java b/java/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestComplexCopier.java index 29f25170332..f85ca9af1e9 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestComplexCopier.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestComplexCopier.java @@ -21,7 +21,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.math.BigDecimal; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.DecimalVector; @@ -62,7 +61,7 @@ public void terminate() throws Exception { @Test public void testCopyFixedSizeListVector() { try (FixedSizeListVector from = FixedSizeListVector.empty("v", 3, allocator); - FixedSizeListVector to = FixedSizeListVector.empty("v", 3, allocator)) { + FixedSizeListVector to = FixedSizeListVector.empty("v", 3, allocator)) { from.addOrGetVector(FieldType.nullable(Types.MinorType.INT.getType())); to.addOrGetVector(FieldType.nullable(Types.MinorType.INT.getType())); @@ -90,14 +89,13 @@ public void testCopyFixedSizeListVector() { // validate equals assertTrue(VectorEqualsVisitor.vectorEquals(from, to)); - } } @Test public void testInvalidCopyFixedSizeListVector() { try (FixedSizeListVector from = FixedSizeListVector.empty("v", 3, allocator); - FixedSizeListVector to = FixedSizeListVector.empty("v", 2, allocator)) { + FixedSizeListVector to = FixedSizeListVector.empty("v", 2, allocator)) { from.addOrGetVector(FieldType.nullable(Types.MinorType.INT.getType())); to.addOrGetVector(FieldType.nullable(Types.MinorType.INT.getType())); @@ -117,8 +115,8 @@ public void testInvalidCopyFixedSizeListVector() { // copy values FieldReader in = from.getReader(); FieldWriter out = to.getWriter(); - IllegalStateException e = assertThrows(IllegalStateException.class, - () -> ComplexCopier.copy(in, out)); + IllegalStateException e = + assertThrows(IllegalStateException.class, () -> ComplexCopier.copy(in, out)); assertTrue(e.getMessage().contains("greater than listSize")); } } @@ -126,7 +124,7 @@ public void testInvalidCopyFixedSizeListVector() { @Test public void testCopyMapVector() { try (final MapVector from = MapVector.empty("v", allocator, false); - final MapVector to = MapVector.empty("v", allocator, false)) { + final MapVector to = MapVector.empty("v", allocator, false)) { from.allocateNew(); @@ -165,7 +163,7 @@ public void testCopyMapVector() { @Test public void testCopyListVector() { try (ListVector from = ListVector.empty("v", allocator); - ListVector to = ListVector.empty("v", allocator)) { + ListVector to = ListVector.empty("v", allocator)) { UnionListWriter listWriter = from.getWriter(); listWriter.allocate(); @@ -204,14 +202,13 @@ public void testCopyListVector() { // validate equals assertTrue(VectorEqualsVisitor.vectorEquals(from, to)); - } } @Test public void testCopyListVectorToANonEmptyList() { try (ListVector from = ListVector.empty("v", allocator); - ListVector to = ListVector.empty("v", allocator)) { + ListVector to = ListVector.empty("v", allocator)) { UnionListWriter listWriter = from.getWriter(); listWriter.allocate(); @@ -262,7 +259,7 @@ public void testCopyListVectorToANonEmptyList() { @Test public void testCopyListVectorWithNulls() { try (ListVector from = ListVector.empty("v", allocator); - ListVector to = ListVector.empty("v", allocator)) { + ListVector to = ListVector.empty("v", allocator)) { UnionListWriter listWriter = from.getWriter(); listWriter.allocate(); @@ -303,7 +300,7 @@ public void testCopyListVectorWithNulls() { @Test public void testCopyListOfListVectorWithNulls() { try (ListVector from = ListVector.empty("v", allocator); - ListVector to = ListVector.empty("v", allocator);) { + ListVector to = ListVector.empty("v", allocator); ) { UnionListWriter listWriter = from.getWriter(); listWriter.allocate(); @@ -358,7 +355,7 @@ public void testCopyListOfListVectorWithNulls() { @Test public void testCopyListOStructVectorWithNulls() { try (ListVector from = ListVector.empty("v", allocator); - ListVector to = ListVector.empty("v", allocator);) { + ListVector to = ListVector.empty("v", allocator); ) { UnionListWriter listWriter = from.getWriter(); listWriter.allocate(); @@ -408,7 +405,7 @@ public void testCopyListOStructVectorWithNulls() { @Test public void testCopyListOfListOfStructVectorWithNulls() { try (ListVector from = ListVector.empty("v", allocator); - ListVector to = ListVector.empty("v", allocator);) { + ListVector to = ListVector.empty("v", allocator); ) { UnionListWriter listWriter = from.getWriter(); listWriter.allocate(); @@ -485,7 +482,7 @@ public void testCopyListOfListOfStructVectorWithNulls() { @Test public void testMapWithListValue() throws Exception { try (MapVector from = MapVector.empty("map", allocator, false); - MapVector to = MapVector.empty("map", allocator, false)) { + MapVector to = MapVector.empty("map", allocator, false)) { UnionMapWriter mapWriter = from.getWriter(); BaseWriter.ListWriter valueWriter; @@ -573,7 +570,7 @@ public void testMapWithListValue() throws Exception { @Test public void testCopyFixedSizedListOfDecimalsVector() { try (FixedSizeListVector from = FixedSizeListVector.empty("v", 4, allocator); - FixedSizeListVector to = FixedSizeListVector.empty("v", 4, allocator)) { + FixedSizeListVector to = FixedSizeListVector.empty("v", 4, allocator)) { from.addOrGetVector(FieldType.nullable(new ArrowType.Decimal(3, 0, 128))); to.addOrGetVector(FieldType.nullable(new ArrowType.Decimal(3, 0, 128))); @@ -587,17 +584,21 @@ public void testCopyFixedSizedListOfDecimalsVector() { writer.startList(); writer.decimal().writeDecimal(BigDecimal.valueOf(i)); - DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(i * 2), holder.buffer, 0, DecimalVector.TYPE_WIDTH); + DecimalUtility.writeBigDecimalToArrowBuf( + new BigDecimal(i * 2), holder.buffer, 0, DecimalVector.TYPE_WIDTH); holder.start = 0; holder.scale = 0; holder.precision = 3; writer.decimal().write(holder); - DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(i * 3), holder.buffer, 0, DecimalVector.TYPE_WIDTH); + DecimalUtility.writeBigDecimalToArrowBuf( + new BigDecimal(i * 3), holder.buffer, 0, DecimalVector.TYPE_WIDTH); writer.decimal().writeDecimal(0, holder.buffer, arrowType); - writer.decimal().writeBigEndianBytesToDecimal(BigDecimal.valueOf(i * 4).unscaledValue().toByteArray(), - arrowType); + writer + .decimal() + .writeBigEndianBytesToDecimal( + BigDecimal.valueOf(i * 4).unscaledValue().toByteArray(), arrowType); writer.endList(); } @@ -622,7 +623,7 @@ public void testCopyFixedSizedListOfDecimalsVector() { @Test public void testCopyUnionListWithDecimal() { try (ListVector from = ListVector.empty("v", allocator); - ListVector to = ListVector.empty("v", allocator)) { + ListVector to = ListVector.empty("v", allocator)) { UnionListWriter listWriter = from.getWriter(); listWriter.allocate(); @@ -633,8 +634,11 @@ public void testCopyUnionListWithDecimal() { listWriter.decimal().writeDecimal(BigDecimal.valueOf(i * 2)); listWriter.integer().writeInt(i); - listWriter.decimal().writeBigEndianBytesToDecimal(BigDecimal.valueOf(i * 3).unscaledValue().toByteArray(), - new ArrowType.Decimal(3, 0, 128)); + listWriter + .decimal() + .writeBigEndianBytesToDecimal( + BigDecimal.valueOf(i * 3).unscaledValue().toByteArray(), + new ArrowType.Decimal(3, 0, 128)); listWriter.endList(); } @@ -653,14 +657,13 @@ public void testCopyUnionListWithDecimal() { // validate equals assertTrue(VectorEqualsVisitor.vectorEquals(from, to)); - } } @Test public void testCopyStructVector() { try (final StructVector from = StructVector.empty("v", allocator); - final StructVector to = StructVector.empty("v", allocator)) { + final StructVector to = StructVector.empty("v", allocator)) { from.allocateNewSafe(); @@ -674,8 +677,11 @@ public void testCopyStructVector() { innerStructWriter.start(); innerStructWriter.integer("innerint").writeInt(i * 3); innerStructWriter.decimal("innerdec", 0, 38).writeDecimal(BigDecimal.valueOf(i * 4)); - innerStructWriter.decimal("innerdec", 0, 38).writeBigEndianBytesToDecimal(BigDecimal.valueOf(i * 4) - .unscaledValue().toByteArray(), new ArrowType.Decimal(3, 0, 128)); + innerStructWriter + .decimal("innerdec", 0, 38) + .writeBigEndianBytesToDecimal( + BigDecimal.valueOf(i * 4).unscaledValue().toByteArray(), + new ArrowType.Decimal(3, 0, 128)); innerStructWriter.end(); structWriter.end(); } @@ -700,7 +706,7 @@ public void testCopyStructVector() { @Test public void testCopyDecimalVectorWrongScale() { try (FixedSizeListVector from = FixedSizeListVector.empty("v", 3, allocator); - FixedSizeListVector to = FixedSizeListVector.empty("v", 3, allocator)) { + FixedSizeListVector to = FixedSizeListVector.empty("v", 3, allocator)) { from.addOrGetVector(FieldType.nullable(new ArrowType.Decimal(3, 2, 128))); to.addOrGetVector(FieldType.nullable(new ArrowType.Decimal(3, 1, 128))); @@ -718,16 +724,17 @@ public void testCopyDecimalVectorWrongScale() { // copy values FieldReader in = from.getReader(); FieldWriter out = to.getWriter(); - UnsupportedOperationException e = assertThrows(UnsupportedOperationException.class, - () -> ComplexCopier.copy(in, out)); - assertTrue(e.getMessage().contains("BigDecimal scale must equal that in the Arrow vector: 2 != 1")); + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> ComplexCopier.copy(in, out)); + assertTrue( + e.getMessage().contains("BigDecimal scale must equal that in the Arrow vector: 2 != 1")); } } @Test public void testCopyStructVectorWithNulls() { try (StructVector from = StructVector.empty("v", allocator); - StructVector to = StructVector.empty("v", allocator)) { + StructVector to = StructVector.empty("v", allocator)) { NullableStructWriter writer = from.getWriter(); @@ -764,7 +771,7 @@ public void testCopyStructVectorWithNulls() { @Test public void testCopyStructOfMap() { try (final StructVector from = StructVector.empty("v", allocator); - final StructVector to = StructVector.empty("v", allocator);) { + final StructVector to = StructVector.empty("v", allocator); ) { from.allocateNew(); @@ -802,7 +809,7 @@ public void testCopyStructOfMap() { @Test public void testCopyMapVectorWithMapValue() { try (final MapVector from = MapVector.empty("v", allocator, false); - final MapVector to = MapVector.empty("v", allocator, false)) { + final MapVector to = MapVector.empty("v", allocator, false)) { from.allocateNew(); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestPromotableWriter.java b/java/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestPromotableWriter.java index b7fc681c161..d46465f241a 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestPromotableWriter.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/complex/impl/TestPromotableWriter.java @@ -26,7 +26,6 @@ import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; import java.util.Objects; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.DirtyRootAllocator; @@ -72,9 +71,10 @@ public void terminate() throws Exception { @Test public void testPromoteToUnion() throws Exception { - try (final NonNullableStructVector container = NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); - final StructVector v = container.addOrGetStruct("test"); - final PromotableWriter writer = new PromotableWriter(v, container)) { + try (final NonNullableStructVector container = + NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); + final StructVector v = container.addOrGetStruct("test"); + final PromotableWriter writer = new PromotableWriter(v, container)) { container.allocateNew(); @@ -155,8 +155,11 @@ public void testPromoteToUnion() throws Exception { assertEquals(444413L, ((java.time.Duration) uv.getObject(7)).getSeconds()); assertFalse("8 shouldn't be null", uv.isNull(8)); - assertEquals(18978, - ByteBuffer.wrap(uv.getFixedSizeBinaryVector().get(8)).order(ByteOrder.nativeOrder()).getInt()); + assertEquals( + 18978, + ByteBuffer.wrap(uv.getFixedSizeBinaryVector().get(8)) + .order(ByteOrder.nativeOrder()) + .getInt()); container.clear(); container.allocateNew(); @@ -172,10 +175,14 @@ public void testPromoteToUnion() throws Exception { Field childField1 = container.getField().getChildren().get(0).getChildren().get(0); Field childField2 = container.getField().getChildren().get(0).getChildren().get(1); - assertEquals("Child field should be union type: " + - childField1.getName(), ArrowTypeID.Union, childField1.getType().getTypeID()); - assertEquals("Child field should be decimal type: " + - childField2.getName(), ArrowTypeID.Decimal, childField2.getType().getTypeID()); + assertEquals( + "Child field should be union type: " + childField1.getName(), + ArrowTypeID.Union, + childField1.getType().getTypeID()); + assertEquals( + "Child field should be decimal type: " + childField2.getName(), + ArrowTypeID.Decimal, + childField2.getType().getTypeID()); buf.close(); } @@ -184,9 +191,10 @@ public void testPromoteToUnion() throws Exception { @Test public void testNoPromoteFloat4ToUnionWithNull() throws Exception { - try (final NonNullableStructVector container = NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); - final StructVector v = container.addOrGetStruct("test"); - final PromotableWriter writer = new PromotableWriter(v, container)) { + try (final NonNullableStructVector container = + NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); + final StructVector v = container.addOrGetStruct("test"); + final PromotableWriter writer = new PromotableWriter(v, container)) { container.allocateNew(); @@ -195,15 +203,25 @@ public void testNoPromoteFloat4ToUnionWithNull() throws Exception { writer.list("list").endList(); writer.end(); - FieldType childTypeOfListInContainer = container.getField().getChildren().get(0).getChildren().get(0) - .getChildren().get(0).getFieldType(); + FieldType childTypeOfListInContainer = + container + .getField() + .getChildren() + .get(0) + .getChildren() + .get(0) + .getChildren() + .get(0) + .getFieldType(); // create a listvector with same type as list in container to, say, hold a copy // this will be a nullvector ListVector lv = ListVector.empty("name", allocator); lv.addOrGetVector(childTypeOfListInContainer); assertEquals(childTypeOfListInContainer.getType(), Types.MinorType.NULL.getType()); - assertEquals(lv.getChildrenFromFields().get(0).getMinorType().getType(), Types.MinorType.NULL.getType()); + assertEquals( + lv.getChildrenFromFields().get(0).getMinorType().getType(), + Types.MinorType.NULL.getType()); writer.start(); writer.list("list").startList(); @@ -213,14 +231,24 @@ public void testNoPromoteFloat4ToUnionWithNull() throws Exception { container.setValueCount(2); - childTypeOfListInContainer = container.getField().getChildren().get(0).getChildren().get(0) - .getChildren().get(0).getFieldType(); + childTypeOfListInContainer = + container + .getField() + .getChildren() + .get(0) + .getChildren() + .get(0) + .getChildren() + .get(0) + .getFieldType(); // repeat but now the type in container has been changed from null to float // we expect same behaviour from listvector lv.addOrGetVector(childTypeOfListInContainer); assertEquals(childTypeOfListInContainer.getType(), Types.MinorType.FLOAT4.getType()); - assertEquals(lv.getChildrenFromFields().get(0).getMinorType().getType(), Types.MinorType.FLOAT4.getType()); + assertEquals( + lv.getChildrenFromFields().get(0).getMinorType().getType(), + Types.MinorType.FLOAT4.getType()); lv.close(); } @@ -229,9 +257,10 @@ public void testNoPromoteFloat4ToUnionWithNull() throws Exception { @Test public void testNoPromoteTimeStampMilliTZToUnionWithNull() throws Exception { - try (final NonNullableStructVector container = NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); - final StructVector v = container.addOrGetStruct("test"); - final PromotableWriter writer = new PromotableWriter(v, container)) { + try (final NonNullableStructVector container = + NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); + final StructVector v = container.addOrGetStruct("test"); + final PromotableWriter writer = new PromotableWriter(v, container)) { container.allocateNew(); @@ -240,15 +269,25 @@ public void testNoPromoteTimeStampMilliTZToUnionWithNull() throws Exception { writer.list("list").endList(); writer.end(); - FieldType childTypeOfListInContainer = container.getField().getChildren().get(0).getChildren().get(0) - .getChildren().get(0).getFieldType(); + FieldType childTypeOfListInContainer = + container + .getField() + .getChildren() + .get(0) + .getChildren() + .get(0) + .getChildren() + .get(0) + .getFieldType(); // create a listvector with same type as list in container to, say, hold a copy // this will be a nullvector ListVector lv = ListVector.empty("name", allocator); lv.addOrGetVector(childTypeOfListInContainer); assertEquals(childTypeOfListInContainer.getType(), Types.MinorType.NULL.getType()); - assertEquals(lv.getChildrenFromFields().get(0).getMinorType().getType(), Types.MinorType.NULL.getType()); + assertEquals( + lv.getChildrenFromFields().get(0).getMinorType().getType(), + Types.MinorType.NULL.getType()); writer.start(); writer.list("list").startList(); @@ -259,24 +298,38 @@ public void testNoPromoteTimeStampMilliTZToUnionWithNull() throws Exception { // Test that we get an exception when the timezone doesn't match holder.timezone = "SomeTimeZone"; - IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, - () -> writer.list("list").timeStampMilliTZ().write(holder)); - assertEquals("holder.timezone: SomeTimeZone not equal to vector timezone: FakeTimeZone", ex.getMessage()); + IllegalArgumentException ex = + assertThrows( + IllegalArgumentException.class, + () -> writer.list("list").timeStampMilliTZ().write(holder)); + assertEquals( + "holder.timezone: SomeTimeZone not equal to vector timezone: FakeTimeZone", + ex.getMessage()); writer.list("list").endList(); writer.end(); container.setValueCount(2); - childTypeOfListInContainer = container.getField().getChildren().get(0).getChildren().get(0) - .getChildren().get(0).getFieldType(); + childTypeOfListInContainer = + container + .getField() + .getChildren() + .get(0) + .getChildren() + .get(0) + .getChildren() + .get(0) + .getFieldType(); // repeat but now the type in container has been changed from null to float // we expect same behaviour from listvector lv.addOrGetVector(childTypeOfListInContainer); - assertEquals(childTypeOfListInContainer.getType(), + assertEquals( + childTypeOfListInContainer.getType(), new ArrowType.Timestamp(TimeUnit.MILLISECOND, "FakeTimeZone")); - assertEquals(lv.getChildrenFromFields().get(0).getField().getType(), + assertEquals( + lv.getChildrenFromFields().get(0).getField().getType(), new ArrowType.Timestamp(TimeUnit.MILLISECOND, "FakeTimeZone")); lv.close(); @@ -286,9 +339,10 @@ public void testNoPromoteTimeStampMilliTZToUnionWithNull() throws Exception { @Test public void testNoPromoteDurationToUnionWithNull() throws Exception { - try (final NonNullableStructVector container = NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); - final StructVector v = container.addOrGetStruct("test"); - final PromotableWriter writer = new PromotableWriter(v, container)) { + try (final NonNullableStructVector container = + NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); + final StructVector v = container.addOrGetStruct("test"); + final PromotableWriter writer = new PromotableWriter(v, container)) { container.allocateNew(); @@ -297,15 +351,25 @@ public void testNoPromoteDurationToUnionWithNull() throws Exception { writer.list("list").endList(); writer.end(); - FieldType childTypeOfListInContainer = container.getField().getChildren().get(0).getChildren().get(0) - .getChildren().get(0).getFieldType(); + FieldType childTypeOfListInContainer = + container + .getField() + .getChildren() + .get(0) + .getChildren() + .get(0) + .getChildren() + .get(0) + .getFieldType(); // create a listvector with same type as list in container to, say, hold a copy // this will be a nullvector ListVector lv = ListVector.empty("name", allocator); lv.addOrGetVector(childTypeOfListInContainer); assertEquals(childTypeOfListInContainer.getType(), Types.MinorType.NULL.getType()); - assertEquals(lv.getChildrenFromFields().get(0).getMinorType().getType(), Types.MinorType.NULL.getType()); + assertEquals( + lv.getChildrenFromFields().get(0).getMinorType().getType(), + Types.MinorType.NULL.getType()); writer.start(); writer.list("list").startList(); @@ -316,24 +380,35 @@ public void testNoPromoteDurationToUnionWithNull() throws Exception { // Test that we get an exception when the unit doesn't match holder.unit = TimeUnit.MICROSECOND; - IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, - () -> writer.list("list").duration().write(holder)); - assertEquals("holder.unit: MICROSECOND not equal to vector unit: NANOSECOND", ex.getMessage()); + IllegalArgumentException ex = + assertThrows( + IllegalArgumentException.class, () -> writer.list("list").duration().write(holder)); + assertEquals( + "holder.unit: MICROSECOND not equal to vector unit: NANOSECOND", ex.getMessage()); writer.list("list").endList(); writer.end(); container.setValueCount(2); - childTypeOfListInContainer = container.getField().getChildren().get(0).getChildren().get(0) - .getChildren().get(0).getFieldType(); + childTypeOfListInContainer = + container + .getField() + .getChildren() + .get(0) + .getChildren() + .get(0) + .getChildren() + .get(0) + .getFieldType(); // repeat but now the type in container has been changed from null to float // we expect same behaviour from listvector lv.addOrGetVector(childTypeOfListInContainer); - assertEquals(childTypeOfListInContainer.getType(), - new ArrowType.Duration(TimeUnit.NANOSECOND)); - assertEquals(lv.getChildrenFromFields().get(0).getField().getType(), + assertEquals( + childTypeOfListInContainer.getType(), new ArrowType.Duration(TimeUnit.NANOSECOND)); + assertEquals( + lv.getChildrenFromFields().get(0).getField().getType(), new ArrowType.Duration(TimeUnit.NANOSECOND)); lv.close(); @@ -343,9 +418,10 @@ public void testNoPromoteDurationToUnionWithNull() throws Exception { @Test public void testNoPromoteFixedSizeBinaryToUnionWithNull() throws Exception { - try (final NonNullableStructVector container = NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); - final StructVector v = container.addOrGetStruct("test"); - final PromotableWriter writer = new PromotableWriter(v, container)) { + try (final NonNullableStructVector container = + NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); + final StructVector v = container.addOrGetStruct("test"); + final PromotableWriter writer = new PromotableWriter(v, container)) { container.allocateNew(); @@ -354,15 +430,25 @@ public void testNoPromoteFixedSizeBinaryToUnionWithNull() throws Exception { writer.list("list").endList(); writer.end(); - FieldType childTypeOfListInContainer = container.getField().getChildren().get(0).getChildren().get(0) - .getChildren().get(0).getFieldType(); + FieldType childTypeOfListInContainer = + container + .getField() + .getChildren() + .get(0) + .getChildren() + .get(0) + .getChildren() + .get(0) + .getFieldType(); // create a listvector with same type as list in container to, say, hold a copy // this will be a nullvector ListVector lv = ListVector.empty("name", allocator); lv.addOrGetVector(childTypeOfListInContainer); assertEquals(childTypeOfListInContainer.getType(), Types.MinorType.NULL.getType()); - assertEquals(lv.getChildrenFromFields().get(0).getMinorType().getType(), Types.MinorType.NULL.getType()); + assertEquals( + lv.getChildrenFromFields().get(0).getMinorType().getType(), + Types.MinorType.NULL.getType()); writer.start(); writer.list("list").startList(); @@ -375,8 +461,10 @@ public void testNoPromoteFixedSizeBinaryToUnionWithNull() throws Exception { // Test that we get an exception when the unit doesn't match holder.byteWidth = 7; - IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, - () -> writer.list("list").fixedSizeBinary().write(holder)); + IllegalArgumentException ex = + assertThrows( + IllegalArgumentException.class, + () -> writer.list("list").fixedSizeBinary().write(holder)); assertEquals("holder.byteWidth: 7 not equal to vector byteWidth: 4", ex.getMessage()); writer.list("list").endList(); @@ -384,16 +472,23 @@ public void testNoPromoteFixedSizeBinaryToUnionWithNull() throws Exception { container.setValueCount(2); - childTypeOfListInContainer = container.getField().getChildren().get(0).getChildren().get(0) - .getChildren().get(0).getFieldType(); + childTypeOfListInContainer = + container + .getField() + .getChildren() + .get(0) + .getChildren() + .get(0) + .getChildren() + .get(0) + .getFieldType(); // repeat but now the type in container has been changed from null to float // we expect same behaviour from listvector lv.addOrGetVector(childTypeOfListInContainer); - assertEquals(childTypeOfListInContainer.getType(), - new ArrowType.FixedSizeBinary(4)); - assertEquals(lv.getChildrenFromFields().get(0).getField().getType(), - new ArrowType.FixedSizeBinary(4)); + assertEquals(childTypeOfListInContainer.getType(), new ArrowType.FixedSizeBinary(4)); + assertEquals( + lv.getChildrenFromFields().get(0).getField().getType(), new ArrowType.FixedSizeBinary(4)); lv.close(); buf.close(); @@ -402,9 +497,10 @@ public void testNoPromoteFixedSizeBinaryToUnionWithNull() throws Exception { @Test public void testPromoteLargeVarCharHelpersOnStruct() throws Exception { - try (final NonNullableStructVector container = NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); - final StructVector v = container.addOrGetStruct("test"); - final PromotableWriter writer = new PromotableWriter(v, container)) { + try (final NonNullableStructVector container = + NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); + final StructVector v = container.addOrGetStruct("test"); + final PromotableWriter writer = new PromotableWriter(v, container)) { container.allocateNew(); writer.start(); @@ -422,9 +518,10 @@ public void testPromoteLargeVarCharHelpersOnStruct() throws Exception { @Test public void testPromoteVarCharHelpersOnStruct() throws Exception { - try (final NonNullableStructVector container = NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); - final StructVector v = container.addOrGetStruct("test"); - final PromotableWriter writer = new PromotableWriter(v, container)) { + try (final NonNullableStructVector container = + NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); + final StructVector v = container.addOrGetStruct("test"); + final PromotableWriter writer = new PromotableWriter(v, container)) { container.allocateNew(); writer.start(); @@ -442,9 +539,10 @@ public void testPromoteVarCharHelpersOnStruct() throws Exception { @Test public void testPromoteVarCharHelpersDirect() throws Exception { - try (final NonNullableStructVector container = NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); - final StructVector v = container.addOrGetStruct("test"); - final PromotableWriter writer = new PromotableWriter(v, container)) { + try (final NonNullableStructVector container = + NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); + final StructVector v = container.addOrGetStruct("test"); + final PromotableWriter writer = new PromotableWriter(v, container)) { container.allocateNew(); writer.start(); @@ -464,9 +562,10 @@ public void testPromoteVarCharHelpersDirect() throws Exception { @Test public void testPromoteLargeVarCharHelpersDirect() throws Exception { - try (final NonNullableStructVector container = NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); - final StructVector v = container.addOrGetStruct("test"); - final PromotableWriter writer = new PromotableWriter(v, container)) { + try (final NonNullableStructVector container = + NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); + final StructVector v = container.addOrGetStruct("test"); + final PromotableWriter writer = new PromotableWriter(v, container)) { container.allocateNew(); writer.start(); @@ -486,22 +585,33 @@ public void testPromoteLargeVarCharHelpersDirect() throws Exception { @Test public void testPromoteVarBinaryHelpersOnStruct() throws Exception { - try (final NonNullableStructVector container = NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); - final StructVector v = container.addOrGetStruct("test"); - final PromotableWriter writer = new PromotableWriter(v, container)) { + try (final NonNullableStructVector container = + NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); + final StructVector v = container.addOrGetStruct("test"); + final PromotableWriter writer = new PromotableWriter(v, container)) { container.allocateNew(); writer.start(); writer.setPosition(0); writer.varBinary("c").writeVarBinary("row1".getBytes(StandardCharsets.UTF_8)); writer.setPosition(1); - writer.varBinary("c").writeVarBinary("row2".getBytes(StandardCharsets.UTF_8), 0, - "row2".getBytes(StandardCharsets.UTF_8).length); + writer + .varBinary("c") + .writeVarBinary( + "row2".getBytes(StandardCharsets.UTF_8), + 0, + "row2".getBytes(StandardCharsets.UTF_8).length); writer.setPosition(2); - writer.varBinary("c").writeVarBinary(ByteBuffer.wrap("row3".getBytes(StandardCharsets.UTF_8))); + writer + .varBinary("c") + .writeVarBinary(ByteBuffer.wrap("row3".getBytes(StandardCharsets.UTF_8))); writer.setPosition(3); - writer.varBinary("c").writeVarBinary(ByteBuffer.wrap("row4".getBytes(StandardCharsets.UTF_8)), 0, - "row4".getBytes(StandardCharsets.UTF_8).length); + writer + .varBinary("c") + .writeVarBinary( + ByteBuffer.wrap("row4".getBytes(StandardCharsets.UTF_8)), + 0, + "row4".getBytes(StandardCharsets.UTF_8).length); writer.end(); final VarBinaryVector uv = v.getChild("c", VarBinaryVector.class); @@ -514,21 +624,26 @@ public void testPromoteVarBinaryHelpersOnStruct() throws Exception { @Test public void testPromoteVarBinaryHelpersDirect() throws Exception { - try (final NonNullableStructVector container = NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); - final StructVector v = container.addOrGetStruct("test"); - final PromotableWriter writer = new PromotableWriter(v, container)) { + try (final NonNullableStructVector container = + NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); + final StructVector v = container.addOrGetStruct("test"); + final PromotableWriter writer = new PromotableWriter(v, container)) { container.allocateNew(); writer.start(); writer.setPosition(0); writer.writeVarBinary("row1".getBytes(StandardCharsets.UTF_8)); writer.setPosition(1); - writer.writeVarBinary("row2".getBytes(StandardCharsets.UTF_8), 0, + writer.writeVarBinary( + "row2".getBytes(StandardCharsets.UTF_8), + 0, "row2".getBytes(StandardCharsets.UTF_8).length); writer.setPosition(2); writer.writeVarBinary(ByteBuffer.wrap("row3".getBytes(StandardCharsets.UTF_8))); writer.setPosition(3); - writer.writeVarBinary(ByteBuffer.wrap("row4".getBytes(StandardCharsets.UTF_8)), 0, + writer.writeVarBinary( + ByteBuffer.wrap("row4".getBytes(StandardCharsets.UTF_8)), + 0, "row4".getBytes(StandardCharsets.UTF_8).length); writer.end(); @@ -544,22 +659,33 @@ public void testPromoteVarBinaryHelpersDirect() throws Exception { @Test public void testPromoteLargeVarBinaryHelpersOnStruct() throws Exception { - try (final NonNullableStructVector container = NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); - final StructVector v = container.addOrGetStruct("test"); - final PromotableWriter writer = new PromotableWriter(v, container)) { + try (final NonNullableStructVector container = + NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); + final StructVector v = container.addOrGetStruct("test"); + final PromotableWriter writer = new PromotableWriter(v, container)) { container.allocateNew(); writer.start(); writer.setPosition(0); writer.largeVarBinary("c").writeLargeVarBinary("row1".getBytes(StandardCharsets.UTF_8)); writer.setPosition(1); - writer.largeVarBinary("c").writeLargeVarBinary("row2".getBytes(StandardCharsets.UTF_8), 0, - "row2".getBytes(StandardCharsets.UTF_8).length); + writer + .largeVarBinary("c") + .writeLargeVarBinary( + "row2".getBytes(StandardCharsets.UTF_8), + 0, + "row2".getBytes(StandardCharsets.UTF_8).length); writer.setPosition(2); - writer.largeVarBinary("c").writeLargeVarBinary(ByteBuffer.wrap("row3".getBytes(StandardCharsets.UTF_8))); + writer + .largeVarBinary("c") + .writeLargeVarBinary(ByteBuffer.wrap("row3".getBytes(StandardCharsets.UTF_8))); writer.setPosition(3); - writer.largeVarBinary("c").writeLargeVarBinary(ByteBuffer.wrap("row4".getBytes(StandardCharsets.UTF_8)), 0, - "row4".getBytes(StandardCharsets.UTF_8).length); + writer + .largeVarBinary("c") + .writeLargeVarBinary( + ByteBuffer.wrap("row4".getBytes(StandardCharsets.UTF_8)), + 0, + "row4".getBytes(StandardCharsets.UTF_8).length); writer.end(); final LargeVarBinaryVector uv = v.getChild("c", LargeVarBinaryVector.class); @@ -572,21 +698,26 @@ public void testPromoteLargeVarBinaryHelpersOnStruct() throws Exception { @Test public void testPromoteLargeVarBinaryHelpersDirect() throws Exception { - try (final NonNullableStructVector container = NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); - final StructVector v = container.addOrGetStruct("test"); - final PromotableWriter writer = new PromotableWriter(v, container)) { + try (final NonNullableStructVector container = + NonNullableStructVector.empty(EMPTY_SCHEMA_PATH, allocator); + final StructVector v = container.addOrGetStruct("test"); + final PromotableWriter writer = new PromotableWriter(v, container)) { container.allocateNew(); writer.start(); writer.setPosition(0); writer.writeLargeVarBinary("row1".getBytes(StandardCharsets.UTF_8)); writer.setPosition(1); - writer.writeLargeVarBinary("row2".getBytes(StandardCharsets.UTF_8), 0, + writer.writeLargeVarBinary( + "row2".getBytes(StandardCharsets.UTF_8), + 0, "row2".getBytes(StandardCharsets.UTF_8).length); writer.setPosition(2); writer.writeLargeVarBinary(ByteBuffer.wrap("row3".getBytes(StandardCharsets.UTF_8))); writer.setPosition(3); - writer.writeLargeVarBinary(ByteBuffer.wrap("row4".getBytes(StandardCharsets.UTF_8)), 0, + writer.writeLargeVarBinary( + ByteBuffer.wrap("row4".getBytes(StandardCharsets.UTF_8)), + 0, "row4".getBytes(StandardCharsets.UTF_8).length); writer.end(); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/complex/writer/TestComplexWriter.java b/java/vector/src/test/java/org/apache/arrow/vector/complex/writer/TestComplexWriter.java index 19f0ea9d4e3..9c9729db905 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/complex/writer/TestComplexWriter.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/complex/writer/TestComplexWriter.java @@ -27,7 +27,6 @@ import java.util.HashSet; import java.util.List; import java.util.Set; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -145,7 +144,8 @@ public void transferPairSchemaChange() { private NonNullableStructVector populateStructVector(CallBack callBack) { NonNullableStructVector parent = - new NonNullableStructVector("parent", allocator, new FieldType(false, Struct.INSTANCE, null, null), callBack); + new NonNullableStructVector( + "parent", allocator, new FieldType(false, Struct.INSTANCE, null, null), callBack); ComplexWriter writer = new ComplexWriterImpl("root", parent); StructWriter rootWriter = writer.rootAsStruct(); IntWriter intWriter = rootWriter.integer("int"); @@ -162,7 +162,8 @@ private NonNullableStructVector populateStructVector(CallBack callBack) { @Test public void nullableStruct() { - try (NonNullableStructVector structVector = NonNullableStructVector.empty("parent", allocator)) { + try (NonNullableStructVector structVector = + NonNullableStructVector.empty("parent", allocator)) { ComplexWriter writer = new ComplexWriterImpl("root", structVector); StructWriter rootWriter = writer.rootAsStruct(); for (int i = 0; i < COUNT; i++) { @@ -182,11 +183,13 @@ public void nullableStruct() { } /** - * This test is similar to {@link #nullableStruct()} ()} but we get the inner struct writer once at the beginning. + * This test is similar to {@link #nullableStruct()} ()} but we get the inner struct writer once + * at the beginning. */ @Test public void nullableStruct2() { - try (NonNullableStructVector structVector = NonNullableStructVector.empty("parent", allocator)) { + try (NonNullableStructVector structVector = + NonNullableStructVector.empty("parent", allocator)) { ComplexWriter writer = new ComplexWriterImpl("root", structVector); StructWriter rootWriter = writer.rootAsStruct(); StructWriter structWriter = rootWriter.struct("struct"); @@ -336,13 +339,15 @@ public void listDecimalType() { if (j % 4 == 0) { listWriter.writeDecimal(new BigDecimal(j)); } else if (j % 4 == 1) { - DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(j), holder.buffer, 0, DecimalVector.TYPE_WIDTH); + DecimalUtility.writeBigDecimalToArrowBuf( + new BigDecimal(j), holder.buffer, 0, DecimalVector.TYPE_WIDTH); holder.start = 0; holder.scale = 0; holder.precision = 10; listWriter.write(holder); } else if (j % 4 == 2) { - DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(j), holder.buffer, 0, DecimalVector.TYPE_WIDTH); + DecimalUtility.writeBigDecimalToArrowBuf( + new BigDecimal(j), holder.buffer, 0, DecimalVector.TYPE_WIDTH); listWriter.writeDecimal(0, holder.buffer, arrowType); } else { byte[] value = BigDecimal.valueOf(j).unscaledValue().toByteArray(); @@ -506,7 +511,8 @@ public void listScalarTypeNullable() { listReader.setPosition(i); if (i % 2 == 0) { assertTrue("index is set: " + i, listReader.isSet()); - assertEquals("correct length at: " + i, i % 7, ((List) listReader.readObject()).size()); + assertEquals( + "correct length at: " + i, i % 7, ((List) listReader.readObject()).size()); } else { assertFalse("index is not set: " + i, listReader.isSet()); assertNull("index is not set: " + i, listReader.readObject()); @@ -537,7 +543,8 @@ public void listStructType() { listReader.setPosition(i); for (int j = 0; j < i % 7; j++) { listReader.next(); - Assert.assertEquals("record: " + i, j, listReader.reader().reader("int").readInteger().intValue()); + Assert.assertEquals( + "record: " + i, j, listReader.reader().reader("int").readInteger().intValue()); Assert.assertEquals(j, listReader.reader().reader("bigInt").readLong().longValue()); } } @@ -567,7 +574,8 @@ public void listListType() { } /** - * This test is similar to {@link #listListType()} but we get the inner list writer once at the beginning. + * This test is similar to {@link #listListType()} but we get the inner list writer once at the + * beginning. */ @Test public void listListType2() { @@ -634,7 +642,8 @@ public void unionListListType() { } /** - * This test is similar to {@link #unionListListType()} but we get the inner list writer once at the beginning. + * This test is similar to {@link #unionListListType()} but we get the inner list writer once at + * the beginning. */ @Test public void unionListListType2() { @@ -673,7 +682,8 @@ private void checkUnionList(ListVector listVector) { for (int k = 0; k < i % 13; k++) { innerListReader.next(); if (k % 2 == 0) { - Assert.assertEquals("record: " + i, k, innerListReader.reader().readInteger().intValue()); + Assert.assertEquals( + "record: " + i, k, innerListReader.reader().readInteger().intValue()); } else { Assert.assertEquals("record: " + i, k, innerListReader.reader().readLong().longValue()); } @@ -738,7 +748,8 @@ private void checkListMap(ListVector listVector) { @Test public void simpleUnion() throws Exception { List bufs = new ArrayList(); - UnionVector vector = new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); + UnionVector vector = + new UnionVector("union", allocator, /* field type */ null, /* call-back */ null); UnionWriter unionWriter = new UnionWriter(vector); unionWriter.allocate(); for (int i = 0; i < COUNT; i++) { @@ -845,9 +856,7 @@ public void promotableWriter() { } } - /** - * Even without writing to the writer, the union schema is created correctly. - */ + /** Even without writing to the writer, the union schema is created correctly. */ @Test public void promotableWriterSchema() { try (NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator)) { @@ -915,14 +924,16 @@ public void structWriterMixedCaseFieldNames() { Assert.assertTrue(fieldNamesCaseSensitive.contains("list_field::$data$::Bit_Field")); // test case-insensitive StructWriter - ComplexWriter writerCaseInsensitive = new ComplexWriterImpl("rootCaseInsensitive", parent, false, false); + ComplexWriter writerCaseInsensitive = + new ComplexWriterImpl("rootCaseInsensitive", parent, false, false); StructWriter rootWriterCaseInsensitive = writerCaseInsensitive.rootAsStruct(); rootWriterCaseInsensitive.bigInt("int_field"); rootWriterCaseInsensitive.bigInt("Int_Field"); rootWriterCaseInsensitive.float4("float_field"); rootWriterCaseInsensitive.float4("Float_Field"); - StructWriter structFieldWriterCaseInsensitive = rootWriterCaseInsensitive.struct("struct_field"); + StructWriter structFieldWriterCaseInsensitive = + rootWriterCaseInsensitive.struct("struct_field"); structFieldWriterCaseInsensitive.varChar("char_field"); structFieldWriterCaseInsensitive.varChar("Char_Field"); ListWriter listFieldWriterCaseInsensitive = rootWriterCaseInsensitive.list("list_field"); @@ -993,9 +1004,10 @@ public void timeStampSecWriter() throws Exception { public void timeStampMilliWriters() throws Exception { // test values final long expectedMillis = 981173106123L; - final LocalDateTime expectedMilliDateTime = LocalDateTime.of(2001, 2, 3, 4, 5, 6, 123 * 1_000_000); + final LocalDateTime expectedMilliDateTime = + LocalDateTime.of(2001, 2, 3, 4, 5, 6, 123 * 1_000_000); - try (NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator);) { + try (NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator); ) { // write ComplexWriter writer = new ComplexWriterImpl("root", parent); StructWriter rootWriter = writer.rootAsStruct(); @@ -1049,7 +1061,8 @@ private void checkTimestampTZField(Field field, String name, String tz) { public void timeStampMicroWriters() throws Exception { // test values final long expectedMicros = 981173106123456L; - final LocalDateTime expectedMicroDateTime = LocalDateTime.of(2001, 2, 3, 4, 5, 6, 123456 * 1000); + final LocalDateTime expectedMicroDateTime = + LocalDateTime.of(2001, 2, 3, 4, 5, 6, 123456 * 1000); try (NonNullableStructVector parent = NonNullableStructVector.empty("parent", allocator)) { // write @@ -1139,7 +1152,6 @@ public void timeStampNanoWriters() throws Exception { Assert.assertEquals(expectedNanos, h.value); } } - } @Test @@ -1165,7 +1177,8 @@ public void fixedSizeBinaryWriters() throws Exception { StructWriter rootWriter = writer.rootAsStruct(); String fieldName = "fixedSizeBinary"; - FixedSizeBinaryWriter fixedSizeBinaryWriter = rootWriter.fixedSizeBinary(fieldName, byteWidth); + FixedSizeBinaryWriter fixedSizeBinaryWriter = + rootWriter.fixedSizeBinary(fieldName, byteWidth); for (int i = 0; i < numValues; i++) { fixedSizeBinaryWriter.setPosition(i); fixedSizeBinaryWriter.writeFixedSizeBinary(bufs[i]); @@ -1174,7 +1187,8 @@ public void fixedSizeBinaryWriters() throws Exception { // schema List children = parent.getField().getChildren().get(0).getChildren(); Assert.assertEquals(fieldName, children.get(0).getName()); - Assert.assertEquals(ArrowType.FixedSizeBinary.TYPE_TYPE, children.get(0).getType().getTypeID()); + Assert.assertEquals( + ArrowType.FixedSizeBinary.TYPE_TYPE, children.get(0).getType().getTypeID()); // read StructReader rootReader = new SingleStructReaderImpl(parent).reader("root"); @@ -1334,8 +1348,6 @@ public void testSingleStructWriter1() { } } } - - } @Test @@ -1494,7 +1506,8 @@ public void testListOfListOfListWriterWithNulls() { listReader.reader().reader().next(); Assert.assertEquals(i, listReader.reader().reader().reader().readInteger().intValue()); listReader.reader().reader().next(); - Assert.assertEquals(i * 2, listReader.reader().reader().reader().readInteger().intValue()); + Assert.assertEquals( + i * 2, listReader.reader().reader().reader().readInteger().intValue()); } } } @@ -1684,7 +1697,8 @@ public void structWriterVarCharHelpers() { rootWriter.varChar("c").writeVarChar("row2"); rootWriter.end(); - VarCharVector vector = parent.getChild("root", StructVector.class).getChild("c", VarCharVector.class); + VarCharVector vector = + parent.getChild("root", StructVector.class).getChild("c", VarCharVector.class); assertEquals("row1", vector.getObject(0).toString()); assertEquals("row2", vector.getObject(1).toString()); @@ -1703,8 +1717,8 @@ public void structWriterLargeVarCharHelpers() { rootWriter.largeVarChar("c").writeLargeVarChar("row2"); rootWriter.end(); - LargeVarCharVector vector = parent.getChild("root", StructVector.class).getChild("c", - LargeVarCharVector.class); + LargeVarCharVector vector = + parent.getChild("root", StructVector.class).getChild("c", LargeVarCharVector.class); assertEquals("row1", vector.getObject(0).toString()); assertEquals("row2", vector.getObject(1).toString()); @@ -1720,16 +1734,27 @@ public void structWriterVarBinaryHelpers() { rootWriter.setPosition(0); rootWriter.varBinary("c").writeVarBinary("row1".getBytes(StandardCharsets.UTF_8)); rootWriter.setPosition(1); - rootWriter.varBinary("c").writeVarBinary("row2".getBytes(StandardCharsets.UTF_8), 0, - "row2".getBytes(StandardCharsets.UTF_8).length); + rootWriter + .varBinary("c") + .writeVarBinary( + "row2".getBytes(StandardCharsets.UTF_8), + 0, + "row2".getBytes(StandardCharsets.UTF_8).length); rootWriter.setPosition(2); - rootWriter.varBinary("c").writeVarBinary(ByteBuffer.wrap("row3".getBytes(StandardCharsets.UTF_8))); + rootWriter + .varBinary("c") + .writeVarBinary(ByteBuffer.wrap("row3".getBytes(StandardCharsets.UTF_8))); rootWriter.setPosition(3); - rootWriter.varBinary("c").writeVarBinary(ByteBuffer.wrap( - "row4".getBytes(StandardCharsets.UTF_8)), 0, "row4".getBytes(StandardCharsets.UTF_8).length); + rootWriter + .varBinary("c") + .writeVarBinary( + ByteBuffer.wrap("row4".getBytes(StandardCharsets.UTF_8)), + 0, + "row4".getBytes(StandardCharsets.UTF_8).length); rootWriter.end(); - VarBinaryVector uv = parent.getChild("root", StructVector.class).getChild("c", VarBinaryVector.class); + VarBinaryVector uv = + parent.getChild("root", StructVector.class).getChild("c", VarBinaryVector.class); assertEquals("row1", new String(uv.get(0), StandardCharsets.UTF_8)); assertEquals("row2", new String(uv.get(1), StandardCharsets.UTF_8)); @@ -1747,17 +1772,27 @@ public void structWriterLargeVarBinaryHelpers() { rootWriter.setPosition(0); rootWriter.largeVarBinary("c").writeLargeVarBinary("row1".getBytes(StandardCharsets.UTF_8)); rootWriter.setPosition(1); - rootWriter.largeVarBinary("c").writeLargeVarBinary("row2".getBytes(StandardCharsets.UTF_8), 0, - "row2".getBytes(StandardCharsets.UTF_8).length); + rootWriter + .largeVarBinary("c") + .writeLargeVarBinary( + "row2".getBytes(StandardCharsets.UTF_8), + 0, + "row2".getBytes(StandardCharsets.UTF_8).length); rootWriter.setPosition(2); - rootWriter.largeVarBinary("c").writeLargeVarBinary(ByteBuffer.wrap("row3".getBytes(StandardCharsets.UTF_8))); + rootWriter + .largeVarBinary("c") + .writeLargeVarBinary(ByteBuffer.wrap("row3".getBytes(StandardCharsets.UTF_8))); rootWriter.setPosition(3); - rootWriter.largeVarBinary("c").writeLargeVarBinary(ByteBuffer.wrap( - "row4".getBytes(StandardCharsets.UTF_8)), 0, "row4".getBytes(StandardCharsets.UTF_8).length); + rootWriter + .largeVarBinary("c") + .writeLargeVarBinary( + ByteBuffer.wrap("row4".getBytes(StandardCharsets.UTF_8)), + 0, + "row4".getBytes(StandardCharsets.UTF_8).length); rootWriter.end(); - LargeVarBinaryVector uv = parent.getChild("root", StructVector.class).getChild("c", - LargeVarBinaryVector.class); + LargeVarBinaryVector uv = + parent.getChild("root", StructVector.class).getChild("c", LargeVarBinaryVector.class); assertEquals("row1", new String(uv.get(0), StandardCharsets.UTF_8)); assertEquals("row2", new String(uv.get(1), StandardCharsets.UTF_8)); @@ -1803,17 +1838,25 @@ public void listVarBinaryHelpers() { UnionListWriter listWriter = new UnionListWriter(listVector); listWriter.startList(); listWriter.writeVarBinary("row1".getBytes(StandardCharsets.UTF_8)); - listWriter.writeVarBinary("row2".getBytes(StandardCharsets.UTF_8), 0, + listWriter.writeVarBinary( + "row2".getBytes(StandardCharsets.UTF_8), + 0, "row2".getBytes(StandardCharsets.UTF_8).length); listWriter.writeVarBinary(ByteBuffer.wrap("row3".getBytes(StandardCharsets.UTF_8))); - listWriter.writeVarBinary(ByteBuffer.wrap( - "row4".getBytes(StandardCharsets.UTF_8)), 0, "row4".getBytes(StandardCharsets.UTF_8).length); + listWriter.writeVarBinary( + ByteBuffer.wrap("row4".getBytes(StandardCharsets.UTF_8)), + 0, + "row4".getBytes(StandardCharsets.UTF_8).length); listWriter.endList(); listWriter.setValueCount(1); - assertEquals("row1", new String((byte[]) listVector.getObject(0).get(0), StandardCharsets.UTF_8)); - assertEquals("row2", new String((byte[]) listVector.getObject(0).get(1), StandardCharsets.UTF_8)); - assertEquals("row3", new String((byte[]) listVector.getObject(0).get(2), StandardCharsets.UTF_8)); - assertEquals("row4", new String((byte[]) listVector.getObject(0).get(3), StandardCharsets.UTF_8)); + assertEquals( + "row1", new String((byte[]) listVector.getObject(0).get(0), StandardCharsets.UTF_8)); + assertEquals( + "row2", new String((byte[]) listVector.getObject(0).get(1), StandardCharsets.UTF_8)); + assertEquals( + "row3", new String((byte[]) listVector.getObject(0).get(2), StandardCharsets.UTF_8)); + assertEquals( + "row4", new String((byte[]) listVector.getObject(0).get(3), StandardCharsets.UTF_8)); } } @@ -1824,23 +1867,32 @@ public void listLargeVarBinaryHelpers() { UnionListWriter listWriter = new UnionListWriter(listVector); listWriter.startList(); listWriter.writeLargeVarBinary("row1".getBytes(StandardCharsets.UTF_8)); - listWriter.writeLargeVarBinary("row2".getBytes(StandardCharsets.UTF_8), 0, + listWriter.writeLargeVarBinary( + "row2".getBytes(StandardCharsets.UTF_8), + 0, "row2".getBytes(StandardCharsets.UTF_8).length); listWriter.writeLargeVarBinary(ByteBuffer.wrap("row3".getBytes(StandardCharsets.UTF_8))); - listWriter.writeLargeVarBinary(ByteBuffer.wrap( - "row4".getBytes(StandardCharsets.UTF_8)), 0, "row4".getBytes(StandardCharsets.UTF_8).length); + listWriter.writeLargeVarBinary( + ByteBuffer.wrap("row4".getBytes(StandardCharsets.UTF_8)), + 0, + "row4".getBytes(StandardCharsets.UTF_8).length); listWriter.endList(); listWriter.setValueCount(1); - assertEquals("row1", new String((byte[]) listVector.getObject(0).get(0), StandardCharsets.UTF_8)); - assertEquals("row2", new String((byte[]) listVector.getObject(0).get(1), StandardCharsets.UTF_8)); - assertEquals("row3", new String((byte[]) listVector.getObject(0).get(2), StandardCharsets.UTF_8)); - assertEquals("row4", new String((byte[]) listVector.getObject(0).get(3), StandardCharsets.UTF_8)); + assertEquals( + "row1", new String((byte[]) listVector.getObject(0).get(0), StandardCharsets.UTF_8)); + assertEquals( + "row2", new String((byte[]) listVector.getObject(0).get(1), StandardCharsets.UTF_8)); + assertEquals( + "row3", new String((byte[]) listVector.getObject(0).get(2), StandardCharsets.UTF_8)); + assertEquals( + "row4", new String((byte[]) listVector.getObject(0).get(3), StandardCharsets.UTF_8)); } } @Test public void unionWithVarCharAndBinaryHelpers() throws Exception { - try (UnionVector vector = new UnionVector("union", allocator, /* field type */ null, /* call-back */ null)) { + try (UnionVector vector = + new UnionVector("union", allocator, /* field type */ null, /* call-back */ null)) { UnionWriter unionWriter = new UnionWriter(vector); unionWriter.allocate(); unionWriter.start(); @@ -1855,37 +1907,51 @@ public void unionWithVarCharAndBinaryHelpers() throws Exception { unionWriter.setPosition(4); unionWriter.writeVarBinary("row5".getBytes(StandardCharsets.UTF_8)); unionWriter.setPosition(5); - unionWriter.writeVarBinary("row6".getBytes(StandardCharsets.UTF_8), 0, + unionWriter.writeVarBinary( + "row6".getBytes(StandardCharsets.UTF_8), + 0, "row6".getBytes(StandardCharsets.UTF_8).length); unionWriter.setPosition(6); unionWriter.writeVarBinary(ByteBuffer.wrap("row7".getBytes(StandardCharsets.UTF_8))); unionWriter.setPosition(7); - unionWriter.writeVarBinary(ByteBuffer.wrap("row8".getBytes(StandardCharsets.UTF_8)), 0, + unionWriter.writeVarBinary( + ByteBuffer.wrap("row8".getBytes(StandardCharsets.UTF_8)), + 0, "row8".getBytes(StandardCharsets.UTF_8).length); unionWriter.setPosition(8); unionWriter.writeLargeVarBinary("row9".getBytes(StandardCharsets.UTF_8)); unionWriter.setPosition(9); - unionWriter.writeLargeVarBinary("row10".getBytes(StandardCharsets.UTF_8), 0, + unionWriter.writeLargeVarBinary( + "row10".getBytes(StandardCharsets.UTF_8), + 0, "row10".getBytes(StandardCharsets.UTF_8).length); unionWriter.setPosition(10); unionWriter.writeLargeVarBinary(ByteBuffer.wrap("row11".getBytes(StandardCharsets.UTF_8))); unionWriter.setPosition(11); - unionWriter.writeLargeVarBinary(ByteBuffer.wrap( - "row12".getBytes(StandardCharsets.UTF_8)), 0, "row12".getBytes(StandardCharsets.UTF_8).length); + unionWriter.writeLargeVarBinary( + ByteBuffer.wrap("row12".getBytes(StandardCharsets.UTF_8)), + 0, + "row12".getBytes(StandardCharsets.UTF_8).length); unionWriter.end(); assertEquals("row1", new String(vector.getVarCharVector().get(0), StandardCharsets.UTF_8)); assertEquals("row2", new String(vector.getVarCharVector().get(1), StandardCharsets.UTF_8)); - assertEquals("row3", new String(vector.getLargeVarCharVector().get(2), StandardCharsets.UTF_8)); - assertEquals("row4", new String(vector.getLargeVarCharVector().get(3), StandardCharsets.UTF_8)); + assertEquals( + "row3", new String(vector.getLargeVarCharVector().get(2), StandardCharsets.UTF_8)); + assertEquals( + "row4", new String(vector.getLargeVarCharVector().get(3), StandardCharsets.UTF_8)); assertEquals("row5", new String(vector.getVarBinaryVector().get(4), StandardCharsets.UTF_8)); assertEquals("row6", new String(vector.getVarBinaryVector().get(5), StandardCharsets.UTF_8)); assertEquals("row7", new String(vector.getVarBinaryVector().get(6), StandardCharsets.UTF_8)); assertEquals("row8", new String(vector.getVarBinaryVector().get(7), StandardCharsets.UTF_8)); - assertEquals("row9", new String(vector.getLargeVarBinaryVector().get(8), StandardCharsets.UTF_8)); - assertEquals("row10", new String(vector.getLargeVarBinaryVector().get(9), StandardCharsets.UTF_8)); - assertEquals("row11", new String(vector.getLargeVarBinaryVector().get(10), StandardCharsets.UTF_8)); - assertEquals("row12", new String(vector.getLargeVarBinaryVector().get(11), StandardCharsets.UTF_8)); + assertEquals( + "row9", new String(vector.getLargeVarBinaryVector().get(8), StandardCharsets.UTF_8)); + assertEquals( + "row10", new String(vector.getLargeVarBinaryVector().get(9), StandardCharsets.UTF_8)); + assertEquals( + "row11", new String(vector.getLargeVarBinaryVector().get(10), StandardCharsets.UTF_8)); + assertEquals( + "row12", new String(vector.getLargeVarBinaryVector().get(11), StandardCharsets.UTF_8)); } } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/complex/writer/TestSimpleWriter.java b/java/vector/src/test/java/org/apache/arrow/vector/complex/writer/TestSimpleWriter.java index 27b8f1796ee..0c28c5d6cd2 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/complex/writer/TestSimpleWriter.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/complex/writer/TestSimpleWriter.java @@ -18,7 +18,6 @@ package org.apache.arrow.vector.complex.writer; import java.nio.ByteBuffer; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.LargeVarBinaryVector; @@ -52,8 +51,8 @@ public void terminate() throws Exception { @Test public void testWriteByteArrayToVarBinary() throws Exception { try (VarBinaryVector vector = new VarBinaryVector("test", allocator); - VarBinaryWriter writer = new VarBinaryWriterImpl(vector)) { - byte[] input = new byte[] { 0x01, 0x02 }; + VarBinaryWriter writer = new VarBinaryWriterImpl(vector)) { + byte[] input = new byte[] {0x01, 0x02}; writer.writeVarBinary(input); byte[] result = vector.get(0); Assert.assertArrayEquals(input, result); @@ -63,19 +62,19 @@ public void testWriteByteArrayToVarBinary() throws Exception { @Test public void testWriteByteArrayWithOffsetToVarBinary() throws Exception { try (VarBinaryVector vector = new VarBinaryVector("test", allocator); - VarBinaryWriter writer = new VarBinaryWriterImpl(vector)) { - byte[] input = new byte[] { 0x01, 0x02 }; + VarBinaryWriter writer = new VarBinaryWriterImpl(vector)) { + byte[] input = new byte[] {0x01, 0x02}; writer.writeVarBinary(input, 1, 1); byte[] result = vector.get(0); - Assert.assertArrayEquals(new byte[] { 0x02 }, result); + Assert.assertArrayEquals(new byte[] {0x02}, result); } } @Test public void testWriteByteBufferToVarBinary() throws Exception { try (VarBinaryVector vector = new VarBinaryVector("test", allocator); - VarBinaryWriter writer = new VarBinaryWriterImpl(vector)) { - byte[] input = new byte[] { 0x01, 0x02 }; + VarBinaryWriter writer = new VarBinaryWriterImpl(vector)) { + byte[] input = new byte[] {0x01, 0x02}; ByteBuffer buffer = ByteBuffer.wrap(input); writer.writeVarBinary(buffer); byte[] result = vector.get(0); @@ -86,20 +85,20 @@ public void testWriteByteBufferToVarBinary() throws Exception { @Test public void testWriteByteBufferWithOffsetToVarBinary() throws Exception { try (VarBinaryVector vector = new VarBinaryVector("test", allocator); - VarBinaryWriter writer = new VarBinaryWriterImpl(vector)) { - byte[] input = new byte[] { 0x01, 0x02 }; + VarBinaryWriter writer = new VarBinaryWriterImpl(vector)) { + byte[] input = new byte[] {0x01, 0x02}; ByteBuffer buffer = ByteBuffer.wrap(input); writer.writeVarBinary(buffer, 1, 1); byte[] result = vector.get(0); - Assert.assertArrayEquals(new byte[] { 0x02 }, result); + Assert.assertArrayEquals(new byte[] {0x02}, result); } } @Test public void testWriteByteArrayToLargeVarBinary() throws Exception { try (LargeVarBinaryVector vector = new LargeVarBinaryVector("test", allocator); - LargeVarBinaryWriter writer = new LargeVarBinaryWriterImpl(vector)) { - byte[] input = new byte[] { 0x01, 0x02 }; + LargeVarBinaryWriter writer = new LargeVarBinaryWriterImpl(vector)) { + byte[] input = new byte[] {0x01, 0x02}; writer.writeLargeVarBinary(input); byte[] result = vector.get(0); Assert.assertArrayEquals(input, result); @@ -109,19 +108,19 @@ public void testWriteByteArrayToLargeVarBinary() throws Exception { @Test public void testWriteByteArrayWithOffsetToLargeVarBinary() throws Exception { try (LargeVarBinaryVector vector = new LargeVarBinaryVector("test", allocator); - LargeVarBinaryWriter writer = new LargeVarBinaryWriterImpl(vector)) { - byte[] input = new byte[] { 0x01, 0x02 }; + LargeVarBinaryWriter writer = new LargeVarBinaryWriterImpl(vector)) { + byte[] input = new byte[] {0x01, 0x02}; writer.writeLargeVarBinary(input, 1, 1); byte[] result = vector.get(0); - Assert.assertArrayEquals(new byte[] { 0x02 }, result); + Assert.assertArrayEquals(new byte[] {0x02}, result); } } @Test public void testWriteByteBufferToLargeVarBinary() throws Exception { try (LargeVarBinaryVector vector = new LargeVarBinaryVector("test", allocator); - LargeVarBinaryWriter writer = new LargeVarBinaryWriterImpl(vector)) { - byte[] input = new byte[] { 0x01, 0x02 }; + LargeVarBinaryWriter writer = new LargeVarBinaryWriterImpl(vector)) { + byte[] input = new byte[] {0x01, 0x02}; ByteBuffer buffer = ByteBuffer.wrap(input); writer.writeLargeVarBinary(buffer); byte[] result = vector.get(0); @@ -132,19 +131,19 @@ public void testWriteByteBufferToLargeVarBinary() throws Exception { @Test public void testWriteByteBufferWithOffsetToLargeVarBinary() throws Exception { try (LargeVarBinaryVector vector = new LargeVarBinaryVector("test", allocator); - LargeVarBinaryWriter writer = new LargeVarBinaryWriterImpl(vector)) { - byte[] input = new byte[] { 0x01, 0x02 }; + LargeVarBinaryWriter writer = new LargeVarBinaryWriterImpl(vector)) { + byte[] input = new byte[] {0x01, 0x02}; ByteBuffer buffer = ByteBuffer.wrap(input); writer.writeLargeVarBinary(buffer, 1, 1); byte[] result = vector.get(0); - Assert.assertArrayEquals(new byte[] { 0x02 }, result); + Assert.assertArrayEquals(new byte[] {0x02}, result); } } @Test public void testWriteStringToVarChar() throws Exception { try (VarCharVector vector = new VarCharVector("test", allocator); - VarCharWriter writer = new VarCharWriterImpl(vector)) { + VarCharWriter writer = new VarCharWriterImpl(vector)) { String input = "testInput"; writer.writeVarChar(input); String result = vector.getObject(0).toString(); @@ -155,7 +154,7 @@ public void testWriteStringToVarChar() throws Exception { @Test public void testWriteTextToVarChar() throws Exception { try (VarCharVector vector = new VarCharVector("test", allocator); - VarCharWriter writer = new VarCharWriterImpl(vector)) { + VarCharWriter writer = new VarCharWriterImpl(vector)) { String input = "testInput"; writer.writeVarChar(new Text(input)); String result = vector.getObject(0).toString(); @@ -166,7 +165,7 @@ public void testWriteTextToVarChar() throws Exception { @Test public void testWriteStringToLargeVarChar() throws Exception { try (LargeVarCharVector vector = new LargeVarCharVector("test", allocator); - LargeVarCharWriter writer = new LargeVarCharWriterImpl(vector)) { + LargeVarCharWriter writer = new LargeVarCharWriterImpl(vector)) { String input = "testInput"; writer.writeLargeVarChar(input); String result = vector.getObject(0).toString(); @@ -177,7 +176,7 @@ public void testWriteStringToLargeVarChar() throws Exception { @Test public void testWriteTextToLargeVarChar() throws Exception { try (LargeVarCharVector vector = new LargeVarCharVector("test", allocator); - LargeVarCharWriter writer = new LargeVarCharWriterImpl(vector)) { + LargeVarCharWriter writer = new LargeVarCharWriterImpl(vector)) { String input = "testInput"; writer.writeLargeVarChar(new Text(input)); String result = vector.getObject(0).toString(); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/ipc/BaseFileTest.java b/java/vector/src/test/java/org/apache/arrow/vector/ipc/BaseFileTest.java index de9187edb66..9e8b799f89c 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/ipc/BaseFileTest.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/ipc/BaseFileTest.java @@ -32,7 +32,6 @@ import java.time.ZoneOffset; import java.util.Arrays; import java.util.List; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -90,9 +89,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * Helps testing the file formats. - */ +/** Helps testing the file formats. */ public class BaseFileTest { private static final Logger LOGGER = LoggerFactory.getLogger(BaseFileTest.class); protected static final int COUNT = 10; @@ -108,13 +105,19 @@ public void tearDown() { allocator.close(); } - - private static short [] uint1Values = new short[]{0, 255, 1, 128, 2}; - private static char [] uint2Values = new char[]{0, Character.MAX_VALUE, 1, Short.MAX_VALUE * 2, 2}; - private static long [] uint4Values = new long[]{0, Integer.MAX_VALUE + 1L, 1, Integer.MAX_VALUE * 2L, 2}; - private static BigInteger[] uint8Values = new BigInteger[]{BigInteger.valueOf(0), - BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(2)), BigInteger.valueOf(2), - BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.valueOf(1)), BigInteger.valueOf(2)}; + private static short[] uint1Values = new short[] {0, 255, 1, 128, 2}; + private static char[] uint2Values = + new char[] {0, Character.MAX_VALUE, 1, Short.MAX_VALUE * 2, 2}; + private static long[] uint4Values = + new long[] {0, Integer.MAX_VALUE + 1L, 1, Integer.MAX_VALUE * 2L, 2}; + private static BigInteger[] uint8Values = + new BigInteger[] { + BigInteger.valueOf(0), + BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(2)), + BigInteger.valueOf(2), + BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.valueOf(1)), + BigInteger.valueOf(2) + }; protected void writeData(int count, StructVector parent) { ComplexWriter writer = new ComplexWriterImpl("root", parent); @@ -132,11 +135,11 @@ protected void writeData(int count, StructVector parent) { uint1Writer.setPosition(i); // TODO: Fix add safe write methods on uint methods. uint1Writer.setPosition(i); - uint1Writer.writeUInt1((byte) uint1Values[i % uint1Values.length] ); + uint1Writer.writeUInt1((byte) uint1Values[i % uint1Values.length]); uint2Writer.setPosition(i); - uint2Writer.writeUInt2((char) uint2Values[i % uint2Values.length] ); + uint2Writer.writeUInt2((char) uint2Values[i % uint2Values.length]); uint4Writer.setPosition(i); - uint4Writer.writeUInt4((int) uint4Values[i % uint4Values.length] ); + uint4Writer.writeUInt4((int) uint4Values[i % uint4Values.length]); uint8Writer.setPosition(i); uint8Writer.writeUInt8(uint8Values[i % uint8Values.length].longValue()); bigIntWriter.setPosition(i); @@ -147,17 +150,23 @@ protected void writeData(int count, StructVector parent) { writer.setValueCount(count); } - protected void validateContent(int count, VectorSchemaRoot root) { for (int i = 0; i < count; i++) { Assert.assertEquals(i, root.getVector("int").getObject(i)); - Assert.assertEquals((Short) uint1Values[i % uint1Values.length], + Assert.assertEquals( + (Short) uint1Values[i % uint1Values.length], ((UInt1Vector) root.getVector("uint1")).getObjectNoOverflow(i)); - Assert.assertEquals("Failed for index: " + i, (Character) uint2Values[i % uint2Values.length], + Assert.assertEquals( + "Failed for index: " + i, + (Character) uint2Values[i % uint2Values.length], (Character) ((UInt2Vector) root.getVector("uint2")).get(i)); - Assert.assertEquals("Failed for index: " + i, (Long) uint4Values[i % uint4Values.length], + Assert.assertEquals( + "Failed for index: " + i, + (Long) uint4Values[i % uint4Values.length], ((UInt4Vector) root.getVector("uint4")).getObjectNoOverflow(i)); - Assert.assertEquals("Failed for index: " + i, uint8Values[i % uint8Values.length], + Assert.assertEquals( + "Failed for index: " + i, + uint8Values[i % uint8Values.length], ((UInt8Vector) root.getVector("uint8")).getObjectNoOverflow(i)); Assert.assertEquals(Long.valueOf(i), root.getVector("bigInt").getObject(i)); Assert.assertEquals(i == 0 ? Float.NaN : i, root.getVector("float").getObject(i)); @@ -241,27 +250,33 @@ protected void writeDateTimeData(int count, StructVector parent) { DateMilliWriter dateWriter = rootWriter.dateMilli("date"); TimeMilliWriter timeWriter = rootWriter.timeMilli("time"); TimeStampMilliWriter timeStampMilliWriter = rootWriter.timeStampMilli("timestamp-milli"); - TimeStampMilliTZWriter timeStampMilliTZWriter = rootWriter.timeStampMilliTZ("timestamp-milliTZ", "Europe/Paris"); + TimeStampMilliTZWriter timeStampMilliTZWriter = + rootWriter.timeStampMilliTZ("timestamp-milliTZ", "Europe/Paris"); TimeStampNanoWriter timeStampNanoWriter = rootWriter.timeStampNano("timestamp-nano"); for (int i = 0; i < count; i++) { LocalDateTime dt = makeDateTimeFromCount(i); - // Number of days in milliseconds since epoch, stored as 64-bit integer, only date part is used + // Number of days in milliseconds since epoch, stored as 64-bit integer, only date part is + // used dateWriter.setPosition(i); long dateLong = dt.toLocalDate().atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli(); dateWriter.writeDateMilli(dateLong); // Time is a value in milliseconds since midnight, stored as 32-bit integer timeWriter.setPosition(i); - int milliOfDay = (int) java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(dt.toLocalTime().toNanoOfDay()); + int milliOfDay = + (int) java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(dt.toLocalTime().toNanoOfDay()); timeWriter.writeTimeMilli(milliOfDay); // Timestamp as milliseconds since the epoch, stored as 64-bit integer timeStampMilliWriter.setPosition(i); timeStampMilliWriter.writeTimeStampMilli(dt.toInstant(ZoneOffset.UTC).toEpochMilli()); // Timestamp as milliseconds since epoch with timezone timeStampMilliTZWriter.setPosition(i); - timeStampMilliTZWriter.writeTimeStampMilliTZ(dt.atZone(ZoneId.of("Europe/Paris")).toInstant().toEpochMilli()); + timeStampMilliTZWriter.writeTimeStampMilliTZ( + dt.atZone(ZoneId.of("Europe/Paris")).toInstant().toEpochMilli()); // Timestamp as nanoseconds since epoch timeStampNanoWriter.setPosition(i); - long tsNanos = dt.toInstant(ZoneOffset.UTC).toEpochMilli() * 1_000_000 + i; // need to add back in nano val + long tsNanos = + dt.toInstant(ZoneOffset.UTC).toEpochMilli() * 1_000_000 + + i; // need to add back in nano val timeStampNanoWriter.writeTimeStampNano(tsNanos); } writer.setValueCount(count); @@ -281,15 +296,15 @@ protected void validateDateTimeContent(int count, VectorSchemaRoot root) { Object timestampMilliVal = root.getVector("timestamp-milli").getObject(i); Assert.assertEquals(dtMilli, timestampMilliVal); Object timestampMilliTZVal = root.getVector("timestamp-milliTZ").getObject(i); - Assert.assertEquals(dt.atZone(ZoneId.of("Europe/Paris")).toInstant().toEpochMilli(), timestampMilliTZVal); + Assert.assertEquals( + dt.atZone(ZoneId.of("Europe/Paris")).toInstant().toEpochMilli(), timestampMilliTZVal); Object timestampNanoVal = root.getVector("timestamp-nano").getObject(i); Assert.assertEquals(dt, timestampNanoVal); } } protected VectorSchemaRoot writeFlatDictionaryData( - BufferAllocator bufferAllocator, - DictionaryProvider.MapDictionaryProvider provider) { + BufferAllocator bufferAllocator, DictionaryProvider.MapDictionaryProvider provider) { // Define dictionaries and add to provider VarCharVector dictionary1Vector = newVarCharVector("D1", bufferAllocator); @@ -299,7 +314,8 @@ protected VectorSchemaRoot writeFlatDictionaryData( dictionary1Vector.set(2, "baz".getBytes(StandardCharsets.UTF_8)); dictionary1Vector.setValueCount(3); - Dictionary dictionary1 = new Dictionary(dictionary1Vector, new DictionaryEncoding(1L, false, null)); + Dictionary dictionary1 = + new Dictionary(dictionary1Vector, new DictionaryEncoding(1L, false, null)); provider.put(dictionary1); VarCharVector dictionary2Vector = newVarCharVector("D2", bufferAllocator); @@ -309,7 +325,8 @@ protected VectorSchemaRoot writeFlatDictionaryData( dictionary2Vector.set(2, "large".getBytes(StandardCharsets.UTF_8)); dictionary2Vector.setValueCount(3); - Dictionary dictionary2 = new Dictionary(dictionary2Vector, new DictionaryEncoding(2L, false, null)); + Dictionary dictionary2 = + new Dictionary(dictionary2Vector, new DictionaryEncoding(2L, false, null)); provider.put(dictionary2); // Populate the vectors @@ -346,9 +363,11 @@ protected VectorSchemaRoot writeFlatDictionaryData( FieldVector encodedVector2 = (FieldVector) DictionaryEncoder.encode(vector2, dictionary2); vector2.close(); // Done with this vector after encoding - List fields = Arrays.asList(encodedVector1A.getField(), encodedVector1B.getField(), - encodedVector2.getField()); - List vectors = Collections2.asImmutableList(encodedVector1A, encodedVector1B, encodedVector2); + List fields = + Arrays.asList( + encodedVector1A.getField(), encodedVector1B.getField(), encodedVector2.getField()); + List vectors = + Collections2.asImmutableList(encodedVector1A, encodedVector1B, encodedVector2); return new VectorSchemaRoot(fields, vectors, encodedVector1A.getValueCount()); } @@ -418,8 +437,7 @@ protected void validateFlatDictionary(VectorSchemaRoot root, DictionaryProvider } protected VectorSchemaRoot writeNestedDictionaryData( - BufferAllocator bufferAllocator, - DictionaryProvider.MapDictionaryProvider provider) { + BufferAllocator bufferAllocator, DictionaryProvider.MapDictionaryProvider provider) { // Define the dictionary and add to the provider VarCharVector dictionaryVector = newVarCharVector("D2", bufferAllocator); @@ -428,7 +446,8 @@ protected VectorSchemaRoot writeNestedDictionaryData( dictionaryVector.set(1, "bar".getBytes(StandardCharsets.UTF_8)); dictionaryVector.setValueCount(2); - Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(2L, false, null)); + Dictionary dictionary = + new Dictionary(dictionaryVector, new DictionaryEncoding(2L, false, null)); provider.put(dictionary); // Write the vector data using dictionary indices @@ -498,9 +517,11 @@ protected VectorSchemaRoot writeDecimalData(BufferAllocator bufferAllocator) { decimalVector2.setValueCount(count); decimalVector3.setValueCount(count); - List fields = Collections2.asImmutableList(decimalVector1.getField(), decimalVector2.getField(), - decimalVector3.getField()); - List vectors = Collections2.asImmutableList(decimalVector1, decimalVector2, decimalVector3); + List fields = + Collections2.asImmutableList( + decimalVector1.getField(), decimalVector2.getField(), decimalVector3.getField()); + List vectors = + Collections2.asImmutableList(decimalVector1, decimalVector2, decimalVector3); return new VectorSchemaRoot(fields, vectors, count); } @@ -538,7 +559,8 @@ protected VectorSchemaRoot writeNullData(int valueCount) { nullVector1.setValueCount(valueCount); nullVector2.setValueCount(valueCount); - List fields = Collections2.asImmutableList(nullVector1.getField(), nullVector2.getField()); + List fields = + Collections2.asImmutableList(nullVector1.getField(), nullVector2.getField()); List vectors = Collections2.asImmutableList(nullVector1, nullVector2); return new VectorSchemaRoot(fields, vectors, valueCount); } @@ -669,7 +691,8 @@ protected void validateVarBinary(int count, VectorSchemaRoot root) { Assert.assertEquals(binaryVector.getLastSet(), numVarBinaryValues - 1); } - protected void writeBatchData(ArrowWriter writer, IntVector vector, VectorSchemaRoot root) throws IOException { + protected void writeBatchData(ArrowWriter writer, IntVector vector, VectorSchemaRoot root) + throws IOException { writer.start(); vector.setNull(0); @@ -752,7 +775,8 @@ protected VectorSchemaRoot writeMapData(BufferAllocator bufferAllocator) { mapWriter.setValueCount(COUNT); sortedMapWriter.setValueCount(COUNT); - List fields = Collections2.asImmutableList(mapVector.getField(), sortedMapVector.getField()); + List fields = + Collections2.asImmutableList(mapVector.getField(), sortedMapVector.getField()); List vectors = Collections2.asImmutableList(mapVector, sortedMapVector); return new VectorSchemaRoot(fields, vectors, count); } @@ -801,9 +825,12 @@ protected VectorSchemaRoot writeListAsMapData(BufferAllocator bufferAllocator) { ListVector mapEntryList = ListVector.empty("entryList", bufferAllocator); FieldType mapEntryType = new FieldType(false, ArrowType.Struct.INSTANCE, null, null); StructVector mapEntryData = new StructVector("entryData", bufferAllocator, mapEntryType, null); - mapEntryData.addOrGet("myKey", new FieldType(false, new ArrowType.Int(64, true), null), BigIntVector.class); - mapEntryData.addOrGet("myValue", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); - mapEntryList.initializeChildrenFromFields(Collections2.asImmutableList(mapEntryData.getField())); + mapEntryData.addOrGet( + "myKey", new FieldType(false, new ArrowType.Int(64, true), null), BigIntVector.class); + mapEntryData.addOrGet( + "myValue", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); + mapEntryList.initializeChildrenFromFields( + Collections2.asImmutableList(mapEntryData.getField())); UnionListWriter entryWriter = mapEntryList.getWriter(); entryWriter.allocate(); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/ipc/ITTestIPCWithLargeArrowBuffers.java b/java/vector/src/test/java/org/apache/arrow/vector/ipc/ITTestIPCWithLargeArrowBuffers.java index d3c91fd1443..b93b8260472 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/ipc/ITTestIPCWithLargeArrowBuffers.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/ipc/ITTestIPCWithLargeArrowBuffers.java @@ -28,7 +28,6 @@ import java.io.IOException; import java.util.Arrays; import java.util.Map; - import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.BigIntVector; import org.apache.arrow.vector.IntVector; @@ -45,14 +44,14 @@ import org.slf4j.LoggerFactory; /** - * Integration test for reading/writing {@link org.apache.arrow.vector.VectorSchemaRoot} with - * large (more than 2GB) buffers by {@link ArrowReader} and {@link ArrowWriter}.. - * To run this test, please make sure there is at least 8GB free memory, and 8GB - * free.disk space in the system. + * Integration test for reading/writing {@link org.apache.arrow.vector.VectorSchemaRoot} with large + * (more than 2GB) buffers by {@link ArrowReader} and {@link ArrowWriter}.. To run this test, please + * make sure there is at least 8GB free memory, and 8GB free.disk space in the system. */ public class ITTestIPCWithLargeArrowBuffers { - private static final Logger logger = LoggerFactory.getLogger(ITTestIPCWithLargeArrowBuffers.class); + private static final Logger logger = + LoggerFactory.getLogger(ITTestIPCWithLargeArrowBuffers.class); // 4GB buffer size static final long BUFFER_SIZE = 4 * 1024 * 1024 * 1024L; @@ -78,12 +77,13 @@ public class ITTestIPCWithLargeArrowBuffers { private void testWriteLargeArrowData(boolean streamMode) throws IOException { // simulate encoding big int as int try (RootAllocator allocator = new RootAllocator(Long.MAX_VALUE); - BigIntVector dictVector = new BigIntVector("dic vector", allocator); - FileOutputStream out = new FileOutputStream(FILE_NAME); - IntVector encodedVector = (IntVector) ENCODED_VECTOR_FIELD.createVector(allocator)) { + BigIntVector dictVector = new BigIntVector("dic vector", allocator); + FileOutputStream out = new FileOutputStream(FILE_NAME); + IntVector encodedVector = (IntVector) ENCODED_VECTOR_FIELD.createVector(allocator)) { // prepare dictionary provider. - DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); + DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); Dictionary dictionary = new Dictionary(dictVector, DICTIONARY_ENCODING); provider.put(dictionary); @@ -107,11 +107,14 @@ private void testWriteLargeArrowData(boolean streamMode) throws IOException { // build vector schema root and write data. try (VectorSchemaRoot root = - new VectorSchemaRoot( - Arrays.asList(ENCODED_VECTOR_FIELD), Arrays.asList(encodedVector), ENCODED_VECTOR_SIZE); - ArrowWriter writer = streamMode ? - new ArrowStreamWriter(root, provider, out) : - new ArrowFileWriter(root, provider, out.getChannel())) { + new VectorSchemaRoot( + Arrays.asList(ENCODED_VECTOR_FIELD), + Arrays.asList(encodedVector), + ENCODED_VECTOR_SIZE); + ArrowWriter writer = + streamMode + ? new ArrowStreamWriter(root, provider, out) + : new ArrowFileWriter(root, provider, out.getChannel())) { writer.start(); writer.writeBatch(); writer.end(); @@ -124,10 +127,11 @@ private void testWriteLargeArrowData(boolean streamMode) throws IOException { private void testReadLargeArrowData(boolean streamMode) throws IOException { try (RootAllocator allocator = new RootAllocator(Long.MAX_VALUE); - FileInputStream in = new FileInputStream(FILE_NAME); - ArrowReader reader = streamMode ? - new ArrowStreamReader(in, allocator) : - new ArrowFileReader(in.getChannel(), allocator)) { + FileInputStream in = new FileInputStream(FILE_NAME); + ArrowReader reader = + streamMode + ? new ArrowStreamReader(in, allocator) + : new ArrowFileReader(in.getChannel(), allocator)) { // verify schema Schema readSchema = reader.getVectorSchemaRoot().getSchema(); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/ipc/MessageSerializerTest.java b/java/vector/src/test/java/org/apache/arrow/vector/ipc/MessageSerializerTest.java index 79a4b249a8a..b2f113920df 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/ipc/MessageSerializerTest.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/ipc/MessageSerializerTest.java @@ -33,7 +33,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -74,7 +73,7 @@ private int intToByteRoundtrip(int v, byte[] bytes) { @Test public void testIntToBytes() { byte[] bytes = new byte[4]; - int[] values = new int[]{1, 15, 1 << 8, 1 << 16, Integer.MAX_VALUE}; + int[] values = new int[] {1, 15, 1 << 8, 1 << 16, Integer.MAX_VALUE}; for (int v : values) { assertEquals(intToByteRoundtrip(v, bytes), v); } @@ -129,13 +128,12 @@ public void testWriteMessageBufferAligned() throws IOException { public void testSchemaMessageSerialization() throws IOException { Schema schema = testSchema(); ByteArrayOutputStream out = new ByteArrayOutputStream(); - long size = MessageSerializer.serialize( - new WriteChannel(Channels.newChannel(out)), schema); + long size = MessageSerializer.serialize(new WriteChannel(Channels.newChannel(out)), schema); assertEquals(size, out.toByteArray().length); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Schema deserialized = MessageSerializer.deserializeSchema( - new ReadChannel(Channels.newChannel(in))); + Schema deserialized = + MessageSerializer.deserializeSchema(new ReadChannel(Channels.newChannel(in))); assertEquals(schema, deserialized); assertEquals(1, deserialized.getFields().size()); } @@ -143,32 +141,33 @@ public void testSchemaMessageSerialization() throws IOException { @Test public void testSchemaDictionaryMessageSerialization() throws IOException { DictionaryEncoding dictionary = new DictionaryEncoding(9L, false, new ArrowType.Int(8, true)); - Field field = new Field("test", new FieldType(true, ArrowType.Utf8.INSTANCE, dictionary, null), null); + Field field = + new Field("test", new FieldType(true, ArrowType.Utf8.INSTANCE, dictionary, null), null); Schema schema = new Schema(Collections.singletonList(field)); ByteArrayOutputStream out = new ByteArrayOutputStream(); long size = MessageSerializer.serialize(new WriteChannel(Channels.newChannel(out)), schema); assertEquals(size, out.toByteArray().length); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Schema deserialized = MessageSerializer.deserializeSchema(new ReadChannel(Channels.newChannel(in))); + Schema deserialized = + MessageSerializer.deserializeSchema(new ReadChannel(Channels.newChannel(in))); assertEquals(schema, deserialized); } - @Rule - public ExpectedException expectedEx = ExpectedException.none(); + @Rule public ExpectedException expectedEx = ExpectedException.none(); @Test public void testSerializeRecordBatchV4() throws IOException { - byte[] validity = new byte[]{(byte) 255, 0}; + byte[] validity = new byte[] {(byte) 255, 0}; // second half is "undefined" - byte[] values = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; + byte[] values = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; BufferAllocator alloc = new RootAllocator(Long.MAX_VALUE); ArrowBuf validityb = buf(alloc, validity); ArrowBuf valuesb = buf(alloc, values); - ArrowRecordBatch batch = new ArrowRecordBatch( - 16, asList(new ArrowFieldNode(16, 8)), asList(validityb, valuesb)); + ArrowRecordBatch batch = + new ArrowRecordBatch(16, asList(new ArrowFieldNode(16, 8)), asList(validityb, valuesb)); // avoid writing legacy ipc format by default IpcOption option = new IpcOption(false, MetadataVersion.V4); @@ -184,16 +183,16 @@ public void testSerializeRecordBatchV4() throws IOException { @Test public void testSerializeRecordBatchV5() throws Exception { - byte[] validity = new byte[]{(byte) 255, 0}; + byte[] validity = new byte[] {(byte) 255, 0}; // second half is "undefined" - byte[] values = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; + byte[] values = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; BufferAllocator alloc = new RootAllocator(Long.MAX_VALUE); ArrowBuf validityb = buf(alloc, validity); ArrowBuf valuesb = buf(alloc, values); - ArrowRecordBatch batch = new ArrowRecordBatch( - 16, asList(new ArrowFieldNode(16, 8)), asList(validityb, valuesb)); + ArrowRecordBatch batch = + new ArrowRecordBatch(16, asList(new ArrowFieldNode(16, 8)), asList(validityb, valuesb)); // avoid writing legacy ipc format by default IpcOption option = new IpcOption(false, MetadataVersion.V5); @@ -214,20 +213,25 @@ public void testSerializeRecordBatchV5() throws Exception { { byte[] validBytes = out.toByteArray(); - byte[] missingBytes = Arrays.copyOfRange(validBytes, /*from=*/0, validBytes.length - 1); + byte[] missingBytes = Arrays.copyOfRange(validBytes, /* from= */ 0, validBytes.length - 1); ByteArrayInputStream in = new ByteArrayInputStream(missingBytes); ReadChannel channel = new ReadChannel(Channels.newChannel(in)); - assertThrows(IOException.class, () -> MessageSerializer.deserializeMessageBatch(channel, alloc)); + assertThrows( + IOException.class, () -> MessageSerializer.deserializeMessageBatch(channel, alloc)); } alloc.close(); } public static Schema testSchema() { - return new Schema(asList(new Field( - "testField", FieldType.nullable(new ArrowType.Int(8, true)), Collections.emptyList()))); + return new Schema( + asList( + new Field( + "testField", + FieldType.nullable(new ArrowType.Int(8, true)), + Collections.emptyList()))); } // Verifies batch contents matching test schema. @@ -243,5 +247,4 @@ public static void verifyBatch(ArrowRecordBatch batch, byte[] validity, byte[] v assertArrayEquals(validity, MessageSerializerTest.array(buffers.get(0))); assertArrayEquals(values, MessageSerializerTest.array(buffers.get(1))); } - } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowFile.java b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowFile.java index 4fb58227860..bebe18940ea 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowFile.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowFile.java @@ -31,7 +31,6 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.util.Collections2; import org.apache.arrow.vector.FieldVector; @@ -50,8 +49,8 @@ public class TestArrowFile extends BaseFileTest { public void testWrite() throws IOException { File file = new File("target/mytest_write.arrow"); int count = COUNT; - try ( - BufferAllocator vectorAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); + try (BufferAllocator vectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); StructVector parent = StructVector.empty("parent", vectorAllocator)) { writeData(count, parent); write(parent.getChild("root"), file, new ByteArrayOutputStream()); @@ -62,8 +61,8 @@ public void testWrite() throws IOException { public void testWriteComplex() throws IOException { File file = new File("target/mytest_write_complex.arrow"); int count = COUNT; - try ( - BufferAllocator vectorAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); + try (BufferAllocator vectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); StructVector parent = StructVector.empty("parent", vectorAllocator)) { writeComplexData(count, parent); FieldVector root = parent.getChild("root"); @@ -73,14 +72,15 @@ public void testWriteComplex() throws IOException { } /** - * Writes the contents of parents to file. If outStream is non-null, also writes it - * to outStream in the streaming serialized format. + * Writes the contents of parents to file. If outStream is non-null, also writes it to outStream + * in the streaming serialized format. */ private void write(FieldVector parent, File file, OutputStream outStream) throws IOException { VectorSchemaRoot root = new VectorSchemaRoot(parent); try (FileOutputStream fileOutputStream = new FileOutputStream(file); - ArrowFileWriter arrowWriter = new ArrowFileWriter(root, null, fileOutputStream.getChannel());) { + ArrowFileWriter arrowWriter = + new ArrowFileWriter(root, null, fileOutputStream.getChannel()); ) { LOGGER.debug("writing schema: " + root.getSchema()); arrowWriter.start(); arrowWriter.writeBatch(); @@ -124,7 +124,8 @@ public void testFileStreamHasEos() throws IOException { byte[] bytesWithoutMagic = new byte[bytes.length - 8]; System.arraycopy(bytes, 8, bytesWithoutMagic, 0, bytesWithoutMagic.length); - try (ArrowStreamReader reader = new ArrowStreamReader(new ByteArrayInputStream(bytesWithoutMagic), allocator)) { + try (ArrowStreamReader reader = + new ArrowStreamReader(new ByteArrayInputStream(bytesWithoutMagic), allocator)) { assertTrue(reader.loadNextBatch()); // here will throw exception if read footer instead of eos. assertFalse(reader.loadNextBatch()); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowFooter.java b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowFooter.java index 38c65bddedd..6b6d68e604f 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowFooter.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowFooter.java @@ -20,11 +20,11 @@ import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals; +import com.google.flatbuffers.FlatBufferBuilder; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; import java.util.List; - import org.apache.arrow.flatbuf.Footer; import org.apache.arrow.vector.ipc.message.ArrowBlock; import org.apache.arrow.vector.ipc.message.ArrowFooter; @@ -34,17 +34,20 @@ import org.apache.arrow.vector.types.pojo.Schema; import org.junit.Test; -import com.google.flatbuffers.FlatBufferBuilder; - public class TestArrowFooter { @Test public void test() { - Schema schema = new Schema(asList( - new Field("a", FieldType.nullable(new ArrowType.Int(8, true)), Collections.emptyList()) - )); + Schema schema = + new Schema( + asList( + new Field( + "a", + FieldType.nullable(new ArrowType.Int(8, true)), + Collections.emptyList()))); ArrowFooter footer = - new ArrowFooter(schema, Collections.emptyList(), Collections.emptyList()); + new ArrowFooter( + schema, Collections.emptyList(), Collections.emptyList()); ArrowFooter newFooter = roundTrip(footer); assertEquals(footer, newFooter); @@ -55,7 +58,6 @@ public void test() { assertEquals(footer, roundTrip(footer)); } - private ArrowFooter roundTrip(ArrowFooter footer) { FlatBufferBuilder builder = new FlatBufferBuilder(); int i = footer.writeTo(builder); @@ -64,5 +66,4 @@ private ArrowFooter roundTrip(ArrowFooter footer) { ArrowFooter newFooter = new ArrowFooter(Footer.getRootAsFooter(dataBuffer)); return newFooter; } - } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowReaderWriter.java b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowReaderWriter.java index 07875b25029..5abe0993488 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowReaderWriter.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowReaderWriter.java @@ -45,7 +45,6 @@ import java.util.Map; import java.util.function.BiFunction; import java.util.stream.Collectors; - import org.apache.arrow.flatbuf.FieldNode; import org.apache.arrow.flatbuf.Message; import org.apache.arrow.flatbuf.RecordBatch; @@ -114,40 +113,51 @@ public void init() { allocator = new RootAllocator(Long.MAX_VALUE); dictionaryVector1 = newVarCharVector("D1", allocator); - setVector(dictionaryVector1, + setVector( + dictionaryVector1, "foo".getBytes(StandardCharsets.UTF_8), "bar".getBytes(StandardCharsets.UTF_8), "baz".getBytes(StandardCharsets.UTF_8)); dictionaryVector2 = newVarCharVector("D2", allocator); - setVector(dictionaryVector2, + setVector( + dictionaryVector2, "aa".getBytes(StandardCharsets.UTF_8), "bb".getBytes(StandardCharsets.UTF_8), "cc".getBytes(StandardCharsets.UTF_8)); dictionaryVector3 = newVarCharVector("D3", allocator); - setVector(dictionaryVector3, + setVector( + dictionaryVector3, "foo".getBytes(StandardCharsets.UTF_8), "bar".getBytes(StandardCharsets.UTF_8), "baz".getBytes(StandardCharsets.UTF_8), "aa".getBytes(StandardCharsets.UTF_8), "bb".getBytes(StandardCharsets.UTF_8), "cc".getBytes(StandardCharsets.UTF_8)); - + dictionaryVector4 = newVector(StructVector.class, "D4", MinorType.STRUCT, allocator); final Map> dictionaryValues4 = new HashMap<>(); dictionaryValues4.put("a", Arrays.asList(1, 2, 3)); dictionaryValues4.put("b", Arrays.asList(4, 5, 6)); setVector(dictionaryVector4, dictionaryValues4); - dictionary1 = new Dictionary(dictionaryVector1, - new DictionaryEncoding(/*id=*/1L, /*ordered=*/false, /*indexType=*/null)); - dictionary2 = new Dictionary(dictionaryVector2, - new DictionaryEncoding(/*id=*/2L, /*ordered=*/false, /*indexType=*/null)); - dictionary3 = new Dictionary(dictionaryVector3, - new DictionaryEncoding(/*id=*/1L, /*ordered=*/false, /*indexType=*/null)); - dictionary4 = new Dictionary(dictionaryVector4, - new DictionaryEncoding(/*id=*/3L, /*ordered=*/false, /*indexType=*/null)); + dictionary1 = + new Dictionary( + dictionaryVector1, + new DictionaryEncoding(/* id= */ 1L, /* ordered= */ false, /* indexType= */ null)); + dictionary2 = + new Dictionary( + dictionaryVector2, + new DictionaryEncoding(/* id= */ 2L, /* ordered= */ false, /* indexType= */ null)); + dictionary3 = + new Dictionary( + dictionaryVector3, + new DictionaryEncoding(/* id= */ 1L, /* ordered= */ false, /* indexType= */ null)); + dictionary4 = + new Dictionary( + dictionaryVector4, + new DictionaryEncoding(/* id= */ 3L, /* ordered= */ false, /* indexType= */ null)); } @After @@ -173,8 +183,13 @@ byte[] array(ArrowBuf buf) { @Test public void test() throws IOException { - Schema schema = new Schema(asList(new Field("testField", FieldType.nullable(new ArrowType.Int(8, true)), - Collections.emptyList()))); + Schema schema = + new Schema( + asList( + new Field( + "testField", + FieldType.nullable(new ArrowType.Int(8, true)), + Collections.emptyList()))); ArrowType type = schema.getFields().get(0).getType(); FieldVector vector = TestUtils.newVector(FieldVector.class, "testField", type, allocator); vector.initializeChildrenFromFields(schema.getFields().get(0).getChildren()); @@ -185,10 +200,11 @@ public void test() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); try (VectorSchemaRoot root = new VectorSchemaRoot(schema.getFields(), asList(vector), 16); - ArrowFileWriter writer = new ArrowFileWriter(root, null, newChannel(out))) { + ArrowFileWriter writer = new ArrowFileWriter(root, null, newChannel(out))) { ArrowBuf validityb = buf(validity); ArrowBuf valuesb = buf(values); - ArrowRecordBatch batch = new ArrowRecordBatch(16, asList(new ArrowFieldNode(16, 8)), asList(validityb, valuesb)); + ArrowRecordBatch batch = + new ArrowRecordBatch(16, asList(new ArrowFieldNode(16, 8)), asList(validityb, valuesb)); VectorLoader loader = new VectorLoader(root); loader.load(batch); writer.writeBatch(); @@ -200,8 +216,9 @@ public void test() throws IOException { byte[] byteArray = out.toByteArray(); - try (SeekableReadChannel channel = new SeekableReadChannel(new ByteArrayReadableSeekableByteChannel(byteArray)); - ArrowFileReader reader = new ArrowFileReader(channel, allocator)) { + try (SeekableReadChannel channel = + new SeekableReadChannel(new ByteArrayReadableSeekableByteChannel(byteArray)); + ArrowFileReader reader = new ArrowFileReader(channel, allocator)) { Schema readSchema = reader.getVectorSchemaRoot().getSchema(); assertEquals(schema, readSchema); // TODO: dictionaries @@ -249,11 +266,12 @@ public void testWriteReadNullVector() throws IOException { Schema schema = new Schema(asList(nullVector.getField())); ByteArrayOutputStream out = new ByteArrayOutputStream(); - try (VectorSchemaRoot root = new VectorSchemaRoot(schema.getFields(), asList(nullVector), valueCount); + try (VectorSchemaRoot root = + new VectorSchemaRoot(schema.getFields(), asList(nullVector), valueCount); ArrowFileWriter writer = new ArrowFileWriter(root, null, newChannel(out))) { - ArrowRecordBatch batch = new ArrowRecordBatch(valueCount, - asList(new ArrowFieldNode(valueCount, 0)), - Collections.emptyList()); + ArrowRecordBatch batch = + new ArrowRecordBatch( + valueCount, asList(new ArrowFieldNode(valueCount, 0)), Collections.emptyList()); VectorLoader loader = new VectorLoader(root); loader.load(batch); writer.writeBatch(); @@ -261,7 +279,8 @@ public void testWriteReadNullVector() throws IOException { byte[] byteArray = out.toByteArray(); - try (SeekableReadChannel channel = new SeekableReadChannel(new ByteArrayReadableSeekableByteChannel(byteArray)); + try (SeekableReadChannel channel = + new SeekableReadChannel(new ByteArrayReadableSeekableByteChannel(byteArray)); ArrowFileReader reader = new ArrowFileReader(channel, allocator)) { Schema readSchema = reader.getVectorSchemaRoot().getSchema(); assertEquals(schema, readSchema); @@ -271,14 +290,16 @@ public void testWriteReadNullVector() throws IOException { assertTrue(reader.loadNextBatch()); assertEquals(1, reader.getVectorSchemaRoot().getFieldVectors().size()); - NullVector readNullVector = (NullVector) reader.getVectorSchemaRoot().getFieldVectors().get(0); + NullVector readNullVector = + (NullVector) reader.getVectorSchemaRoot().getFieldVectors().get(0); assertEquals(valueCount, readNullVector.getValueCount()); } } @Test public void testWriteReadWithDictionaries() throws IOException { - DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); + DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); provider.put(dictionary1); VarCharVector vector1 = newVarCharVector("varchar1", allocator); @@ -306,16 +327,17 @@ public void testWriteReadWithDictionaries() throws IOException { List fields = Arrays.asList(encodedVector1.getField(), encodedVector2.getField()); List vectors = Collections2.asImmutableList(encodedVector1, encodedVector2); - try (VectorSchemaRoot root = new VectorSchemaRoot(fields, vectors, encodedVector1.getValueCount()); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ArrowFileWriter writer = new ArrowFileWriter(root, provider, newChannel(out));) { + try (VectorSchemaRoot root = + new VectorSchemaRoot(fields, vectors, encodedVector1.getValueCount()); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ArrowFileWriter writer = new ArrowFileWriter(root, provider, newChannel(out)); ) { writer.start(); writer.writeBatch(); writer.end(); - try (SeekableReadChannel channel = new SeekableReadChannel( - new ByteArrayReadableSeekableByteChannel(out.toByteArray())); + try (SeekableReadChannel channel = + new SeekableReadChannel(new ByteArrayReadableSeekableByteChannel(out.toByteArray())); ArrowFileReader reader = new ArrowFileReader(channel, allocator)) { Schema readSchema = reader.getVectorSchemaRoot().getSchema(); assertEquals(root.getSchema(), readSchema); @@ -345,19 +367,18 @@ public void testWriteReadWithStructDictionaries() throws IOException { List fields = Arrays.asList(encodedVector.getField()); List vectors = Collections2.asImmutableList(encodedVector); - try ( - VectorSchemaRoot root = + try (VectorSchemaRoot root = new VectorSchemaRoot(fields, vectors, encodedVector.getValueCount()); ByteArrayOutputStream out = new ByteArrayOutputStream(); - ArrowFileWriter writer = new ArrowFileWriter(root, provider, newChannel(out));) { + ArrowFileWriter writer = new ArrowFileWriter(root, provider, newChannel(out)); ) { writer.start(); writer.writeBatch(); writer.end(); - try ( - SeekableReadChannel channel = new SeekableReadChannel( - new ByteArrayReadableSeekableByteChannel(out.toByteArray())); + try (SeekableReadChannel channel = + new SeekableReadChannel( + new ByteArrayReadableSeekableByteChannel(out.toByteArray())); ArrowFileReader reader = new ArrowFileReader(channel, allocator)) { final VectorSchemaRoot readRoot = reader.getVectorSchemaRoot(); final Schema readSchema = readRoot.getSchema(); @@ -372,8 +393,9 @@ public void testWriteReadWithStructDictionaries() throws IOException { // Read the encoded vector and check it final FieldVector readEncoded = readRoot.getVector(0); assertEquals(encodedVector.getValueCount(), readEncoded.getValueCount()); - assertTrue(new RangeEqualsVisitor(encodedVector, readEncoded) - .rangeEquals(new Range(0, 0, encodedVector.getValueCount()))); + assertTrue( + new RangeEqualsVisitor(encodedVector, readEncoded) + .rangeEquals(new Range(0, 0, encodedVector.getValueCount()))); // Read the dictionary final Map readDictionaryMap = reader.getDictionaryVectors(); @@ -386,16 +408,18 @@ public void testWriteReadWithStructDictionaries() throws IOException { assertEquals(dictionaryVector4.getValueCount(), readDictionaryVector.getValueCount()); final BiFunction typeComparatorIgnoreName = (v1, v2) -> new TypeEqualsVisitor(v1, false, true).equals(v2); - assertTrue("Dictionary vectors are not equal", - new RangeEqualsVisitor(dictionaryVector4, readDictionaryVector, - typeComparatorIgnoreName) - .rangeEquals(new Range(0, 0, dictionaryVector4.getValueCount()))); + assertTrue( + "Dictionary vectors are not equal", + new RangeEqualsVisitor( + dictionaryVector4, readDictionaryVector, typeComparatorIgnoreName) + .rangeEquals(new Range(0, 0, dictionaryVector4.getValueCount()))); // Assert the decoded vector is correct try (final ValueVector readVector = DictionaryEncoder.decode(readEncoded, readDictionary)) { assertEquals(vector.getValueCount(), readVector.getValueCount()); - assertTrue("Decoded vectors are not equal", + assertTrue( + "Decoded vectors are not equal", new RangeEqualsVisitor(vector, readVector, typeComparatorIgnoreName) .rangeEquals(new Range(0, 0, vector.getValueCount()))); } @@ -407,7 +431,8 @@ public void testWriteReadWithStructDictionaries() throws IOException { @Test public void testEmptyStreamInFileIPC() throws IOException { - DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); + DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); provider.put(dictionary1); VarCharVector vector = newVarCharVector("varchar", allocator); @@ -425,16 +450,17 @@ public void testEmptyStreamInFileIPC() throws IOException { List fields = Arrays.asList(encodedVector1A.getField()); List vectors = Collections2.asImmutableList(encodedVector1A); - try (VectorSchemaRoot root = new VectorSchemaRoot(fields, vectors, encodedVector1A.getValueCount()); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ArrowFileWriter writer = new ArrowFileWriter(root, provider, newChannel(out))) { + try (VectorSchemaRoot root = + new VectorSchemaRoot(fields, vectors, encodedVector1A.getValueCount()); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ArrowFileWriter writer = new ArrowFileWriter(root, provider, newChannel(out))) { writer.start(); writer.end(); - try (SeekableReadChannel channel = new SeekableReadChannel( - new ByteArrayReadableSeekableByteChannel(out.toByteArray())); - ArrowFileReader reader = new ArrowFileReader(channel, allocator)) { + try (SeekableReadChannel channel = + new SeekableReadChannel(new ByteArrayReadableSeekableByteChannel(out.toByteArray())); + ArrowFileReader reader = new ArrowFileReader(channel, allocator)) { Schema readSchema = reader.getVectorSchemaRoot().getSchema(); assertEquals(root.getSchema(), readSchema); assertEquals(1, reader.getDictionaryVectors().size()); @@ -442,13 +468,13 @@ public void testEmptyStreamInFileIPC() throws IOException { assertEquals(0, reader.getRecordBlocks().size()); } } - } @Test public void testEmptyStreamInStreamingIPC() throws IOException { - DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); + DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); provider.put(dictionary1); VarCharVector vector = newVarCharVector("varchar", allocator); @@ -465,29 +491,30 @@ public void testEmptyStreamInStreamingIPC() throws IOException { List fields = Arrays.asList(encodedVector.getField()); try (VectorSchemaRoot root = - new VectorSchemaRoot(fields, Arrays.asList(encodedVector), encodedVector.getValueCount()); + new VectorSchemaRoot( + fields, Arrays.asList(encodedVector), encodedVector.getValueCount()); ByteArrayOutputStream out = new ByteArrayOutputStream(); ArrowStreamWriter writer = new ArrowStreamWriter(root, provider, newChannel(out))) { writer.start(); writer.end(); - - try (ArrowStreamReader reader = new ArrowStreamReader( - new ByteArrayReadableSeekableByteChannel(out.toByteArray()), allocator)) { + try (ArrowStreamReader reader = + new ArrowStreamReader( + new ByteArrayReadableSeekableByteChannel(out.toByteArray()), allocator)) { Schema readSchema = reader.getVectorSchemaRoot().getSchema(); assertEquals(root.getSchema(), readSchema); assertEquals(1, reader.getDictionaryVectors().size()); assertFalse(reader.loadNextBatch()); } } - } @Test public void testDictionaryReplacement() throws Exception { VarCharVector vector1 = newVarCharVector("varchar1", allocator); - setVector(vector1, + setVector( + vector1, "foo".getBytes(StandardCharsets.UTF_8), "bar".getBytes(StandardCharsets.UTF_8), "baz".getBytes(StandardCharsets.UTF_8), @@ -496,7 +523,8 @@ public void testDictionaryReplacement() throws Exception { FieldVector encodedVector1 = (FieldVector) DictionaryEncoder.encode(vector1, dictionary1); VarCharVector vector2 = newVarCharVector("varchar2", allocator); - setVector(vector2, + setVector( + vector2, "foo".getBytes(StandardCharsets.UTF_8), "foo".getBytes(StandardCharsets.UTF_8), "foo".getBytes(StandardCharsets.UTF_8), @@ -504,11 +532,14 @@ public void testDictionaryReplacement() throws Exception { FieldVector encodedVector2 = (FieldVector) DictionaryEncoder.encode(vector2, dictionary1); - DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); + DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); provider.put(dictionary1); List schemaFields = new ArrayList<>(); - schemaFields.add(DictionaryUtility.toMessageFormat(encodedVector1.getField(), provider, new HashSet<>())); - schemaFields.add(DictionaryUtility.toMessageFormat(encodedVector2.getField(), provider, new HashSet<>())); + schemaFields.add( + DictionaryUtility.toMessageFormat(encodedVector1.getField(), provider, new HashSet<>())); + schemaFields.add( + DictionaryUtility.toMessageFormat(encodedVector2.getField(), provider, new HashSet<>())); Schema schema = new Schema(schemaFields); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); @@ -531,8 +562,9 @@ public void testDictionaryReplacement() throws Exception { // write eos out.writeIntLittleEndian(0); - try (ArrowStreamReader reader = new ArrowStreamReader( - new ByteArrayReadableSeekableByteChannel(outStream.toByteArray()), allocator)) { + try (ArrowStreamReader reader = + new ArrowStreamReader( + new ByteArrayReadableSeekableByteChannel(outStream.toByteArray()), allocator)) { assertEquals(1, reader.getDictionaryVectors().size()); assertTrue(reader.loadNextBatch()); FieldVector dictionaryVector = reader.getDictionaryVectors().get(1L).getVector(); @@ -549,7 +581,8 @@ public void testDictionaryReplacement() throws Exception { @Test public void testDeltaDictionary() throws Exception { VarCharVector vector1 = newVarCharVector("varchar1", allocator); - setVector(vector1, + setVector( + vector1, "foo".getBytes(StandardCharsets.UTF_8), "bar".getBytes(StandardCharsets.UTF_8), "baz".getBytes(StandardCharsets.UTF_8), @@ -558,7 +591,8 @@ public void testDeltaDictionary() throws Exception { FieldVector encodedVector1 = (FieldVector) DictionaryEncoder.encode(vector1, dictionary1); VarCharVector vector2 = newVarCharVector("varchar2", allocator); - setVector(vector2, + setVector( + vector2, "foo".getBytes(StandardCharsets.UTF_8), "aa".getBytes(StandardCharsets.UTF_8), "bb".getBytes(StandardCharsets.UTF_8), @@ -566,12 +600,15 @@ public void testDeltaDictionary() throws Exception { FieldVector encodedVector2 = (FieldVector) DictionaryEncoder.encode(vector2, dictionary3); - DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); + DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); provider.put(dictionary1); provider.put(dictionary3); List schemaFields = new ArrayList<>(); - schemaFields.add(DictionaryUtility.toMessageFormat(encodedVector1.getField(), provider, new HashSet<>())); - schemaFields.add(DictionaryUtility.toMessageFormat(encodedVector2.getField(), provider, new HashSet<>())); + schemaFields.add( + DictionaryUtility.toMessageFormat(encodedVector1.getField(), provider, new HashSet<>())); + schemaFields.add( + DictionaryUtility.toMessageFormat(encodedVector2.getField(), provider, new HashSet<>())); Schema schema = new Schema(schemaFields); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); @@ -597,8 +634,9 @@ public void testDeltaDictionary() throws Exception { // write eos out.writeIntLittleEndian(0); - try (ArrowStreamReader reader = new ArrowStreamReader( - new ByteArrayReadableSeekableByteChannel(outStream.toByteArray()), allocator)) { + try (ArrowStreamReader reader = + new ArrowStreamReader( + new ByteArrayReadableSeekableByteChannel(outStream.toByteArray()), allocator)) { assertEquals(1, reader.getDictionaryVectors().size()); assertTrue(reader.loadNextBatch()); FieldVector dictionaryVector = reader.getDictionaryVectors().get(1L).getVector(); @@ -610,13 +648,13 @@ public void testDeltaDictionary() throws Exception { vector1.close(); vector2.close(); AutoCloseables.close(closeableList); - } // Tests that the ArrowStreamWriter re-emits dictionaries when they change @Test public void testWriteReadStreamWithDictionaryReplacement() throws Exception { - DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); + DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); provider.put(dictionary1); String[] batch0 = {"foo", "bar", "baz", "bar", "baz"}; @@ -633,8 +671,9 @@ public void testWriteReadStreamWithDictionaryReplacement() throws Exception { List fields = Arrays.asList(encodedVector1.getField()); try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { try (VectorSchemaRoot root = - new VectorSchemaRoot(fields, Arrays.asList(encodedVector1), encodedVector1.getValueCount()); - ArrowStreamWriter writer = new ArrowStreamWriter(root, provider, newChannel(out))) { + new VectorSchemaRoot( + fields, Arrays.asList(encodedVector1), encodedVector1.getValueCount()); + ArrowStreamWriter writer = new ArrowStreamWriter(root, provider, newChannel(out))) { writer.start(); // Write batch with initial data and dictionary @@ -660,8 +699,9 @@ public void testWriteReadStreamWithDictionaryReplacement() throws Exception { writer.end(); } - try (ArrowStreamReader reader = new ArrowStreamReader( - new ByteArrayReadableSeekableByteChannel(out.toByteArray()), allocator)) { + try (ArrowStreamReader reader = + new ArrowStreamReader( + new ByteArrayReadableSeekableByteChannel(out.toByteArray()), allocator)) { VectorSchemaRoot root = reader.getVectorSchemaRoot(); // Read and verify first batch @@ -670,7 +710,7 @@ public void testWriteReadStreamWithDictionaryReplacement() throws Exception { FieldVector readEncoded1 = root.getVector(0); long dictionaryId = readEncoded1.getField().getDictionary().getId(); try (VarCharVector decodedValues = - (VarCharVector) DictionaryEncoder.decode(readEncoded1, reader.lookup(dictionaryId))) { + (VarCharVector) DictionaryEncoder.decode(readEncoded1, reader.lookup(dictionaryId))) { for (int i = 0; i < batch0.length; ++i) { assertEquals(batch0[i], new String(decodedValues.get(i), StandardCharsets.UTF_8)); } @@ -682,7 +722,7 @@ public void testWriteReadStreamWithDictionaryReplacement() throws Exception { FieldVector readEncoded2 = root.getVector(0); dictionaryId = readEncoded2.getField().getDictionary().getId(); try (VarCharVector decodedValues = - (VarCharVector) DictionaryEncoder.decode(readEncoded2, reader.lookup(dictionaryId))) { + (VarCharVector) DictionaryEncoder.decode(readEncoded2, reader.lookup(dictionaryId))) { for (int i = 0; i < batch1.length; ++i) { assertEquals(batch1[i], new String(decodedValues.get(i), StandardCharsets.UTF_8)); } @@ -696,33 +736,29 @@ public void testWriteReadStreamWithDictionaryReplacement() throws Exception { } private void serializeDictionaryBatch( - WriteChannel out, - Dictionary dictionary, - boolean isDelta, - List closeables) throws IOException { + WriteChannel out, Dictionary dictionary, boolean isDelta, List closeables) + throws IOException { FieldVector dictVector = dictionary.getVector(); - VectorSchemaRoot root = new VectorSchemaRoot( - Collections.singletonList(dictVector.getField()), - Collections.singletonList(dictVector), - dictVector.getValueCount()); + VectorSchemaRoot root = + new VectorSchemaRoot( + Collections.singletonList(dictVector.getField()), + Collections.singletonList(dictVector), + dictVector.getValueCount()); ArrowDictionaryBatch batch = - new ArrowDictionaryBatch(dictionary.getEncoding().getId(), new VectorUnloader(root).getRecordBatch(), isDelta); + new ArrowDictionaryBatch( + dictionary.getEncoding().getId(), new VectorUnloader(root).getRecordBatch(), isDelta); MessageSerializer.serialize(out, batch); closeables.add(batch); closeables.add(root); } private void serializeRecordBatch( - WriteChannel out, - List vectors, - List closeables) throws IOException { + WriteChannel out, List vectors, List closeables) + throws IOException { List fields = vectors.stream().map(v -> v.getField()).collect(Collectors.toList()); - VectorSchemaRoot root = new VectorSchemaRoot( - fields, - vectors, - vectors.get(0).getValueCount()); + VectorSchemaRoot root = new VectorSchemaRoot(fields, vectors, vectors.get(0).getValueCount()); VectorUnloader unloader = new VectorUnloader(root); ArrowRecordBatch batch = unloader.getRecordBatch(); MessageSerializer.serialize(out, batch); @@ -742,10 +778,11 @@ public void testReadInterleavedData() throws IOException { // write dictionary1 FieldVector dictVector1 = dictionary1.getVector(); - VectorSchemaRoot dictRoot1 = new VectorSchemaRoot( - Collections.singletonList(dictVector1.getField()), - Collections.singletonList(dictVector1), - dictVector1.getValueCount()); + VectorSchemaRoot dictRoot1 = + new VectorSchemaRoot( + Collections.singletonList(dictVector1.getField()), + Collections.singletonList(dictVector1), + dictVector1.getValueCount()); ArrowDictionaryBatch dictionaryBatch1 = new ArrowDictionaryBatch(1, new VectorUnloader(dictRoot1).getRecordBatch()); MessageSerializer.serialize(out, dictionaryBatch1); @@ -757,10 +794,11 @@ public void testReadInterleavedData() throws IOException { // write dictionary2 FieldVector dictVector2 = dictionary2.getVector(); - VectorSchemaRoot dictRoot2 = new VectorSchemaRoot( - Collections.singletonList(dictVector2.getField()), - Collections.singletonList(dictVector2), - dictVector2.getValueCount()); + VectorSchemaRoot dictRoot2 = + new VectorSchemaRoot( + Collections.singletonList(dictVector2.getField()), + Collections.singletonList(dictVector2), + dictVector2.getValueCount()); ArrowDictionaryBatch dictionaryBatch2 = new ArrowDictionaryBatch(2, new VectorUnloader(dictRoot2).getRecordBatch()); MessageSerializer.serialize(out, dictionaryBatch2); @@ -773,8 +811,9 @@ public void testReadInterleavedData() throws IOException { // write eos out.writeIntLittleEndian(0); - try (ArrowStreamReader reader = new ArrowStreamReader( - new ByteArrayReadableSeekableByteChannel(outStream.toByteArray()), allocator)) { + try (ArrowStreamReader reader = + new ArrowStreamReader( + new ByteArrayReadableSeekableByteChannel(outStream.toByteArray()), allocator)) { Schema readSchema = reader.getVectorSchemaRoot().getSchema(); assertEquals(encodedSchema, readSchema); assertEquals(2, reader.getDictionaryVectors().size()); @@ -789,7 +828,8 @@ public void testReadInterleavedData() throws IOException { private List createRecordBatches() { List batches = new ArrayList<>(); - DictionaryProvider.MapDictionaryProvider provider = new DictionaryProvider.MapDictionaryProvider(); + DictionaryProvider.MapDictionaryProvider provider = + new DictionaryProvider.MapDictionaryProvider(); provider.put(dictionary1); provider.put(dictionary2); @@ -841,11 +881,14 @@ private List createRecordBatches() { rootB.close(); List schemaFields = new ArrayList<>(); - schemaFields.add(DictionaryUtility.toMessageFormat(encodedVectorA1.getField(), provider, new HashSet<>())); - schemaFields.add(DictionaryUtility.toMessageFormat(encodedVectorA2.getField(), provider, new HashSet<>())); + schemaFields.add( + DictionaryUtility.toMessageFormat(encodedVectorA1.getField(), provider, new HashSet<>())); + schemaFields.add( + DictionaryUtility.toMessageFormat(encodedVectorA2.getField(), provider, new HashSet<>())); schema = new Schema(schemaFields); - encodedSchema = new Schema(Arrays.asList(encodedVectorA1.getField(), encodedVectorA2.getField())); + encodedSchema = + new Schema(Arrays.asList(encodedVectorA1.getField(), encodedVectorA2.getField())); return batches; } @@ -858,8 +901,11 @@ public void testLegacyIpcBackwardsCompatibility() throws Exception { vector.setValueCount(valueCount); vector.setSafe(0, 1); vector.setSafe(1, 2); - ArrowRecordBatch batch = new ArrowRecordBatch(valueCount, asList(new ArrowFieldNode(valueCount, 0)), - asList(vector.getValidityBuffer(), vector.getDataBuffer())); + ArrowRecordBatch batch = + new ArrowRecordBatch( + valueCount, + asList(new ArrowFieldNode(valueCount, 0)), + asList(vector.getValidityBuffer(), vector.getDataBuffer())); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); WriteChannel out = new WriteChannel(newChannel(outStream)); @@ -882,7 +928,8 @@ public void testLegacyIpcBackwardsCompatibility() throws Exception { MessageSerializer.serialize(out, schema, option); MessageSerializer.serialize(out, batch); - ReadChannel in2 = new ReadChannel(newChannel(new ByteArrayInputStream(outStream.toByteArray()))); + ReadChannel in2 = + new ReadChannel(newChannel(new ByteArrayInputStream(outStream.toByteArray()))); Schema readSchema2 = MessageSerializer.deserializeSchema(in2); assertEquals(schema, readSchema2); ArrowRecordBatch readBatch2 = MessageSerializer.deserializeRecordBatch(in2, allocator); @@ -900,8 +947,9 @@ public void testChannelReadFully() throws IOException { buf.putInt(200); buf.rewind(); - try (ReadChannel channel = new ReadChannel(Channels.newChannel(new ByteArrayInputStream(buf.array()))); - ArrowBuf arrBuf = allocator.buffer(8)) { + try (ReadChannel channel = + new ReadChannel(Channels.newChannel(new ByteArrayInputStream(buf.array()))); + ArrowBuf arrBuf = allocator.buffer(8)) { arrBuf.setInt(0, 100); arrBuf.writerIndex(4); assertEquals(4, arrBuf.writerIndex()); @@ -921,8 +969,9 @@ public void testChannelReadFullyEos() throws IOException { buf.putInt(10); buf.rewind(); - try (ReadChannel channel = new ReadChannel(Channels.newChannel(new ByteArrayInputStream(buf.array()))); - ArrowBuf arrBuf = allocator.buffer(8)) { + try (ReadChannel channel = + new ReadChannel(Channels.newChannel(new ByteArrayInputStream(buf.array()))); + ArrowBuf arrBuf = allocator.buffer(8)) { int n = channel.readFully(arrBuf.nioBuffer(0, 8)); assertEquals(4, n); @@ -946,13 +995,13 @@ public void testCustomMetaData() throws IOException { metadata.put("key2", "value2"); try (VectorSchemaRoot root = new VectorSchemaRoot(fields, vectors, vector.getValueCount()); ByteArrayOutputStream out = new ByteArrayOutputStream(); - ArrowFileWriter writer = new ArrowFileWriter(root, null, newChannel(out), metadata);) { + ArrowFileWriter writer = new ArrowFileWriter(root, null, newChannel(out), metadata); ) { writer.start(); writer.end(); - try (SeekableReadChannel channel = new SeekableReadChannel( - new ByteArrayReadableSeekableByteChannel(out.toByteArray())); + try (SeekableReadChannel channel = + new SeekableReadChannel(new ByteArrayReadableSeekableByteChannel(out.toByteArray())); ArrowFileReader reader = new ArrowFileReader(channel, allocator)) { reader.getVectorSchemaRoot(); @@ -965,9 +1014,8 @@ public void testCustomMetaData() throws IOException { } /** - * This test case covers the case for which the footer size is extremely large - * (much larger than the file size). - * Due to integer overflow, our implementation fails detect the problem, which + * This test case covers the case for which the footer size is extremely large (much larger than + * the file size). Due to integer overflow, our implementation fails detect the problem, which * leads to extremely large memory allocation and eventually causing an OutOfMemoryError. */ @Test @@ -980,18 +1028,22 @@ public void testFileFooterSizeOverflow() { System.arraycopy(magicBytes, 0, data, 0, ArrowMagic.MAGIC_LENGTH); int footerLength = Integer.MAX_VALUE; byte[] footerLengthBytes = - ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(footerLength).array(); + ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(footerLength).array(); int footerOffset = data.length - ArrowMagic.MAGIC_LENGTH - 4; System.arraycopy(footerLengthBytes, 0, data, footerOffset, 4); System.arraycopy(magicBytes, 0, data, footerOffset + 4, ArrowMagic.MAGIC_LENGTH); // test file reader - InvalidArrowFileException e = Assertions.assertThrows(InvalidArrowFileException.class, () -> { - try (SeekableReadChannel channel = new SeekableReadChannel(new ByteArrayReadableSeekableByteChannel(data)); - ArrowFileReader reader = new ArrowFileReader(channel, allocator)) { - reader.getVectorSchemaRoot().getSchema(); - } - }); + InvalidArrowFileException e = + Assertions.assertThrows( + InvalidArrowFileException.class, + () -> { + try (SeekableReadChannel channel = + new SeekableReadChannel(new ByteArrayReadableSeekableByteChannel(data)); + ArrowFileReader reader = new ArrowFileReader(channel, allocator)) { + reader.getVectorSchemaRoot().getSchema(); + } + }); assertEquals("invalid footer length: " + footerLength, e.getMessage()); } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowStream.java b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowStream.java index 145bdd588e9..4f2bf025272 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowStream.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowStream.java @@ -26,7 +26,6 @@ import java.io.IOException; import java.nio.channels.Channels; import java.util.Collections; - import org.apache.arrow.vector.IntVector; import org.apache.arrow.vector.TinyIntVector; import org.apache.arrow.vector.VectorSchemaRoot; @@ -61,11 +60,12 @@ public void testEmptyStream() throws IOException { public void testStreamZeroLengthBatch() throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); - try (IntVector vector = new IntVector("foo", allocator);) { + try (IntVector vector = new IntVector("foo", allocator); ) { Schema schema = new Schema(Collections.singletonList(vector.getField())); try (VectorSchemaRoot root = - new VectorSchemaRoot(schema, Collections.singletonList(vector), vector.getValueCount()); - ArrowStreamWriter writer = new ArrowStreamWriter(root, null, Channels.newChannel(os));) { + new VectorSchemaRoot( + schema, Collections.singletonList(vector), vector.getValueCount()); + ArrowStreamWriter writer = new ArrowStreamWriter(root, null, Channels.newChannel(os)); ) { vector.setValueCount(0); root.setRowCount(0); writer.writeBatch(); @@ -75,7 +75,7 @@ public void testStreamZeroLengthBatch() throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(os.toByteArray()); - try (ArrowStreamReader reader = new ArrowStreamReader(in, allocator);) { + try (ArrowStreamReader reader = new ArrowStreamReader(in, allocator); ) { VectorSchemaRoot root = reader.getVectorSchemaRoot(); IntVector vector = (IntVector) root.getFieldVectors().get(0); reader.loadNextBatch(); @@ -128,18 +128,19 @@ public void testReadWrite() throws IOException { public void testReadWriteMultipleBatches() throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); - try (IntVector vector = new IntVector("foo", allocator);) { + try (IntVector vector = new IntVector("foo", allocator); ) { Schema schema = new Schema(Collections.singletonList(vector.getField())); try (VectorSchemaRoot root = - new VectorSchemaRoot(schema, Collections.singletonList(vector), vector.getValueCount()); - ArrowStreamWriter writer = new ArrowStreamWriter(root, null, Channels.newChannel(os));) { + new VectorSchemaRoot( + schema, Collections.singletonList(vector), vector.getValueCount()); + ArrowStreamWriter writer = new ArrowStreamWriter(root, null, Channels.newChannel(os)); ) { writeBatchData(writer, vector, root); } } ByteArrayInputStream in = new ByteArrayInputStream(os.toByteArray()); - try (ArrowStreamReader reader = new ArrowStreamReader(in, allocator);) { + try (ArrowStreamReader reader = new ArrowStreamReader(in, allocator); ) { IntVector vector = (IntVector) reader.getVectorSchemaRoot().getFieldVectors().get(0); validateBatchData(reader, vector); } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowStreamPipe.java b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowStreamPipe.java index 422a63f57f7..018b04ea5bb 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowStreamPipe.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowStreamPipe.java @@ -24,14 +24,10 @@ import java.nio.channels.Pipe; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.TinyIntVector; import org.apache.arrow.vector.VectorSchemaRoot; -import org.apache.arrow.vector.ipc.ArrowStreamReader; -import org.apache.arrow.vector.ipc.ArrowStreamWriter; -import org.apache.arrow.vector.ipc.MessageSerializerTest; import org.apache.arrow.vector.types.pojo.Schema; import org.junit.Assert; import org.junit.Test; @@ -46,8 +42,7 @@ private final class WriterThread extends Thread { private final ArrowStreamWriter writer; private final VectorSchemaRoot root; - public WriterThread(int numBatches, WritableByteChannel sinkChannel) - throws IOException { + public WriterThread(int numBatches, WritableByteChannel sinkChannel) throws IOException { this.numBatches = numBatches; BufferAllocator allocator = alloc.newChildAllocator("writer thread", 0, Integer.MAX_VALUE); root = VectorSchemaRoot.create(schema, allocator); @@ -90,33 +85,33 @@ private final class ReaderThread extends Thread { private final BufferAllocator alloc = new RootAllocator(Long.MAX_VALUE); private boolean done = false; - public ReaderThread(ReadableByteChannel sourceChannel) - throws IOException { - reader = new ArrowStreamReader(sourceChannel, alloc) { - - @Override - public boolean loadNextBatch() throws IOException { - if (super.loadNextBatch()) { - batchesRead++; - } else { - done = true; - return false; - } - VectorSchemaRoot root = getVectorSchemaRoot(); - Assert.assertEquals(16, root.getRowCount()); - TinyIntVector vector = (TinyIntVector) root.getFieldVectors().get(0); - Assert.assertEquals((byte) (batchesRead - 1), vector.get(0)); - for (int i = 1; i < 16; i++) { - if (i < 8) { - Assert.assertEquals((byte) (i + 1), vector.get(i)); - } else { - Assert.assertTrue(vector.isNull(i)); + public ReaderThread(ReadableByteChannel sourceChannel) throws IOException { + reader = + new ArrowStreamReader(sourceChannel, alloc) { + + @Override + public boolean loadNextBatch() throws IOException { + if (super.loadNextBatch()) { + batchesRead++; + } else { + done = true; + return false; + } + VectorSchemaRoot root = getVectorSchemaRoot(); + Assert.assertEquals(16, root.getRowCount()); + TinyIntVector vector = (TinyIntVector) root.getFieldVectors().get(0); + Assert.assertEquals((byte) (batchesRead - 1), vector.get(0)); + for (int i = 1; i < 16; i++) { + if (i < 8) { + Assert.assertEquals((byte) (i + 1), vector.get(i)); + } else { + Assert.assertTrue(vector.isNull(i)); + } + } + + return true; } - } - - return true; - } - }; + }; } @Override diff --git a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestJSONFile.java b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestJSONFile.java index bd5bd4feabb..38f3cb63543 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestJSONFile.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestJSONFile.java @@ -28,7 +28,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.VectorSchemaRoot; @@ -56,8 +55,8 @@ public void testNoBatches() throws IOException { File file = new File("target/no_batches.json"); try (BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); - StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); + StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { BaseWriter.ComplexWriter writer = new ComplexWriterImpl("root", parent); BaseWriter.StructWriter rootWriter = writer.rootAsStruct(); rootWriter.integer("int"); @@ -70,10 +69,9 @@ public void testNoBatches() throws IOException { } // read - try ( - BufferAllocator readerAllocator = allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); - JsonFileReader reader = new JsonFileReader(file, readerAllocator) - ) { + try (BufferAllocator readerAllocator = + allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); + JsonFileReader reader = new JsonFileReader(file, readerAllocator)) { Schema schema = reader.start(); LOGGER.debug("reading schema: " + schema); } @@ -86,22 +84,21 @@ public void testWriteRead() throws IOException { // write try (BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); - StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); + StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { writeData(count, parent); writeJSON(file, new VectorSchemaRoot(parent.getChild("root")), null); } // read - try ( - BufferAllocator readerAllocator = allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); - JsonFileReader reader = new JsonFileReader(file, readerAllocator) - ) { + try (BufferAllocator readerAllocator = + allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); + JsonFileReader reader = new JsonFileReader(file, readerAllocator)) { Schema schema = reader.start(); LOGGER.debug("reading schema: " + schema); // initialize vectors - try (VectorSchemaRoot root = reader.read();) { + try (VectorSchemaRoot root = reader.read(); ) { validateContent(count, root); } } @@ -113,23 +110,22 @@ public void testWriteReadComplexJSON() throws IOException { int count = COUNT; // write - try ( - BufferAllocator originalVectorAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); + try (BufferAllocator originalVectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { writeComplexData(count, parent); writeJSON(file, new VectorSchemaRoot(parent.getChild("root")), null); } // read - try ( - BufferAllocator readerAllocator = allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); - JsonFileReader reader = new JsonFileReader(file, readerAllocator); - ) { + try (BufferAllocator readerAllocator = + allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); + JsonFileReader reader = new JsonFileReader(file, readerAllocator); ) { Schema schema = reader.start(); LOGGER.debug("reading schema: " + schema); // initialize vectors - try (VectorSchemaRoot root = reader.read();) { + try (VectorSchemaRoot root = reader.read(); ) { validateComplexContent(count, root); } } @@ -139,8 +135,8 @@ public void testWriteReadComplexJSON() throws IOException { public void testWriteComplexJSON() throws IOException { File file = new File("target/mytest_write_complex.json"); int count = COUNT; - try ( - BufferAllocator vectorAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); + try (BufferAllocator vectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); StructVector parent = StructVector.empty("parent", vectorAllocator)) { writeComplexData(count, parent); VectorSchemaRoot root = new VectorSchemaRoot(parent.getChild("root")); @@ -149,20 +145,20 @@ public void testWriteComplexJSON() throws IOException { } } - public void writeJSON(File file, VectorSchemaRoot root, DictionaryProvider provider) throws IOException { + public void writeJSON(File file, VectorSchemaRoot root, DictionaryProvider provider) + throws IOException { JsonFileWriter writer = new JsonFileWriter(file, JsonFileWriter.config().pretty(true)); writer.start(root.getSchema(), provider); writer.write(root); writer.close(); } - @Test public void testWriteReadUnionJSON() throws IOException { File file = new File("target/mytest_write_union.json"); int count = COUNT; - try ( - BufferAllocator vectorAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); + try (BufferAllocator vectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); StructVector parent = StructVector.empty("parent", vectorAllocator)) { writeUnionData(count, parent); printVectors(parent.getChildrenFromFields()); @@ -172,13 +168,14 @@ public void testWriteReadUnionJSON() throws IOException { writeJSON(file, root, null); // read - try (BufferAllocator readerAllocator = allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE)) { + try (BufferAllocator readerAllocator = + allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE)) { JsonFileReader reader = new JsonFileReader(file, readerAllocator); Schema schema = reader.start(); LOGGER.debug("reading schema: " + schema); - try (VectorSchemaRoot rootFromJson = reader.read();) { + try (VectorSchemaRoot rootFromJson = reader.read(); ) { validateUnionData(count, rootFromJson); Validator.compareVectorSchemaRoot(root, rootFromJson); } @@ -193,8 +190,8 @@ public void testWriteReadDateTimeJSON() throws IOException { int count = COUNT; // write - try ( - BufferAllocator vectorAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); + try (BufferAllocator vectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); StructVector parent = StructVector.empty("parent", vectorAllocator)) { writeDateTimeData(count, parent); @@ -208,15 +205,14 @@ public void testWriteReadDateTimeJSON() throws IOException { } // read - try ( - BufferAllocator readerAllocator = allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); - JsonFileReader reader = new JsonFileReader(file, readerAllocator) - ) { + try (BufferAllocator readerAllocator = + allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); + JsonFileReader reader = new JsonFileReader(file, readerAllocator)) { Schema schema = reader.start(); LOGGER.debug("reading schema: " + schema); // initialize vectors - try (VectorSchemaRoot root = reader.read();) { + try (VectorSchemaRoot root = reader.read(); ) { validateDateTimeContent(count, root); } } @@ -227,9 +223,8 @@ public void testWriteReadDictionaryJSON() throws IOException { File file = new File("target/mytest_dictionary.json"); // write - try ( - BufferAllocator vectorAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE) - ) { + try (BufferAllocator vectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE)) { MapDictionaryProvider provider = new MapDictionaryProvider(); try (VectorSchemaRoot root = writeFlatDictionaryData(vectorAllocator, provider)) { @@ -245,15 +240,14 @@ public void testWriteReadDictionaryJSON() throws IOException { } // read - try ( - BufferAllocator readerAllocator = allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); - JsonFileReader reader = new JsonFileReader(file, readerAllocator) - ) { + try (BufferAllocator readerAllocator = + allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); + JsonFileReader reader = new JsonFileReader(file, readerAllocator)) { Schema schema = reader.start(); LOGGER.debug("reading schema: " + schema); // initialize vectors - try (VectorSchemaRoot root = reader.read();) { + try (VectorSchemaRoot root = reader.read(); ) { validateFlatDictionary(root, reader); } } @@ -267,9 +261,8 @@ public void testWriteReadNestedDictionaryJSON() throws IOException { // [['foo', 'bar'], ['foo'], ['bar']] -> [[0, 1], [0], [1]] // write - try ( - BufferAllocator vectorAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE) - ) { + try (BufferAllocator vectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE)) { MapDictionaryProvider provider = new MapDictionaryProvider(); try (VectorSchemaRoot root = writeNestedDictionaryData(vectorAllocator, provider)) { @@ -285,15 +278,14 @@ public void testWriteReadNestedDictionaryJSON() throws IOException { } // read - try ( - BufferAllocator readerAllocator = allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); - JsonFileReader reader = new JsonFileReader(file, readerAllocator) - ) { + try (BufferAllocator readerAllocator = + allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); + JsonFileReader reader = new JsonFileReader(file, readerAllocator)) { Schema schema = reader.start(); LOGGER.debug("reading schema: " + schema); // initialize vectors - try (VectorSchemaRoot root = reader.read();) { + try (VectorSchemaRoot root = reader.read(); ) { validateNestedDictionary(root, reader); } } @@ -304,7 +296,8 @@ public void testWriteReadDecimalJSON() throws IOException { File file = new File("target/mytest_decimal.json"); // write - try (BufferAllocator vectorAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); + try (BufferAllocator vectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); VectorSchemaRoot root = writeDecimalData(vectorAllocator)) { printVectors(root.getFieldVectors()); validateDecimalData(root); @@ -312,15 +305,14 @@ public void testWriteReadDecimalJSON() throws IOException { } // read - try ( - BufferAllocator readerAllocator = allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); - JsonFileReader reader = new JsonFileReader(file, readerAllocator) - ) { + try (BufferAllocator readerAllocator = + allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); + JsonFileReader reader = new JsonFileReader(file, readerAllocator)) { Schema schema = reader.start(); LOGGER.debug("reading schema: " + schema); // initialize vectors - try (VectorSchemaRoot root = reader.read();) { + try (VectorSchemaRoot root = reader.read(); ) { validateDecimalData(root); } } @@ -332,15 +324,14 @@ public void testSetStructLength() throws IOException { if (!file.exists()) { file = new File("../docs/source/format/integration_json_examples/struct.json"); } - try ( - BufferAllocator readerAllocator = allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); - JsonFileReader reader = new JsonFileReader(file, readerAllocator) - ) { + try (BufferAllocator readerAllocator = + allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); + JsonFileReader reader = new JsonFileReader(file, readerAllocator)) { Schema schema = reader.start(); LOGGER.debug("reading schema: " + schema); // initialize vectors - try (VectorSchemaRoot root = reader.read();) { + try (VectorSchemaRoot root = reader.read(); ) { FieldVector vector = root.getVector("struct_nullable"); Assert.assertEquals(7, vector.getValueCount()); } @@ -353,8 +344,8 @@ public void testWriteReadVarBinJSON() throws IOException { int count = COUNT; // write - try ( - BufferAllocator vectorAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); + try (BufferAllocator vectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); StructVector parent = StructVector.empty("parent", vectorAllocator)) { writeVarBinaryData(count, parent); VectorSchemaRoot root = new VectorSchemaRoot(parent.getChild("root")); @@ -363,13 +354,14 @@ public void testWriteReadVarBinJSON() throws IOException { } // read - try (BufferAllocator readerAllocator = allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); + try (BufferAllocator readerAllocator = + allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); JsonFileReader reader = new JsonFileReader(file, readerAllocator)) { Schema schema = reader.start(); LOGGER.debug("reading schema: " + schema); // initialize vectors - try (VectorSchemaRoot root = reader.read();) { + try (VectorSchemaRoot root = reader.read(); ) { validateVarBinary(count, root); } } @@ -380,7 +372,8 @@ public void testWriteReadMapJSON() throws IOException { File file = new File("target/mytest_map.json"); // write - try (BufferAllocator vectorAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); + try (BufferAllocator vectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); VectorSchemaRoot root = writeMapData(vectorAllocator)) { printVectors(root.getFieldVectors()); validateMapData(root); @@ -388,13 +381,14 @@ public void testWriteReadMapJSON() throws IOException { } // read - try (BufferAllocator readerAllocator = allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); + try (BufferAllocator readerAllocator = + allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); JsonFileReader reader = new JsonFileReader(file, readerAllocator)) { Schema schema = reader.start(); LOGGER.debug("reading schema: " + schema); // initialize vectors - try (VectorSchemaRoot root = reader.read();) { + try (VectorSchemaRoot root = reader.read(); ) { validateMapData(root); } } @@ -406,7 +400,8 @@ public void testWriteReadNullJSON() throws IOException { int valueCount = 10; // write - try (BufferAllocator vectorAllocator = allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); + try (BufferAllocator vectorAllocator = + allocator.newChildAllocator("original vectors", 0, Integer.MAX_VALUE); VectorSchemaRoot root = writeNullData(valueCount)) { printVectors(root.getFieldVectors()); validateNullData(root, valueCount); @@ -414,16 +409,15 @@ public void testWriteReadNullJSON() throws IOException { } // read - try ( - BufferAllocator readerAllocator = allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); - JsonFileReader reader = new JsonFileReader(file, readerAllocator) - ) { + try (BufferAllocator readerAllocator = + allocator.newChildAllocator("reader", 0, Integer.MAX_VALUE); + JsonFileReader reader = new JsonFileReader(file, readerAllocator)) { Schema schema = reader.start(); LOGGER.debug("reading schema: " + schema); // initialize vectors - try (VectorSchemaRoot root = reader.read();) { + try (VectorSchemaRoot root = reader.read(); ) { validateNullData(root, valueCount); } } @@ -432,29 +426,45 @@ public void testWriteReadNullJSON() throws IOException { /** Regression test for ARROW-17107. */ @Test public void testRoundtripEmptyVector() throws Exception { - final List fields = Arrays.asList( - Field.nullable("utf8", ArrowType.Utf8.INSTANCE), - Field.nullable("largeutf8", ArrowType.LargeUtf8.INSTANCE), - Field.nullable("binary", ArrowType.Binary.INSTANCE), - Field.nullable("largebinary", ArrowType.LargeBinary.INSTANCE), - Field.nullable("fixedsizebinary", new ArrowType.FixedSizeBinary(2)), - Field.nullable("decimal128", new ArrowType.Decimal(3, 2, 128)), - Field.nullable("decimal128", new ArrowType.Decimal(3, 2, 256)), - new Field("list", FieldType.nullable(ArrowType.List.INSTANCE), - Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))), - new Field("largelist", FieldType.nullable(ArrowType.LargeList.INSTANCE), - Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))), - new Field("map", FieldType.nullable(new ArrowType.Map(/*keyssorted*/ false)), - Collections.singletonList(new Field("items", FieldType.notNullable(ArrowType.Struct.INSTANCE), - Arrays.asList(Field.notNullable("keys", new ArrowType.Int(32, true)), - Field.nullable("values", new ArrowType.Int(32, true)))))), - new Field("fixedsizelist", FieldType.nullable(new ArrowType.FixedSizeList(2)), - Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))), - new Field("denseunion", FieldType.nullable(new ArrowType.Union(UnionMode.Dense, new int[] {0})), - Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))), - new Field("sparseunion", FieldType.nullable(new ArrowType.Union(UnionMode.Sparse, new int[] {0})), - Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))) - ); + final List fields = + Arrays.asList( + Field.nullable("utf8", ArrowType.Utf8.INSTANCE), + Field.nullable("largeutf8", ArrowType.LargeUtf8.INSTANCE), + Field.nullable("binary", ArrowType.Binary.INSTANCE), + Field.nullable("largebinary", ArrowType.LargeBinary.INSTANCE), + Field.nullable("fixedsizebinary", new ArrowType.FixedSizeBinary(2)), + Field.nullable("decimal128", new ArrowType.Decimal(3, 2, 128)), + Field.nullable("decimal128", new ArrowType.Decimal(3, 2, 256)), + new Field( + "list", + FieldType.nullable(ArrowType.List.INSTANCE), + Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))), + new Field( + "largelist", + FieldType.nullable(ArrowType.LargeList.INSTANCE), + Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))), + new Field( + "map", + FieldType.nullable(new ArrowType.Map(/*keyssorted*/ false)), + Collections.singletonList( + new Field( + "items", + FieldType.notNullable(ArrowType.Struct.INSTANCE), + Arrays.asList( + Field.notNullable("keys", new ArrowType.Int(32, true)), + Field.nullable("values", new ArrowType.Int(32, true)))))), + new Field( + "fixedsizelist", + FieldType.nullable(new ArrowType.FixedSizeList(2)), + Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))), + new Field( + "denseunion", + FieldType.nullable(new ArrowType.Union(UnionMode.Dense, new int[] {0})), + Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true)))), + new Field( + "sparseunion", + FieldType.nullable(new ArrowType.Union(UnionMode.Sparse, new int[] {0})), + Collections.singletonList(Field.nullable("items", new ArrowType.Int(32, true))))); for (final Field field : fields) { final Schema schema = new Schema(Collections.singletonList(field)); @@ -464,7 +474,8 @@ public void testRoundtripEmptyVector() throws Exception { outputFile.deleteOnExit(); // Try with no allocation - try (final JsonFileWriter jsonWriter = new JsonFileWriter(outputFile, JsonFileWriter.config().pretty(true))) { + try (final JsonFileWriter jsonWriter = + new JsonFileWriter(outputFile, JsonFileWriter.config().pretty(true))) { jsonWriter.start(schema, null); jsonWriter.write(root); } catch (Exception e) { @@ -484,7 +495,8 @@ public void testRoundtripEmptyVector() throws Exception { // Try with an explicit allocation root.allocateNew(); root.setRowCount(0); - try (final JsonFileWriter jsonWriter = new JsonFileWriter(outputFile, JsonFileWriter.config().pretty(true))) { + try (final JsonFileWriter jsonWriter = + new JsonFileWriter(outputFile, JsonFileWriter.config().pretty(true))) { jsonWriter.start(schema, null); jsonWriter.write(root); } catch (Exception e) { diff --git a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestRoundTrip.java b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestRoundTrip.java index 5f57e90f6ba..7ac5e180526 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestRoundTrip.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestRoundTrip.java @@ -41,7 +41,6 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.BiConsumer; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.util.Collections2; @@ -96,8 +95,7 @@ public static Collection getWriteOption() { return Arrays.asList( new Object[] {"V4Legacy", legacy}, new Object[] {"V4", version4}, - new Object[] {"V5", IpcOption.DEFAULT} - ); + new Object[] {"V5", IpcOption.DEFAULT}); } @BeforeClass @@ -113,12 +111,12 @@ public static void tearDownClass() { @Test public void testStruct() throws Exception { try (final BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); - final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { + allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); + final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { writeData(COUNT, parent); roundTrip( new VectorSchemaRoot(parent.getChild("root")), - /* dictionaryProvider */null, + /* dictionaryProvider */ null, TestRoundTrip::writeSingleBatch, validateFileBatches(new int[] {COUNT}, this::validateContent), validateStreamBatches(new int[] {COUNT}, this::validateContent)); @@ -128,12 +126,12 @@ public void testStruct() throws Exception { @Test public void testComplex() throws Exception { try (final BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); - final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { + allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); + final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { writeComplexData(COUNT, parent); roundTrip( new VectorSchemaRoot(parent.getChild("root")), - /* dictionaryProvider */null, + /* dictionaryProvider */ null, TestRoundTrip::writeSingleBatch, validateFileBatches(new int[] {COUNT}, this::validateComplexContent), validateStreamBatches(new int[] {COUNT}, this::validateComplexContent)); @@ -144,12 +142,12 @@ public void testComplex() throws Exception { public void testMultipleRecordBatches() throws Exception { int[] counts = {10, 5}; try (final BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); - final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { + allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); + final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { writeData(counts[0], parent); roundTrip( new VectorSchemaRoot(parent.getChild("root")), - /* dictionaryProvider */null, + /* dictionaryProvider */ null, (root, writer) -> { writer.start(); parent.allocateNew(); @@ -158,7 +156,8 @@ public void testMultipleRecordBatches() throws Exception { writer.writeBatch(); parent.allocateNew(); - // if we write the same data we don't catch that the metadata is stored in the wrong order. + // if we write the same data we don't catch that the metadata is stored in the wrong + // order. writeData(counts[1], parent); root.setRowCount(counts[1]); writer.writeBatch(); @@ -178,20 +177,26 @@ public void testUnionV4() throws Exception { final ByteArrayOutputStream memoryStream = new ByteArrayOutputStream(); try (final BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); - final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { + allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); + final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { writeUnionData(COUNT, parent); final VectorSchemaRoot root = new VectorSchemaRoot(parent.getChild("root")); - IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { - try (final FileOutputStream fileStream = new FileOutputStream(temp)) { - new ArrowFileWriter(root, null, fileStream.getChannel(), writeOption); - new ArrowStreamWriter(root, null, Channels.newChannel(memoryStream), writeOption); - } - }); + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> { + try (final FileOutputStream fileStream = new FileOutputStream(temp)) { + new ArrowFileWriter(root, null, fileStream.getChannel(), writeOption); + new ArrowStreamWriter(root, null, Channels.newChannel(memoryStream), writeOption); + } + }); assertTrue(e.getMessage(), e.getMessage().contains("Cannot write union with V4 metadata")); - e = assertThrows(IllegalArgumentException.class, () -> { - new ArrowStreamWriter(root, null, Channels.newChannel(memoryStream), writeOption); - }); + e = + assertThrows( + IllegalArgumentException.class, + () -> { + new ArrowStreamWriter(root, null, Channels.newChannel(memoryStream), writeOption); + }); assertTrue(e.getMessage(), e.getMessage().contains("Cannot write union with V4 metadata")); } } @@ -200,14 +205,14 @@ public void testUnionV4() throws Exception { public void testUnionV5() throws Exception { Assume.assumeTrue(writeOption.metadataVersion == MetadataVersion.V5); try (final BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); - final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { + allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); + final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { writeUnionData(COUNT, parent); VectorSchemaRoot root = new VectorSchemaRoot(parent.getChild("root")); validateUnionData(COUNT, root); roundTrip( root, - /* dictionaryProvider */null, + /* dictionaryProvider */ null, TestRoundTrip::writeSingleBatch, validateFileBatches(new int[] {COUNT}, this::validateUnionData), validateStreamBatches(new int[] {COUNT}, this::validateUnionData)); @@ -216,7 +221,8 @@ public void testUnionV5() throws Exception { @Test public void testTiny() throws Exception { - try (final VectorSchemaRoot root = VectorSchemaRoot.create(MessageSerializerTest.testSchema(), allocator)) { + try (final VectorSchemaRoot root = + VectorSchemaRoot.create(MessageSerializerTest.testSchema(), allocator)) { root.getFieldVectors().get(0).allocateNew(); int count = 16; TinyIntVector vector = (TinyIntVector) root.getFieldVectors().get(0); @@ -228,7 +234,7 @@ public void testTiny() throws Exception { roundTrip( root, - /* dictionaryProvider */null, + /* dictionaryProvider */ null, TestRoundTrip::writeSingleBatch, validateFileBatches(new int[] {count}, this::validateTinyData), validateStreamBatches(new int[] {count}, this::validateTinyData)); @@ -250,13 +256,34 @@ private void validateTinyData(int count, VectorSchemaRoot root) { @Test public void testMetadata() throws Exception { List childFields = new ArrayList<>(); - childFields.add(new Field("varchar-child", new FieldType(true, ArrowType.Utf8.INSTANCE, null, metadata(1)), null)); - childFields.add(new Field("float-child", - new FieldType(true, new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE), null, metadata(2)), null)); - childFields.add(new Field("int-child", new FieldType(false, new ArrowType.Int(32, true), null, metadata(3)), null)); - childFields.add(new Field("list-child", new FieldType(true, ArrowType.List.INSTANCE, null, metadata(4)), - Collections2.asImmutableList(new Field("l1", FieldType.nullable(new ArrowType.Int(16, true)), null)))); - Field field = new Field("meta", new FieldType(true, ArrowType.Struct.INSTANCE, null, metadata(0)), childFields); + childFields.add( + new Field( + "varchar-child", + new FieldType(true, ArrowType.Utf8.INSTANCE, null, metadata(1)), + null)); + childFields.add( + new Field( + "float-child", + new FieldType( + true, + new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE), + null, + metadata(2)), + null)); + childFields.add( + new Field( + "int-child", + new FieldType(false, new ArrowType.Int(32, true), null, metadata(3)), + null)); + childFields.add( + new Field( + "list-child", + new FieldType(true, ArrowType.List.INSTANCE, null, metadata(4)), + Collections2.asImmutableList( + new Field("l1", FieldType.nullable(new ArrowType.Int(16, true)), null)))); + Field field = + new Field( + "meta", new FieldType(true, ArrowType.Struct.INSTANCE, null, metadata(0)), childFields); Map metadata = new HashMap<>(); metadata.put("s1", "v1"); metadata.put("s2", "v2"); @@ -264,27 +291,28 @@ public void testMetadata() throws Exception { assertEquals(metadata, originalSchema.getCustomMetadata()); try (final BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); - final StructVector vector = (StructVector) field.createVector(originalVectorAllocator)) { + allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); + final StructVector vector = (StructVector) field.createVector(originalVectorAllocator)) { vector.allocateNewSafe(); vector.setValueCount(0); List vectors = Collections2.asImmutableList(vector); VectorSchemaRoot root = new VectorSchemaRoot(originalSchema, vectors, 0); - BiConsumer validate = (count, readRoot) -> { - Schema schema = readRoot.getSchema(); - assertEquals(originalSchema, schema); - assertEquals(originalSchema.getCustomMetadata(), schema.getCustomMetadata()); - Field top = schema.getFields().get(0); - assertEquals(metadata(0), top.getMetadata()); - for (int i = 0; i < 4; i++) { - assertEquals(metadata(i + 1), top.getChildren().get(i).getMetadata()); - } - }; + BiConsumer validate = + (count, readRoot) -> { + Schema schema = readRoot.getSchema(); + assertEquals(originalSchema, schema); + assertEquals(originalSchema.getCustomMetadata(), schema.getCustomMetadata()); + Field top = schema.getFields().get(0); + assertEquals(metadata(0), top.getMetadata()); + for (int i = 0; i < 4; i++) { + assertEquals(metadata(i + 1), top.getChildren().get(i).getMetadata()); + } + }; roundTrip( root, - /* dictionaryProvider */null, + /* dictionaryProvider */ null, TestRoundTrip::writeSingleBatch, validateFileBatches(new int[] {0}, validate), validateStreamBatches(new int[] {0}, validate)); @@ -303,8 +331,8 @@ public void testFlatDictionary() throws Exception { AtomicInteger numDictionaryBlocksWritten = new AtomicInteger(); MapDictionaryProvider provider = new MapDictionaryProvider(); try (final BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); - final VectorSchemaRoot root = writeFlatDictionaryData(originalVectorAllocator, provider)) { + allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); + final VectorSchemaRoot root = writeFlatDictionaryData(originalVectorAllocator, provider)) { roundTrip( root, provider, @@ -313,7 +341,8 @@ public void testFlatDictionary() throws Exception { writer.writeBatch(); writer.end(); if (writer instanceof ArrowFileWriter) { - numDictionaryBlocksWritten.set(((ArrowFileWriter) writer).getDictionaryBlocks().size()); + numDictionaryBlocksWritten.set( + ((ArrowFileWriter) writer).getDictionaryBlocks().size()); } }, (fileReader) -> { @@ -346,15 +375,17 @@ public void testNestedDictionary() throws Exception { // data being written: // [['foo', 'bar'], ['foo'], ['bar']] -> [[0, 1], [0], [1]] try (final BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); - final VectorSchemaRoot root = writeNestedDictionaryData(originalVectorAllocator, provider)) { - CheckedConsumer validateDictionary = (streamReader) -> { - VectorSchemaRoot readRoot = streamReader.getVectorSchemaRoot(); - Schema schema = readRoot.getSchema(); - LOGGER.debug("reading schema: " + schema); - assertTrue(streamReader.loadNextBatch()); - validateNestedDictionary(readRoot, streamReader); - }; + allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); + final VectorSchemaRoot root = + writeNestedDictionaryData(originalVectorAllocator, provider)) { + CheckedConsumer validateDictionary = + (streamReader) -> { + VectorSchemaRoot readRoot = streamReader.getVectorSchemaRoot(); + Schema schema = readRoot.getSchema(); + LOGGER.debug("reading schema: " + schema); + assertTrue(streamReader.loadNextBatch()); + validateNestedDictionary(readRoot, streamReader); + }; roundTrip( root, provider, @@ -363,7 +394,8 @@ public void testNestedDictionary() throws Exception { writer.writeBatch(); writer.end(); if (writer instanceof ArrowFileWriter) { - numDictionaryBlocksWritten.set(((ArrowFileWriter) writer).getDictionaryBlocks().size()); + numDictionaryBlocksWritten.set( + ((ArrowFileWriter) writer).getDictionaryBlocks().size()); } }, validateDictionary, @@ -387,17 +419,22 @@ public void testFixedSizeBinary() throws Exception { } } - BiConsumer validator = (expectedCount, root) -> { - for (int i = 0; i < expectedCount; i++) { - assertArrayEquals(byteValues[i], ((byte[]) root.getVector("fixed-binary").getObject(i))); - } - }; + BiConsumer validator = + (expectedCount, root) -> { + for (int i = 0; i < expectedCount; i++) { + assertArrayEquals( + byteValues[i], ((byte[]) root.getVector("fixed-binary").getObject(i))); + } + }; try (final BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); - final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { - FixedSizeBinaryVector fixedSizeBinaryVector = parent.addOrGet("fixed-binary", - FieldType.nullable(new ArrowType.FixedSizeBinary(typeWidth)), FixedSizeBinaryVector.class); + allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); + final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { + FixedSizeBinaryVector fixedSizeBinaryVector = + parent.addOrGet( + "fixed-binary", + FieldType.nullable(new ArrowType.FixedSizeBinary(typeWidth)), + FixedSizeBinaryVector.class); parent.allocateNew(); for (int i = 0; i < count; i++) { fixedSizeBinaryVector.set(i, byteValues[i]); @@ -406,7 +443,7 @@ public void testFixedSizeBinary() throws Exception { roundTrip( new VectorSchemaRoot(parent), - /* dictionaryProvider */null, + /* dictionaryProvider */ null, TestRoundTrip::writeSingleBatch, validateFileBatches(new int[] {count}, validator), validateStreamBatches(new int[] {count}, validator)); @@ -415,22 +452,31 @@ public void testFixedSizeBinary() throws Exception { @Test public void testFixedSizeList() throws Exception { - BiConsumer validator = (expectedCount, root) -> { - for (int i = 0; i < expectedCount; i++) { - assertEquals(Collections2.asImmutableList(i + 0.1f, i + 10.1f), root.getVector("float-pairs") - .getObject(i)); - assertEquals(i, root.getVector("ints").getObject(i)); - } - }; + BiConsumer validator = + (expectedCount, root) -> { + for (int i = 0; i < expectedCount; i++) { + assertEquals( + Collections2.asImmutableList(i + 0.1f, i + 10.1f), + root.getVector("float-pairs").getObject(i)); + assertEquals(i, root.getVector("ints").getObject(i)); + } + }; try (final BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); - final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { - FixedSizeListVector tuples = parent.addOrGet("float-pairs", - FieldType.nullable(new ArrowType.FixedSizeList(2)), FixedSizeListVector.class); - Float4Vector floats = (Float4Vector) tuples.addOrGetVector(FieldType.nullable(Types.MinorType.FLOAT4.getType())) - .getVector(); - IntVector ints = parent.addOrGet("ints", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); + allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); + final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { + FixedSizeListVector tuples = + parent.addOrGet( + "float-pairs", + FieldType.nullable(new ArrowType.FixedSizeList(2)), + FixedSizeListVector.class); + Float4Vector floats = + (Float4Vector) + tuples + .addOrGetVector(FieldType.nullable(Types.MinorType.FLOAT4.getType())) + .getVector(); + IntVector ints = + parent.addOrGet("ints", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); parent.allocateNew(); for (int i = 0; i < COUNT; i++) { tuples.setNotNull(i); @@ -442,7 +488,7 @@ public void testFixedSizeList() throws Exception { roundTrip( new VectorSchemaRoot(parent), - /* dictionaryProvider */null, + /* dictionaryProvider */ null, TestRoundTrip::writeSingleBatch, validateFileBatches(new int[] {COUNT}, validator), validateStreamBatches(new int[] {COUNT}, validator)); @@ -452,18 +498,18 @@ public void testFixedSizeList() throws Exception { @Test public void testVarBinary() throws Exception { try (final BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); - final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { + allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); + final StructVector parent = StructVector.empty("parent", originalVectorAllocator)) { writeVarBinaryData(COUNT, parent); VectorSchemaRoot root = new VectorSchemaRoot(parent.getChild("root")); validateVarBinary(COUNT, root); roundTrip( root, - /* dictionaryProvider */null, + /* dictionaryProvider */ null, TestRoundTrip::writeSingleBatch, - validateFileBatches(new int[]{COUNT}, this::validateVarBinary), - validateStreamBatches(new int[]{COUNT}, this::validateVarBinary)); + validateFileBatches(new int[] {COUNT}, this::validateVarBinary), + validateStreamBatches(new int[] {COUNT}, this::validateVarBinary)); } } @@ -472,19 +518,21 @@ public void testReadWriteMultipleBatches() throws IOException { File file = new File("target/mytest_nulls_multibatch.arrow"); int numBlocksWritten = 0; - try (IntVector vector = new IntVector("foo", allocator);) { + try (IntVector vector = new IntVector("foo", allocator); ) { Schema schema = new Schema(Collections.singletonList(vector.getField())); try (FileOutputStream fileOutputStream = new FileOutputStream(file); - VectorSchemaRoot root = - new VectorSchemaRoot(schema, Collections.singletonList((FieldVector) vector), vector.getValueCount()); - ArrowFileWriter writer = new ArrowFileWriter(root, null, fileOutputStream.getChannel(), writeOption)) { + VectorSchemaRoot root = + new VectorSchemaRoot( + schema, Collections.singletonList((FieldVector) vector), vector.getValueCount()); + ArrowFileWriter writer = + new ArrowFileWriter(root, null, fileOutputStream.getChannel(), writeOption)) { writeBatchData(writer, vector, root); numBlocksWritten = writer.getRecordBlocks().size(); } } try (FileInputStream fileInputStream = new FileInputStream(file); - ArrowFileReader reader = new ArrowFileReader(fileInputStream.getChannel(), allocator);) { + ArrowFileReader reader = new ArrowFileReader(fileInputStream.getChannel(), allocator); ) { IntVector vector = (IntVector) reader.getVectorSchemaRoot().getFieldVectors().get(0); validateBatchData(reader, vector); assertEquals(numBlocksWritten, reader.getRecordBlocks().size()); @@ -494,34 +542,40 @@ public void testReadWriteMultipleBatches() throws IOException { @Test public void testMap() throws Exception { try (final BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); - final VectorSchemaRoot root = writeMapData(originalVectorAllocator)) { + allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); + final VectorSchemaRoot root = writeMapData(originalVectorAllocator)) { roundTrip( root, - /* dictionaryProvider */null, + /* dictionaryProvider */ null, TestRoundTrip::writeSingleBatch, - validateFileBatches(new int[]{root.getRowCount()}, (count, readRoot) -> validateMapData(readRoot)), - validateStreamBatches(new int[]{root.getRowCount()}, (count, readRoot) -> validateMapData(readRoot))); + validateFileBatches( + new int[] {root.getRowCount()}, (count, readRoot) -> validateMapData(readRoot)), + validateStreamBatches( + new int[] {root.getRowCount()}, (count, readRoot) -> validateMapData(readRoot))); } } @Test public void testListAsMap() throws Exception { try (final BufferAllocator originalVectorAllocator = - allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); - final VectorSchemaRoot root = writeListAsMapData(originalVectorAllocator)) { + allocator.newChildAllocator("original vectors", 0, allocator.getLimit()); + final VectorSchemaRoot root = writeListAsMapData(originalVectorAllocator)) { roundTrip( root, - /* dictionaryProvider */null, + /* dictionaryProvider */ null, TestRoundTrip::writeSingleBatch, - validateFileBatches(new int[]{root.getRowCount()}, (count, readRoot) -> validateListAsMapData(readRoot)), - validateStreamBatches(new int[]{root.getRowCount()}, (count, readRoot) -> validateListAsMapData(readRoot))); + validateFileBatches( + new int[] {root.getRowCount()}, (count, readRoot) -> validateListAsMapData(readRoot)), + validateStreamBatches( + new int[] {root.getRowCount()}, + (count, readRoot) -> validateListAsMapData(readRoot))); } } // Generic test helpers - private static void writeSingleBatch(VectorSchemaRoot root, ArrowWriter writer) throws IOException { + private static void writeSingleBatch(VectorSchemaRoot root, ArrowWriter writer) + throws IOException { writer.start(); writer.writeBatch(); writer.end(); @@ -539,7 +593,8 @@ private CheckedConsumer validateFileBatches( assertEquals(counts.length, recordBatches.size()); long previousOffset = 0; for (ArrowBlock rbBlock : recordBatches) { - assertTrue(rbBlock.getOffset() + " > " + previousOffset, rbBlock.getOffset() > previousOffset); + assertTrue( + rbBlock.getOffset() + " > " + previousOffset, rbBlock.getOffset() > previousOffset); previousOffset = rbBlock.getOffset(); arrowReader.loadRecordBatch(rbBlock); assertEquals("RB #" + i, counts[i], root.getRowCount()); @@ -590,34 +645,40 @@ interface CheckedBiConsumer { void accept(T t, U u) throws Exception; } - private void roundTrip(VectorSchemaRoot root, DictionaryProvider provider, - CheckedBiConsumer writer, - CheckedConsumer fileValidator, - CheckedConsumer streamValidator) throws Exception { + private void roundTrip( + VectorSchemaRoot root, + DictionaryProvider provider, + CheckedBiConsumer writer, + CheckedConsumer fileValidator, + CheckedConsumer streamValidator) + throws Exception { final File temp = File.createTempFile("arrow-test-" + name + "-", ".arrow"); temp.deleteOnExit(); final ByteArrayOutputStream memoryStream = new ByteArrayOutputStream(); final Map metadata = new HashMap<>(); metadata.put("foo", "bar"); try (final FileOutputStream fileStream = new FileOutputStream(temp); - final ArrowFileWriter fileWriter = - new ArrowFileWriter(root, provider, fileStream.getChannel(), metadata, writeOption); - final ArrowStreamWriter streamWriter = - new ArrowStreamWriter(root, provider, Channels.newChannel(memoryStream), writeOption)) { + final ArrowFileWriter fileWriter = + new ArrowFileWriter(root, provider, fileStream.getChannel(), metadata, writeOption); + final ArrowStreamWriter streamWriter = + new ArrowStreamWriter(root, provider, Channels.newChannel(memoryStream), writeOption)) { writer.accept(root, fileWriter); writer.accept(root, streamWriter); } - MessageMetadataResult metadataResult = MessageSerializer.readMessage( - new ReadChannel(Channels.newChannel(new ByteArrayInputStream(memoryStream.toByteArray())))); + MessageMetadataResult metadataResult = + MessageSerializer.readMessage( + new ReadChannel( + Channels.newChannel(new ByteArrayInputStream(memoryStream.toByteArray())))); assertNotNull(metadataResult); assertEquals(writeOption.metadataVersion.toFlatbufID(), metadataResult.getMessage().version()); - try ( - BufferAllocator readerAllocator = allocator.newChildAllocator("reader", 0, allocator.getLimit()); + try (BufferAllocator readerAllocator = + allocator.newChildAllocator("reader", 0, allocator.getLimit()); FileInputStream fileInputStream = new FileInputStream(temp); ByteArrayInputStream inputStream = new ByteArrayInputStream(memoryStream.toByteArray()); - ArrowFileReader fileReader = new ArrowFileReader(fileInputStream.getChannel(), readerAllocator); + ArrowFileReader fileReader = + new ArrowFileReader(fileInputStream.getChannel(), readerAllocator); ArrowStreamReader streamReader = new ArrowStreamReader(inputStream, readerAllocator)) { fileValidator.accept(fileReader); streamValidator.accept(streamReader); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestUIntDictionaryRoundTrip.java b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestUIntDictionaryRoundTrip.java index ac95121eb73..0ac85754925 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestUIntDictionaryRoundTrip.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/ipc/TestUIntDictionaryRoundTrip.java @@ -32,7 +32,6 @@ import java.util.Collection; import java.util.Map; import java.util.function.ToIntBiFunction; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.FieldVector; @@ -57,10 +56,7 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -/** - * Test the round-trip of dictionary encoding, - * with unsigned integer as indices. - */ +/** Test the round-trip of dictionary encoding, with unsigned integer as indices. */ @RunWith(Parameterized.class) public class TestUIntDictionaryRoundTrip { @@ -89,10 +85,13 @@ private byte[] writeData(FieldVector encodedVector) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); VectorSchemaRoot root = new VectorSchemaRoot( - Arrays.asList(encodedVector.getField()), Arrays.asList(encodedVector), encodedVector.getValueCount()); - try (ArrowWriter writer = streamMode ? - new ArrowStreamWriter(root, dictionaryProvider, out) : - new ArrowFileWriter(root, dictionaryProvider, Channels.newChannel(out))) { + Arrays.asList(encodedVector.getField()), + Arrays.asList(encodedVector), + encodedVector.getValueCount()); + try (ArrowWriter writer = + streamMode + ? new ArrowStreamWriter(root, dictionaryProvider, out) + : new ArrowFileWriter(root, dictionaryProvider, Channels.newChannel(out))) { writer.start(); writer.writeBatch(); writer.end(); @@ -107,10 +106,14 @@ private void readData( ToIntBiFunction valGetter, long dictionaryID, int[] expectedIndices, - String[] expectedDictItems) throws IOException { - try (ArrowReader reader = streamMode ? - new ArrowStreamReader(new ByteArrayInputStream(data), allocator) : - new ArrowFileReader(new SeekableReadChannel(new ByteArrayReadableSeekableByteChannel(data)), allocator)) { + String[] expectedDictItems) + throws IOException { + try (ArrowReader reader = + streamMode + ? new ArrowStreamReader(new ByteArrayInputStream(data), allocator) + : new ArrowFileReader( + new SeekableReadChannel(new ByteArrayReadableSeekableByteChannel(data)), + allocator)) { // verify schema Schema readSchema = reader.getVectorSchemaRoot().getSchema(); @@ -160,7 +163,7 @@ private ValueVector createEncodedVector(int bitWidth, VarCharVector dictionaryVe public void testUInt1RoundTrip() throws IOException { final int vectorLength = UInt1Vector.MAX_UINT1 & UInt1Vector.PROMOTION_MASK; try (VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator); - UInt1Vector encodedVector1 = (UInt1Vector) createEncodedVector(8, dictionaryVector)) { + UInt1Vector encodedVector1 = (UInt1Vector) createEncodedVector(8, dictionaryVector)) { int[] indices = new int[vectorLength]; String[] dictionaryItems = new String[vectorLength]; for (int i = 0; i < vectorLength; i++) { @@ -172,8 +175,12 @@ public void testUInt1RoundTrip() throws IOException { setVector(dictionaryVector, dictionaryItems); byte[] data = writeData(encodedVector1); readData( - data, encodedVector1.getField(), (vector, index) -> (int) ((UInt1Vector) vector).getValueAsLong(index), - 8L, indices, dictionaryItems); + data, + encodedVector1.getField(), + (vector, index) -> (int) ((UInt1Vector) vector).getValueAsLong(index), + 8L, + indices, + dictionaryItems); } } @@ -181,18 +188,24 @@ public void testUInt1RoundTrip() throws IOException { public void testUInt2RoundTrip() throws IOException { try (VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator); UInt2Vector encodedVector2 = (UInt2Vector) createEncodedVector(16, dictionaryVector)) { - int[] indices = new int[]{1, 3, 5, 7, 9, UInt2Vector.MAX_UINT2}; + int[] indices = new int[] {1, 3, 5, 7, 9, UInt2Vector.MAX_UINT2}; String[] dictItems = new String[UInt2Vector.MAX_UINT2]; for (int i = 0; i < UInt2Vector.MAX_UINT2; i++) { dictItems[i] = String.valueOf(i); } - setVector(encodedVector2, (char) 1, (char) 3, (char) 5, (char) 7, (char) 9, UInt2Vector.MAX_UINT2); + setVector( + encodedVector2, (char) 1, (char) 3, (char) 5, (char) 7, (char) 9, UInt2Vector.MAX_UINT2); setVector(dictionaryVector, dictItems); byte[] data = writeData(encodedVector2); - readData(data, encodedVector2.getField(), (vector, index) -> (int) ((UInt2Vector) vector).getValueAsLong(index), - 16L, indices, dictItems); + readData( + data, + encodedVector2.getField(), + (vector, index) -> (int) ((UInt2Vector) vector).getValueAsLong(index), + 16L, + indices, + dictItems); } } @@ -201,7 +214,7 @@ public void testUInt4RoundTrip() throws IOException { final int dictLength = 10; try (VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator); UInt4Vector encodedVector4 = (UInt4Vector) createEncodedVector(32, dictionaryVector)) { - int[] indices = new int[]{1, 3, 5, 7, 9}; + int[] indices = new int[] {1, 3, 5, 7, 9}; String[] dictItems = new String[dictLength]; for (int i = 0; i < dictLength; i++) { dictItems[i] = String.valueOf(i); @@ -212,8 +225,13 @@ public void testUInt4RoundTrip() throws IOException { setVector(encodedVector4, 1, 3, 5, 7, 9); byte[] data = writeData(encodedVector4); - readData(data, encodedVector4.getField(), (vector, index) -> (int) ((UInt4Vector) vector).getValueAsLong(index), - 32L, indices, dictItems); + readData( + data, + encodedVector4.getField(), + (vector, index) -> (int) ((UInt4Vector) vector).getValueAsLong(index), + 32L, + indices, + dictItems); } } @@ -222,7 +240,7 @@ public void testUInt8RoundTrip() throws IOException { final int dictLength = 10; try (VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator); UInt8Vector encodedVector8 = (UInt8Vector) createEncodedVector(64, dictionaryVector)) { - int[] indices = new int[]{1, 3, 5, 7, 9}; + int[] indices = new int[] {1, 3, 5, 7, 9}; String[] dictItems = new String[dictLength]; for (int i = 0; i < dictLength; i++) { dictItems[i] = String.valueOf(i); @@ -232,16 +250,18 @@ public void testUInt8RoundTrip() throws IOException { setVector(dictionaryVector, dictItems); byte[] data = writeData(encodedVector8); - readData(data, encodedVector8.getField(), (vector, index) -> (int) ((UInt8Vector) vector).getValueAsLong(index), - 64L, indices, dictItems); + readData( + data, + encodedVector8.getField(), + (vector, index) -> (int) ((UInt8Vector) vector).getValueAsLong(index), + 64L, + indices, + dictItems); } } @Parameterized.Parameters(name = "stream mode = {0}") public static Collection getRepeat() { - return Arrays.asList( - new Object[]{true}, - new Object[]{false} - ); + return Arrays.asList(new Object[] {true}, new Object[] {false}); } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/ipc/message/TestMessageMetadataResult.java b/java/vector/src/test/java/org/apache/arrow/vector/ipc/message/TestMessageMetadataResult.java index 0505a18484b..f4b187ef9ab 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/ipc/message/TestMessageMetadataResult.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/ipc/message/TestMessageMetadataResult.java @@ -20,7 +20,6 @@ import static org.junit.Assert.assertEquals; import java.nio.ByteBuffer; - import org.junit.Test; public class TestMessageMetadataResult { @@ -28,9 +27,9 @@ public class TestMessageMetadataResult { @Test public void getMessageLength_returnsConstructValue() { // This API is used by spark. - MessageMetadataResult result = new MessageMetadataResult(1, ByteBuffer.allocate(0), - new org.apache.arrow.flatbuf.Message()); + MessageMetadataResult result = + new MessageMetadataResult( + 1, ByteBuffer.allocate(0), new org.apache.arrow.flatbuf.Message()); assertEquals(1, result.getMessageLength()); } - } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/pojo/TestConvert.java b/java/vector/src/test/java/org/apache/arrow/vector/pojo/TestConvert.java index 5cc0d080053..2be68be6c28 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/pojo/TestConvert.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/pojo/TestConvert.java @@ -22,12 +22,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import com.google.flatbuffers.FlatBufferBuilder; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.util.Collections2; @@ -48,11 +48,7 @@ import org.apache.arrow.vector.types.pojo.Schema; import org.junit.Test; -import com.google.flatbuffers.FlatBufferBuilder; - -/** - * Test conversion between Flatbuf and Pojo field representations. - */ +/** Test conversion between Flatbuf and Pojo field representations. */ public class TestConvert { @Test @@ -65,7 +61,9 @@ public void simple() { public void complex() { java.util.List children = new ArrayList<>(); children.add(new Field("child1", FieldType.nullable(Utf8.INSTANCE), null)); - children.add(new Field("child2", FieldType.nullable(new FloatingPoint(SINGLE)), Collections.emptyList())); + children.add( + new Field( + "child2", FieldType.nullable(new FloatingPoint(SINGLE)), Collections.emptyList())); Field initialField = new Field("a", FieldType.nullable(Struct.INSTANCE), children); run(initialField); @@ -75,8 +73,9 @@ public void complex() { public void list() throws Exception { java.util.List children = new ArrayList<>(); try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - ListVector writeVector = ListVector.empty("list", allocator); - FixedSizeListVector writeFixedVector = FixedSizeListVector.empty("fixedlist", 5, allocator)) { + ListVector writeVector = ListVector.empty("list", allocator); + FixedSizeListVector writeFixedVector = + FixedSizeListVector.empty("fixedlist", 5, allocator)) { Field listVectorField = writeVector.getField(); children.add(listVectorField); Field listFixedVectorField = writeFixedVector.getField(); @@ -88,7 +87,8 @@ public void list() throws Exception { parent.add(initialField); FlatBufferBuilder builder = new FlatBufferBuilder(); builder.finish(initialField.getField(builder)); - org.apache.arrow.flatbuf.Field flatBufField = org.apache.arrow.flatbuf.Field.getRootAsField(builder.dataBuffer()); + org.apache.arrow.flatbuf.Field flatBufField = + org.apache.arrow.flatbuf.Field.getRootAsField(builder.dataBuffer()); Field finalField = Field.convertField(flatBufField); assertEquals(initialField, finalField); assertFalse(finalField.toString().contains("[DEFAULT]")); @@ -110,7 +110,9 @@ public void list() throws Exception { public void schema() { java.util.List children = new ArrayList<>(); children.add(new Field("child1", FieldType.nullable(Utf8.INSTANCE), null)); - children.add(new Field("child2", FieldType.nullable(new FloatingPoint(SINGLE)), Collections.emptyList())); + children.add( + new Field( + "child2", FieldType.nullable(new FloatingPoint(SINGLE)), Collections.emptyList())); Schema initialSchema = new Schema(children); run(initialSchema); } @@ -119,7 +121,9 @@ public void schema() { public void schemaMetadata() { java.util.List children = new ArrayList<>(); children.add(new Field("child1", FieldType.nullable(Utf8.INSTANCE), null)); - children.add(new Field("child2", FieldType.nullable(new FloatingPoint(SINGLE)), Collections.emptyList())); + children.add( + new Field( + "child2", FieldType.nullable(new FloatingPoint(SINGLE)), Collections.emptyList())); Map metadata = new HashMap<>(); metadata.put("key1", "value1"); metadata.put("key2", "value2"); @@ -131,21 +135,45 @@ public void schemaMetadata() { public void nestedSchema() { java.util.List children = new ArrayList<>(); children.add(new Field("child1", FieldType.nullable(Utf8.INSTANCE), null)); - children.add(new Field("child2", FieldType.nullable(new FloatingPoint(SINGLE)), Collections.emptyList())); - children.add(new Field("child3", FieldType.nullable(new Struct()), Collections2.asImmutableList( - new Field("child3.1", FieldType.nullable(Utf8.INSTANCE), null), - new Field("child3.2", FieldType.nullable(new FloatingPoint(DOUBLE)), Collections.emptyList()) - ))); - children.add(new Field("child4", FieldType.nullable(new List()), Collections2.asImmutableList( - new Field("child4.1", FieldType.nullable(Utf8.INSTANCE), null) - ))); - children.add(new Field("child5", FieldType.nullable( - new Union(UnionMode.Sparse, new int[] {MinorType.TIMESTAMPMILLI.ordinal(), MinorType.FLOAT8.ordinal()})), - Collections2.asImmutableList( - new Field("child5.1", FieldType.nullable(new Timestamp(TimeUnit.MILLISECOND, null)), null), - new Field("child5.2", FieldType.nullable(new FloatingPoint(DOUBLE)), Collections.emptyList()), - new Field("child5.3", FieldType.nullable(new Timestamp(TimeUnit.MILLISECOND, "UTC")), null) - ))); + children.add( + new Field( + "child2", FieldType.nullable(new FloatingPoint(SINGLE)), Collections.emptyList())); + children.add( + new Field( + "child3", + FieldType.nullable(new Struct()), + Collections2.asImmutableList( + new Field("child3.1", FieldType.nullable(Utf8.INSTANCE), null), + new Field( + "child3.2", + FieldType.nullable(new FloatingPoint(DOUBLE)), + Collections.emptyList())))); + children.add( + new Field( + "child4", + FieldType.nullable(new List()), + Collections2.asImmutableList( + new Field("child4.1", FieldType.nullable(Utf8.INSTANCE), null)))); + children.add( + new Field( + "child5", + FieldType.nullable( + new Union( + UnionMode.Sparse, + new int[] {MinorType.TIMESTAMPMILLI.ordinal(), MinorType.FLOAT8.ordinal()})), + Collections2.asImmutableList( + new Field( + "child5.1", + FieldType.nullable(new Timestamp(TimeUnit.MILLISECOND, null)), + null), + new Field( + "child5.2", + FieldType.nullable(new FloatingPoint(DOUBLE)), + Collections.emptyList()), + new Field( + "child5.3", + FieldType.nullable(new Timestamp(TimeUnit.MILLISECOND, "UTC")), + null)))); Schema initialSchema = new Schema(children); run(initialSchema); } @@ -153,7 +181,8 @@ public void nestedSchema() { private void run(Field initialField) { FlatBufferBuilder builder = new FlatBufferBuilder(); builder.finish(initialField.getField(builder)); - org.apache.arrow.flatbuf.Field flatBufField = org.apache.arrow.flatbuf.Field.getRootAsField(builder.dataBuffer()); + org.apache.arrow.flatbuf.Field flatBufField = + org.apache.arrow.flatbuf.Field.getRootAsField(builder.dataBuffer()); Field finalField = Field.convertField(flatBufField); assertEquals(initialField, finalField); } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/table/BaseTableTest.java b/java/vector/src/test/java/org/apache/arrow/vector/table/BaseTableTest.java index 1b7f984992a..31a22a5d5eb 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/table/BaseTableTest.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/table/BaseTableTest.java @@ -32,7 +32,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.FieldVector; @@ -176,8 +175,7 @@ void testGetVector() { List vectorList = twoIntColumns(allocator); try (Table t = new Table(vectorList)) { assertNotNull(t.getVector(INT_VECTOR_NAME_1)); - assertThrows(IllegalArgumentException.class, - () -> t.getVector("wrong name")); + assertThrows(IllegalArgumentException.class, () -> t.getVector("wrong name")); } } @@ -197,8 +195,7 @@ void getVectorCopyByIndex() { assertEquals(original.getObject(i), copy.getObject(i)); } } - assertThrows(IllegalArgumentException.class, - () -> t.getVector("wrong name")); + assertThrows(IllegalArgumentException.class, () -> t.getVector("wrong name")); } } @@ -217,8 +214,7 @@ void getVectorCopyByName() { assertEquals(original.getObject(i), copy.getObject(i)); } } - assertThrows(IllegalArgumentException.class, - () -> t.getVector("wrong name")); + assertThrows(IllegalArgumentException.class, () -> t.getVector("wrong name")); } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/table/RowTest.java b/java/vector/src/test/java/org/apache/arrow/vector/table/RowTest.java index 3e6a096104d..31057f004c0 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/table/RowTest.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/table/RowTest.java @@ -53,7 +53,6 @@ import java.time.Period; import java.util.ArrayList; import java.util.List; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -169,8 +168,7 @@ void testNameNotFound() { try (Table t = new Table(vectorList)) { Row c = t.immutableRow(); c.setPosition(1); - assertThrows(IllegalArgumentException.class, - () -> c.getVarCharObj("wrong name")); + assertThrows(IllegalArgumentException.class, () -> c.getVarCharObj("wrong name")); } } @@ -180,8 +178,7 @@ void testWrongType() { try (Table t = new Table(vectorList)) { Row c = t.immutableRow(); c.setPosition(1); - assertThrows(ClassCastException.class, - () -> c.getVarCharObj(INT_VECTOR_NAME_1)); + assertThrows(ClassCastException.class, () -> c.getVarCharObj(INT_VECTOR_NAME_1)); } } @@ -252,7 +249,8 @@ void getIntervalDay() { IntervalUnit unit = IntervalUnit.DAY_TIME; final FieldType fieldType = FieldType.nullable(new ArrowType.Interval(unit)); - IntervalDayVector intervalDayVector = new IntervalDayVector("intervalDay_vector", fieldType, allocator); + IntervalDayVector intervalDayVector = + new IntervalDayVector("intervalDay_vector", fieldType, allocator); NullableIntervalDayHolder holder1 = new NullableIntervalDayHolder(); NullableIntervalDayHolder holder2 = new NullableIntervalDayHolder(); @@ -329,7 +327,8 @@ void getIntervalYear() { IntervalUnit unit = IntervalUnit.YEAR_MONTH; final FieldType fieldType = FieldType.nullable(new ArrowType.Interval(unit)); - IntervalYearVector intervalYearVector = new IntervalYearVector("intervalYear_vector", fieldType, allocator); + IntervalYearVector intervalYearVector = + new IntervalYearVector("intervalYear_vector", fieldType, allocator); NullableIntervalYearHolder holder1 = new NullableIntervalYearHolder(); NullableIntervalYearHolder holder2 = new NullableIntervalYearHolder(); @@ -650,7 +649,8 @@ void getVarChar() { c.setPosition(1); assertEquals(c.getVarCharObj(1), "two"); assertEquals(c.getVarCharObj(1), c.getVarCharObj(VARCHAR_VECTOR_NAME_1)); - assertArrayEquals("two".getBytes(StandardCharsets.UTF_8), c.getVarChar(VARCHAR_VECTOR_NAME_1)); + assertArrayEquals( + "two".getBytes(StandardCharsets.UTF_8), c.getVarChar(VARCHAR_VECTOR_NAME_1)); assertArrayEquals("two".getBytes(StandardCharsets.UTF_8), c.getVarChar(1)); } } @@ -685,7 +685,8 @@ void getLargeVarChar() { c.setPosition(1); assertEquals(c.getLargeVarCharObj(1), "two"); assertEquals(c.getLargeVarCharObj(1), c.getLargeVarCharObj(VARCHAR_VECTOR_NAME_1)); - assertArrayEquals("two".getBytes(StandardCharsets.UTF_8), c.getLargeVarChar(VARCHAR_VECTOR_NAME_1)); + assertArrayEquals( + "two".getBytes(StandardCharsets.UTF_8), c.getLargeVarChar(VARCHAR_VECTOR_NAME_1)); assertArrayEquals("two".getBytes(StandardCharsets.UTF_8), c.getLargeVarChar(1)); } } @@ -737,8 +738,7 @@ void testSimpleStructVector1() { JsonStringHashMap struct = (JsonStringHashMap) c.getStruct(STRUCT_VECTOR_NAME); @SuppressWarnings("unchecked") - JsonStringHashMap struct1 = - (JsonStringHashMap) c.getStruct(0); + JsonStringHashMap struct1 = (JsonStringHashMap) c.getStruct(0); int a = (int) struct.get("struct_int_child"); double b = (double) struct.get("struct_flt_child"); int a1 = (int) struct1.get("struct_int_child"); @@ -792,21 +792,21 @@ void testSimpleDenseUnionVector() { @Test void testExtensionTypeVector() { - TestExtensionType.LocationVector vector = new TestExtensionType.LocationVector("location", allocator); + TestExtensionType.LocationVector vector = + new TestExtensionType.LocationVector("location", allocator); vector.allocateNew(); vector.set(0, 34.073814f, -118.240784f); vector.setValueCount(1); try (VectorSchemaRoot vsr = VectorSchemaRoot.of(vector); - Table table = new Table(vsr)) { + Table table = new Table(vsr)) { Row c = table.immutableRow(); c.setPosition(0); Object object0 = c.getExtensionType("location"); Object object1 = c.getExtensionType(0); assertEquals(object0, object1); @SuppressWarnings("unchecked") - JsonStringHashMap struct0 = - (JsonStringHashMap) object0; + JsonStringHashMap struct0 = (JsonStringHashMap) object0; assertEquals(34.073814f, struct0.get("Latitude")); } } @@ -822,8 +822,7 @@ void testSimpleMapVector1() { List> list = (List>) c.getMap(BIGINT_INT_MAP_VECTOR_NAME); @SuppressWarnings("unchecked") - List> list1 = - (List>) c.getMap(0); + List> list1 = (List>) c.getMap(0); for (int j = 0; j < list1.size(); j++) { assertEquals(list.get(j), list1.get(j)); } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/table/TableTest.java b/java/vector/src/test/java/org/apache/arrow/vector/table/TableTest.java index 539482e510a..ee7e910f5c0 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/table/TableTest.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/table/TableTest.java @@ -26,7 +26,6 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.FieldVector; @@ -100,7 +99,8 @@ void constructor() { } /** - * Tests construction with an iterable that's not a list (there is a specialty constructor for Lists). + * Tests construction with an iterable that's not a list (there is a specialty constructor for + * Lists). */ @Test void constructor2() { @@ -133,7 +133,7 @@ void copy() { try (Table t = new Table(vectorList)) { assertEquals(2, t.getVectorCount()); try (Table copy = t.copy()) { - for (FieldVector v: t.fieldVectors) { + for (FieldVector v : t.fieldVectors) { FieldVector vCopy = copy.getVector(v.getName()); assertNotNull(vCopy); assertEquals(v.getValueCount(), vCopy.getValueCount()); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/table/TestUtils.java b/java/vector/src/test/java/org/apache/arrow/vector/table/TestUtils.java index c0b3bfdf732..7c98c6fd5e7 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/table/TestUtils.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/table/TestUtils.java @@ -23,7 +23,6 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.vector.BigIntVector; import org.apache.arrow.vector.BitVector; @@ -145,8 +144,8 @@ static List intPlusLargeVarcharColumns(BufferAllocator allocator) { /** * Returns a list of two FieldVectors to be used to instantiate Tables for testing. The first - * vector is an IntVector and the second is a VarBinaryVector. Each vector has two values set. - * The large binary vectors values are "one" and "two" encoded with UTF-8 + * vector is an IntVector and the second is a VarBinaryVector. Each vector has two values set. The + * large binary vectors values are "one" and "two" encoded with UTF-8 */ static List intPlusVarBinaryColumns(BufferAllocator allocator) { List vectorList = new ArrayList<>(); @@ -163,8 +162,8 @@ static List intPlusVarBinaryColumns(BufferAllocator allocator) { /** * Returns a list of two FieldVectors to be used to instantiate Tables for testing. The first - * vector is an IntVector and the second is a VarBinaryVector. Each vector has two values set. - * The large binary vectors values are "one" and "two" encoded with UTF-8 + * vector is an IntVector and the second is a VarBinaryVector. Each vector has two values set. The + * large binary vectors values are "one" and "two" encoded with UTF-8 */ static List intPlusLargeVarBinaryColumns(BufferAllocator allocator) { List vectorList = new ArrayList<>(); @@ -181,8 +180,8 @@ static List intPlusLargeVarBinaryColumns(BufferAllocator allocator) /** * Returns a list of two FieldVectors to be used to instantiate Tables for testing. The first - * vector is an IntVector and the second is a FixedSizeBinary vector. Each vector has two values set. - * The large binary vectors values are "one" and "two" encoded with UTF-8 + * vector is an IntVector and the second is a FixedSizeBinary vector. Each vector has two values + * set. The large binary vectors values are "one" and "two" encoded with UTF-8 */ static List intPlusFixedBinaryColumns(BufferAllocator allocator) { List vectorList = new ArrayList<>(); @@ -208,9 +207,10 @@ private static IntVector getSimpleIntVector(BufferAllocator allocator) { /** * Returns a list of fixed-width vectors for testing. It includes + * *

    - *
  1. all integral and floating point types
  2. - *
  3. all basic times and timestamps (second, milli, micro, nano
  4. + *
  5. all integral and floating point types + *
  6. all basic times and timestamps (second, milli, micro, nano *
* * The vector names are based on their type name (e.g. BigIntVector is called "bigInt_vector" @@ -223,8 +223,8 @@ static List fixedWidthVectors(BufferAllocator allocator, int rowCou } /** - * Returns a list of all integral and floating point vectors. - * The vector names are based on their type name (e.g. BigIntVector is called "bigInt_vector" + * Returns a list of all integral and floating point vectors. The vector names are based on their + * type name (e.g. BigIntVector is called "bigInt_vector" */ static List numericVectors( List vectors, BufferAllocator allocator, int rowCount) { @@ -286,8 +286,11 @@ static List intervalVectors(BufferAllocator allocator, int rowCount vectors.add(new IntervalDayVector("intervalDay_vector", allocator)); vectors.add(new IntervalYearVector("intervalYear_vector", allocator)); vectors.add(new IntervalMonthDayNanoVector("intervalMonthDayNano_vector", allocator)); - vectors.add(new DurationVector("duration_vector", - new FieldType(true, new ArrowType.Duration(TimeUnit.SECOND), null), allocator)); + vectors.add( + new DurationVector( + "duration_vector", + new FieldType(true, new ArrowType.Duration(TimeUnit.SECOND), null), + allocator)); vectors.forEach(vec -> GenerateSampleData.generateTestData(vec, rowCount)); return vectors; } @@ -370,9 +373,11 @@ static MapVector simpleMapVector(BufferAllocator allocator) { static List decimalVector(BufferAllocator allocator, int rowCount) { List vectors = new ArrayList<>(); - vectors.add(new DecimalVector("decimal_vector", - new FieldType(true, new ArrowType.Decimal(38, 10, 128), null), - allocator)); + vectors.add( + new DecimalVector( + "decimal_vector", + new FieldType(true, new ArrowType.Decimal(38, 10, 128), null), + allocator)); vectors.forEach(vec -> generateDecimalData((DecimalVector) vec, rowCount)); return vectors; } @@ -434,5 +439,4 @@ private static void generateDecimalData(DecimalVector vector, int valueCount) { } vector.setValueCount(valueCount); } - } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/test/util/ArrowTestDataUtil.java b/java/vector/src/test/java/org/apache/arrow/vector/test/util/ArrowTestDataUtil.java index 901a09e313f..9015ae207d4 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/test/util/ArrowTestDataUtil.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/test/util/ArrowTestDataUtil.java @@ -21,9 +21,7 @@ import java.nio.file.Paths; import java.util.Objects; -/** - * Utility methods and constants for working with the arrow-testing repo. - */ +/** Utility methods and constants for working with the arrow-testing repo. */ public final class ArrowTestDataUtil { public static final String TEST_DATA_ENV_VAR = "ARROW_TEST_DATA"; public static final String TEST_DATA_PROPERTY = "arrow.test.dataRoot"; @@ -33,11 +31,13 @@ public static Path getTestDataRoot() { if (path == null) { path = System.getProperty(TEST_DATA_PROPERTY); } - return Paths.get(Objects.requireNonNull(path, - String.format("Could not find test data path. Set the environment variable %s or the JVM property %s.", - TEST_DATA_ENV_VAR, TEST_DATA_PROPERTY))); + return Paths.get( + Objects.requireNonNull( + path, + String.format( + "Could not find test data path. Set the environment variable %s or the JVM property %s.", + TEST_DATA_ENV_VAR, TEST_DATA_PROPERTY))); } - private ArrowTestDataUtil() { - } + private ArrowTestDataUtil() {} } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/testing/RandomDataGenerator.java b/java/vector/src/test/java/org/apache/arrow/vector/testing/RandomDataGenerator.java index 4b1094d288b..dff28dde76f 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/testing/RandomDataGenerator.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/testing/RandomDataGenerator.java @@ -20,9 +20,7 @@ import java.util.Random; import java.util.function.Supplier; -/** - * Utility for generating random data. - */ +/** Utility for generating random data. */ public class RandomDataGenerator { static final Random random = new Random(0); @@ -39,6 +37,5 @@ public class RandomDataGenerator { public static final Supplier DOUBLE_GENERATOR = () -> random.nextDouble(); - private RandomDataGenerator() { - } + private RandomDataGenerator() {} } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/testing/TestValueVectorPopulator.java b/java/vector/src/test/java/org/apache/arrow/vector/testing/TestValueVectorPopulator.java index 3c075c92930..cfaf752f716 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/testing/TestValueVectorPopulator.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/testing/TestValueVectorPopulator.java @@ -21,7 +21,6 @@ import static org.apache.arrow.vector.testing.ValueVectorDataPopulator.setVector; import java.nio.charset.StandardCharsets; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.BigIntVector; @@ -52,7 +51,6 @@ import org.apache.arrow.vector.VarBinaryVector; import org.apache.arrow.vector.VarCharVector; import org.apache.arrow.vector.compare.VectorEqualsVisitor; -import org.apache.arrow.vector.testing.ValueVectorDataPopulator; import org.apache.arrow.vector.types.TimeUnit; import org.apache.arrow.vector.types.pojo.ArrowType; import org.apache.arrow.vector.types.pojo.FieldType; @@ -77,7 +75,7 @@ public void terminate() throws Exception { @Test public void testPopulateBigIntVector() { try (final BigIntVector vector1 = new BigIntVector("vector", allocator); - final BigIntVector vector2 = new BigIntVector("vector", allocator)) { + final BigIntVector vector2 = new BigIntVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -97,7 +95,7 @@ public void testPopulateBigIntVector() { @Test public void testPopulateBitVector() { try (final BitVector vector1 = new BitVector("vector", allocator); - final BitVector vector2 = new BitVector("vector", allocator)) { + final BitVector vector2 = new BitVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -117,7 +115,7 @@ public void testPopulateBitVector() { @Test public void testPopulateDateDayVector() { try (final DateDayVector vector1 = new DateDayVector("vector", allocator); - final DateDayVector vector2 = new DateDayVector("vector", allocator)) { + final DateDayVector vector2 = new DateDayVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -137,7 +135,7 @@ public void testPopulateDateDayVector() { @Test public void testPopulateDateMilliVector() { try (final DateMilliVector vector1 = new DateMilliVector("vector", allocator); - final DateMilliVector vector2 = new DateMilliVector("vector", allocator)) { + final DateMilliVector vector2 = new DateMilliVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -157,7 +155,7 @@ public void testPopulateDateMilliVector() { @Test public void testPopulateDecimalVector() { try (final DecimalVector vector1 = new DecimalVector("vector", allocator, 10, 3); - final DecimalVector vector2 = new DecimalVector("vector", allocator, 10, 3)) { + final DecimalVector vector2 = new DecimalVector("vector", allocator, 10, 3)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -178,7 +176,7 @@ public void testPopulateDecimalVector() { public void testPopulateDurationVector() { final FieldType fieldType = FieldType.nullable(new ArrowType.Duration(TimeUnit.SECOND)); try (final DurationVector vector1 = new DurationVector("vector", fieldType, allocator); - final DurationVector vector2 = new DurationVector("vector", fieldType, allocator)) { + final DurationVector vector2 = new DurationVector("vector", fieldType, allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -199,7 +197,7 @@ public void testPopulateDurationVector() { @Test public void testPopulateFixedSizeBinaryVector() { try (final FixedSizeBinaryVector vector1 = new FixedSizeBinaryVector("vector", allocator, 5); - final FixedSizeBinaryVector vector2 = new FixedSizeBinaryVector("vector", allocator, 5)) { + final FixedSizeBinaryVector vector2 = new FixedSizeBinaryVector("vector", allocator, 5)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -211,9 +209,18 @@ public void testPopulateFixedSizeBinaryVector() { } vector1.setValueCount(10); - setVector(vector2, null, "test1".getBytes(StandardCharsets.UTF_8), null, - "test3".getBytes(StandardCharsets.UTF_8), null, "test5".getBytes(StandardCharsets.UTF_8), null, - "test7".getBytes(StandardCharsets.UTF_8), null, "test9".getBytes(StandardCharsets.UTF_8)); + setVector( + vector2, + null, + "test1".getBytes(StandardCharsets.UTF_8), + null, + "test3".getBytes(StandardCharsets.UTF_8), + null, + "test5".getBytes(StandardCharsets.UTF_8), + null, + "test7".getBytes(StandardCharsets.UTF_8), + null, + "test9".getBytes(StandardCharsets.UTF_8)); assertTrue(VectorEqualsVisitor.vectorEquals(vector1, vector2)); } } @@ -221,7 +228,7 @@ public void testPopulateFixedSizeBinaryVector() { @Test public void testPopulateFloat4Vector() { try (final Float4Vector vector1 = new Float4Vector("vector", allocator); - final Float4Vector vector2 = new Float4Vector("vector", allocator)) { + final Float4Vector vector2 = new Float4Vector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -240,7 +247,7 @@ public void testPopulateFloat4Vector() { @Test public void testPopulateFloat8Vector() { try (final Float8Vector vector1 = new Float8Vector("vector", allocator); - final Float8Vector vector2 = new Float8Vector("vector", allocator)) { + final Float8Vector vector2 = new Float8Vector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -259,7 +266,7 @@ public void testPopulateFloat8Vector() { @Test public void testPopulateIntVector() { try (final IntVector vector1 = new IntVector("vector", allocator); - final IntVector vector2 = new IntVector("vector", allocator)) { + final IntVector vector2 = new IntVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -279,7 +286,7 @@ public void testPopulateIntVector() { @Test public void testPopulateSmallIntVector() { try (final SmallIntVector vector1 = new SmallIntVector("vector", allocator); - final SmallIntVector vector2 = new SmallIntVector("vector", allocator)) { + final SmallIntVector vector2 = new SmallIntVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -291,8 +298,9 @@ public void testPopulateSmallIntVector() { } vector1.setValueCount(10); - ValueVectorDataPopulator.setVector(vector2, null, (short) 1, null, (short) 3, null, (short) 5, - null, (short) 7, null, (short) 9); + ValueVectorDataPopulator.setVector( + vector2, null, (short) 1, null, (short) 3, null, (short) 5, null, (short) 7, null, + (short) 9); assertTrue(VectorEqualsVisitor.vectorEquals(vector1, vector2)); } } @@ -300,7 +308,7 @@ public void testPopulateSmallIntVector() { @Test public void testPopulateIntervalDayVector() { try (final IntervalYearVector vector1 = new IntervalYearVector("vector", allocator); - final IntervalYearVector vector2 = new IntervalYearVector("vector", allocator)) { + final IntervalYearVector vector2 = new IntervalYearVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -320,7 +328,7 @@ public void testPopulateIntervalDayVector() { @Test public void testPopulateTimeMicroVector() { try (final TimeMicroVector vector1 = new TimeMicroVector("vector", allocator); - final TimeMicroVector vector2 = new TimeMicroVector("vector", allocator)) { + final TimeMicroVector vector2 = new TimeMicroVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -358,7 +366,7 @@ public void testPopulateTimeMilliVector() { @Test public void testPopulateTimeNanoVector() { try (final TimeNanoVector vector1 = new TimeNanoVector("vector", allocator); - final TimeNanoVector vector2 = new TimeNanoVector("vector", allocator)) { + final TimeNanoVector vector2 = new TimeNanoVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -377,7 +385,7 @@ public void testPopulateTimeNanoVector() { @Test public void testPopulateTimeSecVector() { try (final TimeSecVector vector1 = new TimeSecVector("vector", allocator); - final TimeSecVector vector2 = new TimeSecVector("vector", allocator)) { + final TimeSecVector vector2 = new TimeSecVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -415,7 +423,7 @@ public void testPopulateTimeStampMicroVector() { @Test public void testPopulateTimeStampMilliVector() { try (final TimeStampMilliVector vector1 = new TimeStampMilliVector("vector", allocator); - final TimeStampMilliVector vector2 = new TimeStampMilliVector("vector", allocator)) { + final TimeStampMilliVector vector2 = new TimeStampMilliVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -453,7 +461,7 @@ public void testPopulateTimeStampNanoVector() { @Test public void testPopulateTimeStampSecVector() { try (final TimeStampSecVector vector1 = new TimeStampSecVector("vector", allocator); - final TimeStampSecVector vector2 = new TimeStampSecVector("vector", allocator)) { + final TimeStampSecVector vector2 = new TimeStampSecVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -472,7 +480,7 @@ public void testPopulateTimeStampSecVector() { @Test public void testPopulateTinyIntVector() { try (final TinyIntVector vector1 = new TinyIntVector("vector", allocator); - final TinyIntVector vector2 = new TinyIntVector("vector", allocator)) { + final TinyIntVector vector2 = new TinyIntVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -483,7 +491,8 @@ public void testPopulateTinyIntVector() { } } vector1.setValueCount(10); - setVector(vector2, null, (byte) 1, null, (byte) 3, null, (byte) 5, null, (byte) 7, null, (byte) 9); + setVector( + vector2, null, (byte) 1, null, (byte) 3, null, (byte) 5, null, (byte) 7, null, (byte) 9); assertTrue(VectorEqualsVisitor.vectorEquals(vector1, vector2)); } } @@ -491,7 +500,7 @@ public void testPopulateTinyIntVector() { @Test public void testPopulateUInt1Vector() { try (final UInt1Vector vector1 = new UInt1Vector("vector", allocator); - final UInt1Vector vector2 = new UInt1Vector("vector", allocator)) { + final UInt1Vector vector2 = new UInt1Vector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -502,7 +511,8 @@ public void testPopulateUInt1Vector() { } } vector1.setValueCount(10); - setVector(vector2, null, (byte) 1, null, (byte) 3, null, (byte) 5, null, (byte) 7, null, (byte) 9); + setVector( + vector2, null, (byte) 1, null, (byte) 3, null, (byte) 5, null, (byte) 7, null, (byte) 9); assertTrue(VectorEqualsVisitor.vectorEquals(vector1, vector2)); } } @@ -510,7 +520,7 @@ public void testPopulateUInt1Vector() { @Test public void testPopulateUInt2Vector() { try (final UInt2Vector vector1 = new UInt2Vector("vector", allocator); - final UInt2Vector vector2 = new UInt2Vector("vector", allocator)) { + final UInt2Vector vector2 = new UInt2Vector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -521,7 +531,8 @@ public void testPopulateUInt2Vector() { } } vector1.setValueCount(10); - setVector(vector2, null, (char) 1, null, (char) 3, null, (char) 5, null, (char) 7, null, (char) 9); + setVector( + vector2, null, (char) 1, null, (char) 3, null, (char) 5, null, (char) 7, null, (char) 9); assertTrue(VectorEqualsVisitor.vectorEquals(vector1, vector2)); } } @@ -529,7 +540,7 @@ public void testPopulateUInt2Vector() { @Test public void testPopulateUInt4Vector() { try (final UInt4Vector vector1 = new UInt4Vector("vector", allocator); - final UInt4Vector vector2 = new UInt4Vector("vector", allocator)) { + final UInt4Vector vector2 = new UInt4Vector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -567,7 +578,7 @@ public void testPopulateUInt8Vector() { @Test public void testPopulateVarBinaryVector() { try (final VarBinaryVector vector1 = new VarBinaryVector("vector", allocator); - final VarBinaryVector vector2 = new VarBinaryVector("vector", allocator)) { + final VarBinaryVector vector2 = new VarBinaryVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { @@ -579,9 +590,18 @@ public void testPopulateVarBinaryVector() { } vector1.setValueCount(10); - setVector(vector2, null, "test1".getBytes(StandardCharsets.UTF_8), null, - "test3".getBytes(StandardCharsets.UTF_8), null, "test5".getBytes(StandardCharsets.UTF_8), null, - "test7".getBytes(StandardCharsets.UTF_8), null, "test9".getBytes(StandardCharsets.UTF_8)); + setVector( + vector2, + null, + "test1".getBytes(StandardCharsets.UTF_8), + null, + "test3".getBytes(StandardCharsets.UTF_8), + null, + "test5".getBytes(StandardCharsets.UTF_8), + null, + "test7".getBytes(StandardCharsets.UTF_8), + null, + "test9".getBytes(StandardCharsets.UTF_8)); assertTrue(VectorEqualsVisitor.vectorEquals(vector1, vector2)); } } @@ -589,7 +609,7 @@ public void testPopulateVarBinaryVector() { @Test public void testPopulateVarCharVector() { try (final VarCharVector vector1 = new VarCharVector("vector", allocator); - final VarCharVector vector2 = new VarCharVector("vector", allocator)) { + final VarCharVector vector2 = new VarCharVector("vector", allocator)) { vector1.allocateNew(10); for (int i = 0; i < 10; i++) { diff --git a/java/vector/src/test/java/org/apache/arrow/vector/testing/ValueVectorDataPopulator.java b/java/vector/src/test/java/org/apache/arrow/vector/testing/ValueVectorDataPopulator.java index f9f0357861c..427df2ec180 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/testing/ValueVectorDataPopulator.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/testing/ValueVectorDataPopulator.java @@ -24,7 +24,6 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; - import org.apache.arrow.vector.BigIntVector; import org.apache.arrow.vector.BitVector; import org.apache.arrow.vector.BitVectorHelper; @@ -70,16 +69,12 @@ import org.apache.arrow.vector.types.Types.MinorType; import org.apache.arrow.vector.types.pojo.FieldType; -/** - * Utility for populating {@link org.apache.arrow.vector.ValueVector}. - */ +/** Utility for populating {@link org.apache.arrow.vector.ValueVector}. */ public class ValueVectorDataPopulator { - private ValueVectorDataPopulator(){} + private ValueVectorDataPopulator() {} - /** - * Populate values for BigIntVector. - */ + /** Populate values for BigIntVector. */ public static void setVector(BigIntVector vector, Long... values) { final int length = values.length; vector.allocateNew(length); @@ -91,9 +86,7 @@ public static void setVector(BigIntVector vector, Long... values) { vector.setValueCount(length); } - /** - * Populate values for BitVector. - */ + /** Populate values for BitVector. */ public static void setVector(BitVector vector, Integer... values) { final int length = values.length; vector.allocateNew(length); @@ -107,6 +100,7 @@ public static void setVector(BitVector vector, Integer... values) { /** * Populate values for DateDayVector. + * * @param values numbers of days since UNIX epoch */ public static void setVector(DateDayVector vector, Integer... values) { @@ -122,6 +116,7 @@ public static void setVector(DateDayVector vector, Integer... values) { /** * Populate values for DateMilliVector. + * * @param values numbers of milliseconds since UNIX epoch */ public static void setVector(DateMilliVector vector, Long... values) { @@ -135,9 +130,7 @@ public static void setVector(DateMilliVector vector, Long... values) { vector.setValueCount(length); } - /** - * Populate values for DecimalVector. - */ + /** Populate values for DecimalVector. */ public static void setVector(DecimalVector vector, Long... values) { final int length = values.length; vector.allocateNew(length); @@ -149,9 +142,7 @@ public static void setVector(DecimalVector vector, Long... values) { vector.setValueCount(length); } - /** - * Populate values for Decimal256Vector. - */ + /** Populate values for Decimal256Vector. */ public static void setVector(Decimal256Vector vector, Long... values) { final int length = values.length; vector.allocateNew(length); @@ -163,9 +154,7 @@ public static void setVector(Decimal256Vector vector, Long... values) { vector.setValueCount(length); } - /** - * Populate values for Decimal256Vector. - */ + /** Populate values for Decimal256Vector. */ public static void setVector(Decimal256Vector vector, BigDecimal... values) { final int length = values.length; vector.allocateNew(length); @@ -179,7 +168,9 @@ public static void setVector(Decimal256Vector vector, BigDecimal... values) { /** * Populate values for DurationVector. - * @param values values of elapsed time in either seconds, milliseconds, microseconds or nanoseconds. + * + * @param values values of elapsed time in either seconds, milliseconds, microseconds or + * nanoseconds. */ public static void setVector(DurationVector vector, Long... values) { final int length = values.length; @@ -192,9 +183,7 @@ public static void setVector(DurationVector vector, Long... values) { vector.setValueCount(length); } - /** - * Populate values for FixedSizeBinaryVector. - */ + /** Populate values for FixedSizeBinaryVector. */ public static void setVector(FixedSizeBinaryVector vector, byte[]... values) { final int length = values.length; vector.allocateNewSafe(); @@ -206,9 +195,7 @@ public static void setVector(FixedSizeBinaryVector vector, byte[]... values) { vector.setValueCount(length); } - /** - * Populate values for Float4Vector. - */ + /** Populate values for Float4Vector. */ public static void setVector(Float4Vector vector, Float... values) { final int length = values.length; vector.allocateNew(length); @@ -220,9 +207,7 @@ public static void setVector(Float4Vector vector, Float... values) { vector.setValueCount(length); } - /** - * Populate values for Float8Vector. - */ + /** Populate values for Float8Vector. */ public static void setVector(Float8Vector vector, Double... values) { final int length = values.length; vector.allocateNew(length); @@ -234,9 +219,7 @@ public static void setVector(Float8Vector vector, Double... values) { vector.setValueCount(length); } - /** - * Populate values for IntVector. - */ + /** Populate values for IntVector. */ public static void setVector(IntVector vector, Integer... values) { final int length = values.length; vector.allocateNew(length); @@ -250,7 +233,9 @@ public static void setVector(IntVector vector, Integer... values) { /** * Populate values for IntervalDayVector. - * @param values holders witch holds days and milliseconds values which represents interval in SQL style. + * + * @param values holders witch holds days and milliseconds values which represents interval in SQL + * style. */ public static void setVector(IntervalDayVector vector, IntervalDayHolder... values) { final int length = values.length; @@ -265,6 +250,7 @@ public static void setVector(IntervalDayVector vector, IntervalDayHolder... valu /** * Populate values for IntervalYearVector. + * * @param values total month intervals in SQL style. */ public static void setVector(IntervalYearVector vector, Integer... values) { @@ -278,9 +264,7 @@ public static void setVector(IntervalYearVector vector, Integer... values) { vector.setValueCount(length); } - /** - * Populate values for SmallIntVector. - */ + /** Populate values for SmallIntVector. */ public static void setVector(SmallIntVector vector, Short... values) { final int length = values.length; vector.allocateNew(length); @@ -294,6 +278,7 @@ public static void setVector(SmallIntVector vector, Short... values) { /** * Populate values for TimeMicroVector. + * * @param values numbers of microseconds since UNIX epoch */ public static void setVector(TimeMicroVector vector, Long... values) { @@ -309,6 +294,7 @@ public static void setVector(TimeMicroVector vector, Long... values) { /** * Populate values for TimeMicroVector. + * * @param values numbers of milliseconds since UNIX epoch */ public static void setVector(TimeMilliVector vector, Integer... values) { @@ -324,6 +310,7 @@ public static void setVector(TimeMilliVector vector, Integer... values) { /** * Populate values for TimeNanoVector. + * * @param values numbers of nanoseconds since UNIX epoch */ public static void setVector(TimeNanoVector vector, Long... values) { @@ -339,6 +326,7 @@ public static void setVector(TimeNanoVector vector, Long... values) { /** * Populate values for TimeSecVector. + * * @param values numbers of seconds since UNIX epoch */ public static void setVector(TimeSecVector vector, Integer... values) { @@ -354,6 +342,7 @@ public static void setVector(TimeSecVector vector, Integer... values) { /** * Populate values for TimeStampMicroTZVector. + * * @param values numbers of microseconds since UNIX epoch */ public static void setVector(TimeStampMicroTZVector vector, Long... values) { @@ -369,6 +358,7 @@ public static void setVector(TimeStampMicroTZVector vector, Long... values) { /** * Populate values for TimeStampMicroVector. + * * @param values numbers of microseconds since UNIX epoch */ public static void setVector(TimeStampMicroVector vector, Long... values) { @@ -384,6 +374,7 @@ public static void setVector(TimeStampMicroVector vector, Long... values) { /** * Populate values for TimeStampMilliTZVector. + * * @param values numbers of milliseconds since UNIX epoch */ public static void setVector(TimeStampMilliTZVector vector, Long... values) { @@ -399,6 +390,7 @@ public static void setVector(TimeStampMilliTZVector vector, Long... values) { /** * Populate values for TimeStampMilliVector. + * * @param values numbers of milliseconds since UNIX epoch */ public static void setVector(TimeStampMilliVector vector, Long... values) { @@ -414,6 +406,7 @@ public static void setVector(TimeStampMilliVector vector, Long... values) { /** * Populate values for TimeStampNanoTZVector. + * * @param values numbers of nanoseconds since UNIX epoch */ public static void setVector(TimeStampNanoTZVector vector, Long... values) { @@ -429,6 +422,7 @@ public static void setVector(TimeStampNanoTZVector vector, Long... values) { /** * Populate values for TimeStampNanoVector. + * * @param values numbers of nanoseconds since UNIX epoch */ public static void setVector(TimeStampNanoVector vector, Long... values) { @@ -444,6 +438,7 @@ public static void setVector(TimeStampNanoVector vector, Long... values) { /** * Populate values for TimeStampSecTZVector. + * * @param values numbers of seconds since UNIX epoch */ public static void setVector(TimeStampSecTZVector vector, Long... values) { @@ -459,6 +454,7 @@ public static void setVector(TimeStampSecTZVector vector, Long... values) { /** * Populate values for TimeStampSecVector. + * * @param values numbers of seconds since UNIX epoch */ public static void setVector(TimeStampSecVector vector, Long... values) { @@ -472,9 +468,7 @@ public static void setVector(TimeStampSecVector vector, Long... values) { vector.setValueCount(length); } - /** - * Populate values for TinyIntVector. - */ + /** Populate values for TinyIntVector. */ public static void setVector(TinyIntVector vector, Byte... values) { final int length = values.length; vector.allocateNew(length); @@ -486,9 +480,7 @@ public static void setVector(TinyIntVector vector, Byte... values) { vector.setValueCount(length); } - /** - * Populate values for UInt1Vector. - */ + /** Populate values for UInt1Vector. */ public static void setVector(UInt1Vector vector, Byte... values) { final int length = values.length; vector.allocateNew(length); @@ -500,9 +492,7 @@ public static void setVector(UInt1Vector vector, Byte... values) { vector.setValueCount(length); } - /** - * Populate values for UInt2Vector. - */ + /** Populate values for UInt2Vector. */ public static void setVector(UInt2Vector vector, Character... values) { final int length = values.length; vector.allocateNew(length); @@ -514,9 +504,7 @@ public static void setVector(UInt2Vector vector, Character... values) { vector.setValueCount(length); } - /** - * Populate values for UInt4Vector. - */ + /** Populate values for UInt4Vector. */ public static void setVector(UInt4Vector vector, Integer... values) { final int length = values.length; vector.allocateNew(length); @@ -528,9 +516,7 @@ public static void setVector(UInt4Vector vector, Integer... values) { vector.setValueCount(length); } - /** - * Populate values for UInt8Vector. - */ + /** Populate values for UInt8Vector. */ public static void setVector(UInt8Vector vector, Long... values) { final int length = values.length; vector.allocateNew(length); @@ -542,9 +528,7 @@ public static void setVector(UInt8Vector vector, Long... values) { vector.setValueCount(length); } - /** - * Populate values for VarBinaryVector. - */ + /** Populate values for VarBinaryVector. */ public static void setVector(VarBinaryVector vector, byte[]... values) { final int length = values.length; vector.allocateNewSafe(); @@ -556,9 +540,7 @@ public static void setVector(VarBinaryVector vector, byte[]... values) { vector.setValueCount(length); } - /** - * Populate values for VarCharVector. - */ + /** Populate values for VarCharVector. */ public static void setVector(VarCharVector vector, byte[]... values) { final int length = values.length; vector.allocateNewSafe(); @@ -570,9 +552,7 @@ public static void setVector(VarCharVector vector, byte[]... values) { vector.setValueCount(length); } - /** - * Populate values for LargeVarCharVector. - */ + /** Populate values for LargeVarCharVector. */ public static void setVector(LargeVarCharVector vector, byte[]... values) { final int length = values.length; vector.allocateNewSafe(); @@ -584,9 +564,7 @@ public static void setVector(LargeVarCharVector vector, byte[]... values) { vector.setValueCount(length); } - /** - * Populate values for VarCharVector. - */ + /** Populate values for VarCharVector. */ public static void setVector(VarCharVector vector, String... values) { final int length = values.length; vector.allocateNewSafe(); @@ -598,9 +576,7 @@ public static void setVector(VarCharVector vector, String... values) { vector.setValueCount(length); } - /** - * Populate values for LargeVarCharVector. - */ + /** Populate values for LargeVarCharVector. */ public static void setVector(LargeVarCharVector vector, String... values) { final int length = values.length; vector.allocateNewSafe(); @@ -612,9 +588,7 @@ public static void setVector(LargeVarCharVector vector, String... values) { vector.setValueCount(length); } - /** - * Populate values for {@link ListVector}. - */ + /** Populate values for {@link ListVector}. */ public static void setVector(ListVector vector, List... values) { vector.allocateNewSafe(); Types.MinorType type = Types.MinorType.INT; @@ -643,9 +617,7 @@ public static void setVector(ListVector vector, List... values) { vector.setValueCount(values.length); } - /** - * Populate values for {@link LargeListVector}. - */ + /** Populate values for {@link LargeListVector}. */ public static void setVector(LargeListVector vector, List... values) { vector.allocateNewSafe(); Types.MinorType type = Types.MinorType.INT; @@ -674,9 +646,7 @@ public static void setVector(LargeListVector vector, List... values) { vector.setValueCount(values.length); } - /** - * Populate values for {@link FixedSizeListVector}. - */ + /** Populate values for {@link FixedSizeListVector}. */ public static void setVector(FixedSizeListVector vector, List... values) { vector.allocateNewSafe(); for (int i = 0; i < values.length; i++) { @@ -708,17 +678,16 @@ public static void setVector(FixedSizeListVector vector, List... values vector.setValueCount(values.length); } - /** - * Populate values for {@link StructVector}. - */ + /** Populate values for {@link StructVector}. */ public static void setVector(StructVector vector, Map> values) { vector.allocateNewSafe(); int valueCount = 0; for (final Entry> entry : values.entrySet()) { // Add the child - final IntVector child = vector.addOrGet(entry.getKey(), - FieldType.nullable(MinorType.INT.getType()), IntVector.class); + final IntVector child = + vector.addOrGet( + entry.getKey(), FieldType.nullable(MinorType.INT.getType()), IntVector.class); // Write the values to the child child.allocateNew(); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestExtensionType.java b/java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestExtensionType.java index 872b2f3934b..4babc42d9c5 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestExtensionType.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestExtensionType.java @@ -33,7 +33,6 @@ import java.nio.file.StandardOpenOption; import java.util.Collections; import java.util.UUID; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.memory.util.hash.ArrowBufHasher; @@ -55,13 +54,12 @@ import org.junit.Test; public class TestExtensionType { - /** - * Test that a custom UUID type can be round-tripped through a temporary file. - */ + /** Test that a custom UUID type can be round-tripped through a temporary file. */ @Test public void roundtripUuid() throws IOException { ExtensionTypeRegistry.register(new UuidType()); - final Schema schema = new Schema(Collections.singletonList(Field.nullable("a", new UuidType()))); + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("a", new UuidType()))); try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { UUID u1 = UUID.randomUUID(); @@ -73,15 +71,16 @@ public void roundtripUuid() throws IOException { root.setRowCount(2); final File file = File.createTempFile("uuidtest", ".arrow"); - try (final WritableByteChannel channel = FileChannel - .open(Paths.get(file.getAbsolutePath()), StandardOpenOption.WRITE); + try (final WritableByteChannel channel = + FileChannel.open(Paths.get(file.getAbsolutePath()), StandardOpenOption.WRITE); final ArrowFileWriter writer = new ArrowFileWriter(root, null, channel)) { writer.start(); writer.writeBatch(); writer.end(); } - try (final SeekableByteChannel channel = Files.newByteChannel(Paths.get(file.getAbsolutePath())); + try (final SeekableByteChannel channel = + Files.newByteChannel(Paths.get(file.getAbsolutePath())); final ArrowFileReader reader = new ArrowFileReader(channel, allocator)) { reader.loadNextBatch(); final VectorSchemaRoot readerRoot = reader.getVectorSchemaRoot(); @@ -89,12 +88,15 @@ public void roundtripUuid() throws IOException { final Field field = readerRoot.getSchema().getFields().get(0); final UuidType expectedType = new UuidType(); - Assert.assertEquals(field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_NAME), + Assert.assertEquals( + field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_NAME), expectedType.extensionName()); - Assert.assertEquals(field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_METADATA), + Assert.assertEquals( + field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_METADATA), expectedType.serialize()); - final ExtensionTypeVector deserialized = (ExtensionTypeVector) readerRoot.getFieldVectors().get(0); + final ExtensionTypeVector deserialized = + (ExtensionTypeVector) readerRoot.getFieldVectors().get(0); Assert.assertEquals(vector.getValueCount(), deserialized.getValueCount()); for (int i = 0; i < vector.getValueCount(); i++) { Assert.assertEquals(vector.isNull(i), deserialized.isNull(i)); @@ -106,13 +108,12 @@ public void roundtripUuid() throws IOException { } } - /** - * Test that a custom UUID type can be read as its underlying type. - */ + /** Test that a custom UUID type can be read as its underlying type. */ @Test public void readUnderlyingType() throws IOException { ExtensionTypeRegistry.register(new UuidType()); - final Schema schema = new Schema(Collections.singletonList(Field.nullable("a", new UuidType()))); + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("a", new UuidType()))); try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { UUID u1 = UUID.randomUUID(); @@ -124,8 +125,8 @@ public void readUnderlyingType() throws IOException { root.setRowCount(2); final File file = File.createTempFile("uuidtest", ".arrow"); - try (final WritableByteChannel channel = FileChannel - .open(Paths.get(file.getAbsolutePath()), StandardOpenOption.WRITE); + try (final WritableByteChannel channel = + FileChannel.open(Paths.get(file.getAbsolutePath()), StandardOpenOption.WRITE); final ArrowFileWriter writer = new ArrowFileWriter(root, null, channel)) { writer.start(); writer.writeBatch(); @@ -134,24 +135,32 @@ public void readUnderlyingType() throws IOException { ExtensionTypeRegistry.unregister(new UuidType()); - try (final SeekableByteChannel channel = Files.newByteChannel(Paths.get(file.getAbsolutePath())); + try (final SeekableByteChannel channel = + Files.newByteChannel(Paths.get(file.getAbsolutePath())); final ArrowFileReader reader = new ArrowFileReader(channel, allocator)) { reader.loadNextBatch(); final VectorSchemaRoot readerRoot = reader.getVectorSchemaRoot(); Assert.assertEquals(1, readerRoot.getSchema().getFields().size()); Assert.assertEquals("a", readerRoot.getSchema().getFields().get(0).getName()); - Assert.assertTrue(readerRoot.getSchema().getFields().get(0).getType() instanceof ArrowType.FixedSizeBinary); - Assert.assertEquals(16, - ((ArrowType.FixedSizeBinary) readerRoot.getSchema().getFields().get(0).getType()).getByteWidth()); + Assert.assertTrue( + readerRoot.getSchema().getFields().get(0).getType() + instanceof ArrowType.FixedSizeBinary); + Assert.assertEquals( + 16, + ((ArrowType.FixedSizeBinary) readerRoot.getSchema().getFields().get(0).getType()) + .getByteWidth()); final Field field = readerRoot.getSchema().getFields().get(0); final UuidType expectedType = new UuidType(); - Assert.assertEquals(field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_NAME), + Assert.assertEquals( + field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_NAME), expectedType.extensionName()); - Assert.assertEquals(field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_METADATA), + Assert.assertEquals( + field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_METADATA), expectedType.serialize()); - final FixedSizeBinaryVector deserialized = (FixedSizeBinaryVector) readerRoot.getFieldVectors().get(0); + final FixedSizeBinaryVector deserialized = + (FixedSizeBinaryVector) readerRoot.getFieldVectors().get(0); Assert.assertEquals(vector.getValueCount(), deserialized.getValueCount()); for (int i = 0; i < vector.getValueCount(); i++) { Assert.assertEquals(vector.isNull(i), deserialized.isNull(i)); @@ -169,26 +178,27 @@ public void readUnderlyingType() throws IOException { @Test public void testNullCheck() { - NullPointerException e = assertThrows(NullPointerException.class, - () -> { - try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - final ExtensionTypeVector vector = new UuidVector("uuid", allocator, null)) { - vector.getField(); - vector.allocateNewSafe(); - } - }); + NullPointerException e = + assertThrows( + NullPointerException.class, + () -> { + try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); + final ExtensionTypeVector vector = new UuidVector("uuid", allocator, null)) { + vector.getField(); + vector.allocateNewSafe(); + } + }); assertTrue(e.getMessage().contains("underlyingVector cannot be null.")); } - /** - * Test that a custom Location type can be round-tripped through a temporary file. - */ + /** Test that a custom Location type can be round-tripped through a temporary file. */ @Test public void roundtripLocation() throws IOException { ExtensionTypeRegistry.register(new LocationType()); - final Schema schema = new Schema(Collections.singletonList(Field.nullable("location", new LocationType()))); + final Schema schema = + new Schema(Collections.singletonList(Field.nullable("location", new LocationType()))); try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { + final VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { LocationVector vector = (LocationVector) root.getVector("location"); vector.allocateNew(); vector.set(0, 34.073814f, -118.240784f); @@ -198,28 +208,32 @@ public void roundtripLocation() throws IOException { root.setRowCount(4); final File file = File.createTempFile("locationtest", ".arrow"); - try (final WritableByteChannel channel = FileChannel - .open(Paths.get(file.getAbsolutePath()), StandardOpenOption.WRITE); - final ArrowFileWriter writer = new ArrowFileWriter(root, null, channel)) { + try (final WritableByteChannel channel = + FileChannel.open(Paths.get(file.getAbsolutePath()), StandardOpenOption.WRITE); + final ArrowFileWriter writer = new ArrowFileWriter(root, null, channel)) { writer.start(); writer.writeBatch(); writer.end(); } - try (final SeekableByteChannel channel = Files.newByteChannel(Paths.get(file.getAbsolutePath())); - final ArrowFileReader reader = new ArrowFileReader(channel, allocator)) { + try (final SeekableByteChannel channel = + Files.newByteChannel(Paths.get(file.getAbsolutePath())); + final ArrowFileReader reader = new ArrowFileReader(channel, allocator)) { reader.loadNextBatch(); final VectorSchemaRoot readerRoot = reader.getVectorSchemaRoot(); Assert.assertEquals(root.getSchema(), readerRoot.getSchema()); final Field field = readerRoot.getSchema().getFields().get(0); final LocationType expectedType = new LocationType(); - Assert.assertEquals(field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_NAME), - expectedType.extensionName()); - Assert.assertEquals(field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_METADATA), - expectedType.serialize()); + Assert.assertEquals( + field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_NAME), + expectedType.extensionName()); + Assert.assertEquals( + field.getMetadata().get(ExtensionType.EXTENSION_METADATA_KEY_METADATA), + expectedType.serialize()); - final ExtensionTypeVector deserialized = (ExtensionTypeVector) readerRoot.getFieldVectors().get(0); + final ExtensionTypeVector deserialized = + (ExtensionTypeVector) readerRoot.getFieldVectors().get(0); Assert.assertTrue(deserialized instanceof LocationVector); Assert.assertEquals("location", deserialized.getName()); StructVector deserStruct = (StructVector) deserialized.getUnderlyingVector(); @@ -241,10 +255,12 @@ public void testVectorCompare() { UuidType uuidType = new UuidType(); ExtensionTypeRegistry.register(uuidType); try (final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); - UuidVector a1 = (UuidVector) uuidType.getNewVector("a", FieldType.nullable(uuidType), allocator); - UuidVector a2 = (UuidVector) uuidType.getNewVector("a", FieldType.nullable(uuidType), allocator); - UuidVector bb = (UuidVector) uuidType.getNewVector("a", FieldType.nullable(uuidType), allocator) - ) { + UuidVector a1 = + (UuidVector) uuidType.getNewVector("a", FieldType.nullable(uuidType), allocator); + UuidVector a2 = + (UuidVector) uuidType.getNewVector("a", FieldType.nullable(uuidType), allocator); + UuidVector bb = + (UuidVector) uuidType.getNewVector("a", FieldType.nullable(uuidType), allocator)) { UUID u1 = UUID.randomUUID(); UUID u2 = UUID.randomUUID(); @@ -298,7 +314,8 @@ public boolean extensionEquals(ExtensionType other) { @Override public ArrowType deserialize(ArrowType storageType, String serializedData) { if (!storageType.equals(storageType())) { - throw new UnsupportedOperationException("Cannot construct UuidType from underlying type " + storageType); + throw new UnsupportedOperationException( + "Cannot construct UuidType from underlying type " + storageType); } return new UuidType(); } @@ -316,7 +333,8 @@ public FieldVector getNewVector(String name, FieldType fieldType, BufferAllocato static class UuidVector extends ExtensionTypeVector { - public UuidVector(String name, BufferAllocator allocator, FixedSizeBinaryVector underlyingVector) { + public UuidVector( + String name, BufferAllocator allocator, FixedSizeBinaryVector underlyingVector) { super(name, allocator, underlyingVector); } @@ -364,7 +382,8 @@ public boolean extensionEquals(ExtensionType other) { @Override public ArrowType deserialize(ArrowType storageType, String serializedData) { if (!storageType.equals(storageType())) { - throw new UnsupportedOperationException("Cannot construct LocationType from underlying type " + storageType); + throw new UnsupportedOperationException( + "Cannot construct LocationType from underlying type " + storageType); } return new LocationType(); } @@ -384,11 +403,15 @@ public static class LocationVector extends ExtensionTypeVector { private static StructVector buildUnderlyingVector(String name, BufferAllocator allocator) { final StructVector underlyingVector = - new StructVector(name, allocator, FieldType.nullable(ArrowType.Struct.INSTANCE), null); - underlyingVector.addOrGet("Latitude", - FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)), Float4Vector.class); - underlyingVector.addOrGet("Longitude", - FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)), Float4Vector.class); + new StructVector(name, allocator, FieldType.nullable(ArrowType.Struct.INSTANCE), null); + underlyingVector.addOrGet( + "Latitude", + FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)), + Float4Vector.class); + underlyingVector.addOrGet( + "Longitude", + FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE)), + Float4Vector.class); return underlyingVector; } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestField.java b/java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestField.java index bc984fa642d..acb697dc061 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestField.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestField.java @@ -26,13 +26,13 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; - import org.apache.arrow.vector.types.pojo.ArrowType.Int; import org.junit.Test; public class TestField { - private static Field field(String name, boolean nullable, ArrowType type, Map metadata) { + private static Field field( + String name, boolean nullable, ArrowType type, Map metadata) { return new Field(name, new FieldType(nullable, type, null, metadata), Collections.emptyList()); } @@ -41,14 +41,16 @@ public void testMetadata() throws IOException { Map metadata = new HashMap<>(1); metadata.put("testKey", "testValue"); - Schema schema = new Schema(Collections.singletonList( - field("a", false, new Int(8, true), metadata) - )); + Schema schema = + new Schema(Collections.singletonList(field("a", false, new Int(8, true), metadata))); String json = schema.toJson(); Schema actual = Schema.fromJSON(json); - jsonContains(json, "\"" + METADATA_KEY + "\" : \"testKey\"", "\"" + METADATA_VALUE + "\" : \"testValue\""); + jsonContains( + json, + "\"" + METADATA_KEY + "\" : \"testKey\"", + "\"" + METADATA_VALUE + "\" : \"testValue\""); Map actualMetadata = actual.getFields().get(0).getMetadata(); assertEquals(1, actualMetadata.size()); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestSchema.java b/java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestSchema.java index 7b62247c6e1..580f9411d72 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestSchema.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestSchema.java @@ -27,7 +27,6 @@ import java.nio.ByteBuffer; import java.util.HashMap; import java.util.Map; - import org.apache.arrow.vector.types.DateUnit; import org.apache.arrow.vector.types.FloatingPointPrecision; import org.apache.arrow.vector.types.IntervalUnit; @@ -63,84 +62,87 @@ private static Field field(String name, ArrowType type, Field... children) { @Test public void testComplex() throws IOException { - Schema schema = new Schema(asList( - field("a", false, new Int(8, true)), - field("b", new Struct(), - field("c", new Int(16, true)), - field("d", new Utf8())), - field("e", new List(), field(null, new Date(DateUnit.MILLISECOND))), - field("f", new FloatingPoint(FloatingPointPrecision.SINGLE)), - field("g", new Timestamp(TimeUnit.MILLISECOND, "UTC")), - field("h", new Timestamp(TimeUnit.MICROSECOND, null)), - field("i", new Interval(IntervalUnit.DAY_TIME)), - field("j", new ArrowType.Duration(TimeUnit.SECOND)) - )); + Schema schema = + new Schema( + asList( + field("a", false, new Int(8, true)), + field("b", new Struct(), field("c", new Int(16, true)), field("d", new Utf8())), + field("e", new List(), field(null, new Date(DateUnit.MILLISECOND))), + field("f", new FloatingPoint(FloatingPointPrecision.SINGLE)), + field("g", new Timestamp(TimeUnit.MILLISECOND, "UTC")), + field("h", new Timestamp(TimeUnit.MICROSECOND, null)), + field("i", new Interval(IntervalUnit.DAY_TIME)), + field("j", new ArrowType.Duration(TimeUnit.SECOND)))); roundTrip(schema); assertEquals( - "Schema, e: List, " + - "f: FloatingPoint(SINGLE), g: Timestamp(MILLISECOND, UTC), h: Timestamp(MICROSECOND, null), " + - "i: Interval(DAY_TIME), j: Duration(SECOND)>", + "Schema, e: List, " + + "f: FloatingPoint(SINGLE), g: Timestamp(MILLISECOND, UTC), h: Timestamp(MICROSECOND, null), " + + "i: Interval(DAY_TIME), j: Duration(SECOND)>", schema.toString()); } @Test public void testAll() throws IOException { - Schema schema = new Schema(asList( - field("a", false, new Null()), - field("b", new Struct(), field("ba", new Null())), - field("c", new List(), field("ca", new Null())), - field("d", new Union(UnionMode.Sparse, new int[] {1, 2, 3}), field("da", new Null())), - field("e", new Int(8, true)), - field("f", new FloatingPoint(FloatingPointPrecision.SINGLE)), - field("g", new Utf8()), - field("h", new Binary()), - field("i", new Bool()), - field("j", new Decimal(5, 5, 128)), - field("k", new Date(DateUnit.DAY)), - field("l", new Date(DateUnit.MILLISECOND)), - field("m", new Time(TimeUnit.SECOND, 32)), - field("n", new Time(TimeUnit.MILLISECOND, 32)), - field("o", new Time(TimeUnit.MICROSECOND, 64)), - field("p", new Time(TimeUnit.NANOSECOND, 64)), - field("q", new Timestamp(TimeUnit.MILLISECOND, "UTC")), - field("r", new Timestamp(TimeUnit.MICROSECOND, null)), - field("s", new Interval(IntervalUnit.DAY_TIME)), - field("t", new FixedSizeBinary(100)), - field("u", new Duration(TimeUnit.SECOND)), - field("v", new Duration(TimeUnit.MICROSECOND)) - )); + Schema schema = + new Schema( + asList( + field("a", false, new Null()), + field("b", new Struct(), field("ba", new Null())), + field("c", new List(), field("ca", new Null())), + field( + "d", new Union(UnionMode.Sparse, new int[] {1, 2, 3}), field("da", new Null())), + field("e", new Int(8, true)), + field("f", new FloatingPoint(FloatingPointPrecision.SINGLE)), + field("g", new Utf8()), + field("h", new Binary()), + field("i", new Bool()), + field("j", new Decimal(5, 5, 128)), + field("k", new Date(DateUnit.DAY)), + field("l", new Date(DateUnit.MILLISECOND)), + field("m", new Time(TimeUnit.SECOND, 32)), + field("n", new Time(TimeUnit.MILLISECOND, 32)), + field("o", new Time(TimeUnit.MICROSECOND, 64)), + field("p", new Time(TimeUnit.NANOSECOND, 64)), + field("q", new Timestamp(TimeUnit.MILLISECOND, "UTC")), + field("r", new Timestamp(TimeUnit.MICROSECOND, null)), + field("s", new Interval(IntervalUnit.DAY_TIME)), + field("t", new FixedSizeBinary(100)), + field("u", new Duration(TimeUnit.SECOND)), + field("v", new Duration(TimeUnit.MICROSECOND)))); roundTrip(schema); } @Test public void testUnion() throws IOException { - Schema schema = new Schema(asList( - field("d", new Union(UnionMode.Sparse, new int[] {1, 2, 3}), field("da", new Null())) - )); + Schema schema = + new Schema( + asList( + field( + "d", + new Union(UnionMode.Sparse, new int[] {1, 2, 3}), + field("da", new Null())))); roundTrip(schema); contains(schema, "Sparse"); } @Test public void testDate() throws IOException { - Schema schema = new Schema(asList( - field("a", new Date(DateUnit.DAY)), - field("b", new Date(DateUnit.MILLISECOND)) - )); + Schema schema = + new Schema( + asList(field("a", new Date(DateUnit.DAY)), field("b", new Date(DateUnit.MILLISECOND)))); roundTrip(schema); - assertEquals( - "Schema", - schema.toString()); + assertEquals("Schema", schema.toString()); } @Test public void testTime() throws IOException { - Schema schema = new Schema(asList( - field("a", new Time(TimeUnit.SECOND, 32)), - field("b", new Time(TimeUnit.MILLISECOND, 32)), - field("c", new Time(TimeUnit.MICROSECOND, 64)), - field("d", new Time(TimeUnit.NANOSECOND, 64)) - )); + Schema schema = + new Schema( + asList( + field("a", new Time(TimeUnit.SECOND, 32)), + field("b", new Time(TimeUnit.MILLISECOND, 32)), + field("c", new Time(TimeUnit.MICROSECOND, 64)), + field("d", new Time(TimeUnit.NANOSECOND, 64)))); roundTrip(schema); assertEquals( "Schema", @@ -149,53 +151,57 @@ public void testTime() throws IOException { @Test public void testTS() throws IOException { - Schema schema = new Schema(asList( - field("a", new Timestamp(TimeUnit.SECOND, "UTC")), - field("b", new Timestamp(TimeUnit.MILLISECOND, "UTC")), - field("c", new Timestamp(TimeUnit.MICROSECOND, "UTC")), - field("d", new Timestamp(TimeUnit.NANOSECOND, "UTC")), - field("e", new Timestamp(TimeUnit.SECOND, null)), - field("f", new Timestamp(TimeUnit.MILLISECOND, null)), - field("g", new Timestamp(TimeUnit.MICROSECOND, null)), - field("h", new Timestamp(TimeUnit.NANOSECOND, null)) - )); + Schema schema = + new Schema( + asList( + field("a", new Timestamp(TimeUnit.SECOND, "UTC")), + field("b", new Timestamp(TimeUnit.MILLISECOND, "UTC")), + field("c", new Timestamp(TimeUnit.MICROSECOND, "UTC")), + field("d", new Timestamp(TimeUnit.NANOSECOND, "UTC")), + field("e", new Timestamp(TimeUnit.SECOND, null)), + field("f", new Timestamp(TimeUnit.MILLISECOND, null)), + field("g", new Timestamp(TimeUnit.MICROSECOND, null)), + field("h", new Timestamp(TimeUnit.NANOSECOND, null)))); roundTrip(schema); assertEquals( - "Schema", + "Schema", schema.toString()); } @Test public void testInterval() throws IOException { - Schema schema = new Schema(asList( - field("a", new Interval(IntervalUnit.YEAR_MONTH)), - field("b", new Interval(IntervalUnit.DAY_TIME)) - )); + Schema schema = + new Schema( + asList( + field("a", new Interval(IntervalUnit.YEAR_MONTH)), + field("b", new Interval(IntervalUnit.DAY_TIME)))); roundTrip(schema); contains(schema, "YEAR_MONTH", "DAY_TIME"); } @Test public void testRoundTripDurationInterval() throws IOException { - Schema schema = new Schema(asList( - field("a", new Duration(TimeUnit.SECOND)), - field("b", new Duration(TimeUnit.MILLISECOND)), - field("c", new Duration(TimeUnit.MICROSECOND)), - field("d", new Duration(TimeUnit.NANOSECOND)) - )); + Schema schema = + new Schema( + asList( + field("a", new Duration(TimeUnit.SECOND)), + field("b", new Duration(TimeUnit.MILLISECOND)), + field("c", new Duration(TimeUnit.MICROSECOND)), + field("d", new Duration(TimeUnit.NANOSECOND)))); roundTrip(schema); contains(schema, "SECOND", "MILLI", "MICRO", "NANO"); } @Test public void testFP() throws IOException { - Schema schema = new Schema(asList( - field("a", new FloatingPoint(FloatingPointPrecision.HALF)), - field("b", new FloatingPoint(FloatingPointPrecision.SINGLE)), - field("c", new FloatingPoint(FloatingPointPrecision.DOUBLE)) - )); + Schema schema = + new Schema( + asList( + field("a", new FloatingPoint(FloatingPointPrecision.HALF)), + field("b", new FloatingPoint(FloatingPointPrecision.SINGLE)), + field("c", new FloatingPoint(FloatingPointPrecision.DOUBLE)))); roundTrip(schema); contains(schema, "HALF", "SINGLE", "DOUBLE"); } @@ -205,44 +211,47 @@ public void testMetadata() throws IOException { Map metadata = new HashMap<>(1); metadata.put("testKey", "testValue"); - java.util.List fields = asList( - field("a", false, new Int(8, true)), - field("b", new Struct(), - field("c", new Int(16, true)), - field("d", new Utf8())), - field("e", new List(), field(null, new Date(DateUnit.MILLISECOND))) - ); + java.util.List fields = + asList( + field("a", false, new Int(8, true)), + field("b", new Struct(), field("c", new Int(16, true)), field("d", new Utf8())), + field("e", new List(), field(null, new Date(DateUnit.MILLISECOND)))); Schema schema = new Schema(fields, metadata); roundTrip(schema); - contains(schema, "\"" + METADATA_KEY + "\" : \"testKey\"", "\"" + METADATA_VALUE + "\" : \"testValue\""); + contains( + schema, + "\"" + METADATA_KEY + "\" : \"testKey\"", + "\"" + METADATA_VALUE + "\" : \"testValue\""); } @Test public void testMessageSerialization() { - Schema schema = new Schema(asList( - field("a", false, new Null()), - field("b", new Struct(), field("ba", new Null())), - field("c", new List(), field("ca", new Null())), - field("d", new Union(UnionMode.Sparse, new int[] {1, 2, 3}), field("da", new Null())), - field("e", new Int(8, true)), - field("f", new FloatingPoint(FloatingPointPrecision.SINGLE)), - field("g", new Utf8()), - field("h", new Binary()), - field("i", new Bool()), - field("j", new Decimal(5, 5, 128)), - field("k", new Date(DateUnit.DAY)), - field("l", new Date(DateUnit.MILLISECOND)), - field("m", new Time(TimeUnit.SECOND, 32)), - field("n", new Time(TimeUnit.MILLISECOND, 32)), - field("o", new Time(TimeUnit.MICROSECOND, 64)), - field("p", new Time(TimeUnit.NANOSECOND, 64)), - field("q", new Timestamp(TimeUnit.MILLISECOND, "UTC")), - field("r", new Timestamp(TimeUnit.MICROSECOND, null)), - field("s", new Interval(IntervalUnit.DAY_TIME)), - field("t", new FixedSizeBinary(100)), - field("u", new Duration(TimeUnit.SECOND)), - field("v", new Duration(TimeUnit.MICROSECOND)) - )); + Schema schema = + new Schema( + asList( + field("a", false, new Null()), + field("b", new Struct(), field("ba", new Null())), + field("c", new List(), field("ca", new Null())), + field( + "d", new Union(UnionMode.Sparse, new int[] {1, 2, 3}), field("da", new Null())), + field("e", new Int(8, true)), + field("f", new FloatingPoint(FloatingPointPrecision.SINGLE)), + field("g", new Utf8()), + field("h", new Binary()), + field("i", new Bool()), + field("j", new Decimal(5, 5, 128)), + field("k", new Date(DateUnit.DAY)), + field("l", new Date(DateUnit.MILLISECOND)), + field("m", new Time(TimeUnit.SECOND, 32)), + field("n", new Time(TimeUnit.MILLISECOND, 32)), + field("o", new Time(TimeUnit.MICROSECOND, 64)), + field("p", new Time(TimeUnit.NANOSECOND, 64)), + field("q", new Timestamp(TimeUnit.MILLISECOND, "UTC")), + field("r", new Timestamp(TimeUnit.MICROSECOND, null)), + field("s", new Interval(IntervalUnit.DAY_TIME)), + field("t", new FixedSizeBinary(100)), + field("u", new Duration(TimeUnit.SECOND)), + field("v", new Duration(TimeUnit.MICROSECOND)))); roundTripMessage(schema); } @@ -264,7 +273,8 @@ private void roundTripMessage(Schema schema) { assertEquals(schema.hashCode(), actual.hashCode()); } - private void validateFieldsHashcode(java.util.List schemaFields, java.util.List actualFields) { + private void validateFieldsHashcode( + java.util.List schemaFields, java.util.List actualFields) { assertEquals(schemaFields.size(), actualFields.size()); if (schemaFields.size() == 0) { return; @@ -289,5 +299,4 @@ private void contains(Schema schema, String... s) { assertTrue(json + " contains " + string, json.contains(string)); } } - } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/util/DecimalUtilityTest.java b/java/vector/src/test/java/org/apache/arrow/vector/util/DecimalUtilityTest.java index 804092ed94a..4f9c3cbcd7a 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/util/DecimalUtilityTest.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/util/DecimalUtilityTest.java @@ -19,7 +19,6 @@ import java.math.BigDecimal; import java.math.BigInteger; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -27,19 +26,24 @@ import org.junit.Test; public class DecimalUtilityTest { - private static final BigInteger[] MAX_BIG_INT = new BigInteger[]{BigInteger.valueOf(10).pow(38) - .subtract(java.math.BigInteger.ONE), java.math.BigInteger.valueOf(10).pow(76)}; - private static final BigInteger[] MIN_BIG_INT = new BigInteger[]{MAX_BIG_INT[0].multiply(BigInteger.valueOf(-1)), - MAX_BIG_INT[1].multiply(BigInteger.valueOf(-1))}; + private static final BigInteger[] MAX_BIG_INT = + new BigInteger[] { + BigInteger.valueOf(10).pow(38).subtract(java.math.BigInteger.ONE), + java.math.BigInteger.valueOf(10).pow(76) + }; + private static final BigInteger[] MIN_BIG_INT = + new BigInteger[] { + MAX_BIG_INT[0].multiply(BigInteger.valueOf(-1)), + MAX_BIG_INT[1].multiply(BigInteger.valueOf(-1)) + }; @Test public void testSetLongInDecimalArrowBuf() { - int[] byteLengths = new int[]{16, 32}; + int[] byteLengths = new int[] {16, 32}; for (int x = 0; x < 2; x++) { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(byteLengths[x]); - ) { - int [] intValues = new int [] {Integer.MAX_VALUE, Integer.MIN_VALUE, 0}; + ArrowBuf buf = allocator.buffer(byteLengths[x]); ) { + int[] intValues = new int[] {Integer.MAX_VALUE, Integer.MIN_VALUE, 0}; for (int val : intValues) { buf.clear(); DecimalUtility.writeLongToArrowBuf((long) val, buf, 0, byteLengths[x]); @@ -53,30 +57,32 @@ public void testSetLongInDecimalArrowBuf() { @Test public void testSetByteArrayInDecimalArrowBuf() { - int[] byteLengths = new int[]{16, 32}; + int[] byteLengths = new int[] {16, 32}; for (int x = 0; x < 2; x++) { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(byteLengths[x]); - ) { - int [] intValues = new int [] {Integer.MAX_VALUE, Integer.MIN_VALUE, 0}; + ArrowBuf buf = allocator.buffer(byteLengths[x]); ) { + int[] intValues = new int[] {Integer.MAX_VALUE, Integer.MIN_VALUE, 0}; for (int val : intValues) { buf.clear(); - DecimalUtility.writeByteArrayToArrowBuf(BigInteger.valueOf(val).toByteArray(), buf, 0, byteLengths[x]); + DecimalUtility.writeByteArrayToArrowBuf( + BigInteger.valueOf(val).toByteArray(), buf, 0, byteLengths[x]); BigDecimal actual = DecimalUtility.getBigDecimalFromArrowBuf(buf, 0, 0, byteLengths[x]); BigDecimal expected = BigDecimal.valueOf(val); Assert.assertEquals(expected, actual); } - long [] longValues = new long[] {Long.MIN_VALUE, 0 , Long.MAX_VALUE}; + long[] longValues = new long[] {Long.MIN_VALUE, 0, Long.MAX_VALUE}; for (long val : longValues) { buf.clear(); - DecimalUtility.writeByteArrayToArrowBuf(BigInteger.valueOf(val).toByteArray(), buf, 0, byteLengths[x]); + DecimalUtility.writeByteArrayToArrowBuf( + BigInteger.valueOf(val).toByteArray(), buf, 0, byteLengths[x]); BigDecimal actual = DecimalUtility.getBigDecimalFromArrowBuf(buf, 0, 0, byteLengths[x]); BigDecimal expected = BigDecimal.valueOf(val); Assert.assertEquals(expected, actual); } - BigInteger [] decimals = new BigInteger[] {MAX_BIG_INT[x], new BigInteger("0"), MIN_BIG_INT[x]}; + BigInteger[] decimals = + new BigInteger[] {MAX_BIG_INT[x], new BigInteger("0"), MIN_BIG_INT[x]}; for (BigInteger val : decimals) { buf.clear(); DecimalUtility.writeByteArrayToArrowBuf(val.toByteArray(), buf, 0, byteLengths[x]); @@ -90,12 +96,11 @@ public void testSetByteArrayInDecimalArrowBuf() { @Test public void testSetBigDecimalInDecimalArrowBuf() { - int[] byteLengths = new int[]{16, 32}; + int[] byteLengths = new int[] {16, 32}; for (int x = 0; x < 2; x++) { try (BufferAllocator allocator = new RootAllocator(128); - ArrowBuf buf = allocator.buffer(byteLengths[x]); - ) { - int [] intValues = new int [] {Integer.MAX_VALUE, Integer.MIN_VALUE, 0}; + ArrowBuf buf = allocator.buffer(byteLengths[x]); ) { + int[] intValues = new int[] {Integer.MAX_VALUE, Integer.MIN_VALUE, 0}; for (int val : intValues) { buf.clear(); DecimalUtility.writeBigDecimalToArrowBuf(BigDecimal.valueOf(val), buf, 0, byteLengths[x]); @@ -104,7 +109,7 @@ public void testSetBigDecimalInDecimalArrowBuf() { Assert.assertEquals(expected, actual); } - long [] longValues = new long[] {Long.MIN_VALUE, 0 , Long.MAX_VALUE}; + long[] longValues = new long[] {Long.MIN_VALUE, 0, Long.MAX_VALUE}; for (long val : longValues) { buf.clear(); DecimalUtility.writeBigDecimalToArrowBuf(BigDecimal.valueOf(val), buf, 0, byteLengths[x]); @@ -113,7 +118,8 @@ public void testSetBigDecimalInDecimalArrowBuf() { Assert.assertEquals(expected, actual); } - BigInteger [] decimals = new BigInteger[] {MAX_BIG_INT[x], new BigInteger("0"), MIN_BIG_INT[x]}; + BigInteger[] decimals = + new BigInteger[] {MAX_BIG_INT[x], new BigInteger("0"), MIN_BIG_INT[x]}; for (BigInteger val : decimals) { buf.clear(); DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(val), buf, 0, byteLengths[x]); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/util/TestDataSizeRoundingUtil.java b/java/vector/src/test/java/org/apache/arrow/vector/util/TestDataSizeRoundingUtil.java index 4138ea9d7a1..319980c6542 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/util/TestDataSizeRoundingUtil.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/util/TestDataSizeRoundingUtil.java @@ -21,9 +21,7 @@ import org.junit.Test; -/** - * Test cases for {@link DataSizeRoundingUtil}. - */ +/** Test cases for {@link DataSizeRoundingUtil}. */ public class TestDataSizeRoundingUtil { @Test diff --git a/java/vector/src/test/java/org/apache/arrow/vector/util/TestElementAddressableVectorIterator.java b/java/vector/src/test/java/org/apache/arrow/vector/util/TestElementAddressableVectorIterator.java index 1c8281c8598..431155423b7 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/util/TestElementAddressableVectorIterator.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/util/TestElementAddressableVectorIterator.java @@ -21,7 +21,6 @@ import static org.junit.Assert.assertEquals; import java.nio.charset.StandardCharsets; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.memory.util.ArrowBufPointer; @@ -31,9 +30,7 @@ import org.junit.Before; import org.junit.Test; -/** - * Test cases for {@link ElementAddressableVectorIterator}. - */ +/** Test cases for {@link ElementAddressableVectorIterator}. */ public class TestElementAddressableVectorIterator { private final int VECTOR_LENGTH = 100; @@ -66,7 +63,8 @@ public void testIterateIntVector() { } // iterate - ElementAddressableVectorIterator it = new ElementAddressableVectorIterator<>(intVector); + ElementAddressableVectorIterator it = + new ElementAddressableVectorIterator<>(intVector); int index = 0; while (it.hasNext()) { ArrowBufPointer pt; @@ -105,7 +103,8 @@ public void testIterateVarCharVector() { } // iterate - ElementAddressableVectorIterator it = new ElementAddressableVectorIterator<>(strVector); + ElementAddressableVectorIterator it = + new ElementAddressableVectorIterator<>(strVector); int index = 0; while (it.hasNext()) { ArrowBufPointer pt; diff --git a/java/vector/src/test/java/org/apache/arrow/vector/util/TestMapWithOrdinal.java b/java/vector/src/test/java/org/apache/arrow/vector/util/TestMapWithOrdinal.java index edd5221faf2..0c61cb57949 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/util/TestMapWithOrdinal.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/util/TestMapWithOrdinal.java @@ -23,7 +23,6 @@ import static org.junit.Assert.assertTrue; import java.util.Collection; - import org.junit.Before; import org.junit.Test; diff --git a/java/vector/src/test/java/org/apache/arrow/vector/util/TestMultiMapWithOrdinal.java b/java/vector/src/test/java/org/apache/arrow/vector/util/TestMultiMapWithOrdinal.java index ea829060d1c..4bd169272d4 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/util/TestMultiMapWithOrdinal.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/util/TestMultiMapWithOrdinal.java @@ -54,7 +54,5 @@ public void test() { map.removeAll("z"); Assert.assertEquals(2, map.size()); Assert.assertFalse(map.containsKey("z")); - - } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/util/TestReusableByteArray.java b/java/vector/src/test/java/org/apache/arrow/vector/util/TestReusableByteArray.java index f562e63b4bf..1157133be74 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/util/TestReusableByteArray.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/util/TestReusableByteArray.java @@ -26,7 +26,6 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Base64; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -58,11 +57,16 @@ public void testSetByteArrayRepeatedly() { workingBuf.setBytes(0, str.getBytes(StandardCharsets.UTF_8)); byteArray.set(workingBuf, 0, str.getBytes(StandardCharsets.UTF_8).length); assertEquals(str.getBytes(StandardCharsets.UTF_8).length, byteArray.getLength()); - assertArrayEquals(str.getBytes(StandardCharsets.UTF_8), Arrays.copyOfRange(byteArray.getBuffer(), 0, - (int) byteArray.getLength())); - assertEquals(Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8)), byteArray.toString()); + assertArrayEquals( + str.getBytes(StandardCharsets.UTF_8), + Arrays.copyOfRange(byteArray.getBuffer(), 0, (int) byteArray.getLength())); + assertEquals( + Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8)), + byteArray.toString()); assertEquals(new ReusableByteArray(str.getBytes(StandardCharsets.UTF_8)), byteArray); - assertEquals(new ReusableByteArray(str.getBytes(StandardCharsets.UTF_8)).hashCode(), byteArray.hashCode()); + assertEquals( + new ReusableByteArray(str.getBytes(StandardCharsets.UTF_8)).hashCode(), + byteArray.hashCode()); // Test a longer string. Should require reallocation. final String str2 = "test_longer"; @@ -71,11 +75,16 @@ public void testSetByteArrayRepeatedly() { workingBuf.setBytes(0, str2.getBytes(StandardCharsets.UTF_8)); byteArray.set(workingBuf, 0, str2.getBytes(StandardCharsets.UTF_8).length); assertEquals(str2.getBytes(StandardCharsets.UTF_8).length, byteArray.getLength()); - assertArrayEquals(str2.getBytes(StandardCharsets.UTF_8), Arrays.copyOfRange(byteArray.getBuffer(), 0, - (int) byteArray.getLength())); - assertEquals(Base64.getEncoder().encodeToString(str2.getBytes(StandardCharsets.UTF_8)), byteArray.toString()); + assertArrayEquals( + str2.getBytes(StandardCharsets.UTF_8), + Arrays.copyOfRange(byteArray.getBuffer(), 0, (int) byteArray.getLength())); + assertEquals( + Base64.getEncoder().encodeToString(str2.getBytes(StandardCharsets.UTF_8)), + byteArray.toString()); assertEquals(new ReusableByteArray(str2.getBytes(StandardCharsets.UTF_8)), byteArray); - assertEquals(new ReusableByteArray(str2.getBytes(StandardCharsets.UTF_8)).hashCode(), byteArray.hashCode()); + assertEquals( + new ReusableByteArray(str2.getBytes(StandardCharsets.UTF_8)).hashCode(), + byteArray.hashCode()); // Verify reallocation needed. assertNotSame(oldBuffer, byteArray.getBuffer()); @@ -88,11 +97,16 @@ public void testSetByteArrayRepeatedly() { workingBuf.setBytes(0, str3.getBytes(StandardCharsets.UTF_8)); byteArray.set(workingBuf, 0, str3.getBytes(StandardCharsets.UTF_8).length); assertEquals(str3.getBytes(StandardCharsets.UTF_8).length, byteArray.getLength()); - assertArrayEquals(str3.getBytes(StandardCharsets.UTF_8), Arrays.copyOfRange(byteArray.getBuffer(), 0, - (int) byteArray.getLength())); - assertEquals(Base64.getEncoder().encodeToString(str3.getBytes(StandardCharsets.UTF_8)), byteArray.toString()); + assertArrayEquals( + str3.getBytes(StandardCharsets.UTF_8), + Arrays.copyOfRange(byteArray.getBuffer(), 0, (int) byteArray.getLength())); + assertEquals( + Base64.getEncoder().encodeToString(str3.getBytes(StandardCharsets.UTF_8)), + byteArray.toString()); assertEquals(new ReusableByteArray(str3.getBytes(StandardCharsets.UTF_8)), byteArray); - assertEquals(new ReusableByteArray(str3.getBytes(StandardCharsets.UTF_8)).hashCode(), byteArray.hashCode()); + assertEquals( + new ReusableByteArray(str3.getBytes(StandardCharsets.UTF_8)).hashCode(), + byteArray.hashCode()); // Verify reallocation was not needed. assertSame(oldBuffer, byteArray.getBuffer()); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/util/TestSchemaUtil.java b/java/vector/src/test/java/org/apache/arrow/vector/util/TestSchemaUtil.java index 52b65840868..9e77e9fce15 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/util/TestSchemaUtil.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/util/TestSchemaUtil.java @@ -21,13 +21,11 @@ import static org.junit.Assert.assertEquals; import java.io.IOException; - import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.types.pojo.ArrowType; import org.apache.arrow.vector.types.pojo.Field; import org.apache.arrow.vector.types.pojo.FieldType; import org.apache.arrow.vector.types.pojo.Schema; -import org.apache.arrow.vector.util.SchemaUtility; import org.junit.Test; public class TestSchemaUtil { @@ -38,11 +36,12 @@ private static Field field(String name, boolean nullable, ArrowType type, Field. @Test public void testSerializationAndDeserialization() throws IOException { - Schema schema = new Schema(asList( - field("a", false, new ArrowType.Null()), - field("b", true, new ArrowType.Utf8()), - field("c", true, new ArrowType.Binary())) - ); + Schema schema = + new Schema( + asList( + field("a", false, new ArrowType.Null()), + field("b", true, new ArrowType.Utf8()), + field("c", true, new ArrowType.Binary()))); byte[] serialized = SchemaUtility.serialize(schema); Schema deserialized = SchemaUtility.deserialize(serialized, new RootAllocator(Long.MAX_VALUE)); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/util/TestVectorAppender.java b/java/vector/src/test/java/org/apache/arrow/vector/util/TestVectorAppender.java index 93e75359475..aae106d620f 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/util/TestVectorAppender.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/util/TestVectorAppender.java @@ -25,7 +25,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.BaseValueVector; @@ -57,9 +56,7 @@ import org.junit.Before; import org.junit.Test; -/** - * Test cases for {@link VectorAppender}. - */ +/** Test cases for {@link VectorAppender}. */ public class TestVectorAppender { private BufferAllocator allocator; @@ -80,7 +77,7 @@ public void testAppendFixedWidthVector() { final int length1 = 10; final int length2 = 5; try (IntVector target = new IntVector("", allocator); - IntVector delta = new IntVector("", allocator)) { + IntVector delta = new IntVector("", allocator)) { target.allocateNew(length1); delta.allocateNew(length2); @@ -95,7 +92,8 @@ public void testAppendFixedWidthVector() { try (IntVector expected = new IntVector("expected", allocator)) { expected.allocateNew(); - ValueVectorDataPopulator.setVector(expected, 0, 1, 2, 3, 4, 5, 6, null, 8, 9, null, 11, 12, 13, 14); + ValueVectorDataPopulator.setVector( + expected, 0, 1, 2, 3, 4, 5, 6, null, 8, 9, null, 11, 12, 13, 14); assertVectorsEqual(expected, target); } } @@ -106,7 +104,7 @@ public void testAppendBitVector() { final int length1 = 10; final int length2 = 5; try (BitVector target = new BitVector("", allocator); - BitVector delta = new BitVector("", allocator)) { + BitVector delta = new BitVector("", allocator)) { target.allocateNew(length1); delta.allocateNew(length2); @@ -121,7 +119,8 @@ public void testAppendBitVector() { try (BitVector expected = new BitVector("expected", allocator)) { expected.allocateNew(); - ValueVectorDataPopulator.setVector(expected, 0, 1, 0, 1, 0, 1, 0, null, 0, 1, null, 1, 1, 0, 0); + ValueVectorDataPopulator.setVector( + expected, 0, 1, 0, 1, 0, 1, 0, null, 0, 1, null, 1, 1, 0, 0); assertVectorsEqual(expected, target); } } @@ -130,7 +129,7 @@ public void testAppendBitVector() { @Test public void testAppendEmptyFixedWidthVector() { try (IntVector target = new IntVector("", allocator); - IntVector delta = new IntVector("", allocator)) { + IntVector delta = new IntVector("", allocator)) { ValueVectorDataPopulator.setVector(target, 0, 1, 2, 3, 4, 5, 6, null, 8, 9); @@ -151,12 +150,13 @@ public void testAppendVariableWidthVector() { final int length1 = 10; final int length2 = 5; try (VarCharVector target = new VarCharVector("", allocator); - VarCharVector delta = new VarCharVector("", allocator)) { + VarCharVector delta = new VarCharVector("", allocator)) { target.allocateNew(5, length1); delta.allocateNew(5, length2); - ValueVectorDataPopulator.setVector(target, "a0", "a1", "a2", "a3", null, "a5", "a6", "a7", "a8", "a9"); + ValueVectorDataPopulator.setVector( + target, "a0", "a1", "a2", "a3", null, "a5", "a6", "a7", "a8", "a9"); ValueVectorDataPopulator.setVector(delta, "a10", "a11", "a12", "a13", null); VectorAppender appender = new VectorAppender(target); @@ -164,8 +164,9 @@ public void testAppendVariableWidthVector() { try (VarCharVector expected = new VarCharVector("expected", allocator)) { expected.allocateNew(); - ValueVectorDataPopulator.setVector(expected, - "a0", "a1", "a2", "a3", null, "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13", null); + ValueVectorDataPopulator.setVector( + expected, "a0", "a1", "a2", "a3", null, "a5", "a6", "a7", "a8", "a9", "a10", "a11", + "a12", "a13", null); assertVectorsEqual(expected, target); } } @@ -174,16 +175,17 @@ public void testAppendVariableWidthVector() { @Test public void testAppendEmptyVariableWidthVector() { try (VarCharVector target = new VarCharVector("", allocator); - VarCharVector delta = new VarCharVector("", allocator)) { + VarCharVector delta = new VarCharVector("", allocator)) { - ValueVectorDataPopulator.setVector(target, "a0", "a1", "a2", "a3", null, "a5", "a6", "a7", "a8", "a9"); + ValueVectorDataPopulator.setVector( + target, "a0", "a1", "a2", "a3", null, "a5", "a6", "a7", "a8", "a9"); VectorAppender appender = new VectorAppender(target); delta.accept(appender, null); try (VarCharVector expected = new VarCharVector("expected", allocator)) { - ValueVectorDataPopulator.setVector(expected, - "a0", "a1", "a2", "a3", null, "a5", "a6", "a7", "a8", "a9"); + ValueVectorDataPopulator.setVector( + expected, "a0", "a1", "a2", "a3", null, "a5", "a6", "a7", "a8", "a9"); assertVectorsEqual(expected, target); } } @@ -215,21 +217,23 @@ public void testAppendLargeVariableWidthVector() { final int length1 = 5; final int length2 = 10; try (LargeVarCharVector target = new LargeVarCharVector("", allocator); - LargeVarCharVector delta = new LargeVarCharVector("", allocator)) { + LargeVarCharVector delta = new LargeVarCharVector("", allocator)) { target.allocateNew(5, length1); delta.allocateNew(5, length2); ValueVectorDataPopulator.setVector(target, "a0", null, "a2", "a3", null); - ValueVectorDataPopulator.setVector(delta, "a5", "a6", "a7", null, null, "a10", "a11", "a12", "a13", null); + ValueVectorDataPopulator.setVector( + delta, "a5", "a6", "a7", null, null, "a10", "a11", "a12", "a13", null); VectorAppender appender = new VectorAppender(target); delta.accept(appender, null); try (LargeVarCharVector expected = new LargeVarCharVector("expected", allocator)) { expected.allocateNew(); - ValueVectorDataPopulator.setVector(expected, - "a0", null, "a2", "a3", null, "a5", "a6", "a7", null, null, "a10", "a11", "a12", "a13", null); + ValueVectorDataPopulator.setVector( + expected, "a0", null, "a2", "a3", null, "a5", "a6", "a7", null, null, "a10", "a11", + "a12", "a13", null); assertVectorsEqual(expected, target); } } @@ -238,7 +242,7 @@ public void testAppendLargeVariableWidthVector() { @Test public void testAppendEmptyLargeVariableWidthVector() { try (LargeVarCharVector target = new LargeVarCharVector("", allocator); - LargeVarCharVector delta = new LargeVarCharVector("", allocator)) { + LargeVarCharVector delta = new LargeVarCharVector("", allocator)) { ValueVectorDataPopulator.setVector(target, "a0", null, "a2", "a3", null); @@ -257,10 +261,11 @@ public void testAppendListVector() { final int length1 = 5; final int length2 = 2; try (ListVector target = ListVector.empty("target", allocator); - ListVector delta = ListVector.empty("delta", allocator)) { + ListVector delta = ListVector.empty("delta", allocator)) { target.allocateNew(); - ValueVectorDataPopulator.setVector(target, + ValueVectorDataPopulator.setVector( + target, Arrays.asList(0, 1), Arrays.asList(2, 3), null, @@ -269,9 +274,8 @@ public void testAppendListVector() { assertEquals(length1, target.getValueCount()); delta.allocateNew(); - ValueVectorDataPopulator.setVector(delta, - Arrays.asList(10, 11, 12, 13, 14), - Arrays.asList(15, 16, 17, 18, 19)); + ValueVectorDataPopulator.setVector( + delta, Arrays.asList(10, 11, 12, 13, 14), Arrays.asList(15, 16, 17, 18, 19)); assertEquals(length2, delta.getValueCount()); VectorAppender appender = new VectorAppender(target); @@ -304,13 +308,10 @@ public void testAppendListVector() { @Test public void testAppendEmptyListVector() { try (ListVector target = ListVector.empty("target", allocator); - ListVector delta = ListVector.empty("delta", allocator)) { + ListVector delta = ListVector.empty("delta", allocator)) { // populate target with data - ValueVectorDataPopulator.setVector(target, - Arrays.asList(0, 1), - Arrays.asList(2, 3), - null, - Arrays.asList(6, 7)); + ValueVectorDataPopulator.setVector( + target, Arrays.asList(0, 1), Arrays.asList(2, 3), null, Arrays.asList(6, 7)); assertEquals(4, target.getValueCount()); // leave delta vector empty and unallocated @@ -338,18 +339,15 @@ public void testAppendEmptyListVector() { @Test public void testAppendFixedSizeListVector() { try (FixedSizeListVector target = FixedSizeListVector.empty("target", 5, allocator); - FixedSizeListVector delta = FixedSizeListVector.empty("delta", 5, allocator)) { + FixedSizeListVector delta = FixedSizeListVector.empty("delta", 5, allocator)) { target.allocateNew(); - ValueVectorDataPopulator.setVector(target, - Arrays.asList(0, 1, 2, 3, 4), - null); + ValueVectorDataPopulator.setVector(target, Arrays.asList(0, 1, 2, 3, 4), null); assertEquals(2, target.getValueCount()); delta.allocateNew(); - ValueVectorDataPopulator.setVector(delta, - Arrays.asList(10, 11, 12, 13, 14), - Arrays.asList(15, 16, 17, 18, 19)); + ValueVectorDataPopulator.setVector( + delta, Arrays.asList(10, 11, 12, 13, 14), Arrays.asList(15, 16, 17, 18, 19)); assertEquals(2, delta.getValueCount()); VectorAppender appender = new VectorAppender(target); @@ -367,11 +365,9 @@ public void testAppendFixedSizeListVector() { @Test public void testAppendEmptyFixedSizeListVector() { try (FixedSizeListVector target = FixedSizeListVector.empty("target", 5, allocator); - FixedSizeListVector delta = FixedSizeListVector.empty("delta", 5, allocator)) { + FixedSizeListVector delta = FixedSizeListVector.empty("delta", 5, allocator)) { - ValueVectorDataPopulator.setVector(target, - Arrays.asList(0, 1, 2, 3, 4), - null); + ValueVectorDataPopulator.setVector(target, Arrays.asList(0, 1, 2, 3, 4), null); assertEquals(2, target.getValueCount()); // leave delta vector empty and unallocated @@ -390,11 +386,9 @@ public void testAppendEmptyFixedSizeListVector() { @Test public void testAppendEmptyLargeListVector() { try (LargeListVector target = LargeListVector.empty("target", allocator); - LargeListVector delta = LargeListVector.empty("delta", allocator)) { + LargeListVector delta = LargeListVector.empty("delta", allocator)) { - ValueVectorDataPopulator.setVector(target, - Arrays.asList(0, 1, 2, 3, 4), - null); + ValueVectorDataPopulator.setVector(target, Arrays.asList(0, 1, 2, 3, 4), null); assertEquals(2, target.getValueCount()); // leave delta vector empty and unallocated @@ -415,18 +409,23 @@ public void testAppendStructVector() { final int length1 = 10; final int length2 = 5; try (final StructVector target = StructVector.empty("target", allocator); - final StructVector delta = StructVector.empty("delta", allocator)) { + final StructVector delta = StructVector.empty("delta", allocator)) { - IntVector targetChild1 = target.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); - VarCharVector targetChild2 = target.addOrGet("f1", FieldType.nullable(new ArrowType.Utf8()), VarCharVector.class); + IntVector targetChild1 = + target.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); + VarCharVector targetChild2 = + target.addOrGet("f1", FieldType.nullable(new ArrowType.Utf8()), VarCharVector.class); targetChild1.allocateNew(); targetChild2.allocateNew(); ValueVectorDataPopulator.setVector(targetChild1, 0, 1, 2, 3, 4, null, 6, 7, 8, 9); - ValueVectorDataPopulator.setVector(targetChild2, "a0", "a1", "a2", "a3", "a4", "a5", "a6", null, "a8", "a9"); + ValueVectorDataPopulator.setVector( + targetChild2, "a0", "a1", "a2", "a3", "a4", "a5", "a6", null, "a8", "a9"); target.setValueCount(length1); - IntVector deltaChild1 = delta.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); - VarCharVector deltaChild2 = delta.addOrGet("f1", FieldType.nullable(new ArrowType.Utf8()), VarCharVector.class); + IntVector deltaChild1 = + delta.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); + VarCharVector deltaChild2 = + delta.addOrGet("f1", FieldType.nullable(new ArrowType.Utf8()), VarCharVector.class); deltaChild1.allocateNew(); deltaChild2.allocateNew(); ValueVectorDataPopulator.setVector(deltaChild1, 10, 11, 12, null, 14); @@ -439,13 +438,15 @@ public void testAppendStructVector() { assertEquals(length1 + length2, target.getValueCount()); try (IntVector expected1 = new IntVector("expected1", allocator); - VarCharVector expected2 = new VarCharVector("expected2", allocator)) { + VarCharVector expected2 = new VarCharVector("expected2", allocator)) { expected1.allocateNew(); expected2.allocateNew(); - ValueVectorDataPopulator.setVector(expected1, 0, 1, 2, 3, 4, null, 6, 7, 8, 9, 10, 11, 12, null, 14); - ValueVectorDataPopulator.setVector(expected2, - "a0", "a1", "a2", "a3", "a4", "a5", "a6", null, "a8", "a9", "a10", "a11", "a12", "a13", "a14"); + ValueVectorDataPopulator.setVector( + expected1, 0, 1, 2, 3, 4, null, 6, 7, 8, 9, 10, 11, 12, null, 14); + ValueVectorDataPopulator.setVector( + expected2, "a0", "a1", "a2", "a3", "a4", "a5", "a6", null, "a8", "a9", "a10", "a11", + "a12", "a13", "a14"); assertVectorsEqual(expected1, target.getChild("f0")); assertVectorsEqual(expected2, target.getChild("f1")); @@ -456,12 +457,15 @@ public void testAppendStructVector() { @Test public void testAppendEmptyStructVector() { try (final StructVector target = StructVector.empty("target", allocator); - final StructVector delta = StructVector.empty("delta", allocator)) { + final StructVector delta = StructVector.empty("delta", allocator)) { - IntVector targetChild1 = target.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); - VarCharVector targetChild2 = target.addOrGet("f1", FieldType.nullable(new ArrowType.Utf8()), VarCharVector.class); + IntVector targetChild1 = + target.addOrGet("f0", FieldType.nullable(new ArrowType.Int(32, true)), IntVector.class); + VarCharVector targetChild2 = + target.addOrGet("f1", FieldType.nullable(new ArrowType.Utf8()), VarCharVector.class); ValueVectorDataPopulator.setVector(targetChild1, 0, 1, 2, 3, 4, null, 6, 7, 8, 9); - ValueVectorDataPopulator.setVector(targetChild2, "a0", "a1", "a2", "a3", "a4", "a5", "a6", null, "a8", "a9"); + ValueVectorDataPopulator.setVector( + targetChild2, "a0", "a1", "a2", "a3", "a4", "a5", "a6", null, "a8", "a9"); target.setValueCount(10); // leave delta vector fields empty and unallocated @@ -474,10 +478,10 @@ public void testAppendEmptyStructVector() { assertEquals(10, target.getValueCount()); try (IntVector expected1 = new IntVector("expected1", allocator); - VarCharVector expected2 = new VarCharVector("expected2", allocator)) { + VarCharVector expected2 = new VarCharVector("expected2", allocator)) { ValueVectorDataPopulator.setVector(expected1, 0, 1, 2, 3, 4, null, 6, 7, 8, 9); - ValueVectorDataPopulator.setVector(expected2, - "a0", "a1", "a2", "a3", "a4", "a5", "a6", null, "a8", "a9"); + ValueVectorDataPopulator.setVector( + expected2, "a0", "a1", "a2", "a3", "a4", "a5", "a6", null, "a8", "a9"); assertVectorsEqual(expected1, target.getChild("f0")); assertVectorsEqual(expected2, target.getChild("f1")); @@ -491,7 +495,7 @@ public void testAppendUnionVector() { final int length2 = 5; try (final UnionVector target = UnionVector.empty("target", allocator); - final UnionVector delta = UnionVector.empty("delta", allocator)) { + final UnionVector delta = UnionVector.empty("delta", allocator)) { // alternating ints and big ints target.setType(0, Types.MinorType.INT); @@ -519,14 +523,52 @@ public void testAppendUnionVector() { targetIntVec.allocateNew(); ValueVectorDataPopulator.setVector( targetIntVec, - 0, null, 1, null, 2, null, 3, null, 4, null, 5, null, 6, null, 7, null, 8, null, 9, null); + 0, + null, + 1, + null, + 2, + null, + 3, + null, + 4, + null, + 5, + null, + 6, + null, + 7, + null, + 8, + null, + 9, + null); assertEquals(length1 * 2, targetIntVec.getValueCount()); BigIntVector targetBigIntVec = target.getBigIntVector(); targetBigIntVec.allocateNew(); ValueVectorDataPopulator.setVector( targetBigIntVec, - null, 0L, null, 1L, null, 2L, null, 3L, null, 4L, null, 5L, null, 6L, null, 7L, null, 8L, null, 9L); + null, + 0L, + null, + 1L, + null, + 2L, + null, + 3L, + null, + 4L, + null, + 5L, + null, + 6L, + null, + 7L, + null, + 8L, + null, + 9L); assertEquals(length1 * 2, targetBigIntVec.getValueCount()); target.setValueCount(length1 * 2); @@ -572,7 +614,7 @@ public void testAppendEmptyUnionVector() { final int length1 = 10; try (final UnionVector target = UnionVector.empty("target", allocator); - final UnionVector delta = UnionVector.empty("delta", allocator)) { + final UnionVector delta = UnionVector.empty("delta", allocator)) { // alternating ints and big ints target.setType(0, Types.MinorType.INT); @@ -599,13 +641,51 @@ public void testAppendEmptyUnionVector() { IntVector targetIntVec = target.getIntVector(); ValueVectorDataPopulator.setVector( targetIntVec, - 0, null, 1, null, 2, null, 3, null, 4, null, 5, null, 6, null, 7, null, 8, null, 9, null); + 0, + null, + 1, + null, + 2, + null, + 3, + null, + 4, + null, + 5, + null, + 6, + null, + 7, + null, + 8, + null, + 9, + null); assertEquals(length1 * 2, targetIntVec.getValueCount()); BigIntVector targetBigIntVec = target.getBigIntVector(); ValueVectorDataPopulator.setVector( targetBigIntVec, - null, 0L, null, 1L, null, 2L, null, 3L, null, 4L, null, 5L, null, 6L, null, 7L, null, 8L, null, 9L); + null, + 0L, + null, + 1L, + null, + 2L, + null, + 3L, + null, + 4L, + null, + 5L, + null, + 6L, + null, + 7L, + null, + 8L, + null, + 9L); assertEquals(length1 * 2, targetBigIntVec.getValueCount()); target.setValueCount(length1 * 2); @@ -651,20 +731,22 @@ private DenseUnionVector getTargetVector() { targetVector.reAlloc(); } - byte intTypeId = targetVector.registerNewTypeId(Field.nullable("", Types.MinorType.INT.getType())); + byte intTypeId = + targetVector.registerNewTypeId(Field.nullable("", Types.MinorType.INT.getType())); targetVector.setTypeId(0, intTypeId); intHolder.value = 1; targetVector.setSafe(0, intHolder); targetVector.setTypeId(1, intTypeId); intHolder.value = 2; targetVector.setSafe(1, intHolder); - byte longTypeId = targetVector.registerNewTypeId(Field.nullable("", Types.MinorType.BIGINT.getType())); + byte longTypeId = + targetVector.registerNewTypeId(Field.nullable("", Types.MinorType.BIGINT.getType())); targetVector.setTypeId(3, longTypeId); longHolder.value = 10L; targetVector.setSafe(3, longHolder); targetVector.setValueCount(4); - assertVectorValuesEqual(targetVector, new Object[]{1, 2, null, 10L}); + assertVectorValuesEqual(targetVector, new Object[] {1, 2, null, 10L}); return targetVector; } @@ -683,29 +765,32 @@ private DenseUnionVector getDeltaVector() { while (deltaVector.getValueCapacity() < 4) { deltaVector.reAlloc(); } - byte intTypeId = deltaVector.registerNewTypeId(Field.nullable("", Types.MinorType.INT.getType())); + byte intTypeId = + deltaVector.registerNewTypeId(Field.nullable("", Types.MinorType.INT.getType())); deltaVector.setTypeId(0, intTypeId); intHolder.value = 7; deltaVector.setSafe(0, intHolder); - byte longTypeId = deltaVector.registerNewTypeId(Field.nullable("", Types.MinorType.BIGINT.getType())); + byte longTypeId = + deltaVector.registerNewTypeId(Field.nullable("", Types.MinorType.BIGINT.getType())); deltaVector.setTypeId(2, longTypeId); longHolder.value = 8L; deltaVector.setSafe(2, longHolder); - byte floatTypeId = deltaVector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT4.getType())); + byte floatTypeId = + deltaVector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT4.getType())); deltaVector.setTypeId(3, floatTypeId); floatHolder.value = 9.0f; deltaVector.setSafe(3, floatHolder); deltaVector.setValueCount(4); - assertVectorValuesEqual(deltaVector, new Object[]{7, null, 8L, 9.0f}); + assertVectorValuesEqual(deltaVector, new Object[] {7, null, 8L, 9.0f}); return deltaVector; } @Test public void testAppendDenseUnionVector() { try (DenseUnionVector targetVector = getTargetVector(); - DenseUnionVector deltaVector = getDeltaVector()) { + DenseUnionVector deltaVector = getDeltaVector()) { // append VectorAppender appender = new VectorAppender(targetVector); @@ -715,7 +800,7 @@ public void testAppendDenseUnionVector() { // test reverse append try (DenseUnionVector targetVector = getTargetVector(); - DenseUnionVector deltaVector = getDeltaVector()) { + DenseUnionVector deltaVector = getDeltaVector()) { // append VectorAppender appender = new VectorAppender(deltaVector); @@ -728,13 +813,16 @@ private DenseUnionVector getEmptyDeltaVector() { // create a vector, but leave it empty and uninitialized DenseUnionVector deltaVector = new DenseUnionVector("target vector", allocator, null, null); - byte intTypeId = deltaVector.registerNewTypeId(Field.nullable("", Types.MinorType.INT.getType())); + byte intTypeId = + deltaVector.registerNewTypeId(Field.nullable("", Types.MinorType.INT.getType())); deltaVector.setTypeId(0, intTypeId); - byte longTypeId = deltaVector.registerNewTypeId(Field.nullable("", Types.MinorType.BIGINT.getType())); + byte longTypeId = + deltaVector.registerNewTypeId(Field.nullable("", Types.MinorType.BIGINT.getType())); deltaVector.setTypeId(2, longTypeId); - byte floatTypeId = deltaVector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT4.getType())); + byte floatTypeId = + deltaVector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT4.getType())); deltaVector.setTypeId(3, floatTypeId); return deltaVector; @@ -743,7 +831,7 @@ private DenseUnionVector getEmptyDeltaVector() { @Test public void testAppendEmptyDenseUnionVector() { try (DenseUnionVector targetVector = getTargetVector(); - DenseUnionVector deltaVector = getEmptyDeltaVector()) { + DenseUnionVector deltaVector = getEmptyDeltaVector()) { // append VectorAppender appender = new VectorAppender(targetVector); @@ -752,9 +840,7 @@ public void testAppendEmptyDenseUnionVector() { } } - /** - * Test appending dense union vectors where the child vectors do not match. - */ + /** Test appending dense union vectors where the child vectors do not match. */ @Test public void testAppendDenseUnionVectorMismatch() { final NullableIntHolder intHolder = new NullableIntHolder(); @@ -766,8 +852,10 @@ public void testAppendDenseUnionVectorMismatch() { final NullableFloat4Holder floatHolder = new NullableFloat4Holder(); floatHolder.isSet = 1; - try (DenseUnionVector targetVector = new DenseUnionVector("target vector" , allocator, null, null); - DenseUnionVector deltaVector = new DenseUnionVector("target vector" , allocator, null, null)) { + try (DenseUnionVector targetVector = + new DenseUnionVector("target vector", allocator, null, null); + DenseUnionVector deltaVector = + new DenseUnionVector("target vector", allocator, null, null)) { targetVector.allocateNew(); deltaVector.allocateNew(); @@ -775,11 +863,13 @@ public void testAppendDenseUnionVectorMismatch() { while (targetVector.getValueCapacity() < 2) { targetVector.reAlloc(); } - byte intTypeId = targetVector.registerNewTypeId(Field.nullable("", Types.MinorType.INT.getType())); + byte intTypeId = + targetVector.registerNewTypeId(Field.nullable("", Types.MinorType.INT.getType())); targetVector.setTypeId(0, intTypeId); intHolder.value = 1; targetVector.setSafe(0, intHolder); - byte longTypeId = targetVector.registerNewTypeId(Field.nullable("", Types.MinorType.BIGINT.getType())); + byte longTypeId = + targetVector.registerNewTypeId(Field.nullable("", Types.MinorType.BIGINT.getType())); targetVector.setTypeId(1, longTypeId); longHolder.value = 2L; targetVector.setSafe(1, longHolder); @@ -795,7 +885,8 @@ public void testAppendDenseUnionVectorMismatch() { deltaVector.setTypeId(0, intTypeId); intHolder.value = 3; deltaVector.setSafe(0, intHolder); - byte floatTypeId = deltaVector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT4.getType())); + byte floatTypeId = + deltaVector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT4.getType())); deltaVector.setTypeId(1, floatTypeId); floatHolder.value = 5.0f; deltaVector.setSafe(1, floatHolder); @@ -805,8 +896,7 @@ public void testAppendDenseUnionVectorMismatch() { // append VectorAppender appender = new VectorAppender(targetVector); - assertThrows(IllegalArgumentException.class, - () -> deltaVector.accept(appender, null)); + assertThrows(IllegalArgumentException.class, () -> deltaVector.accept(appender, null)); } } @@ -814,15 +904,14 @@ public void testAppendDenseUnionVectorMismatch() { public void testAppendVectorNegative() { final int vectorLength = 10; try (IntVector target = new IntVector("", allocator); - VarCharVector delta = new VarCharVector("", allocator)) { + VarCharVector delta = new VarCharVector("", allocator)) { target.allocateNew(vectorLength); delta.allocateNew(vectorLength); VectorAppender appender = new VectorAppender(target); - assertThrows(IllegalArgumentException.class, - () -> delta.accept(appender, null)); + assertThrows(IllegalArgumentException.class, () -> delta.accept(appender, null)); } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/util/TestVectorBatchAppender.java b/java/vector/src/test/java/org/apache/arrow/vector/util/TestVectorBatchAppender.java index 799c25c0ad7..e90618f8769 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/util/TestVectorBatchAppender.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/util/TestVectorBatchAppender.java @@ -27,9 +27,7 @@ import org.junit.Before; import org.junit.Test; -/** - * Test cases for {@link VectorBatchAppender}. - */ +/** Test cases for {@link VectorBatchAppender}. */ public class TestVectorBatchAppender { private BufferAllocator allocator; @@ -50,8 +48,8 @@ public void testBatchAppendIntVector() { final int length2 = 5; final int length3 = 7; try (IntVector target = new IntVector("", allocator); - IntVector delta1 = new IntVector("", allocator); - IntVector delta2 = new IntVector("", allocator)) { + IntVector delta1 = new IntVector("", allocator); + IntVector delta2 = new IntVector("", allocator)) { target.allocateNew(length1); delta1.allocateNew(length2); diff --git a/java/vector/src/test/java/org/apache/arrow/vector/util/TestVectorSchemaRootAppender.java b/java/vector/src/test/java/org/apache/arrow/vector/util/TestVectorSchemaRootAppender.java index 6309d385870..d692029e083 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/util/TestVectorSchemaRootAppender.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/util/TestVectorSchemaRootAppender.java @@ -32,9 +32,7 @@ import org.junit.Before; import org.junit.Test; -/** - * Test cases for {@link VectorSchemaRootAppender}. - */ +/** Test cases for {@link VectorSchemaRootAppender}. */ public class TestVectorSchemaRootAppender { private BufferAllocator allocator; @@ -56,16 +54,14 @@ public void testVectorSchemaRootAppend() { final int length3 = 2; try (IntVector targetChild1 = new IntVector("t1", allocator); - VarCharVector targetChild2 = new VarCharVector("t2", allocator); - BigIntVector targetChild3 = new BigIntVector("t3", allocator); - - IntVector deltaChildOne1 = new IntVector("do1", allocator); - VarCharVector deltaChildOne2 = new VarCharVector("do2", allocator); - BigIntVector deltaChildOne3 = new BigIntVector("do3", allocator); - - IntVector deltaChildTwo1 = new IntVector("dt1", allocator); - VarCharVector deltaChildTwo2 = new VarCharVector("dt2", allocator); - BigIntVector deltaChildTwo3 = new BigIntVector("dt3", allocator)) { + VarCharVector targetChild2 = new VarCharVector("t2", allocator); + BigIntVector targetChild3 = new BigIntVector("t3", allocator); + IntVector deltaChildOne1 = new IntVector("do1", allocator); + VarCharVector deltaChildOne2 = new VarCharVector("do2", allocator); + BigIntVector deltaChildOne3 = new BigIntVector("do3", allocator); + IntVector deltaChildTwo1 = new IntVector("dt1", allocator); + VarCharVector deltaChildTwo2 = new VarCharVector("dt2", allocator); + BigIntVector deltaChildTwo3 = new BigIntVector("dt3", allocator)) { ValueVectorDataPopulator.setVector(targetChild1, 0, 1, null, 3, 4); ValueVectorDataPopulator.setVector(targetChild2, "zero", "one", null, "three", "four"); @@ -90,13 +86,14 @@ public void testVectorSchemaRootAppend() { assertEquals(3, root1.getFieldVectors().size()); try (IntVector expected1 = new IntVector("", allocator); - VarCharVector expected2 = new VarCharVector("", allocator); - BigIntVector expected3 = new BigIntVector("", allocator)) { + VarCharVector expected2 = new VarCharVector("", allocator); + BigIntVector expected3 = new BigIntVector("", allocator)) { ValueVectorDataPopulator.setVector(expected1, 0, 1, null, 3, 4, 5, 6, 7, null, 9); ValueVectorDataPopulator.setVector( expected2, "zero", "one", null, "three", "four", "five", "six", "seven", null, "nine"); - ValueVectorDataPopulator.setVector(expected3, 0L, 10L, null, 30L, 40L, 50L, 60L, 70L, null, 90L); + ValueVectorDataPopulator.setVector( + expected3, 0L, 10L, null, 30L, 40L, 50L, 60L, 70L, null, 90L); assertVectorsEqual(expected1, root1.getVector(0)); assertVectorsEqual(expected2, root1.getVector(1)); @@ -108,11 +105,10 @@ public void testVectorSchemaRootAppend() { @Test public void testRootWithDifferentChildCounts() { try (IntVector targetChild1 = new IntVector("t1", allocator); - VarCharVector targetChild2 = new VarCharVector("t2", allocator); - BigIntVector targetChild3 = new BigIntVector("t3", allocator); - - IntVector deltaChild1 = new IntVector("d1", allocator); - VarCharVector deltaChild2 = new VarCharVector("d2", allocator)) { + VarCharVector targetChild2 = new VarCharVector("t2", allocator); + BigIntVector targetChild3 = new BigIntVector("t3", allocator); + IntVector deltaChild1 = new IntVector("d1", allocator); + VarCharVector deltaChild2 = new VarCharVector("d2", allocator)) { ValueVectorDataPopulator.setVector(targetChild1, 0, 1, null, 3, 4); ValueVectorDataPopulator.setVector(targetChild2, "zero", "one", null, "three", "four"); @@ -125,20 +121,21 @@ public void testRootWithDifferentChildCounts() { VectorSchemaRoot root2 = VectorSchemaRoot.of(deltaChild1, deltaChild2); root2.setRowCount(3); - IllegalArgumentException exp = assertThrows(IllegalArgumentException.class, - () -> VectorSchemaRootAppender.append(root1, root2)); + IllegalArgumentException exp = + assertThrows( + IllegalArgumentException.class, () -> VectorSchemaRootAppender.append(root1, root2)); - assertEquals("Vector schema roots have different numbers of child vectors.", exp.getMessage()); + assertEquals( + "Vector schema roots have different numbers of child vectors.", exp.getMessage()); } } @Test public void testRootWithDifferentChildTypes() { try (IntVector targetChild1 = new IntVector("t1", allocator); - VarCharVector targetChild2 = new VarCharVector("t2", allocator); - - IntVector deltaChild1 = new IntVector("d1", allocator); - VarCharVector deltaChild2 = new VarCharVector("d2", allocator)) { + VarCharVector targetChild2 = new VarCharVector("t2", allocator); + IntVector deltaChild1 = new IntVector("d1", allocator); + VarCharVector deltaChild2 = new VarCharVector("d2", allocator)) { ValueVectorDataPopulator.setVector(targetChild1, 0, 1, null, 3, 4); ValueVectorDataPopulator.setVector(targetChild2, "zero", "one", null, "three", "four"); @@ -152,8 +149,9 @@ public void testRootWithDifferentChildTypes() { VectorSchemaRoot root2 = VectorSchemaRoot.of(deltaChild2, deltaChild1); root2.setRowCount(3); - IllegalArgumentException exp = assertThrows(IllegalArgumentException.class, - () -> VectorSchemaRootAppender.append(root1, root2)); + IllegalArgumentException exp = + assertThrows( + IllegalArgumentException.class, () -> VectorSchemaRootAppender.append(root1, root2)); assertEquals("Vector schema roots have different schemas.", exp.getMessage()); } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVector.java b/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVector.java index 20492036dab..3687e5fbb98 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVector.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVector.java @@ -24,7 +24,6 @@ import java.nio.charset.Charset; import java.util.Arrays; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.BigIntVector; @@ -75,8 +74,8 @@ public void testBaseFixedWidthVector() { validate(vector); vector.getDataBuffer().capacity(0); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validate(vector)); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validate(vector)); assertTrue(e.getMessage().contains("Not enough capacity for fixed width data buffer")); } } @@ -89,8 +88,8 @@ public void testBaseVariableWidthVector() { validate(vector); vector.getDataBuffer().capacity(0); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validate(vector)); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validate(vector)); assertTrue(e.getMessage().contains("Not enough capacity for data buffer")); } } @@ -103,8 +102,8 @@ public void testBaseLargeVariableWidthVector() { validate(vector); vector.getDataBuffer().capacity(0); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validate(vector)); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validate(vector)); assertTrue(e.getMessage().contains("Not enough capacity for data buffer")); } } @@ -117,8 +116,8 @@ public void testListVector() { validate(vector); vector.getDataVector().setValueCount(3); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validate(vector)); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validate(vector)); assertTrue(e.getMessage().contains("Inner vector does not contain enough elements.")); } } @@ -131,8 +130,8 @@ public void testLargeListVector() { validate(vector); vector.getDataVector().setValueCount(4); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validate(vector)); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validate(vector)); assertTrue(e.getMessage().contains("Inner vector does not contain enough elements.")); } } @@ -145,8 +144,8 @@ public void testFixedSizeListVector() { validate(vector); vector.getDataVector().setValueCount(3); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validate(vector)); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validate(vector)); assertTrue(e.getMessage().contains("Inner vector does not contain enough elements.")); } } @@ -170,16 +169,16 @@ public void testStructVectorRangeEquals() { writer.setValueCount(5); vector.getChild("f0").setValueCount(2); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validate(vector)); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validate(vector)); assertTrue(e.getMessage().contains("Struct vector length not equal to child vector length")); vector.getChild("f0").setValueCount(5); validate(vector); vector.getChild("f0").getDataBuffer().capacity(0); - ValidateUtil.ValidateException e2 = assertThrows(ValidateUtil.ValidateException.class, - () -> validate(vector)); + ValidateUtil.ValidateException e2 = + assertThrows(ValidateUtil.ValidateException.class, () -> validate(vector)); assertTrue(e2.getMessage().contains("Not enough capacity for fixed width data buffer")); } } @@ -206,16 +205,16 @@ public void testUnionVector() { validate(vector); vector.getChildrenFromFields().get(0).setValueCount(1); - ValidateUtil.ValidateException e1 = assertThrows(ValidateUtil.ValidateException.class, - () -> validate(vector)); + ValidateUtil.ValidateException e1 = + assertThrows(ValidateUtil.ValidateException.class, () -> validate(vector)); assertTrue(e1.getMessage().contains("Union vector length not equal to child vector length")); vector.getChildrenFromFields().get(0).setValueCount(2); validate(vector); vector.getChildrenFromFields().get(0).getDataBuffer().capacity(0); - ValidateUtil.ValidateException e2 = assertThrows(ValidateUtil.ValidateException.class, - () -> validate(vector)); + ValidateUtil.ValidateException e2 = + assertThrows(ValidateUtil.ValidateException.class, () -> validate(vector)); assertTrue(e2.getMessage().contains("Not enough capacity for fixed width data buffer")); } } @@ -233,8 +232,10 @@ public void testDenseUnionVector() { float8Holder.value = 2.02f; float8Holder.isSet = 1; - byte float4TypeId = vector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT4.getType())); - byte float8TypeId = vector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT8.getType())); + byte float4TypeId = + vector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT4.getType())); + byte float8TypeId = + vector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT8.getType())); vector.setTypeId(0, float4TypeId); vector.setSafe(0, float4Holder); @@ -245,8 +246,8 @@ public void testDenseUnionVector() { validate(vector); vector.getChildrenFromFields().get(0).getDataBuffer().capacity(0); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validate(vector)); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validate(vector)); assertTrue(e.getMessage().contains("Not enough capacity for fixed width data buffer")); } } @@ -259,8 +260,8 @@ public void testBaseFixedWidthVectorInstanceMethod() { vector.validate(); vector.getDataBuffer().capacity(0); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> vector.validate()); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> vector.validate()); assertTrue(e.getMessage().contains("Not enough capacity for fixed width data buffer")); } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorFull.java b/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorFull.java index ca71a622bb8..be7688e2486 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorFull.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorFull.java @@ -25,7 +25,6 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; - import org.apache.arrow.memory.ArrowBuf; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; @@ -77,9 +76,11 @@ public void testBaseVariableWidthVector() { offsetBuf.setInt(0, 100); offsetBuf.setInt(4, 50); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validateFull(vector)); - assertTrue(e.getMessage().contains("The values in positions 0 and 1 of the offset buffer are decreasing")); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validateFull(vector)); + assertTrue( + e.getMessage() + .contains("The values in positions 0 and 1 of the offset buffer are decreasing")); } } @@ -94,9 +95,12 @@ public void testBaseLargeVariableWidthVector() { offsetBuf.setLong(0, 100); offsetBuf.setLong(8, 50); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validateFull(vector)); - assertTrue(e.getMessage().contains("The values in positions 0 and 1 of the large offset buffer are decreasing")); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validateFull(vector)); + assertTrue( + e.getMessage() + .contains( + "The values in positions 0 and 1 of the large offset buffer are decreasing")); } } @@ -111,9 +115,11 @@ public void testListVector() { offsetBuf.setInt(0, 100); offsetBuf.setInt(8, 50); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validateFull(vector)); - assertTrue(e.getMessage().contains("The values in positions 0 and 1 of the offset buffer are decreasing")); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validateFull(vector)); + assertTrue( + e.getMessage() + .contains("The values in positions 0 and 1 of the offset buffer are decreasing")); } } @@ -128,9 +134,12 @@ public void testLargeListVector() { offsetBuf.setLong(0, 100); offsetBuf.setLong(16, 50); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validateFull(vector)); - assertTrue(e.getMessage().contains("The values in positions 0 and 1 of the large offset buffer are decreasing")); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validateFull(vector)); + assertTrue( + e.getMessage() + .contains( + "The values in positions 0 and 1 of the large offset buffer are decreasing")); } } @@ -158,13 +167,16 @@ public void testStructVectorRangeEquals() { offsetBuf.setInt(0, 100); offsetBuf.setInt(8, 50); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validateFull(strVector)); - assertTrue(e.getMessage().contains("The values in positions 0 and 1 of the offset buffer are decreasing")); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validateFull(strVector)); + assertTrue( + e.getMessage() + .contains("The values in positions 0 and 1 of the offset buffer are decreasing")); - e = assertThrows(ValidateUtil.ValidateException.class, - () -> validateFull(vector)); - assertTrue(e.getMessage().contains("The values in positions 0 and 1 of the offset buffer are decreasing")); + e = assertThrows(ValidateUtil.ValidateException.class, () -> validateFull(vector)); + assertTrue( + e.getMessage() + .contains("The values in positions 0 and 1 of the offset buffer are decreasing")); } } @@ -192,8 +204,8 @@ public void testUnionVector() { // negative type id vector.getTypeBuffer().setByte(0, -1); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validateFull(vector)); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validateFull(vector)); assertTrue(e.getMessage().contains("The type id at position 0 is negative")); } } @@ -211,8 +223,10 @@ public void testDenseUnionVector() { float8Holder.value = 2.02f; float8Holder.isSet = 1; - byte float4TypeId = vector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT4.getType())); - byte float8TypeId = vector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT8.getType())); + byte float4TypeId = + vector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT4.getType())); + byte float8TypeId = + vector.registerNewTypeId(Field.nullable("", Types.MinorType.FLOAT8.getType())); vector.setTypeId(0, float4TypeId); vector.setSafe(0, float4Holder); @@ -229,8 +243,8 @@ public void testDenseUnionVector() { // shrink sub-vector subVector.setValueCount(0); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validateFull(vector)); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validateFull(vector)); assertTrue(e.getMessage().contains("Dense union vector offset exceeds sub-vector boundary")); } } @@ -246,9 +260,11 @@ public void testBaseVariableWidthVectorInstanceMethod() { offsetBuf.setInt(0, 100); offsetBuf.setInt(4, 50); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - vector::validateFull); - assertTrue(e.getMessage().contains("The values in positions 0 and 1 of the offset buffer are decreasing")); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, vector::validateFull); + assertTrue( + e.getMessage() + .contains("The values in positions 0 and 1 of the offset buffer are decreasing")); } } @@ -256,10 +272,13 @@ public void testBaseVariableWidthVectorInstanceMethod() { public void testValidateVarCharUTF8() { try (final VarCharVector vector = new VarCharVector("v", allocator)) { vector.validateFull(); - setVector(vector, "aaa".getBytes(StandardCharsets.UTF_8), "bbb".getBytes(StandardCharsets.UTF_8), + setVector( + vector, + "aaa".getBytes(StandardCharsets.UTF_8), + "bbb".getBytes(StandardCharsets.UTF_8), new byte[] {(byte) 0xFF, (byte) 0xFE}); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - vector::validateFull); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, vector::validateFull); assertTrue(e.getMessage().contains("UTF")); } } @@ -268,40 +287,47 @@ public void testValidateVarCharUTF8() { public void testValidateLargeVarCharUTF8() { try (final LargeVarCharVector vector = new LargeVarCharVector("v", allocator)) { vector.validateFull(); - setVector(vector, "aaa".getBytes(StandardCharsets.UTF_8), "bbb".getBytes(StandardCharsets.UTF_8), + setVector( + vector, + "aaa".getBytes(StandardCharsets.UTF_8), + "bbb".getBytes(StandardCharsets.UTF_8), new byte[] {(byte) 0xFF, (byte) 0xFE}); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - vector::validateFull); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, vector::validateFull); assertTrue(e.getMessage().contains("UTF")); } } @Test public void testValidateDecimal() { - try (final DecimalVector vector = new DecimalVector(Field.nullable("v", - new ArrowType.Decimal(2, 0, DecimalVector.TYPE_WIDTH * 8)), allocator)) { + try (final DecimalVector vector = + new DecimalVector( + Field.nullable("v", new ArrowType.Decimal(2, 0, DecimalVector.TYPE_WIDTH * 8)), + allocator)) { vector.validateFull(); setVector(vector, 1L); vector.validateFull(); vector.clear(); setVector(vector, Long.MAX_VALUE); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - vector::validateFull); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, vector::validateFull); assertTrue(e.getMessage().contains("Decimal")); } } @Test public void testValidateDecimal256() { - try (final Decimal256Vector vector = new Decimal256Vector(Field.nullable("v", - new ArrowType.Decimal(2, 0, DecimalVector.TYPE_WIDTH * 8)), allocator)) { + try (final Decimal256Vector vector = + new Decimal256Vector( + Field.nullable("v", new ArrowType.Decimal(2, 0, DecimalVector.TYPE_WIDTH * 8)), + allocator)) { vector.validateFull(); setVector(vector, 1L); vector.validateFull(); vector.clear(); setVector(vector, Long.MAX_VALUE); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - vector::validateFull); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, vector::validateFull); assertTrue(e.getMessage().contains("Decimal")); } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorSchemaRoot.java b/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorSchemaRoot.java index 1885fb21f17..a997ee2a943 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorSchemaRoot.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorSchemaRoot.java @@ -50,7 +50,7 @@ public void terminate() throws Exception { @Test public void testValidatePositive() { try (IntVector intVector = new IntVector("int vector", allocator); - VarCharVector strVector = new VarCharVector("var char vector", allocator)) { + VarCharVector strVector = new VarCharVector("var char vector", allocator)) { VectorSchemaRoot root = VectorSchemaRoot.of(intVector, strVector); @@ -69,7 +69,7 @@ public void testValidatePositive() { @Test public void testValidateNegative() { try (IntVector intVector = new IntVector("int vector", allocator); - VarCharVector strVector = new VarCharVector("var char vector", allocator)) { + VarCharVector strVector = new VarCharVector("var char vector", allocator)) { VectorSchemaRoot root = VectorSchemaRoot.of(intVector, strVector); @@ -80,12 +80,15 @@ public void testValidateNegative() { root.setRowCount(4); intVector.setValueCount(5); strVector.setValueCount(5); - ValidateUtil.ValidateException e = assertThrows(ValidateUtil.ValidateException.class, - () -> validate(root)); - assertTrue(e.getMessage().contains("Child vector and vector schema root have different value counts")); - e = assertThrows(ValidateUtil.ValidateException.class, - () -> validateFull(root)); - assertTrue(e.getMessage().contains("Child vector and vector schema root have different value counts")); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validate(root)); + assertTrue( + e.getMessage() + .contains("Child vector and vector schema root have different value counts")); + e = assertThrows(ValidateUtil.ValidateException.class, () -> validateFull(root)); + assertTrue( + e.getMessage() + .contains("Child vector and vector schema root have different value counts")); // valid problems with the child vector root.setRowCount(5); @@ -93,9 +96,10 @@ public void testValidateNegative() { offsetBuf.setInt(0, 100); offsetBuf.setInt(8, 50); validate(root); - e = assertThrows(ValidateUtil.ValidateException.class, - () -> validateFull(root)); - assertTrue(e.getMessage().contains("The values in positions 0 and 1 of the offset buffer are decreasing")); + e = assertThrows(ValidateUtil.ValidateException.class, () -> validateFull(root)); + assertTrue( + e.getMessage() + .contains("The values in positions 0 and 1 of the offset buffer are decreasing")); } } } diff --git a/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorTypeVisitor.java b/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorTypeVisitor.java index 0ddd790d6ff..f9ea39289df 100644 --- a/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorTypeVisitor.java +++ b/java/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorTypeVisitor.java @@ -20,7 +20,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.function.Supplier; - import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.BigIntVector; @@ -74,9 +73,7 @@ import org.junit.Before; import org.junit.Test; -/** - * Test cases for {@link ValidateVectorTypeVisitor}. - */ +/** Test cases for {@link ValidateVectorTypeVisitor}. */ public class TestValidateVectorTypeVisitor { private BufferAllocator allocator; @@ -94,16 +91,18 @@ public void terminate() throws Exception { } private void testPositiveCase(Supplier vectorGenerator) { - try (ValueVector vector = vectorGenerator.get();) { + try (ValueVector vector = vectorGenerator.get(); ) { vector.accept(visitor, null); } } private void testNegativeCase(Supplier vectorGenerator) { try (ValueVector vector = vectorGenerator.get()) { - assertThrows(ValidateUtil.ValidateException.class, () -> { - vector.accept(visitor, null); - }); + assertThrows( + ValidateUtil.ValidateException.class, + () -> { + vector.accept(visitor, null); + }); } } @@ -126,8 +125,10 @@ public void testFixedWidthVectorsPositive() { testPositiveCase(() -> new DateDayVector("vector", allocator)); testPositiveCase(() -> new DateMilliVector("vector", allocator)); - testPositiveCase(() -> new DurationVector( - "vector", FieldType.nullable(new ArrowType.Duration(TimeUnit.SECOND)), allocator)); + testPositiveCase( + () -> + new DurationVector( + "vector", FieldType.nullable(new ArrowType.Duration(TimeUnit.SECOND)), allocator)); // float vectors testPositiveCase(() -> new Float4Vector("vector", allocator)); @@ -160,96 +161,173 @@ public void testFixedWidthVectorsPositive() { public void testFixedWidthVectorsNegative() { // integer vectors testNegativeCase( - () -> new TinyIntVector("vector", FieldType.nullable(Types.MinorType.INT.getType()), allocator)); + () -> + new TinyIntVector( + "vector", FieldType.nullable(Types.MinorType.INT.getType()), allocator)); testNegativeCase( - () -> new SmallIntVector("vector", FieldType.nullable(Types.MinorType.INT.getType()), allocator)); + () -> + new SmallIntVector( + "vector", FieldType.nullable(Types.MinorType.INT.getType()), allocator)); testNegativeCase( - () -> new BigIntVector("vector", FieldType.nullable(Types.MinorType.SMALLINT.getType()), allocator)); + () -> + new BigIntVector( + "vector", FieldType.nullable(Types.MinorType.SMALLINT.getType()), allocator)); testNegativeCase( - () -> new BigIntVector("vector", FieldType.nullable(Types.MinorType.SMALLINT.getType()), allocator)); + () -> + new BigIntVector( + "vector", FieldType.nullable(Types.MinorType.SMALLINT.getType()), allocator)); testNegativeCase( - () -> new UInt1Vector("vector", FieldType.nullable(Types.MinorType.SMALLINT.getType()), allocator)); + () -> + new UInt1Vector( + "vector", FieldType.nullable(Types.MinorType.SMALLINT.getType()), allocator)); testNegativeCase( - () -> new UInt2Vector("vector", FieldType.nullable(Types.MinorType.SMALLINT.getType()), allocator)); + () -> + new UInt2Vector( + "vector", FieldType.nullable(Types.MinorType.SMALLINT.getType()), allocator)); testNegativeCase( - () -> new UInt4Vector("vector", FieldType.nullable(Types.MinorType.SMALLINT.getType()), allocator)); + () -> + new UInt4Vector( + "vector", FieldType.nullable(Types.MinorType.SMALLINT.getType()), allocator)); testNegativeCase( - () -> new UInt8Vector("vector", FieldType.nullable(Types.MinorType.SMALLINT.getType()), allocator)); + () -> + new UInt8Vector( + "vector", FieldType.nullable(Types.MinorType.SMALLINT.getType()), allocator)); testNegativeCase( - () -> new BitVector("vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); - testNegativeCase( - () -> new DecimalVector("vector", allocator, 30, -16)); + () -> + new BitVector( + "vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); + testNegativeCase(() -> new DecimalVector("vector", allocator, 30, -16)); // date vectors testNegativeCase( - () -> new DateDayVector("vector", FieldType.nullable(Types.MinorType.FLOAT4.getType()), allocator)); + () -> + new DateDayVector( + "vector", FieldType.nullable(Types.MinorType.FLOAT4.getType()), allocator)); testNegativeCase( - () -> new DateMilliVector("vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); + () -> + new DateMilliVector( + "vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); // float pont vectors testNegativeCase( - () -> new Float4Vector("vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); + () -> + new Float4Vector( + "vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); testNegativeCase( - () -> new Float8Vector("vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); + () -> + new Float8Vector( + "vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); // interval vectors testNegativeCase( - () -> new IntervalDayVector("vector", FieldType.nullable(Types.MinorType.INT.getType()), allocator)); + () -> + new IntervalDayVector( + "vector", FieldType.nullable(Types.MinorType.INT.getType()), allocator)); testNegativeCase( - () -> new IntervalYearVector("vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); + () -> + new IntervalYearVector( + "vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); // time vectors testNegativeCase( - () -> new TimeMilliVector("vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); + () -> + new TimeMilliVector( + "vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); testNegativeCase( - () -> new TimeMicroVector("vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); + () -> + new TimeMicroVector( + "vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); testNegativeCase( - () -> new TimeNanoVector("vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); + () -> + new TimeNanoVector( + "vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); testNegativeCase( - () -> new TimeSecVector("vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); + () -> + new TimeSecVector( + "vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); // time stamp vectors - testNegativeCase( - () -> new TimeStampMicroTZVector("vector", allocator, null)); - testNegativeCase( - () -> new TimeStampMicroVector("vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); - testNegativeCase( - () -> new TimeStampMilliTZVector("vector", allocator, null)); - testNegativeCase( - () -> new TimeStampMilliVector("vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); - testNegativeCase( - () -> new TimeStampNanoTZVector("vector", allocator, null)); - testNegativeCase( - () -> new TimeStampNanoVector("vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); - testNegativeCase( - () -> new TimeStampSecTZVector("vector", allocator, null)); - testNegativeCase( - () -> new TimeStampSecVector("vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); + testNegativeCase(() -> new TimeStampMicroTZVector("vector", allocator, null)); + testNegativeCase( + () -> + new TimeStampMicroVector( + "vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); + testNegativeCase(() -> new TimeStampMilliTZVector("vector", allocator, null)); + testNegativeCase( + () -> + new TimeStampMilliVector( + "vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); + testNegativeCase(() -> new TimeStampNanoTZVector("vector", allocator, null)); + testNegativeCase( + () -> + new TimeStampNanoVector( + "vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); + testNegativeCase(() -> new TimeStampSecTZVector("vector", allocator, null)); + testNegativeCase( + () -> + new TimeStampSecVector( + "vector", FieldType.nullable(Types.MinorType.BIGINT.getType()), allocator)); } @Test public void testDecimalVector() { - testPositiveCase(() -> - new DecimalVector("dec", FieldType.nullable(ArrowType.Decimal.createDecimal(10, 10, 128)), allocator)); - testPositiveCase(() -> - new DecimalVector("dec", FieldType.nullable(ArrowType.Decimal.createDecimal(38, 10, 128)), allocator)); - testPositiveCase(() -> - new Decimal256Vector("dec", FieldType.nullable(ArrowType.Decimal.createDecimal(50, 10, 256)), allocator)); - testPositiveCase(() -> - new Decimal256Vector("dec", FieldType.nullable(ArrowType.Decimal.createDecimal(76, 10, 256)), allocator)); - testNegativeCase(() -> - new DecimalVector("dec", FieldType.nullable(ArrowType.Decimal.createDecimal(50, 10, 128)), allocator)); - testNegativeCase(() -> - new Decimal256Vector("dec", FieldType.nullable(ArrowType.Decimal.createDecimal(100, 10, 256)), allocator)); - testNegativeCase(() -> - new DecimalVector("dec", FieldType.nullable(ArrowType.Decimal.createDecimal(0, 10, 128)), allocator)); - testNegativeCase(() -> - new Decimal256Vector("dec", FieldType.nullable(ArrowType.Decimal.createDecimal(-1, 10, 256)), allocator)); - testNegativeCase(() -> - new Decimal256Vector("dec", FieldType.nullable(ArrowType.Decimal.createDecimal(30, 10, 64)), allocator)); - testNegativeCase(() -> - new Decimal256Vector("dec", FieldType.nullable(ArrowType.Decimal.createDecimal(10, 20, 256)), allocator)); + testPositiveCase( + () -> + new DecimalVector( + "dec", + FieldType.nullable(ArrowType.Decimal.createDecimal(10, 10, 128)), + allocator)); + testPositiveCase( + () -> + new DecimalVector( + "dec", + FieldType.nullable(ArrowType.Decimal.createDecimal(38, 10, 128)), + allocator)); + testPositiveCase( + () -> + new Decimal256Vector( + "dec", + FieldType.nullable(ArrowType.Decimal.createDecimal(50, 10, 256)), + allocator)); + testPositiveCase( + () -> + new Decimal256Vector( + "dec", + FieldType.nullable(ArrowType.Decimal.createDecimal(76, 10, 256)), + allocator)); + testNegativeCase( + () -> + new DecimalVector( + "dec", + FieldType.nullable(ArrowType.Decimal.createDecimal(50, 10, 128)), + allocator)); + testNegativeCase( + () -> + new Decimal256Vector( + "dec", + FieldType.nullable(ArrowType.Decimal.createDecimal(100, 10, 256)), + allocator)); + testNegativeCase( + () -> + new DecimalVector( + "dec", FieldType.nullable(ArrowType.Decimal.createDecimal(0, 10, 128)), allocator)); + testNegativeCase( + () -> + new Decimal256Vector( + "dec", + FieldType.nullable(ArrowType.Decimal.createDecimal(-1, 10, 256)), + allocator)); + testNegativeCase( + () -> + new Decimal256Vector( + "dec", FieldType.nullable(ArrowType.Decimal.createDecimal(30, 10, 64)), allocator)); + testNegativeCase( + () -> + new Decimal256Vector( + "dec", + FieldType.nullable(ArrowType.Decimal.createDecimal(10, 20, 256)), + allocator)); } @Test @@ -261,9 +339,13 @@ public void testVariableWidthVectorsPositive() { @Test public void testVariableWidthVectorsNegative() { testNegativeCase( - () -> new VarCharVector("vector", FieldType.nullable(Types.MinorType.INT.getType()), allocator)); + () -> + new VarCharVector( + "vector", FieldType.nullable(Types.MinorType.INT.getType()), allocator)); testNegativeCase( - () -> new VarBinaryVector("vector", FieldType.nullable(Types.MinorType.INT.getType()), allocator)); + () -> + new VarBinaryVector( + "vector", FieldType.nullable(Types.MinorType.INT.getType()), allocator)); } @Test @@ -275,9 +357,13 @@ public void testLargeVariableWidthVectorsPositive() { @Test public void testLargeVariableWidthVectorsNegative() { testNegativeCase( - () -> new LargeVarCharVector("vector", FieldType.nullable(Types.MinorType.INT.getType()), allocator)); + () -> + new LargeVarCharVector( + "vector", FieldType.nullable(Types.MinorType.INT.getType()), allocator)); testNegativeCase( - () -> new LargeVarBinaryVector("vector", FieldType.nullable(Types.MinorType.INT.getType()), allocator)); + () -> + new LargeVarBinaryVector( + "vector", FieldType.nullable(Types.MinorType.INT.getType()), allocator)); } @Test @@ -285,7 +371,9 @@ public void testListVector() { testPositiveCase(() -> ListVector.empty("vector", allocator)); testNegativeCase( - () -> new ListVector("vector", allocator, FieldType.nullable(Types.MinorType.INT.getType()), null)); + () -> + new ListVector( + "vector", allocator, FieldType.nullable(Types.MinorType.INT.getType()), null)); } @Test @@ -293,7 +381,9 @@ public void testLargeListVector() { testPositiveCase(() -> LargeListVector.empty("vector", allocator)); testNegativeCase( - () -> new LargeListVector("vector", allocator, FieldType.nullable(Types.MinorType.INT.getType()), null)); + () -> + new LargeListVector( + "vector", allocator, FieldType.nullable(Types.MinorType.INT.getType()), null)); } @Test @@ -306,7 +396,9 @@ public void testStructVector() { testPositiveCase(() -> StructVector.empty("vector", allocator)); testNegativeCase( - () -> new StructVector("vector", allocator, FieldType.nullable(Types.MinorType.INT.getType()), null)); + () -> + new StructVector( + "vector", allocator, FieldType.nullable(Types.MinorType.INT.getType()), null)); } @Test diff --git a/java/vector/src/test/resources/logback.xml b/java/vector/src/test/resources/logback.xml index f9e449fa67b..30a9eca7285 100644 --- a/java/vector/src/test/resources/logback.xml +++ b/java/vector/src/test/resources/logback.xml @@ -1,4 +1,4 @@ - + -