Skip to content

cass_result: implement missing functions #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions scylla-rust-wrapper/src/cass_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ include!(concat!(env!("OUT_DIR"), "/cppdriver_data_types.rs"));
include!(concat!(env!("OUT_DIR"), "/cppdriver_data_query_error.rs"));
include!(concat!(env!("OUT_DIR"), "/cppdriver_batch_types.rs"));

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UDTDataType {
// Vec to preserve the order of types
pub field_types: Vec<(String, Arc<CassDataType>)>,
Expand Down Expand Up @@ -131,14 +131,14 @@ impl Default for UDTDataType {
}
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MapDataType {
Untyped,
Key(Arc<CassDataType>),
KeyAndValue(Arc<CassDataType>, Arc<CassDataType>),
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CassDataType {
Value(CassValueType),
UDT(UDTDataType),
Expand Down
14 changes: 14 additions & 0 deletions scylla-rust-wrapper/src/prepared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ use scylla::prepared_statement::PreparedStatement;
pub struct CassPrepared {
// Data types of columns from PreparedMetadata.
pub variable_col_data_types: Vec<Arc<CassDataType>>,
// Data types of columns from ResultMetadata.
//
// Arc<CassDataType> -> to share each data type with other structs such as `CassValue`
// Arc<Vec<...>> -> to share the whole vector with `CassResultData`.
pub result_col_data_types: Arc<Vec<Arc<CassDataType>>>,
pub statement: PreparedStatement,
}

Expand All @@ -23,8 +28,17 @@ impl CassPrepared {
.map(|col_spec| Arc::new(get_column_type(&col_spec.typ)))
.collect();

let result_col_data_types: Arc<Vec<Arc<CassDataType>>> = Arc::new(
statement
.get_result_set_col_specs()
.iter()
.map(|col_spec| Arc::new(get_column_type(&col_spec.typ)))
.collect(),
);

Self {
variable_col_data_types,
result_col_data_types,
statement,
}
}
Expand Down
269 changes: 263 additions & 6 deletions scylla-rust-wrapper/src/query_result.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::argconv::*;
use crate::cass_error::CassError;
use crate::cass_types::{cass_data_type_type, CassDataType, CassValueType, MapDataType};
use crate::cass_types::{
cass_data_type_type, get_column_type, CassDataType, CassValueType, MapDataType,
};
use crate::inet::CassInet;
use crate::metadata::{
CassColumnMeta, CassKeyspaceMeta, CassMaterializedViewMeta, CassSchemaMeta, CassTableMeta,
Expand All @@ -22,9 +24,39 @@ pub struct CassResult {
pub struct CassResultData {
pub paging_state_response: PagingStateResponse,
pub col_specs: Vec<ColumnSpec>,
pub col_data_types: Arc<Vec<Arc<CassDataType>>>,
pub tracing_id: Option<Uuid>,
}

impl CassResultData {
pub fn from_result_payload(
paging_state_response: PagingStateResponse,
col_specs: Vec<ColumnSpec>,
maybe_col_data_types: Option<Arc<Vec<Arc<CassDataType>>>>,
tracing_id: Option<Uuid>,
) -> CassResultData {
// `maybe_col_data_types` is:
// - Some(_) for prepared statements executions
// - None for unprepared (simple) queries executions
let col_data_types = maybe_col_data_types.unwrap_or_else(|| {
// This allocation is unfortunately necessary, because of the type of CassResultData::col_data_types.
Arc::new(
col_specs
.iter()
.map(|col_spec| Arc::new(get_column_type(&col_spec.typ)))
.collect(),
)
});

CassResultData {
paging_state_response,
col_specs,
col_data_types,
tracing_id,
}
}
}

/// The lifetime of CassRow is bound to CassResult.
/// It will be freed, when CassResult is freed.(see #[cass_result_free])
pub struct CassRow {
Expand Down Expand Up @@ -905,6 +937,36 @@ pub unsafe extern "C" fn cass_result_column_name(
CassError::CASS_OK
}

#[no_mangle]
pub unsafe extern "C" fn cass_result_column_type(
result: *const CassResult,
index: size_t,
) -> CassValueType {
let data_type_ptr = cass_result_column_data_type(result, index);
if data_type_ptr.is_null() {
return CassValueType::CASS_VALUE_TYPE_UNKNOWN;
}
cass_data_type_type(data_type_ptr)
}

#[no_mangle]
pub unsafe extern "C" fn cass_result_column_data_type(
result: *const CassResult,
index: size_t,
) -> *const CassDataType {
let result_from_raw: &CassResult = ptr_to_ref(result);
let index_usize: usize = index
.try_into()
.expect("Provided index is out of bounds. Max possible value is usize::MAX");

result_from_raw
.metadata
.col_data_types
.get(index_usize)
.map(Arc::as_ptr)
.unwrap_or(std::ptr::null())
}

#[no_mangle]
pub unsafe extern "C" fn cass_value_type(value: *const CassValue) -> CassValueType {
let value_from_raw = ptr_to_ref(value);
Expand Down Expand Up @@ -1283,11 +1345,12 @@ pub unsafe extern "C" fn cass_result_column_count(result_raw: *const CassResult)
pub unsafe extern "C" fn cass_result_first_row(result_raw: *const CassResult) -> *const CassRow {
let result = ptr_to_ref(result_raw);

if result.rows.is_some() || result.rows.as_ref().unwrap().is_empty() {
return result.rows.as_ref().unwrap().first().unwrap();
}

std::ptr::null()
result
.rows
.as_ref()
.and_then(|rows| rows.first())
.map(|row| row as *const CassRow)
.unwrap_or(std::ptr::null())
}

#[no_mangle]
Expand Down Expand Up @@ -1322,6 +1385,200 @@ pub unsafe extern "C" fn cass_result_paging_state_token(
CassError::CASS_OK
}

#[cfg(test)]
mod tests {
use std::{ffi::c_char, ptr::addr_of_mut, sync::Arc};

use scylla::{
frame::response::result::{ColumnSpec, ColumnType, CqlValue, Row, TableSpec},
transport::PagingStateResponse,
};

use crate::{
cass_error::CassError,
cass_types::{CassDataType, CassValueType},
query_result::{
cass_result_column_data_type, cass_result_column_name, cass_result_first_row,
ptr_to_cstr_n, ptr_to_ref, size_t,
},
session::create_cass_rows_from_rows,
};

use super::{cass_result_column_count, cass_result_column_type, CassResult, CassResultData};

fn col_spec(name: &str, typ: ColumnType) -> ColumnSpec {
ColumnSpec {
table_spec: TableSpec::borrowed("ks", "tbl"),
name: name.to_owned(),
typ,
}
}

const FIRST_COLUMN_NAME: &str = "bigint_col";
const SECOND_COLUMN_NAME: &str = "varint_col";
const THIRD_COLUMN_NAME: &str = "list_double_col";
fn create_cass_rows_result() -> CassResult {
let metadata = Arc::new(CassResultData::from_result_payload(
PagingStateResponse::NoMorePages,
vec![
col_spec(FIRST_COLUMN_NAME, ColumnType::BigInt),
col_spec(SECOND_COLUMN_NAME, ColumnType::Varint),
col_spec(
THIRD_COLUMN_NAME,
ColumnType::List(Box::new(ColumnType::Double)),
),
],
None,
None,
));

let rows = create_cass_rows_from_rows(
vec![Row {
columns: vec![
Some(CqlValue::BigInt(42)),
None,
Some(CqlValue::List(vec![
CqlValue::Float(0.5),
CqlValue::Float(42.42),
CqlValue::Float(9999.9999),
])),
],
}],
&metadata,
);

CassResult {
rows: Some(rows),
metadata,
}
}

unsafe fn cass_result_column_name_rust_str(
result_ptr: *const CassResult,
column_index: u64,
) -> Option<&'static str> {
let mut name_ptr: *const c_char = std::ptr::null();
let mut name_length: size_t = 0;
let cass_err = cass_result_column_name(
result_ptr,
column_index,
addr_of_mut!(name_ptr),
addr_of_mut!(name_length),
);
assert_eq!(CassError::CASS_OK, cass_err);
ptr_to_cstr_n(name_ptr, name_length)
}

#[test]
fn rows_cass_result_api_test() {
let result = create_cass_rows_result();

unsafe {
let result_ptr = std::ptr::addr_of!(result);

// cass_result_column_count test
{
let column_count = cass_result_column_count(result_ptr);
assert_eq!(3, column_count);
}

// cass_result_column_name test
{
let first_column_name = cass_result_column_name_rust_str(result_ptr, 0).unwrap();
assert_eq!(FIRST_COLUMN_NAME, first_column_name);
let second_column_name = cass_result_column_name_rust_str(result_ptr, 1).unwrap();
assert_eq!(SECOND_COLUMN_NAME, second_column_name);
let third_column_name = cass_result_column_name_rust_str(result_ptr, 2).unwrap();
assert_eq!(THIRD_COLUMN_NAME, third_column_name);
}

// cass_result_column_type test
{
let first_col_type = cass_result_column_type(result_ptr, 0);
assert_eq!(CassValueType::CASS_VALUE_TYPE_BIGINT, first_col_type);
let second_col_type = cass_result_column_type(result_ptr, 1);
assert_eq!(CassValueType::CASS_VALUE_TYPE_VARINT, second_col_type);
let third_col_type = cass_result_column_type(result_ptr, 2);
assert_eq!(CassValueType::CASS_VALUE_TYPE_LIST, third_col_type);
let out_of_bound_col_type = cass_result_column_type(result_ptr, 555);
assert_eq!(
CassValueType::CASS_VALUE_TYPE_UNKNOWN,
out_of_bound_col_type
);
}

// cass_result_column_data_type test
{
let first_col_data_type = ptr_to_ref(cass_result_column_data_type(result_ptr, 0));
assert_eq!(
&CassDataType::Value(CassValueType::CASS_VALUE_TYPE_BIGINT),
first_col_data_type
);
let second_col_data_type = ptr_to_ref(cass_result_column_data_type(result_ptr, 1));
assert_eq!(
&CassDataType::Value(CassValueType::CASS_VALUE_TYPE_VARINT),
second_col_data_type
);
let third_col_data_type = ptr_to_ref(cass_result_column_data_type(result_ptr, 2));
assert_eq!(
&CassDataType::List {
typ: Some(Arc::new(CassDataType::Value(
CassValueType::CASS_VALUE_TYPE_DOUBLE
))),
frozen: false
},
third_col_data_type
);
let out_of_bound_col_data_type = cass_result_column_data_type(result_ptr, 555);
assert!(out_of_bound_col_data_type.is_null());
}
}
}

fn create_non_rows_cass_result() -> CassResult {
let metadata = Arc::new(CassResultData::from_result_payload(
PagingStateResponse::NoMorePages,
vec![],
None,
None,
));
CassResult {
rows: None,
metadata,
}
}

#[test]
fn non_rows_cass_result_api_test() {
let result = create_non_rows_cass_result();

// Check that API functions do not panic when rows are empty - e.g. for INSERT queries.
unsafe {
let result_ptr = std::ptr::addr_of!(result);

assert_eq!(0, cass_result_column_count(result_ptr));
assert_eq!(
CassValueType::CASS_VALUE_TYPE_UNKNOWN,
cass_result_column_type(result_ptr, 0)
);
assert!(cass_result_column_data_type(result_ptr, 0).is_null());
assert!(cass_result_first_row(result_ptr).is_null());

{
let mut name_ptr: *const c_char = std::ptr::null();
let mut name_length: size_t = 0;
let cass_err = cass_result_column_name(
result_ptr,
0,
addr_of_mut!(name_ptr),
addr_of_mut!(name_length),
);
assert_eq!(CassError::CASS_ERROR_LIB_INDEX_OUT_OF_BOUNDS, cass_err);
}
}
}
}

// CassResult functions:
/*
extern "C" {
Expand Down
Loading