|
| 1 | +// Copyright 2021-present StarRocks, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.starrocks.connector.paimon; |
| 16 | + |
| 17 | +import com.starrocks.thrift.TIcebergSchema; |
| 18 | +import com.starrocks.thrift.TIcebergSchemaField; |
| 19 | +import org.apache.paimon.format.parquet.ParquetSchemaConverter; |
| 20 | +import org.apache.paimon.table.SpecialFields; |
| 21 | +import org.apache.paimon.types.ArrayType; |
| 22 | +import org.apache.paimon.types.DataField; |
| 23 | +import org.apache.paimon.types.DataType; |
| 24 | +import org.apache.paimon.types.DataTypeRoot; |
| 25 | +import org.apache.paimon.types.MapType; |
| 26 | +import org.apache.paimon.types.RowType; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +public class PaimonUtils { |
| 32 | + |
| 33 | + public static TIcebergSchema getTPaimonSchema(RowType rowType) { |
| 34 | + // reuse TIcebergSchema directly for compatibility. |
| 35 | + TIcebergSchema tPaimonSchema = new TIcebergSchema(); |
| 36 | + List<DataField> paimonFields = rowType.getFields(); |
| 37 | + List<TIcebergSchemaField> tIcebergFields = new ArrayList<>(paimonFields.size()); |
| 38 | + for (DataField field : paimonFields) { |
| 39 | + tIcebergFields.add(getTPaimonSchemaField(field.name(), field.type(), field.id(), 0, -1)); |
| 40 | + } |
| 41 | + tPaimonSchema.setFields(tIcebergFields); |
| 42 | + return tPaimonSchema; |
| 43 | + } |
| 44 | + |
| 45 | + public static TIcebergSchemaField getTPaimonSchemaField(String name, DataType type, int fieldId, int depth, int parentId) { |
| 46 | + TIcebergSchemaField tPaimonSchemaField = new TIcebergSchemaField(); |
| 47 | + if (parentId != -1) { |
| 48 | + tPaimonSchemaField.setField_id(parentId); |
| 49 | + } else { |
| 50 | + tPaimonSchemaField.setField_id(fieldId); |
| 51 | + } |
| 52 | + tPaimonSchemaField.setName(name); |
| 53 | + if (type.getTypeRoot() == DataTypeRoot.MAP) { |
| 54 | + org.apache.paimon.types.MapType mapType = (MapType) type; |
| 55 | + DataType keyType = mapType.getKeyType(); |
| 56 | + DataType valueType = mapType.getValueType(); |
| 57 | + int mapKeyFieldId = SpecialFields.getMapKeyFieldId(fieldId, depth + 1); |
| 58 | + int mapValueFieldId = SpecialFields.getMapValueFieldId(fieldId, depth + 1); |
| 59 | + List<TIcebergSchemaField> children = new ArrayList<>(2); |
| 60 | + children.add(getTPaimonSchemaField(ParquetSchemaConverter.MAP_KEY_NAME, keyType, fieldId, depth + 1, mapKeyFieldId)); |
| 61 | + children.add(getTPaimonSchemaField(ParquetSchemaConverter.MAP_VALUE_NAME, valueType, fieldId, |
| 62 | + depth + 1, mapValueFieldId)); |
| 63 | + tPaimonSchemaField.setChildren(children); |
| 64 | + } |
| 65 | + if (type.getTypeRoot() == DataTypeRoot.ARRAY) { |
| 66 | + org.apache.paimon.types.ArrayType arrayType = (ArrayType) type; |
| 67 | + DataType elementType = arrayType.getElementType(); |
| 68 | + int elementId = SpecialFields.getArrayElementFieldId(fieldId, depth + 1); |
| 69 | + List<TIcebergSchemaField> children = new ArrayList<>(1); |
| 70 | + children.add(getTPaimonSchemaField(ParquetSchemaConverter.LIST_ELEMENT_NAME, elementType, fieldId, |
| 71 | + depth + 1, elementId)); |
| 72 | + tPaimonSchemaField.setChildren(children); |
| 73 | + } |
| 74 | + // the parent id of row type is always -1, refer to: org.apache.paimon.format.parquet.ParquetSchemaConverter |
| 75 | + if (type.getTypeRoot() == DataTypeRoot.ROW) { |
| 76 | + RowType rowType = (RowType) type; |
| 77 | + List<DataField> childrenFields = rowType.getFields(); |
| 78 | + List<TIcebergSchemaField> children = new ArrayList<>(rowType.getFieldCount()); |
| 79 | + for (DataField childrenField : childrenFields) { |
| 80 | + children.add(getTPaimonSchemaField(childrenField.name(), childrenField.type(), childrenField.id(), |
| 81 | + depth + 1, -1)); |
| 82 | + } |
| 83 | + tPaimonSchemaField.setChildren(children); |
| 84 | + } |
| 85 | + return tPaimonSchemaField; |
| 86 | + } |
| 87 | +} |
0 commit comments