[
https://issues.apache.org/jira/browse/BEAM-8933?focusedWorklogId=600083&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-600083
]
ASF GitHub Bot logged work on BEAM-8933:
----------------------------------------
Author: ASF GitHub Bot
Created on: 20/May/21 22:11
Start Date: 20/May/21 22:11
Worklog Time Spent: 10m
Work Description: TheNeuralBit commented on a change in pull request
#14586:
URL: https://github.com/apache/beam/pull/14586#discussion_r636506720
##########
File path:
sdks/java/extensions/arrow/src/main/java/org/apache/beam/sdk/extensions/arrow/ArrowConversion.java
##########
@@ -0,0 +1,524 @@
+/*
+ * 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.beam.sdk.extensions.arrow;
+
+import static
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.channels.Channels;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Optional;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import javax.annotation.Nullable;
+import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.FieldVector;
+import org.apache.arrow.vector.VectorLoader;
+import org.apache.arrow.vector.VectorSchemaRoot;
+import org.apache.arrow.vector.ipc.ReadChannel;
+import org.apache.arrow.vector.ipc.message.ArrowRecordBatch;
+import org.apache.arrow.vector.ipc.message.MessageSerializer;
+import org.apache.arrow.vector.types.TimeUnit;
+import org.apache.arrow.vector.types.pojo.ArrowType;
+import org.apache.arrow.vector.util.Text;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.schemas.CachingFactory;
+import org.apache.beam.sdk.schemas.Factory;
+import org.apache.beam.sdk.schemas.FieldValueGetter;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.Schema.Field;
+import org.apache.beam.sdk.schemas.Schema.FieldType;
+import org.apache.beam.sdk.schemas.logicaltypes.FixedBytes;
+import org.apache.beam.sdk.values.Row;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+
+/**
+ * Utilities to create {@link Iterable}s of Beam {@link Row} instances backed
by Arrow record
+ * batches.
+ */
+@Experimental(Experimental.Kind.SCHEMAS)
+public class ArrowConversion {
+
+ /** Get Beam Field from Arrow Field. */
+ private static Field toBeamField(org.apache.arrow.vector.types.pojo.Field
field) {
+ FieldType beamFieldType = toFieldType(field.getFieldType(),
field.getChildren());
+ return Field.of(field.getName(), beamFieldType);
+ }
+
+ /** Converts Arrow FieldType to Beam FieldType. */
+ private static FieldType toFieldType(
+ org.apache.arrow.vector.types.pojo.FieldType arrowFieldType,
+ List<org.apache.arrow.vector.types.pojo.Field> childrenFields) {
+ FieldType fieldType =
+ arrowFieldType
+ .getType()
+ .accept(
+ new ArrowType.ArrowTypeVisitor<FieldType>() {
+ @Override
+ public FieldType visit(ArrowType.Null type) {
+ throw new IllegalArgumentException(
+ "Type \'" + type.toString() + "\' not supported.");
+ }
+
+ @Override
+ public FieldType visit(ArrowType.Struct type) {
+ return
FieldType.row(ArrowSchemaTranslator.toBeamSchema(childrenFields));
+ }
+
+ @Override
+ public FieldType visit(ArrowType.List type) {
+ checkArgument(
+ childrenFields.size() == 1,
+ "Encountered "
+ + childrenFields.size()
+ + " child fields for list type, expected 1");
+ return
FieldType.array(toBeamField(childrenFields.get(0)).getType());
+ }
+
+ @Override
+ public FieldType visit(ArrowType.FixedSizeList type) {
+ throw new IllegalArgumentException(
+ "Type \'" + type.toString() + "\' not supported.");
+ }
+
+ @Override
+ public FieldType visit(ArrowType.Union type) {
+ throw new IllegalArgumentException(
+ "Type \'" + type.toString() + "\' not supported.");
+ }
+
+ @Override
+ public FieldType visit(ArrowType.Map type) {
+ checkArgument(
+ childrenFields.size() == 2,
+ "Encountered "
+ + childrenFields.size()
+ + " child fields for map type, expected 2");
+ return FieldType.map(
+ toBeamField(childrenFields.get(0)).getType(),
+ toBeamField(childrenFields.get(1)).getType());
+ }
+
+ @Override
+ public FieldType visit(ArrowType.Int type) {
+ if (!type.getIsSigned()) {
+ throw new IllegalArgumentException("Unsigned integers
are not supported.");
+ }
+ switch (type.getBitWidth()) {
+ case 8:
+ return FieldType.BYTE;
+ case 16:
+ return FieldType.INT16;
+ case 32:
+ return FieldType.INT32;
+ case 64:
+ return FieldType.INT64;
+ default:
+ throw new IllegalArgumentException(
+ "Unsupported integer bit width: " +
type.getBitWidth());
+ }
+ }
+
+ @Override
+ public FieldType visit(ArrowType.FloatingPoint type) {
+ switch (type.getPrecision()) {
+ case SINGLE:
+ return FieldType.FLOAT;
+ case DOUBLE:
+ return FieldType.DOUBLE;
+ default:
+ throw new IllegalArgumentException(
+ "Unsupported floating-point precision: " +
type.getPrecision().name());
+ }
+ }
+
+ @Override
+ public FieldType visit(ArrowType.Utf8 type) {
+ return FieldType.STRING;
+ }
+
+ @Override
+ public FieldType visit(ArrowType.Binary type) {
+ return FieldType.BYTES;
+ }
+
+ @Override
+ public FieldType visit(ArrowType.FixedSizeBinary type) {
+ return
FieldType.logicalType(FixedBytes.of(type.getByteWidth()));
+ }
+
+ @Override
+ public FieldType visit(ArrowType.Bool type) {
+ return FieldType.BOOLEAN;
+ }
+
+ @Override
+ public FieldType visit(ArrowType.Decimal type) {
+ // FieldType.DECIMAL isn't perfect here since arrow
decimal has a
+ // scale/precision fixed by the schema, but
FieldType.DECIMAL uses a BigDecimal,
+ // whose precision/scale can change from row to row.
+ throw new IllegalArgumentException(
+ "Type \'" + type.toString() + "\' not supported.");
+ }
+
+ @Override
+ public FieldType visit(ArrowType.Date type) {
+ throw new IllegalArgumentException(
+ "Type \'" + type.toString() + "\' not supported.");
+ }
+
+ @Override
+ public FieldType visit(ArrowType.Time type) {
+ throw new IllegalArgumentException(
+ "Type \'" + type.toString() + "\' not supported.");
+ }
+
+ @Override
+ public FieldType visit(ArrowType.Timestamp type) {
+ if (type.getUnit() == TimeUnit.MILLISECOND
+ || type.getUnit() == TimeUnit.MICROSECOND) {
+ return FieldType.DATETIME;
+ } else {
+ throw new IllegalArgumentException(
+ "Unsupported timestamp unit: " +
type.getUnit().name());
+ }
+ }
+
+ @Override
+ public FieldType visit(ArrowType.Interval type) {
+ throw new IllegalArgumentException(
+ "Type \'" + type.toString() + "\' not supported.");
+ }
+
+ @Override
+ public FieldType visit(ArrowType.Duration type) {
+ throw new IllegalArgumentException(
+ "Type \'" + type.toString() + "\' not supported.");
+ }
+
+ @Override
+ public FieldType visit(ArrowType.LargeBinary type) {
+ throw new IllegalArgumentException(
+ "Type \'" + type.toString() + "\' not supported.");
+ }
+
+ @Override
+ public FieldType visit(ArrowType.LargeUtf8 type) {
+ throw new IllegalArgumentException(
+ "Type \'" + type.toString() + "\' not supported.");
+ }
+
+ @Override
+ public FieldType visit(ArrowType.LargeList type) {
+ throw new IllegalArgumentException(
+ "Type \'" + type.toString() + "\' not supported.");
+ }
+ });
+ return fieldType.withNullable(arrowFieldType.isNullable());
+ }
+
+ /**
+ * Returns an {@link Iterable<Row>} backed by the Arrow record batch stored
in {@code
+ * vectorSchemaRoot}.
+ *
+ * <p>Note this is a lazy interface. The data in the underlying Arrow buffer
is not read until a
+ * field of one of the returned {@link Row}s is accessed.
+ */
+ public static Iterable<Row> rowsFromRecordBatch(
+ Schema schema, VectorSchemaRoot vectorSchemaRoot) {
+ return new RecordBatchIterable(schema, vectorSchemaRoot);
+ }
+
+ public static Iterable<Row> rowsFromRecordBatch(VectorSchemaRoot
vectorSchemaRoot) {
+ return rowsFromRecordBatch(
+ ArrowSchemaTranslator.toBeamSchema(vectorSchemaRoot.getSchema()),
vectorSchemaRoot);
+ }
+
+ public static VectorSchemaRoot rowFromSerializedRecordBatch(InputStream
input)
Review comment:
nit:
```suggestion
public static VectorSchemaRoot rowsFromSerializedRecordBatch(InputStream
input)
```
Could you modify this to return an `Iterable<Row>` by calling
`rowsFromRecordBatch`?
We also need to manage the lifecycle of the `ReadChannel` and
`RootAllocator` created in this method, by close()-ing them when done,
otherwise we could leak memory. Probably what we should do is make sure the
iterator has a reference to these objects, then it can close() them in it's own
close method. You could do this either by passing those objects to the
Iterable/Iterator, or making the Iterable responsible for creating them.
Then we'll have to call `close()` in `BigQueryStorageArrowReader.close`, and
`BigQueryStorageArrowReader.resetBuffer`.
##########
File path:
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/SchemaAndRecord.java
##########
@@ -18,23 +18,32 @@
package org.apache.beam.sdk.io.gcp.bigquery;
import com.google.api.services.bigquery.model.TableSchema;
+import javax.annotation.Nullable;
import org.apache.avro.generic.GenericRecord;
+import org.apache.beam.sdk.values.Row;
/**
* A wrapper for a {@link GenericRecord} and the {@link TableSchema}
representing the schema of the
* table (or query) it was generated from.
*/
public class SchemaAndRecord {
- private final GenericRecord record;
+ private final Object record;
private final TableSchema tableSchema;
- public SchemaAndRecord(GenericRecord record, TableSchema tableSchema) {
+ public SchemaAndRecord(Object record, TableSchema tableSchema) {
this.record = record;
this.tableSchema = tableSchema;
}
public GenericRecord getRecord() {
- return record;
+ if (!(record instanceof GenericRecord)) {
+ throw new IllegalStateException("Object is not GenericRecord");
+ }
+ return (GenericRecord) record;
+ }
Review comment:
Do we still need the ability to for `record` to be a `GenericRecord`? I
thought the plan was for `record` to always be a `Row`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 600083)
Time Spent: 36.5h (was: 36h 20m)
> BigQuery IO should support reading Arrow format over Storage API
> ----------------------------------------------------------------
>
> Key: BEAM-8933
> URL: https://issues.apache.org/jira/browse/BEAM-8933
> Project: Beam
> Issue Type: Improvement
> Components: io-java-gcp
> Reporter: Kirill Kozlov
> Assignee: Miguel Anzo
> Priority: P3
> Time Spent: 36.5h
> Remaining Estimate: 0h
>
> As of right now BigQuery uses Avro format for reading and writing.
> We should add a config to BigQueryIO to specify which format to use: Arrow or
> Avro (with Avro as default).
--
This message was sent by Atlassian Jira
(v8.3.4#803005)