MCPcopy Create free account
hub / github.com/GraphLite-AI/GraphLite / execute

Method execute

graphlite/src/functions/numeric_functions.rs:39–92  ·  view source on GitHub ↗
(&self, context: &FunctionContext)

Source from the content-addressed store, hash-verified

37 }
38
39 fn execute(&self, context: &FunctionContext) -> FunctionResult<Value> {
40 // Handle 1 or 2 arguments
41 let arg_count = context.arguments.len();
42 if arg_count == 0 || arg_count > 2 {
43 return Err(FunctionError::InvalidArgumentType {
44 message: "ROUND function expects 1 or 2 arguments".to_string(),
45 });
46 }
47
48 // Get the number argument
49 let value = context.get_argument(0)?;
50
51 if value.is_null() {
52 return Ok(Value::Null);
53 }
54
55 // Convert to number
56 let number = if let Some(n) = value.as_number() {
57 n
58 } else if let Some(s) = value.as_string() {
59 s.parse::<f64>()
60 .map_err(|_| FunctionError::InvalidArgumentType {
61 message: format!("Cannot convert '{}' to number", s),
62 })?
63 } else {
64 return Err(FunctionError::InvalidArgumentType {
65 message: "ROUND argument must be a number or convertible to number".to_string(),
66 });
67 };
68
69 // Get decimal places (default to 0)
70 let decimal_places = if arg_count == 2 {
71 let places_value = context.get_argument(1)?;
72 if let Some(n) = places_value.as_number() {
73 n as i32
74 } else {
75 return Err(FunctionError::InvalidArgumentType {
76 message: "ROUND decimal places argument must be a number".to_string(),
77 });
78 }
79 } else {
80 0
81 };
82
83 // Handle special case: if number is 0, always return 0
84 if number == 0.0 {
85 return Ok(Value::Number(0.0));
86 }
87
88 // Oracle ROUND logic
89 let rounded = oracle_round(number, decimal_places);
90
91 Ok(Value::Number(rounded))
92 }
93
94 fn return_type(&self) -> &str {
95 "Number"

Callers

nothing calls this directly

Calls 6

oracle_roundFunction · 0.85
get_argumentMethod · 0.80
is_nullMethod · 0.80
as_numberMethod · 0.80
lenMethod · 0.45
as_stringMethod · 0.45

Tested by

no test coverage detected