You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/03/23 13:38:27 UTC

[GitHub] [beam] zhoufek commented on a change in pull request #16762: [BEAM-12697] Add primitive field generation from IR to SBE extension

zhoufek commented on a change in pull request #16762:
URL: https://github.com/apache/beam/pull/16762#discussion_r833277570



##########
File path: sdks/java/extensions/sbe/src/main/java/org/apache/beam/sdk/extensions/sbe/SbeSchema.java
##########
@@ -0,0 +1,252 @@
+/*
+ * 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.sbe;
+
+import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState;
+
+import com.google.auto.value.AutoValue;
+import java.io.Serializable;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.io.payloads.PayloadSerializerProvider;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import uk.co.real_logic.sbe.ir.Ir;
+
+/**
+ * Represents an SBE schema that can be translated to a Beam {@link Schema} and {@link
+ * PayloadSerializerProvider}.
+ *
+ * <p>The schema represents a single SBE message. If the XML schema contains more than one message,
+ * then a new instance must be created for each message that the pipeline will work with.
+ *
+ * <p>The currently supported ways of generating a schema are:
+ *
+ * <ul>
+ *   <li>Through an intermediate representation ({@link Ir}).
+ * </ul>
+ *
+ * <h3>Intermediate Representation</h3>
+ *
+ * <p>An {@link Ir} allows for a reflection-less way of getting a very accurate representation of
+ * the SBE schema, since it is a tokenized form of the original XML schema. To help deal with some
+ * ambiguities, such as which message to base the schema around, passing {@link IrOptions} is
+ * required. See the Javadoc for the options for more details.
+ *
+ * <p>At this time, we cannot support serialization to an SBE message through IR. As a result, the
+ * {@code byte[]} output from the {@link PayloadSerializerProvider} serializer will be from a JSON
+ * representation of the message, not an SBE-serialized message. Downstream systems will need to
+ * account for this.
+ */
+@Experimental(Kind.SCHEMAS)
+public final class SbeSchema implements Serializable {
+  private static final long serialVersionUID = 1L;
+
+  private final @Nullable SerializableIr ir;
+  private final @Nullable IrOptions irOptions;
+  private final ImmutableList<SbeField> sbeFields;
+
+  private SbeSchema(
+      @Nullable SerializableIr ir,
+      @Nullable IrOptions irOptions,
+      ImmutableList<SbeField> sbeFields) {
+    this.ir = ir;
+    this.irOptions = irOptions;
+    this.sbeFields = sbeFields;
+  }
+
+  /**
+   * Creates a new {@link SbeSchema} from the given intermediate representation.
+   *
+   * <p>This makes no guarantees about the state of the returned instance. That is, it may or may
+   * not have the generated SBE schema representation, and it may or may not have translated the SBE
+   * schema into a Beam schema.
+   *
+   * @param ir the intermediate representation of the SBE schema. Modifications to the passed-in
+   *     value will not be reflected in the returned instance.
+   * @param irOptions options for configuring how to deal with cases where the desired behavior is
+   *     ambiguous.
+   * @return a new {@link SbeSchema} instance
+   */
+  public static SbeSchema fromIr(Ir ir, IrOptions irOptions) {
+    ImmutableList<SbeField> sbeFields = IrFieldGenerator.generateFields(ir, irOptions);
+
+    Ir copy =
+        new Ir(
+            ir.packageName(),
+            ir.namespaceName(),
+            ir.id(),
+            ir.version(),
+            ir.description(),
+            ir.semanticVersion(),
+            ir.byteOrder(),
+            ImmutableList.copyOf(ir.headerStructure().tokens()));
+
+    return new SbeSchema(SerializableIr.fromIr(copy), irOptions, sbeFields);
+  }
+
+  @VisibleForTesting
+  @Nullable
+  Ir getIr() {
+    return ir == null ? null : ir.ir();
+  }
+
+  @VisibleForTesting
+  @Nullable
+  IrOptions getIrOptions() {
+    return irOptions;
+  }
+
+  @VisibleForTesting
+  ImmutableList<SbeField> getSbeFields() {
+    return sbeFields;
+  }
+
+  /**
+   * Options for controlling what to do with unsigned types, specifically whether to use a higher
+   * bit count or, in the case of uint64, a string.
+   */
+  @AutoValue
+  public abstract static class UnsignedOptions implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+    public abstract Boolean useMoreBitsForUint8();
+
+    public abstract Boolean useMoreBitsForUint16();
+
+    public abstract Boolean useMoreBitsForUint32();
+
+    public abstract Boolean useStringForUint64();

Review comment:
       Starting off with keeping the same bit count, changing to next higher bit count, converting to string, and converting to `BigDecimal`.




-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org