Skip to content

Commit

Permalink
feat: deny unknown field in options (#184)
Browse files Browse the repository at this point in the history
* fix: deny unknown options

Signed-off-by: usamoi <[email protected]>

* test: deny unknown options

Signed-off-by: usamoi <[email protected]>

---------

Signed-off-by: usamoi <[email protected]>
  • Loading branch information
usamoi authored Dec 15, 2023
1 parent c50912e commit cc1b7d0
Show file tree
Hide file tree
Showing 19 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions crates/service/src/algorithms/quantization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::sync::Arc;
use validator::Validate;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "snake_case")]
pub enum QuantizationOptions {
Trivial(TrivialQuantizationOptions),
Expand Down
2 changes: 2 additions & 0 deletions crates/service/src/algorithms/quantization/product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::sync::Arc;
use validator::Validate;

#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
#[serde(deny_unknown_fields)]
pub struct ProductQuantizationOptions {
#[serde(default = "ProductQuantizationOptions::default_sample")]
pub sample: u32,
Expand All @@ -40,6 +41,7 @@ impl Default for ProductQuantizationOptions {

#[repr(u16)]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "snake_case")]
pub enum ProductQuantizationOptionsRatio {
X4 = 1,
Expand Down
1 change: 1 addition & 0 deletions crates/service/src/algorithms/quantization/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::sync::Arc;
use validator::Validate;

#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
#[serde(deny_unknown_fields)]
pub struct ScalarQuantizationOptions {}

impl Default for ScalarQuantizationOptions {
Expand Down
1 change: 1 addition & 0 deletions crates/service/src/algorithms/quantization/trivial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::sync::Arc;
use validator::Validate;

#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
#[serde(deny_unknown_fields)]
pub struct TrivialQuantizationOptions {}

impl Default for TrivialQuantizationOptions {
Expand Down
3 changes: 2 additions & 1 deletion crates/service/src/index/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::utils::file_wal::FileWal;
use dashmap::mapref::entry::Entry;
use dashmap::DashMap;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::sync::Arc;

Expand Down Expand Up @@ -79,7 +80,7 @@ impl Delete {
}
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
struct Log {
key: Pointer,
}
1 change: 1 addition & 0 deletions crates/service/src/index/indexing/flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::sync::Arc;
use validator::Validate;

#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
#[serde(deny_unknown_fields)]
pub struct FlatIndexingOptions {
#[serde(default)]
#[validate]
Expand Down
1 change: 1 addition & 0 deletions crates/service/src/index/indexing/hnsw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{path::PathBuf, sync::Arc};
use validator::Validate;

#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
#[serde(deny_unknown_fields)]
pub struct HnswIndexingOptions {
#[serde(default = "HnswIndexingOptions::default_m")]
#[validate(range(min = 4, max = 128))]
Expand Down
1 change: 1 addition & 0 deletions crates/service/src/index/indexing/ivf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::sync::Arc;
use validator::Validate;

#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
#[serde(deny_unknown_fields)]
pub struct IvfIndexingOptions {
#[serde(default = "IvfIndexingOptions::default_least_iterations")]
#[validate(range(min = 1, max = 1_000_000))]
Expand Down
1 change: 1 addition & 0 deletions crates/service/src/index/indexing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::sync::Arc;
use validator::Validate;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "snake_case")]
pub enum IndexingOptions {
Flat(FlatIndexingOptions),
Expand Down
4 changes: 3 additions & 1 deletion crates/service/src/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use validator::Validate;
pub struct OutdatedError(#[from] pub Option<GrowingSegmentInsertError>);

#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
#[serde(deny_unknown_fields)]
pub struct VectorOptions {
#[validate(range(min = 1, max = 65535))]
#[serde(rename = "dimensions")]
Expand All @@ -48,6 +49,7 @@ pub struct VectorOptions {
}

#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
#[serde(deny_unknown_fields)]
pub struct IndexOptions {
#[validate]
pub vector: VectorOptions,
Expand Down Expand Up @@ -467,7 +469,7 @@ impl<S: G> IndexView<S> {
}
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
struct IndexStartup {
sealeds: HashSet<Uuid>,
growings: HashSet<Uuid>,
Expand Down
1 change: 1 addition & 0 deletions crates/service/src/index/optimizing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
use validator::Validate;

#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
#[serde(deny_unknown_fields)]
pub struct OptimizingOptions {
#[serde(default = "OptimizingOptions::default_sealing_secs")]
#[validate(range(min = 0, max = 60))]
Expand Down
3 changes: 2 additions & 1 deletion crates/service/src/index/segments/growing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::prelude::*;
use crate::utils::dir_ops::sync_dir;
use crate::utils::file_wal::FileWal;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use std::cell::UnsafeCell;
use std::mem::MaybeUninit;
use std::path::PathBuf;
Expand Down Expand Up @@ -199,7 +200,7 @@ impl<S: G> Drop for GrowingSegment<S> {
}
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Log<S: G> {
vector: Vec<S::Scalar>,
payload: Payload,
Expand Down
1 change: 1 addition & 0 deletions crates/service/src/index/segments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use validator::Validate;
use validator::ValidationError;

#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
#[serde(deny_unknown_fields)]
#[validate(schema(function = "Self::validate_0"))]
pub struct SegmentsOptions {
#[serde(default = "SegmentsOptions::default_max_growing_segment_size")]
Expand Down
2 changes: 1 addition & 1 deletion crates/service/src/prelude/global/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub trait FloatCast: Sized {
}
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DynamicVector {
F32(Vec<F32>),
F16(Vec<F16>),
Expand Down
2 changes: 1 addition & 1 deletion crates/service/src/utils/vec2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;
use std::ops::{Deref, DerefMut, Index, IndexMut};

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone)]
pub struct Vec2<S: G> {
dims: u16,
v: Vec<S::Scalar>,
Expand Down
3 changes: 2 additions & 1 deletion crates/service/src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::utils::dir_ops::sync_dir;
use crate::utils::file_atomic::FileAtomic;
use arc_swap::ArcSwap;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use serde_with::DisplayFromStr;
use std::collections::HashMap;
use std::path::PathBuf;
Expand Down Expand Up @@ -172,7 +173,7 @@ impl WorkerProtect {
}

#[serde_with::serde_as]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
struct WorkerStartup {
#[serde_as(as = "HashMap<DisplayFromStr, _>")]
indexes: HashMap<Id, IndexOptions>,
Expand Down
5 changes: 3 additions & 2 deletions src/index/am_setup.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::datatype::typmod::Typmod;
use serde::{Deserialize, Serialize};
use serde::Deserialize;
use service::index::indexing::IndexingOptions;
use service::index::optimizing::OptimizingOptions;
use service::index::segments::SegmentsOptions;
Expand Down Expand Up @@ -118,7 +118,8 @@ unsafe fn get_parsed_from_varlena(helper: *const pgrx::pg_sys::varlena) -> Parse
toml::from_str::<Parsed>(cstr.to_str().unwrap()).unwrap()
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[derive(Debug, Clone, Deserialize, Default)]
#[serde(deny_unknown_fields)]
struct Parsed {
#[serde(default)]
segment: SegmentsOptions,
Expand Down
3 changes: 1 addition & 2 deletions src/ipc/packet/create.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use serde::Deserialize;
use serde::Serialize;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub enum CreatePacket {
Expand Down
12 changes: 12 additions & 0 deletions tests/sqllogictest/index.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
statement ok
DROP TABLE IF EXISTS t;

statement ok
CREATE TABLE t (val vector(3));

statement ok
INSERT INTO t (val) SELECT ARRAY[random(), random(), random()]::real[] FROM generate_series(1, 1000);

statement error unknown
CREATE INDEX ON t USING vectors (val vector_l2_ops)
WITH (options = "unknown_field = 1");

0 comments on commit cc1b7d0

Please sign in to comment.