Skip to main content

reifydb_engine/flow/compiler/operator/
take.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::interface::catalog::flow::FlowNodeId;
5use reifydb_rql::{
6	flow::node::FlowNodeType::Take,
7	nodes::{TakeLimit, TakeNode},
8	query::QueryPlan,
9};
10use reifydb_transaction::transaction::admin::AdminTransaction;
11use reifydb_type::Result;
12
13use crate::flow::compiler::{CompileOperator, FlowCompiler};
14
15pub(crate) struct TakeCompiler {
16	pub input: Box<QueryPlan>,
17	pub limit: usize,
18}
19
20impl From<TakeNode> for TakeCompiler {
21	fn from(node: TakeNode) -> Self {
22		let limit = match node.take {
23			TakeLimit::Literal(n) => n,
24			TakeLimit::Variable(_) => unreachable!(),
25		};
26		Self {
27			input: node.input,
28			limit,
29		}
30	}
31}
32
33impl CompileOperator for TakeCompiler {
34	fn compile(self, compiler: &mut FlowCompiler, txn: &mut AdminTransaction) -> Result<FlowNodeId> {
35		let input_node = compiler.compile_plan(txn, *self.input)?;
36
37		let node_id = compiler.add_node(
38			txn,
39			Take {
40				limit: self.limit,
41			},
42		)?;
43
44		compiler.add_edge(txn, &input_node, &node_id)?;
45		Ok(node_id)
46	}
47}