aishikbh commented on code in PR #12032: URL: https://github.com/apache/pinot/pull/12032#discussion_r1402165509
########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/recordtransformer/SpecialValueTransformer.java: ########## @@ -0,0 +1,96 @@ +/** + * 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.local.recordtransformer; + +import java.util.HashSet; +import org.apache.pinot.spi.data.FieldSpec; +import org.apache.pinot.spi.data.FieldSpec.DataType; +import org.apache.pinot.spi.data.Schema; +import org.apache.pinot.spi.data.readers.GenericRow; + + +/** + * The {@code SpecialValueTransformer} class will transform special values according to the following rules: + * <ul> + * <li>Negative zero (-0.0) should be converted to 0.0</li> + * <li>NaN should be converted to default null</li> + * </ul> + * <p>NOTE: should put this after the {@link DataTypeTransformer} so that all values follow the data types in + * {@link FieldSpec}. + */ +public class SpecialValueTransformer implements RecordTransformer { + private final HashSet<String> _specialValuesKeySet = new HashSet<>(); + + public SpecialValueTransformer(Schema schema) { + for (FieldSpec fieldSpec : schema.getAllFieldSpecs()) { + if (!fieldSpec.isVirtualColumn() && (fieldSpec.getDataType() == DataType.FLOAT + || fieldSpec.getDataType() == DataType.DOUBLE)) { + _specialValuesKeySet.add(fieldSpec.getName()); + } + } + } + + private Object transformNegativeZero(Object value) { + if ((value instanceof Float) && (Float.floatToRawIntBits((float) value) == Float.floatToRawIntBits(-0.0f))) { + value = 0.0f; + } else if ((value instanceof Double) && (Double.doubleToLongBits((double) value) == Double.doubleToLongBits( + -0.0d))) { + value = 0.0d; + } + return value; + } + + private Object transformNaN(Object value) { + if ((value instanceof Float) && ((Float) value).isNaN()) { + value = FieldSpec.DEFAULT_DIMENSION_NULL_VALUE_OF_FLOAT; + } else if ((value instanceof Double) && ((Double) value).isNaN()) { + value = FieldSpec.DEFAULT_DIMENSION_NULL_VALUE_OF_DOUBLE; + } + return value; + } + + @Override + public boolean isNoOp() { + return _specialValuesKeySet.isEmpty(); + } + + @Override + public GenericRow transform(GenericRow record) { + for (String element : _specialValuesKeySet) { + Object value = record.getValue(element); + if (value instanceof Float || value instanceof Double) { + // Single-valued column. + Object zeroTransformedValue = transformNegativeZero(value); + Object nanTransformedValue = transformNaN(zeroTransformedValue); + if (nanTransformedValue != value) { + record.putValue(element, nanTransformedValue); + } + } else if (value instanceof Object[]) { + // Multi-valued column. Review Comment: Seems like `null` values are removed from mv columns in `DataTypeTransformer` [here](https://github.com/apache/pinot/blob/e4d3a8dde8df8be8fcc3ae20c96ec6d004dd7933/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/recordtransformer/DataTypeTransformer.java#L153-L157) and not by `NullValueTransformer`. So, if I put `null` instead of `NaN` in a mv column, we end up with an unchecked `null` in the data. For single value column replacing `NaN` with `null` and placing `SpecialValueTrasformer` before `NullValueTransformer` is reasonable as NullValueTransformer replaces `null` with default null. For mv column with `NaN`, I can think of two solutions: 1. Replace `null` with default null in `SpecialValueTransformer` 2. Remove NaN from the list of values (as we do with `null`) I think solution 1. should be reasonable as we are replacing `NaN` in single valued column with default null anyway. Thoughts @Jackie-Jiang @snleee ? -- 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]
