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 @@ -73,6 +73,7 @@ public static class Indexes {
public static final String VECTOR_HNSW_INDEX_DOCID_MAPPING_FILE_EXTENSION = ".vector.hnsw.mapping";
public static final String VECTOR_IVF_FLAT_INDEX_FILE_EXTENSION = ".vector.ivfflat.index";
public static final String VECTOR_IVF_PQ_INDEX_FILE_EXTENSION = ".vector.ivfpq.index";
public static final String COLUMNAR_MAP_INDEX_FILE_EXTENSION = ".columnarmap.idx";
}

public static class MetadataKeys {
Expand Down Expand Up @@ -167,6 +168,16 @@ public static class Column {
// Optional, default false
public static final String IS_AUTO_GENERATED = "isAutoGenerated";

// Optional, default false. True for materialized columns produced from a COLUMNAR_MAP parent column.
// Example: for a MAP column "metrics", materialized column "value_map_string$__latency" has:
// mapMaterializedColumn = true
// parentMapColumn = metrics
// Materialized columns appear in index_map alongside regular columns with their own forward index,
// optional inverted index, and null-value vector.
public static final String IS_MAP_MATERIALIZED_COLUMN = "mapMaterializedColumn";
// Optional. The parent MAP column name for columns materialized from a COLUMNAR_MAP index.
public static final String PARENT_MAP_COLUMN = "parentMapColumn";

/// Partition function, all optional
public static final String PARTITION_FUNCTION = "partitionFunction";
public static final String NUM_PARTITIONS = "numPartitions";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,23 @@ public List<String> getComplexColumnNames() {
return getQualifyingFields(FieldType.COMPLEX, true);
}

/// Returns the names of columns that have COLUMNAR_MAP index enabled in the table config.
public List<String> getColumnarMapColumnNames() {
List<String> result = new ArrayList<>();
if (_tableConfig != null) {
List<FieldConfig> fieldConfigs = _tableConfig.getFieldConfigList();
if (fieldConfigs != null) {
for (FieldConfig fieldConfig : fieldConfigs) {
List<FieldConfig.IndexType> indexTypes = fieldConfig.getIndexTypes();
if (indexTypes != null && indexTypes.contains(FieldConfig.IndexType.COLUMNAR_MAP)) {
result.add(fieldConfig.getName());
}
}
}
}
return result;
}

public void setSegmentPartitionConfig(SegmentPartitionConfig segmentPartitionConfig) {
_segmentPartitionConfig = segmentPartitionConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@

public interface MapDataSource extends DataSource {

/**
* Get the map FieldSpec.
*/
/// Returns the map FieldSpec.
ComplexFieldSpec.MapFieldSpec getFieldSpec();

/**
* Get the Data Source representation of a single key within this map column.
*/
/// Returns the DataSource for the given map key. For absent keys, returns a [NullDataSource]
/// that produces the column default value for every document. Callers need not null-check.
DataSource getKeyDataSource(String key);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not introduced in this PR, but let's rename it to:

Suggested change
DataSource getKeyDataSource(String key);
@Nullable
DataSource getDataSource(String key);

It is very confusing now because the data source is for value, not key.
Suggest letting it return @Nullable to represent key not exist

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on the naming — getDataSource(key) is cleaner. I'll do the full getKeyXXX → getXXX rename across the SPI in a separate refactoring PR so it's one clean sweep rather than scattered across the stack. Will raise a new PR for this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For @nullable: I'd prefer to keep the current non-null contract backed by NullDataSource. The callers in ProjectionBlock and ItemTransformFunction dereference the result immediately without a null-check — NullDataSource lets them do that safely by returning the column's default value for every doc.

If we switch to @nullable, those two callers need null-guards, and so does any future caller. NullDataSource gives the same semantic (absent key → null/default for all rows) without pushing null-handling into every call site.
Happy to revisit this if you strognly feel we should add @nullable.

Let me know your thoughts.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the API names here - #18437


/**
* Get the Data Source representation of all keys within this map column.
*/
/// Returns whether this segment MAY contain the given key. Implementations are allowed to return
/// `true` conservatively (i.e., when it is not possible to determine key presence without a
/// full scan). Callers must handle the case where the key is absent even when this returns
/// `true` — [#getKeyDataSource(String)] will return a DataSource for an absent key
/// (forward-index reads return the column default value; null-value bitmap marks all rows as null).
default boolean containsKey(String key) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(optional) This is probably not needed if we make getDataSource return @Nullable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replied above.

For https://github.com/nullable: I'd prefer to keep the current non-null contract backed by NullDataSource. The callers in ProjectionBlock and ItemTransformFunction dereference the result immediately without a null-check — NullDataSource lets them do that safely by returning the column's default value for every doc.

If we switch to https://github.com/nullable, those two callers need null-guards, and so does any future caller. NullDataSource gives the same semantic (absent key → null/default for all rows) without pushing null-handling into every call site.
Happy to revisit this if you strognly feel we should add https://github.com/nullable.

Let me know your thoughts.

return getKeyDataSources().containsKey(key);
}

/// Returns DataSources for all keys present in this segment.
Map<String, DataSource> getKeyDataSources();

/**
* Get the DataSourceMetadata of a single key within this map column.
*/
/// Returns the DataSourceMetadata for the given key.
DataSourceMetadata getKeyDataSourceMetadata(String key);

/**
* Get the IndexContainer of a single key within this map column.
*/
/// Returns the ColumnIndexContainer for the given key.
ColumnIndexContainer getKeyIndexContainer(String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.pinot.segment.spi.index;

import org.apache.pinot.segment.spi.index.creator.BloomFilterCreator;
import org.apache.pinot.segment.spi.index.creator.ColumnarMapIndexCreator;
import org.apache.pinot.segment.spi.index.creator.CombinedInvertedIndexCreator;
import org.apache.pinot.segment.spi.index.creator.DictionaryBasedInvertedIndexCreator;
import org.apache.pinot.segment.spi.index.creator.FSTIndexCreator;
Expand All @@ -31,6 +32,7 @@
import org.apache.pinot.segment.spi.index.creator.VectorIndexConfig;
import org.apache.pinot.segment.spi.index.creator.VectorIndexCreator;
import org.apache.pinot.segment.spi.index.reader.BloomFilterReader;
import org.apache.pinot.segment.spi.index.reader.ColumnarMapIndexReader;
import org.apache.pinot.segment.spi.index.reader.Dictionary;
import org.apache.pinot.segment.spi.index.reader.ForwardIndexReader;
import org.apache.pinot.segment.spi.index.reader.H3IndexReader;
Expand All @@ -41,6 +43,7 @@
import org.apache.pinot.segment.spi.index.reader.TextIndexReader;
import org.apache.pinot.segment.spi.index.reader.VectorIndexReader;
import org.apache.pinot.spi.config.table.BloomFilterConfig;
import org.apache.pinot.spi.config.table.ColumnarMapIndexConfig;
import org.apache.pinot.spi.config.table.IndexConfig;
import org.apache.pinot.spi.config.table.JsonIndexConfig;

Expand Down Expand Up @@ -79,6 +82,7 @@ public class StandardIndexes {
public static final String TEXT_ID = "text_index";
public static final String H3_ID = "h3_index";
public static final String VECTOR_ID = "vector_index";
public static final String COLUMNAR_MAP_ID = "columnar_map";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just call it MAP? Do you foresee other map types to be added in the future that doesn't go under this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already a MAP concept in the codebase. The existing MapIndexReader / MapDataSource / MapColumnPreIndexStatsCollector family represents the non-columnar MAP storage path. Naming the new index type MAP would collide with that established concept and make it unclear which path a given piece of code refers to.
Naming it columnar_map also calls out how it's stored clearly.


private StandardIndexes() {
}
Expand Down Expand Up @@ -142,4 +146,12 @@ public static IndexType<VectorIndexConfig, VectorIndexReader, VectorIndexCreator
return (IndexType<VectorIndexConfig, VectorIndexReader, VectorIndexCreator>)
IndexService.getInstance().get(VECTOR_ID);
}

/// Returns the COLUMNAR_MAP index type, which materializes MAP column keys as virtual columns.
@SuppressWarnings("unchecked")
public static IndexType<ColumnarMapIndexConfig, ColumnarMapIndexReader, ColumnarMapIndexCreator>
columnarMap() {
return (IndexType<ColumnarMapIndexConfig, ColumnarMapIndexReader, ColumnarMapIndexCreator>)
IndexService.getInstance().get(COLUMNAR_MAP_ID);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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.segment.spi.index.creator;

import java.io.IOException;
import java.util.Map;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.pinot.segment.spi.index.IndexCreator;


/// Creator for the COLUMNAR_MAP index. Accepts one map per document during segment creation and
/// decomposes it into per-key columnar storage on `seal()`.
///
/// Implementations are not thread-safe; callers must serialize `add` calls per creator instance.
///
/// The inherited `add(Object, int)` method from `IndexCreator` treats the first argument as the
/// map and the second as the docId, matching the column-major creator path.
public interface ColumnarMapIndexCreator extends IndexCreator {

/// Adds one document's map. Keys are routed to per-key columnar storage; declared-type keys are
/// coerced to those types, others use the configured default value type. An empty map is valid.
/// Callers must pass an empty map rather than `null`.
///
/// @param mapValue the document's map (non-null, may be empty)
/// @param docId document id, must be monotonically non-decreasing across calls
void add(Map<String, Object> mapValue, int docId)
throws IOException;

/// Returns metadata properties for the materialized columns this creator produced during `seal()`.
/// The framework merges the returned properties into the segment metadata.
/// Returns an empty map for creators that produce no materialized columns. Call after `seal()`.
default Map<String, PropertiesConfiguration> getMaterializedColumnMetadata() {
return Map.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class ColumnMetadataImpl implements ColumnMetadata {
private final PartitionFunction _partitionFunction;
private final Set<Integer> _partitions;
private final boolean _autoGenerated;
private final String _parentMapColumn;

/// List of longs, each encodes:
/// - 2 byte - numeric id of IndexType
Expand All @@ -84,7 +85,8 @@ private ColumnMetadataImpl(FieldSpec fieldSpec, int totalDocs, int cardinality,
@Nullable Comparable minValue, @Nullable Comparable maxValue, boolean minMaxValueInvalid,
int lengthOfShortestElement, int lengthOfLongestElement, boolean isAscii, int totalNumberOfEntries,
int maxNumberOfMultiValues, int bitsPerElement, @Nullable PartitionFunction partitionFunction,
@Nullable Set<Integer> partitions, boolean autoGenerated) {
@Nullable Set<Integer> partitions, boolean autoGenerated,
@Nullable String parentMapColumn) {
_fieldSpec = fieldSpec;
_totalDocs = totalDocs;
_cardinality = cardinality;
Expand All @@ -102,6 +104,7 @@ private ColumnMetadataImpl(FieldSpec fieldSpec, int totalDocs, int cardinality,
_partitionFunction = partitionFunction;
_partitions = partitions;
_autoGenerated = autoGenerated;
_parentMapColumn = parentMapColumn;
}

@Override
Expand Down Expand Up @@ -193,6 +196,17 @@ public boolean isAutoGenerated() {
return _autoGenerated;
}

/// Returns `true` if this column is a materialized column produced from a COLUMNAR_MAP parent column.
public boolean isMaterializedMapColumn() {
return _parentMapColumn != null;
}

/// Returns the name of the parent MAP column, or `null` if this is not a materialized column.
@Nullable
public String getParentMapColumn() {
return _parentMapColumn;
}

@Override
public long getIndexSizeFor(IndexType type) {
short indexId = IndexService.getInstance().getNumericId(type);
Expand Down Expand Up @@ -252,17 +266,20 @@ public boolean equals(Object o) {
&& _lengthOfLongestElement == that._lengthOfLongestElement && _isAscii == that._isAscii
&& _totalNumberOfEntries == that._totalNumberOfEntries
&& _maxNumberOfMultiValues == that._maxNumberOfMultiValues && _bitsPerElement == that._bitsPerElement
&& _autoGenerated == that._autoGenerated && Objects.equals(_fieldSpec, that._fieldSpec) && Objects.equals(
&& _autoGenerated == that._autoGenerated
&& Objects.equals(_fieldSpec, that._fieldSpec) && Objects.equals(
_minValue, that._minValue) && Objects.equals(_maxValue, that._maxValue) && Objects.equals(_partitionFunction,
that._partitionFunction) && Objects.equals(_partitions, that._partitions) && Objects.equals(_indexTypeSizeList,
that._indexTypeSizeList);
that._partitionFunction) && Objects.equals(_partitions, that._partitions)
&& Objects.equals(_parentMapColumn, that._parentMapColumn)
&& Objects.equals(_indexTypeSizeList, that._indexTypeSizeList);
}

@Override
public int hashCode() {
return Objects.hash(_fieldSpec, _totalDocs, _cardinality, _hasDictionary, _sorted, _minValue, _maxValue,
_minMaxValueInvalid, _lengthOfShortestElement, _lengthOfLongestElement, _isAscii, _totalNumberOfEntries,
_maxNumberOfMultiValues, _bitsPerElement, _partitionFunction, _partitions, _autoGenerated, _indexTypeSizeList);
_maxNumberOfMultiValues, _bitsPerElement, _partitionFunction, _partitions, _autoGenerated,
_parentMapColumn, _indexTypeSizeList);
}

@Override
Expand All @@ -273,7 +290,8 @@ public String toString() {
+ _lengthOfShortestElement + ", _lengthOfLongestElement=" + _lengthOfLongestElement + ", _isAscii=" + _isAscii
+ ", _totalNumberOfEntries=" + _totalNumberOfEntries + ", _maxNumberOfMultiValues=" + _maxNumberOfMultiValues
+ ", _bitsPerElement=" + _bitsPerElement + ", _partitionFunction=" + _partitionFunction + ", _partitions="
+ _partitions + ", _autoGenerated=" + _autoGenerated + ", _indexTypeSizeList=" + _indexTypeSizeList + '}';
+ _partitions + ", _autoGenerated=" + _autoGenerated
+ ", _parentMapColumn=" + _parentMapColumn + ", _indexTypeSizeList=" + _indexTypeSizeList + '}';
}

public static ColumnMetadataImpl fromPropertiesConfiguration(PropertiesConfiguration config, int totalDocs,
Expand All @@ -295,7 +313,8 @@ public static ColumnMetadataImpl fromPropertiesConfiguration(PropertiesConfigura
.setMaxNumberOfMultiValues(
config.getInt(Column.getKeyFor(column, Column.MAX_MULTI_VALUE_ELEMENTS), UNAVAILABLE))
.setBitsPerElement(config.getInt(Column.getKeyFor(column, Column.BITS_PER_ELEMENT), UNAVAILABLE))
.setAutoGenerated(config.getBoolean(Column.getKeyFor(column, Column.IS_AUTO_GENERATED), false));
.setAutoGenerated(config.getBoolean(Column.getKeyFor(column, Column.IS_AUTO_GENERATED), false))
.setParentMapColumn(config.getString(Column.getKeyFor(column, Column.PARENT_MAP_COLUMN), null));

// Set min/max value
DataType storedType = fieldSpec.getDataType().getStoredType();
Expand Down Expand Up @@ -383,9 +402,11 @@ public static FieldSpec generateFieldSpec(String column, PropertiesConfiguration
List<String> childFieldNames =
config.getList(String.class, Column.getKeyFor(column, Column.COMPLEX_CHILD_FIELD_NAMES));
Map<String, FieldSpec> childFieldSpecs = new HashMap<>();
for (String childField : childFieldNames) {
childFieldSpecs.put(childField,
generateFieldSpec(ComplexFieldSpec.getFullChildName(column, childField), config));
if (childFieldNames != null) {
for (String childField : childFieldNames) {
childFieldSpecs.put(childField,
generateFieldSpec(ComplexFieldSpec.getFullChildName(column, childField), config));
}
}
return new ComplexFieldSpec(fieldName, dataType, true, childFieldSpecs);
default:
Expand Down Expand Up @@ -451,6 +472,7 @@ public static class Builder {
private PartitionFunction _partitionFunction;
private Set<Integer> _partitions;
private boolean _autoGenerated;
private String _parentMapColumn;

public Builder setFieldSpec(FieldSpec fieldSpec) {
_fieldSpec = fieldSpec;
Expand Down Expand Up @@ -542,6 +564,11 @@ public Builder setAutoGenerated(boolean autoGenerated) {
return this;
}

public Builder setParentMapColumn(@Nullable String parentMapColumn) {
_parentMapColumn = parentMapColumn;
return this;
}

public ColumnMetadataImpl build() {
// Canonicalize length of shortest/longest element
DataType storedType = _fieldSpec.getDataType().getStoredType();
Expand All @@ -568,7 +595,8 @@ public ColumnMetadataImpl build() {

return new ColumnMetadataImpl(_fieldSpec, _totalDocs, _cardinality, _hasDictionary, _sorted, _minValue, _maxValue,
_minMaxValueInvalid, _lengthOfShortestElement, _lengthOfLongestElement, _isAscii, _totalNumberOfEntries,
_maxNumberOfMultiValues, _bitsPerElement, _partitionFunction, _partitions, _autoGenerated);
_maxNumberOfMultiValues, _bitsPerElement, _partitionFunction, _partitions, _autoGenerated,
_parentMapColumn);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* 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.segment.spi.index.reader;

import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import org.apache.pinot.segment.spi.index.IndexReader;
import org.apache.pinot.spi.data.FieldSpec.DataType;
import org.roaringbitmap.buffer.ImmutableRoaringBitmap;


/// Reader for the COLUMNAR_MAP index. Each indexed key is materialized as its own per-key
/// forward index plus a presence bitmap.
///
/// Implementations must be safe for concurrent reads. Mutable implementations may impose
/// a single-writer constraint; refer to the concrete implementation's Javadoc for details.
///
/// Per-key `DataSource` construction is the responsibility of the surrounding
/// `ColumnarMapDataSource` wrappers, not this reader. This interface exposes only
/// the primitives a wrapper needs (key set, type, presence bitmap, per-doc map view).
public interface ColumnarMapIndexReader extends IndexReader {

/// Returns the set of all indexed key names. Never null; empty if no keys are indexed.
Set<String> getKeys();

/// Returns the value DataType for the given key, or null if the key is not indexed.
@Nullable
DataType getValueType(String key);

/// Returns the number of documents that have a non-null value for the given key.
/// Returns 0 if the key is not indexed.
int getNumDocsWithKey(String key);

/// Returns the presence bitmap for the given key (docIds with non-null values).
/// Returns an empty bitmap if the key is not indexed. The returned bitmap must not be mutated.
ImmutableRoaringBitmap getPresenceBitmap(String key);

/// Reconstructs the full map for a single document from per-key data. Only keys with a
/// non-null value at `docId` appear in the result. Returns an empty map if the document has
/// no values; behavior for an out-of-range `docId` is implementation-defined.
Map<String, Object> getMap(int docId);

/// Returns whether the given key has an inverted index available. False if the key is not indexed.
default boolean hasInvertedIndex(String key) {
return false;
}

/// Returns sorted distinct values for the key from the inverted index, or null if no
/// inverted index is available (or the key is not indexed).
@Nullable
default String[] getDistinctValuesForKey(String key) {
return null;
}
}
Loading