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

org.apache.hudi.hive.SchemaDifference Maven / Gradle / Ivy

The 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.hudi.hive;

import org.apache.parquet.schema.MessageType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

/**
 * Represents the schema difference between the storage schema and hive table schema.
 */
public class SchemaDifference {

  private final MessageType storageSchema;
  private final Map tableSchema;
  private final List deleteColumns;
  private final Map updateColumnTypes;
  private final Map addColumnTypes;

  private SchemaDifference(MessageType storageSchema, Map tableSchema, List deleteColumns,
      Map updateColumnTypes, Map addColumnTypes) {
    this.storageSchema = storageSchema;
    this.tableSchema = tableSchema;
    this.deleteColumns = Collections.unmodifiableList(deleteColumns);
    this.updateColumnTypes = Collections.unmodifiableMap(updateColumnTypes);
    this.addColumnTypes = Collections.unmodifiableMap(addColumnTypes);
  }

  public List getDeleteColumns() {
    return deleteColumns;
  }

  public Map getUpdateColumnTypes() {
    return updateColumnTypes;
  }

  public Map getAddColumnTypes() {
    return addColumnTypes;
  }

  public static Builder newBuilder(MessageType storageSchema, Map tableSchema) {
    return new Builder(storageSchema, tableSchema);
  }

  public boolean isEmpty() {
    return deleteColumns.isEmpty() && updateColumnTypes.isEmpty() && addColumnTypes.isEmpty();
  }

  @Override
  public String toString() {
    return new StringJoiner(", ", SchemaDifference.class.getSimpleName() + "[", "]")
           .add("storageSchema=" + storageSchema)
           .add("tableSchema=" + tableSchema)
           .add("deleteColumns=" + deleteColumns)
           .add("updateColumnTypes=" + updateColumnTypes)
           .add("addColumnTypes=" + addColumnTypes)
           .toString();
  }

  public static class Builder {

    private final MessageType storageSchema;
    private final Map tableSchema;
    private List deleteColumns;
    private Map updateColumnTypes;
    private Map addColumnTypes;

    public Builder(MessageType storageSchema, Map tableSchema) {
      this.storageSchema = storageSchema;
      this.tableSchema = tableSchema;
      deleteColumns = new ArrayList<>();
      updateColumnTypes = new HashMap<>();
      addColumnTypes = new LinkedHashMap<>();
    }

    public Builder deleteTableColumn(String column) {
      deleteColumns.add(column);
      return this;
    }

    public Builder updateTableColumn(String column, String storageColumnType) {
      updateColumnTypes.put(column, storageColumnType);
      return this;
    }

    public Builder addTableColumn(String name, String type) {
      addColumnTypes.put(name, type);
      return this;
    }

    public SchemaDifference build() {
      return new SchemaDifference(storageSchema, tableSchema, deleteColumns, updateColumnTypes, addColumnTypes);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy