Skip to content

Commit

Permalink
Fix pipeline e2e tests for opengauss
Browse files Browse the repository at this point in the history
  • Loading branch information
menghaoranss committed Jan 2, 2025
1 parent a6a09ba commit 3b1e8d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,23 @@ private Optional<String> decorateOpenGauss(final String databaseName, final Stri
if (queryContext.toLowerCase().startsWith(SET_SEARCH_PATH_PREFIX)) {
return Optional.empty();
}
return Optional.of(replaceTableNameWithPrefix(queryContext, schemaName + ".", databaseName, parserEngine));
return Optional.of(replaceTableNameWithPrefix(queryContext, schemaName, databaseName, parserEngine));
}

private String replaceTableNameWithPrefix(final String sql, final String prefix, final String databaseName, final SQLParserEngine parserEngine) {
private String replaceTableNameWithPrefix(final String sql, final String schemaName, final String databaseName, final SQLParserEngine parserEngine) {
SQLStatementContext sqlStatementContext = parseSQL(databaseName, parserEngine, sql);
if (sqlStatementContext instanceof CreateTableStatementContext || sqlStatementContext instanceof CommentStatementContext
|| sqlStatementContext instanceof CreateIndexStatementContext || sqlStatementContext instanceof AlterTableStatementContext) {
if (((TableAvailable) sqlStatementContext).getTablesContext().getSimpleTables().isEmpty()) {
return sql;
}
if (((TableAvailable) sqlStatementContext).getTablesContext().getSchemaName().isPresent()) {
Optional<String> sqlSchemaName = ((TableAvailable) sqlStatementContext).getTablesContext().getSchemaName();
if (sqlSchemaName.isPresent() && sqlSchemaName.get().equals(schemaName)) {
return sql;
}
Map<SQLSegment, String> replaceMap = new TreeMap<>(Comparator.comparing(SQLSegment::getStartIndex));
TableNameSegment tableNameSegment = ((TableAvailable) sqlStatementContext).getTablesContext().getSimpleTables().iterator().next().getTableName();
replaceMap.put(tableNameSegment, prefix + tableNameSegment.getIdentifier().getValue());
replaceMap.put(tableNameSegment, schemaName + "." + tableNameSegment.getIdentifier().getValue());
return doDecorateActualTable(replaceMap, sql);
}
return sql;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ public Collection<String> buildCreateTableSQLs(final DataSource dataSource, fina
ResultSet resultSet = statement.executeQuery(String.format("SELECT * FROM pg_get_tabledef('%s.%s')", schemaName, tableName))) {
if (resultSet.next()) {
// TODO use ";" to split is not always correct if return value's comments contains ";"
return Arrays.asList(resultSet.getString("pg_get_tabledef").split(";"));
Collection<String> defSQLs = Arrays.asList(resultSet.getString("pg_get_tabledef").split(";"));
return defSQLs.stream().map(sql -> {
String targetPrefix = "ALTER TABLE " + tableName;
if (sql.trim().startsWith(targetPrefix)) {
return sql.replaceFirst(targetPrefix, "ALTER TABLE " + schemaName + "." + tableName);
}
return sql;
}).collect(Collectors.toList());
}
}
throw new CreateTableSQLGenerateException(tableName);
Expand Down

0 comments on commit 3b1e8d3

Please sign in to comment.