Skip to content

Commit 9ca448e

Browse files
authored
Fix tree model load type mismatch conversion (#17949)
* Fix tree load type mismatch conversion * Fix load-and-alter expected count
1 parent b332786 commit 9ca448e

3 files changed

Lines changed: 75 additions & 10 deletions

File tree

integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBAlterTimeSeriesTypeIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,8 +879,8 @@ public void testLoadAndAlter()
879879
session.executeQueryStatement("select count(s1) from " + database + ".load_and_alter");
880880
RowRecord rec;
881881
rec = dataSet.next();
882-
// Before alter, DOUBLE TsFiles loaded directly are invisible under the existing INT32 schema.
883-
assertEquals(9, rec.getFields().get(0).getLongV());
882+
// Before alter, DOUBLE TsFiles are converted to INT32 and visible under the existing schema.
883+
assertEquals(15, rec.getFields().get(0).getLongV());
884884
assertFalse(dataSet.hasNext());
885885
}
886886

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -483,14 +483,15 @@ private void verifySchema(ISchemaTree schemaTree)
483483
}
484484

485485
// check datatype
486-
if (LOGGER.isDebugEnabled() && !tsFileSchema.getType().equals(iotdbSchema.getType())) {
487-
LOGGER.debug(
488-
"Measurement {}{}{} datatype not match, TsFile: {}, IoTDB: {}",
489-
device,
490-
TsFileConstant.PATH_SEPARATOR,
491-
iotdbSchema.getMeasurementName(),
492-
tsFileSchema.getType(),
493-
iotdbSchema.getType());
486+
if (!tsFileSchema.getType().equals(iotdbSchema.getType())) {
487+
throw new LoadAnalyzeTypeMismatchException(
488+
String.format(
489+
"Data type mismatch for measurement %s%s%s, type in TsFile: %s, type in IoTDB: %s",
490+
device,
491+
TsFileConstant.PATH_SEPARATOR,
492+
iotdbSchema.getMeasurementName(),
493+
tsFileSchema.getType(),
494+
iotdbSchema.getType()));
494495
}
495496

496497
// check encoding

iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/load/LoadTsFileAnalyzerTest.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@
1919

2020
package org.apache.iotdb.db.queryengine.plan.analyze.load;
2121

22+
import org.apache.iotdb.commons.path.PartialPath;
2223
import org.apache.iotdb.commons.queryengine.plan.relational.metadata.ColumnSchema;
2324
import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory;
2425
import org.apache.iotdb.db.conf.IoTDBDescriptor;
2526
import org.apache.iotdb.db.exception.load.LoadAnalyzeTypeMismatchException;
2627
import org.apache.iotdb.db.exception.load.LoadRuntimeOutOfMemoryException;
2728
import org.apache.iotdb.db.queryengine.common.MPPQueryContext;
2829
import org.apache.iotdb.db.queryengine.common.QueryId;
30+
import org.apache.iotdb.db.queryengine.common.schematree.ClusterSchemaTree;
31+
import org.apache.iotdb.db.queryengine.common.schematree.ISchemaTree;
2932
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.LoadTsFile;
33+
import org.apache.iotdb.db.queryengine.plan.statement.crud.LoadTsFileStatement;
3034
import org.apache.iotdb.db.storageengine.dataregion.tsfile.TsFileResource;
3135

3236
import org.apache.tsfile.enums.ColumnCategory;
@@ -149,6 +153,51 @@ public void testTableSchemaCacheShouldNotThrowMismatchWhenSkippingDataTypeVerifi
149153
}
150154
}
151155

156+
@Test
157+
public void testTreeSchemaVerifierShouldThrowMismatchWhenVerifyingDataType() throws Exception {
158+
final File tsFile = new File("load-tree-type-mismatch.tsfile");
159+
if (tsFile.exists()) {
160+
Assert.assertTrue(tsFile.delete());
161+
}
162+
Assert.assertTrue(tsFile.createNewFile());
163+
164+
try (final LoadTsFileAnalyzer analyzer =
165+
new LoadTsFileAnalyzer(
166+
LoadTsFileStatement.createUnchecked(tsFile.getAbsolutePath()),
167+
false,
168+
new MPPQueryContext(new QueryId("load_tree_test")))) {
169+
final TreeSchemaAutoCreatorAndVerifier verifier =
170+
new TreeSchemaAutoCreatorAndVerifier(analyzer);
171+
try {
172+
final IDeviceID device = IDeviceID.Factory.DEFAULT_FACTORY.create("root.sg.d1");
173+
final LoadTsFileTreeSchemaCache schemaCache = getTreeSchemaCache(verifier);
174+
schemaCache.addTimeSeries(device, new MeasurementSchema("s1", TSDataType.BOOLEAN));
175+
schemaCache.addIsAlignedCache(device, true, true);
176+
177+
final ClusterSchemaTree schemaTree = new ClusterSchemaTree();
178+
schemaTree.appendSingleMeasurement(
179+
new PartialPath("root.sg.d1.s1"),
180+
new MeasurementSchema("s1", TSDataType.INT32),
181+
null,
182+
null,
183+
null,
184+
true);
185+
186+
final InvocationTargetException exception =
187+
Assert.assertThrows(
188+
InvocationTargetException.class,
189+
() -> getVerifyTreeSchemaMethod().invoke(verifier, schemaTree));
190+
Assert.assertTrue(exception.getCause() instanceof LoadAnalyzeTypeMismatchException);
191+
} finally {
192+
verifier.close();
193+
}
194+
} finally {
195+
if (tsFile.exists()) {
196+
Assert.assertTrue(tsFile.delete());
197+
}
198+
}
199+
}
200+
152201
private void writeTableTsFileWithMixedDevices(final File tsFile) throws Exception {
153202
if (tsFile.exists()) {
154203
Assert.assertTrue(tsFile.delete());
@@ -203,6 +252,14 @@ private void injectTableSchemaCache(
203252
tableSchemaCacheField.set(analyzer, schemaCache);
204253
}
205254

255+
private LoadTsFileTreeSchemaCache getTreeSchemaCache(
256+
final TreeSchemaAutoCreatorAndVerifier verifier) throws Exception {
257+
final Field schemaCacheField =
258+
TreeSchemaAutoCreatorAndVerifier.class.getDeclaredField("schemaCache");
259+
schemaCacheField.setAccessible(true);
260+
return (LoadTsFileTreeSchemaCache) schemaCacheField.get(verifier);
261+
}
262+
206263
private LoadTsFileTableSchemaCache createTableSchemaCache(final boolean shouldVerifyDataType)
207264
throws LoadRuntimeOutOfMemoryException {
208265
return new LoadTsFileTableSchemaCache(
@@ -219,6 +276,13 @@ private Method getVerifyTableDataTypeMethod() throws NoSuchMethodException {
219276
return method;
220277
}
221278

279+
private Method getVerifyTreeSchemaMethod() throws NoSuchMethodException {
280+
final Method method =
281+
TreeSchemaAutoCreatorAndVerifier.class.getDeclaredMethod("verifySchema", ISchemaTree.class);
282+
method.setAccessible(true);
283+
return method;
284+
}
285+
222286
private org.apache.iotdb.commons.queryengine.plan.relational.metadata.TableSchema
223287
createTableSchema(final TSDataType fieldType) {
224288
return new org.apache.iotdb.commons.queryengine.plan.relational.metadata.TableSchema(

0 commit comments

Comments
 (0)