Skip to content

Commit

Permalink
fix: correct the case sensitivity behavior for PromQL (#3296)
Browse files Browse the repository at this point in the history
* fix: correct the case sensitivity behavior for PromQL

Signed-off-by: Ruihang Xia <[email protected]>

* remove debug code

Signed-off-by: Ruihang Xia <[email protected]>

* consolidate sqlness case

Signed-off-by: Ruihang Xia <[email protected]>

* drop table

Signed-off-by: Ruihang Xia <[email protected]>

---------

Signed-off-by: Ruihang Xia <[email protected]>
  • Loading branch information
waynexia authored Feb 8, 2024
1 parent 6b4be3a commit 2f98fa0
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/promql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,16 @@ impl PromPlanner {
(None, None) => {
let left_input = self.prom_expr_to_plan(*lhs.clone()).await?;
let left_field_columns = self.ctx.field_columns.clone();
let mut left_table_ref: OwnedTableReference =
self.ctx.table_name.clone().unwrap_or_default().into();
let mut left_table_ref = OwnedTableReference::bare(
self.ctx.table_name.clone().unwrap_or_default(),
);
let left_context = self.ctx.clone();

let right_input = self.prom_expr_to_plan(*rhs.clone()).await?;
let right_field_columns = self.ctx.field_columns.clone();
let mut right_table_ref: OwnedTableReference =
self.ctx.table_name.clone().unwrap_or_default().into();
let mut right_table_ref = OwnedTableReference::bare(
self.ctx.table_name.clone().unwrap_or_default(),
);
let right_context = self.ctx.clone();

// TODO(ruihang): avoid join if left and right are the same table
Expand Down Expand Up @@ -572,6 +574,7 @@ impl PromPlanner {
.context(NoMetricMatcherSnafu)?,
);
}

self.ctx.table_name = metric_name;

let mut matchers = HashSet::new();
Expand Down Expand Up @@ -1883,7 +1886,7 @@ impl PromPlanner {
.chain(self.ctx.time_index_column.iter())
.map(|col| {
Ok(DfExpr::Column(Column::new(
self.ctx.table_name.clone(),
self.ctx.table_name.clone().map(TableReference::bare),
col,
)))
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ desc table RUNTIME_METRICS;
| timestamp | TimestampMillisecond | | NO | | FIELD |
+-------------+----------------------+-----+------+---------+---------------+

drop table my_db.foo;

Affected Rows: 0

use public;

Affected Rows: 0
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/standalone/common/system/information_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,6 @@ select * from CHECK_CONSTRAINTS;

desc table RUNTIME_METRICS;

drop table my_db.foo;

use public;
77 changes: 77 additions & 0 deletions tests/cases/standalone/common/tql/case_sensitive.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
create table "MemAvailable" (ts timestamp time index, instance string primary key, val double);

Affected Rows: 0

create table "MemTotal" (ts timestamp time index, instance string primary key, val double);

Affected Rows: 0

insert into "MemAvailable" values
(0, 'host0', 10),
(5000, 'host0', 20),
(10000, 'host0', 30),
(0, 'host1', 40),
(5000, 'host1', 50),
(10000, 'host1', 60);

Affected Rows: 6

insert into "MemTotal" values
(0, 'host0', 100),
(5000, 'host0', 100),
(10000, 'host0', 100),
(0, 'host1', 100),
(5000, 'host1', 100),
(10000, 'host1', 100);

Affected Rows: 6

select table_name from information_schema.tables where table_type = 'BASE TABLE' order by table_id;

+--------------+
| table_name |
+--------------+
| MemAvailable |
| MemTotal |
+--------------+

-- SQLNESS SORT_RESULT 3 1
tql eval (0,10,'5s') sum(MemAvailable / 4) + sum(MemTotal / 4);

+---------------------+---------------------------------------------------------------------+
| ts | MemAvailable.SUM(val / Float64(4)) + MemTotal.SUM(val / Float64(4)) |
+---------------------+---------------------------------------------------------------------+
| 1970-01-01T00:00:00 | 62.5 |
| 1970-01-01T00:00:05 | 67.5 |
| 1970-01-01T00:00:10 | 72.5 |
+---------------------+---------------------------------------------------------------------+

drop table "MemTotal";

Affected Rows: 0

create schema "AnotherSchema";

Affected Rows: 1

create table "AnotherSchema"."MemTotal" (ts timestamp time index, instance string primary key, val double);

Affected Rows: 0

tql eval (0,10,'5s') sum(MemAvailable / 4) + sum(MemTotal / 4);

Error: 4001(TableNotFound), Table not found: greptime.public.MemTotal

-- Cross schema is not supported
tql eval (0,10,'5s') sum(MemAvailable / 4) + sum({__name__="AnotherSchema.MemTotal"} / 4);

Error: 4001(TableNotFound), Table not found: greptime.public.AnotherSchema.MemTotal

drop table "MemAvailable";

Affected Rows: 0

drop table "AnotherSchema"."MemTotal";

Affected Rows: 0

39 changes: 39 additions & 0 deletions tests/cases/standalone/common/tql/case_sensitive.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
create table "MemAvailable" (ts timestamp time index, instance string primary key, val double);

create table "MemTotal" (ts timestamp time index, instance string primary key, val double);

insert into "MemAvailable" values
(0, 'host0', 10),
(5000, 'host0', 20),
(10000, 'host0', 30),
(0, 'host1', 40),
(5000, 'host1', 50),
(10000, 'host1', 60);

insert into "MemTotal" values
(0, 'host0', 100),
(5000, 'host0', 100),
(10000, 'host0', 100),
(0, 'host1', 100),
(5000, 'host1', 100),
(10000, 'host1', 100);

select table_name from information_schema.tables where table_type = 'BASE TABLE' order by table_id;

-- SQLNESS SORT_RESULT 3 1
tql eval (0,10,'5s') sum(MemAvailable / 4) + sum(MemTotal / 4);

drop table "MemTotal";

create schema "AnotherSchema";

create table "AnotherSchema"."MemTotal" (ts timestamp time index, instance string primary key, val double);

tql eval (0,10,'5s') sum(MemAvailable / 4) + sum(MemTotal / 4);

-- Cross schema is not supported
tql eval (0,10,'5s') sum(MemAvailable / 4) + sum({__name__="AnotherSchema.MemTotal"} / 4);

drop table "MemAvailable";

drop table "AnotherSchema"."MemTotal";

0 comments on commit 2f98fa0

Please sign in to comment.