-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature](jdbc catalog) support gbase jdbc catalog
- Loading branch information
Showing
9 changed files
with
428 additions
and
1 deletion.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
...e-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/GbaseJdbcExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// 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 java.math.BigDecimal; | ||
import java.sql.Date; | ||
import java.sql.SQLException; | ||
import java.sql.Timestamp; | ||
import java.util.Objects; | ||
|
||
public class GbaseJdbcExecutor extends BaseJdbcExecutor { | ||
|
||
public GbaseJdbcExecutor(byte[] thriftParams) throws Exception { | ||
super(thriftParams); | ||
} | ||
|
||
@Override | ||
protected Object getColumnValue(int columnIndex, ColumnType type, String[] replaceStringList) throws SQLException { | ||
switch (type.getType()) { | ||
case TINYINT: | ||
byte tinyIntVal = resultSet.getByte(columnIndex + 1); | ||
return resultSet.wasNull() ? null : tinyIntVal; | ||
case SMALLINT: | ||
short smallIntVal = resultSet.getShort(columnIndex + 1); | ||
return resultSet.wasNull() ? null : smallIntVal; | ||
case INT: | ||
int intVal = resultSet.getInt(columnIndex + 1); | ||
return resultSet.wasNull() ? null : intVal; | ||
case BIGINT: | ||
long bigIntVal = resultSet.getLong(columnIndex + 1); | ||
return resultSet.wasNull() ? null : bigIntVal; | ||
case FLOAT: | ||
float floatVal = resultSet.getFloat(columnIndex + 1); | ||
return resultSet.wasNull() ? null : floatVal; | ||
case DOUBLE: | ||
double doubleVal = resultSet.getDouble(columnIndex + 1); | ||
return resultSet.wasNull() ? null : doubleVal; | ||
case DECIMALV2: | ||
case DECIMAL32: | ||
case DECIMAL64: | ||
case DECIMAL128: | ||
BigDecimal decimalVal = resultSet.getBigDecimal(columnIndex + 1); | ||
return resultSet.wasNull() ? null : decimalVal; | ||
case DATE: | ||
case DATEV2: | ||
Date dateVal = resultSet.getDate(columnIndex + 1); | ||
return resultSet.wasNull() ? null : dateVal.toLocalDate(); | ||
case DATETIME: | ||
case DATETIMEV2: | ||
Timestamp timestampVal = resultSet.getTimestamp(columnIndex + 1); | ||
return resultSet.wasNull() ? null : timestampVal.toLocalDateTime(); | ||
case CHAR: | ||
case VARCHAR: | ||
case STRING: | ||
String stringVal = resultSet.getString(columnIndex + 1); | ||
return resultSet.wasNull() ? null : stringVal; | ||
default: | ||
throw new IllegalArgumentException("Unsupported column type: " + type.getType()); | ||
} | ||
} | ||
|
||
@Override | ||
protected ColumnValueConverter getOutputConverter(ColumnType columnType, String replaceString) { | ||
if (Objects.requireNonNull(columnType.getType()) == Type.CHAR) { | ||
return createConverter( | ||
input -> trimSpaces(input.toString()), String.class); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
fe/fe-core/src/main/java/org/apache/doris/datasource/jdbc/client/JdbcGbaseClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// 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.datasource.jdbc.client; | ||
|
||
import org.apache.doris.catalog.PrimitiveType; | ||
import org.apache.doris.catalog.ScalarType; | ||
import org.apache.doris.catalog.Type; | ||
import org.apache.doris.common.util.Util; | ||
import org.apache.doris.datasource.jdbc.util.JdbcFieldSchema; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DatabaseMetaData; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Types; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
public class JdbcGbaseClient extends JdbcClient { | ||
|
||
protected JdbcGbaseClient(JdbcClientConfig jdbcClientConfig) { | ||
super(jdbcClientConfig); | ||
} | ||
|
||
@Override | ||
public List<String> getDatabaseNameList() { | ||
Connection conn = getConnection(); | ||
ResultSet rs = null; | ||
List<String> remoteDatabaseNames = Lists.newArrayList(); | ||
try { | ||
if (isOnlySpecifiedDatabase && includeDatabaseMap.isEmpty() && excludeDatabaseMap.isEmpty()) { | ||
String currentDatabase = conn.getCatalog(); | ||
remoteDatabaseNames.add(currentDatabase); | ||
} else { | ||
rs = conn.getMetaData().getCatalogs(); | ||
while (rs.next()) { | ||
remoteDatabaseNames.add(rs.getString("TABLE_CAT")); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
throw new JdbcClientException("failed to get database name list from jdbc", e); | ||
} finally { | ||
close(rs, conn); | ||
} | ||
return filterDatabaseNames(remoteDatabaseNames); | ||
} | ||
|
||
@Override | ||
protected void processTable(String remoteDbName, String remoteTableName, String[] tableTypes, | ||
Consumer<ResultSet> resultSetConsumer) { | ||
Connection conn = null; | ||
ResultSet rs = null; | ||
try { | ||
conn = super.getConnection(); | ||
DatabaseMetaData databaseMetaData = conn.getMetaData(); | ||
rs = databaseMetaData.getTables(remoteDbName, null, remoteTableName, tableTypes); | ||
resultSetConsumer.accept(rs); | ||
} catch (SQLException e) { | ||
throw new JdbcClientException("Failed to process table", e); | ||
} finally { | ||
close(rs, conn); | ||
} | ||
} | ||
|
||
@Override | ||
protected ResultSet getRemoteColumns(DatabaseMetaData databaseMetaData, String catalogName, String remoteDbName, | ||
String remoteTableName) throws SQLException { | ||
return databaseMetaData.getColumns(remoteDbName, null, remoteTableName, null); | ||
} | ||
|
||
@Override | ||
public List<JdbcFieldSchema> getJdbcColumnsInfo(String localDbName, String localTableName) { | ||
Connection conn = getConnection(); | ||
ResultSet rs = null; | ||
List<JdbcFieldSchema> tableSchema = Lists.newArrayList(); | ||
String remoteDbName = getRemoteDatabaseName(localDbName); | ||
String remoteTableName = getRemoteTableName(localDbName, localTableName); | ||
try { | ||
DatabaseMetaData databaseMetaData = conn.getMetaData(); | ||
String catalogName = getCatalogName(conn); | ||
rs = getRemoteColumns(databaseMetaData, catalogName, remoteDbName, remoteTableName); | ||
while (rs.next()) { | ||
JdbcFieldSchema field = new JdbcFieldSchema(rs); | ||
tableSchema.add(field); | ||
} | ||
} catch (SQLException e) { | ||
throw new JdbcClientException("failed to get jdbc columns info for remote table `%s.%s`: %s", | ||
remoteDbName, remoteTableName, Util.getRootCauseMessage(e)); | ||
} finally { | ||
close(rs, conn); | ||
} | ||
return tableSchema; | ||
} | ||
|
||
@Override | ||
protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) { | ||
switch (fieldSchema.getDataType()) { | ||
case Types.TINYINT: | ||
return Type.TINYINT; | ||
case Types.SMALLINT: | ||
return Type.SMALLINT; | ||
case Types.INTEGER: | ||
return Type.INT; | ||
case Types.BIGINT: | ||
return Type.BIGINT; | ||
case Types.FLOAT: | ||
case Types.REAL: | ||
return Type.FLOAT; | ||
case Types.DOUBLE: | ||
return Type.DOUBLE; | ||
case Types.NUMERIC: | ||
case Types.DECIMAL: { | ||
int precision = fieldSchema.getColumnSize() | ||
.orElseThrow(() -> new IllegalArgumentException("Precision not present")); | ||
int scale = fieldSchema.getDecimalDigits() | ||
.orElseThrow(() -> new JdbcClientException("Scale not present")); | ||
return createDecimalOrStringType(precision, scale); | ||
} | ||
case Types.DATE: | ||
return Type.DATEV2; | ||
case Types.TIMESTAMP: { | ||
int scale = fieldSchema.getDecimalDigits().orElse(0); | ||
if (scale > 6) { | ||
scale = 6; | ||
} | ||
return ScalarType.createDatetimeV2Type(scale); | ||
} | ||
case Types.TIME: | ||
case Types.CHAR: | ||
ScalarType charType = ScalarType.createType(PrimitiveType.CHAR); | ||
charType.setLength(fieldSchema.getColumnSize() | ||
.orElseThrow(() -> new IllegalArgumentException("Length not present"))); | ||
return charType; | ||
case Types.VARCHAR: | ||
case Types.LONGVARCHAR: | ||
return ScalarType.createStringType(); | ||
default: | ||
return Type.UNSUPPORTED; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
regression-test/data/external_table_p0/jdbc/test_gbase_jdbc_catalog.out
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.