|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Example demonstrating how to use RowSelection to skip rows when reading ORC files |
| 19 | +
|
| 20 | +use std::fs::File; |
| 21 | +use std::sync::Arc; |
| 22 | + |
| 23 | +use arrow::array::{ArrayRef, Int32Array, RecordBatch, StringArray}; |
| 24 | +use arrow::datatypes::{DataType, Field, Schema}; |
| 25 | +use orc_rust::arrow_reader::ArrowReaderBuilder; |
| 26 | +use orc_rust::arrow_writer::ArrowWriterBuilder; |
| 27 | +use orc_rust::row_selection::{RowSelection, RowSelector}; |
| 28 | + |
| 29 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 30 | + // Step 1: Create a sample ORC file with 100 rows |
| 31 | + println!("Creating sample ORC file..."); |
| 32 | + let file_path = "/tmp/row_selection_example.orc"; |
| 33 | + create_sample_orc_file(file_path)?; |
| 34 | + |
| 35 | + // Step 2: Read the file without row selection (baseline) |
| 36 | + println!("\n=== Reading all rows (no selection) ==="); |
| 37 | + let file = File::open(file_path)?; |
| 38 | + let reader = ArrowReaderBuilder::try_new(file)?.build(); |
| 39 | + let mut total_rows = 0; |
| 40 | + for batch in reader { |
| 41 | + let batch = batch?; |
| 42 | + total_rows += batch.num_rows(); |
| 43 | + } |
| 44 | + println!("Total rows read: {}", total_rows); |
| 45 | + |
| 46 | + // Step 3: Read with row selection - skip first 30 rows, select next 40, skip rest |
| 47 | + println!("\n=== Reading with row selection ==="); |
| 48 | + let file = File::open(file_path)?; |
| 49 | + |
| 50 | + // Create a selection: skip 30, select 40, skip 30 |
| 51 | + let selection = vec![ |
| 52 | + RowSelector::skip(30), |
| 53 | + RowSelector::select(40), |
| 54 | + RowSelector::skip(30), |
| 55 | + ] |
| 56 | + .into(); |
| 57 | + |
| 58 | + let reader = ArrowReaderBuilder::try_new(file)? |
| 59 | + .with_row_selection(selection) |
| 60 | + .build(); |
| 61 | + |
| 62 | + let mut selected_rows = 0; |
| 63 | + let mut batches = Vec::new(); |
| 64 | + for batch in reader { |
| 65 | + let batch = batch?; |
| 66 | + selected_rows += batch.num_rows(); |
| 67 | + batches.push(batch); |
| 68 | + } |
| 69 | + |
| 70 | + println!("Total rows selected: {}", selected_rows); |
| 71 | + println!("Expected: 40, Actual: {}", selected_rows); |
| 72 | + |
| 73 | + // Display some of the selected data |
| 74 | + if let Some(first_batch) = batches.first() { |
| 75 | + let id_col = first_batch |
| 76 | + .column(0) |
| 77 | + .as_any() |
| 78 | + .downcast_ref::<Int32Array>() |
| 79 | + .unwrap(); |
| 80 | + let name_col = first_batch |
| 81 | + .column(1) |
| 82 | + .as_any() |
| 83 | + .downcast_ref::<StringArray>() |
| 84 | + .unwrap(); |
| 85 | + |
| 86 | + println!("\nFirst 5 selected rows:"); |
| 87 | + for i in 0..5.min(first_batch.num_rows()) { |
| 88 | + println!( |
| 89 | + " id: {}, name: {}", |
| 90 | + id_col.value(i), |
| 91 | + name_col.value(i) |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Step 4: Read with multiple non-consecutive selections |
| 97 | + println!("\n=== Reading with multiple selections ==="); |
| 98 | + let file = File::open(file_path)?; |
| 99 | + |
| 100 | + // Select rows 10-20 and 60-70 |
| 101 | + let selection = RowSelection::from_consecutive_ranges( |
| 102 | + vec![10..20, 60..70].into_iter(), |
| 103 | + 100, |
| 104 | + ); |
| 105 | + |
| 106 | + let reader = ArrowReaderBuilder::try_new(file)? |
| 107 | + .with_row_selection(selection) |
| 108 | + .build(); |
| 109 | + |
| 110 | + let mut selected_rows = 0; |
| 111 | + for batch in reader { |
| 112 | + let batch = batch?; |
| 113 | + selected_rows += batch.num_rows(); |
| 114 | + } |
| 115 | + |
| 116 | + println!("Total rows selected: {}", selected_rows); |
| 117 | + println!("Expected: 20 (10 from each range)"); |
| 118 | + |
| 119 | + println!("\n✓ Row selection example completed successfully!"); |
| 120 | + |
| 121 | + Ok(()) |
| 122 | +} |
| 123 | + |
| 124 | +fn create_sample_orc_file(path: &str) -> Result<(), Box<dyn std::error::Error>> { |
| 125 | + let schema = Arc::new(Schema::new(vec![ |
| 126 | + Field::new("id", DataType::Int32, false), |
| 127 | + Field::new("name", DataType::Utf8, false), |
| 128 | + ])); |
| 129 | + |
| 130 | + // Create 100 rows |
| 131 | + let ids: ArrayRef = Arc::new(Int32Array::from((0..100).collect::<Vec<i32>>())); |
| 132 | + let names: ArrayRef = Arc::new(StringArray::from( |
| 133 | + (0..100) |
| 134 | + .map(|i| format!("name_{}", i)) |
| 135 | + .collect::<Vec<String>>(), |
| 136 | + )); |
| 137 | + |
| 138 | + let batch = RecordBatch::try_new(schema.clone(), vec![ids, names])?; |
| 139 | + |
| 140 | + let file = File::create(path)?; |
| 141 | + let mut writer = ArrowWriterBuilder::new(file, schema).try_build()?; |
| 142 | + writer.write(&batch)?; |
| 143 | + writer.close()?; |
| 144 | + |
| 145 | + Ok(()) |
| 146 | +} |
| 147 | + |
0 commit comments