|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.dtstack.chunjun.connector.opengauss.converter; |
| 20 | + |
| 21 | +import com.dtstack.chunjun.config.TypeConfig; |
| 22 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.BitType; |
| 23 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.BoolType; |
| 24 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.BpcharType; |
| 25 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.ByteaType; |
| 26 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.CharType; |
| 27 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.DateType; |
| 28 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.Float4Type; |
| 29 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.Float8Type; |
| 30 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.Int2Type; |
| 31 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.Int4Type; |
| 32 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.Int8Type; |
| 33 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.JsonType; |
| 34 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.JsonbType; |
| 35 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.MoneyType; |
| 36 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.NameType; |
| 37 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.NumericType; |
| 38 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.OidType; |
| 39 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.PointType; |
| 40 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.TextType; |
| 41 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.TimeType; |
| 42 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.TimestampType; |
| 43 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.TimestampTzType; |
| 44 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.TimetzType; |
| 45 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.UuidType; |
| 46 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.VarbitType; |
| 47 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.VarcharType; |
| 48 | +import com.dtstack.chunjun.connector.opengauss.converter.logical.XmlType; |
| 49 | +import com.dtstack.chunjun.throwable.UnsupportedTypeException; |
| 50 | + |
| 51 | +import org.apache.flink.table.api.DataTypes; |
| 52 | +import org.apache.flink.table.types.AtomicDataType; |
| 53 | +import org.apache.flink.table.types.DataType; |
| 54 | +import org.apache.flink.table.types.logical.LogicalTypeRoot; |
| 55 | + |
| 56 | +public class OpengaussRawTypeMapper { |
| 57 | + |
| 58 | + /** |
| 59 | + * https://docs-opengauss.osinfra.cn/zh/docs/5.0.0/docs/BriefTutorial/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B.html |
| 60 | + * |
| 61 | + * @param type |
| 62 | + */ |
| 63 | + public static DataType apply(TypeConfig type) { |
| 64 | + switch (type.getType()) { |
| 65 | + case "BIT": |
| 66 | + return new AtomicDataType(new BitType(true, LogicalTypeRoot.BOOLEAN, false)); |
| 67 | + case "BOOLEAN": |
| 68 | + case "BOOL": |
| 69 | + return DataTypes.BOOLEAN(); |
| 70 | + case "SMALLINT": |
| 71 | + case "SMALLSERIAL": |
| 72 | + case "INT2": |
| 73 | + case "INT": |
| 74 | + case "INTEGER": |
| 75 | + case "SERIAL": |
| 76 | + case "INT4": |
| 77 | + return DataTypes.INT(); |
| 78 | + case "BIGINT": |
| 79 | + case "BIGSERIAL": |
| 80 | + case "OID": |
| 81 | + case "INT8": |
| 82 | + return DataTypes.BIGINT(); |
| 83 | + case "REAL": |
| 84 | + case "FLOAT4": |
| 85 | + return DataTypes.FLOAT(); |
| 86 | + case "FLOAT": |
| 87 | + case "DOUBLE PRECISION": |
| 88 | + case "FLOAT8": |
| 89 | + return DataTypes.DOUBLE(); |
| 90 | + case "MONEY": |
| 91 | + return new AtomicDataType(new MoneyType(true, LogicalTypeRoot.DOUBLE, false)); |
| 92 | + case "DECIMAL": |
| 93 | + case "NUMERIC": |
| 94 | + return type.toDecimalDataType(); |
| 95 | + case "CHARACTER VARYING": |
| 96 | + case "VARCHAR": |
| 97 | + case "CHARACTER": |
| 98 | + case "CHAR": |
| 99 | + case "TEXT": |
| 100 | + case "NAME": |
| 101 | + case "BPCHAR": |
| 102 | + return DataTypes.STRING(); |
| 103 | + // Binary Data Types |
| 104 | + case "BYTEA": |
| 105 | + return DataTypes.BYTES(); |
| 106 | + case "VARBIT": |
| 107 | + return new AtomicDataType(new VarbitType(true, LogicalTypeRoot.VARCHAR, false)); |
| 108 | + case "XML": |
| 109 | + return new AtomicDataType(new XmlType(true, LogicalTypeRoot.VARCHAR, false)); |
| 110 | + case "UUID": |
| 111 | + return new AtomicDataType(new UuidType(true, LogicalTypeRoot.VARCHAR, false)); |
| 112 | + case "POINT": |
| 113 | + return new AtomicDataType(new PointType(true, LogicalTypeRoot.VARCHAR, false)); |
| 114 | + // Date/Time Types |
| 115 | + case "ABSTIME": |
| 116 | + case "TIMESTAMP": |
| 117 | + case "TIMESTAMPTZ": |
| 118 | + return type.toTimestampDataType(6); |
| 119 | + case "DATE": |
| 120 | + return DataTypes.DATE(); |
| 121 | + case "TIME": |
| 122 | + case "TIMETZ": |
| 123 | + // todo check sync |
| 124 | + return type.toTimeDataType(6); |
| 125 | + case "JSON": |
| 126 | + return new AtomicDataType(new JsonType(true, LogicalTypeRoot.VARCHAR, false)); |
| 127 | + case "JSONB": |
| 128 | + return new AtomicDataType(new JsonbType(true, LogicalTypeRoot.VARCHAR, false)); |
| 129 | + case "_BIT": |
| 130 | + return new AtomicDataType(new BitType(true, LogicalTypeRoot.VARCHAR, true)); |
| 131 | + case "_BOOL": |
| 132 | + return new AtomicDataType(new BoolType(true, LogicalTypeRoot.VARCHAR, true)); |
| 133 | + case "_INT2": |
| 134 | + return new AtomicDataType(new Int2Type(true, LogicalTypeRoot.VARCHAR, true)); |
| 135 | + case "_INT4": |
| 136 | + return new AtomicDataType(new Int4Type(true, LogicalTypeRoot.VARCHAR, true)); |
| 137 | + case "_INT8": |
| 138 | + return new AtomicDataType(new Int8Type(true, LogicalTypeRoot.VARCHAR, true)); |
| 139 | + case "_FLOAT4": |
| 140 | + return new AtomicDataType(new Float4Type(true, LogicalTypeRoot.VARCHAR, true)); |
| 141 | + case "_FLOAT8": |
| 142 | + return new AtomicDataType(new Float8Type(true, LogicalTypeRoot.VARCHAR, true)); |
| 143 | + case "_NUMERIC": |
| 144 | + return new AtomicDataType(new NumericType(true, LogicalTypeRoot.VARCHAR, true)); |
| 145 | + case "_TIME": |
| 146 | + return new AtomicDataType(new TimeType(true, LogicalTypeRoot.VARCHAR, true)); |
| 147 | + case "_TIMETZ": |
| 148 | + return new AtomicDataType(new TimetzType(true, LogicalTypeRoot.VARCHAR, true)); |
| 149 | + case "_TIMESTAMP": |
| 150 | + return new AtomicDataType(new TimestampType(true, LogicalTypeRoot.VARCHAR, true)); |
| 151 | + case "_TIMESTAMPTZ": |
| 152 | + return new AtomicDataType(new TimestampTzType(true, LogicalTypeRoot.VARCHAR, true)); |
| 153 | + case "_DATE": |
| 154 | + return new AtomicDataType(new DateType(true, LogicalTypeRoot.VARCHAR, true)); |
| 155 | + case "_BYTEA": |
| 156 | + return new AtomicDataType(new ByteaType(true, LogicalTypeRoot.VARCHAR, true)); |
| 157 | + case "_VARCHAR": |
| 158 | + return new AtomicDataType(new VarcharType(true, LogicalTypeRoot.VARCHAR, true)); |
| 159 | + case "_OID": |
| 160 | + return new AtomicDataType(new OidType(true, LogicalTypeRoot.VARCHAR, true)); |
| 161 | + case "_BPCHAR": |
| 162 | + return new AtomicDataType(new BpcharType(true, LogicalTypeRoot.VARCHAR, true)); |
| 163 | + case "_TEXT": |
| 164 | + return new AtomicDataType(new TextType(true, LogicalTypeRoot.VARCHAR, true)); |
| 165 | + case "_MONEY": |
| 166 | + return new AtomicDataType(new MoneyType(true, LogicalTypeRoot.VARCHAR, true)); |
| 167 | + // case "_INTERVAL": |
| 168 | + case "_CHAR": |
| 169 | + return new AtomicDataType(new CharType(true, LogicalTypeRoot.VARCHAR, true)); |
| 170 | + case "_VARBIT": |
| 171 | + return new AtomicDataType(new VarbitType(true, LogicalTypeRoot.VARCHAR, true)); |
| 172 | + case "_NAME": |
| 173 | + return new AtomicDataType(new NameType(true, LogicalTypeRoot.VARCHAR, true)); |
| 174 | + case "_UUID": |
| 175 | + return new AtomicDataType(new UuidType(true, LogicalTypeRoot.VARCHAR, true)); |
| 176 | + case "_XML": |
| 177 | + return new AtomicDataType(new XmlType(true, LogicalTypeRoot.VARCHAR, true)); |
| 178 | + // case "_POINT": |
| 179 | + // case "_JSONB": |
| 180 | + // case "_JSON": |
| 181 | + // case "_REF_CURSOR": |
| 182 | + |
| 183 | + // 以下类型无法支持 |
| 184 | + // Enumerated Types |
| 185 | + |
| 186 | + // Geometric Types |
| 187 | + // case "LINE": |
| 188 | + // case "LSEG": |
| 189 | + // case "BOX": |
| 190 | + // case "PATH": |
| 191 | + // case "POLYGON": |
| 192 | + // case "CIRCLE": |
| 193 | + |
| 194 | + // Network Address Types |
| 195 | + |
| 196 | + // |
| 197 | + // // JSON Types |
| 198 | + // case "JSONB": |
| 199 | + // case "JSONPATH": |
| 200 | + // return DataTypes.STRING(); |
| 201 | + |
| 202 | + default: |
| 203 | + throw new UnsupportedTypeException(type); |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments