-
Notifications
You must be signed in to change notification settings - Fork 19
/
DataTypeFactoryImpl.java
165 lines (141 loc) · 6.54 KB
/
DataTypeFactoryImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* 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.catalog;
import org.apache.flink.annotation.Internal;
import org.apache.flink.api.common.ExecutionConfig;
import org.apache.flink.api.common.serialization.SerializerConfig;
import org.apache.flink.api.common.serialization.SerializerConfigImpl;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer;
import org.apache.flink.configuration.ReadableConfig;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.TableException;
import org.apache.flink.table.api.ValidationException;
import org.apache.flink.table.types.AbstractDataType;
import org.apache.flink.table.types.DataType;
import org.apache.flink.table.types.UnresolvedDataType;
import org.apache.flink.table.types.extraction.DataTypeExtractor;
import org.apache.flink.table.types.logical.LogicalType;
import org.apache.flink.table.types.logical.LogicalTypeRoot;
import org.apache.flink.table.types.logical.UnresolvedUserDefinedType;
import org.apache.flink.table.types.logical.utils.LogicalTypeDuplicator;
import org.apache.flink.table.types.logical.utils.LogicalTypeParser;
import org.apache.flink.table.types.utils.TypeInfoDataTypeConverter;
import javax.annotation.Nullable;
import java.util.function.Supplier;
import static org.apache.flink.table.types.utils.TypeConversions.fromLogicalToDataType;
/** Implementation of a {@link DataTypeFactory}. */
@Internal
final class DataTypeFactoryImpl implements DataTypeFactory {
private final LogicalTypeResolver resolver = new LogicalTypeResolver();
private final ClassLoader classLoader;
private final Supplier<SerializerConfig> serializerConfig;
DataTypeFactoryImpl(
ClassLoader classLoader,
ReadableConfig config,
@Nullable SerializerConfig serializerConfig) {
this.classLoader = classLoader;
this.serializerConfig = createSerializerConfig(classLoader, config, serializerConfig);
}
@Override
public DataType createDataType(AbstractDataType<?> abstractDataType) {
if (abstractDataType instanceof DataType) {
return (DataType) abstractDataType;
} else if (abstractDataType instanceof UnresolvedDataType) {
return ((UnresolvedDataType) abstractDataType).toDataType(this);
}
throw new ValidationException("Unsupported abstract data type.");
}
@Override
public DataType createDataType(String typeString) {
return fromLogicalToDataType(createLogicalType(typeString));
}
@Override
public DataType createDataType(UnresolvedIdentifier identifier) {
return fromLogicalToDataType(createLogicalType(identifier));
}
@Override
public <T> DataType createDataType(Class<T> clazz) {
return DataTypeExtractor.extractFromType(this, clazz);
}
@Override
public <T> DataType createDataType(TypeInformation<T> typeInfo) {
return TypeInfoDataTypeConverter.toDataType(this, typeInfo);
}
@Override
public <T> DataType createRawDataType(Class<T> clazz) {
// we assume that a RAW type is nullable by default
return DataTypes.RAW(clazz, new KryoSerializer<>(clazz, serializerConfig.get()));
}
@Override
public <T> DataType createRawDataType(TypeInformation<T> typeInfo) {
// we assume that a RAW type is nullable by default
return DataTypes.RAW(
typeInfo.getTypeClass(), typeInfo.createSerializer(serializerConfig.get()));
}
@Override
public LogicalType createLogicalType(String typeString) {
final LogicalType parsedType = LogicalTypeParser.parse(typeString, classLoader);
return parsedType.accept(resolver);
}
@Override
public LogicalType createLogicalType(UnresolvedIdentifier identifier) {
if (!identifier.getDatabaseName().isPresent()) {
return createLogicalType(identifier.getObjectName());
}
return resolveType(identifier);
}
// --------------------------------------------------------------------------------------------
/**
* Creates a lazy {@link ExecutionConfig} that contains options for {@link TypeSerializer}s with
* information from existing {@link ExecutionConfig} (if available) enriched with table {@link
* ReadableConfig}.
*/
private static Supplier<SerializerConfig> createSerializerConfig(
ClassLoader classLoader, ReadableConfig config, SerializerConfig serializerConfig) {
return () -> {
SerializerConfig newSerializerConfig = new SerializerConfigImpl();
if (serializerConfig != null) {
newSerializerConfig = serializerConfig.copy();
} else {
newSerializerConfig.configure(config, classLoader);
}
return newSerializerConfig;
};
}
/** Resolves all {@link UnresolvedUserDefinedType}s. */
private class LogicalTypeResolver extends LogicalTypeDuplicator {
@Override
protected LogicalType defaultMethod(LogicalType logicalType) {
if (logicalType.is(LogicalTypeRoot.UNRESOLVED)) {
final UnresolvedUserDefinedType unresolvedType =
(UnresolvedUserDefinedType) logicalType;
return resolveType(unresolvedType.getUnresolvedIdentifier())
.copy(unresolvedType.isNullable());
}
return logicalType;
}
}
private LogicalType resolveType(UnresolvedIdentifier identifier) {
assert identifier != null;
// TODO validate implementation class of structured types when converting from LogicalType
// to DataType
throw new TableException("User-defined types are not supported yet.");
}
}