FrankChen021 commented on code in PR #19698:
URL: https://github.com/apache/druid/pull/19698#discussion_r3622008086
##########
server/src/main/java/org/apache/druid/metadata/SQLMetadataConnector.java:
##########
@@ -1038,6 +1045,73 @@ public void createAuditTable()
}
}
+ @Override
+ public void exportTable(
+ final String tableName,
+ final String outputPath
+ )
+ {
+ exportTableWithJdbc(tableName, outputPath);
+ }
+
+ /**
+ * Exports a table to a CSV file using generic JDBC.
+ * Binary columns are hex-encoded and booleans are written as true/false
strings.
+ * Subclasses may override {@link #exportTable} with a database-specific
implementation
+ * while this method remains available for testing or fallback.
+ */
+ protected void exportTableWithJdbc(
+ final String tableName,
+ final String outputPath
+ )
+ {
+ retryWithHandle(
+ (HandleCallback<Void>) handle -> {
+ try (Statement stmt = handle.getConnection().createStatement();
+ ResultSet rs = stmt.executeQuery(StringUtils.format("SELECT
* FROM %s", tableName));
+ FileOutputStream fos = new FileOutputStream(outputPath);
+ OutputStreamWriter writer = new OutputStreamWriter(fos,
StandardCharsets.UTF_8)) {
+ final ResultSetMetaData meta = rs.getMetaData();
+ final int columnCount = meta.getColumnCount();
+ while (rs.next()) {
+ for (int i = 1; i <= columnCount; i++) {
+ if (i > 1) {
+ writer.write(',');
+ }
+ final int colType = meta.getColumnType(i);
+ if (colType == Types.BINARY || colType == Types.VARBINARY
+ || colType == Types.LONGVARBINARY || colType ==
Types.BLOB
+ || (colType == Types.OTHER &&
"bytea".equalsIgnoreCase(meta.getColumnTypeName(i)))) {
+ final byte[] bytes = rs.getBytes(i);
+ if (bytes != null) {
+ writer.write(BaseEncoding.base16().encode(bytes));
+ }
+ } else if (colType == Types.BOOLEAN || colType ==
Types.BIT) {
+ final boolean val = rs.getBoolean(i);
+ if (!rs.wasNull()) {
+ writer.write(String.valueOf(val));
+ }
+ } else {
+ final String val = rs.getString(i);
+ if (val != null) {
+ if (val.contains(",") || val.contains("\"") ||
val.contains("\n") || val.contains("\r")) {
Review Comment:
The comma/quote round-trip is fixed, but multiline fields are still split
because each rewrite uses `BufferedReader.readLine()` plus
`CSVParser.parseLine()` on one physical line. `exportTableWithJdbc` writes
quoted newline or carriage-return values as CSV records spanning lines, so the
rewrite misparses them before `csvEscapeField` runs. Please use a record-aware
`CSVReader`/`readNext` path and cover this end to end. Reviewed 8 of 8 changed
files.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]