Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use ethnum::i256;

// JSON text constants
pub(crate) const UNICODE_LEN: usize = 4;

Expand All @@ -22,6 +24,26 @@ pub(crate) const UNICODE_LEN: usize = 4;
/// `to_value`, `partial_cmp` and others overflow 2 MiB at 512 levels.
pub(crate) const MAX_NESTING_DEPTH: usize = 512;

/// Maximum parenthesis depth in a JSONPath expression. The JSONPath parser
/// uses much more stack per level than data traversal.
pub(crate) const MAX_JSON_PATH_DEPTH: usize = 64;

/// Maximum number of `&&` and `||` operators in a JSONPath expression. Each
/// one adds a level to the parsed tree, which is processed recursively.
/// The parser counts them because unquoted field names may contain `&&`.
pub(crate) const MAX_JSON_PATH_LOGICAL_OPERATORS: usize = 256;

/// Base work units for one JSONPath evaluation; see `Selector::reset_budgets`.
/// Each selected item, filter value, node visited by `.**` and compared pair
/// costs one unit.
pub(crate) const MAX_JSON_PATH_ITEMS: usize = 1_000_000;
Comment thread
b41sh marked this conversation as resolved.
Resolved

/// Bytes one JSONPath evaluation may copy beyond the document size. A value
/// is copied once per selected ancestor, so `$.**.**` on 500 nested arrays
/// around a 1 KiB string copies about 300 MB. The budget is this plus the
/// document size, so any single value can be copied at least once.
pub(crate) const MAX_JSON_PATH_EXTRA_COPY_BYTES: usize = 32 * 1024 * 1024;

/// Returns the nesting depth after entering one more array or object.
///
/// # Errors
Expand Down Expand Up @@ -83,6 +105,10 @@ pub(crate) const DECIMAL64_MIN: i128 = -999_999_999_999_999_999i128;
pub(crate) const DECIMAL64_MAX: i128 = 999_999_999_999_999_999i128;
pub(crate) const DECIMAL128_MIN: i128 = -99_999_999_999_999_999_999_999_999_999_999_999_999i128;
pub(crate) const DECIMAL128_MAX: i128 = 99_999_999_999_999_999_999_999_999_999_999_999_999i128;
pub(crate) const DECIMAL256_MIN: i256 =
ethnum::int!("-9999999999999999999999999999999999999999999999999999999999999999999999999999");
pub(crate) const DECIMAL256_MAX: i256 =
ethnum::int!("9999999999999999999999999999999999999999999999999999999999999999999999999999");

pub(crate) const NUMBER_STRUCT_TOKEN: &str = "$jsonb::private::Number";
pub(crate) const NUMBER_STRUCT_FIELD_SCALE: &str = "$jsonb::private::Number::Scale";
Expand Down
15 changes: 5 additions & 10 deletions src/core/databend/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,34 +503,29 @@ impl Number {
9 => {
let value = i64::from_be_bytes(bytes[1..9].try_into().unwrap());
let scale = u8::from_be_bytes(bytes[9..10].try_into().unwrap());
let dec = Decimal64 { scale, value };
Number::Decimal64(dec)
Number::Decimal64(Decimal64::new(scale, value)?)
}
17 => {
let value = i128::from_be_bytes(bytes[1..17].try_into().unwrap());
let scale = u8::from_be_bytes(bytes[17..18].try_into().unwrap());
let dec = Decimal128 { scale, value };
Number::Decimal128(dec)
Number::Decimal128(Decimal128::new(scale, value)?)
}
18 => {
// Compatible with deprecated Decimal128 formats, including precision
let value = i128::from_be_bytes(bytes[1..17].try_into().unwrap());
let scale = u8::from_be_bytes(bytes[18..19].try_into().unwrap());
let dec = Decimal128 { scale, value };
Number::Decimal128(dec)
Number::Decimal128(Decimal128::new(scale, value)?)
}
33 => {
let value = i256::from_be_bytes(bytes[1..33].try_into().unwrap());
let scale = u8::from_be_bytes(bytes[33..34].try_into().unwrap());
let dec = Decimal256 { scale, value };
Number::Decimal256(dec)
Number::Decimal256(Decimal256::new(scale, value)?)
}
34 => {
// Compatible with deprecated Decimal256 formats, including precision
let value = i256::from_be_bytes(bytes[1..33].try_into().unwrap());
let scale = u8::from_be_bytes(bytes[34..35].try_into().unwrap());
let dec = Decimal256 { scale, value };
Number::Decimal256(dec)
Number::Decimal256(Decimal256::new(scale, value)?)
}
_ => {
return Err(Error::InvalidJsonbNumber);
Expand Down
Loading
Loading