Skip to content

Commit

Permalink
[fix](jdbc catalog) Use factory methods to return mapped types instead (
Browse files Browse the repository at this point in the history
#46623)

We should use factory methods to create types instead of returning a type directly, because the returned type is static and final, which will cause problems with length setting.
  • Loading branch information
zy-kkk authored and Your Name committed Jan 9, 2025
1 parent 2e992bb commit 37e8d80
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ public List<JdbcFieldSchema> getJdbcColumnsInfo(String remoteDbName, String remo
return tableSchema;
}

public List<Column> getColumnsFromJdbc(String localDbName, String localTableName) {
List<JdbcFieldSchema> jdbcTableSchema = getJdbcColumnsInfo(localDbName, localTableName);
public List<Column> getColumnsFromJdbc(String remoteDbName, String remoteTableName) {
List<JdbcFieldSchema> jdbcTableSchema = getJdbcColumnsInfo(remoteDbName, remoteTableName);
List<Column> dorisTableSchema = Lists.newArrayListWithCapacity(jdbcTableSchema.size());
for (JdbcFieldSchema field : jdbcTableSchema) {
dorisTableSchema.add(new Column(field.getColumnName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

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.datasource.jdbc.util.JdbcFieldSchema;
Expand Down Expand Up @@ -84,14 +83,10 @@ protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
case "REAL":
return Type.FLOAT;
case "CHAR":
ScalarType charType = ScalarType.createType(PrimitiveType.CHAR);
charType.setLength(fieldSchema.getColumnSize().orElse(0));
return charType;
return ScalarType.createCharType(fieldSchema.requiredColumnSize());
case "VARCHAR":
case "LONG VARCHAR":
ScalarType varcharType = ScalarType.createType(PrimitiveType.VARCHAR);
varcharType.setLength(fieldSchema.getColumnSize().orElse(0));
return varcharType;
return ScalarType.createVarcharType(fieldSchema.requiredColumnSize());
case "DATE":
return ScalarType.createDateV2Type();
case "TIMESTAMP": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

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;
Expand Down Expand Up @@ -143,10 +142,7 @@ protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
return ScalarType.createDatetimeV2Type(scale);
}
case Types.CHAR:
ScalarType charType = ScalarType.createType(PrimitiveType.CHAR);
charType.setLength(fieldSchema.getColumnSize()
.orElseThrow(() -> new IllegalArgumentException("Length not present")));
return charType;
return ScalarType.createCharType(fieldSchema.requiredColumnSize());
case Types.TIME:
case Types.VARCHAR:
case Types.LONGVARCHAR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,9 @@ protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
return createDecimalOrStringType(precision, scale);
}
case "CHAR":
ScalarType charType = ScalarType.createType(PrimitiveType.CHAR);
charType.setLength(fieldSchema.requiredColumnSize());
return charType;
return ScalarType.createCharType(fieldSchema.requiredColumnSize());
case "VARCHAR":
return ScalarType.createVarcharType(fieldSchema.getColumnSize().orElse(0));
return ScalarType.createVarcharType(fieldSchema.requiredColumnSize());
case "BIT":
if (fieldSchema.requiredColumnSize() == 1) {
return Type.BOOLEAN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.doris.datasource.jdbc.client;

import org.apache.doris.catalog.ArrayType;
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;
Expand Down Expand Up @@ -115,9 +114,7 @@ protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
case "float8":
return Type.DOUBLE;
case "bpchar":
ScalarType charType = ScalarType.createType(PrimitiveType.CHAR);
charType.setLength(fieldSchema.getColumnSize().orElse(0));
return charType;
return ScalarType.createCharType(fieldSchema.requiredColumnSize());
case "timestamp":
case "timestamptz": {
// postgres can support microsecond
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

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.datasource.jdbc.util.JdbcFieldSchema;
Expand Down Expand Up @@ -80,9 +79,7 @@ protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
return Type.BOOLEAN;
case "CHAR":
case "NCHAR":
ScalarType charType = ScalarType.createType(PrimitiveType.CHAR);
charType.setLength(fieldSchema.getColumnSize().orElse(0));
return charType;
return ScalarType.createCharType(fieldSchema.requiredColumnSize());
case "TIME":
case "VARCHAR":
case "NVARCHAR":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.doris.datasource.jdbc.client;

import org.apache.doris.catalog.ArrayType;
import org.apache.doris.catalog.PrimitiveType;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.catalog.Type;
import org.apache.doris.datasource.jdbc.util.JdbcFieldSchema;
Expand Down Expand Up @@ -65,9 +64,7 @@ protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
}

if (trinoType.startsWith("char")) {
ScalarType charType = ScalarType.createType(PrimitiveType.CHAR);
charType.setLength(fieldSchema.getColumnSize().orElse(0));
return charType;
return ScalarType.createCharType(fieldSchema.requiredColumnSize());
}

if (trinoType.startsWith("timestamp")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ datetime datetime Yes true \N NONE
timestamp1 datetime Yes true \N NONE
timestamp2 datetime(3) Yes true \N NONE
timestamp3 datetime(6) Yes true \N NONE
char char(6) Yes true \N NONE
char char(5) Yes true \N NONE
varchar varchar(10) Yes true \N NONE
text text Yes true \N NONE
blob text Yes true \N NONE
json text Yes true \N NONE
set char(6) Yes true \N NONE
set char(23) Yes true \N NONE
bit text Yes true \N NONE
binary text Yes true \N NONE
varbinary text Yes true \N NONE
Expand Down Expand Up @@ -86,12 +86,12 @@ datetime datetime Yes true \N NONE
timestamp1 datetime Yes true \N NONE
timestamp2 datetime(3) Yes true \N NONE
timestamp3 datetime(6) Yes true \N NONE
char char(6) Yes true \N NONE
char char(5) Yes true \N NONE
varchar varchar(10) Yes true \N NONE
text text Yes true \N NONE
blob text Yes true \N NONE
json text Yes true \N NONE
set char(6) Yes true \N NONE
set char(23) Yes true \N NONE
bit text Yes true \N NONE
binary text Yes true \N NONE
varbinary text Yes true \N NONE
Expand Down

0 comments on commit 37e8d80

Please sign in to comment.