Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FLINK-12253][table-common] Setup a class hierarchy for the new logical type system #8335

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a6a8f2a
[FLINK-12253][table-common] Introduce logical type skeleton
twalthr Apr 23, 2019
143887c
[FLINK-12253][table-common] Add a CHAR type
twalthr Apr 30, 2019
d2fd35f
[FLINK-12253][table-common] Add a VARCHAR type
twalthr Apr 30, 2019
2bfc47d
[FLINK-12253][table-common] Add a BOOLEAN type
twalthr Apr 30, 2019
063398d
[FLINK-12253][table-common] Add a BINARY type
twalthr Apr 30, 2019
c0652a4
[FLINK-12253][table-common] Add a VARBINARY type
twalthr Apr 30, 2019
db0e8cb
[FLINK-12253][table-common] Add a DECIMAL type
twalthr Apr 30, 2019
52229e1
[FLINK-12253][table-common] Add a TINYINT type
twalthr Apr 30, 2019
a6fe3ff
[FLINK-12253][table-common] Add a SMALLINT type
twalthr Apr 30, 2019
908aa48
[FLINK-12253][table-common] Add a INT type
twalthr Apr 30, 2019
08fa733
[FLINK-12253][table-common] Add a BIGINT type
twalthr Apr 30, 2019
6fb6b8e
[FLINK-12253][table-common] Add a FLOAT type
twalthr May 2, 2019
6e0cffd
[FLINK-12253][table-common] Add a DOUBLE type
twalthr May 2, 2019
ea6a13c
[FLINK-12253][table-common] Add a DATE type
twalthr May 2, 2019
06ccb79
[FLINK-12253][table-common] Add a TIME type
twalthr May 2, 2019
73e2dee
[FLINK-12253][table-common] Add a TIMESTAMP type
twalthr May 2, 2019
00ea5ef
[FLINK-12253][table-common] Add a TIMESTAMP WITH TIME ZONE type
twalthr May 2, 2019
aabaf5a
[FLINK-12253][table-common] Add a TIMESTAMP WITH LOCAL TIME ZONE type
twalthr May 2, 2019
13580d4
[FLINK-12253][table-common] Add an INTERVAL YEAR TO MONTH type
twalthr May 2, 2019
efb19a7
[FLINK-12253][table-common] Add an INTERVAL DAY TO SECOND type
twalthr May 2, 2019
c4ac5ad
[FLINK-12253][table-common] Add an ARRAY type
twalthr May 2, 2019
65a71d3
[FLINK-12253][table-common] Add a MULTISET type
twalthr May 2, 2019
b266f0e
[FLINK-12253][table-common] Add a MAP type
twalthr May 2, 2019
7400873
[FLINK-12253][table-common] Add a ROW type
twalthr May 2, 2019
df20555
[FLINK-12253][table-common] Add user-defined types
twalthr May 2, 2019
2fdf042
[FLINK-12253][table-common] Add a NULL type
twalthr May 2, 2019
6da683b
[FLINK-12253][table-common] Add an ANY type
twalthr May 2, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,210 @@
/*
* 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.io.Serializable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
* A logical type that describes the data type of a value. It does not imply a concrete physical
* representation for transmission or storage but defines the boundaries between JVM-based languages
* and the table ecosystem.
*
* <p>The definition of a logical type is similar to the SQL standard's "data type" terminology but
* also contains information about the nullability of a value for efficient handling of scalar
* expressions.
*
* <p>Subclasses of this class define characteristics of built-in or user-defined types.
*
* <p>Instances of this class describe the fully parameterized, immutable type with additional
* information such as numeric precision or expected length.
*
* <p>NOTE: A logical type is just a description of a type, a planner or runtime might not support
* every type in every logical precision yet!
*/
@PublicEvolving
public abstract class LogicalType implements Serializable {

private final boolean isNullable;

private final LogicalTypeRoot typeRoot;

public LogicalType(boolean isNullable, LogicalTypeRoot typeRoot) {
this.isNullable = isNullable;
this.typeRoot = Preconditions.checkNotNull(typeRoot);
}

/**
* Returns whether a value of this type can be {@code null}.
*/
public boolean isNullable() {
return isNullable;
}

/**
* Returns the root of this type. It is an essential description without additional parameters.
*/
public LogicalTypeRoot getTypeRoot() {
return typeRoot;
}

/**
* Returns a deep copy of this type with possibly different nullability.
*
* @param isNullable the intended nullability of the copied type
* @return a deep copy
*/
public abstract LogicalType copy(boolean isNullable);

/**
* Returns a deep copy of this type.
*
* @return a deep copy
*/
public LogicalType copy() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this method final with a comment to implement LogicalType copy(boolean isNullable) instead?

return copy(isNullable);
}

