Skip to content

Commit 808f575

Browse files
Load: fix some minor issues (#13781)
1 parent 4558493 commit 808f575

File tree

6 files changed

+49
-51
lines changed

6 files changed

+49
-51
lines changed

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/load/LoadTsFileAnalyzer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.iotdb.db.queryengine.plan.analyze.load;
2121

2222
import org.apache.iotdb.commons.auth.AuthException;
23+
import org.apache.iotdb.db.exception.VerifyMetadataException;
2324
import org.apache.iotdb.db.exception.sql.SemanticException;
2425
import org.apache.iotdb.db.queryengine.common.MPPQueryContext;
2526
import org.apache.iotdb.db.queryengine.plan.analyze.ClusterPartitionFetcher;
@@ -143,7 +144,7 @@ protected boolean doAnalyzeFileByFile(IAnalysis analysis) {
143144
} catch (Exception e) {
144145
final String exceptionMessage =
145146
String.format(
146-
"The file %s is not a valid tsfile. Please check the input file. Detail: %s",
147+
"Loading file %s failed. Detail: %s",
147148
tsFile.getPath(), e.getMessage() == null ? e.getClass().getName() : e.getMessage());
148149
LOGGER.warn(exceptionMessage, e);
149150
analysis.setFinishQueryAfterAnalyze(true);
@@ -154,7 +155,8 @@ protected boolean doAnalyzeFileByFile(IAnalysis analysis) {
154155
return true;
155156
}
156157

157-
protected abstract void analyzeSingleTsFile(final File tsFile) throws IOException, AuthException;
158+
protected abstract void analyzeSingleTsFile(final File tsFile)
159+
throws IOException, AuthException, VerifyMetadataException;
158160

159161
protected TsFileResource constructTsFileResource(
160162
final TsFileSequenceReader reader, final File tsFile) throws IOException {

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/load/LoadTsFileToTableModelAnalyzer.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ public IAnalysis analyzeFileByFile(IAnalysis analysis) {
115115
}
116116

117117
@Override
118-
protected void analyzeSingleTsFile(final File tsFile) throws IOException, AuthException {
118+
protected void analyzeSingleTsFile(final File tsFile)
119+
throws IOException, AuthException, VerifyMetadataException {
119120
try (final TsFileSequenceReader reader = new TsFileSequenceReader(tsFile.getAbsolutePath())) {
120121
// can be reused when constructing tsfile resource
121122
final TsFileSequenceReaderTimeseriesMetadataIterator timeseriesMetadataIterator =
@@ -127,6 +128,8 @@ protected void analyzeSingleTsFile(final File tsFile) throws IOException, AuthEx
127128
}
128129

129130
// check whether the tsfile is table-model or not
131+
// TODO: currently, loading a file with both tree-model and table-model data is not supported.
132+
// May need to support this and remove this check in the future.
130133
if (Objects.isNull(reader.readFileMetadata().getTableSchemaMap())
131134
|| reader.readFileMetadata().getTableSchemaMap().size() == 0) {
132135
throw new SemanticException("Attempted to load a tree-model TsFile into table-model.");
@@ -147,7 +150,10 @@ protected void analyzeSingleTsFile(final File tsFile) throws IOException, AuthEx
147150
true)
148151
.orElse(null);
149152
if (Objects.isNull(realSchema)) {
150-
LOGGER.warn("Failed to validata schema for table {}", name2Schema.getValue());
153+
throw new VerifyMetadataException(
154+
String.format(
155+
"Failed to validate schema for table {%s, %s}",
156+
name2Schema.getKey(), name2Schema.getValue()));
151157
}
152158
}
153159

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/load/LoadTsFileToTreeModelAnalyzer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ protected void analyzeSingleTsFile(final File tsFile) throws IOException, AuthEx
119119
}
120120

121121
// check whether the tsfile is tree-model or not
122+
// TODO: currently, loading a file with both tree-model and table-model data is not supported.
123+
// May need to support this and remove this check in the future.
122124
if (Objects.nonNull(reader.readFileMetadata().getTableSchemaMap())
123125
&& reader.readFileMetadata().getTableSchemaMap().size() != 0) {
124126
throw new SemanticException("Attempted to load a table-model TsFile into tree-model.");

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -561,40 +561,26 @@ public Node visitShowFunctionsStatement(RelationalSqlParser.ShowFunctionsStateme
561561

562562
@Override
563563
public Node visitLoadTsFileStatement(RelationalSqlParser.LoadTsFileStatementContext ctx) {
564-
final Map<String, String> loadTsFileAttributes = new HashMap<>();
565-
if (ctx.loadFileWithAttributeClauses() != null) {
566-
for (RelationalSqlParser.LoadFileWithAttributeClauseContext attributeContext :
567-
ctx.loadFileWithAttributeClauses().loadFileWithAttributeClause()) {
568-
final String key =
569-
parseStringLiteral(attributeContext.loadFileWithKey.getText()).trim().toLowerCase();
570-
final String value =
571-
parseStringLiteral(attributeContext.loadFileWithValue.getText()).trim().toLowerCase();
572-
573-
LoadTsFileConfigurator.validateParameters(key, value);
574-
loadTsFileAttributes.put(key, value);
575-
}
576-
}
564+
final Map<String, String> withAttributes =
565+
ctx.loadFileWithAttributesClause() != null
566+
? parseLoadFileWithAttributeClauses(
567+
ctx.loadFileWithAttributesClause().loadFileWithAttributeClause())
568+
: new HashMap<>();
577569

570+
withAttributes.forEach(LoadTsFileConfigurator::validateParameters);
578571
return new LoadTsFile(
579-
getLocation(ctx), ((StringLiteral) visit(ctx.fileName)).getValue(), loadTsFileAttributes);
572+
getLocation(ctx), ((StringLiteral) visit(ctx.fileName)).getValue(), withAttributes);
580573
}
581574

582-
public static String parseStringLiteral(String src) {
583-
if (2 <= src.length()) {
584-
// do not unescape string
585-
String unWrappedString = src.substring(1, src.length() - 1);
586-
if (src.charAt(0) == '\"' && src.charAt(src.length() - 1) == '\"') {
587-
// replace "" with "
588-
String replaced = unWrappedString.replace("\"\"", "\"");
589-
return replaced.length() == 0 ? "" : replaced;
590-
}
591-
if ((src.charAt(0) == '\'' && src.charAt(src.length() - 1) == '\'')) {
592-
// replace '' with '
593-
String replaced = unWrappedString.replace("''", "'");
594-
return replaced.length() == 0 ? "" : replaced;
595-
}
575+
private Map<String, String> parseLoadFileWithAttributeClauses(
576+
List<RelationalSqlParser.LoadFileWithAttributeClauseContext> contexts) {
577+
final Map<String, String> withAttributesMap = new HashMap<>();
578+
for (RelationalSqlParser.LoadFileWithAttributeClauseContext context : contexts) {
579+
withAttributesMap.put(
580+
((StringLiteral) visit(context.loadFileWithKey)).getValue(),
581+
((StringLiteral) visit(context.loadFileWithValue)).getValue());
596582
}
597-
return src;
583+
return withAttributesMap;
598584
}
599585

600586
@Override

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/util/SqlFormatter.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -804,21 +804,23 @@ protected Void visitLoadTsFile(final LoadTsFile node, final Integer indent) {
804804
builder.append("\"" + node.getFilePath() + "\"");
805805
builder.append(" \n");
806806

807-
builder
808-
.append("WITH (")
809-
.append("\n")
810-
.append(
811-
node.getLoadAttributes().entrySet().stream()
812-
.map(
813-
entry ->
814-
indentString(1)
815-
+ "\""
816-
+ entry.getKey()
817-
+ "\" = \""
818-
+ entry.getValue()
819-
+ "\"")
820-
.collect(joining(", " + "\n")))
821-
.append(")\n");
807+
if (!node.getLoadAttributes().isEmpty()) {
808+
builder
809+
.append("WITH (")
810+
.append("\n")
811+
.append(
812+
node.getLoadAttributes().entrySet().stream()
813+
.map(
814+
entry ->
815+
indentString(1)
816+
+ "\""
817+
+ entry.getKey()
818+
+ "\" = \""
819+
+ entry.getValue()
820+
+ "\"")
821+
.collect(joining(", " + "\n")))
822+
.append(")\n");
823+
}
822824
return null;
823825
}
824826

iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,18 @@ showFunctionsStatement
240240

241241
// -------------------------------------------- Load Statement ---------------------------------------------------------
242242
loadTsFileStatement
243-
: LOAD fileName=string (loadFileWithAttributeClauses)?
243+
: LOAD fileName=string (loadFileWithAttributesClause)?
244244
;
245245

246-
loadFileWithAttributeClauses
246+
loadFileWithAttributesClause
247247
: WITH
248248
'('
249249
(loadFileWithAttributeClause ',')* loadFileWithAttributeClause?
250250
')'
251251
;
252252

253253
loadFileWithAttributeClause
254-
: loadFileWithKey=STRING EQ loadFileWithValue=STRING
254+
: loadFileWithKey=string EQ loadFileWithValue=string
255255
;
256256

257257

0 commit comments

Comments
 (0)