Caideyipi commented on code in PR #18828:
URL: https://github.com/apache/iotdb/pull/18828#discussion_r4333877605


##########
iotdb-client/cli/src/main/java/org/apache/iotdb/tool/data/AbstractImportData.java:
##########
@@ -65,15 +97,20 @@ public void run() {
         }
       }
     } catch (Exception e) {
+      importFailed.set(true);
+      loadFileFailedNum.increment();
       ioTPrinter.println(
-          String.format("[%s] - Unexpected error occurred: %s", 
file.getName(), e.getMessage()));
+          String.format(
+              CliMessages.MESSAGE_ARG_UNEXPECTED_ERROR_OCCURRED_ARG_E7255EE6,
+              file == null ? filePath : file.getName(),
+              e.getMessage()));
     }

Review Comment:
   Addressed in `240cfd502cce`. Both worker loops now catch exceptions per 
file, record that file's failure, and continue draining the queue. Added 
single-worker regressions for tree align-by-time, tree align-by-device, and 
table CSV imports: the first batch fails while the next file is still inserted.
   



##########
iotdb-client/cli/src/main/java/org/apache/iotdb/tool/data/AbstractImportData.java:
##########
@@ -34,10 +37,39 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 public abstract class AbstractImportData extends AbstractDataTool implements 
Runnable {
 
   private static final IoTPrinter ioTPrinter = new IoTPrinter(System.out);
+  private static final AtomicBoolean importFailed = new AtomicBoolean();

Review Comment:
   Addressed in `240cfd502cce`. Shared failure markers now cover failed SQL 
statements, table SQL database mismatches, SQL read errors, and CSV files 
rejected for an empty file, invalid time header, unsupported extension, or 
read/path errors. Failed SQL files are counted once as failures instead of 
successes. Added coverage in both models. The existing row-level 
conversion/`.failed` policy is unchanged.
   



##########
iotdb-client/cli/src/main/java/org/apache/iotdb/tool/data/ExportDataTable.java:
##########
@@ -131,31 +147,108 @@ public void exportBySql(String sql, int index) {
       }
     }
     for (int i = 0; i < exportSql.size(); i++) {
-      String path = targetDirectory + targetFile + i;
-      String table = tables.get(i);
-      try (SessionDataSet sessionDataSet =
-          tableSession.executeQueryStatement(exportSql.get(i), timeout)) {
-        if (Constants.SQL_SUFFIXS.equalsIgnoreCase(exportType)) {
-          exportToSqlFile(sessionDataSet, table, path);
-        } else if (Constants.TSFILE_SUFFIXS.equalsIgnoreCase(exportType)) {
-          long start = System.currentTimeMillis();
-          boolean isComplete = exportToTsFile(sessionDataSet, path + 
".tsfile", table);
-          if (isComplete) {
-            long end = System.currentTimeMillis();
-            ioTPrinter.println("Export completely!cost: " + (end - start) + " 
ms.");
+      String path = targetDirectory + targetFile + (customQuery ? index : i);
+      try {
+        String queryTable =
+            customQuery
+                ? (Constants.SQL_SUFFIXS.equalsIgnoreCase(exportType)
+                        || 
Constants.TSFILE_SUFFIXS.equalsIgnoreCase(exportType)
+                    ? getQueryTable(sql)
+                    : null)
+                : tables.get(i);
+        try (SessionDataSet sessionDataSet =
+            tableSession.executeQueryStatement(exportSql.get(i), timeout)) {
+          if (Constants.SQL_SUFFIXS.equalsIgnoreCase(exportType)) {
+            exportToSqlFile(sessionDataSet, queryTable, path);
+          } else if (Constants.TSFILE_SUFFIXS.equalsIgnoreCase(exportType)) {
+            long start = System.currentTimeMillis();
+            boolean isComplete = exportToTsFile(sessionDataSet, path + 
".tsfile", queryTable);
+            if (isComplete) {
+              long end = System.currentTimeMillis();
+              ioTPrinter.println("Export completely!cost: " + (end - start) + 
" ms.");
+            }
+          } else {
+            exportToCsvFile(sessionDataSet, path);
           }
-        } else {
-          exportToCsvFile(sessionDataSet, path);
+          sessionDataSet.closeOperationHandle();
+          ioTPrinter.println(Constants.EXPORT_COMPLETELY);
         }
-        sessionDataSet.closeOperationHandle();
-        ioTPrinter.println(Constants.EXPORT_COMPLETELY);
       } catch (StatementExecutionException
           | IoTDBConnectionException
           | IOException
           | WriteProcessException e) {
         ioTPrinter.println("Cannot dump result because: " + e.getMessage());
+        success = false;
       }
     }
+    return success;
+  }
+
+  private static String getQueryTable(String sql) throws 
StatementExecutionException {
+    String message =
+        String.format(
+            CliMessages
+                
.EXCEPTION_SQL_TSFILE_EXPORT_REQUIRES_A_QUERY_FROM_A_SINGLE_TABLE_IN_DATABASE_ARG_C9EB3768,
+            database);
+    RelationalSqlLexer lexer =
+        new RelationalSqlLexer(new 
CaseInsensitiveStream(CharStreams.fromString(sql)));
+    RelationalSqlParser parser = new RelationalSqlParser(new 
CommonTokenStream(lexer));
+    lexer.removeErrorListeners();
+    lexer.addErrorListener(
+        new BaseErrorListener() {
+          @Override
+          public void syntaxError(
+              Recognizer<?, ?> recognizer,
+              Object offendingSymbol,
+              int line,
+              int charPositionInLine,
+              String error,
+              RecognitionException cause) {
+            throw new ParseCancellationException(message);
+          }
+        });
+    parser.removeErrorListeners();
+    parser.setErrorHandler(new BailErrorStrategy());
+    List<RelationalSqlParser.QualifiedNameContext> sources = new ArrayList<>();
+    try {
+      new RelationalSqlBaseVisitor<Void>() {
+        @Override
+        public Void visitTableName(RelationalSqlParser.TableNameContext 
context) {
+          sources.add(context.qualifiedName());
+          return null;
+        }
+      }.visit(parser.singleStatement());
+    } catch (ParseCancellationException e) {
+      throw new StatementExecutionException(message);
+    }
+    // Multiple source relations do not have an unambiguous destination table.
+    if (sources.size() != 1) {

Review Comment:
   Updated the `-q` help in `240cfd502cce` for both English and Chinese: table 
SQL/TsFile custom queries require one table without joins, subqueries, or CTEs. 
The help also now reflects that `-q` supports SQL/TsFile export, replacing the 
obsolete CSV-only description.
   



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

Reply via email to