gianm commented on code in PR #12745: URL: https://github.com/apache/druid/pull/12745#discussion_r915257669
########## processing/src/main/java/org/apache/druid/frame/field/StringFieldReader.java: ########## @@ -0,0 +1,276 @@ +/* + * 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.frame.field; + +import com.google.common.base.Predicate; +import com.google.common.primitives.Ints; +import org.apache.datasketches.memory.Memory; +import org.apache.druid.frame.read.FrameReaderUtils; +import org.apache.druid.java.util.common.ISE; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.query.extraction.ExtractionFn; +import org.apache.druid.query.filter.ValueMatcher; +import org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector; +import org.apache.druid.segment.ColumnValueSelector; +import org.apache.druid.segment.DimensionSelector; +import org.apache.druid.segment.DimensionSelectorUtils; +import org.apache.druid.segment.IdLookup; +import org.apache.druid.segment.column.ColumnType; +import org.apache.druid.segment.data.IndexedInts; +import org.apache.druid.segment.data.RangeIndexedInts; + +import javax.annotation.Nullable; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Reads fields written by {@link StringFieldWriter} or {@link StringArrayFieldWriter}. + */ +public class StringFieldReader implements FieldReader +{ + private final boolean asArray; + + StringFieldReader(final boolean asArray) + { + this.asArray = asArray; + } + + @Override + public ColumnValueSelector<?> makeColumnValueSelector(Memory memory, ReadableFieldPointer fieldPointer) + { + return new Selector(memory, fieldPointer, null, asArray); + } + + @Override + public DimensionSelector makeDimensionSelector( + Memory memory, + ReadableFieldPointer fieldPointer, + @Nullable ExtractionFn extractionFn + ) + { + if (asArray) { + throw new ISE("Cannot call makeDimensionSelector on field of type [%s]", ColumnType.STRING_ARRAY); + } + + return new Selector(memory, fieldPointer, extractionFn, false); + } + + @Override + public boolean isComparable() + { + return true; + } + + private static class Selector implements DimensionSelector + { + private final Memory memory; + private final ReadableFieldPointer fieldPointer; + @Nullable + private final ExtractionFn extractionFn; + private final boolean asArray; + + private long currentFieldPosition = -1; + private final RangeIndexedInts indexedInts = new RangeIndexedInts(); + private final List<ByteBuffer> currentUtf8Strings = new ArrayList<>(); + + private Selector( + final Memory memory, + final ReadableFieldPointer fieldPointer, + @Nullable final ExtractionFn extractionFn, + final boolean asArray + ) + { + this.memory = memory; + this.fieldPointer = fieldPointer; + this.extractionFn = extractionFn; + this.asArray = asArray; + } + + @Nullable + @Override + public Object getObject() + { + final List<ByteBuffer> currentStrings = computeCurrentUtf8Strings(); + final int size = currentStrings.size(); + + if (size == 0) { + return asArray ? Collections.emptyList() : null; + } else if (size == 1) { + return asArray ? Collections.singletonList(lookupName(0)) : lookupName(0); + } else { + final List<String> strings = new ArrayList<>(size); + for (int i = 0; i < size; i++) { + strings.add(lookupName(i)); + } + return strings; + } + } + + @Override + public IndexedInts getRow() + { + indexedInts.setSize(computeCurrentUtf8Strings().size()); + return indexedInts; + } + + @Nullable + @Override + public String lookupName(int id) + { + final ByteBuffer byteBuffer = computeCurrentUtf8Strings().get(id); + final String s = byteBuffer != null ? StringUtils.fromUtf8(byteBuffer.duplicate()) : null; + return extractionFn == null ? s : extractionFn.apply(s); + } + + @Override + public boolean supportsLookupNameUtf8() + { + return extractionFn == null; + } + + @Nullable + @Override + public ByteBuffer lookupNameUtf8(int id) + { + if (extractionFn != null) { + throw new ISE("Cannot use lookupNameUtf8 on this selector"); + } + + return computeCurrentUtf8Strings().get(id); + } + + @Override + public int getValueCardinality() + { + return CARDINALITY_UNKNOWN; + } Review Comment: This is implementing DimensionSelector, an interface we've had since time immemorial. It's used for reading string and multi-value string columns. I think @clintropolis has been working on teasing some of these things apart, as part of the effort to enable dictionary coding and indexing for things that aren't strings. So he may be able to comment more on future plans for this interface. In this patch, I'm taking it as it is. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
