fix(oceanbase): preserve partition DDL and toggle storage options
This commit is contained in:
1 parent
8eba12ea2f
commit
fe0dd02f2b
36 files changed
+870
-125
No files matched your search
+116
-37
@@ -15,6 +15,7 @@ import com.dbx.agent.MetadataListConstraints;
|
||||
import com.dbx.agent.ObjectInfo;
|
||||
import com.dbx.agent.ObjectSource;
|
||||
import com.dbx.agent.OracleObjectPrivilege;
|
||||
import com.dbx.agent.PartitionInfo;
|
||||
import com.dbx.agent.TableInfo;
|
||||
import com.dbx.agent.TriggerInfo;
|
||||
|
||||
@@ -32,6 +33,8 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public final class OceanBaseOracleAgent extends ConfiguredJdbcAgent {
|
||||
private static final long MICROS_PER_SECOND = 1_000_000L;
|
||||
@@ -536,45 +539,121 @@ public final class OceanBaseOracleAgent extends ConfiguredJdbcAgent {
|
||||
|
||||
@Override
|
||||
public String getTableDdl(String schema, String table) {
|
||||
List<IndexInfo> indexes;
|
||||
try {
|
||||
indexes = listIndexes(schema, table);
|
||||
} catch (RuntimeException e) {
|
||||
indexes = Collections.emptyList();
|
||||
}
|
||||
|
||||
List<ForeignKeyInfo> foreignKeys;
|
||||
try {
|
||||
foreignKeys = listForeignKeys(schema, table);
|
||||
} catch (RuntimeException e) {
|
||||
foreignKeys = Collections.emptyList();
|
||||
}
|
||||
|
||||
String tableComment = null;
|
||||
try {
|
||||
tableComment = getTableComment(schema, table);
|
||||
} catch (RuntimeException e) {
|
||||
// Table comment is optional; DDL generation should still succeed without it.
|
||||
}
|
||||
|
||||
String ddl = DdlBuilder.buildTableDdl(
|
||||
schema,
|
||||
table,
|
||||
getColumns(schema, table),
|
||||
indexes,
|
||||
foreignKeys,
|
||||
java.util.Collections.emptyList(),
|
||||
false,
|
||||
true,
|
||||
tableComment
|
||||
);
|
||||
try {
|
||||
return unchecked(() -> {
|
||||
String owner = normalizeSchema(schema);
|
||||
return DdlBuilder.appendTrailingSql(ddl, queryObjectGrantSql(owner, table));
|
||||
} catch (RuntimeException | SQLException ignored) {
|
||||
// Privilege metadata is optional; CREATE TABLE DDL should still succeed without it.
|
||||
String ddl = queryDbmsMetadataSource(owner, table, "TABLE");
|
||||
if (ddl == null || ddl.isBlank()) {
|
||||
throw new SQLException("DBMS_METADATA.GET_DDL returned empty DDL for " + owner + "." + table);
|
||||
}
|
||||
// OceanBase 4.2.5 omits the owner from CREATE TABLE, even for another schema.
|
||||
var header = Pattern.compile("(?i)^(\\s*CREATE\\s+(?:GLOBAL\\s+TEMPORARY\\s+)?TABLE\\s+)"
|
||||
+ Pattern.quote(quoteIdentifier(table)) + "(?=\\s*\\()").matcher(ddl);
|
||||
if (header.find()) {
|
||||
ddl = header.replaceFirst(Matcher.quoteReplacement(header.group(1) + quoteIdentifier(owner) + "." + quoteIdentifier(table)));
|
||||
}
|
||||
ddl = ddl.strip();
|
||||
if (!ddl.endsWith(";")) ddl += ";";
|
||||
|
||||
// GET_DDL(TABLE) includes constraints/partitioning, but not secondary indexes or comments.
|
||||
String indexSql = """
|
||||
SELECT i.INDEX_NAME FROM ALL_INDEXES i
|
||||
WHERE i.TABLE_OWNER = ? AND i.TABLE_NAME = ?
|
||||
AND NOT EXISTS (SELECT 1 FROM ALL_CONSTRAINTS c
|
||||
WHERE c.OWNER = i.OWNER AND c.INDEX_NAME = i.INDEX_NAME
|
||||
AND c.CONSTRAINT_TYPE IN ('P', 'U'))
|
||||
AND NOT (i.INDEX_NAME = 'IDX_FOR_HEAP_GTT_' || i.TABLE_NAME
|
||||
AND EXISTS (SELECT 1 FROM ALL_TABLES t WHERE t.OWNER = i.TABLE_OWNER
|
||||
AND t.TABLE_NAME = i.TABLE_NAME AND t.TEMPORARY = 'Y')
|
||||
AND EXISTS (SELECT 1 FROM ALL_IND_COLUMNS c WHERE c.INDEX_OWNER = i.OWNER
|
||||
AND c.INDEX_NAME = i.INDEX_NAME AND c.COLUMN_NAME = 'SYS_SESSION_ID'))
|
||||
ORDER BY i.INDEX_NAME
|
||||
""";
|
||||
List<String> indexNames = new ArrayList<>();
|
||||
try (var stmt = requireConnection().prepareStatement(indexSql)) {
|
||||
stmt.setString(1, owner);
|
||||
stmt.setString(2, table);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) indexNames.add(rs.getString(1));
|
||||
}
|
||||
}
|
||||
for (String index : indexNames) {
|
||||
String indexDdl = queryDbmsMetadataSource(owner, index, "INDEX");
|
||||
if (indexDdl == null || indexDdl.isBlank()) throw new SQLException("Empty index DDL: " + index);
|
||||
ddl = DdlBuilder.appendTrailingSql(ddl, indexDdl);
|
||||
}
|
||||
String tableRef = quoteIdentifier(owner) + "." + quoteIdentifier(table);
|
||||
String comment = getTableComment(owner, table);
|
||||
if (comment != null && !comment.isBlank()) {
|
||||
ddl = DdlBuilder.appendTrailingSql(ddl, "COMMENT ON TABLE " + tableRef + " IS '" + comment.replace("'", "''") + "';");
|
||||
}
|
||||
String commentSql = "SELECT COLUMN_NAME, COMMENTS FROM ALL_COL_COMMENTS WHERE OWNER = ? AND TABLE_NAME = ? AND COMMENTS IS NOT NULL ORDER BY COLUMN_NAME";
|
||||
try (var stmt = requireConnection().prepareStatement(commentSql)) {
|
||||
stmt.setString(1, owner);
|
||||
stmt.setString(2, table);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
String text = rs.getString("COMMENTS");
|
||||
if (text == null || text.isBlank()) continue;
|
||||
ddl = DdlBuilder.appendTrailingSql(ddl, "COMMENT ON COLUMN " + tableRef + "." + quoteIdentifier(rs.getString("COLUMN_NAME")) + " IS '" + text.replace("'", "''") + "';");
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
ddl = DdlBuilder.appendTrailingSql(ddl, queryObjectGrantSql(owner, table));
|
||||
} catch (RuntimeException | SQLException ignored) {
|
||||
// Privilege metadata remains optional for users without access to grant views.
|
||||
}
|
||||
return ddl;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static String quoteIdentifier(String name) {
|
||||
return "\"" + name.replace("\"", "\"\"") + "\"";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PartitionInfo> listPartitions(String schema, String table) {
|
||||
return queryPartitions(schema, table, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PartitionInfo> listSubpartitions(String schema, String table) {
|
||||
return queryPartitions(schema, table, true);
|
||||
}
|
||||
|
||||
private List<PartitionInfo> queryPartitions(String schema, String table, boolean subpartition) {
|
||||
return unchecked(() -> {
|
||||
String owner = normalizeSchema(schema);
|
||||
String prefix = subpartition ? "SUBPARTITION" : "PARTITION";
|
||||
String keysView = subpartition ? "ALL_SUBPART_KEY_COLUMNS" : "ALL_PART_KEY_COLUMNS";
|
||||
List<String> keys = new ArrayList<>();
|
||||
try (var stmt = requireConnection().prepareStatement("SELECT COLUMN_NAME FROM " + keysView
|
||||
+ " WHERE OWNER = ? AND NAME = ? AND OBJECT_TYPE = 'TABLE' ORDER BY COLUMN_POSITION")) {
|
||||
stmt.setString(1, owner);
|
||||
stmt.setString(2, table);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) keys.add(quoteIdentifier(rs.getString(1)));
|
||||
}
|
||||
}
|
||||
String sql = "SELECT p." + prefix + "_NAME AS NAME, p." + prefix + "_POSITION AS POSITION, p.HIGH_VALUE, t."
|
||||
+ prefix + "ING_TYPE AS PARTITION_TYPE FROM " + (subpartition ? "ALL_TAB_SUBPARTITIONS" : "ALL_TAB_PARTITIONS")
|
||||
+ " p JOIN ALL_PART_TABLES t ON t.OWNER = p.TABLE_OWNER AND t.TABLE_NAME = p.TABLE_NAME"
|
||||
+ " WHERE p.TABLE_OWNER = ? AND p.TABLE_NAME = ? ORDER BY "
|
||||
+ (subpartition ? "p.PARTITION_NAME, " : "") + "p." + prefix + "_POSITION";
|
||||
List<PartitionInfo> result = new ArrayList<>();
|
||||
try (var stmt = requireConnection().prepareStatement(sql)) {
|
||||
stmt.setString(1, owner);
|
||||
stmt.setString(2, table);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
String value = rs.getString("HIGH_VALUE");
|
||||
result.add(new PartitionInfo(rs.getString("NAME"), rs.getInt("POSITION"), value == null ? "" : value,
|
||||
rs.getString("PARTITION_TYPE"), String.join(", ", keys)));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
private String queryObjectGrantSql(String owner, String table) throws SQLException {
|
||||
|
||||
+66
-13
@@ -465,11 +465,11 @@ class OceanBaseOracleAgentTest {
|
||||
OceanBaseOracleAgent agent = new OceanBaseOracleAgent();
|
||||
TestSupport.setPrivateConnection(agent, preparedConnection(new ArrayList<>(),
|
||||
resultSet(
|
||||
new String[]{"INDEX_NAME", "COLUMN_NAME", "COLUMN_POSITION", "UNIQUENESS", "CONSTRAINT_TYPE", "INDEX_TYPE"},
|
||||
new Object[][]{}
|
||||
new String[]{"DDL"},
|
||||
new Object[][]{{"CREATE TABLE \"AUDIT_LOG\" (\"CREATED_AT\" TIMESTAMP DEFAULT SYSDATE NOT NULL, \"INTERNAL_NOTE\" VARCHAR2(100))"}}
|
||||
),
|
||||
resultSet(
|
||||
new String[]{"CONSTRAINT_NAME", "COLUMN_NAME", "TABLE_NAME", "REF_COLUMN_NAME"},
|
||||
new String[]{"INDEX_NAME"},
|
||||
new Object[][]{}
|
||||
),
|
||||
resultSet(
|
||||
@@ -492,7 +492,7 @@ class OceanBaseOracleAgentTest {
|
||||
|
||||
String ddl = agent.getTableDdl("APP", "AUDIT_LOG");
|
||||
|
||||
Assertions.assertTrue(ddl.contains("\"CREATED_AT\" TIMESTAMP NOT NULL DEFAULT SYSDATE"), ddl);
|
||||
Assertions.assertTrue(ddl.contains("\"CREATED_AT\" TIMESTAMP DEFAULT SYSDATE NOT NULL"), ddl);
|
||||
Assertions.assertTrue(
|
||||
ddl.contains("COMMENT ON COLUMN \"APP\".\"AUDIT_LOG\".\"CREATED_AT\" IS 'Created timestamp';"),
|
||||
ddl
|
||||
@@ -508,11 +508,11 @@ class OceanBaseOracleAgentTest {
|
||||
OceanBaseOracleAgent agent = new OceanBaseOracleAgent();
|
||||
TestSupport.setPrivateConnection(agent, preparedConnection(sql,
|
||||
resultSet(
|
||||
new String[]{"INDEX_NAME", "COLUMN_NAME", "COLUMN_POSITION", "UNIQUENESS", "CONSTRAINT_TYPE", "INDEX_TYPE"},
|
||||
new Object[][]{}
|
||||
new String[]{"DDL"},
|
||||
new Object[][]{{"CREATE TABLE \"USERS\" (\"ID\" NUMBER PRIMARY KEY, \"NAME\" VARCHAR2(100)) PARTITION BY RANGE(\"ID\") (PARTITION P1 VALUES LESS THAN(MAXVALUE))"}}
|
||||
),
|
||||
resultSet(
|
||||
new String[]{"CONSTRAINT_NAME", "COLUMN_NAME", "TABLE_NAME", "REF_COLUMN_NAME"},
|
||||
new String[]{"INDEX_NAME"},
|
||||
new Object[][]{}
|
||||
),
|
||||
resultSet(
|
||||
@@ -567,11 +567,11 @@ class OceanBaseOracleAgentTest {
|
||||
OceanBaseOracleAgent agent = new OceanBaseOracleAgent();
|
||||
TestSupport.setPrivateConnection(agent, dbaFallbackPrivilegeConnection(sql,
|
||||
resultSet(
|
||||
new String[]{"INDEX_NAME", "COLUMN_NAME", "COLUMN_POSITION", "UNIQUENESS", "CONSTRAINT_TYPE", "INDEX_TYPE"},
|
||||
new Object[][]{}
|
||||
new String[]{"DDL"},
|
||||
new Object[][]{{"CREATE TABLE \"USERS\" (\"ID\" NUMBER PRIMARY KEY, \"NAME\" VARCHAR2(100)) PARTITION BY RANGE(\"ID\") (PARTITION P1 VALUES LESS THAN(MAXVALUE))"}}
|
||||
),
|
||||
resultSet(
|
||||
new String[]{"CONSTRAINT_NAME", "COLUMN_NAME", "TABLE_NAME", "REF_COLUMN_NAME"},
|
||||
new String[]{"INDEX_NAME"},
|
||||
new Object[][]{}
|
||||
),
|
||||
resultSet(
|
||||
@@ -616,11 +616,11 @@ class OceanBaseOracleAgentTest {
|
||||
OceanBaseOracleAgent agent = new OceanBaseOracleAgent();
|
||||
TestSupport.setPrivateConnection(agent, privilegeFailingDdlConnection(sql,
|
||||
resultSet(
|
||||
new String[]{"INDEX_NAME", "COLUMN_NAME", "COLUMN_POSITION", "UNIQUENESS", "CONSTRAINT_TYPE", "INDEX_TYPE"},
|
||||
new Object[][]{}
|
||||
new String[]{"DDL"},
|
||||
new Object[][]{{"CREATE TABLE \"USERS\" (\"ID\" NUMBER PRIMARY KEY, \"NAME\" VARCHAR2(100)) PARTITION BY RANGE(\"ID\") (PARTITION P1 VALUES LESS THAN(MAXVALUE))"}}
|
||||
),
|
||||
resultSet(
|
||||
new String[]{"CONSTRAINT_NAME", "COLUMN_NAME", "TABLE_NAME", "REF_COLUMN_NAME"},
|
||||
new String[]{"INDEX_NAME"},
|
||||
new Object[][]{}
|
||||
),
|
||||
resultSet(
|
||||
@@ -642,6 +642,59 @@ class OceanBaseOracleAgentTest {
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void tableDdlPreservesPartitionsAndNativeLocalIndexes() {
|
||||
var agent = new OceanBaseOracleAgent();
|
||||
String nativeTable = "CREATE TABLE \"T\" (\"ID\" NUMBER, CONSTRAINT \"CK\" CHECK (\"ID\" > 0)) "
|
||||
+ "REPLICA_NUM = 1 PARTITION BY RANGE(\"ID\") (PARTITION P1 VALUES LESS THAN(MAXVALUE))";
|
||||
String nativeIndex = "CREATE INDEX \"APP\".\"IX\" ON \"APP\".\"T\"(\"ID\") LOCAL;";
|
||||
TestSupport.setPrivateConnection(agent, preparedConnection(new ArrayList<>(),
|
||||
resultSet(new String[]{"DDL"}, new Object[][]{{nativeTable}}),
|
||||
resultSet(new String[]{"INDEX_NAME"}, new Object[][]{{"IX"}}),
|
||||
resultSet(new String[]{"DDL"}, new Object[][]{{nativeIndex}}),
|
||||
resultSet(new String[]{"COMMENTS"}, new Object[][]{{"Owner's table"}}),
|
||||
resultSet(new String[]{"COLUMN_NAME", "COMMENTS"}, new Object[][]{}),
|
||||
resultSet(new String[]{"GRANTEE", "PRIVILEGE", "GRANTABLE"}, new Object[][]{}),
|
||||
resultSet(new String[]{"GRANTEE", "COLUMN_NAME", "PRIVILEGE", "GRANTABLE"}, new Object[][]{})
|
||||
));
|
||||
String ddl = agent.getTableDdl("APP", "T");
|
||||
Assertions.assertTrue(ddl.startsWith(nativeTable.replace("CREATE TABLE \"T\"", "CREATE TABLE \"APP\".\"T\"") + ";"), ddl);
|
||||
Assertions.assertTrue(ddl.contains(nativeIndex), ddl);
|
||||
Assertions.assertTrue(ddl.contains("COMMENT ON TABLE \"APP\".\"T\" IS 'Owner''s table';"), ddl);
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyMetadataDdlFailsInsteadOfReturningAnIncompleteTable() {
|
||||
var agent = new OceanBaseOracleAgent();
|
||||
TestSupport.setPrivateConnection(agent, preparedConnection(new ArrayList<>(),
|
||||
resultSet(new String[]{"DDL"}, new Object[][]{{" "}})));
|
||||
Assertions.assertThrows(RuntimeException.class, () -> agent.getTableDdl("APP", "T"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void partitionsPreserveDictionaryOrderCompositeKeysAndNullHashBounds() {
|
||||
List<String> sql = new ArrayList<>();
|
||||
var agent = new OceanBaseOracleAgent();
|
||||
TestSupport.setPrivateConnection(agent, preparedConnection(sql,
|
||||
resultSet(new String[]{"COLUMN_NAME"}, new Object[][]{{"ID"}, {"A\"B"}}),
|
||||
resultSet(new String[]{"NAME", "POSITION", "HIGH_VALUE", "PARTITION_TYPE"}, new Object[][]{
|
||||
{"P1", 1, "100, 'East'", "RANGE"}, {"PM", 2, "MAXVALUE, MAXVALUE", "RANGE"}}),
|
||||
resultSet(new String[]{"COLUMN_NAME"}, new Object[][]{{"REGION"}}),
|
||||
resultSet(new String[]{"NAME", "POSITION", "HIGH_VALUE", "PARTITION_TYPE"}, new Object[][]{
|
||||
{"S1", 1, null, "HASH"}})
|
||||
));
|
||||
var partitions = agent.listPartitions("APP", "T");
|
||||
Assertions.assertEquals(List.of("P1", "PM"), partitions.stream().map(com.dbx.agent.PartitionInfo::name).toList());
|
||||
Assertions.assertEquals("\"ID\", \"A\"\"B\"", partitions.get(0).partition_key());
|
||||
Assertions.assertEquals("100, 'East'", partitions.get(0).value());
|
||||
var subpartitions = agent.listSubpartitions("APP", "T");
|
||||
Assertions.assertEquals("", subpartitions.get(0).value());
|
||||
Assertions.assertEquals("HASH", subpartitions.get(0).partition_type());
|
||||
Assertions.assertTrue(sql.get(0).contains("ORDER BY COLUMN_POSITION"));
|
||||
Assertions.assertTrue(sql.get(1).contains("WHERE p.TABLE_OWNER = ? AND p.TABLE_NAME = ?"));
|
||||
Assertions.assertTrue(sql.get(3).contains("ALL_TAB_SUBPARTITIONS"));
|
||||
}
|
||||
|
||||
private static ResultSet columnResultSet(Object[][] rows) {
|
||||
return resultSet(
|
||||
new String[]{
|
||||
|
||||
Reference in new issue
Block a user