Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -232,11 +232,16 @@
private final IPartitionFetcher partitionFetcher;
private final ISchemaFetcher schemaFetcher;
private final IModelFetcher modelFetcher;
private final TreeAnalysisMutationJournal mutationJournal;

public AnalyzeVisitor(IPartitionFetcher partitionFetcher, ISchemaFetcher schemaFetcher) {
public AnalyzeVisitor(
IPartitionFetcher partitionFetcher,
ISchemaFetcher schemaFetcher,
TreeAnalysisMutationJournal mutationJournal) {
this.partitionFetcher = partitionFetcher;
this.schemaFetcher = schemaFetcher;
this.modelFetcher = ModelFetcher.getInstance();
this.mutationJournal = mutationJournal;
}

@Override
Expand Down Expand Up @@ -274,13 +279,24 @@
}

@Override
public Analysis visitQuery(QueryStatement queryStatement, MPPQueryContext context) {

Check warning on line 282 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 122 to 64, Complexity from 15 to 14, Nesting Level from 4 to 2, Number of Variables from 9 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ9PRaXRfVNoVDTzIzAM&open=AZ9PRaXRfVNoVDTzIzAM&pullRequest=18179
Analysis analysis = new Analysis();
analysis.setLastLevelUseWildcard(queryStatement.isLastLevelUseWildcard());

try {
// check for semantic errors
queryStatement.semanticCheck();
boolean originalCountTimeAggregation = queryStatement.isCountTimeAggregation();
try {
queryStatement.semanticCheck();
} finally {
// semanticCheck derives this flag from SELECT and writes it into QueryStatement. Record the
// rare actual change so a failed attempt or dispatch retry restores the parser-owned state;
// ordinary queries pay no journal entry or copy cost.
if (queryStatement.isCountTimeAggregation() != originalCountTimeAggregation) {
mutationJournal.recordCountTimeAggregationChange(
queryStatement, originalCountTimeAggregation);
}
}

context.initResultSetColumnMemoryTracking(
queryStatement.getSeriesLimit(),
Expand Down Expand Up @@ -311,14 +327,21 @@

if (deviceList.size() > 1
&& TemplatedAnalyze.canBuildPlanUseTemplate(
analysis, queryStatement, partitionFetcher, schemaTree, context, deviceList)) {
analysis,
queryStatement,
partitionFetcher,
schemaTree,
context,
deviceList,
mutationJournal)) {
// 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;
}

if (canPushDownLimitOffsetInGroupByTimeForDevice(queryStatement)) {
// remove the device which won't appear in resultSet after limit/offset
mutationJournal.recordLimitOffsetPushDown(queryStatement);
deviceList =
pushDownLimitOffsetInGroupByTimeForDevice(
deviceList, queryStatement, context.getZoneId());
Expand Down Expand Up @@ -460,7 +483,10 @@
queryStatement =
(QueryStatement)
concatPathRewriter.rewrite(
queryStatement, new PathPatternTree(queryStatement.useWildcard()), context);
queryStatement,
new PathPatternTree(queryStatement.useWildcard()),
context,
mutationJournal);
analysis.setRealStatement(queryStatement);

// request schema fetch API
Expand Down Expand Up @@ -521,24 +547,26 @@
Expression globalTimePredicate = null;
boolean hasValueFilter = false;
if (queryStatement.getWhereCondition() != null) {
WhereCondition whereCondition = queryStatement.getWhereCondition();
Expression predicate = whereCondition.getPredicate();

Pair<Expression, Boolean> resultPair =
PredicateUtils.extractGlobalTimePredicate(predicate, true, true);
globalTimePredicate = resultPair.left;
Expression predicate = queryStatement.getWhereCondition().getPredicate();
PredicateUtils.GlobalTimePredicateExtractionResult extractionResult =
PredicateUtils.extractGlobalTimePredicate(predicate);
globalTimePredicate = extractionResult.getGlobalTimePredicate();
if (globalTimePredicate != null) {
globalTimePredicate = PredicateUtils.predicateRemoveNot(globalTimePredicate);
}
hasValueFilter = resultPair.right;

predicate = PredicateUtils.simplifyPredicate(predicate);

// set where condition to null if predicate is true or time filter.
if (!hasValueFilter || predicate.equals(ConstantOperand.TRUE)) {
queryStatement.setWhereCondition(null);
} else {
whereCondition.setPredicate(predicate);
hasValueFilter = extractionResult.hasValueFilter();

Expression residualPredicate =
PredicateUtils.simplifyPredicate(extractionResult.getResidualPredicate());

// Keep the parser-owned predicate untouched. The journal only saves its wrapper reference,
// while the analyzer installs a residual working predicate built by path-copying the nodes
// changed by time-filter extraction and simplification.
if (!hasValueFilter || residualPredicate.equals(ConstantOperand.TRUE)) {
mutationJournal.replaceWhereCondition(queryStatement, null);
} else if (residualPredicate != predicate) {
mutationJournal.replaceWhereCondition(
queryStatement, WhereCondition.fromNormalizedPredicate(residualPredicate));
}
}
analysis.setGlobalTimePredicate(globalTimePredicate);
Expand All @@ -565,7 +593,7 @@
return analyzeLastSourceAndDataPartition(analysis, selectExpressions, schemaTree, context);
}

private Analysis analyzeLastSourceAndDataPartition(

Check warning on line 596 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 73 to 64, Complexity from 15 to 14, Nesting Level from 5 to 2, Number of Variables from 20 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ9HcVNeywqgongkTFDI&open=AZ9HcVNeywqgongkTFDI&pullRequest=18179
Analysis analysis,
List<Expression> selectExpressions,
ISchemaTree schemaTree,
Expand Down Expand Up @@ -1810,7 +1838,7 @@
orderByExpressions.add(orderByExpression);
}
analysis.setOrderByExpressions(orderByExpressions);
queryStatement.updateSortItems(orderByExpressions);
mutationJournal.updateSortItems(queryStatement, orderByExpressions);
}

private void analyzeOrderBy(
Expand Down Expand Up @@ -1986,7 +2014,7 @@
}

analysis.setOrderByExpressions(deviceViewOrderByExpression);
queryStatement.updateSortItems(deviceViewOrderByExpression);
mutationJournal.updateSortItems(queryStatement, deviceViewOrderByExpression);
analysis.setDeviceToSortItems(deviceToSortItems);
analysis.setDeviceToOrderByExpressions(deviceToOrderByExpressions);
}
Expand Down Expand Up @@ -2315,7 +2343,7 @@
if (!queryStatement.isSelectInto()) {
return;
}
queryStatement.setOrderByComponent(null);
mutationJournal.clearOrderByComponent(queryStatement);

