-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.py
More file actions
1246 lines (1034 loc) · 53.2 KB
/
connection.py
File metadata and controls
1246 lines (1034 loc) · 53.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Connection module for Confluent SQL DB-API driver.
This module provides the connect function and Connection class for establishing
connections to Confluent SQL services.
"""
from __future__ import annotations
import logging
import uuid
import warnings
from collections import namedtuple
from collections.abc import Generator
from contextlib import contextmanager
from dataclasses import fields, is_dataclass
from typing import Any
import httpx
from .__version__ import VERSION
from .cursor import Cursor
from .exceptions import (
InterfaceError,
OperationalError,
StatementDeletedError,
StatementNotFoundError,
)
from .execution_mode import ExecutionMode
from .statement import LABEL_PREFIX as STATEMENT_LABEL_PREFIX
from .statement import ChangelogRow, Statement
from .types import PropertiesDict, RowPythonTypes
logger = logging.getLogger(__name__)
def connect( # noqa: PLR0913
*,
flink_api_key: str,
flink_api_secret: str,
environment: str,
compute_pool_id: str,
organization_id: str,
cloud_provider: str | None = None,
cloud_region: str | None = None,
database: str | None = None,
endpoint: str | None = None,
dbname: str | None = None, # deprecated, use database parameter
result_page_fetch_pause_millis: int = 100,
http_user_agent: str | None = None,
) -> Connection:
"""
Create a connection to a Confluent SQL service.
Args:
flink_api_key: Flink region API key
flink_api_secret: Flink region API secret
environment: Environment ID
compute_pool_id: Compute pool ID for SQL execution
organization_id: Organization ID
cloud_provider: Cloud provider (e.g., "aws", "gcp", "azure"). Required if endpoint is not
provided; must not be provided if endpoint is specified.
cloud_region: Cloud region (e.g., "us-east-2", "us-west-2"). Required if endpoint is not
provided; must not be provided if endpoint is specified.
database: The default Flink database (Kafka cluster) to use when resolving
table/view/udf names (optional)
endpoint: The base URL for Confluent Cloud API (optional). If not provided, the public
networking endpoint will be constructed based on the cloud_provider and cloud_region
parameters in the format "https://flink.{cloud_region}.{cloud_provider}.confluent.cloud".
A trailing slash is optional and will be stripped if provided (e.g., both
"https://custom.example.com" and "https://custom.example.com/" are accepted).
If your Kafka clusters / Flink tables require private networking, supply the base
endpoint URL here (`"https://flink.us-east-2.aws.private.confluent.cloud"` for example,
but cases and private networking technologies vary).
dbname: Deprecated alias for database parameter (optional)
result_page_fetch_pause_millis: Maximum milliseconds to wait between fetching pages of
statement results (per statement). Defaults to 100ms. Prevents tight loops of requests
to the statement results API when consuming results for a statement, especially when
no results are currently available but more may be forthcoming, such as when
consuming results from a running streaming query, or prior to when the first page
of results is ready for a snapshot query.
If it has already been at least this long since the most recent fetch of results for the
statement, then no delay will happen.
http_user_agent: User-Agent header value for HTTP requests. Must be a string
between 1-100 characters. Defaults to
"Confluent-SQL-Dbapi/v<version> (https://confluent.io; support@confluent.io)"
where version is from __version__.py
Returns:
A Connection object representing the database connection
Raises:
InterfaceError: If connection parameters are invalid
OperationalError: If connection cannot be established
"""
if not environment:
raise InterfaceError("Environment ID is required")
if not compute_pool_id:
raise InterfaceError("Compute pool ID is required")
if not organization_id:
raise InterfaceError("Organization ID is required")
if endpoint:
if cloud_provider or cloud_region:
raise InterfaceError(
"cloud_provider and cloud_region should not be provided when endpoint is specified"
)
else:
if not cloud_provider:
raise InterfaceError("Cloud provider is required when endpoint is not provided")
if not cloud_region:
raise InterfaceError("Cloud region is required when endpoint is not provided")
if not flink_api_key or not flink_api_secret:
raise InterfaceError("Flink region API key and secret are required")
if dbname is not None:
warnings.warn(
"The 'dbname' parameter is deprecated and will be removed in a future release. "
"Please use the 'database' parameter instead.",
DeprecationWarning,
stacklevel=2,
)
if database is not None:
raise InterfaceError(
"Cannot specify both 'database' and deprecated 'dbname' parameters"
)
return Connection(
flink_api_key,
flink_api_secret,
environment,
compute_pool_id,
organization_id,
cloud_provider,
cloud_region,
endpoint,
database=database or dbname, # dbname is deprecated.
statement_results_page_fetch_pause_millis=result_page_fetch_pause_millis,
http_user_agent=http_user_agent,
)
class Connection:
"""
A connection to a Confluent SQL service.
This class represents a connection to a Confluent SQL service and provides
methods for creating cursors and managing the connection lifecycle.
"""
DEFAULT_USER_AGENT = (
f"Confluent-SQL-Dbapi/v{VERSION} (https://confluent.io; support@confluent.io)"
)
environment: str
organization_id: str
compute_pool_id: str
statement_results_page_fetch_pause_secs: float
"""Maximum seconds to wait between fetching pages of statement
results (per statement). Prevents tight loops of requests to the
statement results API when consuming results for a statement, especially when no results are
currently available but more may be forthcoming, such as when consuming results from
a running streaming query, or prior to when the first page of results is ready for
a snapshot query.
If it has already been at least this long since the most recent fetch of results for the
statement, then no delay will happen.
Referenced by the result reader when fetching pages of results for individual
statements.
"""
_closed: bool
_database: str | None
_client: httpx.Client
_http_user_agent: str
_row_type_registry: RowTypeRegistry
"""Registry for user-defined row types, see register_row_type()."""
_snapshot_warning_issued: bool
"""Internal flag to track whether the snapshot query early access warning has been issued.
Remove after snapshot queries reach open preview (expected May 2026)."""
def __init__( # noqa: PLR0913
self,
flink_api_key: str,
flink_api_secret: str,
environment: str,
compute_pool_id: str,
organization_id: str,
cloud_provider: str | None,
cloud_region: str | None,
endpoint: str | None,
database: str | None = None,
statement_results_page_fetch_pause_millis: int = 100,
http_user_agent: str | None = None,
):
"""
Initialize a new connection to a Confluent SQL service.
Args:
flink_api_key: Flink region API key
flink_api_secret: Flink region API secret
environment: Environment ID
compute_pool_id: Compute pool ID for SQL execution
organization_id: Organization ID
cloud_provider: Cloud provider (required if endpoint is not provided)
cloud_region: Cloud region (e.g., "us-east-2", "us-west-2"). Required if endpoint is
not provided.
endpoint: The base URL for Confluent Cloud API (optional). If not provided, the
endpoint will be constructed based on the cloud_provider and cloud_region
parameters in the format "https://flink.{cloud_region}.{cloud_provider}.confluent.cloud".
A trailing slash is optional and will be stripped if provided.
If your Kafka clusters / Flink tables require private networking, supply the base
endpoint URL here (`"https://flink.us-east-2.aws.private.confluent.cloud"` for
example, but cases and private networking technologies vary).
database: The name of the database to use (optional)
result_page_fetch_pause_millis: Milliseconds to possibly wait between fetching pages of
statement results. Defaults to 100ms. If most recent fetch of results for a
statement was more than this long ago, then no delay will happen when fetching
the next page of results for the statement.
http_user_agent: User-Agent header for HTTP requests. String, 1-100 chars.
Defaults to the value of DEFAULT_USER_AGENT, which includes the
driver name/version, documentation URL, and support email.
"""
self.environment = environment
self.compute_pool_id = compute_pool_id
self.organization_id = organization_id
if statement_results_page_fetch_pause_millis < 0:
raise InterfaceError("result_page_fetch_pause_millis must be non-negative")
# Will be referenced by cursor / result reader when
# fetching pages of results for individual statements.
self.statement_results_page_fetch_pause_secs = (
statement_results_page_fetch_pause_millis / 1000.0
)
# Internal state
self._closed = False
self._database = database
# Set user agent (validation happens in setter, default if None)
self.http_user_agent = (
http_user_agent if http_user_agent is not None else self.DEFAULT_USER_AGENT
)
if not endpoint and not (cloud_provider and cloud_region):
raise InterfaceError(
"cloud_provider and cloud_region are required when endpoint is not provided"
)
# Create httpx client for making API calls
if not endpoint:
# Construct the endpoint URL based on cloud provider and region.
endpoint = f"https://flink.{cloud_region}.{cloud_provider}.confluent.cloud"
else:
# Strip trailing slash if user provided one, to ensure clean URL construction
endpoint = endpoint.rstrip("/")
base_url = f"{endpoint}/sql/v1/organizations/{organization_id}/environments/{environment}"
# Create httpx client for making API calls
basic_auth = httpx.BasicAuth(username=flink_api_key, password=flink_api_secret)
self._client = httpx.Client(
auth=basic_auth,
base_url=base_url,
headers={
"Content-Type": "application/json",
"User-Agent": self._http_user_agent,
},
)
self._row_type_registry = RowTypeRegistry()
# TODO: remove after snapshot queries reach open preview (May 2026)
self._snapshot_warning_issued = False
def close(self) -> None:
"""
Close the connection.
"""
if not self._closed:
self._closed = True
self._client.close()
else:
logger.info("Trying to close a closed connection, ignoring")
def cursor(self, *, as_dict: bool = False, mode=ExecutionMode.SNAPSHOT) -> Cursor:
"""
Create a new cursor for executing statements. Defaults to creating
a snapshot (bounded) query cursor for returning point-in-time results.
Snapshot queries will return results from a consistent point in time, and
the result stream is considered both bounded and append-only, and will
only be generated when the query execution has completed, having consumed
all source data as of the query start time.
Streaming queries will return results as they are produced by the executing query,
but may or may not be append-only depending on the query characteristics. For example,
a streaming query that only filters from source tables (Kafka topics) will be append-only,
but a streaming query that performs aggregations or joins will not be, as updates to
previously emitted results may occur as more data is processed. Non-append-only streaming
query results will include a changelog operation with each row indicating whether the row
is an insertion, update, or deletion, indicated by the 'op' field in the returned
ChangeloggedRow namedtuple.
So, while mode=ExecutionMode.STREAMING_QUERY will always initiate a streaming query,
the presence of changelog operations in the results depends on whether the
query submitted will result in append-only processing or not.
See the documentation in the Cursor class for more details on the behavior
of the cursor, its fetch method and iteration behavior, as to the differences
between snapshot and streaming queries.
The cursor's fetch methods return different types based on configuration
and query characteristics:
Return Type Matrix
------------------
1. **Append-only queries + as_dict=False** (default):
Returns tuples: `("val1", "val2", ...)`
Standard DB-API format for regular SELECT queries
2. **Append-only queries + as_dict=True**:
Returns dicts: `{"col1": "val1", "col2": "val2"}`
Column names as keys for better readability
3. **Changelog queries + as_dict=False** (streaming non-append-only, row as tuples):
Returns ChangeloggedRow namedtuples: `ChangeloggedRow(op=Op.INSERT,row=("v1", "v2"))`
Includes operation type (INSERT/UPDATE_BEFORE/UPDATE_AFTER/DELETE) with row data
4. **Changelog queries + as_dict=True** (streaming non-append-only, row as dicts):
Returns ChangeloggedRow with dict: `ChangeloggedRow(op=Op.INSERT, row={"col1": "val1"})`
Combines operation tracking with named column access
Args:
as_dict: If True, return row data as dictionaries with column names as keys.
If False (default), return row data as tuples.
mode: The execution mode for the cursor. Defaults to SNAPSHOT for bounded
queries. Use STREAMING_QUERY for continuous/unbounded queries.
Returns:
A new Cursor object associated with this connection
Raises:
InterfaceError: If the connection is closed
Examples:
# Standard snapshot query with tuples
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")
assert not cursor.is_streaming
row = cursor.fetchone() # Returns: ("Alice", 25), or None if no more rows, period.
# Snapshot query with dicts
cursor = conn.cursor(as_dict=True)
cursor.execute("SELECT * FROM users")
assert cursor.as_dict == True
assert not cursor.is_streaming
row = cursor.fetchone() # Returns: {"name": "Alice", "age": 25} or None if no more rows
# Streaming append-only query with tuples
cursor = conn.cursor(mode=ExecutionMode.STREAMING_QUERY)
assert cursor.is_streaming
cursor.execute("SELECT user_id FROM orders")
assert not cursor.returns_changelog # Will not be known until after execute().
while cursor.may_have_results:
# Returns either ("Alice",) or None if _no data available at this time_.
row = cursor.fetchone()
if row is not None:
...
# Streaming changelog query
cursor = conn.cursor(mode=ExecutionMode.STREAMING_QUERY)
cursor.execute("SELECT user_id, count(*) from orders group by user_id")
assert cursor.is_streaming
assert cursor.returns_changelog
while cursor.may_have_results:
row = cursor.fetchone()
# may return None if _no data available at this time_
if row is not None:
# Returns a ChangeloggedRow namedtuple:
# ChangeloggedRow(op=Op.INSERT, row=("Alice", 25))
"""
if self._closed:
raise InterfaceError("Connection is closed")
# TODO: remove after snapshot queries reach open preview (May 2026)
if mode.is_snapshot and not self._snapshot_warning_issued:
self._snapshot_warning_issued = True
warnings.warn(
"Snapshot queries on Confluent Cloud Flink SQL are currently in "
"Early Access and may be subject to change.",
stacklevel=2,
)
return Cursor(self, as_dict=as_dict, execution_mode=mode)
def streaming_cursor(self, *, as_dict: bool = False) -> Cursor:
"""
Create a streaming query cursor. Waits for RUNNING, iterates over continuous results.
This is a convenience method equivalent to:
`cursor(as_dict=as_dict, mode=ExecutionMode.STREAMING_QUERY)`
For streaming queries, the return type depends on whether the query is append-only:
- Append-only: Returns tuples or dicts based on as_dict parameter
- Non-append-only: Returns ChangeloggedRow namedtuples containing operation and row data
See cursor() method documentation for detailed return type information.
Args:
as_dict: If True, return row data as dictionaries. If False, as tuples.
Returns:
A new Cursor configured for streaming query execution
"""
return Cursor(self, as_dict=as_dict, execution_mode=ExecutionMode.STREAMING_QUERY)
@contextmanager
def closing_cursor(
self, *, as_dict: bool = False, mode: ExecutionMode = ExecutionMode.SNAPSHOT
) -> Generator[Cursor, None, None]:
"""
Context manager for creating and automatically closing a cursor.
Creates a cursor with the same return type variations as cursor() method.
See cursor() documentation for details on the four possible return types
based on as_dict and query characteristics (append-only vs changelog).
Args:
as_dict: If True, fetch results as dictionaries, otherwise as tuples
mode: The execution mode for the cursor. Defaults to SNAPSHOT.
Yields:
A new Cursor object associated with this connection
Raises:
InterfaceError: If the connection is closed
Example:
with conn.closing_cursor(as_dict=True) as cursor:
cursor.execute("SELECT * FROM users")
for row in cursor:
print(row) # Prints dicts with column names
# cursor is automatically closed after the with block
"""
cursor = self.cursor(as_dict=as_dict, mode=mode)
try:
yield cursor
finally:
cursor.close()
@contextmanager
def closing_streaming_cursor(self, *, as_dict: bool = False) -> Generator[Cursor, None, None]:
"""
Context manager for creating and automatically closing a streaming cursor.
Convenience method equivalent to:
closing_cursor(as_dict=as_dict, mode=ExecutionMode.STREAMING_QUERY)
Creates a streaming cursor that processes continuous data from Flink SQL
with automatic cleanup. Streaming cursors return data as it arrives without
blocking or collecting all results into memory.
Statement Lifecycle Management:
The context manager automatically closes the cursor via cursor.close(),
which makes a best-effort attempt to delete statements that are already
in terminal phases (COMPLETED/FAILED/STOPPED). Deletion errors are
logged and suppressed, so server-side cleanup is not strictly
guaranteed. Long-running streaming queries that remain RUNNING on the
server after exiting the context manager are NOT automatically stopped
or deleted server-side.
To explicitly stop a RUNNING streaming statement, call
cursor.delete_statement() or connection.delete_statement(statement_id)
before exiting the context manager.
Args:
as_dict: If True, fetch results as dictionaries, otherwise as tuples
Yields:
A new streaming Cursor object associated with this connection
Raises:
InterfaceError: If the connection is closed
Example:
with conn.closing_streaming_cursor(as_dict=True) as cursor:
cursor.execute("SELECT * FROM orders WHERE amount > %s", (1000,))
while cursor.may_have_results:
rows = cursor.fetchmany(10)
if rows:
for row in rows:
process(row)
else:
time.sleep(0.1)
# cursor is automatically closed after the with block
"""
with self.closing_cursor(as_dict=as_dict, mode=ExecutionMode.STREAMING_QUERY) as cursor:
yield cursor
def execute_snapshot_ddl(
self,
statement_text: str,
parameters: tuple | list | None = None,
timeout: int = 3000,
statement_name: str | None = None,
statement_labels: list[str] | None = None,
properties: PropertiesDict | None = None,
*,
compute_pool_id: str | None = None,
) -> Statement:
"""Execute bounded DDL that completes after consuming snapshot data.
Use for statements like:
- CREATE TABLE (not AS SELECT)
- DROP TABLE
- ALTER TABLE
- CREATE VIEW
- DROP VIEW
- CREATE TABLE foo AS SELECT ... (snapshot mode, where the SELECT portion completes
with snapshot behavior)
Args:
statement_text: The DDL statement to execute
parameters: Optional statement parameters
timeout: Maximum time to wait for completion in seconds
statement_name: Optional name for the statement
statement_labels: Optional list of labels for the statement. Labels can be used to
group and manage related statements. Each label will be
prefixed with "user.confluent.io/" when stored but you only
need to provide the label values (e.g., ["my-ddl-batch", "daily"]).
Use HIDDEN_LABEL to mark statements as hidden from default views.
properties: Optional dictionary of statement properties to set for this execution.
Keys must be strings, values must be str, int, or bool.
compute_pool_id: Optional compute pool ID to use for this statement execution.
If not provided, uses the Connection's default compute_pool_id.
The compute pool must be accessible to the API key used for the
connection. Validation of compute pool accessibility is performed
by Confluent Cloud Flink.
Returns:
Statement for managing the statement lifecycle
Raises:
OperationalError: If statement fails or times out
ProgrammingError: If statement is invalid
"""
with self.closing_cursor(mode=ExecutionMode.SNAPSHOT_DDL) as cur:
cur.execute(
statement_text,
parameters,
timeout=timeout,
statement_name=statement_name,
statement_labels=statement_labels,
properties=properties,
compute_pool_id=compute_pool_id,
)
# Return the last version of the statement
return cur.statement
def execute_streaming_ddl(
self,
statement_text: str,
parameters: tuple | list | None = None,
timeout: int = 3000,
statement_name: str | None = None,
statement_labels: list[str] | None = None,
properties: PropertiesDict | None = None,
*,
compute_pool_id: str | None = None,
) -> Statement:
"""Execute unbounded DDL that starts a streaming job.
Use for statements like:
- CREATE TABLE ... AS SELECT ... (streaming mode, where the SELECT portion is unbounded)
- CREATE MATERIALIZED TABLE ... (streaming mode, where the table is populated by an
unbounded streaming job but the overall CREATE statement
itself completes once the population job is started)
Args:
statement_text: The DDL statement to execute
parameters: Optional statement parameters
timeout: Maximum time to wait for completion in seconds
statement_name: Optional name for the statement
statement_labels: Optional list of labels for the statement. Labels can be used to
group and manage related statements. Each label will be
prefixed with "user.confluent.io/" when stored but you only
need to provide the label values (e.g., ["streaming-jobs", "daily"]).
Use HIDDEN_LABEL to mark statements as hidden from default views.
properties: Optional dictionary of statement properties to set for this execution.
Keys must be strings, values must be str, int, or bool.
compute_pool_id: Optional compute pool ID to use for this statement execution.
If not provided, uses the Connection's default compute_pool_id.
The compute pool must be accessible to the API key used for the
connection. Validation of compute pool accessibility is performed
by Confluent Cloud Flink.
Returns:
Statement for any further management of the statement lifecycle
"""
with self.closing_cursor(mode=ExecutionMode.STREAMING_DDL) as cur:
cur.execute(
statement_text,
parameters,
timeout=timeout,
statement_name=statement_name,
statement_labels=statement_labels,
properties=properties,
compute_pool_id=compute_pool_id,
)
return cur.statement
def list_statements(self, *, label: str, page_size: int = 100) -> list[Statement]:
"""Return a list of Statement objects for statements with the given label.
This method retrieves all statements that were created with the specified label,
which is useful for managing groups of related statements. The method handles
pagination automatically to retrieve all matching statements.
Args:
label: The label to filter statements by. You can provide either:
- Just the label value (e.g., "my-batch-job") - the "user.confluent.io/"
prefix will be added automatically
- The full label with prefix (e.g., "user.confluent.io/my-batch-job")
page_size: Number of statements to fetch per API request (default: 100).
The method will automatically paginate through all results.
Returns:
A list of Statement objects that have the specified label. Returns an
empty list if no statements match the label.
Raises:
OperationalError: If the API request fails
Example:
# Submit statements with a label
cursor.execute("SELECT * FROM users", statement_labels=["daily-report"])
cursor.execute("SELECT * FROM orders", statement_labels=["daily-report"])
# Later, retrieve all statements with that label
statements = connection.list_statements(label="daily-report")
# Delete all statements with the label
for stmt in statements:
connection.delete_statement(stmt)
"""
if not label.startswith(STATEMENT_LABEL_PREFIX):
# Append prefix and make it a label selector for the API query parameter. The API
# expects the full label key, which includes the prefix, but we want to allow users
# to filter by just the end-user portion of the label.
adjusted_label_filter = f"{STATEMENT_LABEL_PREFIX}{label}=true"
else:
adjusted_label_filter = f"{label}=true"
statements: list[Statement] = []
has_more_pages = True
next_page_token: str | None = None
# Use the `label_selector` query parameter to filter statements by label
# on the server side.
parameters = {"label_selector": adjusted_label_filter, "page_size": page_size}
while has_more_pages:
response = self._request("/statements", params=parameters)
resp_json = response.json()
statements_json = resp_json.get("data", [])
statements.extend(Statement.from_response(self, s) for s in statements_json)
# Check if there are more pages to fetch based on the presence of a 'next' link in the
# response metadata. The 'next' value will be an entire URL, but we just need to extract
# the page token from it for the next request.
next_page_token = self._get_next_page_token(resp_json.get("metadata", {}).get("next"))
if next_page_token:
parameters["page_token"] = next_page_token
has_more_pages = next_page_token is not None
return statements
def get_statement(self, statement: str | Statement) -> Statement:
"""
Get the current state of a statement by name or Statement object.
Retrieves the latest status and metadata for a statement from the server,
including phase (PENDING, RUNNING, COMPLETED, FAILED, etc.), schema,
and execution traits. Useful for polling statement progress or checking
if results are ready to fetch.
Args:
statement: The name of the statement to retrieve, or a Statement object.
If a Statement object is passed, its current state will be
refreshed from the server.
Returns:
A Statement object representing the current state of the statement.
If a Statement object was passed as input, a new Statement object
with updated metadata and status from the server will be returned.
Raises:
TypeError: If statement is neither a string nor a Statement object
StatementNotFoundError: If statement does not exist (HTTP 404)
OperationalError: If other API errors occur
Example:
# Get statement by name
stmt = connection.get_statement("my-statement-name")
print(f"Status: {stmt.phase}") # e.g., "RUNNING" or "COMPLETED"
# Refresh a Statement object's state
stmt = connection.get_statement(stmt)
if stmt.can_fetch_results(ExecutionMode.SNAPSHOT):
# Results are ready
...
"""
if isinstance(statement, Statement):
statement_name = statement.name
else:
if not isinstance(statement, str):
raise TypeError(
"Statement must be specified by name or Statement object, "
f"got {type(statement)}"
)
statement_name = statement
logger.info(f"Getting statement '{statement_name}'")
response_dict = self._get_statement(statement_name)
return Statement.from_response(self, response_dict)
def delete_statement(self, statement: str | Statement) -> None:
"""
Delete a statement by name or Statement object.
In Flink SQL, executed statements (especially streaming ones) create
resources that linger on within CCLoud until explicitly deleted (or
have stopped and enough time has passed for automatic cleanup).
Deleting a RUNNING statement will stop it first.
Args:
statement: The name of the statement to delete, or the Statement object. If passed
a Statement object that is already deleted, the deletion is ignored. However, if
passed a Statement object representing a still running statement, the delete
operation will be performed, causing the statement to be stopped and deleted.
"""
if isinstance(statement, Statement):
if statement.is_deleted:
logger.info(f"Statement {statement.name} is already deleted, ignoring")
return
statement_name = statement.name
else:
if not isinstance(statement, str):
raise TypeError(
"Statement to delete must be specified by name or Statement object, "
f"got {type(statement)}"
)
statement_name = statement
logger.info(f"Deleting statement {statement_name}")
response = self._request(
f"/statements/{statement_name}", method="DELETE", raise_for_status=False
)
try:
response.raise_for_status()
except httpx.HTTPStatusError as e:
if e.response.status_code != 404:
raise OperationalError("Error deleting statement") from e
# If the response is 404, it means we don't need to delete the statement.
logger.info(f"Statement '{statement_name}' not found while deleting, ignoring")
if isinstance(statement, Statement):
# Mark the Statement object as deleted for if the caller still is interested in its
# reference.
statement.set_deleted()
@property
def is_closed(self) -> bool:
"""
Check if the connection is closed.
Returns:
True if the connection is closed, False otherwise
"""
return self._closed
@property
def http_user_agent(self) -> str:
"""
Get the User-Agent header value sent with all HTTP requests.
Returns:
The current User-Agent string
"""
return self._http_user_agent
@http_user_agent.setter
def http_user_agent(self, value: str) -> None:
"""
Set the User-Agent header value for all HTTP requests made by this connection.
The User-Agent identifies the client software making requests to Confluent Cloud.
This is useful for tracking, debugging, and analytics purposes.
Args:
value: The User-Agent string to use. Must be a non-empty string between
1 and 100 characters in length.
Raises:
InterfaceError: If value is not a string, is empty, or exceeds 100 characters
Example:
conn.http_user_agent = "my-app/1.0"
"""
if not isinstance(value, str):
raise InterfaceError(f"http_user_agent must be a string, got {type(value).__name__}")
if len(value) < 1 or len(value) > 100:
raise InterfaceError(
f"http_user_agent length must be between 1 and 100 characters, got {len(value)}"
)
self._http_user_agent = value
# Update the httpx client headers if client is already initialized
if hasattr(self, "_client"):
self._client.headers["User-Agent"] = value
def register_row_type(self, class_for_flink_row: type[RowPythonTypes]) -> None:
"""Register a user-defined namedtuple, NamedTuple, or @dataclass class to be used
to return deserialized ROW values.
The user-provided class to use when deserializing a ROW in any particular resultset is
determined by matching the sequence of ROW field names to the ordered sequence of declared
field names in the user-provided namedtuple, NamedTuple or @dataclass class.
If no user-registered class matches the field names of a ROW type in a resultset,
a new namedtuple class will be created and cached for future use.
"""
self._row_type_registry.register_row_type(class_for_flink_row)
_NEVER_USER_PROVIDED_PROPERTIES = {
"sql.current-catalog",
"sql.current-database",
"sql.snapshot.mode",
}
def _resolve_properties(
self, properties: PropertiesDict | None, execution_mode: ExecutionMode
) -> PropertiesDict:
"""
Validate and merge user properties with system properties.
Validates the properties parameter and merges it with system-level properties
(catalog, database, snapshot mode). System properties always have precedence
and cannot be overridden by user input.
Args:
properties: Optional dictionary of user-provided statement properties.
Keys must be strings, values must be str, int, or bool.
execution_mode: The execution mode (determines if snapshot.mode is set).
Returns:
Merged properties dictionary with system properties overlaid.
Raises:
InterfaceError: If properties parameter is invalid (not a dict, invalid keys/values).
"""
# Validate properties parameter if provided
if properties is not None:
if not isinstance(properties, dict):
raise InterfaceError(f"properties must be a dict, got {type(properties).__name__}")
for key, value in properties.items():
if not isinstance(key, str):
raise InterfaceError(
f"properties keys must be strings, got {type(key).__name__} for key {key!r}"
)
if not isinstance(value, (str, int, bool)):
raise InterfaceError(
f"properties values must be str, int, or bool, "
f"got {type(value).__name__} for key {key!r}"
)
if key in self._NEVER_USER_PROVIDED_PROPERTIES:
raise InterfaceError(f"'{key}' is a reserved system property.")
# Start with user properties (if provided), then overlay system properties
# This ensures system properties always win and cannot be overridden
merged_properties: PropertiesDict = {}
if properties is not None:
# User properties applied first
merged_properties.update(properties)
# Connection-level properties overlay (always set, cannot be overridden by user)
merged_properties["sql.current-catalog"] = self.environment
if self._database is not None:
merged_properties["sql.current-database"] = self._database
# Cursor-level execution mode properties overlay (always set when applicable)
if execution_mode.is_snapshot:
# Ask for snapshot mode behavior -- point-in-time results.
merged_properties["sql.snapshot.mode"] = "now"
return merged_properties
def _execute_statement(
self,
statement: str,
execution_mode: ExecutionMode,
statement_name: str | None = None,
statement_labels: list[str] | None = None,
properties: PropertiesDict | None = None,
*,
compute_pool_id: str | None = None,
) -> dict[str, Any]:
"""
Execute a SQL statement and return the response. Any statement parameters must have already
been incorproated into the statement string, in that the create statement endpoint
does not support separate parameter passing.
Args:
statement: The SQL statement to execute
execution_mode: The execution mode for the statement (snapshot, streaming query, etc.)
statement_name: Optional name for the statement (defaults to 'dbapi-{uuid}')
statement_labels: Optional list of labels for the statement for easier identification
in server logs and UIs (defaults to None). Use HIDDEN_LABEL to mark
statements as hidden from default views.
properties: Optional dictionary of statement properties to set for this execution.
Keys must be strings, values must be str, int, or bool. System
properties (sql.current-catalog, sql.current-database, sql.snapshot.mode)
are always set by the driver and cannot be overridden.
compute_pool_id: Optional compute pool ID to use for this statement execution.
If not provided, uses the Connection's default compute_pool_id.
The compute pool must be accessible to the API key used for the
connection. Validation of compute pool accessibility is performed
by Confluent Cloud Flink.
Returns:
Dictionary containing the API response
Raises:
OperationalError: If statement execution fails
InterfaceError: If properties parameter is invalid, or if statement_labels is invalid,
or if compute_pool_id is not a string when provided.
"""
# Create the statement payload as per Flink SQL API documentation
if statement_name is None:
statement_name = f"dbapi-{str(uuid.uuid4())}"
# Validate compute_pool_id type if provided
if compute_pool_id is not None and not isinstance(compute_pool_id, str):
raise InterfaceError(
f"compute_pool_id must be a string, got {type(compute_pool_id).__name__}"
)
# Resolve and merge user properties with system properties
merged_properties = self._resolve_properties(properties, execution_mode)
# Use provided compute_pool_id or default to Connection's compute_pool_id
if compute_pool_id is not None:
# Just so that the user is well aware that this is happening ...
logger.info(
f"execute_statement(): Overriding connection compute_pool_id"
f" '{self.compute_pool_id}' with provided compute_pool_id '{compute_pool_id}'"
)
resolved_compute_pool_id = compute_pool_id
else:
resolved_compute_pool_id = self.compute_pool_id
payload = {
"name": statement_name,
"organization_id": self.organization_id,
"environment_id": self.environment,
"spec": {
"statement": statement,
"properties": merged_properties,
"compute_pool_id": resolved_compute_pool_id,
"stopped": False,
},
}