This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git

commit 0f1cbcc86aaaf9d0412abc82ad8631750f616f0f
Author: zy-kkk <zhongy...@gmail.com>
AuthorDate: Tue Mar 5 23:48:48 2024 +0800

    [refactor](jdbc catalog) split postgresql jdbc Executor (#31730)
---
 .../org/apache/doris/jdbc/JdbcExecutorFactory.java |   2 +
 .../apache/doris/jdbc/PostgreSQLJdbcExecutor.java  | 125 +++++++++++++++++++++
 2 files changed, 127 insertions(+)

diff --git 
a/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/JdbcExecutorFactory.java
 
b/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/JdbcExecutorFactory.java
index 78522b8254c..75aa20c149b 100644
--- 
a/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/JdbcExecutorFactory.java
+++ 
b/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/JdbcExecutorFactory.java
@@ -26,6 +26,8 @@ public class JdbcExecutorFactory {
                 return "org/apache/doris/jdbc/MySQLJdbcExecutor";
             case ORACLE:
                 return "org/apache/doris/jdbc/OracleJdbcExecutor";
+            case POSTGRESQL:
+                return "org/apache/doris/jdbc/PostgreSQLJdbcExecutor";
             case SQLSERVER:
                 return "org/apache/doris/jdbc/SQLServerJdbcExecutor";
             case DB2:
diff --git 
a/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/PostgreSQLJdbcExecutor.java
 
b/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/PostgreSQLJdbcExecutor.java
new file mode 100644
index 00000000000..89fdb0ba997
--- /dev/null
+++ 
b/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/PostgreSQLJdbcExecutor.java
@@ -0,0 +1,125 @@
+// 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.doris.jdbc;
+
+import org.apache.doris.common.jni.vec.ColumnType;
+import org.apache.doris.common.jni.vec.ColumnType.Type;
+import org.apache.doris.common.jni.vec.ColumnValueConverter;
+import org.apache.doris.common.jni.vec.VectorTable;
+
+import java.math.BigDecimal;
+import java.sql.SQLException;
+import java.sql.Timestamp;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
+
+public class PostgreSQLJdbcExecutor extends BaseJdbcExecutor {
+    public PostgreSQLJdbcExecutor(byte[] thriftParams) throws Exception {
+        super(thriftParams);
+    }
+
+    @Override
+    protected void initializeBlock(int columnCount, String[] 
replaceStringList, int batchSizeNum,
+            VectorTable outputTable) {
+        for (int i = 0; i < columnCount; ++i) {
+            if (outputTable.getColumnType(i).getType() == Type.DATETIME
+                    || outputTable.getColumnType(i).getType() == 
Type.DATETIMEV2) {
+                block.add(new Object[batchSizeNum]);
+            } else if (outputTable.getColumnType(i).getType() == Type.STRING) {
+                block.add(new Object[batchSizeNum]);
+            } else {
+                
block.add(outputTable.getColumn(i).newObjectContainerArray(batchSizeNum));
+            }
+        }
+    }
+
+    @Override
+    protected Object getColumnValue(int columnIndex, ColumnType type, String[] 
replaceStringList) throws SQLException {
+        switch (type.getType()) {
+            case BOOLEAN:
+                return resultSet.getObject(columnIndex + 1, Boolean.class);
+            case SMALLINT:
+                return resultSet.getObject(columnIndex + 1, Short.class);
+            case INT:
+                return resultSet.getObject(columnIndex + 1, Integer.class);
+            case BIGINT:
+                return resultSet.getObject(columnIndex + 1, Long.class);
+            case FLOAT:
+                return resultSet.getObject(columnIndex + 1, Float.class);
+            case DOUBLE:
+                return resultSet.getObject(columnIndex + 1, Double.class);
+            case DECIMALV2:
+            case DECIMAL32:
+            case DECIMAL64:
+            case DECIMAL128:
+                return resultSet.getObject(columnIndex + 1, BigDecimal.class);
+            case DATE:
+            case DATEV2:
+                return resultSet.getObject(columnIndex + 1, LocalDate.class);
+            case DATETIME:
+            case DATETIMEV2:
+            case CHAR:
+            case VARCHAR:
+            case STRING:
+                return resultSet.getObject(columnIndex + 1);
+            default:
+                throw new IllegalArgumentException("Unsupported column type: " 
+ type.getType());
+        }
+    }
+
+    @Override
+    protected ColumnValueConverter getOutputConverter(ColumnType columnType, 
String replaceString) {
+        switch (columnType.getType()) {
+            case DATETIME:
+            case DATETIMEV2:
+                return createConverter(input -> {
+                    if (input instanceof Timestamp) {
+                        return ((Timestamp) input).toLocalDateTime();
+                    } else if (input instanceof OffsetDateTime) {
+                        return ((OffsetDateTime) input).toLocalDateTime();
+                    } else {
+                        return input;
+                    }
+                }, LocalDateTime.class);
+            case CHAR:
+                return createConverter(
+                        input -> trimSpaces(input.toString()), String.class);
+            case STRING:
+                return createConverter(input -> {
+                    if (input instanceof java.sql.Time) {
+                        return timeToString((java.sql.Time) input);
+                    } else if (input instanceof byte[]) {
+                        return pgByteArrayToHexString((byte[]) input);
+                    } else {
+                        return input.toString();
+                    }
+                }, String.class);
+            default:
+                return null;
+        }
+    }
+
+    private static String pgByteArrayToHexString(byte[] bytes) {
+        StringBuilder hexString = new StringBuilder("\\x");
+        for (byte b : bytes) {
+            hexString.append(String.format("%02x", b & 0xff));
+        }
+        return hexString.toString();
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to