Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.iotdb.itbase.category.ClusterIT;
import org.apache.iotdb.itbase.category.LocalStandaloneIT;
import org.apache.iotdb.jdbc.IoTDBSQLException;
import org.apache.iotdb.pipe.it.dual.TypeConversionSemanticCase;
import org.apache.iotdb.rpc.TSStatusCode;

import org.apache.tsfile.enums.TSDataType;
Expand Down Expand Up @@ -667,6 +668,142 @@ public void testLoadWithCompatibleDataType() throws Exception {
}
}

@Test
public void testLoadWithAllConflictingAlignedDataTypes() throws Exception {
final String database = "root.load.aligned_type_conflict";
final String allConflictDevice = database + ".d_all_conflict";
final String oneConflictDevice = database + ".d_one_conflict";
final String nonAlignedDevice = database + ".na";
final File source = new File(tmpDir, "aligned-type-conflict.tsfile");
final List<TSDataType> conflictingTargetTypes =
Arrays.asList(
TSDataType.INT32,
TSDataType.INT64,
TSDataType.FLOAT,
TSDataType.DOUBLE,
TSDataType.BLOB,
TSDataType.DATE,
TSDataType.TIMESTAMP,
TSDataType.INT32,
TSDataType.INT64);
final List<TSDataType> oneConflictTargetTypes =
new ArrayList<>(Collections.nCopies(conflictingTargetTypes.size(), TSDataType.BOOLEAN));
oneConflictTargetTypes.set(0, TSDataType.INT32);
final List<String> measurements = new ArrayList<>();
final List<IMeasurementSchema> sourceSchemas = new ArrayList<>();
final List<IMeasurementSchema> allConflictSchemas = new ArrayList<>();
final List<IMeasurementSchema> oneConflictSchemas = new ArrayList<>();
for (int measurementIndex = 0;
measurementIndex < conflictingTargetTypes.size();
measurementIndex++) {
final String measurement = "s" + measurementIndex;
measurements.add(measurement);
sourceSchemas.add(new MeasurementSchema(measurement, TSDataType.BOOLEAN, TSEncoding.PLAIN));
allConflictSchemas.add(
new MeasurementSchema(measurement, conflictingTargetTypes.get(measurementIndex)));
oneConflictSchemas.add(
new MeasurementSchema(measurement, oneConflictTargetTypes.get(measurementIndex)));
}
try {
try (final Connection connection = EnvFactory.getEnv().getConnection();
final Statement statement = connection.createStatement()) {
statement.execute("create database " + database);
statement.execute(convert2AlignedSQL(allConflictDevice, allConflictSchemas));
statement.execute(convert2AlignedSQL(oneConflictDevice, oneConflictSchemas));
for (int measurementIndex = 0;
measurementIndex < conflictingTargetTypes.size();
measurementIndex++) {
statement.execute(
convert2SQL(
nonAlignedDevice, (MeasurementSchema) allConflictSchemas.get(measurementIndex)));
}
}
try (final TsFileWriter writer = new TsFileWriter(source)) {
writer.registerAlignedTimeseries(new Path(allConflictDevice), sourceSchemas);
writer.registerAlignedTimeseries(new Path(oneConflictDevice), sourceSchemas);
writer.registerTimeseries(new Path(nonAlignedDevice), sourceSchemas);
writeBooleanRows(writer, allConflictDevice, sourceSchemas, true);
writeBooleanRows(writer, oneConflictDevice, sourceSchemas, true);
writeBooleanRows(writer, nonAlignedDevice, sourceSchemas, false);
}

try (final Connection connection = EnvFactory.getEnv().getConnection();
final Statement statement = connection.createStatement()) {
statement.execute(
String.format(
"load \"%s\" with ('auto-create-schema'='false', 'convert-on-type-mismatch'='true')",
source.getAbsolutePath()));
for (final String device :
Arrays.asList(allConflictDevice, oneConflictDevice, nonAlignedDevice)) {
final List<TSDataType> targetTypes =
device.equals(oneConflictDevice) ? oneConflictTargetTypes : conflictingTargetTypes;
try (final ResultSet resultSet =
statement.executeQuery(
"select "
+ String.join(",", measurements)
+ " from "
+ device
+ " order by time")) {
for (int row = 0; row < 4; row++) {
Assert.assertTrue("Missing row " + row + " for " + device, resultSet.next());
Assert.assertEquals(row + 1, resultSet.getLong(1));
for (int column = 0; column < measurements.size(); column++) {
Assert.assertEquals(
"Unexpected value for "
+ device
+ "."
+ measurements.get(column)
+ " at row "
+ row,
expectedBooleanConversionValue(targetTypes.get(column), row),
resultSet.getString(column + 2));
}
}
Assert.assertFalse(resultSet.next());
}
}
}
} finally {
try (final Connection connection = EnvFactory.getEnv().getConnection();
final Statement statement = connection.createStatement()) {
deleteDatabaseIfExists(statement, database);
}
}
}

private void writeBooleanRows(
final TsFileWriter writer,
final String device,
final List<IMeasurementSchema> schemas,
final boolean isAligned)
throws Exception {
final Tablet tablet = new Tablet(device, schemas, 4);
for (int row = 0; row < 4; row++) {
tablet.addTimestamp(row, row + 1);
for (int column = 0; column < schemas.size(); column++) {
tablet.addValue(row, column, row % 3 != 1);
}
}
if (isAligned) {
writer.writeAligned(tablet);
} else {
writer.writeTree(tablet);
}
}

private String expectedBooleanConversionValue(final TSDataType targetType, final int row) {
if (targetType == TSDataType.BOOLEAN) {
return row % 3 != 1 ? "true" : "false";
}
for (final TypeConversionSemanticCase conversionCase : TypeConversionSemanticCase.CASES) {
if (conversionCase.sourceType == TSDataType.BOOLEAN
&& conversionCase.targetType == targetType) {
return conversionCase.expectedValues[row % conversionCase.expectedValues.length];
}
}
throw new IllegalArgumentException("No Boolean conversion expectation for " + targetType);
}

// Loading aligned and non-aligned devices extends the bound template and preserves every point.
@Test
public void testLoadWithExtendTemplate() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.iotdb.pipe.it.dual.TypeConversionSemanticCase;
import org.apache.iotdb.pipe.it.dual.treemodel.auto.AbstractPipeTreeModelSharedClusterIT;

import org.apache.tsfile.enums.TSDataType;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
Expand All @@ -41,6 +42,12 @@ public class IoTDBPipeTypeConversionSemanticIT extends AbstractPipeTreeModelShar

private static final String DEVICE = "root.pipe_type_conversion.d";
private static final String ALIGNED_DEVICE = "root.pipe_type_conversion.aligned_d";
private static final String HISTORY_DEVICE_PREFIX = "root.pipe_type_conversion.history";
private static final String HISTORY_ALIGNED_ALL_CONFLICT_DEVICE =
HISTORY_DEVICE_PREFIX + ".d_all_conflict";
private static final String HISTORY_ALIGNED_ONE_CONFLICT_DEVICE =
HISTORY_DEVICE_PREFIX + ".d_one_conflict";
private static final String HISTORY_NON_ALIGNED_DEVICE = HISTORY_DEVICE_PREFIX + ".na";
private static final List<TypeConversionSemanticCase> ALIGNED_STREAM_CASES =
getCases(
"bool_to_int32",
Expand All @@ -53,6 +60,8 @@ public class IoTDBPipeTypeConversionSemanticIT extends AbstractPipeTreeModelShar
"int32_to_boolean",
"int32_to_timestamp",
"int32_to_date");
private static final List<TypeConversionSemanticCase> ALIGNED_HISTORY_CASES =
getBooleanIncompatibleCases();

@Override
protected void setupConfig() {
Expand All @@ -66,7 +75,8 @@ protected void setupConfig() {
.getConfig()
.getCommonConfig()
.setDataReplicationFactor(1)
.setSchemaReplicationFactor(1);
.setSchemaReplicationFactor(1)
.setAutoCreateSchemaEnabled(false);
}

@Override
Expand Down Expand Up @@ -110,6 +120,81 @@ public void testAlignedStreamPipeReceiverTypeConversionSemantics() {
60);
}

@Test
public void testHistoryPipeReceiverTypeConversionForAlignedAllConflicts() {
createAlignedTimeseries(
senderEnv, HISTORY_ALIGNED_ALL_CONFLICT_DEVICE, ALIGNED_HISTORY_CASES, true);
createAlignedTimeseries(
receiverEnv, HISTORY_ALIGNED_ALL_CONFLICT_DEVICE, ALIGNED_HISTORY_CASES, false);
createAlignedTimeseries(
senderEnv, HISTORY_ALIGNED_ONE_CONFLICT_DEVICE, ALIGNED_HISTORY_CASES, true);
createAlignedTimeseriesWithOneConflict(receiverEnv, HISTORY_ALIGNED_ONE_CONFLICT_DEVICE);
createTimeseries(senderEnv, HISTORY_NON_ALIGNED_DEVICE, ALIGNED_HISTORY_CASES, true);
createTimeseries(receiverEnv, HISTORY_NON_ALIGNED_DEVICE, ALIGNED_HISTORY_CASES, false);

final List<String> insertStatements = new ArrayList<>();
insertStatements.addAll(
createInsertStatements(HISTORY_ALIGNED_ALL_CONFLICT_DEVICE, ALIGNED_HISTORY_CASES, true));
insertStatements.addAll(
createInsertStatements(HISTORY_ALIGNED_ONE_CONFLICT_DEVICE, ALIGNED_HISTORY_CASES, true));
insertStatements.addAll(
createInsertStatements(HISTORY_NON_ALIGNED_DEVICE, ALIGNED_HISTORY_CASES, false));
TestUtils.executeNonQueries(senderEnv, insertStatements, null);
createHistoryOnlyTsFilePipe();

final HashSet<String> expectedRows = new HashSet<>(createExpectedRows(ALIGNED_HISTORY_CASES));
final HashSet<String> oneConflictExpectedRows =
new HashSet<>(createExpectedRowsForOneConflict(ALIGNED_HISTORY_CASES));
for (final String device :
List.of(
HISTORY_ALIGNED_ONE_CONFLICT_DEVICE,
HISTORY_NON_ALIGNED_DEVICE,
HISTORY_ALIGNED_ALL_CONFLICT_DEVICE)) {
TestUtils.assertDataEventuallyOnEnv(
receiverEnv,
createQuerySql(device, ALIGNED_HISTORY_CASES),
createExpectedHeader(device, ALIGNED_HISTORY_CASES),
device.equals(HISTORY_ALIGNED_ONE_CONFLICT_DEVICE)
? oneConflictExpectedRows
: expectedRows,
60);
}
}

private static void createAlignedTimeseriesWithOneConflict(
final BaseEnv env, final String device) {
final List<String> measurements = new ArrayList<>();
for (int i = 0; i < ALIGNED_HISTORY_CASES.size(); i++) {
measurements.add(
String.format(
"%s %s encoding=PLAIN",
ALIGNED_HISTORY_CASES.get(i).measurement,
i == 0
? ALIGNED_HISTORY_CASES.get(i).targetType
: ALIGNED_HISTORY_CASES.get(i).sourceType));
}
TestUtils.executeNonQuery(
env,
String.format("create aligned timeseries %s(%s)", device, String.join(",", measurements)),
null);
}

private void createHistoryOnlyTsFilePipe() {
TestUtils.executeNonQuery(
senderEnv,
String.format(
"create pipe aligned_history_type_conversion"
+ " with source ('source'='iotdb-source','source.path'='%s.**',"
+ "'source.inclusion'='data.insert','source.capture.tree'='true',"
+ "'source.capture.table'='false','source.history.enable'='true',"
+ "'source.realtime.enable'='false')"
+ " with processor ('processor'='do-nothing-processor')"
+ " with sink ('sink'='iotdb-thrift-sink','sink.node-urls'='%s')",
HISTORY_DEVICE_PREFIX,
receiverEnv.getDataNodeWrapperList().get(0).getIpAndPortString()),
null);
}

private static void createTimeseries(
final BaseEnv env,
final String device,
Expand Down Expand Up @@ -235,6 +320,26 @@ private static List<String> createExpectedRows(
return rows;
}

private static List<String> createExpectedRowsForOneConflict(
final List<TypeConversionSemanticCase> conversionCases) {
final List<String> rows = new ArrayList<>();
for (int row = 0; row < TypeConversionSemanticCase.ROW_COUNT; row++) {
final List<String> values = new ArrayList<>();
values.add(Integer.toString(row + 1));
for (int measurementIndex = 0;
measurementIndex < conversionCases.size();
measurementIndex++) {
final TypeConversionSemanticCase conversionCase = conversionCases.get(measurementIndex);
values.add(
measurementIndex == 0
? conversionCase.expectedValues[row]
: conversionCase.sourceSqlValues[row]);
}
rows.add(String.join(Character.toString(','), values) + Character.toString(','));
}
return rows;
}

private static List<TypeConversionSemanticCase> getCases(final String... measurements) {
final List<TypeConversionSemanticCase> cases = new ArrayList<>();
for (final String measurement : measurements) {
Expand All @@ -243,6 +348,17 @@ private static List<TypeConversionSemanticCase> getCases(final String... measure
return cases;
}

private static List<TypeConversionSemanticCase> getBooleanIncompatibleCases() {
final List<TypeConversionSemanticCase> cases = new ArrayList<>();
for (final TypeConversionSemanticCase conversionCase : TypeConversionSemanticCase.CASES) {
if (conversionCase.sourceType == TSDataType.BOOLEAN
&& !conversionCase.targetType.isCompatible(conversionCase.sourceType)) {
cases.add(conversionCase);
}
}
return cases;
}

private static TypeConversionSemanticCase getCase(final String measurement) {
for (final TypeConversionSemanticCase conversionCase : TypeConversionSemanticCase.CASES) {
if (conversionCase.measurement.equals(measurement)) {
Expand Down
Loading
Loading