All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.iceberg.orc.IdToOrcName Maven / Gradle / Ivy

There is a newer version: 1.7.1
Show newest version
/*
 * 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.iceberg.orc;

import java.util.Deque;
import java.util.List;
import java.util.Map;
import org.apache.iceberg.Schema;
import org.apache.iceberg.relocated.com.google.common.base.Joiner;
import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.types.Type;
import org.apache.iceberg.types.TypeUtil;
import org.apache.iceberg.types.Types;

/**
 * Generates mapping from field IDs to ORC qualified names.
 *
 * 

This visitor also enclose column names in backticks i.e. ` so that ORC can correctly parse * column names with special characters. A comparison of ORC convention with Iceberg convention is * provided below * *

{@code
 *                                      Iceberg           ORC
 * field                                field             field
 * struct -> field                      struct.field      struct.field
 * list -> element                      list.element      list._elem
 * list -> struct element -> field      list.field        list._elem.field
 * map -> key                           map.key           map._key
 * map -> value                         map.value         map._value
 * map -> struct key -> field           map.key.field     map._key.field
 * map -> struct value -> field         map.field         map._value.field
 * }
*/ class IdToOrcName extends TypeUtil.SchemaVisitor> { private static final Joiner DOT = Joiner.on("."); private final Deque fieldNames = Lists.newLinkedList(); private final Map idToName = Maps.newHashMap(); @Override public void beforeField(Types.NestedField field) { fieldNames.push(field.name()); } @Override public void afterField(Types.NestedField field) { fieldNames.pop(); } @Override public void beforeListElement(Types.NestedField elementField) { fieldNames.push("_elem"); } @Override public void afterListElement(Types.NestedField elementField) { fieldNames.pop(); } @Override public void beforeMapKey(Types.NestedField keyField) { fieldNames.push("_key"); } @Override public void afterMapKey(Types.NestedField keyField) { fieldNames.pop(); } @Override public void beforeMapValue(Types.NestedField valueField) { fieldNames.push("_value"); } @Override public void afterMapValue(Types.NestedField valueField) { fieldNames.pop(); } @Override public Map schema(Schema schema, Map structResult) { return structResult; } @Override public Map struct( Types.StructType struct, List> fieldResults) { return idToName; } @Override public Map field(Types.NestedField field, Map fieldResult) { addField(field.name(), field.fieldId()); return idToName; } @Override public Map list(Types.ListType list, Map elementResult) { addField("_elem", list.elementId()); return idToName; } @Override public Map map( Types.MapType map, Map keyResult, Map valueResult) { addField("_key", map.keyId()); addField("_value", map.valueId()); return idToName; } @Override public Map primitive(Type.PrimitiveType primitive) { return idToName; } private void addField(String name, int fieldId) { List fullName = Lists.newArrayList(fieldNames.descendingIterator()); fullName.add(name); idToName.put(fieldId, DOT.join(Iterables.transform(fullName, this::quoteName))); } private String quoteName(String name) { String escapedName = name.replace("`", "``"); // if the column name contains ` then escape it with another ` return "`" + escapedName + "`"; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy