Skip to content
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 @@ -17,8 +17,11 @@

package org.apache.flink.cdc.connectors.postgres.table;

import java.util.List;
import java.util.Objects;
import org.apache.flink.cdc.debezium.table.DeserializationRuntimeConverter;
import org.apache.flink.cdc.debezium.table.DeserializationRuntimeConverterFactory;
import org.apache.flink.table.data.GenericArrayData;
import org.apache.flink.table.data.StringData;
import org.apache.flink.table.types.logical.LogicalType;

Expand Down Expand Up @@ -52,6 +55,8 @@ public Optional<DeserializationRuntimeConverter> createUserDefinedConverter(
switch (logicalType.getTypeRoot()) {
case VARCHAR:
return createStringConverter();
case ARRAY:
return createArrayConverter();
default:
// fallback to default converter
return Optional.empty();
Expand Down Expand Up @@ -96,4 +101,36 @@ public Object convert(Object dbzObj, Schema schema) throws Exception {
}
});
}

private static Optional<DeserializationRuntimeConverter> createArrayConverter() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a more general way, there is no need to implement only in PostgreSQLDeserializationConverterFactory. Java
Inside, in DebeziumEventDeserializationSchema achieve more appropriate

return Optional.of(
new DeserializationRuntimeConverter() {

private static final long serialVersionUID = 1L;

@Override
public Object convert(Object dbzObj, Schema schema) throws Exception {
if (dbzObj == null) {
return null;
}
if (Schema.Type.ARRAY.equals(schema.type()) && dbzObj instanceof List && schema.valueSchema() != null) {
switch (schema.valueSchema().type()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't realize that pgsql array can only support these schema.types. There are more such as Map boleans and so on.

case STRING: return new GenericArrayData(
((List<?>) dbzObj).stream().map(i -> StringData.fromString(i.toString())).toArray());
case INT8:
case INT16:
case INT32: return new GenericArrayData(
((List<?>) dbzObj).stream().mapToInt(i -> ((Integer) i)).toArray());
case INT64: return new GenericArrayData(
((List<?>) dbzObj).stream().mapToLong(i -> ((Long) i)).toArray());
}
} else if (dbzObj instanceof List) {
return new GenericArrayData(((List<?>) dbzObj).stream().filter(Objects::nonNull)
.map(i -> StringData.fromString(i.toString()))
.toArray());
}
return new GenericArrayData(new StringData[]{StringData.fromString(dbzObj.toString())});
}
});
}
}