Skip to content

Commit

Permalink
Fixed the bug of ID Like judgement in queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Caideyipi authored Dec 11, 2024
1 parent 6073d25 commit 21652cc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ public void testDevice() throws SQLException {
statement.execute(
"create table table0(region_id STRING ID, plant_id STRING ID, device_id STRING ID, model STRING ATTRIBUTE, temperature FLOAT MEASUREMENT, humidity DOUBLE MEASUREMENT)");
statement.execute(
"insert into table0(region_id, plant_id, device_id, model, temperature, humidity) values('1', '5', '3', 'A', 37.6, 111.1)");
"insert into table0(region_id, plant_id, device_id, model, temperature, humidity) values('1', '木兰', '3', 'A', 37.6, 111.1)");

// Test plain show / count
TestUtils.assertResultSetEqual(
statement.executeQuery("show devices from table0"),
"region_id,plant_id,device_id,model,",
Collections.singleton("1,5,3,A,"));
Collections.singleton("1,木兰,3,A,"));
TestUtils.assertResultSetEqual(
statement.executeQuery("count devices from table0"),
"count(devices),",
Expand All @@ -93,19 +93,19 @@ public void testDevice() throws SQLException {
statement.executeQuery(
"show devices from table0 where region_id between '0' and '2' and model = 'A'"),
"region_id,plant_id,device_id,model,",
Collections.singleton("1,5,3,A,"));
Collections.singleton("1,木兰,3,A,"));
// Test OR
TestUtils.assertResultSetEqual(
statement.executeQuery(
"count devices from table0 where region_id = '1' or plant_id like '%'"),
"count devices from table0 where region_id = '1' or plant_id like '%木兰'"),
"count(devices),",
Collections.singleton("1,"));
// Test complicated query
TestUtils.assertResultSetEqual(
statement.executeQuery(
"show devices from table0 where region_id < plant_id offset 0 limit 1"),
"region_id,plant_id,device_id,model,",
Collections.singleton("1,5,3,A,"));
Collections.singleton("1,木兰,3,A,"));
TestUtils.assertResultSetEqual(
statement.executeQuery("show devices from table0 where region_id < plant_id limit 0"),
"region_id,plant_id,device_id,model,",
Expand All @@ -116,25 +116,25 @@ public void testDevice() throws SQLException {
Collections.singleton("1,"));
TestUtils.assertResultSetEqual(
statement.executeQuery(
"count devices from table0 where substring(region_id, cast((cast(plant_id as int32) - 4) as int32), 1) < plant_id"),
"count devices from table0 where substring(region_id, cast((cast(device_id as int32) - 2) as int32), 1) < plant_id"),
"count(devices),",
Collections.singleton("1,"));
// Test get from cache
statement.executeQuery(
"select * from table0 where region_id = '1' and plant_id in ('3', '5') and device_id = '3'");
"select * from table0 where region_id = '1' and plant_id in ('木兰', '5') and device_id = '3'");
TestUtils.assertResultSetEqual(
statement.executeQuery(
"show devices from table0 where region_id = '1' and plant_id in ('3', '5') and device_id = '3' offset 0 limit ALL"),
"show devices from table0 where region_id = '1' and plant_id in ('3', '木兰') and device_id = '3' offset 0 limit ALL"),
"region_id,plant_id,device_id,model,",
Collections.singleton("1,5,3,A,"));
Collections.singleton("1,木兰,3,A,"));
TestUtils.assertResultSetEqual(
statement.executeQuery(
"show devices from table0 where region_id = '1' and plant_id in ('3', '5') and device_id = '3' offset 100000000000 limit 100000000000"),
"show devices from table0 where region_id = '1' and plant_id in ('木兰', '5') and device_id = '3' offset 100000000000 limit 100000000000"),
"region_id,plant_id,device_id,model,",
Collections.emptySet());
TestUtils.assertResultSetEqual(
statement.executeQuery(
"count devices from table0 where region_id = '1' and plant_id in ('3', '5') and device_id = '3'"),
"count devices from table0 where region_id = '1' and plant_id in ('3', '木兰') and device_id = '3'"),
"count(devices),",
Collections.singleton("1,"));
// Test filter
Expand All @@ -145,9 +145,9 @@ public void testDevice() throws SQLException {
// Test cache with complicated filter
TestUtils.assertResultSetEqual(
statement.executeQuery(
"show devices from table0 where region_id = '1' and plant_id in ('3', '5') and device_id = '3' and device_id between region_id and plant_id"),
"show devices from table0 where region_id = '1' and plant_id in ('木兰', '5') and device_id = '3' and device_id between region_id and plant_id"),
"region_id,plant_id,device_id,model,",
Collections.singleton("1,5,3,A,"));
Collections.singleton("1,木兰,3,A,"));

