fix(vastbase): support legacy catalog array metadata

This commit is contained in:
t8y2 committed 2026-08-01 03:43:47 +08:00
1 parent a93fcef5d6
commit 312a139998
7 files changed
+1087 -13

No files matched your search

@@ -2,8 +2,10 @@ package com.dbx.agent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -106,20 +108,35 @@ public final class DdlBuilder {
}
List<String> primaryKeys = new ArrayList<>();
for (ColumnInfo column : columns) {
if (column.getIs_primary_key()) {
primaryKeys.add(quoteIdent(column.getName(), useBacktick));
for (IndexInfo index : indexes) {
if (!index.getIs_primary() || index.getColumns().isEmpty()) {
continue;
}
for (String column : index.getColumns()) {
primaryKeys.add(quoteIdent(column, useBacktick));
}
break;
}
if (primaryKeys.isEmpty()) {
for (ColumnInfo column : columns) {
if (column.getIs_primary_key()) {
primaryKeys.add(quoteIdent(column.getName(), useBacktick));
}
}
}
if (!primaryKeys.isEmpty()) {
columnLines.add(" PRIMARY KEY (" + join(primaryKeys, ", ") + ")");
}
for (ForeignKeyInfo fk : foreignKeys) {
for (ForeignKeyGroup foreignKey : groupForeignKeys(foreignKeys)) {
String constraint = notBlank(foreignKey.name)
? "CONSTRAINT " + quoteIdent(foreignKey.name, useBacktick) + " "
: "";
columnLines.add(
" CONSTRAINT " + quoteIdent(fk.getName(), useBacktick)
+ " FOREIGN KEY (" + quoteIdent(fk.getColumn(), useBacktick) + ") "
+ "REFERENCES " + quoteIdent(fk.getRef_table(), useBacktick) + "(" + quoteIdent(fk.getRef_column(), useBacktick) + ")"
" " + constraint
+ "FOREIGN KEY (" + joinQuoted(foreignKey.columns, useBacktick) + ") "
+ "REFERENCES " + quoteIdent(foreignKey.refTable, useBacktick)
+ "(" + joinQuoted(foreignKey.refColumns, useBacktick) + ")"
);
}
@@ -202,6 +219,50 @@ public final class DdlBuilder {
return ddl.toString();
}
private static List<ForeignKeyGroup> groupForeignKeys(List<ForeignKeyInfo> foreignKeys) {
List<ForeignKeyGroup> result = new ArrayList<>();
Map<String, ForeignKeyGroup> namedGroups = new LinkedHashMap<>();
for (ForeignKeyInfo foreignKey : foreignKeys) {
ForeignKeyGroup group = null;
if (notBlank(foreignKey.getName()) && notBlank(foreignKey.getRef_table())) {
String groupKey = foreignKey.getName() + "\u0000" + foreignKey.getRef_table();
group = namedGroups.get(groupKey);
if (group == null) {
group = new ForeignKeyGroup(foreignKey.getName(), foreignKey.getRef_table());
namedGroups.put(groupKey, group);
result.add(group);
}
}
if (group == null) {
group = new ForeignKeyGroup(foreignKey.getName(), foreignKey.getRef_table());
result.add(group);
}
group.columns.add(foreignKey.getColumn());
group.refColumns.add(foreignKey.getRef_column());
}
return result;
}
private static String joinQuoted(List<String> values, boolean useBacktick) {
List<String> quotedValues = new ArrayList<>();
for (String value : values) {
quotedValues.add(quoteIdent(value, useBacktick));
}
return join(quotedValues, ", ");
}
private static final class ForeignKeyGroup {
private final String name;
private final String refTable;
private final List<String> columns = new ArrayList<>();
private final List<String> refColumns = new ArrayList<>();
private ForeignKeyGroup(String name, String refTable) {
this.name = name;
this.refTable = refTable;
}
}
private static String quoteIdent(String identifier, boolean useBacktick) {
return useBacktick
? JdbcIdentifiers.INSTANCE.backtick(identifier)
@@ -3,9 +3,11 @@ package com.dbx.agent;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
public abstract class PostgresLikeAgent extends AbstractJdbcAgent {
@@ -241,16 +243,23 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent {
@Override
public String getTableDdl(String schema, String table) {
TableAttributeCache attributeCache = profile.mapsCatalogAttributeArraysInJava()
? new TableAttributeCache(schema, table)
: null;
List<IndexInfo> indexes;
try {
indexes = listIndexes(schema, table);
indexes = attributeCache == null
? listIndexes(schema, table)
: listIndexes(schema, table, attributeCache);
} catch (RuntimeException e) {
indexes = Collections.emptyList();
}
List<ForeignKeyInfo> foreignKeys;
try {
foreignKeys = listForeignKeys(schema, table);
foreignKeys = attributeCache == null
? listForeignKeys(schema, table)
: listForeignKeys(schema, table, attributeCache);
} catch (RuntimeException e) {
foreignKeys = Collections.emptyList();
}
@@ -274,7 +283,9 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent {
return DdlBuilder.buildTableDdl(
schema,
table,
getColumns(schema, table),
attributeCache == null
? getColumns(schema, table)
: getColumns(schema, table, attributeCache),
indexes,
foreignKeys,
checkConstraints,
@@ -380,8 +391,19 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent {
@Override
public List<ColumnInfo> getColumns(String schema, String table) {
TableAttributeCache attributeCache = profile.mapsCatalogAttributeArraysInJava()
? new TableAttributeCache(schema, table)
: null;
return getColumns(schema, table, attributeCache);
}
private List<ColumnInfo> getColumns(
String schema,
String table,
TableAttributeCache attributeCache
) {
return unchecked(() -> {
Set<String> primaryKeys = primaryKeys(schema, table);
Set<String> primaryKeys = primaryKeys(schema, table, attributeCache);
List<ColumnInfo> result = new ArrayList<>();
String sql = "SELECT a.attname AS column_name, " +
profile.catalogBuiltinFunction("format_type") + "(a.atttypid, a.atttypmod) AS data_type, " +
@@ -427,7 +449,14 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent {
});
}
private Set<String> primaryKeys(String schema, String table) {
private Set<String> primaryKeys(
String schema,
String table,
TableAttributeCache attributeCache
) {
if (profile.mapsCatalogAttributeArraysInJava()) {
return primaryKeysFromCatalogArrays(schema, table, attributeCache);
}
return unchecked(() -> {
Set<String> primaryKeys = new LinkedHashSet<>();
String sql = "SELECT a.attname AS column_name " +
@@ -453,8 +482,56 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent {
});
}
private Set<String> primaryKeysFromCatalogArrays(
String schema,
String table,
TableAttributeCache attributeCache
) {
return unchecked(() -> {
List<Integer> attributeNumbers = Collections.emptyList();
String sql = "SELECT co.conkey AS column_numbers " +
"FROM " + profile.catalogRelation("constraint") + " co " +
"JOIN " + profile.catalogRelation("class") + " c ON c.oid = co.conrelid " +
"JOIN " + profile.catalogRelation("namespace") + " n ON n.oid = c.relnamespace " +
"WHERE co.contype = 'p' " +
"AND n.nspname = ? " +
"AND c.relname = ? " +
"ORDER BY co.conname";
try (java.sql.PreparedStatement stmt = requireConnection().prepareStatement(sql)) {
stmt.setString(1, schema);
stmt.setString(2, table);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
attributeNumbers = readAttributeNumbers(rs, "column_numbers");
}
}
}
if (attributeNumbers.isEmpty()) {
return new LinkedHashSet<>();
}
return new LinkedHashSet<>(mapRequiredAttributeNumbers(
attributeNumbers,
attributeCache.get()
));
});
}
@Override
public List<IndexInfo> listIndexes(String schema, String table) {
TableAttributeCache attributeCache = profile.mapsCatalogAttributeArraysInJava()
? new TableAttributeCache(schema, table)
: null;
return listIndexes(schema, table, attributeCache);
}
private List<IndexInfo> listIndexes(
String schema,
String table,
TableAttributeCache attributeCache
) {
if (profile.mapsCatalogAttributeArraysInJava()) {
return listIndexesFromCatalogArrays(schema, table, attributeCache);
}
return unchecked(() -> {
List<IndexInfo> result = new ArrayList<>();
String sql = "SELECT i.relname AS index_name, am.amname AS index_type, " +
@@ -497,8 +574,80 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent {
});
}
private List<IndexInfo> listIndexesFromCatalogArrays(
String schema,
String table,
TableAttributeCache attributeCache
) {
return unchecked(() -> {
List<CatalogIndex> catalogIndexes = new ArrayList<>();
String sql = "SELECT i.relname AS index_name, am.amname AS index_type, " +
"ix.indisunique AS is_unique, ix.indisprimary AS is_primary, " +
"ix.indkey AS column_numbers " +
"FROM " + profile.catalogRelation("index") + " ix " +
"JOIN " + profile.catalogRelation("class") + " t ON t.oid = ix.indrelid " +
"JOIN " + profile.catalogRelation("class") + " i ON i.oid = ix.indexrelid " +
"JOIN " + profile.catalogRelation("namespace") + " n ON n.oid = t.relnamespace " +
"JOIN " + profile.catalogRelation("am") + " am ON am.oid = i.relam " +
"WHERE n.nspname = ? AND t.relname = ? " +
"ORDER BY i.relname";
try (java.sql.PreparedStatement stmt = requireConnection().prepareStatement(sql)) {
stmt.setString(1, schema);
stmt.setString(2, table);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
catalogIndexes.add(new CatalogIndex(
rs.getString("index_name"),
rs.getString("index_type"),
rs.getBoolean("is_unique"),
rs.getBoolean("is_primary"),
readAttributeNumbers(rs, "column_numbers")
));
}
}
}
if (catalogIndexes.isEmpty()) {
return Collections.emptyList();
}
Map<Integer, String> attributes = attributeCache.get();
List<IndexInfo> result = new ArrayList<>();
for (CatalogIndex index : catalogIndexes) {
List<String> columns = mapRequiredAttributeNumbers(index.attributeNumbers, attributes);
if (columns.isEmpty()) {
continue;
}
result.add(new IndexInfo(
index.name,
columns,
index.unique,
index.primary,
null,
index.type,
null,
null
));
}
return result;
});
}
@Override
public List<ForeignKeyInfo> listForeignKeys(String schema, String table) {
TableAttributeCache attributeCache = profile.mapsCatalogAttributeArraysInJava()
? new TableAttributeCache(schema, table)
: null;
return listForeignKeys(schema, table, attributeCache);
}
private List<ForeignKeyInfo> listForeignKeys(
String schema,
String table,
TableAttributeCache attributeCache
) {
if (profile.mapsCatalogAttributeArraysInJava()) {
return listForeignKeysFromCatalogArrays(schema, table, attributeCache);
}
return unchecked(() -> {
List<ForeignKeyInfo> result = new ArrayList<>();
String sql = "SELECT co.conname AS constraint_name, " +
@@ -535,6 +684,309 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent {
});
}
private List<ForeignKeyInfo> listForeignKeysFromCatalogArrays(
String schema,
String table,
TableAttributeCache attributeCache
) {
return unchecked(() -> {
List<CatalogForeignKey> catalogForeignKeys = new ArrayList<>();
String sql = "SELECT co.conname AS constraint_name, " +
"co.conkey AS column_numbers, co.confkey AS ref_column_numbers, " +
"rc.relname AS ref_table, rc.oid AS ref_table_oid " +
"FROM " + profile.catalogRelation("constraint") + " co " +
"JOIN " + profile.catalogRelation("class") + " c ON c.oid = co.conrelid " +
"JOIN " + profile.catalogRelation("namespace") + " n ON n.oid = c.relnamespace " +
"JOIN " + profile.catalogRelation("class") + " rc ON rc.oid = co.confrelid " +
"WHERE co.contype = 'f' " +
"AND n.nspname = ? " +
"AND c.relname = ? " +
"ORDER BY co.conname";
try (java.sql.PreparedStatement stmt = requireConnection().prepareStatement(sql)) {
stmt.setString(1, schema);
stmt.setString(2, table);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
Object refTableOid = rs.getObject("ref_table_oid");
catalogForeignKeys.add(new CatalogForeignKey(
rs.getString("constraint_name"),
rs.getString("ref_table"),
refTableOid instanceof Number
? ((Number) refTableOid).longValue()
: Long.parseLong(String.valueOf(refTableOid)),
readAttributeNumbers(rs, "column_numbers"),
readAttributeNumbers(rs, "ref_column_numbers")
));
}
}
}
if (catalogForeignKeys.isEmpty()) {
return Collections.emptyList();
}
Map<Integer, String> attributes = attributeCache.get();
Set<Long> referencedRelationOids = new LinkedHashSet<>();
for (CatalogForeignKey foreignKey : catalogForeignKeys) {
if (!foreignKey.attributeNumbers.isEmpty()
&& foreignKey.attributeNumbers.size() == foreignKey.refAttributeNumbers.size()) {
referencedRelationOids.add(foreignKey.refTableOid);
}
}
Map<Long, Map<Integer, String>> referencedAttributes = relationAttributeNames(referencedRelationOids);
List<ForeignKeyInfo> result = new ArrayList<>();
for (CatalogForeignKey foreignKey : catalogForeignKeys) {
if (foreignKey.attributeNumbers.isEmpty()
|| foreignKey.attributeNumbers.size() != foreignKey.refAttributeNumbers.size()) {
continue;
}
Map<Integer, String> refAttributes = referencedAttributes.get(foreignKey.refTableOid);
if (refAttributes == null) {
continue;
}
List<String> columns = mapRequiredAttributeNumbers(
foreignKey.attributeNumbers,
attributes
);
List<String> refColumns = mapRequiredAttributeNumbers(
foreignKey.refAttributeNumbers,
refAttributes
);
if (columns.isEmpty() || refColumns.isEmpty()) {
continue;
}
for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
result.add(new ForeignKeyInfo(
foreignKey.name,
columns.get(columnIndex),
foreignKey.refTable,
refColumns.get(columnIndex)
));
}
}
return result;
});
}
private Map<Integer, String> tableAttributeNames(String schema, String table) throws java.sql.SQLException {
Map<Integer, String> result = new LinkedHashMap<>();
String sql = "SELECT a.attnum AS attribute_number, a.attname AS column_name " +
"FROM " + profile.catalogRelation("attribute") + " a " +
"JOIN " + profile.catalogRelation("class") + " c ON c.oid = a.attrelid " +
"JOIN " + profile.catalogRelation("namespace") + " n ON n.oid = c.relnamespace " +
"WHERE n.nspname = ? AND c.relname = ? " +
"AND a.attnum > 0 AND NOT a.attisdropped " +
"ORDER BY a.attnum";
try (java.sql.PreparedStatement stmt = requireConnection().prepareStatement(sql)) {
stmt.setString(1, schema);
stmt.setString(2, table);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
result.put(rs.getInt("attribute_number"), rs.getString("column_name"));
}
}
}
return result;
}
private Map<Long, Map<Integer, String>> relationAttributeNames(Set<Long> relationOids)
throws java.sql.SQLException {
if (relationOids.isEmpty()) {
return Collections.emptyMap();
}
StringBuilder placeholders = new StringBuilder();
for (int relationIndex = 0; relationIndex < relationOids.size(); relationIndex++) {
if (relationIndex > 0) {
placeholders.append(", ");
}
placeholders.append("?");
}
Map<Long, Map<Integer, String>> result = new LinkedHashMap<>();
String sql = "SELECT a.attrelid AS relation_oid, " +
"a.attnum AS attribute_number, a.attname AS column_name " +
"FROM " + profile.catalogRelation("attribute") + " a " +
"WHERE a.attrelid IN (" + placeholders + ") " +
"AND a.attnum > 0 AND NOT a.attisdropped " +
"ORDER BY a.attrelid, a.attnum";
try (java.sql.PreparedStatement stmt = requireConnection().prepareStatement(sql)) {
int parameterIndex = 1;
for (Long relationOid : relationOids) {
stmt.setLong(parameterIndex, relationOid);
parameterIndex += 1;
}
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
long relationOid = rs.getLong("relation_oid");
Map<Integer, String> attributes = result.get(relationOid);
if (attributes == null) {
attributes = new LinkedHashMap<>();
result.put(relationOid, attributes);
}
attributes.put(rs.getInt("attribute_number"), rs.getString("column_name"));
}
}
}
return result;
}
private static List<String> mapRequiredAttributeNumbers(
List<Integer> attributeNumbers,
Map<Integer, String> attributes
) {
List<String> result = new ArrayList<>();
for (Integer attributeNumber : attributeNumbers) {
if (attributeNumber == null || attributeNumber <= 0) {
return Collections.emptyList();
}
String attribute = attributes.get(attributeNumber);
if (attribute == null) {
return Collections.emptyList();
}
result.add(attribute);
}
return result.size() == attributeNumbers.size()
? result
: Collections.emptyList();
}
private static List<Integer> readAttributeNumbers(ResultSet rs, String columnName)
throws java.sql.SQLException {
Object rawValue = null;
java.sql.Array sqlArray = null;
try {
sqlArray = rs.getArray(columnName);
if (sqlArray != null) {
rawValue = sqlArray.getArray();
}
} catch (java.sql.SQLException | UnsupportedOperationException | IncompatibleClassChangeError ignored) {
} finally {
if (sqlArray != null) {
freeSqlArray(sqlArray);
}
}
if (rawValue == null) {
try {
rawValue = rs.getObject(columnName);
} catch (java.sql.SQLException | UnsupportedOperationException | IncompatibleClassChangeError ignored) {
}
}
if (rawValue == null) {
rawValue = rs.getString(columnName);
}
return parseAttributeNumbers(rawValue);
}
private static List<Integer> parseAttributeNumbers(Object rawValue) throws java.sql.SQLException {
if (rawValue == null) {
return Collections.emptyList();
}
if (rawValue instanceof java.sql.Array) {
java.sql.Array sqlArray = (java.sql.Array) rawValue;
try {
return parseAttributeNumbers(sqlArray.getArray());
} finally {
freeSqlArray(sqlArray);
}
}
List<Integer> result = new ArrayList<>();
if (rawValue.getClass().isArray()) {
int length = java.lang.reflect.Array.getLength(rawValue);
for (int arrayIndex = 0; arrayIndex < length; arrayIndex++) {
appendAttributeNumbers(result, java.lang.reflect.Array.get(rawValue, arrayIndex));
}
return result;
}
appendAttributeNumbers(result, rawValue);
return result;
}
private static void freeSqlArray(java.sql.Array sqlArray) {
try {
sqlArray.free();
} catch (java.sql.SQLException | UnsupportedOperationException | IncompatibleClassChangeError ignored) {
}
}
private static void appendAttributeNumbers(List<Integer> result, Object rawValue) {
if (rawValue == null) {
return;
}
if (rawValue instanceof Number) {
result.add(((Number) rawValue).intValue());
return;
}
java.util.regex.Matcher matcher = java.util.regex.Pattern
.compile("-?\\d+")
.matcher(String.valueOf(rawValue));
while (matcher.find()) {
result.add(Integer.parseInt(matcher.group()));
}
}
private final class TableAttributeCache {
private final String schema;
private final String table;
private Map<Integer, String> attributes;
private TableAttributeCache(String schema, String table) {
this.schema = schema;
this.table = table;
}
private Map<Integer, String> get() throws java.sql.SQLException {
if (attributes == null) {
attributes = tableAttributeNames(schema, table);
}
return attributes;
}
}
private static final class CatalogIndex {
private final String name;
private final String type;
private final boolean unique;
private final boolean primary;
private final List<Integer> attributeNumbers;
private CatalogIndex(
String name,
String type,
boolean unique,
boolean primary,
List<Integer> attributeNumbers
) {
this.name = name;
this.type = type;
this.unique = unique;
this.primary = primary;
this.attributeNumbers = attributeNumbers;
}
}
private static final class CatalogForeignKey {
private final String name;
private final String refTable;
private final long refTableOid;
private final List<Integer> attributeNumbers;
private final List<Integer> refAttributeNumbers;
private CatalogForeignKey(
String name,
String refTable,
long refTableOid,
List<Integer> attributeNumbers,
List<Integer> refAttributeNumbers
) {
this.name = name;
this.refTable = refTable;
this.refTableOid = refTableOid;
this.attributeNumbers = attributeNumbers;
this.refAttributeNumbers = refAttributeNumbers;
}
}
@Override
public List<TriggerInfo> listTriggers(String schema, String table) {
return unchecked(() -> {
@@ -6,6 +6,7 @@ public final class PostgresLikeAgentProfile {
private final int defaultPort;
private final String catalogSchema;
private final String catalogPrefix;
private final boolean mapCatalogAttributeArraysInJava;
public PostgresLikeAgentProfile(String driverClass, String urlTemplate) {
this(driverClass, urlTemplate, 0, "pg_catalog", "pg_");
@@ -26,6 +27,17 @@ public final class PostgresLikeAgentProfile {
int defaultPort,
String catalogSchema,
String catalogPrefix
) {
this(driverClass, urlTemplate, defaultPort, catalogSchema, catalogPrefix, false);
}
private PostgresLikeAgentProfile(
String driverClass,
String urlTemplate,
int defaultPort,
String catalogSchema,
String catalogPrefix,
boolean mapCatalogAttributeArraysInJava
) {
this.driverClass = driverClass;
this.urlTemplate = urlTemplate;
@@ -35,6 +47,7 @@ public final class PostgresLikeAgentProfile {
// the full metadata implementation for each compatible database.
this.catalogSchema = catalogSchema;
this.catalogPrefix = catalogPrefix;
this.mapCatalogAttributeArraysInJava = mapCatalogAttributeArraysInJava;
}
public String getDriverClass() {
@@ -61,6 +74,21 @@ public final class PostgresLikeAgentProfile {
return catalogPrefix + "toast_temp_";
}
public PostgresLikeAgentProfile withCatalogAttributeArraysMappedInJava() {
return new PostgresLikeAgentProfile(
driverClass,
urlTemplate,
defaultPort,
catalogSchema,
catalogPrefix,
true
);
}
public boolean mapsCatalogAttributeArraysInJava() {
return mapCatalogAttributeArraysInJava;
}
public String catalogRelation(String name) {
return catalogSchema + "." + catalogPrefix + name;
}