You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by tw...@apache.org on 2019/05/03 13:23:14 UTC

[flink] branch master updated: [FLINK-12253][table-common] Add a VARBINARY type

This is an automated email from the ASF dual-hosted git repository.

twalthr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/master by this push:
     new f743b1b  [FLINK-12253][table-common] Add a VARBINARY type
f743b1b is described below

commit f743b1b94011b066ea68af8d5f9fce08315ff7eb
Author: Timo Walther <tw...@apache.org>
AuthorDate: Tue Apr 30 16:58:26 2019 +0200

    [FLINK-12253][table-common] Add a VARBINARY type
---
 .../table/types/logical/LogicalTypeVisitor.java    |   2 +
 .../flink/table/types/logical/VarBinaryType.java   | 133 +++++++++++++++++++++
 .../apache/flink/table/types/LogicalTypesTest.java |  14 +++
 3 files changed, 149 insertions(+)

diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/LogicalTypeVisitor.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/LogicalTypeVisitor.java
index 509b84b..c7192a9 100644
--- a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/LogicalTypeVisitor.java
+++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/LogicalTypeVisitor.java
@@ -37,5 +37,7 @@ public interface LogicalTypeVisitor<R> {
 
 	R visit(BinaryType binaryType);
 
+	R visit(VarBinaryType varBinaryType);
+
 	R visit(LogicalType other);
 }
diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/VarBinaryType.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/VarBinaryType.java
new file mode 100644
index 0000000..7ed6b5a
--- /dev/null
+++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/VarBinaryType.java
@@ -0,0 +1,133 @@
+/*
+ * 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.flink.table.types.logical;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.ValidationException;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Logical type of a variable-length binary string (=a sequence of bytes).
+ *
+ * <p>The serialized string representation is {@code VARBINARY(n)} where {@code n} is the maximum
+ * number of bytes. {@code n} must have a value between 1 and {@link Integer#MAX_VALUE} (both
+ * inclusive). If no length is specified, {@code n} is equal to 1.
+ */
+@PublicEvolving
+public final class VarBinaryType extends LogicalType {
+
+	private static final int MIN_LENGTH = 1;
+
+	private static final int MAX_LENGTH = Integer.MAX_VALUE;
+
+	private static final int DEFAULT_LENGTH = 1;
+
+	private static final String FORMAT = "VARBINARY(%d)";
+
+	private static final Set<String> INPUT_OUTPUT_CONVERSION = conversionSet(
+		byte[].class.getName(),
+		"org.apache.flink.table.dataformat.BinaryArray");
+
+	private static final Class<?> DEFAULT_CONVERSION = byte[].class;
+
+	private final int length;
+
+	public VarBinaryType(boolean isNullable, int length) {
+		super(isNullable, LogicalTypeRoot.VARBINARY);
+		if (length < MIN_LENGTH) {
+			throw new ValidationException(
+				String.format(
+					"Variable binary string length must be between %d and %d (both inclusive).",
+					MIN_LENGTH,
+					MAX_LENGTH));
+		}
+		this.length = length;
+	}
+
+	public VarBinaryType(int length) {
+		this(true, length);
+	}
+
+	public VarBinaryType() {
+		this(DEFAULT_LENGTH);
+	}
+
+	public int getLength() {
+		return length;
+	}
+
+	@Override
+	public LogicalType copy(boolean isNullable) {
+		return new VarBinaryType(isNullable, length);
+	}
+
+	@Override
+	public String asSerializableString() {
+		return withNullability(FORMAT, length);
+	}
+
+	@Override
+	public boolean supportsInputConversion(Class<?> clazz) {
+		return INPUT_OUTPUT_CONVERSION.contains(clazz.getName());
+	}
+
+	@Override
+	public boolean supportsOutputConversion(Class<?> clazz) {
+		return INPUT_OUTPUT_CONVERSION.contains(clazz.getName());
+	}
+
+	@Override
+	public Class<?> getDefaultConversion() {
+		return DEFAULT_CONVERSION;
+	}
+
+	@Override
+	public List<LogicalType> getChildren() {
+		return Collections.emptyList();
+	}
+
+	@Override
+	public <R> R accept(LogicalTypeVisitor<R> visitor) {
+		return visitor.visit(this);
+	}
+
+	@Override
+	public boolean equals(Object o) {
+		if (this == o) {
+			return true;
+		}
+		if (o == null || getClass() != o.getClass()) {
+			return false;
+		}
+		if (!super.equals(o)) {
+			return false;
+		}
+		VarBinaryType that = (VarBinaryType) o;
+		return length == that.length;
+	}
+
+	@Override
+	public int hashCode() {
+		return Objects.hash(super.hashCode(), length);
+	}
+}
diff --git a/flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/LogicalTypesTest.java b/flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/LogicalTypesTest.java
index e764679..2fe2f97 100644
--- a/flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/LogicalTypesTest.java
+++ b/flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/LogicalTypesTest.java
@@ -22,6 +22,7 @@ import org.apache.flink.table.types.logical.BinaryType;
 import org.apache.flink.table.types.logical.BooleanType;
 import org.apache.flink.table.types.logical.CharType;
 import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.VarBinaryType;
 import org.apache.flink.table.types.logical.VarCharType;
 import org.apache.flink.util.InstantiationUtil;
 
@@ -92,6 +93,19 @@ public class LogicalTypesTest {
 		);
 	}
 
+	@Test
+	public void testVarBinaryType() {
+		testAll(
+			new VarBinaryType(22),
+			"VARBINARY(22)",
+			"VARBINARY(22)",
+			new Class[]{byte[].class},
+			new Class[]{byte[].class},
+			new LogicalType[]{},
+			new VarBinaryType()
+		);
+	}
+
 	// --------------------------------------------------------------------------------------------
 
 	private static void testAll(