try {
statement.executeQuery("show devices from table2");
Expand Down Expand Up @@ -232,16 +232,16 @@ public void testDevice() throws SQLException {
statement.executeQuery(
"show devices from table0 where substring(region_id, 1, 1) in ('1', '2') and 1 + 1 = 2"),
"region_id,plant_id,device_id,model,",
Collections.singleton("1,5,3,null,"));
Collections.singleton("1,木兰,3,null,"));

// Test common result column
statement.execute(
"update table0 set model = substring(device_id, 1, 1) where cast(region_id as int32) + cast(plant_id as int32) = 6 and region_id = '1'");
"update table0 set model = substring(device_id, 1, 1) where cast(region_id as int32) + cast(device_id as int32) = 4 and region_id = '1'");
TestUtils.assertResultSetEqual(
statement.executeQuery(
"show devices from table0 where substring(region_id, 1, 1) in ('1', '2') and 1 + 1 = 2"),
"region_id,plant_id,device_id,model,",
Collections.singleton("1,5,3,3,"));
Collections.singleton("1,木兰,3,3,"));

// Test limit / offset from multi regions
statement.execute(
Expand All @@ -252,12 +252,12 @@ public void testDevice() throws SQLException {
// TODO: Reopen
if (false) {
// Test delete devices
statement.execute("delete devices from table0 where region_id = '1' and plant_id = '5'");
statement.execute("delete devices from table0 where region_id = '1' and plant_id = '木兰'");
TestUtils.assertResultSetSize(statement.executeQuery("show devices from table0"), 1);

// Test successfully invalidate cache
statement.execute(
"insert into table0(region_id, plant_id, device_id, model, temperature, humidity) values('1', '5', '3', 'A', 37.6, 111.1)");
"insert into table0(region_id, plant_id, device_id, model, temperature, humidity) values('1', '木兰', '3', 'A', 37.6, 111.1)");
TestUtils.assertResultSetSize(statement.executeQuery("show devices from table0"), 2);

// Test successfully delete data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public void testDeviceQuery() throws Exception {
attributeMap.put("cycle", "daily");
SchemaRegionTestUtil.createTableDevice(
schemaRegion, tableName, new String[] {"shandong", "p_1", "d_1"}, attributeMap);
SchemaRegionTestUtil.createTableDevice(
schemaRegion, tableName, new String[] {"广州", "p_2", "d_2"}, attributeMap);

List<IDeviceSchemaInfo> deviceSchemaInfoList =
SchemaRegionTestUtil.getTableDevice(
Expand Down Expand Up @@ -184,8 +186,17 @@ public void testDeviceQuery() throws Exception {
Arrays.asList(
new IdFilter(new InFilter(new HashSet<>(Arrays.asList("d_0", "d_1"))), 2),
new IdFilter(new LikeFilter("__1", Optional.empty()), 2)));

Assert.assertEquals(2, deviceSchemaInfoList.size());

// Test CN characters
deviceSchemaInfoList =
SchemaRegionTestUtil.getTableDevice(
schemaRegion,
tableName,
3,
Collections.singletonList(
new IdFilter(new NotFilter(new LikeFilter("广州%", Optional.empty())), 0)));
Assert.assertEquals(3, deviceSchemaInfoList.size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.apache.iotdb.commons.schema.filter.impl.values.LikeFilter;
import org.apache.iotdb.commons.schema.filter.impl.values.PreciseFilter;

import org.apache.tsfile.common.conf.TSFileConfig;

import java.util.Objects;

public class StringValueFilterVisitor extends SchemaFilterVisitor<String> {
Expand Down Expand Up @@ -78,7 +80,8 @@ public Boolean visitLikeFilter(final LikeFilter filter, final String context) {
if (Objects.isNull(context)) {
return null;
}
return filter.getPattern().getMatcher().match(context.getBytes(), 0, context.length());
final byte[] bytes = context.getBytes(TSFileConfig.STRING_CHARSET);
return filter.getPattern().getMatcher().match(bytes, 0, bytes.length);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public class LikeFilter extends SchemaFilter {
private final LikePattern pattern;

public LikeFilter(final String regex, Optional<Character> escape) {
public LikeFilter(final String regex, final Optional<Character> escape) {
this.pattern = LikePattern.compile(regex, escape);
}

Expand Down

0 comments on commit 21652cc

Please sign in to comment.