Skip to content

Commit

Permalink
fix: fix tab completion for relation (#12908)
Browse files Browse the repository at this point in the history
  • Loading branch information
xxchan authored Oct 17, 2023
1 parent 7a1f371 commit ddeea32
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
25 changes: 25 additions & 0 deletions e2e_test/batch/subquery/tab_completion.slt.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# queries from psql tab completion

statement ok
create table ttttt(x int);

statement ok
create table tttt(x int);

# select * from tt<tab>
query I rowsort
SELECT pg_catalog.quote_ident(c.relname) FROM pg_catalog.pg_class c WHERE c.relkind IN ('r', 'S', 'v', 'm', 'f', 'p') AND substring(pg_catalog.quote_ident(c.relname),1,2)='tt' AND pg_catalog.pg_table_is_visible(c.oid) AND c.relnamespace <> (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')
UNION
SELECT pg_catalog.quote_ident(n.nspname) || '.' FROM pg_catalog.pg_namespace n WHERE substring(pg_catalog.quote_ident(n.nspname) || '.',1,2)='tt' AND (SELECT pg_catalog.count(*) FROM pg_catalog.pg_namespace WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,2) = substring('tt',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) > 1
UNION
SELECT pg_catalog.quote_ident(n.nspname) || '.' || pg_catalog.quote_ident(c.relname) FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n WHERE c.relnamespace = n.oid AND c.relkind IN ('r', 'S', 'v', 'm', 'f', 'p') AND substring(pg_catalog.quote_ident(n.nspname) || '.' || pg_catalog.quote_ident(c.relname),1,2)='tt' AND substring(pg_catalog.quote_ident(n.nspname) || '.',1,2) = substring('tt',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1) AND (SELECT pg_catalog.count(*) FROM pg_catalog.pg_namespace WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,2) = substring('tt',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1
LIMIT 1000
----
tttt
ttttt

statement ok
drop table tttt;

statement ok
drop table ttttt;
36 changes: 26 additions & 10 deletions src/frontend/src/optimizer/plan_visitor/cardinality_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use std::ops::{Mul, Sub};
use risingwave_pb::plan_common::JoinType;

use super::{DefaultBehavior, DefaultValue, PlanVisitor};
use crate::catalog::system_catalog::pg_catalog::PG_NAMESPACE_TABLE_NAME;
use crate::optimizer::plan_node::generic::TopNLimit;
use crate::optimizer::plan_node::{
self, PlanNode, PlanTreeNode, PlanTreeNodeBinary, PlanTreeNodeUnary,
Expand All @@ -44,18 +43,35 @@ impl CardinalityVisitor {
// We don't have UNIQUE key now. So we hack here to support some complex queries on
// system tables.
// TODO(card): remove this after we have UNIQUE key. https://github.com/risingwavelabs/risingwave/issues/12514
if let Some(scan) = input.as_logical_scan()
&& scan.is_sys_table()
&& scan.table_name() == PG_NAMESPACE_TABLE_NAME
{
if let Some(nspname) = scan
.output_col_idx()
.iter()
.find(|i| scan.table_desc().columns[**i].name == "nspname") {
unique_keys.push([*nspname].into_iter().collect());
// Hack for unique key `nspname` on `pg_catalog.pg_namespace`
//
// LogicalFilter { predicate: (rw_schemas.name = ...) }
// (below is expanded logical view, see src/frontend/src/catalog/system_catalog/pg_catalog/pg_namespace.rs)
// └─LogicalProject { exprs: [rw_schemas.id, rw_schemas.name, rw_schemas.owner, rw_schemas.acl] }
// └─LogicalScan { table: rw_schemas, columns: [id, name, owner, acl] }
fn try_get_unique_key_from_pg_namespace(plan: &dyn PlanNode) -> Option<HashSet<usize>> {
let proj = plan.as_logical_project()?;
if !proj.is_identity() {
return None;
}
let scan = proj.input();
let scan = scan.as_logical_scan()?;
if scan.is_sys_table() && scan.table_name() == "rw_schemas" {
if let Some(name) = scan
.output_col_idx()
.iter()
.find(|i| scan.table_desc().columns[**i].name == "name")
{
return Some([*name].into_iter().collect());
}
}
None
}
if let Some(unique_key) = try_get_unique_key_from_pg_namespace(input) {
unique_keys.push(unique_key);
}
}

if unique_keys
.iter()
.any(|unique_key| eq_set.is_superset(unique_key))
Expand Down

0 comments on commit ddeea32

Please sign in to comment.