Skip to main content

QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder { /* private fields */ }
Expand description

Fluent query builder.

§Example

ⓘ
let plan = QueryBuilder::new()
    .measurement("cpu")
    .tag("host", "server-01")
    .field("usage_idle")
    .range(start_ts, end_ts)
    .build()?;

Implementations§

Source§

impl QueryBuilder

Source

pub fn new() -> Self

Create a new empty query builder.

Source

pub fn measurement(self, name: impl Into<String>) -> Self

Set the measurement to query.

Source

pub fn tag(self, key: impl Into<String>, value: impl Into<String>) -> Self

Add a tag equality filter.

Source

pub fn tags(self, tags: &[(&str, &str)]) -> Self

Add multiple tag equality filters.

Source

pub fn field(self, name: impl Into<String>) -> Self

Add a field to the projection (select only these columns).

Source

pub fn fields(self, names: &[&str]) -> Self

Add multiple fields to the projection.

Source

pub fn range(self, start: Timestamp, end: Timestamp) -> Self

Set the time range filter.

Source

pub fn last(self, duration: Duration) -> Self

Set the time range to the last duration from now.

Uses the current system time as the end point.

Source

pub fn aggregate(self, function: AggFn) -> Self

Add an aggregation function.

Source

pub fn group_by(self, columns: &[&str]) -> Self

Set the group-by columns for aggregation.

Source

pub fn estimated_cardinality(self, cardinality: usize) -> Self

Set the estimated group-key cardinality.

When set, the query engine selects between hash-based and sort-based aggregation strategies. Low cardinality (≤ 1024) uses sort-based grouping for better cache locality and sorted output.

Source

pub fn downsample(self, bucket: TimeBucket, function: AggFn) -> Self

Bucket results by bucket, and aggregate each bucket with function.

The bucket carries its own calendar, so all three of these mean what they say:

ⓘ
// A fixed five minutes, aligned to the Unix epoch.
.downsample(TimeBucket::fixed(Duration::from_secs(300)), AggFn::Avg)
// A calendar day in Berlin — 23 hours on the spring transition.
.downsample(TimeBucket::parse("1d", Some("Europe/Berlin"))?, AggFn::Max)
// A calendar month, which is not a span of anything.
.downsample(TimeBucket::parse("1mo", None)?, AggFn::Sum)
Source

pub fn limit(self, n: usize) -> Self

Limit the number of returned rows.

Source

pub fn offset(self, n: usize) -> Self

Skip the first n rows before returning results.

Must be used together with limit().

Source

pub fn window(self, func: WindowFn) -> Self

Add a window function.

Source

pub fn window_partition_by(self, columns: &[&str]) -> Self

Set the partition-by columns for window functions.

Source

pub fn window_value_column(self, column: impl Into<String>) -> Self

Set the value column for window functions.

If not set, the first numeric field column is used by default.

Source

pub fn namespace(self, ns: impl Into<String>) -> Self

Set the namespace scope for tenant isolation.

This embeds the NamespaceId structurally in the query plan AND injects the __namespace__ tag filter, ensuring data isolation even if the HTTP layer is bypassed.

Source

pub fn namespace_scope(self, ns: Option<&str>) -> Self

Apply an optional namespace scope.

None means “no tenancy” — the single-tenant server and the embedded API, where points carry no namespace tag and a filter on one would match nothing. Call sites use this rather than branching, so that scoping a read is one uniform call instead of an if each of them can forget.

Source

pub fn build(self) -> Result<QueryPlan>

Build and validate the query plan.

§Errors

Returns QueryError::Validation if: No measurement is specified Time range is invalid (start > end)

Trait Implementations§

Source§

impl Clone for QueryBuilder

Source§

fn clone(&self) -> QueryBuilder

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for QueryBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for QueryBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more