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 8e8eb35 [FLINK-12253][table-common] Add a MULTISET type 8e8eb35 is described below commit 8e8eb357b387f090ea2c44965b60e7922a98e5ad Author: Timo Walther <twal...@apache.org> AuthorDate: Thu May 2 11:35:29 2019 +0200 [FLINK-12253][table-common] Add a MULTISET type --- .../table/types/logical/LogicalTypeVisitor.java | 2 + .../flink/table/types/logical/MultisetType.java | 127 +++++++++++++++++++++ .../apache/flink/table/types/LogicalTypesTest.java | 25 ++++ 3 files changed, 154 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 877007b..33a496a 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 @@ -69,5 +69,7 @@ public interface LogicalTypeVisitor<R> { R visit(ArrayType arrayType); + R visit(MultisetType multisetType); + R visit(LogicalType other); } diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/MultisetType.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/MultisetType.java new file mode 100644 index 0000000..c093306 --- /dev/null +++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/MultisetType.java @@ -0,0 +1,127 @@ +/* + * 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.util.Preconditions; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +/** + * Logical type of a multiset (=bag). Unlike a set, it allows for multiple instances for each of its + * elements with a common subtype. Each unique value (including {@code NULL}) is mapped to some + * multiplicity. There is no restriction of element types; it is the responsibility of the user to + * ensure uniqueness. + * + * <p>The serialized string representation is {@code MULTISET<t>} where {@code t} is the logical type + * of the contained elements. + * + * <p>A conversion is possible through a map that assigns each value to an integer multiplicity + * ({@code Map<t, Integer>}). + */ +@PublicEvolving +public final class MultisetType extends LogicalType { + + private static final String FORMAT = "MULTISET<%s>"; + + private static final Set<String> INPUT_OUTPUT_CONVERSION = conversionSet( + Map.class.getName(), + "org.apache.flink.table.dataformat.BinaryMap"); + + private static final Class<?> DEFAULT_CONVERSION = Map.class; + + private final LogicalType elementType; + + public MultisetType(boolean isNullable, LogicalType elementType) { + super(isNullable, LogicalTypeRoot.MULTISET); + this.elementType = Preconditions.checkNotNull(elementType, "Element type must not be null."); + } + + public MultisetType(LogicalType elementType) { + this(true, elementType); + } + + public LogicalType getElementType() { + return elementType; + } + + @Override + public LogicalType copy(boolean isNullable) { + return new MultisetType(isNullable, elementType.copy()); + } + + @Override + public String asSummaryString() { + return withNullability(FORMAT, elementType.asSummaryString()); + } + + @Override + public String asSerializableString() { + return withNullability(FORMAT, elementType.asSerializableString()); + } + + @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.singletonList(elementType); + } + + @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; + } + MultisetType that = (MultisetType) o; + return elementType.equals(that.elementType); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), elementType); + } +} 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 e701da6..1e621d7 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 @@ -31,6 +31,7 @@ import org.apache.flink.table.types.logical.FloatType; import org.apache.flink.table.types.logical.IntType; import org.apache.flink.table.types.logical.LocalZonedTimestampType; import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.MultisetType; import org.apache.flink.table.types.logical.SmallIntType; import org.apache.flink.table.types.logical.TimeType; import org.apache.flink.table.types.logical.TimestampType; @@ -46,6 +47,7 @@ import org.junit.Test; import java.math.BigDecimal; import java.util.Arrays; +import java.util.Map; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -331,6 +333,29 @@ public class LogicalTypesTest { assertFalse(nestedArray.supportsOutputConversion(java.sql.Timestamp[].class)); } + @Test + public void testMultisetType() { + testAll( + new MultisetType(new TimestampType()), + "MULTISET<TIMESTAMP(6)>", + "MULTISET<TIMESTAMP(6)>", + new Class[]{Map.class}, + new Class[]{Map.class}, + new LogicalType[]{new TimestampType()}, + new MultisetType(new SmallIntType()) + ); + + testAll( + new MultisetType(new MultisetType(new TimestampType())), + "MULTISET<MULTISET<TIMESTAMP(6)>>", + "MULTISET<MULTISET<TIMESTAMP(6)>>", + new Class[]{Map.class}, + new Class[]{Map.class}, + new LogicalType[]{new MultisetType(new TimestampType())}, + new MultisetType(new MultisetType(new SmallIntType())) + ); + } + // -------------------------------------------------------------------------------------------- private static void testAll(