Hi,
Here is a set of small patches which improve the tab completion of joins
in psql. The completion of select queries and DML is very primitive in
psql but since we already have completion of relation names after FROM
and JOIN I think these small additions fit and at least I find them
useful myself. I don't use USING that much in application code but when
writing ad hoc queries I use it quite a bit.
## 0001-Complete-LATERAL-keyword-for-joins.patch
Adds support for the LATERAL keyword after JOIN.
## 0002-Complete-ON-and-USING-keywords-for-joins.patch
Adds completion of the ON and USING keywords.
## 0002-Complete-ON-and-USING-keywords-for-joins.patch
Adds completion of the first USING column.
Andreas
From acf9f238f34c9f72ade5a56d2a15d366993a5cfa Mon Sep 17 00:00:00 2001
From: Andreas Karlsson <andr...@proxel.se>
Date: Fri, 15 Nov 2024 20:05:17 +0100
Subject: [PATCH 1/3] Complete LATERAL keyword for joins
---
src/bin/psql/tab-complete.in.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index fad2277991d..095e4525df3 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -5136,7 +5136,7 @@ match_previous_words(int pattern_id,
/* ... JOIN ... */
else if (TailMatches("JOIN"))
- COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
+ COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_selectables, "LATERAL");
/* ... AT [ LOCAL | TIME ZONE ] ... */
else if (TailMatches("AT"))
--
2.45.2
From 9388c62ff7be3cec4b13f81dce1ae7085418d275 Mon Sep 17 00:00:00 2001
From: Andreas Karlsson <andr...@proxel.se>
Date: Fri, 15 Nov 2024 20:06:24 +0100
Subject: [PATCH 2/3] Complete ON and USING keywords for joins
---
src/bin/psql/tab-complete.in.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index 095e4525df3..617dd53365e 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -5137,6 +5137,18 @@ match_previous_words(int pattern_id,
/* ... JOIN ... */
else if (TailMatches("JOIN"))
COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_selectables, "LATERAL");
+ else if (TailMatches("JOIN", MatchAny) && !TailMatches("CROSS|NATURAL", "JOIN", MatchAny))
+ COMPLETE_WITH("ON", "USING (");
+ else if (TailMatches("JOIN", MatchAny, MatchAny) &&
+ !TailMatches("CROSS|NATURAL", "JOIN", MatchAny, MatchAny) && !TailMatches("ON|USING"))
+ COMPLETE_WITH("ON", "USING (");
+ else if (TailMatches("JOIN", "LATERAL", MatchAny, MatchAny) &&
+ !TailMatches("CROSS|NATURAL", "JOIN", "LATERAL", MatchAny, MatchAny) && !TailMatches("ON|USING"))
+ COMPLETE_WITH("ON", "USING (");
+ else if (TailMatches("JOIN", MatchAny, "USING") ||
+ TailMatches("JOIN", MatchAny, MatchAny, "USING") ||
+ TailMatches("JOIN", "LATERAL", MatchAny, MatchAny, "USING"))
+ COMPLETE_WITH("(");
/* ... AT [ LOCAL | TIME ZONE ] ... */
else if (TailMatches("AT"))
--
2.45.2
From c599c43f4f7e6fae753c888da5662b420d890fb3 Mon Sep 17 00:00:00 2001
From: Andreas Karlsson <andr...@proxel.se>
Date: Fri, 15 Nov 2024 20:06:41 +0100
Subject: [PATCH 3/3] Complete first member of USING column list
---
src/bin/psql/tab-complete.in.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index 617dd53365e..13304da74ec 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -5149,6 +5149,10 @@ match_previous_words(int pattern_id,
TailMatches("JOIN", MatchAny, MatchAny, "USING") ||
TailMatches("JOIN", "LATERAL", MatchAny, MatchAny, "USING"))
COMPLETE_WITH("(");
+ else if (TailMatches("JOIN", MatchAny, "USING", "("))
+ COMPLETE_WITH_ATTR(prev3_wd);
+ else if (TailMatches("JOIN", MatchAny, MatchAny, "USING", "("))
+ COMPLETE_WITH_ATTR(prev4_wd);
/* ... AT [ LOCAL | TIME ZONE ] ... */
else if (TailMatches("AT"))
--
2.45.2