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

org.apache.hudi.internal.schema.action.TableChangesHelper Maven / Gradle / Ivy

/*
 * 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.hudi.internal.schema.action;

import org.apache.hudi.internal.schema.Types;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Helper class to support Table schema changes.
 */
public class TableChangesHelper {
  /**
   * Apply add operation and column position change operation.
   *
   * @param fields origin column fields.
   * @param adds column fields to be added.
   * @param pchanges a wrapper class hold all the position change operations.
   * @return column fields after adjusting the position.
   */
  public static List applyAddChange2Fields(List fields, ArrayList adds, ArrayList pchanges) {
    if (adds == null && pchanges == null) {
      return fields;
    }
    LinkedList result = new LinkedList<>(fields);
    // apply add columns
    if (adds != null && !adds.isEmpty()) {
      result.addAll(adds);
    }
    // apply position change
    if (pchanges != null && !pchanges.isEmpty()) {
      for (TableChange.ColumnPositionChange pchange : pchanges) {
        Types.Field srcField = result.stream().filter(f -> f.fieldId() == pchange.getSrcId()).findFirst().get();
        Types.Field dsrField = result.stream().filter(f -> f.fieldId() == pchange.getDsrId()).findFirst().orElse(null);
        // we remove srcField first
        result.remove(srcField);
        switch (pchange.type()) {
          case AFTER:
            // add srcField after dsrField
            result.add(result.indexOf(dsrField) + 1, srcField);
            break;
          case BEFORE:
            // add srcField before dsrField
            result.add(result.indexOf(dsrField), srcField);
            break;
          case FIRST:
            result.addFirst(srcField);
            break;
          default:
            // should not reach here
        }
      }
    }
    return result;
  }

  public static String getParentName(String fullColName) {
    int offset = fullColName.lastIndexOf(".");
    return offset > 0 ? fullColName.substring(0, offset) : "";
  }

  public static String getLeafName(String fullColName) {
    int offset = fullColName.lastIndexOf(".");
    return offset > 0 ? fullColName.substring(offset + 1) : fullColName;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy