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 2020/06/18 18:57:30 UTC

[GitHub] [beam] TheNeuralBit commented on a change in pull request #11581: [BEAM-8307] NPE in Calcite dialect when input PCollection has logical…

TheNeuralBit commented on a change in pull request #11581:
URL: https://github.com/apache/beam/pull/11581#discussion_r442424891



##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/logicaltypes/IdenticalBaseTAndInputTLogicalType.java
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.logicaltypes;
+
+import java.util.Objects;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.schemas.Schema;
+
+/** A base class for LogicalTypes that use the same input type as the underlying base type. */
+@Experimental(Experimental.Kind.SCHEMAS)
+public abstract class IdenticalBaseTAndInputTLogicalType<T> implements Schema.LogicalType<T, T> {

Review comment:
       Can you make this package-private?

##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/logicaltypes/IdenticalBaseTAndInputTLogicalType.java
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.logicaltypes;
+
+import java.util.Objects;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.schemas.Schema;
+
+/** A base class for LogicalTypes that use the same input type as the underlying base type. */
+@Experimental(Experimental.Kind.SCHEMAS)
+public abstract class IdenticalBaseTAndInputTLogicalType<T> implements Schema.LogicalType<T, T> {

Review comment:
       Could you make this package-private? I think its only used in `schemas.logicaltypes` right now

##########
File path: sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/utils/CalciteUtilsTest.java
##########
@@ -59,68 +64,121 @@ public void setUp() {
   public void testToCalciteRowType() {
     final Schema schema =
         Schema.builder()
-            .addField("f1", Schema.FieldType.BYTE)
-            .addField("f2", Schema.FieldType.INT16)
-            .addField("f3", Schema.FieldType.INT32)
-            .addField("f4", Schema.FieldType.INT64)
-            .addField("f5", Schema.FieldType.FLOAT)
-            .addField("f6", Schema.FieldType.DOUBLE)
-            .addField("f7", Schema.FieldType.DECIMAL)
-            .addField("f8", Schema.FieldType.BOOLEAN)
-            .addField("f9", Schema.FieldType.BYTES)
-            .addField("f10", Schema.FieldType.STRING)
+            .addField("byte", Schema.FieldType.BYTE)
+            .addField("short", Schema.FieldType.INT16)
+            .addField("int", Schema.FieldType.INT32)
+            .addField("long", Schema.FieldType.INT64)
+            .addField("float", Schema.FieldType.FLOAT)
+            .addField("double", Schema.FieldType.DOUBLE)
+            .addField("decimal", Schema.FieldType.DECIMAL)
+            .addField("boolean", Schema.FieldType.BOOLEAN)
+            .addField("byteArray", Schema.FieldType.BYTES)
+            .addField("string", Schema.FieldType.STRING)
+            .addField("char", CalciteUtils.CHAR)
+            .addField("date", CalciteUtils.DATE)
+            .addField("time", CalciteUtils.TIME)
+            .addField("timestamp", CalciteUtils.TIMESTAMP)
+            .addField("timeWithLocalTZ", CalciteUtils.TIME_WITH_LOCAL_TZ)
+            .addField("timestampWithLocalTZ", CalciteUtils.TIMESTAMP_WITH_LOCAL_TZ)
+            .addField("fixedBytes", Schema.FieldType.logicalType(FixedBytes.of(10)))
+            .addField("variableBytes", Schema.FieldType.logicalType(VariableLengthBytes.of(100)))
+            .addField("fixedString", Schema.FieldType.logicalType(FixedLengthString.of(10)))
+            .addField("variableString", Schema.FieldType.logicalType(VariableLengthString.of(100)))
+            .addField("customDecimal", Schema.FieldType.logicalType(LogicalDecimal.of(10, 5)))

Review comment:
       Could you add tests that use some or all of these new types in a SQL query? Right now I think this is the only test that exercises the new code in CalciteUtils, and it's not actually verifying the types will work in a SQL statement.

##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/logicaltypes/FixedLengthString.java
##########
@@ -0,0 +1,55 @@
+/*
+ * 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.logicaltypes;
+
+import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.schemas.Schema;
+
+/** A LogicalType representing a fixed-length string. */
+@Experimental(Experimental.Kind.SCHEMAS)
+public class FixedLengthString extends IdenticalBaseTAndInputTLogicalType<String> {
+  public static final String IDENTIFIER = "beam:logical_type:fixed_length_string:v1";
+  private final int length;
+
+  private FixedLengthString(int length) {
+    super(IDENTIFIER, Schema.FieldType.INT32, length, Schema.FieldType.STRING);
+    this.length = length;
+  }
+
+  public int getLength() {
+    return length;
+  }
+
+  public static FixedLengthString of(int length) {
+    return new FixedLengthString(length);
+  }
+
+  @Override
+  public String toBaseType(String input) {
+    checkArgument(input == null || input.length() == length);
+    return input;
+  }
+
+  @Override
+  public String toInputType(String base) {
+    checkArgument(base == null || base.length() == length);
+    return base;
+  }
+}

Review comment:
       Could you add constants for these new logical types in [SqlTypes](https://github.com/apache/beam/blob/master/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/logicaltypes/SqlTypes.java)?
   
   cc: @robinyqiu 

##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/logicaltypes/LogicalDecimal.java
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.logicaltypes;
+
+import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import java.math.BigDecimal;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.values.Row;
+
+/** A LogicalType representing a Decimal type with custom precision and scale. */
+@Experimental(Experimental.Kind.SCHEMAS)
+public class LogicalDecimal extends IdenticalBaseTAndInputTLogicalType<BigDecimal> {

Review comment:
       Can we just call this `Decimal`?




----------------------------------------------------------------
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:
users@infra.apache.org