/**
 * 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.hadoop.hive.metastore.hbase;

enum PrincipalType {
    USER = 0;
    ROLE = 1;
}

message AggrStats {
  required int64 parts_found = 1;
  repeated ColumnStats col_stats = 2;
}

message AggrStatsBloomFilter {
  message BloomFilter {
    required int32 num_bits = 1;
    required int32 num_funcs = 2;
    repeated int64 bits = 3;
  }
  required bytes db_name = 1;
  required bytes table_name = 2;
  required BloomFilter bloom_filter = 3;
  required int64 aggregated_at = 4;
}

message AggrStatsInvalidatorFilter {
  message Entry {
    required bytes db_name = 1;
    required bytes table_name = 2;
    required bytes part_name = 3;
  }

  repeated Entry to_invalidate = 1;
  required int64 run_every = 2;
  required int64 max_cache_entry_life = 3;
}

message ColumnStats {

  message BooleanStats {
    optional int64 num_trues = 1;
    optional int64 num_falses = 2;
  }

  message LongStats {
    optional sint64 low_value = 1;
    optional sint64 high_value = 2;
  }

  message DoubleStats {
    optional double low_value = 1;
    optional double high_value = 2;
  }

  message StringStats {
    optional int64 max_col_length = 1;
    optional double avg_col_length = 2;
  }

  message DecimalStats {
    message Decimal {
      required bytes unscaled = 1;
      required int32 scale = 2;
    }
    optional Decimal low_value = 1;
    optional Decimal high_value = 2;
  }

  optional int64 last_analyzed = 1;
  required string column_type = 2;
  optional int64 num_nulls = 3;
  optional int64 num_distinct_values = 4;
  optional BooleanStats bool_stats = 5;
  optional LongStats long_stats = 6;
  optional DoubleStats double_stats = 7;
  optional StringStats string_stats = 8;
  optional StringStats binary_stats = 9;
  optional DecimalStats decimal_stats = 10;
  optional string column_name = 11;
  optional string bit_vectors = 12;
}

message Database {
  optional string description = 1;
  optional string uri = 2;
  optional Parameters parameters = 3;
  optional PrincipalPrivilegeSet privileges = 4;
  optional string owner_name = 5;
  optional PrincipalType owner_type = 6;
}

message DelegationToken {
  required string token_str = 1;
}

message FieldSchema {
  required string name = 1;
  required string type = 2;
  optional string comment = 3;
}

message Function {
  enum FunctionType {
    JAVA = 1;
  }

  message ResourceUri {
    enum ResourceType {
      JAR = 1;
      FILE = 2;
      ARCHIVE = 3;
    }
    required ResourceType resource_type = 1;
    required string uri = 2;
  }

  optional string class_name = 1;
  optional string owner_name = 2;
  optional PrincipalType owner_type = 3;
  optional sint64 create_time = 4;
  optional FunctionType function_type = 5;
  repeated ResourceUri resource_uris = 6;
}

message MasterKey {
  required string master_key = 1;
}

message ParameterEntry {
  required string key = 1;
  required string value = 2;
}

message Parameters {
  repeated ParameterEntry parameter = 1;
}

message Partition {
  optional int64 create_time = 1;
  optional int64 last_access_time = 2;
  optional string location = 3;
  optional Parameters sd_parameters = 4; // storage descriptor parameters
  required bytes sd_hash = 5;
  optional Parameters parameters = 6; // partition parameters
  // We don't support partition level privileges
}

message PrincipalPrivilegeSetEntry {
  required string principal_name = 1;
  repeated PrivilegeGrantInfo privileges = 2;
}

message PrincipalPrivilegeSet {
  repeated PrincipalPrivilegeSetEntry users = 1;
  repeated PrincipalPrivilegeSetEntry roles = 2;
}

message PrivilegeGrantInfo {
  optional string privilege = 1;
  optional int64 create_time = 2;
  optional string grantor = 3;
  optional PrincipalType grantor_type = 4;
  optional bool grant_option = 5;
}

message RoleGrantInfo {
  required string principal_name = 1;
  required PrincipalType principal_type = 2;
  optional int64 add_time = 3;
  optional string grantor = 4;
  optional PrincipalType grantor_type = 5;
  optional bool grant_option = 6;
}

message RoleGrantInfoList {
  repeated RoleGrantInfo grant_info = 1;
}

message RoleList {
  repeated string role = 1;
}

message Role {
  optional int64 create_time = 1;
  optional string owner_name = 2;
}

message StorageDescriptor {
  message Order {
    required string column_name = 1;
    optional sint32 order = 2 [default = 1];
  }

  message SerDeInfo {
    optional string name = 1;
    optional string serialization_lib = 2;
    optional Parameters parameters = 3;
  }

  message SkewedInfo {
    message SkewedColValueList {
      repeated string skewed_col_value = 1;
    }
  
    message SkewedColValueLocationMap {
      repeated string key = 1;
      required string value = 2;
    }

    repeated string skewed_col_names = 1;
    repeated SkewedColValueList skewed_col_values = 2;
    repeated SkewedColValueLocationMap skewed_col_value_location_maps = 3;
  }

  repeated FieldSchema cols = 1;
  optional string input_format = 2;
  optional string output_format = 3;
  optional bool is_compressed = 4;
  optional sint32 num_buckets = 5;
  optional SerDeInfo serde_info = 6;
  repeated string bucket_cols = 7;
  repeated Order sort_cols = 8;
  optional SkewedInfo skewed_info = 9;
  optional bool stored_as_sub_directories = 10;
}

message Table {
  optional string owner = 1;
  optional int64 create_time = 2;
  optional int64 last_access_time = 3;
  optional int64 retention = 4;
  optional string location = 5;
  optional Parameters sd_parameters = 6; // storage descriptor parameters
  required bytes sd_hash = 7;
  repeated FieldSchema partition_keys = 8;
  optional Parameters parameters = 9;
  optional string view_original_text = 10;
  optional string view_expanded_text = 11;
  optional string table_type = 12;
  optional PrincipalPrivilegeSet privileges = 13;
  optional bool is_temporary = 14;
  optional bool is_rewrite_enabled = 15;
}

message Index {
  optional string indexHandlerClass = 1; // reserved
  required string dbName = 2;
  required string origTableName = 3;
  optional string location = 4;
  optional Parameters sd_parameters = 5; // storage descriptor parameters
  optional int32 createTime = 6;
  optional int32 lastAccessTime = 7;
  optional string indexTableName = 8;
  optional bytes sd_hash = 9;
  optional Parameters parameters = 10;
  optional bool deferredRebuild = 11;
}

message PartitionKeyComparator {
  required string names = 1;
  required string types = 2;
  message Mark {
    required string value = 1;
    required bool inclusive = 2;
  }
  message Range {
    required string key = 1;
    optional Mark start = 2;
    optional Mark end = 3;
  }
  message Operator {
    enum Type {
      LIKE = 0;
      NOTEQUALS = 1;
    }
    required Type type = 1;
    required string key = 2;
    required string val = 3;
  }
  repeated Operator op = 3;
  repeated Range range = 4;
}

message PrimaryKey {
  message PrimaryKeyColumn {
    required string column_name = 1;
    required sint32 key_seq = 2;
  }

  required string pk_name = 1;
  repeated PrimaryKeyColumn cols = 2;
  optional bool enable_constraint = 3;
  optional bool validate_constraint = 4;
  optional bool rely_constraint = 5;
}

message ForeignKeys {
  message ForeignKey {
    message ForeignKeyColumn {
      required string column_name = 1;
      required string referenced_column_name = 2;
      required sint32 key_seq = 3;
    }

    required string fk_name = 1;
    required string referenced_db_name = 2;
    required string referenced_table_name = 3;
    optional string referenced_pk_name = 4;
    optional int32 update_rule = 5;
    optional int32 delete_rule = 6;
    repeated ForeignKeyColumn cols = 7;
    optional bool enable_constraint = 8;
    optional bool validate_constraint = 9;
    optional bool rely_constraint = 10;
  }

  repeated ForeignKey fks = 1;
}