List<PartialPath> sourceDevices = new ArrayList<>(deviceSet);
List<Expression> sourceColumns =
Expand Down Expand Up @@ -2373,7 +2401,7 @@
if (!queryStatement.isSelectInto()) {
return;
}
queryStatement.setOrderByComponent(null);
mutationJournal.clearOrderByComponent(queryStatement);

List<Expression> sourceColumns =
outputExpressions.stream()
Expand Down Expand Up @@ -3267,19 +3295,19 @@
private void analyzeGlobalTimeConditionInShowMetaData(
WhereCondition timeCondition, Analysis analysis) {
Expression predicate = timeCondition.getPredicate();
Pair<Expression, Boolean> resultPair =
PredicateUtils.extractGlobalTimePredicate(predicate, true, true);
if (resultPair.right) {
PredicateUtils.GlobalTimePredicateExtractionResult extractionResult =
PredicateUtils.extractGlobalTimePredicate(predicate);
if (extractionResult.hasValueFilter()) {
throw new SemanticException(
DataNodeQueryMessages
.VALUE_FILTER_CAN_T_EXIST_IN_THE_CONDITION_OF_SHOW_COUNT_CLAUSE_ONLY_TIME_CONDITION);
}
if (resultPair.left == null) {
if (extractionResult.getGlobalTimePredicate() == null) {
throw new SemanticException(
DataNodeQueryMessages
.TIME_CONDITION_CAN_T_BE_EMPTY_IN_THE_CONDITION_OF_SHOW_COUNT_CLAUSE);
}
Expression globalTimePredicate = resultPair.left;
Expression globalTimePredicate = extractionResult.getGlobalTimePredicate();
globalTimePredicate = PredicateUtils.predicateRemoveNot(globalTimePredicate);
analysis.setGlobalTimePredicate(globalTimePredicate);
}
Expand Down Expand Up @@ -3859,12 +3887,10 @@
analysis.setSourceExpressions(sourceExpressions);
sourceExpressions.forEach(expression -> analyzeExpressionType(analysis, expression));

if (!analyzeWhereForShowStatements(
analyzeWhereForShowStatements(
analysis,
showQueriesStatement.getWhereCondition(),
ColumnHeaderConstant.showQueriesColumnHeaders)) {
showQueriesStatement.setWhereCondition(null);
}
ColumnHeaderConstant.showQueriesColumnHeaders);

analysis.setMergeOrderParameter(new OrderByParameter(showQueriesStatement.getSortItemList()));

Expand Down Expand Up @@ -3894,12 +3920,10 @@
analysis.setSourceExpressions(sourceExpressions);
sourceExpressions.forEach(expression -> analyzeExpressionType(analysis, expression));

if (!analyzeWhereForShowStatements(
analyzeWhereForShowStatements(
analysis,
showDiskUsageStatement.getWhereCondition(),
ColumnHeaderConstant.showDiskUsageColumnHeaders)) {
showDiskUsageStatement.setWhereCondition(null);
}
ColumnHeaderConstant.showDiskUsageColumnHeaders);

analysis.setMergeOrderParameter(new OrderByParameter(showDiskUsageStatement.getSortItemList()));

Expand Down Expand Up @@ -3927,17 +3951,15 @@
Expression predicate =
ExpressionAnalyzer.bindTypeForTimeSeriesOperand(
whereCondition.getPredicate(), statementColumnHeaders);
Pair<Expression, Boolean> resultPair =
PredicateUtils.extractGlobalTimePredicate(predicate, true, true);
boolean hasValueFilter = resultPair.getRight();
PredicateUtils.GlobalTimePredicateExtractionResult extractionResult =
PredicateUtils.extractGlobalTimePredicate(predicate);
boolean hasValueFilter = extractionResult.hasValueFilter();

predicate = PredicateUtils.simplifyPredicate(predicate);
predicate = PredicateUtils.simplifyPredicate(extractionResult.getResidualPredicate());

// set where condition to null if predicate is true or don't have value filter
if (!hasValueFilter || predicate.equals(ConstantOperand.TRUE)) {
return false;
} else {
whereCondition.setPredicate(predicate);
}
TSDataType outputType = analyzeExpressionType(analysis, predicate);
if (outputType != TSDataType.BOOLEAN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,28 @@

private final IPartitionFetcher partitionFetcher;
private final ISchemaFetcher schemaFetcher;
private final TreeAnalysisMutationJournal mutationJournal;

public Analyzer(
MPPQueryContext context, IPartitionFetcher partitionFetcher, ISchemaFetcher schemaFetcher) {
this(context, partitionFetcher, schemaFetcher, new TreeAnalysisMutationJournal());
}

public Analyzer(
MPPQueryContext context,
IPartitionFetcher partitionFetcher,
ISchemaFetcher schemaFetcher,
TreeAnalysisMutationJournal mutationJournal) {
this.context = context;
this.partitionFetcher = partitionFetcher;
this.schemaFetcher = schemaFetcher;
this.mutationJournal = mutationJournal;
}

public Analysis analyze(Statement statement) {
long startTime = System.nanoTime();
AnalyzeVisitor visitor = new AnalyzeVisitor(partitionFetcher, schemaFetcher);
mutationJournal.prepareForAnalyze();
AnalyzeVisitor visitor = new AnalyzeVisitor(partitionFetcher, schemaFetcher, mutationJournal);
Analysis analysis = null;
context.setReserveMemoryForSchemaTreeFunc(
mem -> {
Expand All @@ -53,6 +64,9 @@
});
try {
analysis = visitor.process(statement, context);
} catch (RuntimeException | Error e) {

Check warning on line 67 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/Analyzer.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Catch Exception instead of Error.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ9PRaVrfVNoVDTzIzAL&open=AZ9PRaVrfVNoVDTzIzAL&pullRequest=18179
mutationJournal.rollback();
throw e;
} finally {
if (analysis != null && context.releaseSchemaTreeAfterAnalyzing()) {
analysis.setSchemaTree(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public PathPatternTree getPatternTree() {
}

public Statement rewrite(
Statement statement, PathPatternTree patternTree, MPPQueryContext queryContext)
Statement statement,
PathPatternTree patternTree,
MPPQueryContext queryContext,
TreeAnalysisMutationJournal mutationJournal)
throws StatementAnalyzeException {
QueryStatement queryStatement = (QueryStatement) statement;
this.patternTree = patternTree;
Expand Down Expand Up @@ -79,20 +82,20 @@ public Statement rewrite(
// concat SELECT with FROM
List<ResultColumn> resultColumns =
concatSelectWithFrom(queryStatement.getSelectComponent(), prefixPaths, queryContext);
queryStatement.getSelectComponent().setResultColumns(resultColumns);
mutationJournal.replaceResultColumns(queryStatement, resultColumns);

// concat GROUP BY with FROM
if (queryStatement.hasGroupByExpression()) {
queryStatement
.getGroupByComponent()
.setControlColumnExpression(
contactGroupByWithFrom(
queryStatement.getGroupByComponent().getControlColumnExpression(),
prefixPaths,
queryContext));
mutationJournal.replaceGroupByControlExpression(
queryStatement,
contactGroupByWithFrom(
queryStatement.getGroupByComponent().getControlColumnExpression(),
prefixPaths,
queryContext));
}
if (queryStatement.hasOrderByExpression()) {
List<Expression> sortItemExpressions = queryStatement.getExpressionSortItemList();
List<Expression> sortItemExpressions =
mutationJournal.getMutableOrderByComponent(queryStatement).getExpressionSortItemList();
sortItemExpressions.replaceAll(
expression -> contactOrderByWithFrom(expression, prefixPaths, queryContext));
}
Expand Down
Loading
Loading