Skip to content

Commit 3da41b6

Browse files
committed
rely on config for name source
1 parent 6df6d83 commit 3da41b6

7 files changed

Lines changed: 66 additions & 50 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
New methods let an entry point process a Data Lake Object's Change Data Feed continuously instead of reading a bounded snapshot:
1010

11-
- `read_dlo_deltas(name)` / `read_dmo_deltas(name)` – return a streaming DataFrame over the object's change feed.
11+
- `read_dlo_deltas()` / `read_dmo_deltas()` – return a streaming DataFrame over the object's change feed.
1212
- `write_dlo_deltas(name, dataframe)` – start a streaming query that writes each micro-batch to the target DLO and return the `StreamingQuery` handle.
1313

1414
```python
15-
deltas = client.read_dlo_deltas("Input__dll")
15+
deltas = client.read_dlo_deltas()
1616
transformed = deltas.withColumn("description__c", upper(col("description__c")))
1717
query = client.write_dlo_deltas("Output__dll", transformed)
1818
query.awaitTermination()

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ You should only need the following methods:
155155
* `write_to_dmo(name, spark_dataframe, write_mode)` – Write to a Data Lake Object by name with a Spark dataframe
156156

157157
For streaming (delta) transforms, the streaming counterparts are:
158-
* `read_dlo_deltas(name)` – Read the streaming change feed (deltas) of a Data Lake Object as a streaming DataFrame
159-
* `read_dmo_deltas(name)` – Read the streaming change feed (deltas) of a Data Model Object as a streaming DataFrame
158+
* `read_dlo_deltas()` – Read the streaming change feed (deltas) of a Data Lake Object as a streaming DataFrame.
159+
* `read_dmo_deltas()` – Read the streaming change feed (deltas) of a Data Model Object as a streaming DataFrame.
160160
* `write_dlo_deltas(name, spark_dataframe)` – Write a streaming DataFrame of deltas to a Data Lake Object; returns the started `StreamingQuery`
161161

162162
For example:
@@ -186,7 +186,8 @@ from datacustomcode import Client
186186
client = Client()
187187

188188
# read_dlo_deltas returns a *streaming* DataFrame over the change feed.
189-
deltas = client.read_dlo_deltas("Input__dll")
189+
# The runtime resolves the single streaming source, so no name is passed.
190+
deltas = client.read_dlo_deltas()
190191

191192
# Ordinary PySpark transform.
192193
transformed = deltas.withColumn("description__c", upper(col("description__c")))

src/datacustomcode/client.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from __future__ import annotations
1616

1717
from enum import Enum
18+
import os
1819
from typing import (
1920
TYPE_CHECKING,
2021
Any,
@@ -45,6 +46,15 @@
4546
from datacustomcode.spark.base import BaseSparkSessionProvider
4647

4748

49+
_STREAMING_SOURCE_ENV = "BYOC_STREAMING_SOURCE_NAME"
50+
_STREAMING_SOURCE_FALLBACK = "<streaming delta source>"
51+
52+
53+
def _streaming_source_name() -> str:
54+
"""Return the runtime streaming source name, or a readable fallback."""
55+
return os.environ.get(_STREAMING_SOURCE_ENV, _STREAMING_SOURCE_FALLBACK)
56+
57+
4858
def _build_spark_llm_gateway() -> "SparkLLMGateway":
4959
"""Instantiate the SDK-configured :class:`SparkLLMGateway`.
5060
@@ -336,7 +346,7 @@ def read_dmo(self, name: str) -> PySparkDataFrame:
336346
self._record_dmo_access(name)
337347
return self._reader.read_dmo(name) # type: ignore[no-any-return]
338348

339-
def read_dlo_deltas(self, name: str) -> PySparkDataFrame:
349+
def read_dlo_deltas(self) -> PySparkDataFrame:
340350
"""Read the streaming change feed (deltas) for a DLO from Data Cloud.
341351
342352
Streaming counterpart to :meth:`read_dlo`, for use in a streaming
@@ -345,29 +355,24 @@ def read_dlo_deltas(self, name: str) -> PySparkDataFrame:
345355
``_commit_*``) alongside the source columns. Pair with
346356
:meth:`write_dlo_deltas` to write the transformed stream back to a DLO.
347357
348-
Args:
349-
name: The name of the DLO to read deltas from.
350-
351358
Returns:
352359
A streaming PySpark DataFrame over the DLO change feed.
353360
"""
354-
self._record_dlo_access(name)
355-
return self._reader.read_dlo_deltas(name) # type: ignore[no-any-return]
361+
self._record_dlo_access(_streaming_source_name())
362+
return self._reader.read_dlo_deltas() # type: ignore[no-any-return]
356363

357-
def read_dmo_deltas(self, name: str) -> PySparkDataFrame:
364+
def read_dmo_deltas(self) -> PySparkDataFrame:
358365
"""Read the streaming change feed (deltas) for a DMO from Data Cloud.
359366
360367
Streaming counterpart to :meth:`read_dmo`. See :meth:`read_dlo_deltas`
361-
for the shape of the returned change feed.
362-
363-
Args:
364-
name: The name of the DMO to read deltas from.
368+
for the shape of the returned change feed and why no source name is
369+
passed.
365370
366371
Returns:
367372
A streaming PySpark DataFrame over the DMO change feed.
368373
"""
369-
self._record_dmo_access(name)
370-
return self._reader.read_dmo_deltas(name) # type: ignore[no-any-return]
374+
self._record_dmo_access(_streaming_source_name())
375+
return self._reader.read_dmo_deltas() # type: ignore[no-any-return]
371376

372377
def write_to_dlo(
373378
self, name: str, dataframe: PySparkDataFrame, write_mode: WriteMode, **kwargs

src/datacustomcode/io/reader/base.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ def read_dmo(
4242
schema: Union[AtomicType, StructType, str, None] = None,
4343
) -> PySparkDataFrame: ...
4444

45-
def read_dlo_deltas(
46-
self,
47-
name: str,
48-
schema: Union[AtomicType, StructType, str, None] = None,
49-
) -> PySparkDataFrame:
45+
def read_dlo_deltas(self) -> PySparkDataFrame:
5046
"""Read the streaming change feed (deltas) for a Data Lake Object.
5147
5248
This is the streaming counterpart to :meth:`read_dlo`. It returns a
@@ -56,11 +52,6 @@ def read_dlo_deltas(
5652
base implementation raises :class:`NotImplementedError` so local
5753
readers that do not support streaming fail clearly.
5854
59-
Args:
60-
name: Data Lake Object name.
61-
schema: Accepted for parity with :meth:`read_dlo`; implementations
62-
may ignore it.
63-
6455
Returns:
6556
A streaming PySpark DataFrame over the DLO change feed.
6657
@@ -74,21 +65,12 @@ def read_dlo_deltas(
7465
"deltas."
7566
)
7667

77-
def read_dmo_deltas(
78-
self,
79-
name: str,
80-
schema: Union[AtomicType, StructType, str, None] = None,
81-
) -> PySparkDataFrame:
68+
def read_dmo_deltas(self) -> PySparkDataFrame:
8269
"""Read the streaming change feed (deltas) for a Data Model Object.
8370
8471
Streaming counterpart to :meth:`read_dmo`. See :meth:`read_dlo_deltas`
8572
for behavior and the local-development caveat.
8673
87-
Args:
88-
name: Data Model Object name.
89-
schema: Accepted for parity with :meth:`read_dmo`; implementations
90-
may ignore it.
91-
9274
Returns:
9375
A streaming PySpark DataFrame over the DMO change feed.
9476

src/datacustomcode/templates/script/examples/streaming_deltas/entrypoint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
of ``read_dlo`` / ``write_to_dlo`` (which read and write a bounded snapshot), it
55
uses the streaming delta methods:
66
7-
* ``client.read_dlo_deltas(name)`` returns a *streaming* DataFrame over the
7+
* ``client.read_dlo_deltas()`` returns a *streaming* DataFrame over the
88
Change Data Feed of the source DLO. Each row carries the source columns plus
99
change-feed metadata columns (``_record_type``, ``_commit_*``).
1010
* ``client.write_dlo_deltas(name, df)`` starts a streaming query that writes
@@ -30,7 +30,7 @@ def main():
3030
client = Client()
3131

3232
# Streaming DataFrame over the source DLO's change feed.
33-
deltas = client.read_dlo_deltas("Account_std__dll")
33+
deltas = client.read_dlo_deltas()
3434

3535
# Ordinary PySpark transform.
3636
transformed = deltas.withColumn("description__c", upper(col("description__c")))

tests/io/reader/test_query_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,13 @@ def test_read_dlo(
227227
def test_read_dlo_deltas_not_supported_locally(self, reader_without_init):
228228
"""Streaming delta reads are not supported by the local reader."""
229229
with pytest.raises(NotImplementedError) as exc_info:
230-
reader_without_init.read_dlo_deltas("test_dlo")
230+
reader_without_init.read_dlo_deltas()
231231
assert "read_dlo_deltas" in str(exc_info.value)
232232

233233
def test_read_dmo_deltas_not_supported_locally(self, reader_without_init):
234234
"""Streaming delta reads are not supported by the local reader."""
235235
with pytest.raises(NotImplementedError) as exc_info:
236-
reader_without_init.read_dmo_deltas("test_dmo")
236+
reader_without_init.read_dmo_deltas()
237237
assert "read_dmo_deltas" in str(exc_info.value)
238238

239239
def test_read_dlo_with_schema(

tests/test_client.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import os
34
from unittest.mock import MagicMock, patch
45

56
from pyspark.sql import DataFrame, SparkSession
@@ -200,11 +201,32 @@ def test_read_dlo_deltas(self, reset_client, mock_spark):
200201
reader.read_dlo_deltas.return_value = mock_df
201202

202203
client = Client(reader=reader, writer=writer)
203-
result = client.read_dlo_deltas("test_dlo")
204+
with patch.dict("os.environ", {}, clear=False):
205+
os.environ.pop("BYOC_STREAMING_SOURCE_NAME", None)
206+
result = client.read_dlo_deltas()
204207

205-
reader.read_dlo_deltas.assert_called_once_with("test_dlo")
208+
reader.read_dlo_deltas.assert_called_once_with()
206209
assert result is mock_df
207-
assert "test_dlo" in client._data_layer_history[DataCloudObjectType.DLO]
210+
assert (
211+
"<streaming delta source>"
212+
in client._data_layer_history[DataCloudObjectType.DLO]
213+
)
214+
215+
def test_read_dlo_deltas_records_runtime_source_name(
216+
self, reset_client, mock_spark
217+
):
218+
"""The runtime source env var populates the access-history entry."""
219+
reader = MagicMock(spec=BaseDataCloudReader)
220+
writer = MagicMock(spec=BaseDataCloudWriter)
221+
reader.read_dlo_deltas.return_value = MagicMock(spec=DataFrame)
222+
223+
client = Client(reader=reader, writer=writer)
224+
with patch.dict(
225+
"os.environ", {"BYOC_STREAMING_SOURCE_NAME": "Account_std__dll"}
226+
):
227+
client.read_dlo_deltas()
228+
229+
assert "Account_std__dll" in client._data_layer_history[DataCloudObjectType.DLO]
208230

209231
def test_read_dmo_deltas(self, reset_client, mock_spark):
210232
reader = MagicMock(spec=BaseDataCloudReader)
@@ -213,11 +235,16 @@ def test_read_dmo_deltas(self, reset_client, mock_spark):
213235
reader.read_dmo_deltas.return_value = mock_df
214236

215237
client = Client(reader=reader, writer=writer)
216-
result = client.read_dmo_deltas("test_dmo")
238+
with patch.dict(
239+
"os.environ", {"BYOC_STREAMING_SOURCE_NAME": "Account_model__dlm"}
240+
):
241+
result = client.read_dmo_deltas()
217242

218-
reader.read_dmo_deltas.assert_called_once_with("test_dmo")
243+
reader.read_dmo_deltas.assert_called_once_with()
219244
assert result is mock_df
220-
assert "test_dmo" in client._data_layer_history[DataCloudObjectType.DMO]
245+
assert (
246+
"Account_model__dlm" in client._data_layer_history[DataCloudObjectType.DMO]
247+
)
221248

222249
def test_write_dlo_deltas(self, reset_client, mock_spark):
223250
reader = MagicMock(spec=BaseDataCloudReader)
@@ -262,10 +289,11 @@ def test_streaming_read_write_flow(self, reset_client, mock_spark):
262289

263290
client = Client(reader=reader, writer=writer)
264291

265-
df = client.read_dlo_deltas("source_dll")
292+
with patch.dict("os.environ", {"BYOC_STREAMING_SOURCE_NAME": "source_dll"}):
293+
df = client.read_dlo_deltas()
266294
client.write_dlo_deltas("target_dll", df)
267295

268-
reader.read_dlo_deltas.assert_called_once_with("source_dll")
296+
reader.read_dlo_deltas.assert_called_once_with()
269297
writer.write_dlo_deltas.assert_called_once_with("target_dll", stream_df)
270298
assert "source_dll" in client._data_layer_history[DataCloudObjectType.DLO]
271299

0 commit comments

Comments
 (0)