CritasWang commented on code in PR #18828:
URL: https://github.com/apache/iotdb/pull/18828#discussion_r4344370873
##########
iotdb-client/cli/src/main/java/org/apache/iotdb/tool/data/ImportDataTable.java:
##########
@@ -370,36 +383,39 @@ protected void writeData(
}
private static void writeAndEmptyDataSet(Tablet tablet, int retryTime) {
- try (ITableSession session = sessionPool.getSession()) {
- session.insert(tablet);
- } catch (IoTDBConnectionException e) {
- if (retryTime > 0) {
- writeAndEmptyDataSet(tablet, --retryTime);
- }
- } catch (StatementExecutionException e) {
- ioTPrinter.println(Constants.INSERT_CSV_MEET_ERROR_MSG + e.getMessage());
- System.exit(1);
- }
+ executeWithRetry(
+ () -> {
+ try (ITableSession session = sessionPool.getSession()) {
+ session.insert(tablet);
+ }
+ },
+ retryTime);
}
private void parseHeaders(
List<String> headerNames,
Map<String, TSDataType> headerTypeMap,
Map<String, String> headerNameMap) {
- String regex = "(?<=\\()\\S+(?=\\))";
+ String regex = "\\(([^()]+)\\)$";
Pattern pattern = Pattern.compile(regex);
for (String headerName : headerNames) {
- headerName = headerName.toLowerCase();
+ String normalizedHeaderName = headerName.toLowerCase(Locale.ROOT);
+ // A complete table column name takes precedence over a possible type
suffix.
+ if (dataTypes.containsKey(normalizedHeaderName)) {
Review Comment:
Verified in `0ccd98b`. Exact column names are matched first, and typed
suffixes are stripped only from the headers left unmatched. Any collision now
rejects the file with `markFileFailed()` before anything is inserted. My
earlier probe now fails explicitly instead of dropping `value`. I also checked
that `TIME,value,value(DOUBLE)`, `TIME,value(DOUBLE),VALUE` and
`TIME,value,VALUE` against a table that has only `value` are each rejected with
no insert. Thanks!
##########
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) {
+ throw new StatementExecutionException(message);
+ }
+ List<RelationalSqlParser.IdentifierContext> names =
sources.get(0).identifier();
+ if (names.size() > 2
+ || (names.size() == 2 &&
!identifierValue(names.get(0)).equalsIgnoreCase(database))) {
+ throw new StatementExecutionException(message);
+ }
+ String name = identifierValue(names.get(names.size() - 1));
+ if (!tables.contains(name)) {
Review Comment:
Verified in `0ccd98b`. Custom-query validation now checks against the full
`show tables` result, while automatic export still honors `-table`.
`queryCommand` is parsed in `parseSpecialParams` before `init()`, so
`filterByTable` sees it. Thanks!
--
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]