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

[FLINK-36271] Support reading json and jsonb types in PostgreSQL dialect #141

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -47,6 +47,10 @@ protected DataType getMapping(String pgType, int precision, int scale) {
return DataTypes.STRING();
case PG_STRING_ARRAY:
return DataTypes.ARRAY(DataTypes.STRING());
case PG_JSON: // TODO: Add support for JSON type.
return null;
case PG_JSONB: // CrateDB supports JSON type, but not JSONB.
return null;
default:
return super.getMapping(pgType, precision, scale);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.flink.annotation.Internal;
import org.apache.flink.connector.jdbc.postgres.database.dialect.CompatiblePostgresDialectConverter;
import org.apache.flink.table.data.StringData;
import org.apache.flink.table.types.logical.RowType;

/**
Expand All @@ -39,4 +40,9 @@ public CrateDBDialectConverter(RowType rowType) {
public String compatibleConverterName() {
return "CrateDB";
}

@Override
protected JdbcDeserializationConverter createVarcharConverter() {
return val -> StringData.fromString((String) val);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public class PostgresTypeMapper implements JdbcCatalogTypeMapper {
private static final String PG_CHARACTER_ARRAY = "_character";
private static final String PG_CHARACTER_VARYING = "varchar";
private static final String PG_CHARACTER_VARYING_ARRAY = "_varchar";
protected static final String PG_JSON = "json";
protected static final String PG_JSONB = "jsonb";

@Override
public DataType mapping(ObjectPath tablePath, ResultSetMetaData metadata, int colIndex)
Expand Down Expand Up @@ -157,6 +159,8 @@ protected DataType getMapping(String pgType, int precision, int scale) {
case PG_CHARACTER_VARYING_ARRAY:
return DataTypes.ARRAY(DataTypes.VARCHAR(precision));
case PG_TEXT:
case PG_JSON:
case PG_JSONB:
return DataTypes.STRING();
case PG_TEXT_ARRAY:
return DataTypes.ARRAY(DataTypes.STRING());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
import org.apache.flink.annotation.Internal;
import org.apache.flink.connector.jdbc.core.database.dialect.AbstractDialectConverter;
import org.apache.flink.table.data.GenericArrayData;
import org.apache.flink.table.data.StringData;
import org.apache.flink.table.types.logical.ArrayType;
import org.apache.flink.table.types.logical.LogicalType;
import org.apache.flink.table.types.logical.LogicalTypeRoot;
import org.apache.flink.table.types.logical.RowType;
import org.apache.flink.table.types.logical.utils.LogicalTypeUtils;

import org.postgresql.util.PGobject;

import java.lang.reflect.Array;

/**
Expand Down Expand Up @@ -91,7 +94,23 @@ private JdbcDeserializationConverter createPostgresArrayConverter(ArrayType arra
// Have its own method so that Postgres can support primitives that super class doesn't support
// in the future
private JdbcDeserializationConverter createPrimitiveConverter(LogicalType type) {
return super.createInternalConverter(type);
switch (type.getTypeRoot()) {
case VARCHAR:
return createVarcharConverter();
default:
return super.createInternalConverter(type);
}
}

protected JdbcDeserializationConverter createVarcharConverter() {
return val -> {
if (val instanceof PGobject) {
PGobject obj = (PGobject) val;
return StringData.fromString(obj.getValue());
} else {
return StringData.fromString((String) val);
}
};
}

@Override
Expand Down