Skip to content

Commit

Permalink
[refactor](jdbc catalog) split trino jdbc executor (apache#34932)
Browse files Browse the repository at this point in the history
  • Loading branch information
zy-kkk authored May 16, 2024
1 parent 1434aef commit 1c962a5
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 2 deletions.
3 changes: 2 additions & 1 deletion be/src/vec/exec/vjdbc_connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,8 @@ Status JdbcConnector::_check_type(SlotDescriptor* slot_desc, const std::string&
case TYPE_DATETIMEV2: {
if (type_str != "java.sql.Timestamp" && type_str != "java.time.LocalDateTime" &&
type_str != "java.sql.Date" && type_str != "java.time.LocalDate" &&
type_str != "oracle.sql.TIMESTAMP" && type_str != "java.time.OffsetDateTime") {
type_str != "oracle.sql.TIMESTAMP" && type_str != "java.time.OffsetDateTime" &&
type_str != "java.lang.String") {
return Status::InternalError(error_msg);
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public static String getExecutorClass(TOdbcTableType type) {
return "org/apache/doris/jdbc/ClickHouseJdbcExecutor";
case SAP_HANA:
return "org/apache/doris/jdbc/SapHanaJdbcExecutor";
case TRINO:
case PRESTO:
return "org/apache/doris/jdbc/TrinoJdbcExecutor";
default:
return "org/apache/doris/jdbc/DefaultJdbcExecutor";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// 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 com.google.common.collect.Lists;

import java.math.BigDecimal;
import java.sql.Array;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class TrinoJdbcExecutor extends BaseJdbcExecutor {
public TrinoJdbcExecutor(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 Timestamp[batchSizeNum]);
} else if (outputTable.getColumnType(i).getType() == Type.ARRAY) {
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 TINYINT:
return resultSet.getObject(columnIndex + 1, Byte.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:
return resultSet.getObject(columnIndex + 1, Timestamp.class);
case CHAR:
case VARCHAR:
case STRING:
return resultSet.getObject(columnIndex + 1, String.class);
case ARRAY: {
Array array = resultSet.getArray(columnIndex + 1);
if (array == null) {
return null;
}
Object[] dataArray = (Object[]) array.getArray();
if (dataArray.length == 0) {
return Collections.emptyList();
}
return Arrays.asList(dataArray);
}
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 -> ((Timestamp) input).toLocalDateTime(), java.time.LocalDateTime.class);
case ARRAY:
return createConverter(
input -> convertArray((List<?>) input, columnType.getChildTypes().get(0)), List.class);
default:
return null;
}
}

private List<?> convertArray(List<?> array, ColumnType type) {
if (array == null) {
return null;
}
if (array.isEmpty()) {
return Collections.emptyList();
}
switch (type.getType()) {
case DATE:
case DATEV2: {
List<LocalDate> result = Lists.newArrayList();
for (Object element : array) {
result.add(element != null ? ((Date) element).toLocalDate() : null);
}
return result;
}
case DATETIME:
case DATETIMEV2: {
List<LocalDateTime> result = Lists.newArrayList();
for (Object element : array) {
result.add(element != null ? ((Timestamp) element).toLocalDateTime() : null);
}
return result;
}
case ARRAY: {
List<List<?>> resultArray = Lists.newArrayList();
for (Object element : array) {
if (element == null) {
resultArray.add(null);
} else {
resultArray.add(
Lists.newArrayList(convertArray((List<?>) element, type.getChildTypes().get(0))));
}
}
return resultArray;
}
default:
return array;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
return Type.BOOLEAN;
case "date":
return ScalarType.createDateV2Type();
case "json":
return ScalarType.createStringType();
default:
break;
}
Expand Down Expand Up @@ -83,7 +85,7 @@ protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
return ArrayType.create(type, true);
}

if (trinoType.startsWith("varchar")) {
if (trinoType.startsWith("varchar") || trinoType.startsWith("time")) {
return ScalarType.createStringType();
}

Expand Down

0 comments on commit 1c962a5

Please sign in to comment.