clintropolis commented on code in PR #12277: URL: https://github.com/apache/druid/pull/12277#discussion_r1002292304
########## processing/src/main/java/org/apache/druid/segment/column/StringEncodingStrategies.java: ########## @@ -0,0 +1,126 @@ +/* + * 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 org.apache.druid.java.util.common.ISE; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector; +import org.apache.druid.segment.data.DictionaryWriter; +import org.apache.druid.segment.data.EncodedStringDictionaryWriter; +import org.apache.druid.segment.data.FrontCodedIndexedWriter; +import org.apache.druid.segment.data.GenericIndexed; +import org.apache.druid.segment.data.GenericIndexedWriter; +import org.apache.druid.segment.data.Indexed; +import org.apache.druid.segment.writeout.SegmentWriteOutMedium; + +import javax.annotation.Nullable; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Iterator; + +public class StringEncodingStrategies +{ + public static DictionaryWriter<String> getStringDictionaryWriter( + StringEncodingStrategy encodingStrategy, + SegmentWriteOutMedium writeoutMedium, + String fileName + ) + { + // write plain utf8 in the legacy format, where generic indexed was written directly + if (StringEncodingStrategy.UTF8.equals(encodingStrategy.getType())) { + return new GenericIndexedWriter<>(writeoutMedium, fileName, GenericIndexed.STRING_STRATEGY); + } else { + // otherwise, we wrap in an EncodedStringDictionaryWriter so that we write a small header that includes + // a version byte that should hopefully never conflict with a GenericIndexed version, along with a byte + // from StringEncodingStrategy.getId to indicate which encoding strategy is used for the dictionary before + // writing the dictionary itself + DictionaryWriter<byte[]> writer; + if (StringEncodingStrategy.FRONT_CODED.equals(encodingStrategy.getType())) { + writer = new FrontCodedIndexedWriter( + writeoutMedium, + ByteOrder.nativeOrder(), Review Comment: Oops, I meant to tighten this up a bit. My intention was to match the byte order we use for the compressed columnar ints which is specified by the byte order set on `DictionaryEncodedColumnPartSerde` and is stored in column metadata. It defaults to `IndexIO.BYTE_ORDER`, which defaults to `ByteOrder.nativeOrder()`. So its technically correct, since it will always match however the columnar ints are serialized, and on the read side gets the byteorder to pass to the read methods, but in a super fragile way that isn't connected for both writers. Though, maybe you are suggesting it is pointless to support anything other than little endian these days? the only thing impacted here would be the int bucket offsets, everything else is just bytes, so I could drop the byteorder argument from `FrontCodedIndexed` and the big endian tests. I can do that if you'd prefer and anyone on big endian can just deal with it, or i can just clean this up a bit to ensure that the writer always matches the byte order used for the int ids and other column parts, I don't have a very strong opinion about it. -- 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]
