1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use std::collections::HashMap;
use std::sync::Arc;
use std::fmt;
use std::cmp;

use byteorder::{BigEndian, ByteOrder};
use kudu_pb::common::{
    PartitionPB,
    PartitionSchemaPB,
    PartitionSchemaPB_ColumnIdentifierPB as ColumnIdentifierPB,
    SchemaPB,
};

use Error;
use Result;
use Row;
use Schema;
use key;
use util;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RangePartitionSchema {
    columns: Vec<usize>,
}

impl RangePartitionSchema {
    fn new(columns: Vec<usize>) -> RangePartitionSchema {
        RangePartitionSchema { columns: columns }
    }

    pub fn columns(&self) -> &[usize] {
        &self.columns
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HashPartitionSchema {
    columns: Vec<usize>,
    buckets: u32,
    seed: u32,
}

impl HashPartitionSchema {
    fn new(columns: Vec<usize>, buckets: u32, seed: u32) -> HashPartitionSchema {
        HashPartitionSchema {
            columns: columns,
            buckets: buckets,
            seed: seed,
        }
    }

    pub fn columns(&self) -> &[usize] {
        &self.columns
    }

    pub fn num_buckets(&self) -> u32 {
        self.buckets
    }

    pub fn seed(&self) -> u32 {
        self.seed
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct Inner {
    range_partition: RangePartitionSchema,
    hash_partitions: Vec<HashPartitionSchema>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PartitionSchema {
    inner: Arc<Inner>,
}

impl PartitionSchema {

    pub fn range_partition_schema(&self) -> &RangePartitionSchema {
        &self.inner.range_partition
    }

    pub fn hash_partition_schemas(&self) -> &[HashPartitionSchema] {
        &self.inner.hash_partitions
    }

    #[doc(hidden)]
    pub fn from_pb(pb: &PartitionSchemaPB, schema: &SchemaPB) -> PartitionSchema {
        let mut columns_by_name = HashMap::new();
        let mut columns_by_id = HashMap::new();

        for (idx, column) in schema.get_columns().iter().enumerate() {
            assert!(column.has_name());
            assert!(column.has_id());
            columns_by_name.insert(column.get_name().to_owned(), idx);
            columns_by_id.insert(column.get_id(), idx);
        }

        let column_to_id = |column: &ColumnIdentifierPB| {
            if column.has_id() {
                columns_by_id[&(column.get_id() as u32)]
            } else if column.has_name() {
                columns_by_name[column.get_name()]
            } else {
                panic!("column identifier must have either a name or ID");
            }
        };

        let range_columns = pb.get_range_schema().get_columns()
                              .iter().map(&column_to_id).collect::<Vec<_>>();

        let mut hash_partitions = Vec::with_capacity(pb.get_hash_bucket_schemas().len());
        for hash in pb.get_hash_bucket_schemas().iter() {
            let columns = hash.get_columns().iter().map(&column_to_id).collect::<Vec<_>>();
            hash_partitions.push(HashPartitionSchema::new(columns,
                                                          hash.get_num_buckets() as u32,
                                                          hash.get_seed()));
        }

        PartitionSchema {
            inner: Arc::new(Inner {
                range_partition: RangePartitionSchema::new(range_columns),
                hash_partitions: hash_partitions,
            })
        }
    }
}

/*
impl fmt::Debug for PartitionKey {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut is_first = true;

        for bucket in &self.hash_buckets {
            if is_first { is_first = false; }
            else { try!(write!(f, ", ")); }
            try!(write!(f, "bucket={}", bucket));
        }

        let row = &self.row;
        for &idx in self.partition_schema().range_partition_schema().columns() {
            if !row.is_set(idx).unwrap() { break; }

            if is_first { is_first = false; }
            else { try!(write!(f, ", ")) }

            let column = &row.schema().columns()[idx];

            try!(write!(f, "{:?}=", column.name()));
            match column.data_type() {
                DataType::Bool => try!(write!(f, "{}", row.get::<bool>(idx).unwrap())),
                DataType::Int8 => try!(write!(f, "{}", row.get::<i8>(idx).unwrap())),
                DataType::Int16 => try!(write!(f, "{}", row.get::<i16>(idx).unwrap())),
                DataType::Int32 => try!(write!(f, "{}", row.get::<i32>(idx).unwrap())),
                DataType::Int64 => try!(write!(f, "{}", row.get::<i64>(idx).unwrap())),
                DataType::Timestamp => try!(util::fmt_timestamp(f, row.get::<SystemTime>(idx).unwrap())),
                DataType::Float => try!(write!(f, "{}", row.get::<f32>(idx).unwrap())),
                DataType::Double => try!(write!(f, "{}", row.get::<f64>(idx).unwrap())),
                DataType::Binary => try!(util::fmt_hex(f, row.get::<&[u8]>(idx).unwrap())),
                DataType::String => try!(write!(f, "{:?}", row.get::<&str>(idx).unwrap())),
            }
        }

        Ok(())
    }
}
*/

#[derive(Clone)]
pub struct Partition {
    partition_schema: PartitionSchema,
    lower_bound_key: Vec<u8>,
    upper_bound_key: Vec<u8>,
    hash_partitions: Vec<u32>,
    range_lower_bound: Row,
    range_upper_bound: Row,
}

impl fmt::Debug for Partition {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[({:?}), ({:?}))", &self.lower_bound_key, &self.upper_bound_key)
    }
}

impl cmp::PartialEq for Partition {
    fn eq(&self, other: &Partition) -> bool {
        &self.lower_bound_key == &other.lower_bound_key &&
            &self.upper_bound_key == &other.upper_bound_key
    }
}

impl cmp::Eq for Partition { }

impl Partition {

    pub fn lower_bound_key(&self) -> &[u8] {
        &self.lower_bound_key
    }

    pub fn upper_bound_key(&self) -> &[u8] {
        &self.upper_bound_key
    }

    pub fn range_lower_bound(&self) -> &Row {
        &self.range_lower_bound
    }

    pub fn range_upper_bound(&self) -> &Row {
        &self.range_upper_bound
    }

    pub fn hash_partitions(&self) -> &[u32] {
        &self.hash_partitions
    }

    /// Formats the range partition.
    ///
    /// VALUES = 123
    /// VALUES = (123, 456)
    /// 123 <= VALUES < 999
    /// (123, 456) <= VALUES < (789, 102)
    pub fn fmt_range_partition(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let idxs = &self.partition_schema.range_partition_schema().columns;

        let lower_bound_idxs = idxs.iter().take_while(|&idx| self.range_lower_bound.is_set(*idx).unwrap()).count();
        let upper_bound_idxs = idxs.iter().take_while(|&idx| self.range_upper_bound.is_set(*idx).unwrap()).count();

        match (lower_bound_idxs, upper_bound_idxs) {
            (0, 0) => write!(f, "UNBOUNDED"),
            (0, count) => {
                try!(write!(f, "VALUES < "));
                fmt_row(f, &self.range_upper_bound, &idxs[..count])
            },
            (count, 0) => {
                try!(write!(f, "VALUES >= "));
                fmt_row(f, &self.range_lower_bound, &idxs[..count])
            },
            _ => {
                if key::is_row_incremented(&self.range_lower_bound, &self.range_upper_bound, idxs) {
                    try!(write!(f, "VALUES = "));
                    fmt_row(f, &self.range_lower_bound, idxs)
                } else {
                    try!(fmt_row(f, &self.range_lower_bound, idxs));
                    try!(write!(f, " <= VALUES < "));
                    fmt_row(f, &self.range_upper_bound, idxs)
                }
            }
        }
    }

    #[doc(hidden)]
    pub fn from_pb(primary_key_schema: &Schema,
                   partition_schema: PartitionSchema,
                   mut partition: PartitionPB)
                   -> Result<Partition> {
        let lower_bound_key = partition.take_partition_key_start();
        let upper_bound_key = partition.take_partition_key_end();

        let hash_partition_levels = partition_schema.hash_partition_schemas().len();
        let mut hash_partitions = Vec::with_capacity(hash_partition_levels);
        {
            let mut key = &lower_bound_key[..];
            for _ in partition_schema.hash_partition_schemas() {
                if key.is_empty() {
                    hash_partitions.push(0)
                } else if key.len() < 4 {
                    return Err(Error::Serialization(format!("invalid lower bound partition key: {:?}",
                                                            lower_bound_key)));
                } else {
                    hash_partitions.push(BigEndian::read_u32(key));
                    key = &key[4..];
                }
            }
        }

        let range_lower_bound = if lower_bound_key.len() > hash_partition_levels * 4 {
            try!(key::decode_range_partition_key(primary_key_schema,
                                                 partition_schema.range_partition_schema(),
                                                 &lower_bound_key[hash_partition_levels * 4..]))
        } else {
            primary_key_schema.new_row()
        };
        let range_upper_bound = if upper_bound_key.len() > hash_partition_levels * 4 {
            try!(key::decode_range_partition_key(primary_key_schema,
                                                 partition_schema.range_partition_schema(),
                                                 &upper_bound_key[hash_partition_levels * 4..]))
        } else {
            primary_key_schema.new_row()
        };

        Ok(Partition {
            partition_schema: partition_schema,
            lower_bound_key: lower_bound_key,
            upper_bound_key: upper_bound_key,
            hash_partitions: hash_partitions,
            range_lower_bound: range_lower_bound,
            range_upper_bound: range_upper_bound,
        })
    }
}

fn fmt_row(f: &mut fmt::Formatter, row: &Row, idxs: &[usize]) -> fmt::Result {
    debug_assert!(!idxs.is_empty());

    if idxs.len() == 1 {
        return util::fmt_cell(f, row, idxs[0]);
    }

    try!(write!(f, "("));
    let mut is_first = true;
    for &idx in idxs {
        if is_first { is_first = false; }
        else { try!(write!(f, ", ")) }
        try!(util::fmt_cell(f, row, idx));
    }
    write!(f, ")")
}