Improved psql tab completion for joins

Started by Andreas Karlssonabout 1 year ago4 messages
#1Andreas Karlsson
andreas@proxel.se
3 attachment(s)

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

Attachments:

0001-Complete-LATERAL-keyword-for-joins.patchtext/x-patch; charset=UTF-8; name=0001-Complete-LATERAL-keyword-for-joins.patchDownload
From acf9f238f34c9f72ade5a56d2a15d366993a5cfa Mon Sep 17 00:00:00 2001
From: Andreas Karlsson <andreas@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

0002-Complete-ON-and-USING-keywords-for-joins.patchtext/x-patch; charset=UTF-8; name=0002-Complete-ON-and-USING-keywords-for-joins.patchDownload
From 9388c62ff7be3cec4b13f81dce1ae7085418d275 Mon Sep 17 00:00:00 2001
From: Andreas Karlsson <andreas@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

0003-Complete-first-member-of-USING-column-list.patchtext/x-patch; charset=UTF-8; name=0003-Complete-first-member-of-USING-column-list.patchDownload
From c599c43f4f7e6fae753c888da5662b420d890fb3 Mon Sep 17 00:00:00 2001
From: Andreas Karlsson <andreas@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

#2Tomas Vondra
tomas@vondra.me
In reply to: Andreas Karlsson (#1)
Re: Improved psql tab completion for joins

Hi,

On 11/16/24 17:59, Andreas Karlsson wrote:

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.

Thanks. On a cursory look these all seem reasonable to me. I'll do a bit
more review to make sure I didn't miss anything, and then I intend to
get this committed ...

regards

--
Tomas Vondra

#3Tomas Vondra
tomas@vondra.me
In reply to: Tomas Vondra (#2)
Re: Improved psql tab completion for joins

On 12/8/24 00:06, Tomas Vondra wrote:

Hi,

On 11/16/24 17:59, Andreas Karlsson wrote:

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.

Thanks. On a cursory look these all seem reasonable to me. I'll do a bit
more review to make sure I didn't miss anything, and then I intend to
get this committed ...

OK, pushed. Similarly to tho the other tab completion patches I
committed today, I chose not to squash the parts, even though these
changes seem to be in the same area. Seems tidier this way.

thanks!

--
Tomas Vondra

#4Andreas Karlsson
andreas@proxel.se
In reply to: Tomas Vondra (#3)
Re: Improved psql tab completion for joins

On 12/16/24 6:51 PM, Tomas Vondra wrote:

OK, pushed. Similarly to tho the other tab completion patches I
committed today, I chose not to squash the parts, even though these
changes seem to be in the same area. Seems tidier this way.

Thanks!

Personally I do not care either way. Most committers seem to prefer a
bit more squashed patches than I do but either is fine.

Andreas