vernedeng commented on code in PR #123: URL: https://github.com/apache/flink-connector-pulsar/pull/123#discussion_r3527800055
########## flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/table/TableDataTypeUtils.java: ########## @@ -0,0 +1,98 @@ +/* + * 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.connector.pulsar.table; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.FieldsDataType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.LogicalTypeRoot; +import org.apache.flink.table.types.logical.RowType; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +/** + * Utility methods for working with table {@link DataType}s. + * + * <p>This class contains methods that were removed from Flink's {@code DataTypeUtils} in Flink 2.x. + * They are copied here to maintain the connector's functionality. + */ +@Internal +public class TableDataTypeUtils { + + private TableDataTypeUtils() {} + + /** + * Removes a prefix from all field names in a row data type. + * + * @param dataType The row data type whose field names should be stripped. + * @param prefix The prefix to remove from each field name. + * @return A new data type with the prefix removed from field names. + */ + public static DataType stripRowPrefix(DataType dataType, String prefix) { + if (!dataType.getLogicalType().is(LogicalTypeRoot.ROW)) { + throw new IllegalArgumentException("Row data type expected."); + } + + final RowType rowType = (RowType) dataType.getLogicalType(); + final List<String> newFieldNames = + rowType.getFieldNames().stream() + .map( + s -> { + if (s.startsWith(prefix)) { + return s.substring(prefix.length()); + } + return s; + }) + .collect(Collectors.toList()); + final LogicalType newRowType = renameRowFields(rowType, newFieldNames); + return new FieldsDataType( + newRowType, dataType.getConversionClass(), dataType.getChildren()); + } + + /** + * Renames the fields in a {@link RowType}. + * + * @param rowType The row type to rename fields in. + * @param newFieldNames The new field names to use. + * @return A new row type with the renamed fields. + */ + public static RowType renameRowFields(RowType rowType, List<String> newFieldNames) { Review Comment: You're right that the method exists in Flink 2.x, and I did switch to LogicalTypeUtils.renameRowFields first. However, the connector's archunit rules (flink-connector-pulsar/archunit-violations/stored.rules) forbid production code from depending on internal packages — and org.apache.flink.table.types.logical.utils is an internal utils package, so the build fails the archunit check: Method <...TableDataTypeUtils.stripRowPrefix> calls method <org.apache.flink.table.types.logical.utils.LogicalTypeUtils.renameRowFields> Since that package isn't @Public, I kept the self-written renameRowFields here. Its behaviour is equivalent to LogicalTypeUtils.renameRowFields (same field-by-field rewrite, nullability preserved), and there's a unit test covering it (TableDataTypeUtilsTest). Happy to drop the local copy if there's a @Public equivalent I missed, or if the archunit rule can be relaxed for this case. -- 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]
