fix: support unknown for timestamp function (#6708)

* fix: support unknown for timestamp function

Signed-off-by: yihong0618 <zouzou0208@gmail.com>

* fix: some sqlness now no error

Signed-off-by: yihong0618 <zouzou0208@gmail.com>

* fix: make clippy happy

Signed-off-by: yihong0618 <zouzou0208@gmail.com>

* fix: address comments

Signed-off-by: yihong0618 <zouzou0208@gmail.com>

* fix: address comments

Signed-off-by: yihong0618 <zouzou0208@gmail.com>

---------

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
This commit is contained in:
yihong
2025-08-14 10:56:33 +08:00
committed by GitHub
parent fb3b1d4866
commit 56f5ccf823
4 changed files with 82 additions and 19 deletions

View File

@@ -1560,23 +1560,30 @@ impl PromPlanner {
let mut result = FunctionArgs::default();
for arg in args {
match *arg.clone() {
PromExpr::Subquery(_)
| PromExpr::VectorSelector(_)
| PromExpr::MatrixSelector(_)
| PromExpr::Extension(_)
| PromExpr::Aggregate(_)
| PromExpr::Paren(_)
| PromExpr::Call(_) => {
if result.input.replace(*arg.clone()).is_some() {
MultipleVectorSnafu { expr: *arg.clone() }.fail()?;
// First try to parse as literal expression (including binary expressions like 100.0 + 3.0)
if let Some(expr) = Self::try_build_literal_expr(arg) {
result.literals.push(expr);
} else {
// If not a literal, treat as vector input
match arg.as_ref() {
PromExpr::Subquery(_)
| PromExpr::VectorSelector(_)
| PromExpr::MatrixSelector(_)
| PromExpr::Extension(_)
| PromExpr::Aggregate(_)
| PromExpr::Paren(_)
| PromExpr::Call(_)
| PromExpr::Binary(_)
| PromExpr::Unary(_) => {
if result.input.replace(*arg.clone()).is_some() {
MultipleVectorSnafu { expr: *arg.clone() }.fail()?;
}
}
}
_ => {
let expr =
Self::get_param_as_literal_expr(&Some(Box::new(*arg.clone())), None, None)?;
result.literals.push(expr);
_ => {
let expr = Self::get_param_as_literal_expr(&Some(arg.clone()), None, None)?;
result.literals.push(expr);
}
}
}
}

View File

@@ -181,7 +181,15 @@ Error: 2000(InvalidSyntax), expected type vector in call to function 'deg', got
-- SQLNESS SORT_RESULT 3 1
TQL EVAL (0, 20, '5s') rad(180 * angles{unit="radians"});
Error: 1004(InvalidArguments), Invalid function argument for unknown
+---------------------+-----------------------------+---------+
| ts | radians(Float64(180) * val) | unit |
+---------------------+-----------------------------+---------+
| 1970-01-01T00:00:00 | 0.0 | radians |
| 1970-01-01T00:00:05 | 4.934813740258846 | radians |
| 1970-01-01T00:00:10 | 9.869627480517693 | radians |
| 1970-01-01T00:00:15 | 14.80444122077654 | radians |
| 1970-01-01T00:00:20 | 19.739254961035385 | radians |
+---------------------+-----------------------------+---------+
-- Test sgn() function (returns sign of values: -1, 0, or 1)
-- SQLNESS SORT_RESULT 3 1
@@ -275,12 +283,43 @@ TQL EVAL (25, 35, '5s') scalar(sgn(angles{unit="positive"}));
-- SQLNESS SORT_RESULT 3 1
TQL EVAL (25, 35, '5s') sgn(angles - 42.5);
Error: 1004(InvalidArguments), Invalid function argument for unknown
+---------------------+-----------------------------+-----------+
| ts | signum(val - Float64(42.5)) | unit |
+---------------------+-----------------------------+-----------+
| 1970-01-01T00:00:25 | -1.0 | negative |
| 1970-01-01T00:00:25 | -1.0 | radians |
| 1970-01-01T00:00:25 | 0.0 | positive |
| 1970-01-01T00:00:25 | 1.0 | degrees |
| 1970-01-01T00:00:30 | -1.0 | negative |
| 1970-01-01T00:00:30 | -1.0 | radians |
| 1970-01-01T00:00:30 | -1.0 | small_pos |
| 1970-01-01T00:00:30 | -1.0 | zero |
| 1970-01-01T00:00:30 | 0.0 | positive |
| 1970-01-01T00:00:30 | 1.0 | degrees |
| 1970-01-01T00:00:35 | -1.0 | negative |
| 1970-01-01T00:00:35 | -1.0 | radians |
| 1970-01-01T00:00:35 | -1.0 | small_neg |
| 1970-01-01T00:00:35 | -1.0 | small_pos |
| 1970-01-01T00:00:35 | -1.0 | zero |
| 1970-01-01T00:00:35 | 0.0 | positive |
| 1970-01-01T00:00:35 | 1.0 | degrees |
+---------------------+-----------------------------+-----------+
-- SQLNESS SORT_RESULT 3 1
TQL EVAL (0, 35, '5s') sgn(angles{unit="radians"} - pi());
Error: 1004(InvalidArguments), Invalid function argument for unknown
+---------------------+----------------------------+
| time | signum(angles.val - .pi()) |
+---------------------+----------------------------+
| 1970-01-01T00:00:00 | -1.0 |
| 1970-01-01T00:00:05 | -1.0 |
| 1970-01-01T00:00:10 | 1.0 |
| 1970-01-01T00:00:15 | 1.0 |
| 1970-01-01T00:00:20 | 1.0 |
| 1970-01-01T00:00:25 | 1.0 |
| 1970-01-01T00:00:30 | 1.0 |
| 1970-01-01T00:00:35 | 1.0 |
+---------------------+----------------------------+
-- SQLNESS SORT_RESULT 3 1
TQL EVAL (25, 35, '5s') sgn(angles) * angles;

View File

@@ -106,7 +106,19 @@ tql eval (0, 60, '30s') timestamp(timestamp_test) - time();
-- Test timestamp() with other functions
tql eval (0, 60, '30s') abs(timestamp(timestamp_test) - avg(timestamp(timestamp_test))) > 20;
Error: 1004(InvalidArguments), Invalid function argument for unknown
++
++
-- Test Issue 6707
tql eval timestamp(demo_memory_usage_bytes * 1);
++
++
tql eval timestamp(-demo_memory_usage_bytes);
++
++
tql eval (0, 60, '30s') timestamp(timestamp_test) == 60;

View File

@@ -36,6 +36,11 @@ tql eval (0, 60, '30s') timestamp(timestamp_test) - time();
-- Test timestamp() with other functions
tql eval (0, 60, '30s') abs(timestamp(timestamp_test) - avg(timestamp(timestamp_test))) > 20;
-- Test Issue 6707
tql eval timestamp(demo_memory_usage_bytes * 1);
tql eval timestamp(-demo_memory_usage_bytes);
tql eval (0, 60, '30s') timestamp(timestamp_test) == 60;
-- Test timestamp() with multiple metrics