ankitsultana commented on code in PR #14611: URL: https://github.com/apache/pinot/pull/14611#discussion_r1878237795
########## pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/timeseries/serde/TimeSeriesBlockSerde.java: ########## @@ -0,0 +1,203 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.query.runtime.timeseries.serde; + +import com.google.common.base.Preconditions; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.time.Duration; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import org.apache.pinot.common.datablock.DataBlock; +import org.apache.pinot.common.datablock.DataBlockUtils; +import org.apache.pinot.common.utils.DataSchema; +import org.apache.pinot.common.utils.DataSchema.ColumnDataType; +import org.apache.pinot.query.runtime.blocks.TransferableBlock; +import org.apache.pinot.query.runtime.blocks.TransferableBlockUtils; +import org.apache.pinot.tsdb.spi.TimeBuckets; +import org.apache.pinot.tsdb.spi.series.TimeSeries; +import org.apache.pinot.tsdb.spi.series.TimeSeriesBlock; + + +/** + * Implements a simple Serde mechanism for the Time Series Block. This is used for transferring data between servers + * and brokers. The approach is to use a {@link TransferableBlock} and rely on the existing serialization code to avoid + * re-inventing the wheel. Once the time-series engine coalesces with the Multistage Engine, we will anyway use + * TransferableBlock for data transfers. + * <p> + * The {@link TimeSeriesBlock} is converted to and from a table, where the first row contains information about the + * time-buckets. For each tag/label in the query, there's a dedicated column, and the Double values are stored in + * the last column. As an example, consider the following, where FBV represents the first bucket value of TimeBuckets. + * <pre> + * +-------------+------------+-------------+---------------------------------+ + * | tag-0 | tag-1 | tag-n | values | + * +-------------+------------+-------------+---------------------------------+ + * | null | null | null | [FBV, bucketSize, numBuckets] | + * +-------------+------------+-------------+---------------------------------+ + * | Chicago | 60607 | ... | [value-0, value-1, ... value-x] | + * +-------------+------------+-------------+---------------------------------+ + * | San Fran. | 94107 | ... | [value-0, value-1, ... value-x] | + * +-------------+------------+-------------+---------------------------------+ + * </pre> + * TODO(timeseries): When we support Time Series selection queries, we will likely need a special column instead of + * tags, because one could store data in JSON Blobs and the series may have different tags/labels. + * </p> + * <p> + * TODO(timeseries): One source of inefficiency is boxing/unboxing of Double arrays. + * TODO(timeseries): The other is tag values being Object[]. We should make tag values String[]. + * </p> + */ +public class TimeSeriesBlockSerde { + /** + * Since DataBlock can only handle primitive double[] arrays, we use Double.MIN_VALUE to represent nulls. + * Using Double.MIN_VALUE is better than using Double.NaN since Double.NaN can help detect divide by 0. + * TODO(timeseries): Check if we can get rid of boxed Doubles altogether. + */ + private static final double NULL_PLACEHOLDER = Double.MIN_VALUE; + + private TimeSeriesBlockSerde() { + } + + public static TimeSeriesBlock deserializeTimeSeriesBlock(ByteBuffer readOnlyByteBuffer) + throws IOException { + DataBlock dataBlock = DataBlockUtils.readFrom(readOnlyByteBuffer); + TransferableBlock transferableBlock = TransferableBlockUtils.wrap(dataBlock); + List<String> tagNames = generateTagNames(Objects.requireNonNull(transferableBlock.getDataSchema(), + "Missing data schema in TransferableBlock")); + List<Object[]> container = transferableBlock.getContainer(); + TimeBuckets timeBuckets = timeBucketsFromRow(container.get(0)); + Map<Long, List<TimeSeries>> seriesMap = new HashMap<>(); + for (int index = 1; index < container.size(); index++) { + Object[] row = container.get(index); + TimeSeries timeSeries = timeSeriesFromRow(tagNames, row, timeBuckets); + long seriesId = Long.parseLong(timeSeries.getId()); + seriesMap.computeIfAbsent(seriesId, (x) -> new ArrayList<>()).add(timeSeries); + } + return new TimeSeriesBlock(timeBuckets, seriesMap); + } + + public static ByteString serializeTimeSeriesBlock(TimeSeriesBlock timeSeriesBlock) + throws IOException { + TimeBuckets timeBuckets = Objects.requireNonNull(timeSeriesBlock.getTimeBuckets()); + List<Object[]> container = new ArrayList<>(); + DataSchema dataSchema = generateDataSchema(timeSeriesBlock); + container.add(timeBucketsToRow(timeBuckets, dataSchema)); + for (var entry : timeSeriesBlock.getSeriesMap().entrySet()) { + for (TimeSeries timeSeries : entry.getValue()) { + container.add(timeSeriesToRow(timeSeries, dataSchema)); + } + } + TransferableBlock transferableBlock = new TransferableBlock(container, dataSchema, DataBlock.Type.ROW); + return DataBlockUtils.toByteString(transferableBlock.getDataBlock()); + } + + // Internal methods below + + private static DataSchema generateDataSchema(TimeSeriesBlock timeSeriesBlock) { + TimeSeries sampledTimeSeries = sampleTimeSeries(timeSeriesBlock).orElse(null); + int numTags = sampledTimeSeries == null ? 0 : sampledTimeSeries.getTagNames().size(); + ColumnDataType[] dataTypes = new ColumnDataType[numTags + 1]; + String[] columnNames = new String[numTags + 1]; + for (int tagIndex = 0; tagIndex < numTags; tagIndex++) { + columnNames[tagIndex] = sampledTimeSeries.getTagNames().get(tagIndex); + dataTypes[tagIndex] = ColumnDataType.STRING; + } + columnNames[numTags] = "__ts_values"; + dataTypes[numTags] = ColumnDataType.DOUBLE_ARRAY; + return new DataSchema(columnNames, dataTypes); + } + + private static List<String> generateTagNames(DataSchema dataSchema) { + String[] columnNames = dataSchema.getColumnNames(); + List<String> tagNames = new ArrayList<>(columnNames.length - 1); + for (int index = 0; index < columnNames.length - 1; index++) { + tagNames.add(columnNames[index]); + } + return tagNames; + } + + private static Optional<TimeSeries> sampleTimeSeries(TimeSeriesBlock timeSeriesBlock) { + if (timeSeriesBlock.getSeriesMap().isEmpty()) { + return Optional.empty(); + } + List<TimeSeries> timeSeriesList = timeSeriesBlock.getSeriesMap().values().iterator().next(); + Preconditions.checkState(!timeSeriesList.isEmpty(), "Found empty time-series list"); + return Optional.of(timeSeriesList.get(0)); + } + + private static Object[] timeBucketsToRow(TimeBuckets timeBuckets, DataSchema dataSchema) { Review Comment: Right now all of these methods are private hence keeping them here. For the foreseeable future we shouldn't need them outside of this class. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org