lincoln-lil commented on code in PR #27778: URL: https://github.com/apache/flink/pull/27778#discussion_r2979389942
########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/BitmapToBinaryCastRule.java: ########## @@ -0,0 +1,88 @@ +/* + * 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.flink.table.planner.functions.casting; + +import org.apache.flink.table.planner.codegen.CodeGenUtils; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.LogicalTypeFamily; +import org.apache.flink.table.types.logical.LogicalTypeRoot; +import org.apache.flink.table.types.logical.utils.LogicalTypeChecks; +import org.apache.flink.types.bitmap.Bitmap; + +import static org.apache.flink.table.planner.functions.casting.BinaryToBinaryCastRule.couldPad; +import static org.apache.flink.table.planner.functions.casting.BinaryToBinaryCastRule.couldTrim; +import static org.apache.flink.table.planner.functions.casting.BinaryToBinaryCastRule.trimOrPadByteArray; +import static org.apache.flink.table.planner.functions.casting.CastRuleUtils.arrayLength; +import static org.apache.flink.table.planner.functions.casting.CastRuleUtils.methodCall; + +/** {@link LogicalTypeRoot#BITMAP} to {@link LogicalTypeFamily#BINARY_STRING} cast rule. */ +class BitmapToBinaryCastRule extends AbstractNullAwareCodeGeneratorCastRule<Bitmap, byte[]> { + + static final BitmapToBinaryCastRule INSTANCE = new BitmapToBinaryCastRule(); + + private BitmapToBinaryCastRule() { + super( + CastRulePredicate.builder() + .input(LogicalTypeRoot.BITMAP) + .target(LogicalTypeFamily.BINARY_STRING) + .build()); + } + + @Override + protected String generateCodeBlockInternal( + CodeGeneratorCastRule.Context context, + String inputTerm, + String returnVariable, + LogicalType inputLogicalType, + LogicalType targetLogicalType) { + final int targetLength = LogicalTypeChecks.getLength(targetLogicalType); + final String byteArrayTerm = + CodeGenUtils.newName(context.getCodeGeneratorContext(), "bitmapBytes"); + + if (context.legacyBehaviour() + || !(couldTrim(targetLength) || couldPad(targetLogicalType, targetLength))) { + return new CastRuleUtils.CodeWriter() + .assignStmt(returnVariable, methodCall(inputTerm, "toBytes")) + .toString(); + } else { + return new CastRuleUtils.CodeWriter() + .declStmt(byte[].class, byteArrayTerm, methodCall(inputTerm, "toBytes")) + .ifStmt( + arrayLength(byteArrayTerm) + " <= " + targetLength, + thenWriter -> { + if (couldPad(targetLogicalType, targetLength)) { + trimOrPadByteArray( Review Comment: IIUC `trimOrPadByteArray` will perform a tail-cut operation (retaining the first N bytes), it may lead to data corruption for Bitmap's RoaringBitmap serialization format (header + container array) which will be undeserializable after truncation? ########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/data/conversion/BitmapBitmapConverter.java: ########## @@ -0,0 +1,47 @@ +/* + * 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.flink.table.data.conversion; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.types.bitmap.Bitmap; +import org.apache.flink.types.bitmap.RoaringBitmapData; + +/** + * Converter for {@link org.apache.flink.table.types.logical.BitmapType BitmapType} of unknown Review Comment: nit: The Javadoc says "unknown Bitmap external type", but toInternal() only accepts RoaringBitmapData and rejects all other Bitmap implementations. Consider updating the Javadoc to reflect this constraint, e.g., "Converter for {@link RoaringBitmapData} as the external Bitmap type." ########## flink-table/flink-table-common/src/main/java/org/apache/flink/table/data/RowData.java: ########## @@ -205,6 +206,11 @@ public interface RowData { /** Returns the variant value at the given position. */ Variant getVariant(int pos); + /** Returns the bitmap value at the given position. */ + default Bitmap getBitmap(int pos) { + throw new UnsupportedOperationException("Bitmap is not supported yet."); Review Comment: This error message is slightly misleading, it's not a temporary limitation of the type system, but rather that this particular RowData implementation hasn't provided BITMAP support. Something like "This RowData implementation does not support Bitmap type." would be clearer. ########## flink-table/flink-table-common/src/main/java/org/apache/flink/table/data/RowData.java: ########## Review Comment: nit: The type mapping table is missing the new BITMAP entry. -- 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]
