Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature][Jdbc] Add Jdbc default dialect for all jdbc series database without dialect #8132

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class BasicTypeDefine<T> implements Serializable {
protected String columnType;
// e.g. `varchar` for MySQL
protected String dataType;
// It's jdbc sql type(java.sql.Types) not SeaTunnel SqlType
protected int sqlType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use nativeType instead of sqlType.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use nativeType instead of sqlType.

no, like 'INT' cannot be converted to standard JDBCType, SQLType doesn't need to worry about this issue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, please add some comment to tell devs the sqlType come from jdbc, not come from seatunnel sql type. cc @hailin0

protected T nativeType;
// e.g. `varchar` length is 10
protected Long length;
Expand Down
12 changes: 12 additions & 0 deletions seatunnel-connectors-v2/connector-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
<iris.jdbc.version>3.0.0</iris.jdbc.version>
<tikv.version>3.2.0</tikv.version>
<opengauss.jdbc.version>5.1.0-og</opengauss.jdbc.version>
<mariadb.jdbc.version>3.5.1</mariadb.jdbc.version>

</properties>

<dependencyManagement>
Expand Down Expand Up @@ -215,6 +217,12 @@
<version>${opengauss.jdbc.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>${mariadb.jdbc.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -329,6 +337,10 @@
<groupId>org.opengauss</groupId>
<artifactId>opengauss-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect;

public class DatabaseIdentifier {
public static final String GENERIC = "Generic";
public static final String DB_2 = "DB2";
public static final String DAMENG = "Dameng";
public static final String GBASE_8A = "Gbase8a";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.jdbc.internal.dialect;

import org.apache.seatunnel.api.table.catalog.TablePath;
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.converter.AbstractJdbcRowConverter;
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.converter.JdbcRowConverter;
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.dialectenum.FieldIdeEnum;

import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

@Slf4j
public class GenericDialect implements JdbcDialect {

public String fieldIde = FieldIdeEnum.ORIGINAL.getValue();

public GenericDialect() {}

public GenericDialect(String fieldIde) {
this.fieldIde = fieldIde;
}

@Override
public String dialectName() {
return DatabaseIdentifier.GENERIC;
}

@Override
public JdbcRowConverter getRowConverter() {
return new AbstractJdbcRowConverter() {
@Override
public String converterName() {
return DatabaseIdentifier.GENERIC;
}
};
}

@Override
public JdbcDialectTypeMapper getJdbcDialectTypeMapper() {
return new GenericTypeMapper();
}

@Override
public String quoteIdentifier(String identifier) {
return getFieldIde(identifier, fieldIde);
}

@Override
public String quoteDatabaseIdentifier(String identifier) {
return identifier;
}

@Override
public String tableIdentifier(TablePath tablePath) {
return tableIdentifier(tablePath.getDatabaseName(), tablePath.getTableName());
}

@Override
public Optional<String> getUpsertStatement(
String database, String tableName, String[] fieldNames, String[] uniqueKeyFields) {
throw new UnsupportedOperationException();
}

@Override
public TablePath parse(String tablePath) {
return TablePath.of(tablePath, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.jdbc.internal.dialect;

import com.google.auto.service.AutoService;

import javax.annotation.Nonnull;

/** Factory for {@link GenericDialect}. */
@AutoService(JdbcDialectFactory.class)
public class GenericDialectFactory implements JdbcDialectFactory {

// GenericDialect does not have any special requirements.
@Override
public boolean acceptsURL(String url) {
return true;
}

@Override
public JdbcDialect create() {
return new GenericDialect();
}

@Override
public JdbcDialect create(@Nonnull String compatibleMode, String fieldIde) {
return new GenericDialect(fieldIde);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* 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.jdbc.internal.dialect;

import org.apache.seatunnel.shade.com.google.common.base.Preconditions;

import org.apache.seatunnel.api.table.catalog.Column;
import org.apache.seatunnel.api.table.catalog.PhysicalColumn;
import org.apache.seatunnel.api.table.converter.BasicTypeDefine;
import org.apache.seatunnel.api.table.converter.TypeConverter;
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 com.google.auto.service.AutoService;
import lombok.extern.slf4j.Slf4j;

import java.sql.Types;

@Slf4j
@AutoService(TypeConverter.class)
public class GenericTypeConverter implements TypeConverter<BasicTypeDefine> {

public static final GenericTypeConverter DEFAULT_INSTANCE = new GenericTypeConverter();

public static final int MAX_PRECISION = 65;
public static final int DEFAULT_PRECISION = 38;
public static final int MAX_SCALE = MAX_PRECISION - 1;
public static final int DEFAULT_SCALE = 18;

@Override
public String identifier() {
return DatabaseIdentifier.GENERIC;
}

/**
* Convert an external system's type definition to {@link Column}.
*
* @param typeDefine type define
* @return column
*/
@Override
public Column convert(BasicTypeDefine typeDefine) {
PhysicalColumn.PhysicalColumnBuilder builder =
PhysicalColumn.builder()
.name(typeDefine.getName())
.nullable(typeDefine.isNullable())
.defaultValue(typeDefine.getDefaultValue())
.comment(typeDefine.getComment());
int sqlType = typeDefine.getSqlType();
switch (sqlType) {
case Types.NULL:
builder.dataType(BasicType.VOID_TYPE);
break;
case Types.BOOLEAN:
builder.dataType(BasicType.BOOLEAN_TYPE);
break;
case Types.BIT:
if (typeDefine.getLength() == null
|| typeDefine.getLength() <= 0
|| typeDefine.getLength() == 1) {
builder.dataType(BasicType.BOOLEAN_TYPE);
} else {
builder.dataType(PrimitiveByteArrayType.INSTANCE);
// BIT(M) -> BYTE(M/8)
long byteLength = typeDefine.getLength() / 8;
byteLength += typeDefine.getLength() % 8 > 0 ? 1 : 0;
builder.columnLength(byteLength);
}
break;
case Types.TINYINT:
builder.dataType(BasicType.BYTE_TYPE);
break;
case Types.SMALLINT:
builder.dataType(BasicType.SHORT_TYPE);
break;
case Types.INTEGER:
builder.dataType(BasicType.INT_TYPE);
break;
case Types.BIGINT:
builder.dataType(BasicType.LONG_TYPE);
break;
case Types.REAL:
case Types.FLOAT:
builder.dataType(BasicType.FLOAT_TYPE);
break;
case Types.DOUBLE:
builder.dataType(BasicType.DOUBLE_TYPE);
break;
case Types.NUMERIC:
DecimalType decimalTypeForNumeric;
if (typeDefine.getPrecision() != null && typeDefine.getPrecision() > 0) {
decimalTypeForNumeric =
new DecimalType(
typeDefine.getPrecision().intValue(), typeDefine.getScale());
} else {
decimalTypeForNumeric = new DecimalType(DEFAULT_PRECISION, DEFAULT_SCALE);
}
builder.dataType(decimalTypeForNumeric);
break;
case Types.DECIMAL:
Preconditions.checkArgument(typeDefine.getPrecision() > 0);
DecimalType decimalType;
if (typeDefine.getPrecision() > DEFAULT_PRECISION) {
decimalType = new DecimalType(DEFAULT_PRECISION, DEFAULT_SCALE);
} else {
decimalType =
new DecimalType(
typeDefine.getPrecision().intValue(),
typeDefine.getScale() == null
? 0
: typeDefine.getScale().intValue());
}
builder.dataType(decimalType);
builder.columnLength(Long.valueOf(decimalType.getPrecision()));
builder.scale(decimalType.getScale());
break;

case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
case Types.LONGNVARCHAR:
case Types.CLOB:
case Types.DATALINK:
case Types.NCLOB:
case Types.SQLXML:
builder.dataType(BasicType.STRING_TYPE);
break;

case Types.BINARY:
case Types.BLOB:
case Types.VARBINARY:
case Types.LONGVARBINARY:
if (typeDefine.getLength() == null || typeDefine.getLength() <= 0) {
builder.columnLength(1L);
} else {
builder.columnLength(typeDefine.getLength());
}
builder.dataType(PrimitiveByteArrayType.INSTANCE);
break;
case Types.DATE:
builder.dataType(LocalTimeType.LOCAL_DATE_TYPE);
break;
case Types.TIME:
builder.dataType(LocalTimeType.LOCAL_TIME_TYPE);
builder.scale(typeDefine.getScale());
break;
case Types.TIMESTAMP:
builder.dataType(LocalTimeType.LOCAL_DATE_TIME_TYPE);
builder.scale(typeDefine.getScale());
break;

case Types.OTHER:
case Types.ARRAY:
case Types.JAVA_OBJECT:
case Types.DISTINCT:
case Types.STRUCT:
case Types.REF:
case Types.ROWID:
default:
log.warn(
"JDBC type {} ({}) not currently supported",
sqlType,
typeDefine.getNativeType());
}
return builder.build();
}

/**
* Convert {@link Column} to an external system's type definition.
*
* @param column
* @return
*/
@Override
public BasicTypeDefine reconvert(Column column) {
throw new UnsupportedOperationException(
String.format(
"%s (%s) type doesn't have a mapping to the SQL database column type",
column.getName(), column.getDataType().getSqlType().name()));
}
}
Loading