diff --git a/agents/drivers/dameng/src/main/java/com/dbx/agent/dameng/DamengAgent.java b/agents/drivers/dameng/src/main/java/com/dbx/agent/dameng/DamengAgent.java index 38279363eb..22a449ed17 100644 --- a/agents/drivers/dameng/src/main/java/com/dbx/agent/dameng/DamengAgent.java +++ b/agents/drivers/dameng/src/main/java/com/dbx/agent/dameng/DamengAgent.java @@ -68,6 +68,10 @@ public final class DamengAgent extends AbstractJdbcAgent { private static final int DBMS_OUTPUT_ENABLE_TIMEOUT_SECS = 5; private static final int DBMS_OUTPUT_ENABLE_NETWORK_TIMEOUT_MILLIS = 5_000; private static final Pattern DATABASE_VERSION_MAJOR_PATTERN = Pattern.compile("(\\d+)\\."); + // Word-boundary match so a type or default merely containing the letters is not mistaken + // for the IDENTITY keyword. + private static final Pattern DDL_IDENTITY_KEYWORD_PATTERN = + Pattern.compile("(?i)(? identityColumns = identityColumns(schema, table); List result = new ArrayList<>(); @@ -1794,6 +1805,23 @@ public final class DamengAgent extends AbstractJdbcAgent { } private Set identityColumns(String schema, String table) { + try { + return identityColumnsFromSystemCatalog(schema, table); + } catch (Exception systemCatalogError) { + // SYS.SYSCOLUMNS is not granted to PUBLIC, so ordinary (non-DBA) accounts fail here + // with a permission error. Reporting that as an empty set is indistinguishable from + // "this table has no identity column", and the data grid then sends the identity + // value on INSERT — which the server rejects, while leaving it out trips the NOT NULL + // check. Fall back to the table DDL, which the table owner can always read. + // + // Any failure triggers the fallback, not just a permission error: the server message + // is localized, so matching on its text would silently stop working on a non-Chinese + // server, and every other failure mode wants the same fallback anyway. + return identityColumnsFromTableDdl(schema, table); + } + } + + private Set identityColumnsFromSystemCatalog(String schema, String table) throws Exception { Set result = new java.util.HashSet<>(); String sql = """ SELECT /*+ PARALLEL(1) */ c.NAME @@ -1813,8 +1841,177 @@ public final class DamengAgent extends AbstractJdbcAgent { } } } + } + return result; + } + + private Set identityColumnsFromTableDdl(String schema, String table) { + String ddl = null; + String sql = "SELECT /*+ PARALLEL(1) */ DBMS_METADATA.GET_DDL(?, ?, ?) FROM DUAL"; + try (PreparedStatement stmt = requireConnected().prepareStatement(sql)) { + stmt.setString(1, "TABLE"); + stmt.setString(2, table); + stmt.setString(3, schema); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + ddl = readTextColumn(rs, 1); + } + } } catch (Exception ignored) { - // Some Dameng versions or users do not expose SYS.SYSCOLUMNS. + // DBMS_METADATA is unavailable on some deployments (and on views); identity + // information stays unknown, which is the best this path can do. + } + return identityColumnsFromDdlText(ddl); + } + + /** + * Reads the identity columns out of a {@code CREATE TABLE} statement, e.g. + * {@code "ID" INT IDENTITY(1, 1) NOT NULL}. Quoted names and string defaults are skipped so a + * column called {@code "IDENTITY"} or a default of {@code 'IDENTITY(1,1)'} is not misread. + */ + static Set identityColumnsFromDdlText(String ddl) { + Set result = new java.util.HashSet<>(); + for (String definition : splitTableDdlDefinitions(ddl)) { + String trimmed = definition.trim(); + if (trimmed.isEmpty() || trimmed.charAt(0) != '"') { + // Table-level constraint (PRIMARY KEY(...), CHECK(...)), not a column definition. + continue; + } + StringBuilder name = new StringBuilder(); + int index = 1; + while (index < trimmed.length()) { + char ch = trimmed.charAt(index); + if (ch == '"') { + if (index + 1 < trimmed.length() && trimmed.charAt(index + 1) == '"') { + name.append('"'); + index += 2; + continue; + } + break; + } + name.append(ch); + index++; + } + if (index >= trimmed.length() || name.length() == 0) { + continue; + } + String rest = stripSingleQuotedLiterals(trimmed.substring(index + 1)); + if (DDL_IDENTITY_KEYWORD_PATTERN.matcher(rest).find()) { + result.add(name.toString()); + } + } + return result; + } + + /** + * Splits the column list of a {@code CREATE TABLE} statement on its top-level commas, so that + * {@code IDENTITY(1, 1)} and {@code VARCHAR(64)} stay attached to their column. + */ + private static List splitTableDdlDefinitions(String ddl) { + List definitions = new ArrayList<>(); + if (ddl == null) { + return definitions; + } + StringBuilder current = new StringBuilder(); + int depth = 0; + boolean inLiteral = false; + boolean inQuotedName = false; + for (int i = 0; i < ddl.length(); i++) { + char ch = ddl.charAt(i); + if (inLiteral) { + current.append(ch); + if (ch == '\'') { + inLiteral = false; + } + continue; + } + if (inQuotedName) { + current.append(ch); + if (ch == '"') { + inQuotedName = false; + } + continue; + } + if (ch == '\'') { + inLiteral = true; + current.append(ch); + continue; + } + if (ch == '"') { + inQuotedName = true; + current.append(ch); + continue; + } + if (ch == '(') { + depth++; + if (depth == 1) { + // Opening paren of the column list; drop the CREATE TABLE prefix before it. + current.setLength(0); + } else { + current.append(ch); + } + continue; + } + if (ch == ')') { + depth--; + if (depth == 0) { + addTableDdlDefinition(definitions, current); + break; + } + current.append(ch); + continue; + } + if (ch == ',' && depth == 1) { + addTableDdlDefinition(definitions, current); + continue; + } + if (depth > 0) { + current.append(ch); + } + } + return definitions; + } + + private static void addTableDdlDefinition(List definitions, StringBuilder current) { + String definition = current.toString().trim(); + current.setLength(0); + if (!definition.isEmpty()) { + definitions.add(definition); + } + } + + private static String stripSingleQuotedLiterals(String text) { + StringBuilder out = new StringBuilder(text.length()); + boolean inLiteral = false; + for (int i = 0; i < text.length(); i++) { + char ch = text.charAt(i); + if (inLiteral) { + if (ch == '\'') { + inLiteral = false; + } + continue; + } + if (ch == '\'') { + inLiteral = true; + out.append(' '); + continue; + } + out.append(ch); + } + return out.toString(); + } + + private Set primaryKeyColumnsFromJdbcMetadata(String schema, String table) { + Set result = new java.util.HashSet<>(); + try (ResultSet rs = requireConnected().getMetaData().getPrimaryKeys(null, schema, table)) { + while (rs.next()) { + String column = rs.getString("COLUMN_NAME"); + if (notBlank(column)) { + result.add(column); + } + } + } catch (Exception ignored) { + // The table is then reported without a primary key, as before this fallback existed. } return result; } diff --git a/agents/drivers/dameng/src/test/java/com/dbx/agent/dameng/DamengAgentMetadataTest.java b/agents/drivers/dameng/src/test/java/com/dbx/agent/dameng/DamengAgentMetadataTest.java index 0a97b6b519..7f7e3084d4 100644 --- a/agents/drivers/dameng/src/test/java/com/dbx/agent/dameng/DamengAgentMetadataTest.java +++ b/agents/drivers/dameng/src/test/java/com/dbx/agent/dameng/DamengAgentMetadataTest.java @@ -1118,6 +1118,148 @@ class DamengAgentMetadataTest { Assertions.assertEquals("identity", columns.get(0).getExtra()); } + @Test + void readsIdentityColumnsFromDdlText() { + Assertions.assertEquals( + java.util.Set.of("ID"), + DamengAgent.identityColumnsFromDdlText( + "CREATE TABLE \"DBX_TEST\".\"T\"\n(\n\"ID\" INT IDENTITY(1, 1) NOT NULL,\n" + + "\"NAME\" VARCHAR(64),\nNOT CLUSTER PRIMARY KEY(\"ID\")) STORAGE(ON \"MAIN\") ;" + ) + ); + } + + @Test + void ignoresIdentityLookalikesInDdlText() { + // A column *named* IDENTITY whose default merely contains the word must not be reported, + // and neither must a table-level constraint that mentions it. + Assertions.assertEquals( + java.util.Set.of(), + DamengAgent.identityColumnsFromDdlText( + "CREATE TABLE \"DBX_TEST\".\"T\"\n(\n\"IDENTITY\" VARCHAR(32) DEFAULT 'IDENTITY(1,1)',\n" + + "\"PLAIN\" INT,\n\"IDENTITY_KIND\" VARCHAR(8)) STORAGE(ON \"MAIN\") ;" + ) + ); + } + + @Test + void readsEveryIdentityColumnFromDdlText() { + Assertions.assertEquals( + java.util.Set.of("A", "C"), + DamengAgent.identityColumnsFromDdlText( + "CREATE TABLE \"S\".\"T\"\n(\n\"A\" BIGINT IDENTITY(1, 1),\n\"B\" VARCHAR(9),\n" + + "\"C\" INT identity(100, 5) NOT NULL,\nCLUSTER PRIMARY KEY(\"A\", \"C\")) ;" + ) + ); + } + + @Test + void toleratesDdlTextThatCannotBeParsed() { + Assertions.assertEquals(java.util.Set.of(), DamengAgent.identityColumnsFromDdlText(null)); + Assertions.assertEquals(java.util.Set.of(), DamengAgent.identityColumnsFromDdlText("")); + Assertions.assertEquals(java.util.Set.of(), DamengAgent.identityColumnsFromDdlText("CREATE TABLE \"S\".\"T\"")); + } + + @Test + void fallsBackToTableDdlWhenSysColumnsIsNotReadable() { + // Ordinary Dameng accounts have no SELECT on SYS.SYSCOLUMNS. Without the fallback the + // identity column looks like a plain NOT NULL column and the grid sends its value. + List sqls = new ArrayList<>(); + DamengAgent agent = new DamengAgent(); + TestSupport.setPrivateConnection(agent, restrictedAccountConnection(sqls, List.of())); + + List columns = agent.getColumns("APP", "USERS"); + + Assertions.assertEquals(1, columns.size()); + Assertions.assertEquals("identity", columns.get(0).getExtra()); + Assertions.assertTrue( + sqls.stream().anyMatch(sql -> sql.contains("DBMS_METADATA.GET_DDL")), + sqls.toString() + ); + } + + @Test + void fallsBackToJdbcPrimaryKeysWhenConstraintDictionaryIsEmpty() { + // ALL_CONS_COLUMNS yields no rows (not an error) for those same accounts. + DamengAgent agent = new DamengAgent(); + TestSupport.setPrivateConnection(agent, restrictedAccountConnection(new ArrayList<>(), List.of())); + + List columns = agent.getColumns("APP", "USERS"); + + Assertions.assertEquals(1, columns.size()); + Assertions.assertTrue(columns.get(0).getIs_primary_key()); + } + + @Test + void keepsPrimaryKeysFromConstraintDictionaryWhenReadable() { + // The JDBC fallback must not displace the dictionary answer when that one works. + DamengAgent agent = new DamengAgent(); + TestSupport.setPrivateConnection(agent, restrictedAccountConnection(new ArrayList<>(), List.of(List.of("ID")))); + + List columns = agent.getColumns("APP", "USERS"); + + Assertions.assertTrue(columns.get(0).getIs_primary_key()); + } + + @Test + void doesNotReadTableDdlWhenSysColumnsIsReadableButEmpty() { + // A readable-but-empty SYS.SYSCOLUMNS means "no identity column" and must stay cheap. + List sqls = new ArrayList<>(); + DamengAgent agent = new DamengAgent(); + TestSupport.setPrivateConnection(agent, metadataConnection(null, null, false, List.of(), sqls)); + + agent.getColumns("APP", "USERS"); + + Assertions.assertTrue( + sqls.stream().noneMatch(sql -> sql.contains("DBMS_METADATA.GET_DDL")), + sqls.toString() + ); + } + + /** + * A connection shaped like a non-DBA Dameng account: SYS.SYSCOLUMNS raises a permission error + * and the constraint dictionary answers with no rows, while the table DDL and the driver's own + * primary-key metadata both still work. + */ + private static Connection restrictedAccountConnection(List sqls, List> constraintRows) { + DatabaseMetaData metadata = proxy(DatabaseMetaData.class, (method, args) -> { + if ("getPrimaryKeys".equals(method.getName())) { + return metadataResultSet(List.of(List.of("ID"))); + } + return defaultValue(method.getReturnType()); + }); + return proxy(Connection.class, (method, args) -> { + String name = method.getName(); + if ("getMetaData".equals(name)) { + return metadata; + } + if ("prepareStatement".equals(name)) { + String sql = (String) args[0]; + sqls.add(sql); + if (sql.contains("SYS.SYSCOLUMNS")) { + return failingMetadataStatement("没有[SYSCOLUMNS]对象的查询权限"); + } + if (sql.contains("DBMS_METADATA.GET_DDL")) { + return metadataStatement(List.of(List.of( + "CREATE TABLE \"APP\".\"USERS\"\n(\n\"ID\" INT IDENTITY(1, 1) NOT NULL)" + + " STORAGE(ON \"MAIN\", CLUSTERBTR) ;" + ))); + } + if (sql.contains("ALL_CONS_COLUMNS")) { + return metadataStatement(constraintRows); + } + if (sql.contains("ALL_TAB_COLUMNS")) { + return metadataStatement(defaultColumnMetadataRows("id comment")); + } + return metadataStatement(List.of()); + } + if ("close".equals(name) || "isClosed".equals(name)) { + return "isClosed".equals(name) ? Boolean.FALSE : null; + } + return defaultValue(method.getReturnType()); + }); + } + @Test void preservesCharacterLengthUnitsFromMetadata() { DamengAgent agent = new DamengAgent();