TyrantLucifer commented on code in PR #3640:
URL: 
https://github.com/apache/incubator-seatunnel/pull/3640#discussion_r1039129046


##########
seatunnel-connectors-v2/connector-maxcompute/src/main/java/org/apache/seatunnel/connectors/seatunnel/maxcompute/source/MaxcomputeSourceReader.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.maxcompute.source;
+
+import org.apache.seatunnel.api.source.Boundedness;
+import org.apache.seatunnel.api.source.Collector;
+import org.apache.seatunnel.api.source.SourceReader;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.common.exception.CommonErrorCode;
+import 
org.apache.seatunnel.connectors.seatunnel.maxcompute.exception.MaxcomputeConnectorException;
+import 
org.apache.seatunnel.connectors.seatunnel.maxcompute.util.MaxcomputeTypeMapper;
+import 
org.apache.seatunnel.connectors.seatunnel.maxcompute.util.MaxcomputeUtil;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import com.aliyun.odps.data.Record;
+import com.aliyun.odps.tunnel.TableTunnel;
+import com.aliyun.odps.tunnel.io.TunnelRecordReader;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+@Slf4j
+public class MaxcomputeSourceReader implements SourceReader<SeaTunnelRow, 
MaxcomputeSourceSplit> {
+    private final SourceReader.Context context;
+    private Set<MaxcomputeSourceSplit> sourceSplits;

Review Comment:
   ```suggestion
       private final Set<MaxcomputeSourceSplit> sourceSplits;
   ```



##########
seatunnel-connectors-v2/connector-maxcompute/src/main/java/org/apache/seatunnel/connectors/seatunnel/maxcompute/source/MaxcomputeSource.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.maxcompute.source;
+
+import org.apache.seatunnel.api.source.Boundedness;
+import org.apache.seatunnel.api.source.SeaTunnelSource;
+import org.apache.seatunnel.api.source.SourceReader;
+import org.apache.seatunnel.api.source.SourceSplitEnumerator;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import 
org.apache.seatunnel.connectors.seatunnel.maxcompute.util.MaxcomputeTypeMapper;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import com.google.auto.service.AutoService;
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+@AutoService(SeaTunnelSource.class)
+public class MaxcomputeSource implements SeaTunnelSource<SeaTunnelRow, 
MaxcomputeSourceSplit, MaxcomputeSourceState> {
+    private SeaTunnelRowType typeInfo;
+    private Config pluginConfig;
+
+    @Override
+    public String getPluginName() {
+        return "MaxcomputeSource";

Review Comment:
   ```suggestion
           return "Maxcompute";
   ```



##########
seatunnel-connectors-v2/connector-maxcompute/src/main/java/org/apache/seatunnel/connectors/seatunnel/maxcompute/sink/MaxcomputeSink.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.maxcompute.sink;
+
+import org.apache.seatunnel.api.common.PrepareFailException;
+import org.apache.seatunnel.api.sink.SeaTunnelSink;
+import org.apache.seatunnel.api.sink.SinkWriter;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import 
org.apache.seatunnel.connectors.seatunnel.common.sink.AbstractSimpleSink;
+import 
org.apache.seatunnel.connectors.seatunnel.common.sink.AbstractSinkWriter;
+import 
org.apache.seatunnel.connectors.seatunnel.maxcompute.util.MaxcomputeUtil;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import com.google.auto.service.AutoService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@AutoService(SeaTunnelSink.class)
+public class MaxcomputeSink extends AbstractSimpleSink<SeaTunnelRow, Void> {
+    private static final Logger LOG = 
LoggerFactory.getLogger(MaxcomputeSink.class);
+    private Config pluginConfig;
+    private SeaTunnelRowType typeInfo;
+
+    @Override
+    public String getPluginName() {
+        return "MaxcomputeSink";

Review Comment:
   ```suggestion
           return "Maxcompute";
   ```



##########
seatunnel-connectors-v2/connector-maxcompute/src/main/java/org/apache/seatunnel/connectors/seatunnel/maxcompute/util/MaxcomputeTypeMapper.java:
##########
@@ -0,0 +1,189 @@
+/*
+ * 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.maxcompute.util;
+
+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.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.common.exception.CommonErrorCode;
+import 
org.apache.seatunnel.connectors.seatunnel.maxcompute.exception.MaxcomputeConnectorException;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import com.aliyun.odps.Column;
+import com.aliyun.odps.Table;
+import com.aliyun.odps.TableSchema;
+import com.aliyun.odps.data.Record;
+import com.aliyun.odps.tunnel.TableTunnel;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+@Slf4j
+public class MaxcomputeTypeMapper implements Serializable {
+
+
+    // ============================data types=====================
+
+    private static final String MAXCOMPUTE_UNKNOWN = "UNKNOWN";
+    private static final String MAXCOMPUTE_BOOLEAN = "BOOLEAN";
+
+    // -------------------------number----------------------------
+    private static final String MAXCOMPUTE_TINYINT = "TINYINT";
+    private static final String MAXCOMPUTE_SMALLINT = "SMALLINT";
+    private static final String MAXCOMPUTE_INT = "INT";
+    private static final String MAXCOMPUTE_BIGINT = "BIGINT";
+
+    private static final String MAXCOMPUTE_FLOAT = "FLOAT";
+
+    private static final String MAXCOMPUTE_DOUBLE = "DOUBLE";
+    private static final String MAXCOMPUTE_DECIMAL = "DECIMAL32";
+
+
+    // -------------------------string----------------------------
+
+    private static final String MAXCOMPUTE_VARCHAR = "VARCHAR";
+    private static final String MAXCOMPUTE_CHAR = "CHAR";
+    private static final String MAXCOMPUTE_STRING = "STRING";
+
+
+    // ------------------------------time-------------------------
+
+    private static final String MAXCOMPUTE_DATE = "DATE";
+    private static final String MAXCOMPUTE_DATETIME = "DATETIME";
+    private static final String MAXCOMPUTE_TIMESTAMP = "TIMESTAMP";
+
+
+    // ------------------------------blob-------------------------
+
+    private static final String MAXCOMPUTE_BINARY = "BINARY";
+    private static final int PRECISION = 20;
+
+    private static SeaTunnelDataType<?> mapping(List<Column> columnSchemaList, 
int colIndex) throws SQLException {
+        String maxcomputeType = 
columnSchemaList.get(colIndex).getTypeInfo().getTypeName().toUpperCase();
+        switch (maxcomputeType) {
+            case MAXCOMPUTE_BOOLEAN:
+                return BasicType.BOOLEAN_TYPE;
+            case MAXCOMPUTE_TINYINT:
+            case MAXCOMPUTE_SMALLINT:
+            case MAXCOMPUTE_INT:
+                return BasicType.INT_TYPE;
+            case MAXCOMPUTE_BIGINT:
+                return BasicType.LONG_TYPE;
+            case MAXCOMPUTE_DECIMAL:
+                return new DecimalType(PRECISION, 0);
+            case MAXCOMPUTE_FLOAT:
+                return BasicType.FLOAT_TYPE;
+            case MAXCOMPUTE_DOUBLE:
+                return BasicType.DOUBLE_TYPE;
+            case MAXCOMPUTE_VARCHAR:
+            case MAXCOMPUTE_CHAR:
+            case MAXCOMPUTE_STRING:
+                return BasicType.STRING_TYPE;
+            case MAXCOMPUTE_DATE:
+            case MAXCOMPUTE_DATETIME:
+            case MAXCOMPUTE_TIMESTAMP:
+                return LocalTimeType.LOCAL_DATE_TIME_TYPE;
+            case MAXCOMPUTE_BINARY:
+                return PrimitiveByteArrayType.INSTANCE;
+            //Doesn't support yet
+            case MAXCOMPUTE_UNKNOWN:
+            default:
+                throw new 
MaxcomputeConnectorException(CommonErrorCode.UNSUPPORTED_DATA_TYPE, 
String.format(
+                    "Doesn't support Maxcompute type '%s' .",
+                    maxcomputeType));
+        }
+    }
+
+    public static SeaTunnelRowType getSeaTunnelRowType(Config pluginConfig) {
+        Table table = MaxcomputeUtil.getTable(pluginConfig);
+        TableSchema tableSchema = table.getSchema();
+        ArrayList<SeaTunnelDataType<?>> seaTunnelDataTypes = new ArrayList<>();
+        ArrayList<String> fieldNames = new ArrayList<>();
+        try {
+            for (int i = 0; i < tableSchema.getColumns().size(); i++) {
+                fieldNames.add(tableSchema.getColumns().get(i).getName());
+                seaTunnelDataTypes.add(mapping(tableSchema.getColumns(), i));
+            }
+        } catch (Exception e) {
+            throw new 
MaxcomputeConnectorException(CommonErrorCode.TABLE_SCHEMA_GET_FAILED, e);
+        }
+        return new SeaTunnelRowType(fieldNames.toArray(new 
String[fieldNames.size()]), seaTunnelDataTypes.toArray(new 
SeaTunnelDataType<?>[seaTunnelDataTypes.size()]));
+    }
+
+    public static SeaTunnelRow getSeaTunnelRowData(Record rs, SeaTunnelRowType 
typeInfo) throws SQLException {
+
+        List<Object> fields = new ArrayList<>();
+        SeaTunnelDataType<?>[] seaTunnelDataTypes = typeInfo.getFieldTypes();
+        for (int i = 0; i < seaTunnelDataTypes.length; i++) {
+            Object seatunnelField;
+            SeaTunnelDataType<?> seaTunnelDataType = seaTunnelDataTypes[i];
+            if (null == rs.get(i)) {
+                seatunnelField = null;
+            } else if (BasicType.BOOLEAN_TYPE.equals(seaTunnelDataType)) {

Review Comment:
   Use switch(seatunnelDataType.getSqlType()) is better. You can refer to 
https://github.com/apache/incubator-seatunnel/blob/dev/seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/source/reader/ParquetReadStrategy.java#L128



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to