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;
|
||||
|
||||
@@ -157,6 +157,34 @@ class PostgresLikeAgentTest {
|
||||
assertTrue(index.getIs_unique());
|
||||
assertFalse(index.getIs_primary());
|
||||
assertEquals("btree", index.getIndex_type());
|
||||
assertTrue(index.getKey_options().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listIndexesPreservesPerKeyOrderingAndSeparatesIncludedColumns() {
|
||||
TestPostgresLikeAgent agent = new TestPostgresLikeAgent(preparedConnection(resultSet(
|
||||
new String[]{
|
||||
"index_name", "index_type", "is_unique", "is_primary", "column_text",
|
||||
"is_expression", "nkeyatts", "key_position", "key_option"
|
||||
},
|
||||
new Object[][]{
|
||||
{"event_order_idx", "btree", false, false, "created_at", false, 4, 1, 1},
|
||||
{"event_order_idx", "btree", false, false, "tenant_id", false, 4, 2, 0},
|
||||
{"event_order_idx", "btree", false, false, "score", false, 4, 3, 2},
|
||||
{"event_order_idx", "btree", false, false, "lower(payload)", true, 4, 4, 3},
|
||||
{"event_order_idx", "btree", false, false, "payload", false, 4, 5, null}
|
||||
}
|
||||
)));
|
||||
agent.connect(new ConnectParams());
|
||||
|
||||
IndexInfo index = agent.listIndexes("public", "event_log").get(0);
|
||||
|
||||
assertEquals(java.util.Arrays.asList("created_at", "tenant_id", "score", "lower(payload)"), index.getColumns());
|
||||
assertEquals(java.util.Arrays.asList(false, false, false, true), index.getKey_is_expression());
|
||||
assertEquals(java.util.Arrays.asList(1, 0, 2, 3), index.getKey_options());
|
||||
assertEquals(java.util.Collections.singletonList("payload"), index.getIncluded_columns());
|
||||
com.google.gson.Gson gson = new com.google.gson.Gson();
|
||||
assertEquals(index, gson.fromJson(gson.toJson(index), IndexInfo.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -174,6 +202,8 @@ class PostgresLikeAgentTest {
|
||||
assertTrue(sql.contains("LEFT JOIN pg_catalog.pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum AND k.attnum > 0"), sql);
|
||||
assertTrue(sql.contains("pg_catalog.pg_get_indexdef(ix.indexrelid, k.n, true)"), sql);
|
||||
assertTrue(sql.contains("ix.indisunique AND ix.indisvalid"), sql);
|
||||
assertTrue(sql.contains("ix.indoption[(k.n - 1)::int]"), sql);
|
||||
assertTrue(sql.contains("array_length(ix.indoption, 1) AS nkeyatts"), sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user