[ https://issues.apache.org/jira/browse/BEAM-5918?focusedWorklogId=160713&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-160713 ]
ASF GitHub Bot logged work on BEAM-5918: ---------------------------------------- Author: ASF GitHub Bot Created on: 30/Oct/18 16:28 Start Date: 30/Oct/18 16:28 Worklog Time Spent: 10m Work Description: kanterov commented on a change in pull request #6888: [BEAM-5918] [WIP] Add Cast transform for Rows URL: https://github.com/apache/beam/pull/6888#discussion_r229382088 ########## File path: sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/transforms/Cast.java ########## @@ -0,0 +1,582 @@ +/* + * 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.schemas.transforms; + +import static org.apache.beam.sdk.schemas.Schema.TypeName.ARRAY; +import static org.apache.beam.sdk.schemas.Schema.TypeName.INT16; +import static org.apache.beam.sdk.schemas.Schema.TypeName.INT32; +import static org.apache.beam.sdk.schemas.Schema.TypeName.INT64; +import static org.apache.beam.sdk.schemas.Schema.TypeName.MAP; +import static org.apache.beam.sdk.schemas.Schema.TypeName.ROW; + +import com.google.auto.value.AutoValue; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.apache.beam.sdk.annotations.Experimental; +import org.apache.beam.sdk.schemas.FieldAccessDescriptor; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.schemas.Schema.TypeName; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.PTransform; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.values.KV; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.Row; + +/** Set of utilities for casting rows between schemas. */ +@Experimental(Experimental.Kind.SCHEMAS) +@AutoValue +public abstract class Cast<T> extends PTransform<PCollection<T>, PCollection<Row>> { + + public abstract Schema outputSchema(); + + public abstract Nullability nullability(); + + public abstract Type type(); + + public abstract Shape shape(); + + /** Builder for {@link Cast}. */ + @AutoValue.Builder + public abstract static class Builder<T> { + + public abstract Builder<T> outputSchema(Schema schema); + + public abstract Builder<T> nullability(Nullability nullability); + + public abstract Builder<T> type(Type type); + + public abstract Builder<T> shape(Shape shape); + + public abstract Cast<T> build(); + } + + public static <T> Builder<T> builder() { + return new AutoValue_Cast.Builder<T>(); + } + + public static <T> Cast<T> to(Schema outputSchema) { + return Cast.<T>builder() + .outputSchema(outputSchema) + .nullability(Nullability.IGNORE) + .type(Type.WIDEN) + .shape(Shape.PROJECTION) + .build(); + } + + public List<CompatibilityError> compatibility(Schema inputSchema) { + return Inference.compatibility(inputSchema, outputSchema(), nullability(), type(), shape()); + } + + public void verifyCompatibility(Schema inputSchema) { + List<CompatibilityError> errors = compatibility(inputSchema); + + if (!errors.isEmpty()) { + String reason = + errors + .stream() + .map(x -> Joiner.on('.').join(x.path()) + ": " + x.message()) + .collect(Collectors.joining("\n\t")); + + throw new IllegalArgumentException("Cast isn't compatible:\n\t" + reason); + } + } + + @Override + public PCollection<Row> expand(PCollection<T> input) { + Schema inputSchema = input.getSchema(); + + verifyCompatibility(inputSchema); + + return input + .apply( + ParDo.of( + new DoFn<T, Row>() { + // TODO: This should be the same as resolved so that Beam knows which fields + // are being accessed. Currently Beam only supports wildcard descriptors. + // Once BEAM-4457 is fixed, fix this. + @FieldAccess("filterFields") + final FieldAccessDescriptor fieldAccessDescriptor = + FieldAccessDescriptor.withAllFields(); + + @ProcessElement + public void process(@FieldAccess("filterFields") Row row, OutputReceiver<Row> r) { + r.output(castRow(row, inputSchema, outputSchema())); + } + })) + .setRowSchema(outputSchema()); + } + + @VisibleForTesting + static Row castRow(Row input, Schema inputSchema, Schema outputSchema) { Review comment: @kennknowles I was thinking about it, but I'm not sure it's stable enough. What is the policy regarding version compatibility? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 160713) Time Spent: 1h 10m (was: 1h) > Add Cast transform for Rows > --------------------------- > > Key: BEAM-5918 > URL: https://issues.apache.org/jira/browse/BEAM-5918 > Project: Beam > Issue Type: Improvement > Components: sdk-java-core > Reporter: Gleb Kanterov > Assignee: Kenneth Knowles > Priority: Major > Time Spent: 1h 10m > Remaining Estimate: 0h > > There is a need for a generic transform that given two Row schemas will > convert rows between them. There must be a possibility to opt-out from > certain kind of conversions, for instance, converting ints to shorts can > cause overflow. Another example, a schema could have a nullable field, but > never have NULL value in practice, because it was filtered out. > What is needed: > - widening values (e.g., int -> long) > - narrowwing (e.g., int -> short) > - runtime check for overflow while narrowing > - ignoring nullability (nullable=true -> nullable=false) > - weakening nullability (nullable=false -> nullable=true) > - projection (Schema(a: Int32, b: Int32) -> Schema(a: Int32)) -- This message was sent by Atlassian JIRA (v7.6.3#76005)