JWuCines commented on code in PR #19698:
URL: https://github.com/apache/druid/pull/19698#discussion_r3614617914


##########
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();

Review Comment:
   Fixed in 
https://github.com/apache/druid/pull/19698/changes/6b73b7ee540578f9412a74d37c74f934f264b978



##########
services/src/main/java/org/apache/druid/cli/ExportMetadata.java:
##########
@@ -245,12 +245,18 @@ private void exportTable(
     } else {
       pathFormatString = "%s/%s.csv";
     }
+    final String exportTableName = isDerby() ? 
StringUtils.toUpperCase(tableName) : tableName;
     dbConnector.exportTable(
-        StringUtils.toUpperCase(tableName),
+        exportTableName,

Review Comment:
   Fixed in 
https://github.com/apache/druid/pull/19698/changes/6b73b7ee540578f9412a74d37c74f934f264b978



##########
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:
   Fixed in 
https://github.com/apache/druid/pull/19698/changes/6b73b7ee540578f9412a74d37c74f934f264b978



-- 
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]

Reply via email to