rusqlite/src/vtab/series.rs

313 lines
9.7 KiB
Rust
Raw Normal View History

//! `feature = "series"` Generate series virtual table.
2018-07-15 16:19:18 +08:00
//!
//! Port of C [generate series
//! "function"](http://www.sqlite.org/cgi/src/finfo?name=ext/misc/series.c):
2020-11-07 19:32:41 +08:00
//! `https://www.sqlite.org/series.html`
2016-08-15 01:53:47 +08:00
use std::default::Default;
use std::marker::PhantomData;
2018-07-15 00:47:52 +08:00
use std::os::raw::c_int;
2016-08-15 01:53:47 +08:00
2018-10-31 03:11:35 +08:00
use crate::ffi;
use crate::types::Type;
use crate::vtab::{
2020-04-14 22:09:50 +08:00
eponymous_only_module, Context, IndexConstraintOp, IndexInfo, VTab, VTabConnection, VTabCursor,
Values,
2018-07-15 01:21:03 +08:00
};
2021-02-28 19:43:46 +08:00
use crate::{Connection, Error, Result};
2016-08-15 01:53:47 +08:00
/// `feature = "series"` Register the "generate_series" module.
2016-08-15 01:53:47 +08:00
pub fn load_module(conn: &Connection) -> Result<()> {
let aux: Option<()> = None;
2020-04-14 22:09:50 +08:00
conn.create_module("generate_series", eponymous_only_module::<SeriesTab>(), aux)
}
2016-08-15 01:53:47 +08:00
// Column numbers
// const SERIES_COLUMN_VALUE : c_int = 0;
const SERIES_COLUMN_START: c_int = 1;
const SERIES_COLUMN_STOP: c_int = 2;
const SERIES_COLUMN_STEP: c_int = 3;
2016-08-15 01:53:47 +08:00
bitflags::bitflags! {
2016-08-15 01:53:47 +08:00
#[repr(C)]
struct QueryPlanFlags: ::std::os::raw::c_int {
2016-08-15 01:53:47 +08:00
// start = $value -- constraint exists
const START = 1;
2016-08-15 01:53:47 +08:00
// stop = $value -- constraint exists
const STOP = 2;
2016-08-15 01:53:47 +08:00
// step = $value -- constraint exists
const STEP = 4;
2016-08-15 01:53:47 +08:00
// output in descending order
const DESC = 8;
2021-02-28 19:43:46 +08:00
// output in descending order
const ASC = 16;
2016-08-15 01:53:47 +08:00
// Both start and stop
2018-01-27 17:32:58 +08:00
const BOTH = QueryPlanFlags::START.bits | QueryPlanFlags::STOP.bits;
2016-08-15 01:53:47 +08:00
}
}
/// An instance of the Series virtual table
#[repr(C)]
struct SeriesTab {
/// Base class. Must be first
base: ffi::sqlite3_vtab,
}
unsafe impl<'vtab> VTab<'vtab> for SeriesTab {
2018-07-15 00:47:52 +08:00
type Aux = ();
type Cursor = SeriesTabCursor<'vtab>;
2018-07-15 00:47:52 +08:00
fn connect(
_: &mut VTabConnection,
_aux: Option<&()>,
_args: &[&[u8]],
) -> Result<(String, SeriesTab)> {
let vtab = SeriesTab {
base: ffi::sqlite3_vtab::default(),
};
Ok((
"CREATE TABLE x(value,start hidden,stop hidden,step hidden)".to_owned(),
vtab,
))
}
2016-08-15 01:53:47 +08:00
fn best_index(&self, info: &mut IndexInfo) -> Result<()> {
// The query plan bitmask
let mut idx_num: QueryPlanFlags = QueryPlanFlags::empty();
2021-02-28 19:43:46 +08:00
// Mask of unusable constraints
let mut unusable_mask: QueryPlanFlags = QueryPlanFlags::empty();
// Constraints on start, stop, and step
let mut a_idx: [Option<usize>; 3] = [None, None, None];
for (i, constraint) in info.constraints().enumerate() {
2021-02-28 19:43:46 +08:00
if constraint.column() < SERIES_COLUMN_START {
2016-08-15 01:53:47 +08:00
continue;
}
2021-02-28 19:43:46 +08:00
let (i_col, i_mask) = match constraint.column() {
SERIES_COLUMN_START => (0, QueryPlanFlags::START),
SERIES_COLUMN_STOP => (1, QueryPlanFlags::STOP),
SERIES_COLUMN_STEP => (2, QueryPlanFlags::STEP),
_ => {
unreachable!()
2016-08-15 01:53:47 +08:00
}
};
2021-02-28 19:43:46 +08:00
if !constraint.is_usable() {
unusable_mask |= i_mask;
} else if constraint.operator() == IndexConstraintOp::SQLITE_INDEX_CONSTRAINT_EQ {
idx_num |= i_mask;
a_idx[i_col] = Some(i);
}
2016-08-15 01:53:47 +08:00
}
2021-02-28 19:43:46 +08:00
// Number of arguments that SeriesTabCursor::filter expects
let mut n_arg = 0;
for j in a_idx.iter().flatten() {
n_arg += 1;
let mut constraint_usage = info.constraint_usage(*j);
constraint_usage.set_argv_index(n_arg);
2016-08-20 18:06:24 +08:00
constraint_usage.set_omit(true);
2016-08-15 01:53:47 +08:00
}
2021-02-28 19:43:46 +08:00
if !(unusable_mask & !idx_num).is_empty() {
2021-02-28 19:58:59 +08:00
return Err(Error::SqliteFailure(
ffi::Error::new(ffi::SQLITE_CONSTRAINT),
None,
));
2016-08-15 01:53:47 +08:00
}
2018-01-27 17:32:58 +08:00
if idx_num.contains(QueryPlanFlags::BOTH) {
2016-08-15 01:53:47 +08:00
// Both start= and stop= boundaries are available.
2018-05-14 01:49:11 +08:00
info.set_estimated_cost(f64::from(
2 - if idx_num.contains(QueryPlanFlags::STEP) {
2018-05-06 23:21:36 +08:00
1
} else {
0
2018-05-14 01:49:11 +08:00
},
));
2016-08-15 01:53:47 +08:00
info.set_estimated_rows(1000);
2018-07-10 00:53:52 +08:00
let order_by_consumed = {
let mut order_bys = info.order_bys();
if let Some(order_by) = order_bys.next() {
if order_by.is_order_by_desc() {
idx_num |= QueryPlanFlags::DESC;
2021-02-28 19:43:46 +08:00
} else {
idx_num |= QueryPlanFlags::ASC;
2018-07-10 00:53:52 +08:00
}
true
} else {
false
2016-08-15 01:53:47 +08:00
}
2018-07-10 00:53:52 +08:00
};
if order_by_consumed {
2016-08-15 01:53:47 +08:00
info.set_order_by_consumed(true);
}
} else {
2021-02-28 19:43:46 +08:00
// If either boundary is missing, we have to generate a huge span
// of numbers. Make this case very expensive so that the query
// planner will work hard to avoid it.
2018-05-14 01:16:12 +08:00
info.set_estimated_rows(2_147_483_647);
2016-08-15 01:53:47 +08:00
}
info.set_idx_num(idx_num.bits());
Ok(())
}
2020-06-02 00:42:30 +08:00
fn open(&self) -> Result<SeriesTabCursor<'_>> {
2016-08-15 01:53:47 +08:00
Ok(SeriesTabCursor::new())
}
}
/// A cursor for the Series virtual table
#[repr(C)]
struct SeriesTabCursor<'vtab> {
2016-08-15 01:53:47 +08:00
/// Base class. Must be first
base: ffi::sqlite3_vtab_cursor,
/// True to count down rather than up
is_desc: bool,
/// The rowid
row_id: i64,
/// Current value ("value")
value: i64,
2021-05-02 19:46:04 +08:00
/// Minimum value ("start")
2016-08-15 01:53:47 +08:00
min_value: i64,
/// Maximum value ("stop")
max_value: i64,
/// Increment ("step")
step: i64,
phantom: PhantomData<&'vtab SeriesTab>,
2016-08-15 01:53:47 +08:00
}
impl SeriesTabCursor<'_> {
fn new<'vtab>() -> SeriesTabCursor<'vtab> {
SeriesTabCursor {
base: ffi::sqlite3_vtab_cursor::default(),
is_desc: false,
row_id: 0,
value: 0,
min_value: 0,
max_value: 0,
step: 0,
phantom: PhantomData,
}
2016-08-15 01:53:47 +08:00
}
}
unsafe impl VTabCursor for SeriesTabCursor<'_> {
2018-12-08 04:57:04 +08:00
fn filter(&mut self, idx_num: c_int, _idx_str: Option<&str>, args: &Values<'_>) -> Result<()> {
2021-02-28 19:43:46 +08:00
let mut idx_num = QueryPlanFlags::from_bits_truncate(idx_num);
2016-08-15 01:53:47 +08:00
let mut i = 0;
2018-01-27 17:32:58 +08:00
if idx_num.contains(QueryPlanFlags::START) {
2018-10-31 03:11:35 +08:00
self.min_value = args.get(i)?;
2016-08-15 01:53:47 +08:00
i += 1;
} else {
self.min_value = 0;
}
2018-01-27 17:32:58 +08:00
if idx_num.contains(QueryPlanFlags::STOP) {
2018-10-31 03:11:35 +08:00
self.max_value = args.get(i)?;
2016-08-15 01:53:47 +08:00
i += 1;
} else {
2018-05-14 01:16:12 +08:00
self.max_value = 0xffff_ffff;
2016-08-15 01:53:47 +08:00
}
2018-01-27 17:32:58 +08:00
if idx_num.contains(QueryPlanFlags::STEP) {
2018-10-31 03:11:35 +08:00
self.step = args.get(i)?;
2021-02-28 19:43:46 +08:00
#[allow(clippy::comparison_chain)]
if self.step == 0 {
2016-08-15 01:53:47 +08:00
self.step = 1;
2021-02-28 19:43:46 +08:00
} else if self.step < 0 {
self.step = -self.step;
if !idx_num.contains(QueryPlanFlags::ASC) {
idx_num |= QueryPlanFlags::DESC;
}
2016-08-15 01:53:47 +08:00
}
} else {
self.step = 1;
};
2018-05-14 01:49:11 +08:00
for arg in args.iter() {
if arg.data_type() == Type::Null {
// If any of the constraints have a NULL value, then return no rows.
self.min_value = 1;
self.max_value = 0;
break;
}
}
2018-01-27 17:32:58 +08:00
self.is_desc = idx_num.contains(QueryPlanFlags::DESC);
2016-08-15 01:53:47 +08:00
if self.is_desc {
self.value = self.max_value;
2018-05-14 01:49:11 +08:00
if self.step > 0 {
2016-08-15 01:53:47 +08:00
self.value -= (self.max_value - self.min_value) % self.step;
}
} else {
self.value = self.min_value;
}
2018-05-14 01:49:11 +08:00
self.row_id = 1;
2016-08-15 01:53:47 +08:00
Ok(())
}
2018-08-17 00:29:46 +08:00
2016-08-15 01:53:47 +08:00
fn next(&mut self) -> Result<()> {
if self.is_desc {
self.value -= self.step;
} else {
self.value += self.step;
}
self.row_id += 1;
Ok(())
}
2018-08-17 00:29:46 +08:00
2016-08-15 01:53:47 +08:00
fn eof(&self) -> bool {
if self.is_desc {
self.value < self.min_value
} else {
self.value > self.max_value
}
}
2018-08-17 00:29:46 +08:00
fn column(&self, ctx: &mut Context, i: c_int) -> Result<()> {
2016-08-15 01:53:47 +08:00
let x = match i {
SERIES_COLUMN_START => self.min_value,
SERIES_COLUMN_STOP => self.max_value,
SERIES_COLUMN_STEP => self.step,
_ => self.value,
};
ctx.set_result(&x)
2016-08-15 01:53:47 +08:00
}
2018-08-17 00:29:46 +08:00
2016-08-15 01:53:47 +08:00
fn rowid(&self) -> Result<i64> {
Ok(self.row_id)
}
}
2016-08-15 15:09:38 +08:00
#[cfg(test)]
mod test {
2018-10-31 03:11:35 +08:00
use crate::ffi;
use crate::vtab::series;
2020-11-06 05:14:00 +08:00
use crate::{Connection, Result};
use fallible_iterator::FallibleIterator;
2016-08-15 15:09:38 +08:00
#[test]
2020-11-06 05:14:00 +08:00
fn test_series_module() -> Result<()> {
2016-08-15 15:09:38 +08:00
let version = unsafe { ffi::sqlite3_libversion_number() };
2019-02-02 18:37:26 +08:00
if version < 3_008_012 {
2020-11-06 05:14:00 +08:00
return Ok(());
2016-08-15 15:09:38 +08:00
}
2020-11-06 05:14:00 +08:00
let db = Connection::open_in_memory()?;
series::load_module(&db)?;
2016-08-15 15:09:38 +08:00
2020-11-06 05:14:00 +08:00
let mut s = db.prepare("SELECT * FROM generate_series(0,20,5)")?;
2016-08-15 15:09:38 +08:00
2020-11-06 05:14:00 +08:00
let series = s.query_map([], |row| row.get::<_, i32>(0))?;
2016-08-15 15:09:38 +08:00
let mut expected = 0;
for value in series {
2020-11-06 05:14:00 +08:00
assert_eq!(expected, value?);
2016-08-15 15:09:38 +08:00
expected += 5;
}
let mut s =
db.prepare("SELECT * FROM generate_series WHERE start=1 AND stop=9 AND step=2")?;
let series: Vec<i32> = s.query([])?.map(|r| r.get(0)).collect()?;
assert_eq!(vec![1, 3, 5, 7, 9], series);
let mut s = db.prepare("SELECT * FROM generate_series LIMIT 5")?;
let series: Vec<i32> = s.query([])?.map(|r| r.get(0)).collect()?;
assert_eq!(vec![0, 1, 2, 3, 4], series);
let mut s = db.prepare("SELECT * FROM generate_series(0,32,5) ORDER BY value DESC")?;
let series: Vec<i32> = s.query([])?.map(|r| r.get(0)).collect()?;
assert_eq!(vec![30, 25, 20, 15, 10, 5, 0], series);
2020-11-06 05:14:00 +08:00
Ok(())
2016-08-15 15:09:38 +08:00
}
}