fix(postgres): preserve index key ordering on transfer

Closes #8559
This commit is contained in:
t8y2 committed 2026-09-09 15:27:31 +08:00
1 parent 19b0c49db4
commit 78b0f935be
24 files changed
+292 -18

No files matched your search

@@ -18,6 +18,7 @@ public final class IndexInfo {
// doesn't track this (provenance unknown for that dialect/path). Mirrors
// crate::types::IndexInfo::key_is_expression on the Rust side (#6312 review).
private List<Boolean> key_is_expression;
private List<Integer> key_options = Collections.emptyList();
// True when this index is the object behind a table constraint (PRIMARY KEY / UNIQUE)
// instead of a standalone index. Dameng lists both kinds in ALL_INDEXES but only accepts
// index-level DDL for standalone ones, so the SQL builder needs to tell them apart
@@ -104,6 +105,10 @@ public final class IndexInfo {
return key_is_expression;
}
public List<Integer> getKey_options() {
return key_options;
}
public boolean getConstraint_backed() {
return constraint_backed;
}
@@ -144,6 +149,10 @@ public final class IndexInfo {
this.key_is_expression = key_is_expression;
}
public void setKey_options(List<Integer> key_options) {
this.key_options = key_options == null ? Collections.emptyList() : key_options;
}
public void setConstraint_backed(boolean constraint_backed) {
this.constraint_backed = constraint_backed;
}
@@ -162,7 +171,8 @@ public final class IndexInfo {
&& Objects.equals(index_type, that.index_type)
&& Objects.equals(included_columns, that.included_columns)
&& Objects.equals(comment, that.comment)
&& Objects.equals(key_is_expression, that.key_is_expression);
&& Objects.equals(key_is_expression, that.key_is_expression)
&& Objects.equals(key_options, that.key_options);
}
@Override
@@ -177,6 +187,7 @@ public final class IndexInfo {
included_columns,
comment,
key_is_expression,
key_options,
constraint_backed
);
}
@@ -192,6 +203,7 @@ public final class IndexInfo {
+ ", included_columns=" + included_columns
+ ", comment=" + comment
+ ", key_is_expression=" + key_is_expression
+ ", key_options=" + key_options
+ ", constraint_backed=" + constraint_backed
+ ")";
}
@@ -546,7 +546,9 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent {
String sql = "SELECT i.relname AS index_name, am.amname AS index_type, " +
"(ix.indisunique AND ix.indisvalid) AS is_unique, ix.indisprimary AS is_primary, " +
"COALESCE(a.attname, " + profile.catalogPrefixedFunction("get_indexdef") + "(ix.indexrelid, k.n, true)) AS column_text, " +
"(a.attname IS NULL) AS is_expression " +
"(a.attname IS NULL) AS is_expression, " +
"array_length(ix.indoption, 1) AS nkeyatts, k.n AS key_position, " +
"ix.indoption[(k.n - 1)::int] AS key_option " +
"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 " +
@@ -569,24 +571,36 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent {
indexName,
name -> new IndexBuilder(name, indexType, isUnique, isPrimary)
);
builder.columns.add(rs.getString("column_text"));
String columnText = rs.getString("column_text");
if (rs.getObject("nkeyatts") != null && rs.getObject("key_position") != null
&& rs.getInt("key_position") > rs.getInt("nkeyatts")) {
builder.includedColumns.add(columnText);
continue;
}
builder.columns.add(columnText);
builder.keyIsExpression.add(rs.getBoolean("is_expression"));
Object keyOption = rs.getObject("key_option");
if (keyOption != null) {
builder.keyOptions.add(rs.getInt("key_option"));
}
}
}
}
List<IndexInfo> result = new ArrayList<>();
for (IndexBuilder builder : byName.values()) {
result.add(new IndexInfo(
IndexInfo index = new IndexInfo(
builder.name,
builder.columns,
builder.isUnique,
builder.isPrimary,
null,
builder.indexType,
null,
builder.includedColumns.isEmpty() ? null : builder.includedColumns,
null,
builder.keyIsExpression
));
);
index.setKey_options(builder.keyOptions);
result.add(index);
}
return result;
});
@@ -598,8 +612,9 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent {
private final boolean isUnique;
private final boolean isPrimary;
private final List<String> columns = new ArrayList<>();
private final List<String> includedColumns = new ArrayList<>();
private final List<Boolean> keyIsExpression = new ArrayList<>();
private final List<Integer> keyOptions = new ArrayList<>();
private IndexBuilder(String name, String indexType, boolean isUnique, boolean isPrimary) {
this.name = name;
this.indexType = indexType;