Debian Codegraph @

src/harlequin_duckdb/completions.py

raw
1 from __future__ import annotations
2
3 import duckdb
4
5
6 def get_completion_data(
7 cur: duckdb.DuckDBPyConnection,
8 ) -> list[tuple[str, str, int, str | None]]:
9 keyword_data = cur.execute(
10 """
11 select distinct
12 keyword_name as label,
13 'kw' as type_label,
14 case when keyword_category = 'reserved' then 100 else 1000 end as priority,
15 null as context
16 from duckdb_keywords()
17 """
18 ).fetchall()
19
20 function_data = cur.execute(
21 """
22 select distinct
23 function_name as label,
24 case when function_type = 'pragma' then 'pragma'
25 when function_type = 'macro' then 'macro'
26 when function_type = 'aggregate' then 'agg'
27 when function_type = 'scalar' then 'fn'
28 when function_type = 'table' then 'fn->T'
29 else 'fn' end as type_label,
30 1000 as priority,
31 case
32 when database_name == 'system' then null
33 else schema_name
34 end as context
35 from duckdb_functions()
36 where database_name != 'temp'
37 """
38 ).fetchall()
39
40 settings_data = cur.execute(
41 """
42 select distinct
43 name as label,
44 'set' as type_label,
45 2000 as priority,
46 null as context
47 from duckdb_settings()
48 """
49 ).fetchall()
50
51 type_data = cur.execute(
52 """
53 with
54 system_types as (
55 select distinct
56 type_name as label,
57 'type' as type_label,
58 1000 as priority,
59 null as context
60 from duckdb_types()
61 where database_name = 'system'
62 ),
63 custom_types as (
64 select distinct
65 type_name as label,
66 'type' as type_label,
67 1000 as priority,
68 schema_name as context
69 from duckdb_types()
70 where
71 database_name not in ('system', 'temp')
72 and type_name not in (select label from system_types)
73 )
74 select *
75 from system_types
76 union all
77 select *
78 from custom_types
79 """
80 ).fetchall()
81
82 return [
83 *keyword_data,
84 *function_data,
85 *settings_data,
86 *type_data,
87 ]