Skip to content

Commit 7636fda

Browse files
committed
feat(c): default branch to main when null in table_from_schema_json
Allow the branch pointer of paimon_table_from_schema_json to be null, defaulting to DEFAULT_MAIN_BRANCH ("main"). A non-null pointer still goes through the full UTF-8 and branch-name validation, so invalid names such as "../dev" are still rejected. Update the doc and Safety comment accordingly and add a test covering the null-branch default.
1 parent 9c39365 commit 7636fda

2 files changed

Lines changed: 50 additions & 11 deletions

File tree

bindings/c/src/table.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::ffi::{c_char, c_void};
2121
use arrow_array::ffi::{FFI_ArrowArray, FFI_ArrowSchema};
2222
use arrow_array::{Array, StructArray};
2323
use futures::StreamExt;
24-
use paimon::catalog::Identifier;
24+
use paimon::catalog::{Identifier, DEFAULT_MAIN_BRANCH};
2525
use paimon::io::FileIO;
2626
use paimon::spec::{DataField, DataType, Datum, Predicate, PredicateBuilder, TableSchema};
2727
use paimon::table::{ArrowRecordBatchStream, DataSplit, Table};
@@ -66,12 +66,14 @@ unsafe fn box_table_read_state(state: TableReadState) -> *mut paimon_table_read
6666
/// This constructor does not create a catalog or derive a warehouse. Storage
6767
/// options are used only to build FileIO; they are not merged into the supplied
6868
/// table schema. `branch` selects the branch-scoped managers while preserving
69-
/// the supplied schema.
69+
/// the supplied schema; pass null to default to the `main` branch.
7070
///
7171
/// # Safety
72-
/// All string pointers must be valid null-terminated C strings. `storage_options`
73-
/// must point to `storage_options_len` valid `paimon_option` values, or be null
74-
/// when `storage_options_len` is 0.
72+
/// All string pointers except `branch` must be valid null-terminated C strings.
73+
/// `branch` may be null to select the default `main` branch, or a valid
74+
/// null-terminated C string. `storage_options` must point to
75+
/// `storage_options_len` valid `paimon_option` values, or be null when
76+
/// `storage_options_len` is 0.
7577
#[no_mangle]
7678
pub unsafe extern "C" fn paimon_table_from_schema_json(
7779
table_path: *const c_char,
@@ -118,12 +120,16 @@ pub unsafe extern "C" fn paimon_table_from_schema_json(
118120
}
119121
}
120122
};
121-
let branch = match validate_cstr(branch, "branch") {
122-
Ok(value) => value,
123-
Err(error) => {
124-
return paimon_result_get_table {
125-
table: std::ptr::null_mut(),
126-
error,
123+
let branch = if branch.is_null() {
124+
DEFAULT_MAIN_BRANCH.to_string()
125+
} else {
126+
match validate_cstr(branch, "branch") {
127+
Ok(value) => value,
128+
Err(error) => {
129+
return paimon_result_get_table {
130+
table: std::ptr::null_mut(),
131+
error,
132+
}
127133
}
128134
}
129135
};

bindings/c/src/tests.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,39 @@ fn test_table_from_schema_json_main_branch_uses_main_schema_directory() {
392392
}
393393
}
394394

395+
#[test]
396+
fn test_table_from_schema_json_null_branch_defaults_to_main() {
397+
let path = CString::new("memory:/test_resolved_null_branch").unwrap();
398+
let database = CString::new("default").unwrap();
399+
let table_name = CString::new("test").unwrap();
400+
let schema = simple_table_schema();
401+
let schema_json = CString::new(serde_json::to_string(&schema).unwrap()).unwrap();
402+
403+
unsafe {
404+
let result = paimon_table_from_schema_json(
405+
path.as_ptr(),
406+
schema_json.as_ptr(),
407+
database.as_ptr(),
408+
table_name.as_ptr(),
409+
std::ptr::null(),
410+
std::ptr::null(),
411+
0,
412+
);
413+
414+
assert!(result.error.is_null());
415+
assert!(!result.table.is_null());
416+
let table = table_ref(result.table);
417+
assert_eq!(table.branch(), "main");
418+
assert!(!table.is_branch_reference());
419+
assert_eq!(
420+
table.schema_manager().schema_path(schema.id()),
421+
"memory:/test_resolved_null_branch/schema/schema-0"
422+
);
423+
424+
paimon_table_free(result.table);
425+
}
426+
}
427+
395428
#[test]
396429
fn test_table_from_schema_json_rejects_invalid_input() {
397430
let path = CString::new("memory:/test_resolved_invalid").unwrap();

0 commit comments

Comments
 (0)