|
| 1 | +""" |
| 2 | +Simple example demonstrating align_tables usage for join operations. |
| 3 | +""" |
| 4 | +import numpy as np |
| 5 | +import pyarrow as pa |
| 6 | + |
| 7 | +from opteryx.draken import Morsel |
| 8 | +from opteryx.draken.morsels import align_tables_pyarray |
| 9 | + |
| 10 | + |
| 11 | +def example_1_basic_alignment(): |
| 12 | + """Basic example: align two tables by indices.""" |
| 13 | + print("Example 1: Basic Alignment") |
| 14 | + print("-" * 50) |
| 15 | + |
| 16 | + # Create left table |
| 17 | + left = pa.table({ |
| 18 | + "user_id": pa.array([1, 2, 3, 4, 5], type=pa.int64()), |
| 19 | + "name": pa.array(["Alice", "Bob", "Charlie", "David", "Eve"]), |
| 20 | + "age": pa.array([25, 30, 35, 40, 45], type=pa.int64()), |
| 21 | + }) |
| 22 | + |
| 23 | + # Create right table |
| 24 | + right = pa.table({ |
| 25 | + "order_id": pa.array([101, 102, 103, 104, 105], type=pa.int64()), |
| 26 | + "amount": pa.array([100.0, 200.0, 150.0, 300.0, 250.0]), |
| 27 | + }) |
| 28 | + |
| 29 | + # Convert to morsels |
| 30 | + left_morsel = Morsel.from_arrow(left) |
| 31 | + right_morsel = Morsel.from_arrow(right) |
| 32 | + |
| 33 | + # Align: take rows [0, 2, 4] from left and [1, 3, 4] from right |
| 34 | + left_indices = np.array([0, 2, 4], dtype=np.int32) |
| 35 | + right_indices = np.array([1, 3, 4], dtype=np.int32) |
| 36 | + |
| 37 | + result = align_tables_pyarray(left_morsel, right_morsel, left_indices, right_indices) |
| 38 | + |
| 39 | + print(f"Input shapes: {left_morsel.shape} + {right_morsel.shape}") |
| 40 | + print(f"Output shape: {result.shape}") |
| 41 | + print(f"Columns: {result.column_names}\n") |
| 42 | + |
| 43 | + result_arrow = result.to_arrow() |
| 44 | + print(result_arrow) |
| 45 | + print() |
| 46 | + |
| 47 | + |
| 48 | +def example_2_inner_join(): |
| 49 | + """Example 2: Simulate an inner join.""" |
| 50 | + print("Example 2: Inner Join Simulation") |
| 51 | + print("-" * 50) |
| 52 | + |
| 53 | + # Users table |
| 54 | + users = pa.table({ |
| 55 | + "user_id": pa.array([1, 2, 3, 4, 5], type=pa.int64()), |
| 56 | + "name": pa.array(["Alice", "Bob", "Charlie", "David", "Eve"]), |
| 57 | + }) |
| 58 | + |
| 59 | + # Orders table |
| 60 | + orders = pa.table({ |
| 61 | + "order_id": pa.array([101, 102, 103, 104], type=pa.int64()), |
| 62 | + "user_id": pa.array([2, 1, 4, 2], type=pa.int64()), # Foreign key |
| 63 | + "amount": pa.array([100.0, 200.0, 150.0, 300.0]), |
| 64 | + }) |
| 65 | + |
| 66 | + users_morsel = Morsel.from_arrow(users) |
| 67 | + orders_morsel = Morsel.from_arrow(orders) |
| 68 | + |
| 69 | + # Simulate hash join: match user_id |
| 70 | + # orders.user_id = [2, 1, 4, 2] |
| 71 | + # Match to users indices: [1, 0, 3, 1] |
| 72 | + user_indices = np.array([1, 0, 3, 1], dtype=np.int32) # Bob, Alice, David, Bob |
| 73 | + order_indices = np.array([0, 1, 2, 3], dtype=np.int32) # All orders |
| 74 | + |
| 75 | + result = align_tables_pyarray(users_morsel, orders_morsel, user_indices, order_indices) |
| 76 | + |
| 77 | + print("Joined users with their orders:") |
| 78 | + print(result.to_arrow()) |
| 79 | + print() |
| 80 | + |
| 81 | + |
| 82 | +def example_3_duplicate_columns(): |
| 83 | + """Example 3: Handle duplicate column names.""" |
| 84 | + print("Example 3: Duplicate Column Handling") |
| 85 | + print("-" * 50) |
| 86 | + |
| 87 | + left = pa.table({ |
| 88 | + "id": pa.array([1, 2, 3], type=pa.int64()), |
| 89 | + "value": pa.array([10, 20, 30], type=pa.int64()), |
| 90 | + }) |
| 91 | + |
| 92 | + right = pa.table({ |
| 93 | + "id": pa.array([4, 5, 6], type=pa.int64()), # Same column name! |
| 94 | + "score": pa.array([100, 200, 300], type=pa.int64()), |
| 95 | + }) |
| 96 | + |
| 97 | + left_morsel = Morsel.from_arrow(left) |
| 98 | + right_morsel = Morsel.from_arrow(right) |
| 99 | + |
| 100 | + indices = np.array([0, 1, 2], dtype=np.int32) |
| 101 | + |
| 102 | + result = align_tables_pyarray(left_morsel, right_morsel, indices, indices) |
| 103 | + |
| 104 | + print("Note: 'id' from right table is ignored (left takes precedence)") |
| 105 | + print(f"Result columns: {result.column_names}") |
| 106 | + print(result.to_arrow()) |
| 107 | + print() |
| 108 | + |
| 109 | + |
| 110 | +def example_4_large_scale(): |
| 111 | + """Example 4: Large-scale performance.""" |
| 112 | + print("Example 4: Large-Scale Performance") |
| 113 | + print("-" * 50) |
| 114 | + |
| 115 | + import time |
| 116 | + |
| 117 | + n_rows = 1_000_000 |
| 118 | + |
| 119 | + # Large left table |
| 120 | + left = pa.table({ |
| 121 | + "id": pa.array(np.arange(n_rows, dtype=np.int64)), |
| 122 | + "val1": pa.array(np.random.rand(n_rows)), |
| 123 | + "val2": pa.array(np.random.rand(n_rows)), |
| 124 | + "val3": pa.array(np.random.rand(n_rows)), |
| 125 | + }) |
| 126 | + |
| 127 | + # Large right table |
| 128 | + right = pa.table({ |
| 129 | + "metric1": pa.array(np.random.rand(n_rows)), |
| 130 | + "metric2": pa.array(np.random.rand(n_rows)), |
| 131 | + }) |
| 132 | + |
| 133 | + left_morsel = Morsel.from_arrow(left) |
| 134 | + right_morsel = Morsel.from_arrow(right) |
| 135 | + |
| 136 | + # Select 50% of rows |
| 137 | + sample_size = n_rows // 2 |
| 138 | + indices = np.random.choice(n_rows, size=sample_size, replace=True).astype(np.int32) |
| 139 | + |
| 140 | + print(f"Left table: {left_morsel.shape}") |
| 141 | + print(f"Right table: {right_morsel.shape}") |
| 142 | + print(f"Sample size: {sample_size:,} rows") |
| 143 | + |
| 144 | + # Warmup |
| 145 | + _ = align_tables_pyarray(left_morsel, right_morsel, indices, indices) |
| 146 | + |
| 147 | + # Benchmark |
| 148 | + num_runs = 5 |
| 149 | + times = [] |
| 150 | + |
| 151 | + for _ in range(num_runs): |
| 152 | + start = time.perf_counter() |
| 153 | + result = align_tables_pyarray(left_morsel, right_morsel, indices, indices) |
| 154 | + elapsed = time.perf_counter() - start |
| 155 | + times.append(elapsed) |
| 156 | + |
| 157 | + avg_time = np.mean(times) * 1000 |
| 158 | + throughput = sample_size / (avg_time / 1000) / 1_000_000 |
| 159 | + |
| 160 | + print("\nPerformance:") |
| 161 | + print(f" Average time: {avg_time:.1f} ms") |
| 162 | + print(f" Throughput: {throughput:.1f} M rows/sec") |
| 163 | + print(f" Result shape: {result.shape}") |
| 164 | + print() |
| 165 | + |
| 166 | + |
| 167 | +def example_5_mixed_types(): |
| 168 | + """Example 5: Mixed data types.""" |
| 169 | + print("Example 5: Mixed Data Types") |
| 170 | + print("-" * 50) |
| 171 | + |
| 172 | + left = pa.table({ |
| 173 | + "int_col": pa.array([1, 2, 3], type=pa.int64()), |
| 174 | + "float_col": pa.array([1.1, 2.2, 3.3]), |
| 175 | + "string_col": pa.array(["a", "b", "c"]), |
| 176 | + "bool_col": pa.array([True, False, True]), |
| 177 | + }) |
| 178 | + |
| 179 | + right = pa.table({ |
| 180 | + "date_col": pa.array([ |
| 181 | + pa.scalar(1, type=pa.date32()), |
| 182 | + pa.scalar(2, type=pa.date32()), |
| 183 | + pa.scalar(3, type=pa.date32()), |
| 184 | + ]), |
| 185 | + "timestamp_col": pa.array([ |
| 186 | + pa.scalar(1000, type=pa.timestamp('us')), |
| 187 | + pa.scalar(2000, type=pa.timestamp('us')), |
| 188 | + pa.scalar(3000, type=pa.timestamp('us')), |
| 189 | + ]), |
| 190 | + }) |
| 191 | + |
| 192 | + left_morsel = Morsel.from_arrow(left) |
| 193 | + right_morsel = Morsel.from_arrow(right) |
| 194 | + |
| 195 | + indices = np.array([0, 2], dtype=np.int32) |
| 196 | + |
| 197 | + result = align_tables_pyarray(left_morsel, right_morsel, indices, indices) |
| 198 | + |
| 199 | + print(f"Handling {result.num_columns} columns of different types:") |
| 200 | + for name, dtype in zip(result.column_names, result.column_types): |
| 201 | + print(f" {name.decode('utf-8')}: {dtype}") |
| 202 | + |
| 203 | + print(f"\nResult:\n{result.to_arrow()}") |
| 204 | + print() |
| 205 | + |
| 206 | + |
| 207 | +if __name__ == "__main__": |
| 208 | + print("=" * 50) |
| 209 | + print("align_tables Usage Examples") |
| 210 | + print("=" * 50) |
| 211 | + print() |
| 212 | + |
| 213 | + example_1_basic_alignment() |
| 214 | + example_2_inner_join() |
| 215 | + example_3_duplicate_columns() |
| 216 | + example_4_large_scale() |
| 217 | + example_5_mixed_types() |
| 218 | + |
| 219 | + print("=" * 50) |
| 220 | + print("All examples completed!") |
| 221 | + print("=" * 50) |
0 commit comments