/**
* Returns a string that fully serializes this instance. The serialized string can be used for
* transmitting or persisting a type.
*
* @return detailed string for transmission or persistence
*/
public abstract String asSerializableString();

/**
* Returns a string that summarizes this type for printing to a console. An implementation might
* shorten long names or skips very specific properties.
*
* <p>Use {@link #asSerializableString()} for a type string that fully serializes
* this instance.
*
* @return summary string of this type for debugging purposes
*/
public String asSummaryString() {
return asSerializableString();
}

/**
* Returns whether an instance of the given class can be represented as a value of this logical
* type when entering the table ecosystem. This method helps for the interoperability between
* JVM-based languages and the relational type system.
*
* <p>A supported conversion directly maps an input class to a logical type without loss of
* precision or type widening.
*
* <p>For example, {@code java.lang.Long} or {@code long} can be used as input for {@code BIGINT}
* independent of the set nullability.
*
* @param clazz input class to be converted into this logical type
* @return flag that indicates if instances of this class can be used as input into the table
* ecosystem
*/
public abstract boolean supportsInputConversion(Class<?> clazz);

/**
* Returns whether a value of this logical type can be represented as an instance of the given
* class when leaving the table ecosystem. This method helps for the interoperability between
* JVM-based languages and the relational type system.
*
* <p>A supported conversion directly maps a logical type to an output class without loss of
* precision or type widening.
*
* <p>For example, {@code java.lang.Long} or {@code long} can be used as output for {@code BIGINT}
* if the type is not nullable. If the type is nullable, only {@code java.lang.Long} can represent
* this.
*
* @param clazz output class to be converted from this logical type
* @return flag that indicates if instances of this class can be used as output from the table
* ecosystem
* @see #getDefaultOutputConversion()
*/
public abstract boolean supportsOutputConversion(Class<?> clazz);

/**
* Returns the default output conversion class. A value of this logical type will be represented
* as an instance of the given class when leaving the table ecosystem if no other conversion
* has been specified.
*
* <p>For example, {@code java.lang.Long} is the default output for {@code BIGINT}.
*
* @return output class to be converted from this logical type
* @see #supportsOutputConversion(Class)
*/
public abstract Class<?> getDefaultOutputConversion();

public abstract List<LogicalType> getChildren();

public abstract <R> R accept(LogicalTypeVisitor<R> visitor);

@Override
public String toString() {
return asSummaryString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LogicalType that = (LogicalType) o;
return isNullable == that.isNullable && typeRoot == that.typeRoot;
}

@Override
public int hashCode() {
return Objects.hash(isNullable, typeRoot);
}

// --------------------------------------------------------------------------------------------

protected String withNullability(String format, Object... params) {
if (!isNullable) {
return String.format(format + " NOT NULL", params);
}
return String.format(format, params);
}

protected static Set<String> conversionSet(String... elements) {
return new HashSet<>(Arrays.asList(elements));
}

protected static String escapeBackticks(String s) {
return s.replace("`", "``");
}

protected static String escapeSingleQuotes(String s) {
return s.replace("'", "''");
}

protected static String escapeIdentifier(String s) {
return "`" + escapeBackticks(s) + "`";
}
}
@@ -0,0 +1,60 @@
/*
* 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;

/**
* An enumeration of logical type families for clustering {@link LogicalTypeRoot}s into categories.
*
* <p>The enumeration is very close to the SQL standard in terms of naming and completeness. However,
* it reflects just a subset of the evolving standard and contains some extensions (indicated
* by {@code EXTENSION}).
*/
@PublicEvolving
public enum LogicalTypeFamily {

PREDEFINED,

CONSTRUCTED,

USER_DEFINED,

CHARACTER_STRING,

BINARY_STRING,

NUMERIC,

EXACT_NUMERIC,

APPROXIMATE_NUMERIC,

DATETIME,

TIME,

TIMESTAMP,

INTERVAL,

COLLECTION,

EXTENSION
}