kaqing 2.0.102__py3-none-any.whl → 2.0.105__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of kaqing might be problematic. Click here for more details.

Files changed (47) hide show
  1. adam/batch.py +0 -14
  2. adam/commands/audit/audit.py +12 -16
  3. adam/commands/audit/audit_repair_tables.py +14 -37
  4. adam/commands/audit/audit_run.py +51 -0
  5. adam/commands/bash.py +60 -2
  6. adam/commands/cd.py +8 -8
  7. adam/commands/commands_utils.py +1 -2
  8. adam/commands/cql/cql_completions.py +4 -4
  9. adam/commands/cql/cql_utils.py +1 -1
  10. adam/commands/cql/cqlsh.py +1 -1
  11. adam/commands/deploy/deploy_pg_agent.py +2 -2
  12. adam/commands/deploy/undeploy_pg_agent.py +2 -2
  13. adam/commands/ls.py +14 -14
  14. adam/commands/nodetool.py +1 -1
  15. adam/commands/postgres/postgres.py +3 -3
  16. adam/commands/postgres/{postgres_session.py → postgres_context.py} +26 -27
  17. adam/commands/postgres/postgres_utils.py +5 -5
  18. adam/commands/postgres/psql_completions.py +1 -1
  19. adam/commands/preview_table.py +11 -30
  20. adam/commands/pwd.py +2 -2
  21. adam/embedded_params.py +1 -1
  22. adam/repl.py +3 -3
  23. adam/repl_commands.py +4 -7
  24. adam/repl_state.py +2 -2
  25. adam/sql/sql_completer.py +67 -76
  26. adam/sql/sql_state_machine.py +518 -0
  27. adam/sql/term_completer.py +3 -0
  28. adam/utils_audits.py +167 -0
  29. adam/utils_k8s/pods.py +2 -2
  30. adam/version.py +1 -1
  31. {kaqing-2.0.102.dist-info → kaqing-2.0.105.dist-info}/METADATA +1 -1
  32. {kaqing-2.0.102.dist-info → kaqing-2.0.105.dist-info}/RECORD +35 -44
  33. adam/commands/audit/audit_table_completer.py +0 -9
  34. adam/commands/cql/cql_table_completer.py +0 -8
  35. adam/commands/describe/__init__.py +0 -0
  36. adam/commands/describe/describe.py +0 -61
  37. adam/commands/describe/describe_keyspace.py +0 -58
  38. adam/commands/describe/describe_keyspaces.py +0 -46
  39. adam/commands/describe/describe_schema.py +0 -46
  40. adam/commands/describe/describe_table.py +0 -57
  41. adam/commands/describe/describe_tables.py +0 -46
  42. adam/commands/postgres/psql_table_completer.py +0 -11
  43. adam/sql/state_machine.py +0 -576
  44. adam/utils_athena.py +0 -95
  45. {kaqing-2.0.102.dist-info → kaqing-2.0.105.dist-info}/WHEEL +0 -0
  46. {kaqing-2.0.102.dist-info → kaqing-2.0.105.dist-info}/entry_points.txt +0 -0
  47. {kaqing-2.0.102.dist-info → kaqing-2.0.105.dist-info}/top_level.txt +0 -0
@@ -1,46 +0,0 @@
1
- from adam.commands.command import Command
2
- from adam.commands.cql.cql_utils import run_cql
3
- from adam.pod_exec_result import PodExecResult
4
- from adam.repl_state import ReplState, RequiredState
5
- from adam.utils import log2
6
-
7
- class DescribeTables(Command):
8
- COMMAND = 'describe tables'
9
-
10
- # the singleton pattern
11
- def __new__(cls, *args, **kwargs):
12
- if not hasattr(cls, 'instance'): cls.instance = super(DescribeTables, cls).__new__(cls)
13
-
14
- return cls.instance
15
-
16
- def __init__(self, successor: Command=None):
17
- super().__init__(successor)
18
-
19
- def required(self):
20
- return RequiredState.CLUSTER
21
-
22
- def command(self):
23
- return DescribeTables.COMMAND
24
-
25
- def run(self, cmd: str, state: ReplState):
26
- if not(args := self.args(cmd)):
27
- return super().run(cmd, state)
28
-
29
- state, args = self.apply_state(args, state)
30
- if not self.validate_state(state):
31
- return state
32
-
33
- args, all_nodes = Command.extract_options(args, '&')
34
- r: list[PodExecResult] = run_cql(state, f'{DescribeTables.COMMAND} {" ".join(args)}', show_out=True, on_any=not all_nodes)
35
- if not r:
36
- log2('No pod is available')
37
- return 'no-pod'
38
-
39
- # do not continue to cql route
40
- return state
41
-
42
- def completion(self, _: ReplState) -> dict[str, any]:
43
- return {}
44
-
45
- def help(self, _: ReplState) -> str:
46
- return f'{DescribeTables.COMMAND} [&]\t describe Cassandra tables'
@@ -1,11 +0,0 @@
1
- from adam.commands.postgres.postgres_utils import pg_table_names
2
- from adam.sql.term_completer import TermCompleter
3
-
4
- class PsqlTableNameCompleter(TermCompleter):
5
- def __init__(self, namespace: str, pg_path: str, ignore_case: bool = True):
6
- super().__init__(pg_table_names(namespace, pg_path), ignore_case=ignore_case)
7
- self.namespace = namespace
8
- self.pg_path = pg_path
9
-
10
- def __repr__(self) -> str:
11
- return "PsqlTableCompleter(%r, pg_path=%r)" % (self.namespace, self.pg_path)
adam/sql/state_machine.py DELETED
@@ -1,576 +0,0 @@
1
- from sqlparse.sql import Token
2
- from sqlparse import tokens as T
3
-
4
- __all__ = [
5
- "StateMachine",
6
- ]
7
-
8
- class SqlSpec:
9
- SPEC = [
10
- # <select_statement> ::= SELECT <select_list>
11
- # FROM <table_expression>
12
- # [WHERE <search_condition>]
13
- # [<group_by_clause>]
14
- # [<having_clause>]
15
- # [<order_by_clause>]
16
- # [<limit_clause>]
17
-
18
- # <search_condition> ::= <boolean_term>
19
- # | <search_condition> OR <boolean_term>
20
-
21
- # <boolean_term> ::= <boolean_factor>
22
- # | <boolean_term> AND <boolean_factor>
23
-
24
- # <boolean_factor> ::= [NOT] <predicate>
25
- # | ([NOT] <search_condition>)
26
-
27
- # <predicate> ::= <comparison_predicate>
28
- # | <between_predicate>
29
- # | <in_predicate>
30
- # | <like_predicate>
31
- # | <null_predicate>
32
- # | <exists_predicate>
33
- # | <quantified_predicate>
34
- # | <unique_predicate>
35
- # | <match_predicate>
36
- # | <overlaps_predicate>
37
- # | <distinct_predicate>
38
- # | <member_predicate>
39
- # | <submultiset_predicate>
40
- # | <set_predicate>
41
-
42
- # <comparison_predicate> ::= <row_value_expression> <comparison_operator> <row_value_expression>
43
- # <comparison_operator> ::= '=' | '<>' | '<' | '<=' | '>' | '>='
44
-
45
- # <row_value_expression> ::= <value_expression>
46
- # | (<value_expression> [ { <comma> <value_expression> }... ])
47
-
48
- # <value_expression> ::= <numeric_value_expression>
49
- # | <string_value_expression>
50
- # | <datetime_value_expression>
51
- # | <interval_value_expression>
52
- # | <boolean_value_expression>
53
- # | <user_defined_type_value_expression>
54
- # | <reference_value_expression>
55
- # | <collection_value_expression>
56
- # | <row_value_constructor>
57
- # | <case_expression>
58
- # | <cast_expression>
59
- # | <subquery>
60
- # | NULL
61
- # | DEFAULT
62
- # | <identifier>
63
- # | <literal>
64
- ' > select > select',
65
- 'select_ > name|* > select_a ^ *',
66
- 'select_a > , > select_a_comma_',
67
- 'select_a_comma_ > name|* > select_a ^ *',
68
- 'select_a_ > from > select_from ^ from',
69
- 'select_from_ > name|audit > select_from_x ^ (select,tables',
70
- '- > ( > select_from_lp_',
71
- '- < ) > select_from_sq',
72
- 'select_from_lp_ > select > select',
73
- 'select_from_x > , > select_from_x_comma_ ^ (select,tables',
74
- 'select_from_sq_ > as > select_from_x_as ^ as',
75
- 'select_from_x_comma_ > name > select_from_x ^ tables',
76
- 'select_from_x_ ^ as,where,inner join,left outer join,right outer join,full outer join,group by,order by,limit',
77
- 'select_from_x_as_x_ > , > select_from_x_comma_ ^ where,inner join,left outer join,right outer join,full outer join,group by,order by,limit',
78
- '- > as > select_from_x_as',
79
- '- > where > select_where',
80
- '- > order > select_order',
81
- '- > order by > select_order_by',
82
- '- > limit > select_where_sc_limit',
83
- '- > group > select_group',
84
- '- > group by > select_group_by',
85
- '- > inner > select_from_x_inner',
86
- '- > inner join > select_join',
87
- '- > left > select_from_x_left',
88
- '- > left join > select_join',
89
- '- > left outer join > select_join',
90
- '- > right > select_from_x_right',
91
- '- > right join > select_join',
92
- '- > right outer join > select_join',
93
- '- > full > select_from_x_full',
94
- '- > full outer join > select_join',
95
- 'select_from_x_as_ > name > select_from_x_as_x ^ x,y,z',
96
- 'select_from_x_as_x > , > select_from_x_as_x_comma_',
97
- 'select_from_x_as_x_comma_ > name > select_from_x ^ tables',
98
- 'select_where_ > name > select_where_a ^ columns',
99
- 'select_where_a > name > select_where_a ^ columns,=,<,<=,>,>=,<>',
100
- '- > comparison > select_where_a_op',
101
- 'select_where_a_ > comparison > select_where_a_op ^ =,<,<=,>,>=,<>,like,not,in',
102
- '- > not > select_where_a_not',
103
- '- > in > select_where_a_in',
104
- 'select_where_a_not_ > comparison > select_where_a_not_op ^ like,in',
105
- '- > in > select_where_a_in',
106
- 'select_where_a_in > ( > select_where_a_in_lp_ ^ (',
107
- '- < ) > select_where_sc',
108
- 'select_where_a_in_lp_ > name|single|num > select_where_a_in_lp_a ^ single,select',
109
- '- > select > select_where_a_in_lp_select',
110
- 'select_where_a_in_lp_select_ > name > select_a ^ id',
111
- 'select_where_a_in_lp_a > , > select_where_a_in_lp_a_comma_ ^ comma,)',
112
- 'select_where_a_in_lp_a_comma_ > name|single|num > select_where_a_in_lp_a ^ single',
113
- 'select_where_a_not_op > name|single|num > select_where_sc ^ single',
114
- 'select_where_a_op > name|single|num > select_where_sc ^ single',
115
- 'select_where_sc_ > and|or > select_where ^ and,or,order by,group by,limit',
116
- '- > group > select_group',
117
- '- > group by > select_group_by',
118
- '- > order > select_order',
119
- '- > order by > select_order_by',
120
- '- > limit > select_where_sc_limit',
121
- 'select_group_ > by > select_group_by ^ by',
122
- 'select_group_by_ > name > select_group_by_a ^ columns',
123
- 'select_group_by_a > name > select_group_by_a ^ columns',
124
- '- > , > select_group_by_a_comma_ ^ columns',
125
- 'select_group_by_a_comma_ > name > select_group_by_a ^ columns',
126
- 'select_group_by_a_ > limit > select_where_sc_limit ^ limit,order by',
127
- '- > order > select_order',
128
- '- > order by > select_order_by',
129
- 'select_order_ > by > select_order_by ^ by',
130
- 'select_order_by_ > name > select_order_by_a ^ columns',
131
- 'select_order_by_a > name > select_order_by_a ^ columns',
132
- '- > , > select_order_by_a_comma_',
133
- 'select_order_by_a_comma_ > name > select_order_by_a ^ columns',
134
- 'select_order_by_a_ > desc|asc > select_order_by_a_desc ^ desc,asc,limit',
135
- '- > limit > select_where_sc_limit',
136
- 'select_order_by_a_desc > , > select_order_by_a_comma_',
137
- 'select_order_by_a_desc_ > limit > select_where_sc_limit ^ limit',
138
- 'select_where_sc_limit_ > num > select_where_sc_limit_num ^ 1',
139
- 'select_where_sc_limit_num_rp__ > as > select_from_x_as ^ as',
140
- 'select_where_x_inner_ > join > select_join',
141
- 'select_join_ > name > select_x_join_y ^ tables',
142
- 'select_from_x_left_ > join > select_join ^ outer join',
143
- '- > outer > select_from_x_left_outer',
144
- 'select_from_x_left_outer_ > join > select_join ^ join',
145
- 'select_from_x_right_ > join > select_join ^ outer join',
146
- '- > outer > select_from_x_right_outer',
147
- 'select_from_x_right_outer_ > join > select_join ^ join',
148
- 'select_from_x_full_ > join > select_join ^ outer join',
149
- '- > outer > select_from_x_full_outer',
150
- 'select_from_x_full_outer_ > join > select_join ^ join',
151
- 'select_x_join_y > name > select_x_join_y ^ tables',
152
- 'select_x_join_y_ > as > select_x_join_y_as ^ as,on',
153
- '- > on > select_x_join_y_on ^ as,on',
154
- 'select_x_join_y_as_ > name > select_x_join_y_as_y ^ x,y,z',
155
- 'select_x_join_y_as_y_ > on > select_x_join_y_on ^ on',
156
- 'select_x_join_y_on_ > name > select_x_join_y_on_a ^ columns',
157
- 'select_x_join_y_on_a > name > select_x_join_y_on_a ^ columns,=',
158
- '- > comparison > select_x_join_y_on_a_op',
159
- 'select_x_join_y_on_a_ > comparison > select_x_join_y_on_a_op ^ =',
160
- 'select_x_join_y_on_a_op > name > select_x_join_y_on_a_op_b ^ columns',
161
- 'select_x_join_y_on_a_op_b > name > select_x_join_y_on_a_op_b ^ columns',
162
- '- > _ > select_from_x_as_x_',
163
-
164
- # <insert_statement> ::= INSERT INTO <table_name> [ ( <column_list> ) ]
165
- # VALUES ( <value_list> )
166
- # | INSERT INTO <table_name> [ ( <column_list> ) ]
167
- # <query_expression>
168
-
169
- # <table_name> ::= <identifier>
170
-
171
- # <column_list> ::= <column_name> [ , <column_list> ]
172
-
173
- # <column_name> ::= <identifier>
174
-
175
- # <value_list> ::= <expression> [ , <value_list> ]
176
-
177
- # <query_expression> ::= SELECT <select_list> FROM <table_reference_list> [ WHERE <search_condition> ] [ GROUP BY <grouping_column_list> ] [ HAVING <search_condition> ] [ ORDER BY <sort_specification_list> ]
178
- ' > insert > insert',
179
- 'insert_ > into > insert_into ^ into',
180
- 'insert_into_ > name > insert_into_x ^ tables',
181
- 'insert_into_x > name > insert_into_x ^ tables',
182
- '- > ( > insert_into_x_lp_',
183
- 'insert_into_x_ > ( > insert_into_x_lp_ ^ (,values(',
184
- '- > values > insert_values',
185
- 'insert_into_x_lp_ > name > insert_into_x_lp_a ^ id',
186
- 'insert_into_x_lp_a > , > insert_into_x_lp_a_comma_',
187
- '- > ) > insert_into_x_lp_a_rp_',
188
- 'insert_into_x_lp_a_comma_ > name > insert_into_x_lp_a ^ id',
189
- 'insert_into_x_lp_a_rp__ > values > insert_values ^ values(,select',
190
- '- > select > select',
191
- 'insert_values > ( > insert_values_lp_',
192
- 'insert_values_lp_ > name|single|num > insert_values_lp_v ^ single',
193
- 'insert_values_lp_v > , > insert_values_lp_v_comma_',
194
- 'insert_values_lp_v_comma_ > name|single|num > insert_values_lp_v',
195
-
196
- # <update_statement> ::= UPDATE <table_name>
197
- # SET <set_clause_list>
198
- # [WHERE <search_condition>]
199
-
200
- # <set_clause_list> ::= <set_clause> { , <set_clause> }
201
-
202
- # <set_clause> ::= <column_name> = <update_value>
203
-
204
- # <update_value> ::= <expression> | NULL | DEFAULT
205
-
206
- # <search_condition> ::= <boolean_expression>
207
- ' > update > update',
208
- 'update_ > name > update_x ^ tables',
209
- 'update_x > name > update_x ^ tables',
210
- 'update_x_ > set > update_set ^ set',
211
- 'update_set_ > name > update_set_a ^ id',
212
- 'update_set_a > comparison > update_set_a_op',
213
- 'update_set_a_op > name|single|num > update_set_sc ^ single',
214
- 'update_set_sc > , > update_set_sc_comma_',
215
- 'update_set_sc_comma_ > name > update_set_a ^ id',
216
- 'update_set_sc_ > , > update_set_sc_comma_ ^ where',
217
- '- > where > update_where',
218
- 'update_where_ > name > update_where_a ^ id',
219
- 'update_where_a > comparison > update_where_a_op',
220
- 'update_where_a_ > comparison > update_where_a_op ^ =,<,<=,>,>=,<>,like,not,in',
221
- '- > not > update_where_a_not',
222
- '- > in > update_where_a_in',
223
- 'update_where_a_not_ > comparison > update_where_a_not_op ^ like,in',
224
- '- > in > update_where_a_in',
225
- 'update_where_a_in > ( > update_where_a_in_lp_ ^ (',
226
- '- < ) > update_where_sc',
227
- 'update_where_a_in_lp_ > name|single|num > update_where_a_in_lp_a ^ single,select',
228
- '- > select > update_where_a_in_lp_select',
229
- 'update_where_a_in_lp_select_ > name > select_a ^ id',
230
- 'update_where_a_in_lp_a > , > update_where_a_in_lp_a_comma_ ^ comma,)',
231
- 'update_where_a_in_lp_a_comma_ > name|single|num > update_where_a_in_lp_a ^ single',
232
- 'update_where_a_not_op > name|single|num > update_where_sc ^ single',
233
- 'update_where_a_op > name|single|num > update_where_sc ^ single',
234
- 'update_where_sc_ > and|or > update_where ^ and,or',
235
-
236
- # <delete_statement> ::= DELETE FROM <table_name> [ WHERE <search_condition> ]
237
-
238
- # <table_name> ::= <identifier>
239
-
240
- # <search_condition> ::= <boolean_expression>
241
-
242
- # <boolean_expression> ::= <predicate>
243
- # | <boolean_expression> AND <predicate>
244
- # | <boolean_expression> OR <predicate>
245
- # | NOT <predicate>
246
- # | ( <boolean_expression> )
247
-
248
- # <predicate> ::= <expression> <comparison_operator> <expression>
249
- # | <expression> IS NULL
250
- # | <expression> IS NOT NULL
251
- # | <expression> LIKE <pattern> [ ESCAPE <escape_character> ]
252
- # | <expression> IN ( <expression_list> )
253
- # | EXISTS ( <select_statement> )
254
- # | ... (other predicates)
255
-
256
- # <comparison_operator> ::= = | <> | != | > | < | >= | <=
257
-
258
- # <expression> ::= <literal>
259
- # | <column_name>
260
- # | <function_call>
261
- # | ( <expression> )
262
- # | <expression> <arithmetic_operator> <expression>
263
- # | ... (other expressions)
264
-
265
- # <literal> ::= <numeric_literal> | <string_literal> | <boolean_literal> | <date_literal> | ...
266
-
267
- # <column_name> ::= <identifier>
268
-
269
- # <identifier> ::= <letter> { <letter> | <digit> | _ }...
270
-
271
- # <pattern> ::= <string_literal>
272
-
273
- # <escape_character> ::= <string_literal> (single character)
274
-
275
- # <expression_list> ::= <expression> { , <expression> }...
276
- ' > delete > delete',
277
- 'delete_ > from > delete_from ^ from',
278
- 'delete_from_ > name > delete_from_x ^ tables',
279
- 'delete_from_x > name > delete_from_x ^ tables',
280
- 'delete_from_x_ > where > update_where ^ where',
281
-
282
- # <alter table action> ::=
283
- # ADD <column definition>
284
- # | DROP COLUMN <column name>
285
- # | MODIFY COLUMN <column name> <column modification>
286
- # | RENAME TO <new table name>
287
- # | ADD CONSTRAINT <constraint definition>
288
- # | DROP CONSTRAINT <constraint name>
289
- # | ... (other actions like adding/dropping indexes, partitions, etc.)
290
-
291
- # <column definition> ::= <column name> <data type> [ <column constraint> ... ]
292
-
293
- # <column modification> ::=
294
- # SET DATA TYPE <data type>
295
- # | SET DEFAULT <expression>
296
- # | DROP DEFAULT
297
- # | SET NOT NULL
298
- # | DROP NOT NULL
299
- # | ...
300
-
301
- # <constraint definition> ::=
302
- # PRIMARY KEY ( <column name list> )
303
- # | UNIQUE ( <column name list> )
304
- # | FOREIGN KEY ( <column name list> ) REFERENCES <referenced table> ( <referenced column list> )
305
- # | CHECK ( <search condition> )
306
-
307
- ' > alter > alter',
308
- 'alter_ > table > alter_table ^ table',
309
- 'alter_table_ > name|audit > alter_table_t ^ tables',
310
- 'alter_table_t > name|audit > alter_table_t ^ tables',
311
- 'alter_table_t_ > add > alter_table_add ^ add,add constraint,drop column,drop constraint,rename to',
312
- '- > drop > alter_table_drop',
313
- ]
314
-
315
- KEYWORDS = [
316
- 'select', 'from', 'as', 'not', 'in', 'where',
317
- 'and', 'or', 'group', 'by', 'group by', 'order', 'order by', 'limit', 'asc', 'desc',
318
- 'inner join', 'on', 'left', 'right', 'full', 'outer', 'left outer join',
319
- 'left join', 'right outer join', 'right join', 'full join', 'full outer join',
320
- 'insert', 'into', 'values',
321
- 'update', 'where', 'set',
322
- 'delete',
323
- 'audit',
324
- 'alter', 'table', 'tables', 'add', 'drop', 'with',
325
- 'describe'
326
- ]
327
-
328
- def spec(self):
329
- return SqlSpec.SPEC
330
-
331
- def keywords(self):
332
- return SqlSpec.KEYWORDS
333
-
334
- class CqlSpec(SqlSpec):
335
- SPEC = SqlSpec.SPEC + [
336
- # ALTER TABLE [ <keyspace_name> . ] <table_name>
337
- # ( ALTER <column_name> TYPE <cql_type>
338
- # | ADD ( <column_definition_list> )
339
- # | DROP ( <column_list> )
340
- # | RENAME <column_name> TO <column_name> [ AND <column_name> TO <column_name> ... ]
341
- # | WITH <table_properties> );
342
-
343
- 'alter_ > table > alter_table ^ table,`tables`',
344
- '- > tables > alter_tables',
345
- 'alter_tables_ > with > alter_table_with ^ with',
346
- 'alter_table_t_ > with > alter_table_with ^ with,add,drop',
347
- 'alter_table_with_ > name > alter_table_with_p ^ table-props',
348
- 'alter_table_with_p > comparison > alter_table_with_p_op ^ =',
349
- 'alter_table_with_p_op > name|single|num > alter_table_with_p_op ^ table-prop-values',
350
-
351
- ' > describe > describe',
352
- 'describe_ > table > desc_table ^ table,`tables`,keyspace,keyspaces,schema',
353
- '- > tables > desc_tables',
354
- '- > keyspace > desc_keyspace',
355
- '- > keyspaces > desc_keyspaces',
356
- '- > schema > desc_schema',
357
- 'desc_table_ > name > desc_table_t ^ tables',
358
- 'desc_table_t > name > desc_table_t ^ tables',
359
- 'desc_table_t_ > & > desc_table_t_bg ^ &',
360
- 'desc_tables_ > & > desc_tables_bg ^ &',
361
- 'desc_keyspace_ > name > desc_keyspace_k',
362
- 'desc_keyspace_k_ > & > desc_keyspace_k_bg ^ &',
363
- 'desc_schema_ > & > desc_schema_bg ^ &',
364
- ]
365
-
366
- KEYWORDS = SqlSpec.KEYWORDS + [
367
- 'schema', 'keyspace', 'keyspaces', 'tables'
368
- ]
369
-
370
- def spec(self):
371
- return CqlSpec.SPEC
372
-
373
- def keywords(self):
374
- return CqlSpec.KEYWORDS
375
-
376
- class AthenaSpec(SqlSpec):
377
- SPEC = SqlSpec.SPEC + [
378
- 'alter_table_t_ > add > alter_table_add ^ add partition,drop partition',
379
- 'alter_table_add_ > partition > alter_partition ^ partition',
380
- 'alter_table_drop_ > partition > alter_partition ^ partition',
381
- 'alter_partition > ( > alter_partition_lp ^ (',
382
- 'alter_partition_lp > name > alter_partition_lp_a ^ partition-columns',
383
- 'alter_partition_lp_a > comparison > alter_partition_lp_a_op ^ =',
384
- 'alter_partition_lp_a_op > single > alter_partition_lp_a_op_v ^ single',
385
- 'alter_partition_lp_a_op_v > , > alter_partition_lp_sc ^ single',
386
- 'alter_partition_lp_sc > name|) > alter_partition_lp_a ^ partition-columns',
387
-
388
- ' > describe > describe',
389
- 'describe_ > name > desc_t ^ tables',
390
- 'desc_t > name > desc_t ^ tables',
391
- 'desc_t_ > name > desc_t_',
392
- ]
393
-
394
- KEYWORDS = SqlSpec.KEYWORDS + [
395
- 'partition'
396
- ]
397
-
398
- def spec(self):
399
- return AthenaSpec.SPEC
400
-
401
- def keywords(self):
402
- return AthenaSpec.KEYWORDS
403
-
404
- class StateTo:
405
- def __init__(self, to_s: str, comeback_token: str = None, comeback_state: str = None):
406
- self.to_s = to_s
407
- self.comeback_token = comeback_token
408
- self.comeback_state = comeback_state
409
- self.context: dict[str, str] = {}
410
-
411
- def __str__(self):
412
- return f'{self.to_s} comeback[{self.comeback_token} {self.comeback_state}]'
413
-
414
- class StateMachine:
415
- def __init__(self, variant = 'sql', indent=0, lp_level = 0, debug = False):
416
- self.variant = SqlSpec()
417
- if variant == 'cql':
418
- self.variant = CqlSpec()
419
- elif variant == 'athena':
420
- self.variant = AthenaSpec()
421
-
422
- self.states: dict[str, StateTo] = {}
423
- self.suggestions: dict[str, str] = {}
424
-
425
- self.indent = indent
426
- self.lp_level = lp_level
427
- self.comebacks: dict[int, str] = {}
428
- self.debug = debug
429
-
430
- from_ss_to_add = []
431
- from_ss = ['']
432
- words: str = None
433
- for l in self.variant.spec():
434
- t_and_w = l.split('^')
435
- if len(t_and_w) > 1:
436
- words = t_and_w[1]
437
- else:
438
- words = None
439
-
440
- tks = t_and_w[0].strip(' ').split('>')
441
- if not l.startswith('-'):
442
- if words:
443
- self.suggestions[tks[0].strip(' ')] = words
444
-
445
- if len(tks) == 1:
446
- from_ss_to_add.append(tks[0].strip(' '))
447
- continue
448
-
449
- from_ss = []
450
- from_ss.extend(from_ss_to_add)
451
- from_ss_to_add = []
452
- from_ss.append(tks[0].strip(' '))
453
-
454
- self.add_transitions(from_ss, tks)
455
-
456
- def add_transitions(self, from_ss: list[str], tks: list[str]):
457
- token = tks[1].strip(' ')
458
- if len(tks) > 2:
459
- to_s = tks[2].strip(' ')
460
- for from_s in from_ss:
461
- self.add_whitespace_transition(from_s, to_s)
462
- self.add_transition(from_s, token, to_s)
463
- elif '<' in tks[0]:
464
- from_and_token = tks[0].split('<')
465
- if len(from_and_token) > 1:
466
- for from_s in from_ss:
467
- self.add_comeback_transition(from_s, from_and_token[1], tks[1].strip(' '))
468
-
469
- def add_whitespace_transition(self, from_s: str, to_s: str):
470
- # add whitespace transition if a state with trailing whitespace is found from from states, for example, select > _ > select_
471
- if from_s.endswith('_') and not from_s.endswith('_comma_') and not from_s.endswith('_lp_') and not from_s.endswith('_rp_'):
472
- if self.debug:
473
- print(f'{from_s[:-1]} > _ = {to_s}')
474
- self.states[f'{from_s[:-1]} > _'] = StateTo(from_s)
475
-
476
- def add_transition(self, from_s: str, token: str, to_s: str):
477
- tokens = [token]
478
- if '|' in token:
479
- tokens = token.split('|')
480
-
481
- for t in tokens:
482
- if self.debug:
483
- print(f'{from_s} > {t} = {to_s}')
484
- self.states[f'{from_s} > {t}'] = StateTo(to_s)
485
-
486
- def add_comeback_transition(self, from_s: str, token: str, to_s: str):
487
- key = f'{from_s} > ('
488
- orig = self.states[key]
489
- if not orig:
490
- raise Exception(f'from state not found for {key}')
491
-
492
- orig.comeback_token = token
493
- orig.comeback_state = to_s
494
- if self.debug:
495
- print(f'{from_s} > ) = {to_s}')
496
- self.states[key] = orig
497
-
498
- def traverse_tokens(self, tokens: list[Token], state: StateTo = StateTo('')):
499
- def handle_opening_parenthesis():
500
- if f'{state.to_s} > {it}' in self.states:
501
- state_test = self.states[f'{state.to_s} > {it}']
502
- if state_test.comeback_token:
503
- self.comebacks[self.lp_level] = state_test.comeback_state
504
-
505
- def handle_closing_parenthesis():
506
- if self.lp_level in self.comebacks:
507
- try:
508
- return StateTo(self.comebacks[self.lp_level])
509
- finally:
510
- del self.comebacks[self.lp_level]
511
-
512
- return None
513
-
514
- for token in tokens:
515
- if self.debug:
516
- if token.ttype == T.Whitespace:
517
- print('_ ', end='')
518
- elif token.ttype in [T.DML, T.Wildcard, T.Punctuation, T.CTE]:
519
- print(f'{token.value} ', end='')
520
- elif token.ttype:
521
- tks = str(token.ttype).split('.')
522
- typ = tks[len(tks) - 1]
523
- if ' ' in token.value:
524
- print(f'"{token.value}:{typ}" ', end='')
525
- else:
526
- print(f'{token.value}:{typ} ', end='')
527
- # print(" " * self.indent + f"Token: {token.value}, Type: {token.ttype}@{token.ttype.__class__}")
528
-
529
- last_name = None
530
- if token.is_group:
531
- state = self.traverse_tokens(token.tokens, state)
532
- else:
533
- comeback_state = None
534
-
535
- it = ''
536
- if (t := token.value.lower()) in self.variant.keywords():
537
- it = t
538
- elif token.ttype == T.Text.Whitespace:
539
- it = '_'
540
- elif token.ttype == T.Name:
541
- it = 'name'
542
- last_name = token.value
543
- elif token.ttype == T.Literal.String.Single:
544
- it = 'single'
545
- elif token.ttype in [T.Literal.Number.Integer, T.Literal.Number.Float]:
546
- it = 'num'
547
- elif token.ttype == T.Wildcard:
548
- it = '*'
549
- elif token.ttype == T.Punctuation:
550
- it = token.value
551
-
552
- if it == '(':
553
- handle_opening_parenthesis()
554
- self.lp_level += 1
555
- elif it == ')':
556
- self.lp_level -= 1
557
- comeback_state = handle_closing_parenthesis()
558
-
559
- elif token.ttype == T.Operator.Comparison:
560
- it = 'comparison'
561
-
562
- try:
563
- # print(f'\n{state.to_s} > {it} > ', end='')
564
- if comeback_state:
565
- state = comeback_state
566
- else:
567
- context = state.context
568
- state = self.states[f'{state.to_s} > {it}']
569
- state.context = context
570
-
571
- if last_name:
572
- state.context['last_name'] = last_name
573
- except:
574
- pass
575
-
576
- return state