JWuCines commented on code in PR #19698:
URL: https://github.com/apache/druid/pull/19698#discussion_r3602925286
##########
server/src/test/java/org/apache/druid/metadata/SQLMetadataConnectorTest.java:
##########
@@ -463,6 +468,219 @@
);
}
+ @Test
+ public void testExportTable() throws IOException
+ {
+ final String tableName = "test_export";
+ connector.getDBI().withHandle(
+ handle -> {
+ handle.execute(
+ StringUtils.format(
+ "CREATE TABLE %s (name VARCHAR(255) NOT NULL, payload BLOB
NOT NULL, active BOOLEAN NOT NULL, PRIMARY KEY(name))",
+ tableName
+ )
+ );
+ handle.execute(
+ StringUtils.format("INSERT INTO %s VALUES (?, ?, ?)", tableName),
+ "key1",
+ StringUtils.toUtf8("{\"type\":\"test\"}"),
+ true
+ );
+ handle.execute(
+ StringUtils.format("INSERT INTO %s VALUES (?, ?, ?)", tableName),
+ "key2",
+ StringUtils.toUtf8("{\"value\":42}"),
+ false
+ );
+ return null;
+ }
+ );
+
+ final File outputFile = File.createTempFile("export_test", ".csv");
+ outputFile.deleteOnExit();
+
+ // Call the base class exportTable (the generic JDBC path used by
PostgreSQL)
+ // rather than DerbyConnector's native SYSCS_EXPORT_TABLE override
+ connector.exportTableGeneric(
+ StringUtils.toUpperCase(tableName),
Review Comment:
[Use Files.createTempFile instead of File.createTempFile for owner-only
permissions](https://github.com/apache/druid/pull/19698/changes/c2d66e690e011db4ed9c3017c5b4cabd950779f4)
##########
server/src/test/java/org/apache/druid/metadata/SQLMetadataConnectorTest.java:
##########
@@ -463,6 +468,219 @@
);
}
+ @Test
+ public void testExportTable() throws IOException
+ {
+ final String tableName = "test_export";
+ connector.getDBI().withHandle(
+ handle -> {
+ handle.execute(
+ StringUtils.format(
+ "CREATE TABLE %s (name VARCHAR(255) NOT NULL, payload BLOB
NOT NULL, active BOOLEAN NOT NULL, PRIMARY KEY(name))",
+ tableName
+ )
+ );
+ handle.execute(
+ StringUtils.format("INSERT INTO %s VALUES (?, ?, ?)", tableName),
+ "key1",
+ StringUtils.toUtf8("{\"type\":\"test\"}"),
+ true
+ );
+ handle.execute(
+ StringUtils.format("INSERT INTO %s VALUES (?, ?, ?)", tableName),
+ "key2",
+ StringUtils.toUtf8("{\"value\":42}"),
+ false
+ );
+ return null;
+ }
+ );
+
+ final File outputFile = File.createTempFile("export_test", ".csv");
+ outputFile.deleteOnExit();
+
+ // Call the base class exportTable (the generic JDBC path used by
PostgreSQL)
+ // rather than DerbyConnector's native SYSCS_EXPORT_TABLE override
+ connector.exportTableGeneric(
+ StringUtils.toUpperCase(tableName),
+ outputFile.getAbsolutePath()
+ );
+
+ final List<String> lines = Files.readAllLines(outputFile.toPath(),
StandardCharsets.UTF_8);
+ Assert.assertEquals(2, lines.size());
+ Collections.sort(lines);
+
+ // Verify rows (sorted by name): hex-encoded payload, boolean as string
+ final String expectedHex1 =
BaseEncoding.base16().encode(StringUtils.toUtf8("{\"type\":\"test\"}"));
+ Assert.assertEquals("key1," + expectedHex1 + ",true", lines.get(0));
+
+ final String expectedHex2 =
BaseEncoding.base16().encode(StringUtils.toUtf8("{\"value\":42}"));
+ Assert.assertEquals("key2," + expectedHex2 + ",false", lines.get(1));
+
+ dropTable(tableName);
+ }
+
+ @Test
+ public void testExportTableWithSpecialCharacters() throws IOException
+ {
+ final String tableName = "test_export_special";
+ connector.getDBI().withHandle(
+ handle -> {
+ handle.execute(
+ StringUtils.format(
+ "CREATE TABLE %s (name VARCHAR(255) NOT NULL, description
VARCHAR(1024), PRIMARY KEY(name))",
+ tableName
+ )
+ );
+ handle.execute(
+ StringUtils.format("INSERT INTO %s VALUES (?, ?)", tableName),
+ "commas",
+ "value,with,commas"
+ );
+ handle.execute(
+ StringUtils.format("INSERT INTO %s VALUES (?, ?)", tableName),
+ "quotes",
+ "value\"with\"quotes"
+ );
+ handle.execute(
+ StringUtils.format("INSERT INTO %s VALUES (?, ?)", tableName),
+ "simple",
+ "plain_value"
+ );
+ return null;
+ }
+ );
+
+ final File outputFile = File.createTempFile("export_special_test", ".csv");
+ outputFile.deleteOnExit();
+
+ connector.exportTableGeneric(
+ StringUtils.toUpperCase(tableName),
+ outputFile.getAbsolutePath()
+ );
Review Comment:
[Use Files.createTempFile instead of File.createTempFile for owner-only
permissions](https://github.com/apache/druid/pull/19698/changes/c2d66e690e011db4ed9c3017c5b4cabd950779f4)
--
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]