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

[Bugfix] Source should throw exception when data types between StarRocks and Flink mismatch #127

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -19,24 +19,20 @@
import com.starrocks.connector.flink.table.source.struct.Const;
import com.starrocks.connector.flink.table.source.struct.SelectColumn;
import com.starrocks.connector.flink.table.source.struct.StarRocksSchema;
import com.starrocks.connector.flink.tools.DataTypeUtils;
import com.starrocks.connector.flink.tools.DataUtil;
import com.starrocks.thrift.TScanBatchResult;

import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.FieldVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.ipc.ArrowStreamReader;
import org.apache.flink.table.data.GenericRowData;
import org.apache.flink.table.types.DataType;
import org.apache.flink.table.types.logical.LogicalTypeRoot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.NoSuchElementException;

Expand Down Expand Up @@ -138,38 +134,33 @@ private void setValueToFlinkRows(int rowIndex, int column, Object obj) {
private void genFlinkRows() {
for (int i = 0; i < fieldVectors.size(); i++) {
FieldVector fieldVector = fieldVectors.get(i);
StarRocksToFlinkTrans translators = null;
Column column = starRocksSchema.getColumn(i);
boolean nullable = true;
if (column != null) {
ColumnRichInfo richInfo = columnRichInfos.get(selectedColumns[i].getColumnIndexInFlinkTable());
nullable = richInfo.getDataType().getLogicalType().isNullable();
LogicalTypeRoot flinkTypeRoot = richInfo.getDataType().getLogicalType().getTypeRoot();
String srType = DataUtil.clearBracket(column.getType());
if (Const.DataTypeRelationMap.containsKey(flinkTypeRoot)
&& Const.DataTypeRelationMap.get(flinkTypeRoot).containsKey(srType)) {
translators = Const.DataTypeRelationMap.get(flinkTypeRoot).get(srType);
}
if (column == null) {
throw new RuntimeException("Can't find StarRocks' column at index " + i);
}

// TODO make sure what's going on here
if (translators == null) {
DataType dataType = DataTypeUtils.map(fieldVector.getMinorType());
if (dataType == null) {
throw new RuntimeException(
"Flink type not support when convert data from starrocks to flink, " +
"type is -> [" + fieldVector.getMinorType().toString() + "]"
);
}
HashMap<String, StarRocksToFlinkTrans> map = Const.DataTypeRelationMap.get(dataType.getLogicalType().getTypeRoot());
translators = map.values().stream().findFirst().orElse(null);
ColumnRichInfo richInfo = columnRichInfos.get(selectedColumns[i].getColumnIndexInFlinkTable());
boolean nullable = richInfo.getDataType().getLogicalType().isNullable();
LogicalTypeRoot flinkTypeRoot = richInfo.getDataType().getLogicalType().getTypeRoot();
String srType = DataUtil.clearBracket(column.getType());

if (!Const.DataTypeRelationMap.containsKey(flinkTypeRoot)) {
throw new RuntimeException(
"Flink type not supported for column " + column.getName() +
", Flink type is -> [" + flinkTypeRoot.toString() + "]");
}
if (!Const.DataTypeRelationMap.get(flinkTypeRoot).containsKey(srType)) {
throw new RuntimeException(
"StarRocks type can not convert to Flink type for column " + column.getName() +
", StarRocks type is -> [" + srType + "], " +
"Flink type is -> [" + flinkTypeRoot.toString() + "]"
);
}

if (translators != null) {
Object[] result = translators.transToFlinkData(fieldVector.getMinorType(), fieldVector, rowCountOfBatch, i, nullable);
for (int ri = 0; ri < result.length; ri ++) {
setValueToFlinkRows(ri, i, result[ri]);
}
StarRocksToFlinkTrans translators = Const.DataTypeRelationMap.get(flinkTypeRoot).get(srType);
Object[] result = translators.transToFlinkData(fieldVector.getMinorType(), fieldVector, rowCountOfBatch, i, nullable);
for (int ri = 0; ri < result.length; ri ++) {
setValueToFlinkRows(ri, i, result[ri]);
}
}
}
Expand Down