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

com.clickzetta.platform.schema.kudu.KuduSchema Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package com.clickzetta.platform.schema.kudu;

import com.clickzetta.platform.common.ColumnSchema;
import com.clickzetta.platform.common.Type;
import com.clickzetta.platform.operator.PartialRow;
import com.clickzetta.platform.schema.Schema;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import org.apache.kudu.KuduCommon;

import java.util.*;

public class KuduSchema implements Schema {

  /**
   * Mapping of column index to column.
   */
  private final List columnsByIndex;

  /**
   * The primary key columns. (sortColumns + keyColumns)
   */
  private final List primaryKeyColumns = new ArrayList<>();

  /**
   * The primary key columns.
   */
  private final List keyColumns = new ArrayList<>();

  /**
   * The primary key columns.
   */
  private final List sortColumns = new ArrayList<>();

  /**
   * Mapping of column name to index.
   */
  private final Map columnsByName;

  /**
   * Mapping of column ID to index, or null if the schema does not have assigned column IDs.
   */
  private final Map columnsById;

  /**
   * Mapping of column name to column ID, or null if the schema does not have assigned column IDs.
   */
  private final Map columnIdByName;

  /**
   * Mapping of var length column index to all var length columns offset.
   * or null if the schema does not have var length column.
   * 

* Varchar | String | Binary. */ private final int[] varLengthColumnOffsetToAllVarCharColumns; /** * Mapping of column index to backing byte array offset. */ private final int[] columnOffsets; /** * Mapping of column name to index with case Insensitive. * such as. column1 will be COLUMN1 or column1. */ private final Map columnsByNameWithCase; private final int varLengthColumnCount; private final int rowSize; private final boolean hasNullableColumns; private final int isDeletedIndex; private static final int NO_IS_DELETED_INDEX = -1; public KuduSchema(List columns, List keyColumns, List sortColumns) { this(columns, null, keyColumns, sortColumns); } public KuduSchema(List columns, List columnIds, List keyColumns, List sortColumns) { boolean hasColumnIds = columnIds != null; if (hasColumnIds && columns.size() != columnIds.size()) { throw new IllegalArgumentException("Schema must be constructed with all column IDs, or none."); } // add sort columns & key columns. this.keyColumns.addAll(keyColumns); this.sortColumns.addAll(sortColumns); this.columnsByIndex = ImmutableList.copyOf(columns); int varLenCnt = 0; this.columnOffsets = new int[columns.size()]; this.columnsByName = new HashMap<>(columns.size()); this.columnsByNameWithCase = new HashMap<>(columns.size() * 2); this.columnsById = hasColumnIds ? new HashMap<>(columnIds.size()) : null; this.columnIdByName = hasColumnIds ? new HashMap<>(columnIds.size()) : null; this.varLengthColumnOffsetToAllVarCharColumns = new int[columns.size()]; int offset = 0; boolean hasNulls = false; int isDeletedIndex = NO_IS_DELETED_INDEX; for (int index = 0; index < columns.size(); index++) { final ColumnSchema column = columns.get(index); if (column.isKey()) { primaryKeyColumns.add(column); } hasNulls |= column.isNullable(); columnOffsets[index] = offset; offset += column.getTypeSize(); if (this.columnsByName.put(column.getName(), index) != null) { throw new IllegalArgumentException(String.format("Column names must be unique: %s", columns)); } // only used for case senstive match when use row.setValue. this.columnsByNameWithCase.put(column.getName().toLowerCase(), index); this.columnsByNameWithCase.put(column.getName().toUpperCase(), index); if (column.getType() == Type.STRING || column.getType() == Type.BINARY || column.getType() == Type.VARCHAR) { this.varLengthColumnOffsetToAllVarCharColumns[index] = varLenCnt; varLenCnt++; } if (hasColumnIds) { if (this.columnsById.put(columnIds.get(index), index) != null) { throw new IllegalArgumentException(String.format("Column IDs must be unique: %s", columnIds)); } if (this.columnIdByName.put(column.getName(), columnIds.get(index)) != null) { throw new IllegalArgumentException(String.format("Column names must be unique: %s", columnIds)); } } if (column.getWireType() == KuduCommon.DataType.IS_DELETED) { isDeletedIndex = index; } } this.varLengthColumnCount = varLenCnt; this.rowSize = getRowSize(this.columnsByIndex); this.hasNullableColumns = hasNulls; this.isDeletedIndex = isDeletedIndex; } @Override public List getColumns() { return this.columnsByIndex; } @Override public int getVarLengthColumnCount() { return this.varLengthColumnCount; } @Override public int getVarLengthColumnOffsetToAllVarCharColumns(int columnIndex) { return this.varLengthColumnOffsetToAllVarCharColumns[columnIndex]; } @Override public int getRowSize() { return this.rowSize; } @Override public int getColumnOffset(int idx) { return this.columnOffsets[idx]; } @Override public boolean hasColumn(String columnName) { return this.columnsByName.containsKey(columnName) || this.columnsByNameWithCase.containsKey(columnName); } @Override public int getColumnIndex(String columnName) { Integer index = this.columnsByName.get(columnName); if (index == null) { index = this.columnsByNameWithCase.get(columnName.toLowerCase()) != null ? this.columnsByNameWithCase.get(columnName.toLowerCase()) : this.columnsByNameWithCase.get(columnName.toUpperCase()); } if (index == null) { throw new IllegalArgumentException( String.format("Unknown column: %s", columnName)); } return index; } @Override public int getColumnIndex(int columnId) { if (!hasColumnIds()) { throw new IllegalStateException("Schema does not have Column IDs"); } Integer index = this.columnsById.get(columnId); if (index == null) { throw new IllegalArgumentException(String.format("Unknown column id: %s", columnId)); } return index; } @Override public ColumnSchema getColumnByIndex(int idx) { return this.columnsByIndex.get(idx); } @Override public ColumnSchema getColumn(String columnName) { return columnsByIndex.get(getColumnIndex(columnName)); } @Override public int getColumnCount() { return this.columnsByIndex.size(); } @Override public int getPrimaryKeyColumnCount() { return this.primaryKeyColumns.size(); } @Override public List getPrimaryKeyColumns() { return primaryKeyColumns; } @Override public List getKeyColumns() { return keyColumns; } @Override public List getSortColumns() { return sortColumns; } @Override public Map getPrimaryKeyIndexMap() { Map map = new LinkedHashMap<>(); for (int i = 0; i < primaryKeyColumns.size(); i++) { map.put(primaryKeyColumns.get(i).getName(), Long.valueOf(Integer.toString(i))); } return map; } @Override public Map getKeyIndexMap() { Map map = new LinkedHashMap<>(); for (int i = 0; i < keyColumns.size(); i++) { map.put(keyColumns.get(i).getName(), Long.valueOf(Integer.toString(i))); } return map; } @Override public Map getSortIndexMap() { Map map = new LinkedHashMap<>(); for (int i = 0; i < sortColumns.size(); i++) { map.put(sortColumns.get(i).getName(), Long.valueOf(Integer.toString(i))); } return map; } @Override public com.clickzetta.platform.common.Schema getRowKeyProjection() { return new com.clickzetta.platform.common.Schema(primaryKeyColumns, keyColumns, sortColumns); } @Override public boolean hasNullableColumns() { return this.hasNullableColumns; } @Override public boolean hasColumnIds() { return columnsById != null; } @Override public int getColumnId(String columnName) { return columnIdByName.get(columnName); } @Override public PartialRow newPartialRow() { return null; } @Override public boolean hasIsDeleted() { return isDeletedIndex != NO_IS_DELETED_INDEX; } @Override public int getIsDeletedIndex() { Preconditions.checkState(hasIsDeleted(), "Schema doesn't have an IS_DELETED columns"); return isDeletedIndex; } @Override public String toString() { return "Schema{" + "columnsByIndex=" + columnsByIndex + ", primaryKeyColumns=" + primaryKeyColumns + ", keyColumns=" + keyColumns + ", sortColumns=" + sortColumns + ", columnsByName=" + columnsByName + ", columnsById=" + columnsById + ", columnIdByName=" + columnIdByName + ", columnOffsets=" + Arrays.toString(columnOffsets) + ", varLengthColumnCount=" + varLengthColumnCount + ", rowSize=" + rowSize + ", hasNullableColumns=" + hasNullableColumns + ", isDeletedIndex=" + isDeletedIndex + '}'; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy