Skip to content

Commit

Permalink
Fix database and tablename case sensitivity in tablet model
Browse files Browse the repository at this point in the history
  • Loading branch information
jt2594838 authored Aug 21, 2024
1 parent 1489c84 commit a32eef9
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.tsfile.read.common.RowRecord;
import org.apache.tsfile.utils.Binary;
import org.apache.tsfile.write.record.Tablet;
import org.apache.tsfile.write.record.Tablet.ColumnType;
import org.apache.tsfile.write.schema.IMeasurementSchema;
import org.apache.tsfile.write.schema.MeasurementSchema;
import org.junit.AfterClass;
Expand All @@ -57,6 +58,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.apache.iotdb.db.it.utils.TestUtils.assertTableNonQueryTestFail;
import static org.apache.iotdb.db.it.utils.TestUtils.resultSetEqualTest;
Expand Down Expand Up @@ -593,7 +595,9 @@ public void testInsertAttributes() throws SQLException {
}

@Test
public void testInsertCaseSensitivity() throws SQLException {
public void testInsertCaseSensitivity()
throws SQLException, IoTDBConnectionException, StatementExecutionException {
// column case sensitivity
try (Connection connection = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
Statement st1 = connection.createStatement()) {
st1.execute("use \"test\"");
Expand All @@ -618,6 +622,182 @@ public void testInsertCaseSensitivity() throws SQLException {
}
assertFalse(rs1.next());
}

// table case sensitivity with record and auto creation
try (ISession session = EnvFactory.getEnv().getSessionConnection(BaseEnv.TABLE_SQL_DIALECT)) {
session.executeNonQueryStatement("USE \"test\"");

List<IMeasurementSchema> schemaList = new ArrayList<>();
schemaList.add(new MeasurementSchema("id1", TSDataType.STRING));
schemaList.add(new MeasurementSchema("attr1", TSDataType.STRING));
schemaList.add(new MeasurementSchema("m1", TSDataType.DOUBLE));
final List<ColumnType> columnTypes =
Arrays.asList(ColumnType.ID, ColumnType.ATTRIBUTE, ColumnType.MEASUREMENT);
List<String> measurementIds =
schemaList.stream()
.map(IMeasurementSchema::getMeasurementId)
.collect(Collectors.toList());
List<TSDataType> dataTypes =
schemaList.stream().map(IMeasurementSchema::getType).collect(Collectors.toList());

long timestamp = 0;

for (long row = 0; row < 15; row++) {
Object[] values = new Object[] {"id:" + row, "attr:" + row, row * 1.0};
session.insertRelationalRecord(
"TaBle19_2", timestamp + row, measurementIds, dataTypes, columnTypes, values);
}

int cnt = 0;
SessionDataSet dataSet =
session.executeQueryStatement("select * from table19_2 order by time");
while (dataSet.hasNext()) {
RowRecord rowRecord = dataSet.next();
timestamp = rowRecord.getFields().get(0).getLongV();
assertEquals("id:" + timestamp, rowRecord.getFields().get(1).getBinaryV().toString());
assertEquals("attr:" + timestamp, rowRecord.getFields().get(2).getBinaryV().toString());
assertEquals(timestamp * 1.0, rowRecord.getFields().get(3).getDoubleV(), 0.0001);
cnt++;
}
assertEquals(15, cnt);
}

// table case sensitivity with record and no auto creation
try (ISession session = EnvFactory.getEnv().getSessionConnection(BaseEnv.TABLE_SQL_DIALECT)) {
session.executeNonQueryStatement("USE \"test\"");
session.executeNonQueryStatement(
"CREATE TABLE tAbLE19_3 (id1 string id, attr1 string attribute, "
+ "m1 double "
+ "measurement)");

List<IMeasurementSchema> schemaList = new ArrayList<>();
schemaList.add(new MeasurementSchema("id1", TSDataType.STRING));
schemaList.add(new MeasurementSchema("attr1", TSDataType.STRING));
schemaList.add(new MeasurementSchema("m1", TSDataType.DOUBLE));
final List<ColumnType> columnTypes =
Arrays.asList(ColumnType.ID, ColumnType.ATTRIBUTE, ColumnType.MEASUREMENT);
List<String> measurementIds =
schemaList.stream()
.map(IMeasurementSchema::getMeasurementId)
.collect(Collectors.toList());
List<TSDataType> dataTypes =
schemaList.stream().map(IMeasurementSchema::getType).collect(Collectors.toList());

long timestamp = 0;

for (long row = 0; row < 15; row++) {
Object[] values = new Object[] {"id:" + row, "attr:" + row, row * 1.0};
session.insertRelationalRecord(
"TaBle19_3", timestamp + row, measurementIds, dataTypes, columnTypes, values);
}

int cnt = 0;
SessionDataSet dataSet =
session.executeQueryStatement("select * from table19_3 order by time");
while (dataSet.hasNext()) {
RowRecord rowRecord = dataSet.next();
timestamp = rowRecord.getFields().get(0).getLongV();
assertEquals("id:" + timestamp, rowRecord.getFields().get(1).getBinaryV().toString());
assertEquals("attr:" + timestamp, rowRecord.getFields().get(2).getBinaryV().toString());
assertEquals(timestamp * 1.0, rowRecord.getFields().get(3).getDoubleV(), 0.0001);
cnt++;
}
assertEquals(15, cnt);
}

// table case sensitivity with tablet and no auto creation
try (ISession session = EnvFactory.getEnv().getSessionConnection(BaseEnv.TABLE_SQL_DIALECT)) {
session.executeNonQueryStatement("USE \"test\"");

List<IMeasurementSchema> schemaList = new ArrayList<>();
schemaList.add(new MeasurementSchema("id1", TSDataType.STRING));
schemaList.add(new MeasurementSchema("attr1", TSDataType.STRING));
schemaList.add(new MeasurementSchema("m1", TSDataType.DOUBLE));
final List<ColumnType> columnTypes =
Arrays.asList(ColumnType.ID, ColumnType.ATTRIBUTE, ColumnType.MEASUREMENT);

long timestamp = 0;
Tablet tablet = new Tablet("TaBle19_4", schemaList, columnTypes, 15);

for (long row = 0; row < 15; row++) {
int rowIndex = tablet.rowSize++;
tablet.addTimestamp(rowIndex, timestamp + row);
tablet.addValue("id1", rowIndex, "id:" + row);
tablet.addValue("attr1", rowIndex, "attr:" + row);
tablet.addValue("m1", rowIndex, row * 1.0);
if (tablet.rowSize == tablet.getMaxRowNumber()) {
session.insertRelationalTablet(tablet, true);
tablet.reset();
}
}

if (tablet.rowSize != 0) {
session.insertRelationalTablet(tablet);
tablet.reset();
}

int cnt = 0;
SessionDataSet dataSet =
session.executeQueryStatement("select * from table19_4 order by time");
while (dataSet.hasNext()) {
RowRecord rowRecord = dataSet.next();
timestamp = rowRecord.getFields().get(0).getLongV();
assertEquals("id:" + timestamp, rowRecord.getFields().get(1).getBinaryV().toString());
assertEquals("attr:" + timestamp, rowRecord.getFields().get(2).getBinaryV().toString());
assertEquals(timestamp * 1.0, rowRecord.getFields().get(3).getDoubleV(), 0.0001);
cnt++;
}
assertEquals(15, cnt);
}

// table case sensitivity with tablet and auto creation
try (ISession session = EnvFactory.getEnv().getSessionConnection(BaseEnv.TABLE_SQL_DIALECT)) {
session.executeNonQueryStatement("USE \"test\"");
session.executeNonQueryStatement(
"CREATE TABLE tAbLE19_5 (id1 string id, attr1 string attribute, "
+ "m1 double "
+ "measurement)");

List<IMeasurementSchema> schemaList = new ArrayList<>();
schemaList.add(new MeasurementSchema("id1", TSDataType.STRING));
schemaList.add(new MeasurementSchema("attr1", TSDataType.STRING));
schemaList.add(new MeasurementSchema("m1", TSDataType.DOUBLE));
final List<ColumnType> columnTypes =
Arrays.asList(ColumnType.ID, ColumnType.ATTRIBUTE, ColumnType.MEASUREMENT);

long timestamp = 0;
Tablet tablet = new Tablet("TaBle19_5", schemaList, columnTypes, 15);

for (long row = 0; row < 15; row++) {
int rowIndex = tablet.rowSize++;
tablet.addTimestamp(rowIndex, timestamp + row);
tablet.addValue("id1", rowIndex, "id:" + row);
tablet.addValue("attr1", rowIndex, "attr:" + row);
tablet.addValue("m1", rowIndex, row * 1.0);
if (tablet.rowSize == tablet.getMaxRowNumber()) {
session.insertRelationalTablet(tablet, true);
tablet.reset();
}
}

if (tablet.rowSize != 0) {
session.insertRelationalTablet(tablet);
tablet.reset();
}

int cnt = 0;
SessionDataSet dataSet =
session.executeQueryStatement("select * from table19_5 order by time");
while (dataSet.hasNext()) {
RowRecord rowRecord = dataSet.next();
timestamp = rowRecord.getFields().get(0).getLongV();
assertEquals("id:" + timestamp, rowRecord.getFields().get(1).getBinaryV().toString());
assertEquals("attr:" + timestamp, rowRecord.getFields().get(2).getBinaryV().toString());
assertEquals(timestamp * 1.0, rowRecord.getFields().get(3).getDoubleV(), 0.0001);
cnt++;
}
assertEquals(15, cnt);
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,9 +922,10 @@ public void insertRelationalSqlAnotherDBTest()
session.executeNonQueryStatement("FLush");

for (long row = 15; row < 30; row++) {
// check case sensitivity
session.executeNonQueryStatement(
String.format(
"INSERT INTO db1.table11 (time, id1, attr1, m1) VALUES (%d, '%s', '%s', %f)",
"INSERT INTO DB1.TaBle11 (time, id1, attr1, m1) VALUES (%d, '%s', '%s', %f)",
row, "id:" + row, "attr:" + row, row * 1.0));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void validate(
public static void validate(
Metadata metadata, WrappedInsertStatement insertStatement, MPPQueryContext context) {
try {
insertStatement.columnsToLowerCase();
insertStatement.toLowerCase();
insertStatement.validateTableSchema(metadata, context);
insertStatement.updateAfterSchemaValidation(context);
insertStatement.validateDeviceSchema(metadata, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public String getDatabase() {
return databaseName;
}

public void columnsToLowerCase() {
getInnerTreeStatement().measurementsToLowerCase();
public void toLowerCase() {
getInnerTreeStatement().toLowerCase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ private InsertRowStatement toInsertRowStatement(

insertRowStatement.setValues(values);
insertRowStatement.setNeedInferType(true);
databaseName.ifPresent(insertRowStatement::setDatabaseName);
databaseName.ifPresent(
databaseName1 -> insertRowStatement.setDatabaseName(databaseName1.toLowerCase()));
return insertRowStatement;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public abstract class InsertBaseStatement extends Statement {
/**
* if use id table, this filed is id form of device path <br>
* if not, this filed is device path<br>
* When using table model, this is the table name.
*/
protected PartialPath devicePath;

Expand Down Expand Up @@ -555,7 +556,8 @@ public Optional<String> getDatabaseName() {
// endregion

@TableModel
public void measurementsToLowerCase() {
public void toLowerCase() {
devicePath.toLowerCase();
if (measurements == null) {
return;
}
Expand All @@ -564,6 +566,11 @@ public void measurementsToLowerCase() {
measurements[i] = measurements[i].toLowerCase();
}
}
if (measurementSchemas != null) {
for (MeasurementSchema measurementSchema : measurementSchemas) {
measurementSchema.setMeasurementId(measurementSchema.getMeasurementId().toLowerCase());
}
}
}

@TableModel
Expand All @@ -576,4 +583,9 @@ public List<String> getAttributeColumnNameList() {
}
return attributeColumnNameList;
}

@TableModel
public String getTableName() {
return devicePath.getFullPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ public InsertBaseStatement removeLogicalView() {

@TableModel
@Override
public void measurementsToLowerCase() {
insertTabletStatementList.forEach(InsertTabletStatement::measurementsToLowerCase);
public void toLowerCase() {
insertTabletStatementList.forEach(InsertTabletStatement::toLowerCase);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public Pair<Integer, Integer> getRangeOfLogicalViewSchemaListRecorded() {
public IDeviceID getTableDeviceID() {
if (deviceID == null) {
String[] deviceIdSegments = new String[getIdColumnIndices().size() + 1];
deviceIdSegments[0] = this.devicePath.getFullPath();
deviceIdSegments[0] = this.getTableName();
for (int i = 0; i < getIdColumnIndices().size(); i++) {
final Integer columnIndex = getIdColumnIndices().get(i);
deviceIdSegments[i + 1] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ public InsertBaseStatement removeLogicalView() {

@TableModel
@Override
public void measurementsToLowerCase() {
insertRowStatementList.forEach(InsertRowStatement::measurementsToLowerCase);
public void toLowerCase() {
insertRowStatementList.forEach(InsertRowStatement::toLowerCase);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ public List<Object[]> getDeviceIdListNoTableName() {

@TableModel
@Override
public void measurementsToLowerCase() {
insertRowStatementList.forEach(InsertRowStatement::measurementsToLowerCase);
public void toLowerCase() {
insertRowStatementList.forEach(InsertRowStatement::toLowerCase);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ public IDeviceID getTableDeviceID(int rowIdx) {
}
if (deviceIDs[rowIdx] == null) {
String[] deviceIdSegments = new String[getIdColumnIndices().size() + 1];
deviceIdSegments[0] = this.devicePath.getFullPath();
deviceIdSegments[0] = this.getTableName();
for (int i = 0; i < getIdColumnIndices().size(); i++) {
final Integer columnIndex = getIdColumnIndices().get(i);
Object idSeg = ((Object[]) columns[columnIndex])[rowIdx];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1067,4 +1067,13 @@ protected IDeviceID toDeviceID(String[] nodes) {
}
return Factory.DEFAULT_FACTORY.create(segments);
}

public void toLowerCase() {
for (int i = 0; i < nodes.length; i++) {
nodes[i] = nodes[i].toLowerCase();
}
if (fullPath != null) {
fullPath = fullPath.toLowerCase();
}
}
}

0 comments on commit a32eef9

Please sign in to comment.