forked from apache/seatunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature][Jdbc] Support hive compatibleMode add inceptor dialect (apa…
- Loading branch information
Showing
5 changed files
with
223 additions
and
0 deletions.
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
35 changes: 35 additions & 0 deletions
35
...apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/inceptor/InceptorDialect.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,35 @@ | ||
/* | ||
* 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.inceptor; | ||
|
||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.converter.JdbcRowConverter; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.DatabaseIdentifier; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.hive.HiveDialect; | ||
|
||
public class InceptorDialect extends HiveDialect { | ||
|
||
@Override | ||
public String dialectName() { | ||
return DatabaseIdentifier.INCEPTOR; | ||
} | ||
|
||
@Override | ||
public JdbcRowConverter getRowConverter() { | ||
return new InceptorJdbcRowConverter(); | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
...atunnel/connectors/seatunnel/jdbc/internal/dialect/inceptor/InceptorJdbcRowConverter.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,141 @@ | ||
/* | ||
* 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.inceptor; | ||
|
||
import org.apache.seatunnel.api.table.catalog.TableSchema; | ||
import org.apache.seatunnel.api.table.type.ArrayType; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelDataType; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRow; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRowType; | ||
import org.apache.seatunnel.api.table.type.SqlType; | ||
import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.exception.JdbcConnectorErrorCode; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.exception.JdbcConnectorException; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.DatabaseIdentifier; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.hive.HiveJdbcRowConverter; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.math.BigDecimal; | ||
import java.sql.PreparedStatement; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
|
||
public class InceptorJdbcRowConverter extends HiveJdbcRowConverter { | ||
|
||
@Override | ||
public String converterName() { | ||
return DatabaseIdentifier.INCEPTOR; | ||
} | ||
|
||
@Override | ||
public PreparedStatement toExternal( | ||
TableSchema tableSchema, SeaTunnelRow row, PreparedStatement statement) { | ||
SeaTunnelRowType rowType = tableSchema.toPhysicalRowDataType(); | ||
for (int fieldIndex = 0; fieldIndex < rowType.getTotalFields(); fieldIndex++) { | ||
try { | ||
SeaTunnelDataType<?> seaTunnelDataType = rowType.getFieldType(fieldIndex); | ||
int statementIndex = fieldIndex + 1; | ||
Object fieldValue = row.getField(fieldIndex); | ||
if (fieldValue == null) { | ||
statement.setObject(statementIndex, StringUtils.EMPTY); | ||
continue; | ||
} | ||
switch (seaTunnelDataType.getSqlType()) { | ||
case STRING: | ||
statement.setString(statementIndex, (String) row.getField(fieldIndex)); | ||
break; | ||
case BOOLEAN: | ||
statement.setBoolean(statementIndex, (Boolean) row.getField(fieldIndex)); | ||
break; | ||
case TINYINT: | ||
statement.setByte(statementIndex, (Byte) row.getField(fieldIndex)); | ||
break; | ||
case SMALLINT: | ||
statement.setShort(statementIndex, (Short) row.getField(fieldIndex)); | ||
break; | ||
case INT: | ||
statement.setInt(statementIndex, (Integer) row.getField(fieldIndex)); | ||
break; | ||
case BIGINT: | ||
statement.setLong(statementIndex, (Long) row.getField(fieldIndex)); | ||
break; | ||
case FLOAT: | ||
statement.setFloat(statementIndex, (Float) row.getField(fieldIndex)); | ||
break; | ||
case DOUBLE: | ||
statement.setDouble(statementIndex, (Double) row.getField(fieldIndex)); | ||
break; | ||
case DECIMAL: | ||
statement.setBigDecimal( | ||
statementIndex, (BigDecimal) row.getField(fieldIndex)); | ||
break; | ||
case DATE: | ||
LocalDate localDate = (LocalDate) row.getField(fieldIndex); | ||
statement.setDate(statementIndex, java.sql.Date.valueOf(localDate)); | ||
break; | ||
case TIME: | ||
writeTime(statement, statementIndex, (LocalTime) row.getField(fieldIndex)); | ||
break; | ||
case TIMESTAMP: | ||
LocalDateTime localDateTime = (LocalDateTime) row.getField(fieldIndex); | ||
statement.setTimestamp( | ||
statementIndex, java.sql.Timestamp.valueOf(localDateTime)); | ||
break; | ||
case BYTES: | ||
statement.setBytes(statementIndex, (byte[]) row.getField(fieldIndex)); | ||
break; | ||
case NULL: | ||
statement.setNull(statementIndex, java.sql.Types.NULL); | ||
break; | ||
case ARRAY: | ||
SeaTunnelDataType elementType = | ||
((ArrayType) seaTunnelDataType).getElementType(); | ||
Object[] array = (Object[]) row.getField(fieldIndex); | ||
if (array == null) { | ||
statement.setNull(statementIndex, java.sql.Types.ARRAY); | ||
break; | ||
} | ||
if (SqlType.TINYINT.equals(elementType.getSqlType())) { | ||
Short[] shortArray = new Short[array.length]; | ||
for (int i = 0; i < array.length; i++) { | ||
shortArray[i] = Short.valueOf(array[i].toString()); | ||
} | ||
statement.setObject(statementIndex, shortArray); | ||
} else { | ||
statement.setObject(statementIndex, array); | ||
} | ||
break; | ||
case MAP: | ||
case ROW: | ||
default: | ||
throw new JdbcConnectorException( | ||
CommonErrorCodeDeprecated.UNSUPPORTED_DATA_TYPE, | ||
"Unexpected value: " + seaTunnelDataType); | ||
} | ||
} catch (Exception e) { | ||
throw new JdbcConnectorException( | ||
JdbcConnectorErrorCode.DATA_TYPE_CAST_FAILED, | ||
"error field:" + rowType.getFieldNames()[fieldIndex], | ||
e); | ||
} | ||
} | ||
return statement; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...che/seatunnel/connectors/seatunnel/jdbc/internal/dialect/hive/HiveDialectFactoryTest.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,36 @@ | ||
/* | ||
* 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.hive; | ||
|
||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialect; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.inceptor.InceptorDialect; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class HiveDialectFactoryTest { | ||
|
||
@Test | ||
public void testWithCompatibleMode() { | ||
HiveDialectFactory hiveDialectFactory = new HiveDialectFactory(); | ||
JdbcDialect inceptorDialect = hiveDialectFactory.create("inceptor", ""); | ||
Assertions.assertTrue(inceptorDialect instanceof InceptorDialect); | ||
JdbcDialect hiveDialect = hiveDialectFactory.create("", ""); | ||
Assertions.assertTrue(hiveDialect instanceof HiveDialect); | ||
} | ||
} |