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
9 changes: 4 additions & 5 deletions influxdb3_cache/src/distinct_cache/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl DistinctCache {
?predicates,
?projection,
?limit,
">>> distinct cache record batches"
"distinct cache record batches"
);
let n_columns = projection
.as_ref()
Expand Down Expand Up @@ -410,17 +410,16 @@ impl Node {
} else if next_predicates.is_empty() && next_builders.is_empty() {
if let Some(builder) = builder {
builder.append_value(value.0);
total_count += 1;
}
}
if let Some(new_limit) = limit.checked_sub(count) {
limit = new_limit;
} else {
break;
}
} else {
if let Some(builder) = builder {
builder.append_value(value.0);
}
} else if let Some(builder) = builder {
builder.append_value(value.0);
total_count += 1;
}
}
Expand Down
155 changes: 155 additions & 0 deletions influxdb3_cache/src/distinct_cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,4 +1015,159 @@ mod tests {
&results
);
}

#[test_log::test(tokio::test)]
async fn test_distinct_with_where_clause_bug() {
let writer = TestWriter::new().await;
writer
.catalog()
.create_table(
TestWriter::DB_NAME,
"bar",
&["t1", "t2", "t3"],
&[("f1", FieldDataType::Float), ("f2", FieldDataType::Float)],
)
.await
.unwrap();
writer
.catalog()
.create_distinct_cache(
TestWriter::DB_NAME,
"bar",
Some("foo"),
&["t1", "t2", "t3"],
Default::default(),
Default::default(),
)
.await
.unwrap();

let dvc = DistinctCacheProvider::new_from_catalog(
writer.catalog().time_provider(),
writer.catalog(),
)
.await
.unwrap();

let write_batch = writer
.write_lp_to_write_batch(
"\
bar,t1=AA,t2=\"(1\\,\\ 2]\",t3=None f1=-0.0621216848940003,f2=160.75626873391323\n\
bar,t1=BB,t2=\"(0\\,\\ 1]\",t3=\"(2.0\\,\\ 4.0]\" f1=0.0183506941911869,f2=60.72371267622072\n\
",
100,
)
.await;
let wal_contents = influxdb3_wal::create::wal_contents(
(0, 100, 1),
[influxdb3_wal::create::write_batch_op(write_batch)],
);
dvc.write_wal_contents_to_cache(&wal_contents);

let ctx = SessionContext::new();
let distinct_func = DistinctCacheFunction::new(writer.db_schema().id, Arc::clone(&dvc));
ctx.register_udtf(DISTINCT_CACHE_UDTF_NAME, Arc::new(distinct_func));

// should be able to do a basic query to distinct cache:
let results = ctx
.sql("select * from distinct_cache('bar', 'foo')")
.await
.unwrap()
.collect()
.await
.unwrap();

assert_batches_eq!(
[
"+----+----------+--------------+",
"| t1 | t2 | t3 |",
"+----+----------+--------------+",
"| AA | \"(1, 2]\" | None |",
"| BB | \"(0, 1]\" | \"(2.0, 4.0]\" |",
"+----+----------+--------------+",
],
&results
);

// should be able to query with a WHERE clause:
let results = ctx
.sql("select * from distinct_cache('bar', 'foo') where t1 = 'BB'")
.await
.unwrap()
.collect()
.await
.unwrap();

assert_batches_eq!(
[
"+----+----------+--------------+",
"| t1 | t2 | t3 |",
"+----+----------+--------------+",
"| BB | \"(0, 1]\" | \"(2.0, 4.0]\" |",
"+----+----------+--------------+",
],
&results
);

// should be able to query with a projection:
let results = ctx
.sql("select t2 from distinct_cache('bar', 'foo')")
.await
.unwrap()
.collect()
.await
.unwrap();

assert_batches_eq!(
[
"+----------+",
"| t2 |",
"+----------+",
"| \"(1, 2]\" |",
"| \"(0, 1]\" |",
"+----------+",
],
&results
);

// should be able to query with projection and a WHERE clause:
let results = ctx
.sql("select t2 from distinct_cache('bar', 'foo') where t1 = 'BB'")
.await
.unwrap()
.collect()
.await
.unwrap();

assert_batches_eq!(
[
"+----------+",
"| t2 |",
"+----------+",
"| \"(0, 1]\" |",
"+----------+",
],
&results
);

// should be able to query with projection and a WHERE clause:
let results = ctx
.sql("select t2, t3 from distinct_cache('bar', 'foo') where t1 = 'BB'")
.await
.unwrap()
.collect()
.await
.unwrap();

assert_batches_eq!(
[
"+----------+--------------+",
"| t2 | t3 |",
"+----------+--------------+",
"| \"(0, 1]\" | \"(2.0, 4.0]\" |",
"+----------+--------------+",
],
&results
);
}
}
4 changes: 4 additions & 0 deletions influxdb3_catalog/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ impl Catalog {
Ok(catalog)
}

pub fn time_provider(&self) -> Arc<dyn TimeProvider> {
Arc::clone(&self.time_provider)
}

pub fn set_state_shutdown(&self) {
*self.state.lock() = CatalogState::Shutdown;
}
Expand Down