Repository navigation
[Bug] Fix SeriesScanUtil/AlignedSeriesScanUtil correctness bugs and remove dead streamingQueryMemChunk config #19445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
52500a9
7734fa2
624424c
afe1c49
5ce8ed4
da61336
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,24 +33,22 @@ | |
| import org.apache.tsfile.enums.TSDataType; | ||
| import org.apache.tsfile.file.metadata.AbstractAlignedTimeSeriesMetadata; | ||
| import org.apache.tsfile.file.metadata.IMetadata; | ||
| import org.apache.tsfile.file.metadata.statistics.Statistics; | ||
| import org.apache.tsfile.read.common.block.TsBlock; | ||
| import org.apache.tsfile.read.reader.IPointReader; | ||
| import org.apache.tsfile.write.schema.IMeasurementSchema; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class AlignedSeriesScanUtil extends SeriesScanUtil { | ||
|
|
||
| private final List<TSDataType> dataTypes; | ||
|
|
||
| // only used for limit and offset push down optimizer, if we select all columns from aligned | ||
| // device, we can use statistics to skip. | ||
| // it's only exact while using limit & offset push down | ||
| // for table scan, it should always be true | ||
| private final boolean queryAllSensors; | ||
|
|
||
| // for table model, it will be false | ||
| // for tree model, it will be true | ||
| private final boolean ignoreAllNullRows; | ||
|
|
@@ -60,15 +58,14 @@ public AlignedSeriesScanUtil( | |
| Ordering scanOrder, | ||
| SeriesScanOptions scanOptions, | ||
| FragmentInstanceContext context) { | ||
| this(seriesPath, scanOrder, scanOptions, context, false, null); | ||
| this(seriesPath, scanOrder, scanOptions, context, null); | ||
| } | ||
|
|
||
| public AlignedSeriesScanUtil( | ||
| AlignedFullPath seriesPath, | ||
| Ordering scanOrder, | ||
| SeriesScanOptions scanOptions, | ||
| FragmentInstanceContext context, | ||
| boolean queryAllSensors, | ||
| List<TSDataType> givenDataTypes) { | ||
| super(seriesPath, scanOrder, scanOptions, context); | ||
| isAligned = true; | ||
|
|
@@ -78,7 +75,6 @@ public AlignedSeriesScanUtil( | |
| : seriesPath.getSchemaList().stream() | ||
| .map(IMeasurementSchema::getType) | ||
| .collect(Collectors.toList()); | ||
| this.queryAllSensors = queryAllSensors; | ||
| this.ignoreAllNullRows = context.isIgnoreAllNullRows(); | ||
| } | ||
|
|
||
|
|
@@ -116,10 +112,153 @@ protected IPointReader getPointReader(TsBlock tsBlock) { | |
| } | ||
|
|
||
| @Override | ||
| protected boolean timeAllSelected(IMetadata metadata) { | ||
| if (queryAllSensors || metadata.getMeasurementCount() == 0) { | ||
| public boolean canUseCurrentFileTimeStatistics() { | ||
| return canUseCurrentFileStatistics() && canUseAlignedTimeStatistics(firstTimeSeriesMetadata); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canUseCurrentChunkTimeStatistics() { | ||
| return canUseCurrentChunkStatistics() && canUseAlignedTimeStatistics(firstChunkMetadata); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canUseCurrentPageTimeStatistics() throws IOException { | ||
| return canUseCurrentPageStatistics() | ||
| && firstPageReader != null | ||
| && !(firstPageReader instanceof LazyMemVersionPageReader) | ||
| && canUseAlignedTimeStatistics(firstPageReader.getPageReader()); | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("squid:S3740") | ||
| public Statistics currentFileTimeStatistics() { | ||
| return getAlignedTimeStatistics(firstTimeSeriesMetadata, super.currentFileTimeStatistics()); | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("squid:S3740") | ||
| public Statistics currentChunkTimeStatistics() { | ||
| return getAlignedTimeStatistics(firstChunkMetadata, super.currentChunkTimeStatistics()); | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("squid:S3740") | ||
| public Statistics currentPageTimeStatistics() { | ||
| if (firstPageReader == null || firstPageReader instanceof LazyMemVersionPageReader) { | ||
| return super.currentPageTimeStatistics(); | ||
| } | ||
| return getAlignedTimeStatistics( | ||
| firstPageReader.getPageReader(), super.currentPageTimeStatistics()); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean canUseTimeStatistics(IMetadata metadata) { | ||
| return canUseAlignedTimeStatistics(metadata); | ||
| } | ||
|
|
||
| private boolean canUseAlignedTimeStatistics(IMetadata metadata) { | ||
| if (!ignoreAllNullRows || metadata == null) { | ||
| return true; | ||
| } | ||
| return timeAllSelected(metadata) || hasSingleNonNullMeasurement(metadata); | ||
| } | ||
|
|
||
| private boolean hasSingleNonNullMeasurement(IMetadata metadata) { | ||
| int nonNullCount = 0; | ||
| for (int i = 0, count = metadata.getMeasurementCount(); i < count; i++) { | ||
| Statistics<?> valueStats = metadata.getMeasurementStatistics(i).orElse(null); | ||
| if (isNonEmpty(valueStats) && ++nonNullCount > 1) { | ||
| return false; | ||
| } | ||
| } | ||
| return nonNullCount == 1; | ||
| } | ||
|
|
||
| @SuppressWarnings("squid:S3740") | ||
| private Statistics getAlignedTimeStatistics(IMetadata metadata, Statistics timeStatistics) { | ||
| if (!ignoreAllNullRows || metadata == null || timeStatistics == null) { | ||
| return timeStatistics; | ||
| } | ||
| int measurementCount = metadata.getMeasurementCount(); | ||
| if (measurementCount <= 0) { | ||
| return timeStatistics; | ||
| } | ||
| Statistics singleNonNullMeasurementStatistics = null; | ||
| int nonNullCount = 0; | ||
| for (int i = 0; i < measurementCount; i++) { | ||
| Statistics valueStats = metadata.getMeasurementStatistics(i).orElse(null); | ||
| if (isNonEmpty(valueStats)) { | ||
| if (++nonNullCount > 1) { | ||
| return timeStatistics; | ||
| } | ||
| singleNonNullMeasurementStatistics = valueStats; | ||
| } | ||
| } | ||
| return nonNullCount == 1 ? singleNonNullMeasurementStatistics : timeStatistics; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Reject
For example, on a |
||
| } | ||
|
|
||
| private boolean timeAllSelected(IMetadata metadata) { | ||
| if (!ignoreAllNullRows || metadata.getMeasurementCount() == 0) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Preserve OFFSET skipping for a single sparse column After dropping |
||
| return true; | ||
| } | ||
| return metadata.timeAllSelected(); | ||
| return metadata.timeAllSelected() || allTimestampsCoveredByDisjointMeasurements(metadata); | ||
| } | ||
|
|
||
| private boolean allTimestampsCoveredByDisjointMeasurements(IMetadata metadata) { | ||
| Statistics<?> timeStats = metadata.getTimeStatistics(); | ||
| int measurementCount = metadata.getMeasurementCount(); | ||
| if (timeStats == null || timeStats.isEmpty() || measurementCount < 2) { | ||
| return false; | ||
| } | ||
| long expectedCount = timeStats.getCount(); | ||
| if (measurementCount == 2) { | ||
| Statistics<?> first = metadata.getMeasurementStatistics(0).orElse(null); | ||
| Statistics<?> second = metadata.getMeasurementStatistics(1).orElse(null); | ||
| return isNonEmpty(first) | ||
| && isNonEmpty(second) | ||
| && first.getCount() == expectedCount - second.getCount() | ||
| && areDisjoint(first, second); | ||
| } | ||
| List<Statistics<?>> nonEmptyStats = collectCoveredMeasurementStats(metadata, expectedCount); | ||
| return !nonEmptyStats.isEmpty() && areAllDisjoint(nonEmptyStats); | ||
| } | ||
|
|
||
| private static List<Statistics<?>> collectCoveredMeasurementStats( | ||
| IMetadata metadata, long expectedCount) { | ||
| int measurementCount = metadata.getMeasurementCount(); | ||
| List<Statistics<?>> nonEmptyStats = new ArrayList<>(measurementCount); | ||
| long countSum = 0; | ||
| for (int i = 0; i < measurementCount; i++) { | ||
| Statistics<?> valueStats = metadata.getMeasurementStatistics(i).orElse(null); | ||
| if (!isNonEmpty(valueStats)) { | ||
| continue; | ||
| } | ||
| if (valueStats.getCount() > expectedCount - countSum) { | ||
| return Collections.emptyList(); | ||
| } | ||
| countSum += valueStats.getCount(); | ||
| nonEmptyStats.add(valueStats); | ||
| } | ||
| return countSum == expectedCount && nonEmptyStats.size() >= 2 | ||
| ? nonEmptyStats | ||
| : Collections.emptyList(); | ||
| } | ||
|
|
||
| private static boolean areAllDisjoint(List<Statistics<?>> statsList) { | ||
| statsList.sort(Comparator.comparingLong(Statistics::getStartTime)); | ||
| for (int i = 1; i < statsList.size(); i++) { | ||
| if (statsList.get(i - 1).getEndTime() >= statsList.get(i).getStartTime()) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private static boolean areDisjoint(Statistics<?> first, Statistics<?> second) { | ||
| return first.getEndTime() < second.getStartTime() || second.getEndTime() < first.getStartTime(); | ||
| } | ||
|
|
||
| private static boolean isNonEmpty(Statistics<?> statistics) { | ||
| return statistics != null && !statistics.isEmpty(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
queryAllSensorsis no longer read anywhere, but it is still a constructor parameter andAlignedSeriesScanOperatorandExternalTsFileSeriesScanUtilstill pass it. Suggest removing it so that nobody assumes the old shortcut can still be switched on.