-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
85 changes: 85 additions & 0 deletions
85
.../java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/GenericDialect.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,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); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...rg/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/GenericDialectFactory.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,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); | ||
} | ||
} |
200 changes: 200 additions & 0 deletions
200
...org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/GenericTypeConverter.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,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())); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, like 'INT' cannot be converted to standard JDBCType, SQLType doesn't need to worry about this issue
There was a problem hiding this comment.
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