From cd5db55cf26d06a07851e2ff0513fe8b343b0c9a Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Sat, 16 Nov 2024 09:22:09 +0200 Subject: [PATCH] core: Make JSON support configurable This adds a `json` feature flag, which allows users to disable JSON support if needed. --- core/Cargo.toml | 13 +++++++++---- core/function.rs | 5 +++++ core/lib.rs | 1 + core/translate/expr.rs | 5 ++++- core/vdbe/mod.rs | 8 ++++++-- 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index 77405c16d..10274fe70 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -14,8 +14,13 @@ name = "limbo_core" path = "lib.rs" [features] -default = ["fs"] +default = ["fs", "json"] fs = [] +json = [ + "dep:jsonb", + "dep:pest", + "dep:pest_derive", +] [target.'cfg(target_os = "linux")'.dependencies] io-uring = "0.6.1" @@ -41,11 +46,11 @@ getrandom = { version = "0.2.15", features = ["js"] } regex = "1.10.5" chrono = "0.4.38" julian_day_converter = "0.3.2" -jsonb = "0.4.1" +jsonb = { version = "0.4.1", optional = true } indexmap = { version="2.2.6", features = ["serde"] } serde = { version = "1.0", features = ["derive"] } -pest = "2.0" -pest_derive = "2.0" +pest = { version = "2.0", optional = true } +pest_derive = { version = "2.0", optional = true } mockall = "0.13.0" rand = "0.8.5" diff --git a/core/function.rs b/core/function.rs index 304d155cf..aa1971c14 100644 --- a/core/function.rs +++ b/core/function.rs @@ -1,11 +1,13 @@ use std::fmt; use std::fmt::Display; +#[cfg(feature = "json")] #[derive(Debug, Clone, PartialEq)] pub enum JsonFunc { Json, } +#[cfg(feature = "json")] impl Display for JsonFunc { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( @@ -129,6 +131,7 @@ impl Display for ScalarFunc { pub enum Func { Agg(AggFunc), Scalar(ScalarFunc), + #[cfg(feature = "json")] Json(JsonFunc), } @@ -137,6 +140,7 @@ impl Display for Func { match self { Func::Agg(agg_func) => write!(f, "{}", agg_func.to_string()), Func::Scalar(scalar_func) => write!(f, "{}", scalar_func), + #[cfg(feature = "json")] Func::Json(json_func) => write!(f, "{}", json_func), } } @@ -189,6 +193,7 @@ impl Func { "unicode" => Ok(Func::Scalar(ScalarFunc::Unicode)), "quote" => Ok(Func::Scalar(ScalarFunc::Quote)), "sqlite_version" => Ok(Func::Scalar(ScalarFunc::SqliteVersion)), + #[cfg(feature = "json")] "json" => Ok(Func::Json(JsonFunc::Json)), "unixepoch" => Ok(Func::Scalar(ScalarFunc::UnixEpoch)), "hex" => Ok(Func::Scalar(ScalarFunc::Hex)), diff --git a/core/lib.rs b/core/lib.rs index 980bc96fd..9bc1bddc9 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -1,6 +1,7 @@ mod error; mod function; mod io; +#[cfg(feature = "json")] mod json; mod pseudo; mod schema; diff --git a/core/translate/expr.rs b/core/translate/expr.rs index f76b3cf88..b0b29bc17 100644 --- a/core/translate/expr.rs +++ b/core/translate/expr.rs @@ -1,11 +1,13 @@ -use crate::{function::JsonFunc, Result}; use sqlite3_parser::ast::{self, UnaryOperator}; use super::optimizer::CachedResult; +#[cfg(feature = "json")] +use crate::function::JsonFunc; use crate::function::{AggFunc, Func, FuncCtx, ScalarFunc}; use crate::schema::{PseudoTable, Table, Type}; use crate::util::normalize_ident; use crate::vdbe::{builder::ProgramBuilder, BranchOffset, Insn}; +use crate::Result; use super::plan::{Aggregate, BTreeTableReference}; @@ -742,6 +744,7 @@ pub fn translate_expr( Func::Agg(_) => { crate::bail_parse_error!("aggregation function in non-aggregation context") } + #[cfg(feature = "json")] Func::Json(j) => match j { JsonFunc::Json => { let args = if let Some(args) = args { diff --git a/core/vdbe/mod.rs b/core/vdbe/mod.rs index 609b8bfb3..322165147 100644 --- a/core/vdbe/mod.rs +++ b/core/vdbe/mod.rs @@ -24,8 +24,7 @@ pub mod sorter; mod datetime; use crate::error::{LimboError, SQLITE_CONSTRAINT_PRIMARYKEY}; -use crate::function::{AggFunc, FuncCtx, JsonFunc, ScalarFunc}; -use crate::json::get_json; +use crate::function::{AggFunc, FuncCtx, ScalarFunc}; use crate::pseudo::PseudoCursor; use crate::schema::Table; use crate::storage::sqlite3_ondisk::DatabaseHeader; @@ -34,6 +33,8 @@ use crate::types::{ AggContext, Cursor, CursorResult, OwnedRecord, OwnedValue, Record, SeekKey, SeekOp, }; use crate::DATABASE_VERSION; +#[cfg(feature = "json")] +use crate::{function::JsonFunc, json::get_json}; use crate::{Connection, Result, TransactionState}; use datetime::{exec_date, exec_time, exec_unixepoch}; @@ -57,6 +58,7 @@ pub type PageIdx = usize; #[derive(Debug)] pub enum Func { Scalar(ScalarFunc), + #[cfg(feature = "json")] Json(JsonFunc), } @@ -64,6 +66,7 @@ impl Display for Func { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let str = match self { Func::Scalar(scalar_func) => scalar_func.to_string(), + #[cfg(feature = "json")] Func::Json(json_func) => json_func.to_string(), }; write!(f, "{}", str) @@ -1751,6 +1754,7 @@ impl Program { } => { let arg_count = func.arg_count; match &func.func { + #[cfg(feature = "json")] crate::function::Func::Json(JsonFunc::Json) => { let json_value = &state.registers[*start_reg]; let json_str = get_json(json_value);