Skip to content

Commit

Permalink
Fix error in template + sort + offset + limit + align by device
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyyes authored Dec 19, 2024
1 parent 61fa8d6 commit d34511a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public class IoTDBAlignByDeviceWithTemplateIT {
"INSERT INTO root.sg2.d4(timestamp,s1,s2,s3) values(1,1111.1,true,1111), (5,5555.5,false,5555);",
};

String[] expectedHeader;
String[] retArray;

@BeforeClass
public static void setUp() throws Exception {
EnvFactory.getEnv().initClusterEnvironment();
Expand All @@ -70,11 +73,32 @@ public static void tearDown() throws Exception {
EnvFactory.getEnv().cleanClusterEnvironment();
}

@Test
public void singleDeviceTest() {
expectedHeader = new String[] {"Time,Device,s3,s1,s2"};
retArray =
new String[] {
"1,root.sg1.d1,1,1.1,false,",
};
resultSetEqualTest(
"SELECT * FROM root.sg1.d1 order by time desc offset 1 limit 1 ALIGN BY DEVICE;",
expectedHeader,
retArray);
retArray =
new String[] {
"1,root.sg2.d1,1,1.1,false,",
};
resultSetEqualTest(
"SELECT * FROM root.sg2.d1 order by time desc offset 1 limit 1 ALIGN BY DEVICE;",
expectedHeader,
retArray);
}

@Test
public void selectWildcardNoFilterTest() {
// 1. order by device
String[] expectedHeader = new String[] {"Time,Device,s3,s1,s2"};
String[] retArray =
expectedHeader = new String[] {"Time,Device,s3,s1,s2"};
retArray =
new String[] {
"1,root.sg1.d1,1,1.1,false,",
"2,root.sg1.d1,2,2.2,false,",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,16 @@ public Analysis visitQuery(QueryStatement queryStatement, MPPQueryContext contex

List<Pair<Expression, String>> outputExpressions;
if (queryStatement.isAlignByDevice()) {
if (TemplatedAnalyze.canBuildPlanUseTemplate(
analysis, queryStatement, partitionFetcher, schemaTree, context)) {
List<PartialPath> deviceList = analyzeFrom(queryStatement, schemaTree);

if (deviceList.size() > 1
&& TemplatedAnalyze.canBuildPlanUseTemplate(
analysis, queryStatement, partitionFetcher, schemaTree, context, deviceList)) {
// when device size is less than 1, there is no need to use template optimization, i.e. no
// need to extract common variables
return analysis;
}

List<PartialPath> deviceList = analyzeFrom(queryStatement, schemaTree);

if (canPushDownLimitOffsetInGroupByTimeForDevice(queryStatement)) {
// remove the device which won't appear in resultSet after limit/offset
deviceList =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import static org.apache.iotdb.db.queryengine.plan.analyze.TemplatedAnalyze.analyzeDataPartition;
import static org.apache.iotdb.db.queryengine.plan.analyze.TemplatedAnalyze.analyzeDeviceToWhere;
import static org.apache.iotdb.db.queryengine.plan.analyze.TemplatedAnalyze.analyzeDeviceViewOutput;
import static org.apache.iotdb.db.queryengine.plan.analyze.TemplatedAnalyze.analyzeFrom;
import static org.apache.iotdb.db.queryengine.plan.optimization.LimitOffsetPushDown.canPushDownLimitOffsetInGroupByTimeForDevice;
import static org.apache.iotdb.db.queryengine.plan.optimization.LimitOffsetPushDown.pushDownLimitOffsetInGroupByTimeForDevice;
import static org.apache.iotdb.db.utils.constant.SqlConstant.COUNT_TIME;
Expand All @@ -68,7 +67,8 @@ static boolean canBuildAggregationPlanUseTemplate(
IPartitionFetcher partitionFetcher,
ISchemaTree schemaTree,
MPPQueryContext context,
Template template) {
Template template,
List<PartialPath> deviceList) {

// not support order by expression and non-aligned template
if (queryStatement.hasOrderByExpression() || !template.isDirectAligned()) {
Expand All @@ -77,8 +77,6 @@ static boolean canBuildAggregationPlanUseTemplate(

analysis.setNoWhereAndAggregation(false);

List<PartialPath> deviceList = analyzeFrom(queryStatement, schemaTree);

if (canPushDownLimitOffsetInGroupByTimeForDevice(queryStatement)) {
// remove the device which won't appear in resultSet after limit/offset
deviceList =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public static boolean canBuildPlanUseTemplate(
QueryStatement queryStatement,
IPartitionFetcher partitionFetcher,
ISchemaTree schemaTree,
MPPQueryContext context) {
MPPQueryContext context,
List<PartialPath> deviceList) {
if (queryStatement.getGroupByComponent() != null
|| queryStatement.isSelectInto()
|| queryStatement.hasFill()
Expand All @@ -114,7 +115,7 @@ public static boolean canBuildPlanUseTemplate(

if (queryStatement.isAggregationQuery()) {
return canBuildAggregationPlanUseTemplate(
analysis, queryStatement, partitionFetcher, schemaTree, context, template);
analysis, queryStatement, partitionFetcher, schemaTree, context, template, deviceList);
}

List<Pair<Expression, String>> outputExpressions = new ArrayList<>();
Expand Down Expand Up @@ -174,8 +175,6 @@ public static boolean canBuildPlanUseTemplate(

analyzeSelect(queryStatement, analysis, outputExpressions, template);

List<PartialPath> deviceList = analyzeFrom(queryStatement, schemaTree);

analyzeDeviceToWhere(analysis, queryStatement);
if (analysis.getWhereExpression() != null
&& analysis.getWhereExpression().equals(ConstantOperand.FALSE)) {
Expand Down

0 comments on commit d34511a

Please sign in to comment.