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 @@ -35,6 +35,7 @@
import org.apache.druid.segment.column.ColumnDescriptor;
import org.apache.druid.segment.column.ColumnHolder;
import org.apache.druid.segment.column.ColumnType;
import org.apache.druid.segment.column.ConstantColumns;
import org.apache.druid.segment.column.ValueType;
import org.apache.druid.segment.data.Indexed;
import org.apache.druid.segment.data.ListIndexed;
Expand All @@ -55,6 +56,7 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -456,7 +458,7 @@ public List<TableClusterGroupSpec> getClusterGroupSchemas()
* cursor-factory level via {@code ClusteringColumnSelectorFactory}.
*/
@Override
public QueryableIndex getClusterGroupQueryableIndex(TableClusterGroupSpec groupSpec)
public QueryableIndex getClusterGroupQueryableIndex(TableClusterGroupSpec groupSpec, boolean withClusteringColumns)
{
if (clusteredBaseSummary == null) {
throw DruidException.defensive("getClusterGroupQueryableIndex called on a non-clustered segment");
Expand All @@ -466,7 +468,7 @@ public QueryableIndex getClusterGroupQueryableIndex(TableClusterGroupSpec groupS
if (groupIndex < 0) {
throw DruidException.defensive("Cluster group spec is not part of this segment");
}
final Map<String, Supplier<BaseColumnHolder>> groupColumns = clusterGroupColumnsByIndex.computeIfAbsent(
final Map<String, Supplier<BaseColumnHolder>> baseColumns = clusterGroupColumnsByIndex.computeIfAbsent(
groupIndex,
i -> buildColumnSuppliers(
clusteredBaseSummary.getTimeColumnName(),
Expand All @@ -476,6 +478,20 @@ public QueryableIndex getClusterGroupQueryableIndex(TableClusterGroupSpec groupS
Map.of()
)
);

final Map<String, Supplier<BaseColumnHolder>> groupColumns;
if (withClusteringColumns) {
groupColumns = new HashMap<>(baseColumns);
ConstantColumns.addConstantClusteringColumns(
groupColumns,
clusteredBaseSummary.getClusteringColumns(),
groupSpec.lookupClusteringValues(),
groupSpec.getNumRows(),
bitmapFactory
);
} else {
groupColumns = baseColumns;
}
final Metadata groupMetadata = new Metadata(
null,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private AsyncCursorHolder makeClusteredCursorHolderAsync(CursorBuildSpec spec)

final List<DownloadBundle> bundles = new ArrayList<>(surviving.size());
for (TableClusterGroupSpec group : surviving) {
final QueryableIndex groupIndex = index.getClusterGroupQueryableIndex(group);
final QueryableIndex groupIndex = index.getClusterGroupQueryableIndex(group, true);
final CursorBuildSpec groupSpec = plan.rebuildCursorBuildSpec(spec, group);
bundles.add(
new DownloadBundle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,15 @@ default List<TableClusterGroupSpec> getClusterGroupSchemas()
}

/**
* Returns a {@link QueryableIndex} sub-view scoped to a single cluster group's column data. Mirrors
* {@link #getProjectionQueryableIndex(String)} but for cluster groups, addressed by reference rather than name.
* Returns a {@link QueryableIndex} sub-view scoped to a single cluster group's column data.
* Default returns {@code null}; only clustered segments override.
* <p>
* Clustering columns are constant within a group. When {@code withClusteringColumns} is true (query paths) they are
* additionally exposed as constant columns so filters and selectors can resolve them; when false
* (the merge/persist path) they are omitted (since they are stored as metadata instead of actual columns).
*/
@Nullable
default QueryableIndex getClusterGroupQueryableIndex(TableClusterGroupSpec groupSpec)
default QueryableIndex getClusterGroupQueryableIndex(TableClusterGroupSpec groupSpec, boolean withClusteringColumns)
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
import org.apache.druid.segment.projections.ClusteringVectorColumnSelectorFactory;
import org.apache.druid.segment.projections.Projections;
import org.apache.druid.segment.projections.QueryableProjection;
import org.apache.druid.segment.projections.SingleGroupClusteringColumnSelectorFactory;
import org.apache.druid.segment.projections.SingleGroupClusteringVectorColumnSelectorFactory;
import org.apache.druid.segment.projections.TableClusterGroupSpec;
import org.apache.druid.segment.vector.ConcatenatingVectorCursor;
import org.apache.druid.segment.vector.MultiValueDimensionVectorSelector;
Expand Down Expand Up @@ -194,47 +192,21 @@ private CursorHolder makeSingleGroupClusteredCursorHolder(
TableClusterGroupSpec valueGroup
)
{
final QueryableIndex groupIndex = index.getClusterGroupQueryableIndex(valueGroup);
final QueryableIndex groupIndex = index.getClusterGroupQueryableIndex(valueGroup, true);
if (groupIndex == null) {
throw DruidException.defensive(
"No cluster-group sub-index resolvable for clustering values "
+ Arrays.toString(valueGroup.lookupClusteringValues())
);
}

// groupIndex exposes the group's clustering columns as constant columns, no selector wrapper is needed
return new QueryableIndexCursorHolder(
groupIndex,
plan.rebuildCursorBuildSpec(spec, valueGroup),
QueryableIndexTimeBoundaryInspector.create(groupIndex),
valueGroup.getSummary().getOrdering()
)
{
@Override
protected ColumnSelectorFactory makeColumnSelectorFactoryForOffset(
ColumnCache columnCache,
Offset baseOffset
)
{
return new SingleGroupClusteringColumnSelectorFactory(
super.makeColumnSelectorFactoryForOffset(columnCache, baseOffset),
valueGroup.getSummary().getClusteringColumns(),
valueGroup.lookupClusteringValues()
);
}

@Override
protected VectorColumnSelectorFactory makeVectorColumnSelectorFactoryForOffset(
ColumnCache columnCache,
VectorOffset baseOffset
)
{
return new SingleGroupClusteringVectorColumnSelectorFactory(
super.makeVectorColumnSelectorFactoryForOffset(columnCache, baseOffset),
valueGroup.getSummary().getClusteringColumns(),
valueGroup.lookupClusteringValues()
);
}
};
);
}

/**
Expand All @@ -258,7 +230,7 @@ private CursorHolder makeMultiGroupClusteredCursorHolder(
final Closer closer = Closer.create();
for (TableClusterGroupSpec valueGroup : matching) {
clusteringValuesByGroup.add(valueGroup.lookupClusteringValues());
final QueryableIndex groupIndex = index.getClusterGroupQueryableIndex(valueGroup);
final QueryableIndex groupIndex = index.getClusterGroupQueryableIndex(valueGroup, true);
if (groupIndex == null) {
throw DruidException.defensive(
"No cluster-group sub-index resolvable for clustering values "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ public IndexableAdapter getProjectionAdapter(String projection)
@Override
public IndexableAdapter getClusterGroupAdapter(TableClusterGroupSpec spec)
{
final QueryableIndex groupIndex = input.getClusterGroupQueryableIndex(spec);
// Merge/persist must not write clustering columns into the per-group files, so omit them here.
final QueryableIndex groupIndex = input.getClusterGroupQueryableIndex(spec, false);
DruidException.conditionalDefensive(groupIndex != null, "Cluster group spec [%s] was not found", spec);
return new QueryableIndexIndexableAdapter(groupIndex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.druid.segment.column.ColumnCapabilitiesImpl;
import org.apache.druid.segment.column.ColumnHolder;
import org.apache.druid.segment.column.ColumnType;
import org.apache.druid.segment.column.ConstantColumns;
import org.apache.druid.segment.column.ValueType;
import org.apache.druid.segment.data.Indexed;
import org.apache.druid.segment.data.ListIndexed;
Expand All @@ -48,6 +49,7 @@

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -234,7 +236,7 @@ public ColumnCapabilities getColumnCapabilities(String column)
if (groups.isEmpty()) {
return null;
}
final QueryableIndex firstGroupIndex = getClusterGroupQueryableIndex(groups.get(0));
final QueryableIndex firstGroupIndex = getClusterGroupQueryableIndex(groups.get(0), false);
return firstGroupIndex == null ? null : firstGroupIndex.getColumnCapabilities(column);
}

Expand Down Expand Up @@ -273,7 +275,7 @@ public List<TableClusterGroupSpec> getClusterGroupSchemas()
* the returned index, they're injected at the cursor-factory level via {@code ClusteringColumnSelectorFactory}.
*/
@Override
public QueryableIndex getClusterGroupQueryableIndex(TableClusterGroupSpec groupSpec)
public QueryableIndex getClusterGroupQueryableIndex(TableClusterGroupSpec groupSpec, boolean withClusteringColumns)
{
if (clusteredBaseSummary == null) {
throw DruidException.defensive("getClusterGroupQueryableIndex called on a non-clustered segment");
Expand All @@ -283,7 +285,20 @@ public QueryableIndex getClusterGroupQueryableIndex(TableClusterGroupSpec groupS
if (index < 0) {
throw DruidException.defensive("Cluster group spec is not part of this segment");
}
final Map<String, Supplier<BaseColumnHolder>> groupColumns = clusterGroupColumns.get(index);
// add clustering columns are constants for query paths
final Map<String, Supplier<BaseColumnHolder>> groupColumns;
if (withClusteringColumns) {
groupColumns = new HashMap<>(clusterGroupColumns.get(index));
ConstantColumns.addConstantClusteringColumns(
groupColumns,
clusteredBaseSummary.getClusteringColumns(),
groupSpec.lookupClusteringValues(),
groupSpec.getNumRows(),
bitmapFactory
);
} else {
groupColumns = clusterGroupColumns.get(index);
}
final Metadata groupMetadata = new Metadata(
null,
null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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.druid.segment.column;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import org.apache.druid.collections.bitmap.BitmapFactory;
import org.apache.druid.error.DruidException;
import org.apache.druid.java.util.common.StringUtils;
import org.apache.druid.segment.data.ConstantColumnarInts;
import org.apache.druid.segment.data.ConstantUtf8Indexed;

import javax.annotation.Nullable;
import java.nio.ByteBuffer;
import java.util.Map;

/**
* Fabricates in-memory {@link BaseColumnHolder}s for columns whose value is constant across every row, without any
* on-disk data. This is the runtime analogue of {@link org.apache.druid.segment.serde.NullColumnPartSerde} for
* arbitrary constants: a STRING constant is presented as a single-value {@link StringUtf8DictionaryEncodedColumn}
* (backed by a one-entry dictionary), and numeric constants as a {@link ConstantNumericColumn}.
*/
public final class ConstantColumns
{
private ConstantColumns()
{
// no instantiation
}

/**
* Add a constant column (via {@link #makeConstantColumnHolder}) to {@code target} for each column in
* {@code clusteringColumns}, using the group-constant value at the matching position of {@code clusteringValues}.
*/
public static void addConstantClusteringColumns(
Map<String, Supplier<BaseColumnHolder>> target,
RowSignature clusteringColumns,
Object[] clusteringValues,
int numRows,
BitmapFactory bitmapFactory
)
{
DruidException.conditionalDefensive(
clusteringValues.length == clusteringColumns.size(),
"clusteringValues length [%s] must match clusteringColumns size [%s]",
clusteringValues.length,
clusteringColumns.size()
);
for (int i = 0; i < clusteringColumns.size(); i++) {
final String columnName = clusteringColumns.getColumnName(i);
final ColumnType columnType = clusteringColumns.getColumnType(i)
.orElseThrow(() -> DruidException.defensive(
"clustering column [%s] is missing a type",
columnName
));
final BaseColumnHolder holder = makeConstantColumnHolder(columnType, clusteringValues[i], numRows, bitmapFactory);

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.

putting trust in caller to pass a good clusteringValues array. bad callsite use could index oob. perhaps worth a defensive size comparison with clusteringColumns

target.put(columnName, Suppliers.ofInstance(holder));
}
}

public static BaseColumnHolder makeConstantColumnHolder(
ColumnType type,
@Nullable Object value,
int numRows,
BitmapFactory bitmapFactory
)
{
final ColumnBuilder builder = new ColumnBuilder()
.setType(type)
.setHasMultipleValues(false)
.setHasNulls(value == null);

if (type.is(ValueType.STRING)) {
final ByteBuffer utf8 = value == null ? null : StringUtils.toUtf8ByteBuffer((String) value);
builder.setDictionaryEncodedColumnSupplier(
Suppliers.ofInstance(
new StringUtf8DictionaryEncodedColumn(
new ConstantColumnarInts(numRows, 0),
null,
new ConstantUtf8Indexed(utf8),
bitmapFactory
)
)
);
} else if (type.isNumeric()) {
builder.setNumericColumnSupplier(
Suppliers.ofInstance(new ConstantNumericColumn(type, (Number) value, numRows))
);
} else {
throw DruidException.defensive("Cannot build a constant column for type[%s]", type);
}
return builder.build();
}
}
Loading
Loading