Copilot commented on code in PR #16252:
URL: https://github.com/apache/iotdb/pull/16252#discussion_r2300234791
##########
iotdb-client/cli/src/main/java/org/apache/iotdb/tool/data/ExportDataTree.java:
##########
@@ -264,47 +278,52 @@ private static Boolean exportToTsFile(SessionDataSet
sessionDataSet, String file
private void exportToCsvFile(SessionDataSet sessionDataSet, String filePath)
throws IOException, IoTDBConnectionException,
StatementExecutionException {
+ processedRows = 0;
+ lastPrintTime = 0;
List<String> headers = sessionDataSet.getColumnNames();
int fileIndex = 0;
- boolean hasNext = sessionDataSet.hasNext();
- while (hasNext) {
+ SessionDataSet.DataIterator iterator = sessionDataSet.iterator();
+ List<String> columnTypeList = iterator.getColumnTypeList();
+ int totalColumns = columnTypeList.size();
+ boolean fromOuterloop = false;
+ while (iterator.next()) {
final String finalFilePath = filePath + "_" + fileIndex + ".csv";
CSVPrinterWrapper csvPrinterWrapper = new
CSVPrinterWrapper(finalFilePath);
- csvPrinterWrapper.printRecord(headers);
- int i = 0;
- while (i++ < linesPerFile && hasNext) {
- RowRecord rowRecord = sessionDataSet.next();
- if (rowRecord.getTimestamp() != 0) {
- csvPrinterWrapper.print(timeTrans(rowRecord.getTimestamp()));
- }
- rowRecord
- .getFields()
- .forEach(
- field -> {
- String fieldStringValue = field.getStringValue();
- if (!"null".equals(field.getStringValue())) {
- if ((field.getDataType() == TSDataType.TEXT
- || field.getDataType() == TSDataType.STRING)
- && !fieldStringValue.startsWith("root.")) {
- fieldStringValue = "\"" + fieldStringValue + "\"";
- }
- csvPrinterWrapper.print(fieldStringValue);
- } else {
- csvPrinterWrapper.print("");
- }
- });
- csvPrinterWrapper.println();
- hasNext = sessionDataSet.hasNext();
- if (!hasNext) {
- break;
+ try {
+ csvPrinterWrapper.printRecord(headers);
+ fromOuterloop = true;
+ int i = 0;
+ while (i++ < linesPerFile && (fromOuterloop || iterator.next())) {
+ fromOuterloop = false;
+ csvPrinterWrapper.print(timeTrans(iterator.getLong(1)));
+ for (int curColumnIndex = 1; curColumnIndex < totalColumns;
curColumnIndex++) {
+ String columnValue = iterator.getString(curColumnIndex + 1);
+ if (columnValue.equals("null")) {
+ csvPrinterWrapper.print("");
+ } else {
+ String curType = columnTypeList.get(curColumnIndex);
+ if ((curType.equalsIgnoreCase("TEXT") ||
curType.equalsIgnoreCase("STRING"))
+ && !columnValue.startsWith("root.")) {
+ csvPrinterWrapper.print("\"" + columnValue + "\"");
+ } else {
+ csvPrinterWrapper.print(columnValue);
+ }
+ }
+ }
+ csvPrinterWrapper.println();
+ processedRows += 1;
+ if (System.currentTimeMillis() - lastPrintTime > updateTimeInterval)
{
+ ioTPrinter.printf(Constants.PROCESSED_PROGRESS, processedRows);
+ lastPrintTime = System.currentTimeMillis();
+ }
}
+ fileIndex++;
+ csvPrinterWrapper.flush();
+ } finally {
+ csvPrinterWrapper.close();
Review Comment:
csvPrinterWrapper.close() is called in the finally block, but
csvPrinterWrapper.flush() is called outside the try-finally block. If an
exception occurs between flush() and close(), the close() method may not be
called, leading to resource leaks.
##########
iotdb-client/cli/src/main/java/org/apache/iotdb/tool/data/ExportDataTree.java:
##########
@@ -141,21 +156,24 @@ private void exportToSqlFile(SessionDataSet
sessionDataSet, String filePath)
}
}
}
- boolean hasNext = sessionDataSet.hasNext();
- while (hasNext) {
+ SessionDataSet.DataIterator iterator = sessionDataSet.iterator();
+ List<String> columnTypeList = iterator.getColumnTypeList();
+ int totalColumns = columnTypeList.size();
+ boolean fromOuterloop = false;
+ while (iterator.next()) {
+ fromOuterloop = true;
final String finalFilePath = filePath + "_" + fileIndex + ".sql";
try (FileWriter writer = new FileWriter(finalFilePath)) {
if (writeNull) {
break;
}
int i = 0;
- while (i++ < linesPerFile && hasNext) {
- RowRecord rowRecord = sessionDataSet.next();
- List<Field> fields = rowRecord.getFields();
+ while (i++ < linesPerFile && (fromOuterloop || iterator.next())) {
+ fromOuterloop = false;
List<String> headersTemp = new ArrayList<>(seriesList);
List<String> timeseries = new ArrayList<>();
if (headers.contains("Device")) {
- deviceName = fields.get(0).toString();
+ deviceName = iterator.getString(2);
Review Comment:
The deviceName is extracted from column index 2, but this assumes the
'Device' column is always at index 1 (0-based). This hardcoded index could
cause issues if the column order changes or if the Device column is at a
different position.
```suggestion
int deviceIndex = headers.indexOf("Device");
deviceName = iterator.getString(deviceIndex);
```
##########
iotdb-client/cli/src/main/java/org/apache/iotdb/tool/data/ExportDataTree.java:
##########
@@ -169,33 +187,31 @@ private void exportToSqlFile(SessionDataSet
sessionDataSet, String filePath)
timeseries.addAll(headers);
timeseries.remove(0);
}
+ long timestamp = iterator.getLong(1);
String sqlMiddle =
Boolean.TRUE.equals(aligned)
- ? " ALIGNED VALUES (" + rowRecord.getTimestamp() + ","
- : " VALUES (" + rowRecord.getTimestamp() + ",";
+ ? " ALIGNED VALUES (" + timestamp + ","
+ : " VALUES (" + timestamp + ",";
List<String> values = new ArrayList<>();
- if (headers.contains("Device")) {
- fields.remove(0);
- }
- for (int index = 0; index < fields.size(); index++) {
- RowRecord next =
- session
- .executeQueryStatement("SHOW TIMESERIES " +
timeseries.get(index), timeout)
- .next();
- if (ObjectUtils.isNotEmpty(next)) {
- List<Field> timeseriesList = next.getFields();
- String value = fields.get(index).toString();
+ int startIndex = headers.contains("Device") ? 2 : 1;
+ for (int index = startIndex; index < totalColumns; index++) {
+ SessionDataSet sessionDataSet2 =
+ session.executeQueryStatement(
+ "SHOW TIMESERIES " + timeseries.get(index - startIndex),
timeout);
+ SessionDataSet.DataIterator iterator2 = sessionDataSet2.iterator();
+ if (iterator2.next()) {
+ String value = iterator.getString(index + 1);
Review Comment:
The getString call uses 'index + 1' but this may not correctly map to the
iterator's column positions. The index calculation should account for the
actual column structure and whether the Device column is present.
```suggestion
String value = iterator.getString(index);
```
--
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]