Function arrow_odbc::arrow_schema_from
source · pub fn arrow_schema_from(
resut_set_metadata: &mut impl ResultSetMetadata
) -> Result<Schema, Error>Expand description
Query the metadata to create an arrow schema. This method is invoked automatically for you by
crate::OdbcReader::new. You may want to call this method in situtation ther you want to
create an arrow schema without creating the reader yet.
Example
use anyhow::Error;
use arrow_odbc::{arrow_schema_from, arrow::datatypes::Schema, odbc_api::Connection};
fn fetch_schema_for_table(
table_name: &str,
connection: &Connection<'_>
) -> Result<Schema, Error> {
// Query column with values to get a cursor
let sql = format!("SELECT * FROM {}", table_name);
let mut prepared = connection.prepare(&sql)?;
// Now that we have prepared statement, we want to use it to query metadata.
let schema = arrow_schema_from(&mut prepared)?;
Ok(schema)
}Examples found in repository?
src/odbc_reader.rs (line 94)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
pub fn new(mut cursor: C, max_batch_size: usize) -> Result<Self, Error> {
// Get number of columns from result set. We know it to contain at least one column,
// otherwise it would not have been created.
let schema = Arc::new(arrow_schema_from(&mut cursor)?);
Self::with_arrow_schema(cursor, max_batch_size, schema)
}
/// Construct a new `OdbcReader instance.
///
/// # Parameters
///
/// * `cursor`: ODBC cursor used to fetch batches from the data source. The constructor will
/// bind buffers to this cursor in order to perform bulk fetches from the source. This is
/// usually faster than fetching results row by row as it saves roundtrips to the database.
/// The type of these buffers will be inferred from the arrow schema. Not every arrow type is
/// supported though.
/// * `max_batch_size`: Maximum batch size requested from the datasource.
/// * `schema`: Arrow schema. Describes the type of the Arrow Arrays in the record batches, but
/// is also used to determine CData type requested from the data source.
pub fn with_arrow_schema(
cursor: C,
max_batch_size: usize,
schema: SchemaRef,
) -> Result<Self, Error> {
Self::with(
cursor,
max_batch_size,
Some(schema),
BufferAllocationOptions::default(),
)
}
/// Construct a new [`crate::OdbcReader`] instance. This method allows you full control over
/// what options to explicitly specify, and what options you want to leave to this crate to
/// automatically decide.
///
/// # Parameters
///
/// * `cursor`: ODBC cursor used to fetch batches from the data source. The constructor will
/// bind buffers to this cursor in order to perform bulk fetches from the source. This is
/// usually faster than fetching results row by row as it saves roundtrips to the database.
/// The type of these buffers will be inferred from the arrow schema. Not every arrow type is
/// supported though.
/// * `max_batch_size`: Maximum batch size requested from the datasource.
/// * `schema`: Arrow schema. Describes the type of the Arrow Arrays in the record batches, but
/// is also used to determine CData type requested from the data source. Set to `None` to
/// infer schema from the data source.
/// * `buffer_allocation_options`: Allows you to specify upper limits for binary and / or text
/// buffer types. This is useful support fetching data from e.g. VARCHAR(max) or
/// VARBINARY(max) columns, which otherwise might lead to errors, due to the ODBC driver
/// having a hard time specifying a good upper bound for the largest possible expected value.
pub fn with(
mut cursor: C,
max_batch_size: usize,
schema: Option<SchemaRef>,
buffer_allocation_options: BufferAllocationOptions,
) -> Result<Self, Error> {
// Infer schema if not given by the user
let schema = if let Some(schema) = schema {
schema
} else {
Arc::new(arrow_schema_from(&mut cursor)?)
};
let column_strategies: Vec<Box<dyn ReadStrategy>> = schema
.fields()
.iter()
.enumerate()
.map(|(index, field)| {
let col_index = (index + 1).try_into().unwrap();
choose_column_strategy(field, &mut cursor, col_index, buffer_allocation_options)
.map_err(|cause| cause.into_crate_error(field.name().clone(), index))
})
.collect::<Result<_, _>>()?;
let descs = column_strategies.iter().map(|cs| cs.buffer_desc());
let row_set_buffer = if buffer_allocation_options.fallibale_allocations {
ColumnarAnyBuffer::try_from_descs(max_batch_size, descs)
.map_err(|err| map_allocation_error(err, &schema))?
} else {
ColumnarAnyBuffer::from_descs(max_batch_size, descs)
};
let cursor = cursor.bind_buffer(row_set_buffer).unwrap();
Ok(Self {
column_strategies,
schema,
cursor,
})
}