|
| 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 | +use super::incremental_scan::{IncrementalPlan, IncrementalScan, IncrementalScanMode}; |
| 19 | +use super::{ArrowRecordBatchStream, Table}; |
| 20 | +use crate::spec::{ |
| 21 | + BigIntType, DataField, DataType, VarCharType, ROW_KIND_FIELD_ID, ROW_KIND_FIELD_NAME, |
| 22 | + SEQUENCE_NUMBER_FIELD_ID, SEQUENCE_NUMBER_FIELD_NAME, |
| 23 | +}; |
| 24 | + |
| 25 | +/// Wrapper that exposes table rows with a leading `rowkind` audit column. |
| 26 | +/// |
| 27 | +/// Incremental reads produce: |
| 28 | +/// - Delta: primary-key rows use physical `_VALUE_KIND`; append rows are `+I` |
| 29 | +/// - Changelog: kinds come from physical `_VALUE_KIND` (`+I`/`-U`/`+U`/`-D`) |
| 30 | +/// - Diff: not implemented in this release |
| 31 | +#[derive(Debug, Clone)] |
| 32 | +pub struct AuditLogTable { |
| 33 | + wrapped: Table, |
| 34 | +} |
| 35 | + |
| 36 | +const TABLE_READ_SEQUENCE_NUMBER_ENABLED: &str = "table-read.sequence-number.enabled"; |
| 37 | + |
| 38 | +impl AuditLogTable { |
| 39 | + pub fn new(wrapped: Table) -> Self { |
| 40 | + Self { wrapped } |
| 41 | + } |
| 42 | + |
| 43 | + pub fn wrapped(&self) -> &Table { |
| 44 | + &self.wrapped |
| 45 | + } |
| 46 | + |
| 47 | + /// Logical fields: `rowkind` (+ optional `_SEQUENCE_NUMBER`) then table fields. |
| 48 | + pub fn fields(&self) -> crate::Result<Vec<DataField>> { |
| 49 | + let mut fields = Vec::with_capacity(self.wrapped.schema().fields().len() + 2); |
| 50 | + fields.push(DataField::new( |
| 51 | + ROW_KIND_FIELD_ID, |
| 52 | + ROW_KIND_FIELD_NAME.to_string(), |
| 53 | + DataType::VarChar(VarCharType::string_type()), |
| 54 | + )); |
| 55 | + if self.sequence_number_enabled() { |
| 56 | + fields.push(DataField::new( |
| 57 | + SEQUENCE_NUMBER_FIELD_ID, |
| 58 | + SEQUENCE_NUMBER_FIELD_NAME.to_string(), |
| 59 | + DataType::BigInt(BigIntType::new()), |
| 60 | + )); |
| 61 | + } |
| 62 | + fields.extend(self.wrapped.schema().fields().iter().cloned()); |
| 63 | + Ok(fields) |
| 64 | + } |
| 65 | + |
| 66 | + fn sequence_number_enabled(&self) -> bool { |
| 67 | + self.wrapped |
| 68 | + .schema() |
| 69 | + .options() |
| 70 | + .get(TABLE_READ_SEQUENCE_NUMBER_ENABLED) |
| 71 | + .is_some_and(|v| v.eq_ignore_ascii_case("true")) |
| 72 | + } |
| 73 | + |
| 74 | + pub fn new_incremental_scan( |
| 75 | + &self, |
| 76 | + mode: IncrementalScanMode, |
| 77 | + start_exclusive: i64, |
| 78 | + end_inclusive: i64, |
| 79 | + ) -> IncrementalScan<'_> { |
| 80 | + IncrementalScan::for_table(&self.wrapped, mode, start_exclusive, end_inclusive) |
| 81 | + } |
| 82 | + |
| 83 | + pub fn to_arrow(&self, plan: &IncrementalPlan) -> crate::Result<ArrowRecordBatchStream> { |
| 84 | + let read = self.wrapped.new_read_builder().new_read()?; |
| 85 | + read.to_audit_log_arrow(plan) |
| 86 | + } |
| 87 | +} |
0 commit comments