mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-10-15 08:22:38 +00:00
* fix(promql): keep vector labels when a computed scalar is on the right `metric + time()` or `metric * scalar(other)` planned the join with the scalar side's context, which has no tags, so every series collapsed into one unlabeled series. Use the vector side's context when the right operand is scalar-typed. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * test(promql): update results for labels kept with a computed scalar on the right v * pi() and v + time() now keep the vector's labels and time index name, as in Prometheus. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): qualify columns of an unnamed operand in vector joins A comparison filter's projection has no table name, so after the join its time index resolved unqualified and was ambiguous when the other operand had the same column. Also keep the empty-metric branch unchanged for scalar left operands. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * test(promql): cover comparison filters joined with a scalar Signed-off-by: Dennis Zhuang <killme2008@gmail.com> --------- Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
355 lines
15 KiB
Plaintext
355 lines
15 KiB
Plaintext
CREATE TABLE angles (
|
|
ts timestamp(3) time index,
|
|
unit STRING PRIMARY KEY,
|
|
val DOUBLE,
|
|
);
|
|
|
|
Affected Rows: 0
|
|
|
|
INSERT INTO TABLE angles VALUES
|
|
(0, 'radians', 0),
|
|
(0, 'degrees', 0),
|
|
(5000, 'radians', 1.5708), -- π/2
|
|
(5000, 'degrees', 90),
|
|
(10000, 'radians', 3.1416), -- π
|
|
(10000, 'degrees', 180),
|
|
(15000, 'radians', 4.7124), -- 3π/2
|
|
(15000, 'degrees', 270),
|
|
(20000, 'radians', 6.2832), -- 2π
|
|
(20000, 'degrees', 360),
|
|
(25000, 'positive', 42.5),
|
|
(25000, 'negative', -17.3),
|
|
(30000, 'zero', 0),
|
|
(30000, 'small_pos', 0.001),
|
|
(35000, 'small_neg', -0.001);
|
|
|
|
Affected Rows: 15
|
|
|
|
-- Test pi() function (returns constant π ≈ 3.141592653589793)
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 20, '5s') pi();
|
|
|
|
+---------------------+-------------------+
|
|
| time | pi() |
|
|
+---------------------+-------------------+
|
|
| 1970-01-01T00:00:00 | 3.141592653589793 |
|
|
| 1970-01-01T00:00:05 | 3.141592653589793 |
|
|
| 1970-01-01T00:00:10 | 3.141592653589793 |
|
|
| 1970-01-01T00:00:15 | 3.141592653589793 |
|
|
| 1970-01-01T00:00:20 | 3.141592653589793 |
|
|
+---------------------+-------------------+
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 20, '5s') pi() + 1;
|
|
|
|
+---------------------+-------------------+
|
|
| time | pi() + Float64(1) |
|
|
+---------------------+-------------------+
|
|
| 1970-01-01T00:00:00 | 4.141592653589793 |
|
|
| 1970-01-01T00:00:05 | 4.141592653589793 |
|
|
| 1970-01-01T00:00:10 | 4.141592653589793 |
|
|
| 1970-01-01T00:00:15 | 4.141592653589793 |
|
|
| 1970-01-01T00:00:20 | 4.141592653589793 |
|
|
+---------------------+-------------------+
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 20, '5s') angles{unit="radians"} * pi();
|
|
|
|
+---------+---------------------+--------------------+
|
|
| unit | ts | angles.val * .pi() |
|
|
+---------+---------------------+--------------------+
|
|
| radians | 1970-01-01T00:00:00 | 0.0 |
|
|
| radians | 1970-01-01T00:00:05 | 4.934813740258847 |
|
|
| radians | 1970-01-01T00:00:10 | 9.869627480517694 |
|
|
| radians | 1970-01-01T00:00:15 | 14.80444122077654 |
|
|
| radians | 1970-01-01T00:00:20 | 19.73925496103539 |
|
|
+---------+---------------------+--------------------+
|
|
|
|
-- Test rad() function (converts degrees to radians)
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 20, '5s') rad(angles{unit="degrees"});
|
|
|
|
+---------------------+--------------------+---------+
|
|
| ts | radians(val) | unit |
|
|
+---------------------+--------------------+---------+
|
|
| 1970-01-01T00:00:00 | 0.0 | degrees |
|
|
| 1970-01-01T00:00:05 | 1.5707963267948966 | degrees |
|
|
| 1970-01-01T00:00:10 | 3.141592653589793 | degrees |
|
|
| 1970-01-01T00:00:15 | 4.71238898038469 | degrees |
|
|
| 1970-01-01T00:00:20 | 6.283185307179586 | degrees |
|
|
+---------------------+--------------------+---------+
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 20, '5s') rad(angles);
|
|
|
|
+---------------------+---------------------+---------+
|
|
| ts | radians(val) | unit |
|
|
+---------------------+---------------------+---------+
|
|
| 1970-01-01T00:00:00 | 0.0 | degrees |
|
|
| 1970-01-01T00:00:00 | 0.0 | radians |
|
|
| 1970-01-01T00:00:05 | 0.02741563189032693 | radians |
|
|
| 1970-01-01T00:00:05 | 1.5707963267948966 | degrees |
|
|
| 1970-01-01T00:00:10 | 0.05483126378065386 | radians |
|
|
| 1970-01-01T00:00:10 | 3.141592653589793 | degrees |
|
|
| 1970-01-01T00:00:15 | 0.08224689567098079 | radians |
|
|
| 1970-01-01T00:00:15 | 4.71238898038469 | degrees |
|
|
| 1970-01-01T00:00:20 | 0.10966252756130772 | radians |
|
|
| 1970-01-01T00:00:20 | 6.283185307179586 | degrees |
|
|
+---------------------+---------------------+---------+
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 20, '5s') scalar(rad(angles{unit="degrees"}));
|
|
|
|
+---------------------+----------------------+
|
|
| ts | scalar(radians(val)) |
|
|
+---------------------+----------------------+
|
|
| 1970-01-01T00:00:00 | 0.0 |
|
|
| 1970-01-01T00:00:05 | 1.5707963267948966 |
|
|
| 1970-01-01T00:00:10 | 3.141592653589793 |
|
|
| 1970-01-01T00:00:15 | 4.71238898038469 |
|
|
| 1970-01-01T00:00:20 | 6.283185307179586 |
|
|
+---------------------+----------------------+
|
|
|
|
-- Test deg() function (converts radians to degrees)
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 20, '5s') deg(angles{unit="radians"});
|
|
|
|
+---------------------+--------------------+---------+
|
|
| ts | degrees(val) | unit |
|
|
+---------------------+--------------------+---------+
|
|
| 1970-01-01T00:00:00 | 0.0 | radians |
|
|
| 1970-01-01T00:00:05 | 90.00021045914971 | radians |
|
|
| 1970-01-01T00:00:10 | 180.00042091829943 | radians |
|
|
| 1970-01-01T00:00:15 | 270.0006313774491 | radians |
|
|
| 1970-01-01T00:00:20 | 360.00084183659885 | radians |
|
|
+---------------------+--------------------+---------+
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 20, '5s') deg(angles);
|
|
|
|
+---------------------+--------------------+---------+
|
|
| ts | degrees(val) | unit |
|
|
+---------------------+--------------------+---------+
|
|
| 1970-01-01T00:00:00 | 0.0 | degrees |
|
|
| 1970-01-01T00:00:00 | 0.0 | radians |
|
|
| 1970-01-01T00:00:05 | 5156.620156177409 | degrees |
|
|
| 1970-01-01T00:00:05 | 90.00021045914971 | radians |
|
|
| 1970-01-01T00:00:10 | 10313.240312354817 | degrees |
|
|
| 1970-01-01T00:00:10 | 180.00042091829943 | radians |
|
|
| 1970-01-01T00:00:15 | 15469.860468532228 | degrees |
|
|
| 1970-01-01T00:00:15 | 270.0006313774491 | radians |
|
|
| 1970-01-01T00:00:20 | 20626.480624709635 | degrees |
|
|
| 1970-01-01T00:00:20 | 360.00084183659885 | radians |
|
|
+---------------------+--------------------+---------+
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 20, '5s') scalar(deg(angles{unit="radians"}));
|
|
|
|
+---------------------+----------------------+
|
|
| ts | scalar(degrees(val)) |
|
|
+---------------------+----------------------+
|
|
| 1970-01-01T00:00:00 | 0.0 |
|
|
| 1970-01-01T00:00:05 | 90.00021045914971 |
|
|
| 1970-01-01T00:00:10 | 180.00042091829943 |
|
|
| 1970-01-01T00:00:15 | 270.0006313774491 |
|
|
| 1970-01-01T00:00:20 | 360.00084183659885 |
|
|
+---------------------+----------------------+
|
|
|
|
-- Test conversions with pi()
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 20, '5s') rad(deg(pi()));
|
|
|
|
Error: 2000(InvalidSyntax), expected type vector in call to function 'deg', got scalar
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 20, '5s') deg(rad(pi()));
|
|
|
|
Error: 2000(InvalidSyntax), expected type vector in call to function 'rad', got scalar
|
|
|
|
-- Test with arithmetic operations
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 20, '5s') deg(angles{unit="radians"}) + rad(angles{unit="degrees"});
|
|
|
|
+------+----+-------------------------------------+
|
|
| unit | ts | lhs.degrees(val) + rhs.radians(val) |
|
|
+------+----+-------------------------------------+
|
|
+------+----+-------------------------------------+
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 20, '5s') deg(pi() * 2);
|
|
|
|
Error: 2000(InvalidSyntax), expected type vector in call to function 'deg', got scalar
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 20, '5s') rad(180 * angles{unit="radians"});
|
|
|
|
+---------------------+-----------------------------+---------+
|
|
| 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
|
|
TQL EVAL (0, 35, '5s') sgn(angles{unit="positive"});
|
|
|
|
+---------------------+-------------+----------+
|
|
| ts | signum(val) | unit |
|
|
+---------------------+-------------+----------+
|
|
| 1970-01-01T00:00:25 | 1.0 | positive |
|
|
| 1970-01-01T00:00:30 | 1.0 | positive |
|
|
| 1970-01-01T00:00:35 | 1.0 | positive |
|
|
+---------------------+-------------+----------+
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 35, '5s') sgn(angles{unit="negative"});
|
|
|
|
+---------------------+-------------+----------+
|
|
| ts | signum(val) | unit |
|
|
+---------------------+-------------+----------+
|
|
| 1970-01-01T00:00:25 | -1.0 | negative |
|
|
| 1970-01-01T00:00:30 | -1.0 | negative |
|
|
| 1970-01-01T00:00:35 | -1.0 | negative |
|
|
+---------------------+-------------+----------+
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 35, '5s') sgn(angles{unit="zero"});
|
|
|
|
+---------------------+-------------+------+
|
|
| ts | signum(val) | unit |
|
|
+---------------------+-------------+------+
|
|
| 1970-01-01T00:00:30 | 0.0 | zero |
|
|
| 1970-01-01T00:00:35 | 0.0 | zero |
|
|
+---------------------+-------------+------+
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 35, '5s') sgn(angles{unit="small_pos"});
|
|
|
|
+---------------------+-------------+-----------+
|
|
| ts | signum(val) | unit |
|
|
+---------------------+-------------+-----------+
|
|
| 1970-01-01T00:00:30 | 1.0 | small_pos |
|
|
| 1970-01-01T00:00:35 | 1.0 | small_pos |
|
|
+---------------------+-------------+-----------+
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (0, 35, '5s') sgn(angles{unit="small_neg"});
|
|
|
|
+---------------------+-------------+-----------+
|
|
| ts | signum(val) | unit |
|
|
+---------------------+-------------+-----------+
|
|
| 1970-01-01T00:00:35 | -1.0 | small_neg |
|
|
+---------------------+-------------+-----------+
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (25, 35, '5s') sgn(angles);
|
|
|
|
+---------------------+-------------+-----------+
|
|
| ts | signum(val) | unit |
|
|
+---------------------+-------------+-----------+
|
|
| 1970-01-01T00:00:25 | -1.0 | negative |
|
|
| 1970-01-01T00:00:25 | 1.0 | degrees |
|
|
| 1970-01-01T00:00:25 | 1.0 | positive |
|
|
| 1970-01-01T00:00:25 | 1.0 | radians |
|
|
| 1970-01-01T00:00:30 | -1.0 | negative |
|
|
| 1970-01-01T00:00:30 | 0.0 | zero |
|
|
| 1970-01-01T00:00:30 | 1.0 | degrees |
|
|
| 1970-01-01T00:00:30 | 1.0 | positive |
|
|
| 1970-01-01T00:00:30 | 1.0 | radians |
|
|
| 1970-01-01T00:00:30 | 1.0 | small_pos |
|
|
| 1970-01-01T00:00:35 | -1.0 | negative |
|
|
| 1970-01-01T00:00:35 | -1.0 | small_neg |
|
|
| 1970-01-01T00:00:35 | 0.0 | zero |
|
|
| 1970-01-01T00:00:35 | 1.0 | degrees |
|
|
| 1970-01-01T00:00:35 | 1.0 | positive |
|
|
| 1970-01-01T00:00:35 | 1.0 | radians |
|
|
| 1970-01-01T00:00:35 | 1.0 | small_pos |
|
|
+---------------------+-------------+-----------+
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (25, 35, '5s') scalar(sgn(angles{unit="positive"}));
|
|
|
|
+---------------------+---------------------+
|
|
| ts | scalar(signum(val)) |
|
|
+---------------------+---------------------+
|
|
| 1970-01-01T00:00:25 | 1.0 |
|
|
| 1970-01-01T00:00:30 | 1.0 |
|
|
| 1970-01-01T00:00:35 | 1.0 |
|
|
+---------------------+---------------------+
|
|
|
|
-- FIXME: test sgn with arithmetic operations
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (25, 35, '5s') sgn(angles - 42.5);
|
|
|
|
+---------------------+-----------------------------+-----------+
|
|
| 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());
|
|
|
|
+---------------------+----------------------------+---------+
|
|
| ts | signum(angles.val - .pi()) | unit |
|
|
+---------------------+----------------------------+---------+
|
|
| 1970-01-01T00:00:00 | -1.0 | radians |
|
|
| 1970-01-01T00:00:05 | -1.0 | radians |
|
|
| 1970-01-01T00:00:10 | 1.0 | radians |
|
|
| 1970-01-01T00:00:15 | 1.0 | radians |
|
|
| 1970-01-01T00:00:20 | 1.0 | radians |
|
|
| 1970-01-01T00:00:25 | 1.0 | radians |
|
|
| 1970-01-01T00:00:30 | 1.0 | radians |
|
|
| 1970-01-01T00:00:35 | 1.0 | radians |
|
|
+---------------------+----------------------------+---------+
|
|
|
|
-- SQLNESS SORT_RESULT 3 1
|
|
TQL EVAL (25, 35, '5s') sgn(angles) * angles;
|
|
|
|
+-----------+---------------------+---------------------------+
|
|
| unit | ts | lhs.signum(val) * rhs.val |
|
|
+-----------+---------------------+---------------------------+
|
|
| degrees | 1970-01-01T00:00:25 | 360.0 |
|
|
| degrees | 1970-01-01T00:00:30 | 360.0 |
|
|
| degrees | 1970-01-01T00:00:35 | 360.0 |
|
|
| negative | 1970-01-01T00:00:25 | 17.3 |
|
|
| negative | 1970-01-01T00:00:30 | 17.3 |
|
|
| negative | 1970-01-01T00:00:35 | 17.3 |
|
|
| positive | 1970-01-01T00:00:25 | 42.5 |
|
|
| positive | 1970-01-01T00:00:30 | 42.5 |
|
|
| positive | 1970-01-01T00:00:35 | 42.5 |
|
|
| radians | 1970-01-01T00:00:25 | 6.2832 |
|
|
| radians | 1970-01-01T00:00:30 | 6.2832 |
|
|
| radians | 1970-01-01T00:00:35 | 6.2832 |
|
|
| small_neg | 1970-01-01T00:00:35 | 0.001 |
|
|
| small_pos | 1970-01-01T00:00:30 | 0.001 |
|
|
| small_pos | 1970-01-01T00:00:35 | 0.001 |
|
|
| zero | 1970-01-01T00:00:30 | 0.0 |
|
|
| zero | 1970-01-01T00:00:35 | 0.0 |
|
|
+-----------+---------------------+---------------------------+
|
|
|
|
Drop table angles;
|
|
|
|
Affected Rows: 0
|
|
|