Skip to content

Commit ffae2f7

Browse files
[Improve][Transform] Remove can't find field exception (#6691)
1 parent 6c65805 commit ffae2f7

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

Diff for: seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/filter/FilterFieldTransform.java

+19-31
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,23 @@
3535
import lombok.extern.slf4j.Slf4j;
3636

3737
import java.util.ArrayList;
38-
import java.util.Arrays;
3938
import java.util.List;
4039
import java.util.stream.Collectors;
4140

4241
@Slf4j
4342
public class FilterFieldTransform extends AbstractCatalogSupportTransform {
4443
public static final String PLUGIN_NAME = "Filter";
4544
private int[] inputValueIndex;
46-
private final String[] fields;
45+
private final List<String> fields;
4746

4847
public FilterFieldTransform(
4948
@NonNull ReadonlyConfig config, @NonNull CatalogTable catalogTable) {
5049
super(catalogTable);
5150
SeaTunnelRowType seaTunnelRowType = catalogTable.getTableSchema().toPhysicalRowDataType();
52-
fields = config.get(FilterFieldTransformConfig.KEY_FIELDS).toArray(new String[0]);
51+
fields = config.get(FilterFieldTransformConfig.KEY_FIELDS);
5352
List<String> canNotFoundFields =
54-
Arrays.stream(fields)
55-
.filter(
56-
field -> {
57-
try {
58-
seaTunnelRowType.indexOf(field);
59-
return false;
60-
} catch (Exception e) {
61-
return true;
62-
}
63-
})
53+
fields.stream()
54+
.filter(field -> seaTunnelRowType.indexOf(field, false) == -1)
6455
.collect(Collectors.toList());
6556

6657
if (!CollectionUtils.isEmpty(canNotFoundFields)) {
@@ -77,34 +68,32 @@ public String getPluginName() {
7768
@Override
7869
protected SeaTunnelRow transformRow(SeaTunnelRow inputRow) {
7970
// todo reuse array container if not remove fields
80-
Object[] values = new Object[fields.length];
81-
for (int i = 0; i < fields.length; i++) {
71+
Object[] values = new Object[fields.size()];
72+
for (int i = 0; i < fields.size(); i++) {
8273
values[i] = inputRow.getField(inputValueIndex[i]);
8374
}
84-
return new SeaTunnelRow(values);
75+
SeaTunnelRow outputRow = new SeaTunnelRow(values);
76+
outputRow.setRowKind(inputRow.getRowKind());
77+
outputRow.setTableId(inputRow.getTableId());
78+
return outputRow;
8579
}
8680

8781
@Override
8882
protected TableSchema transformTableSchema() {
89-
List<String> filterFields = Arrays.asList(fields);
9083
List<Column> outputColumns = new ArrayList<>();
9184

9285
SeaTunnelRowType seaTunnelRowType =
9386
inputCatalogTable.getTableSchema().toPhysicalRowDataType();
9487

95-
inputValueIndex = new int[filterFields.size()];
88+
inputValueIndex = new int[fields.size()];
9689
ArrayList<String> outputFieldNames = new ArrayList<>();
97-
for (int i = 0; i < filterFields.size(); i++) {
98-
String field = filterFields.get(i);
90+
List<Column> inputColumns = inputCatalogTable.getTableSchema().getColumns();
91+
for (int i = 0; i < fields.size(); i++) {
92+
String field = fields.get(i);
9993
int inputFieldIndex = seaTunnelRowType.indexOf(field);
100-
if (inputFieldIndex == -1) {
101-
throw TransformCommonError.cannotFindInputFieldError(getPluginName(), field);
102-
}
10394
inputValueIndex[i] = inputFieldIndex;
104-
outputColumns.add(
105-
inputCatalogTable.getTableSchema().getColumns().get(inputFieldIndex).copy());
106-
outputFieldNames.add(
107-
inputCatalogTable.getTableSchema().getColumns().get(inputFieldIndex).getName());
95+
outputColumns.add(inputColumns.get(inputFieldIndex).copy());
96+
outputFieldNames.add(inputColumns.get(inputFieldIndex).getName());
10897
}
10998

11099
List<ConstraintKey> outputConstraintKeys =
@@ -123,10 +112,9 @@ protected TableSchema transformTableSchema() {
123112
.collect(Collectors.toList());
124113

125114
PrimaryKey copiedPrimaryKey = null;
126-
if (inputCatalogTable.getTableSchema().getPrimaryKey() != null
127-
&& outputFieldNames.containsAll(
128-
inputCatalogTable.getTableSchema().getPrimaryKey().getColumnNames())) {
129-
copiedPrimaryKey = inputCatalogTable.getTableSchema().getPrimaryKey().copy();
115+
PrimaryKey primaryKey = inputCatalogTable.getTableSchema().getPrimaryKey();
116+
if (primaryKey != null && outputFieldNames.containsAll(primaryKey.getColumnNames())) {
117+
copiedPrimaryKey = primaryKey.copy();
130118
}
131119

132120
return TableSchema.builder()

0 commit comments

Comments
 (0)