You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by "XiaoJiang521 (via GitHub)" <gi...@apache.org> on 2023/09/15 02:06:56 UTC

[GitHub] [seatunnel] XiaoJiang521 commented on a diff in pull request #5440: [Feature] Add tidb datatype convertor

XiaoJiang521 commented on code in PR #5440:
URL: https://github.com/apache/seatunnel/pull/5440#discussion_r1326700094


##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/catalog/tidb/TiDBDataTypeConvertor.java:
##########
@@ -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.seatunnel.connectors.seatunnel.jdbc.catalog.tidb;
+
+import org.apache.seatunnel.api.table.catalog.DataTypeConvertException;
+import org.apache.seatunnel.api.table.catalog.DataTypeConvertor;
+import org.apache.seatunnel.api.table.type.BasicType;
+import org.apache.seatunnel.api.table.type.DecimalType;
+import org.apache.seatunnel.api.table.type.LocalTimeType;
+import org.apache.seatunnel.api.table.type.PrimitiveByteArrayType;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SqlType;
+import org.apache.seatunnel.common.exception.CommonErrorCode;
+import org.apache.seatunnel.connectors.seatunnel.jdbc.exception.JdbcConnectorException;
+
+import org.apache.commons.collections4.MapUtils;
+
+import com.google.auto.service.AutoService;
+import com.google.common.collect.ImmutableMap;
+import com.mysql.cj.MysqlType;
+
+import java.util.Collections;
+import java.util.Map;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+@AutoService(DataTypeConvertor.class)
+public class TiDBDataTypeConvertor implements DataTypeConvertor<MysqlType> {
+
+    public static final String PRECISION = "precision";
+    public static final String SCALE = "scale";
+
+    public static final Integer DEFAULT_PRECISION = 10;
+
+    public static final Integer DEFAULT_SCALE = 0;
+
+    @Override
+    public SeaTunnelDataType<?> toSeaTunnelType(String connectorDataType) {
+        checkNotNull(connectorDataType, "connectorDataType can not be null");
+        MysqlType mysqlType = MysqlType.getByName(connectorDataType);
+        Map<String, Object> dataTypeProperties;
+        switch (mysqlType) {
+            case BIGINT_UNSIGNED:
+            case DECIMAL:
+            case DECIMAL_UNSIGNED:
+            case BIT:
+                int left = connectorDataType.indexOf("(");
+                int right = connectorDataType.indexOf(")");
+                int precision = DEFAULT_PRECISION;
+                int scale = DEFAULT_SCALE;
+                if (left != -1 && right != -1) {
+                    String[] precisionAndScale =
+                            connectorDataType.substring(left + 1, right).split(",");
+                    if (precisionAndScale.length == 2) {
+                        precision = Integer.parseInt(precisionAndScale[0]);
+                        scale = Integer.parseInt(precisionAndScale[1]);
+                    } else if (precisionAndScale.length == 1) {
+                        precision = Integer.parseInt(precisionAndScale[0]);
+                    }
+                }
+                dataTypeProperties = ImmutableMap.of(PRECISION, precision, SCALE, scale);
+                break;
+            default:
+                dataTypeProperties = Collections.emptyMap();
+                break;
+        }
+        return toSeaTunnelType(mysqlType, dataTypeProperties);
+    }
+
+    // todo: It's better to wrapper MysqlType to a pojo in ST, since MysqlType doesn't contains

Review Comment:
    contains properties. This place uses MysqlDataTypeConvertor, so take the annotations too QAQ
    



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org