duckrun 0.2.8.dev0__py3-none-any.whl → 0.2.9__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.
duckrun/__init__.py CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  from duckrun.core import Duckrun
4
4
 
5
- __version__ = "0.1.0"
5
+ __version__ = "0.2.9.dev5"
6
6
 
7
7
  # Expose unified connect method at module level
8
8
  connect = Duckrun.connect
duckrun/auth.py CHANGED
@@ -123,6 +123,12 @@ def get_fabric_api_token() -> Optional[str]:
123
123
  Returns:
124
124
  Fabric API token string or None if authentication fails
125
125
  """
126
+ # Check if we already have a cached Fabric API token
127
+ fabric_token_env = os.environ.get("FABRIC_API_TOKEN")
128
+ if fabric_token_env:
129
+ print("✅ Using cached Fabric API token")
130
+ return fabric_token_env
131
+
126
132
  print("🔐 Getting Fabric API token...")
127
133
 
128
134
  # Try Fabric notebook environment first
@@ -130,6 +136,7 @@ def get_fabric_api_token() -> Optional[str]:
130
136
  import notebookutils # type: ignore
131
137
  print("📓 Microsoft Fabric notebook detected - using notebookutils")
132
138
  token = notebookutils.credentials.getToken("pbi")
139
+ os.environ["FABRIC_API_TOKEN"] = token
133
140
  print("✅ Fabric API token obtained!")
134
141
  return token
135
142
  except ImportError:
@@ -158,6 +165,7 @@ def get_fabric_api_token() -> Optional[str]:
158
165
  print("🔐 Trying Azure CLI for Fabric API...")
159
166
  credential = AzureCliCredential()
160
167
  token_obj = credential.get_token("https://api.fabric.microsoft.com/.default")
168
+ os.environ["FABRIC_API_TOKEN"] = token_obj.token
161
169
  print("✅ Fabric API token obtained via Azure CLI!")
162
170
  return token_obj.token
163
171
  except Exception as cli_error:
@@ -167,6 +175,7 @@ def get_fabric_api_token() -> Optional[str]:
167
175
  credential = InteractiveBrowserCredential()
168
176
 
169
177
  token_obj = credential.get_token("https://api.fabric.microsoft.com/.default")
178
+ os.environ["FABRIC_API_TOKEN"] = token_obj.token
170
179
  print("✅ Fabric API token obtained!")
171
180
  return token_obj.token
172
181
 
duckrun/core.py CHANGED
@@ -82,6 +82,15 @@ class Duckrun:
82
82
 
83
83
  self.con = duckdb.connect()
84
84
  self.con.sql("SET preserve_insertion_order = false")
85
+
86
+ # Configure Azure transport for Colab (fixes SSL cert issues)
87
+ try:
88
+ import google.colab # type: ignore
89
+ self.con.sql("SET azure_transport_option_type = 'curl'")
90
+ print("🔧 Colab detected - using curl transport for Azure")
91
+ except ImportError:
92
+ pass # Not in Colab, use default transport
93
+
85
94
  self._attach_lakehouse()
86
95
 
87
96
  @classmethod
@@ -687,6 +696,45 @@ class Duckrun:
687
696
  print(f"❌ Error creating lakehouse '{lakehouse_name}': {e}")
688
697
  return False
689
698
 
699
+ def deploy(self, bim_url: str, dataset_name: Optional[str] = None,
700
+ wait_seconds: int = 5) -> int:
701
+ """
702
+ Deploy a semantic model from a BIM file using DirectLake mode.
703
+
704
+ Args:
705
+ bim_url: URL to the BIM file (e.g., GitHub raw URL)
706
+ dataset_name: Name for the semantic model (default: lakehouse_schema)
707
+ wait_seconds: Seconds to wait for permission propagation (default: 5)
708
+
709
+ Returns:
710
+ 1 for success, 0 for failure
711
+
712
+ Examples:
713
+ dr = Duckrun.connect("My Workspace/My Lakehouse.lakehouse/dbo")
714
+
715
+ # Deploy with auto-generated name
716
+ dr.deploy("https://raw.githubusercontent.com/.../model.bim")
717
+
718
+ # Deploy with custom name
719
+ dr.deploy("https://raw.githubusercontent.com/.../model.bim",
720
+ dataset_name="Sales Model")
721
+ """
722
+ from .semantic_model import deploy_semantic_model
723
+
724
+ # Auto-generate dataset name if not provided
725
+ if dataset_name is None:
726
+ dataset_name = f"{self.lakehouse_name}_{self.schema}"
727
+
728
+ # Call the deployment function (DirectLake only)
729
+ return deploy_semantic_model(
730
+ workspace_name_or_id=self.workspace,
731
+ lakehouse_name_or_id=self.lakehouse_name,
732
+ schema_name=self.schema,
733
+ dataset_name=dataset_name,
734
+ bim_url=bim_url,
735
+ wait_seconds=wait_seconds
736
+ )
737
+
690
738
  def _get_workspace_id_by_name(self, token: str, workspace_name: str) -> Optional[str]:
691
739
  """Helper method to get workspace ID from name"""
692
740
  try:
@@ -0,0 +1,434 @@
1
+ """
2
+ Semantic Model Deployer - DirectLake mode for Fabric Lakehouses
3
+ Uses duckrun's authentication. Works anywhere duckrun works.
4
+ """
5
+
6
+ import requests
7
+ import json
8
+ import time
9
+ import base64
10
+
11
+
12
+ class FabricRestClient:
13
+ """Fabric REST API client using duckrun's authentication."""
14
+
15
+ def __init__(self):
16
+ self.base_url = "https://api.fabric.microsoft.com"
17
+ self.token = None
18
+ self._get_token()
19
+
20
+ def _get_token(self):
21
+ """Get Fabric API token using duckrun's auth module"""
22
+ from duckrun.auth import get_fabric_api_token
23
+ self.token = get_fabric_api_token()
24
+ if not self.token:
25
+ raise Exception("Failed to get Fabric API token")
26
+
27
+ def _get_headers(self):
28
+ return {
29
+ "Authorization": f"Bearer {self.token}",
30
+ "Content-Type": "application/json"
31
+ }
32
+
33
+ def get(self, endpoint: str):
34
+ url = f"{self.base_url}{endpoint}"
35
+ response = requests.get(url, headers=self._get_headers())
36
+ response.raise_for_status()
37
+ return response
38
+
39
+ def post(self, endpoint: str, json: dict = None):
40
+ url = f"{self.base_url}{endpoint}"
41
+ response = requests.post(url, headers=self._get_headers(), json=json)
42
+ response.raise_for_status()
43
+ return response
44
+
45
+
46
+ def get_workspace_id(workspace_name_or_id, client):
47
+ """Get workspace ID by name or validate if already a GUID"""
48
+ import re
49
+
50
+ # Check if input is already a GUID
51
+ guid_pattern = re.compile(r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$', re.IGNORECASE)
52
+ if guid_pattern.match(workspace_name_or_id):
53
+ # It's already a GUID, verify it exists
54
+ try:
55
+ response = client.get(f"/v1/workspaces/{workspace_name_or_id}")
56
+ workspace_name = response.json().get('displayName', workspace_name_or_id)
57
+ print(f"✓ Found workspace: {workspace_name}")
58
+ return workspace_name_or_id
59
+ except:
60
+ raise ValueError(f"Workspace with ID '{workspace_name_or_id}' not found")
61
+
62
+ # It's a name, search for it
63
+ response = client.get("/v1/workspaces")
64
+ workspaces = response.json().get('value', [])
65
+
66
+ workspace_match = next((ws for ws in workspaces if ws.get('displayName') == workspace_name_or_id), None)
67
+ if not workspace_match:
68
+ raise ValueError(f"Workspace '{workspace_name_or_id}' not found")
69
+
70
+ workspace_id = workspace_match['id']
71
+ print(f"✓ Found workspace: {workspace_name_or_id}")
72
+ return workspace_id
73
+
74
+
75
+ def get_lakehouse_id(lakehouse_name_or_id, workspace_id, client):
76
+ """Get lakehouse ID by name or validate if already a GUID"""
77
+ import re
78
+
79
+ # Check if input is already a GUID
80
+ guid_pattern = re.compile(r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$', re.IGNORECASE)
81
+ if guid_pattern.match(lakehouse_name_or_id):
82
+ # It's already a GUID, verify it exists
83
+ try:
84
+ response = client.get(f"/v1/workspaces/{workspace_id}/lakehouses")
85
+ items = response.json().get('value', [])
86
+ lakehouse_match = next((item for item in items if item.get('id') == lakehouse_name_or_id), None)
87
+ if lakehouse_match:
88
+ lakehouse_name = lakehouse_match.get('displayName', lakehouse_name_or_id)
89
+ print(f"✓ Found lakehouse: {lakehouse_name}")
90
+ return lakehouse_name_or_id
91
+ else:
92
+ raise ValueError(f"Lakehouse with ID '{lakehouse_name_or_id}' not found")
93
+ except Exception as e:
94
+ raise ValueError(f"Lakehouse with ID '{lakehouse_name_or_id}' not found: {e}")
95
+
96
+ # It's a name, search for it
97
+ response = client.get(f"/v1/workspaces/{workspace_id}/lakehouses")
98
+ items = response.json().get('value', [])
99
+
100
+ lakehouse_match = next((item for item in items if item.get('displayName') == lakehouse_name_or_id), None)
101
+ if not lakehouse_match:
102
+ raise ValueError(f"Lakehouse '{lakehouse_name_or_id}' not found")
103
+
104
+ lakehouse_id = lakehouse_match['id']
105
+ print(f"✓ Found lakehouse: {lakehouse_name_or_id}")
106
+ return lakehouse_id
107
+
108
+
109
+ def get_dataset_id(dataset_name, workspace_id, client):
110
+ """Get dataset ID by name"""
111
+ response = client.get(f"/v1/workspaces/{workspace_id}/semanticModels")
112
+ items = response.json().get('value', [])
113
+
114
+ dataset_match = next((item for item in items if item.get('displayName') == dataset_name), None)
115
+ if not dataset_match:
116
+ raise ValueError(f"Dataset '{dataset_name}' not found")
117
+
118
+ return dataset_match['id']
119
+
120
+
121
+ def check_dataset_exists(dataset_name, workspace_id, client):
122
+ """Check if dataset already exists"""
123
+ try:
124
+ get_dataset_id(dataset_name, workspace_id, client)
125
+ print(f"⚠️ Dataset '{dataset_name}' already exists")
126
+ return True
127
+ except:
128
+ print(f"✓ Dataset name '{dataset_name}' is available")
129
+ return False
130
+
131
+
132
+ def refresh_dataset(dataset_name, workspace_id, client, dataset_id=None):
133
+ """Refresh a dataset and monitor progress using Power BI API"""
134
+
135
+ # If dataset_id not provided, look it up by name
136
+ if not dataset_id:
137
+ dataset_id = get_dataset_id(dataset_name, workspace_id, client)
138
+
139
+ payload = {
140
+ "type": "full",
141
+ "commitMode": "transactional",
142
+ "maxParallelism": 10,
143
+ "retryCount": 2,
144
+ "objects": []
145
+ }
146
+
147
+ # Use Power BI API for refresh (not Fabric API)
148
+ powerbi_url = f"https://api.powerbi.com/v1.0/myorg/datasets/{dataset_id}/refreshes"
149
+ headers = client._get_headers()
150
+
151
+ response = requests.post(powerbi_url, headers=headers, json=payload)
152
+
153
+ if response.status_code in [200, 202]:
154
+ print(f"✓ Refresh initiated")
155
+
156
+ # For 202, get the refresh_id from the Location header
157
+ if response.status_code == 202:
158
+ location = response.headers.get('Location')
159
+ if location:
160
+ refresh_id = location.split('/')[-1]
161
+ print(" Monitoring refresh progress...")
162
+ max_attempts = 60
163
+ for attempt in range(max_attempts):
164
+ time.sleep(5)
165
+
166
+ # Check refresh status using Power BI API
167
+ status_url = f"https://api.powerbi.com/v1.0/myorg/datasets/{dataset_id}/refreshes/{refresh_id}"
168
+ status_response = requests.get(status_url, headers=headers)
169
+ status_response.raise_for_status()
170
+ status = status_response.json().get('status')
171
+
172
+ if status == 'Completed':
173
+ print(f"✓ Refresh completed successfully")
174
+ return
175
+ elif status == 'Failed':
176
+ error = status_response.json().get('serviceExceptionJson', '')
177
+ raise Exception(f"Refresh failed: {error}")
178
+ elif status == 'Cancelled':
179
+ raise Exception("Refresh was cancelled")
180
+
181
+ if attempt % 6 == 0:
182
+ print(f" Status: {status}...")
183
+
184
+ raise Exception(f"Refresh timed out")
185
+ else:
186
+ response.raise_for_status()
187
+
188
+
189
+ def download_bim_from_github(url):
190
+ """Download BIM file from URL"""
191
+ print(f"Downloading BIM file...")
192
+ response = requests.get(url)
193
+ response.raise_for_status()
194
+ bim_content = response.json()
195
+ print(f"✓ BIM file downloaded")
196
+ print(f" - Tables: {len(bim_content.get('model', {}).get('tables', []))}")
197
+ print(f" - Relationships: {len(bim_content.get('model', {}).get('relationships', []))}")
198
+ return bim_content
199
+
200
+
201
+ def update_bim_for_directlake(bim_content, workspace_id, lakehouse_id, schema_name):
202
+ """Update BIM file for DirectLake mode"""
203
+
204
+ new_url = f"https://onelake.dfs.fabric.microsoft.com/{workspace_id}/{lakehouse_id}"
205
+ expression_name = None
206
+
207
+ # Update or create DirectLake expression
208
+ if 'model' in bim_content and 'expressions' in bim_content['model']:
209
+ for expr in bim_content['model']['expressions']:
210
+ if 'DirectLake' in expr['name'] or expr.get('kind') == 'm':
211
+ expression_name = expr['name']
212
+ expr['expression'] = [
213
+ "let",
214
+ f" Source = AzureStorage.DataLake(\"{new_url}\", [HierarchicalNavigation=true])",
215
+ "in",
216
+ " Source"
217
+ ]
218
+ break
219
+
220
+ if not expression_name:
221
+ expression_name = f"DirectLake - {schema_name}"
222
+ if 'expressions' not in bim_content['model']:
223
+ bim_content['model']['expressions'] = []
224
+
225
+ bim_content['model']['expressions'].append({
226
+ "name": expression_name,
227
+ "kind": "m",
228
+ "expression": [
229
+ "let",
230
+ f" Source = AzureStorage.DataLake(\"{new_url}\", [HierarchicalNavigation=true])",
231
+ "in",
232
+ " Source"
233
+ ],
234
+ "lineageTag": f"directlake-{schema_name}-source"
235
+ })
236
+
237
+ # Update table partitions for DirectLake
238
+ if 'tables' in bim_content['model']:
239
+ for table in bim_content['model']['tables']:
240
+ if 'partitions' in table:
241
+ for partition in table['partitions']:
242
+ if 'source' in partition:
243
+ partition['mode'] = 'directLake'
244
+ partition['source'] = {
245
+ "type": "entity",
246
+ "entityName": partition['source'].get('entityName', table['name']),
247
+ "expressionSource": expression_name,
248
+ "schemaName": schema_name
249
+ }
250
+
251
+ print(f"✓ Updated BIM for DirectLake")
252
+ print(f" - OneLake URL: {new_url}")
253
+ print(f" - Schema: {schema_name}")
254
+
255
+ return bim_content
256
+
257
+
258
+ def create_dataset_from_bim(dataset_name, bim_content, workspace_id, client):
259
+ """Create semantic model from BIM using Fabric REST API and return the dataset ID"""
260
+ # Convert to base64
261
+ bim_json = json.dumps(bim_content, indent=2)
262
+ bim_base64 = base64.b64encode(bim_json.encode('utf-8')).decode('utf-8')
263
+
264
+ pbism_content = {"version": "1.0"}
265
+ pbism_json = json.dumps(pbism_content)
266
+ pbism_base64 = base64.b64encode(pbism_json.encode('utf-8')).decode('utf-8')
267
+
268
+ payload = {
269
+ "displayName": dataset_name,
270
+ "definition": {
271
+ "parts": [
272
+ {
273
+ "path": "model.bim",
274
+ "payload": bim_base64,
275
+ "payloadType": "InlineBase64"
276
+ },
277
+ {
278
+ "path": "definition.pbism",
279
+ "payload": pbism_base64,
280
+ "payloadType": "InlineBase64"
281
+ }
282
+ ]
283
+ }
284
+ }
285
+
286
+ response = client.post(
287
+ f"/v1/workspaces/{workspace_id}/semanticModels",
288
+ json=payload
289
+ )
290
+
291
+ print(f"✓ Semantic model created")
292
+
293
+ # Handle long-running operation and return the dataset ID
294
+ if response.status_code == 202:
295
+ operation_id = response.headers.get('x-ms-operation-id')
296
+ print(f" Waiting for operation to complete...")
297
+
298
+ max_attempts = 30
299
+ for attempt in range(max_attempts):
300
+ time.sleep(2)
301
+
302
+ # Get operation result (not just status)
303
+ result_response = client.get(f"/v1/operations/{operation_id}/result")
304
+
305
+ # Check if operation is complete by getting the status
306
+ status_response = client.get(f"/v1/operations/{operation_id}")
307
+ status = status_response.json().get('status')
308
+
309
+ if status == 'Succeeded':
310
+ print(f"✓ Operation completed")
311
+ # Return the created dataset ID from the result
312
+ result_data = result_response.json()
313
+ dataset_id = result_data.get('id')
314
+ if dataset_id:
315
+ return dataset_id
316
+ else:
317
+ # Fallback: search for the dataset by name
318
+ return get_dataset_id(dataset_name, workspace_id, client)
319
+ elif status == 'Failed':
320
+ error = status_response.json().get('error', {})
321
+ raise Exception(f"Operation failed: {error.get('message')}")
322
+ elif attempt == max_attempts - 1:
323
+ raise Exception(f"Operation timed out")
324
+
325
+ # For non-async responses (status 200/201)
326
+ result_data = response.json()
327
+ dataset_id = result_data.get('id')
328
+ if dataset_id:
329
+ return dataset_id
330
+ else:
331
+ # Fallback: search for the dataset by name
332
+ return get_dataset_id(dataset_name, workspace_id, client)
333
+
334
+
335
+ def deploy_semantic_model(workspace_name_or_id, lakehouse_name_or_id, schema_name, dataset_name,
336
+ bim_url, wait_seconds=5):
337
+ """
338
+ Deploy a semantic model using DirectLake mode.
339
+
340
+ Args:
341
+ workspace_name_or_id: Name or GUID of the target workspace
342
+ lakehouse_name_or_id: Name or GUID of the lakehouse
343
+ schema_name: Schema name (e.g., 'dbo', 'staging')
344
+ dataset_name: Name for the semantic model
345
+ bim_url: URL to the BIM file
346
+ wait_seconds: Seconds to wait before refresh (default: 5)
347
+
348
+ Returns:
349
+ 1 for success, 0 for failure
350
+
351
+ Examples:
352
+ dr = Duckrun.connect("My Workspace/My Lakehouse.lakehouse/dbo")
353
+ dr.deploy("https://raw.githubusercontent.com/.../model.bim")
354
+ """
355
+ print("=" * 70)
356
+ print("Semantic Model Deployment (DirectLake)")
357
+ print("=" * 70)
358
+
359
+ client = FabricRestClient()
360
+
361
+ try:
362
+ # Step 1: Get workspace ID
363
+ print("\n[Step 1/6] Getting workspace information...")
364
+ workspace_id = get_workspace_id(workspace_name_or_id, client)
365
+
366
+ # Step 2: Check if dataset exists
367
+ print(f"\n[Step 2/6] Checking if dataset '{dataset_name}' exists...")
368
+ dataset_exists = check_dataset_exists(dataset_name, workspace_id, client)
369
+
370
+ if dataset_exists:
371
+ print(f"\n✓ Dataset exists - refreshing...")
372
+
373
+ if wait_seconds > 0:
374
+ print(f" Waiting {wait_seconds} seconds...")
375
+ time.sleep(wait_seconds)
376
+
377
+ print("\n[Step 6/6] Refreshing semantic model...")
378
+ refresh_dataset(dataset_name, workspace_id, client)
379
+
380
+ print("\n" + "=" * 70)
381
+ print("🎉 Refresh Completed!")
382
+ print("=" * 70)
383
+ print(f"Dataset: {dataset_name}")
384
+ print("=" * 70)
385
+ return 1
386
+
387
+ # Step 3: Get lakehouse ID
388
+ print(f"\n[Step 3/6] Finding lakehouse...")
389
+ lakehouse_id = get_lakehouse_id(lakehouse_name_or_id, workspace_id, client)
390
+
391
+ # Step 4: Download and update BIM
392
+ print("\n[Step 4/6] Downloading and configuring BIM file...")
393
+ bim_content = download_bim_from_github(bim_url)
394
+
395
+ modified_bim = update_bim_for_directlake(bim_content, workspace_id, lakehouse_id, schema_name)
396
+ modified_bim['name'] = dataset_name
397
+ modified_bim['id'] = dataset_name
398
+
399
+ # Step 5: Deploy and get the dataset ID
400
+ print("\n[Step 5/6] Deploying semantic model...")
401
+ dataset_id = create_dataset_from_bim(dataset_name, modified_bim, workspace_id, client)
402
+ print(f" Dataset ID: {dataset_id}")
403
+
404
+ if wait_seconds > 0:
405
+ print(f" Waiting {wait_seconds} seconds before refresh...")
406
+ time.sleep(wait_seconds)
407
+
408
+ # Step 6: Refresh using the dataset ID returned from creation
409
+ print("\n[Step 6/6] Refreshing semantic model...")
410
+ refresh_dataset(dataset_name, workspace_id, client, dataset_id=dataset_id)
411
+
412
+ print("\n" + "=" * 70)
413
+ print("🎉 Deployment Completed!")
414
+ print("=" * 70)
415
+ print(f"Dataset: {dataset_name}")
416
+ print(f"Workspace: {workspace_name_or_id}")
417
+ print(f"Lakehouse: {lakehouse_name_or_id}")
418
+ print(f"Schema: {schema_name}")
419
+ print("=" * 70)
420
+
421
+ return 1
422
+
423
+ except Exception as e:
424
+ print("\n" + "=" * 70)
425
+ print("❌ Deployment Failed")
426
+ print("=" * 70)
427
+ print(f"Error: {str(e)}")
428
+ print("\n💡 Troubleshooting:")
429
+ print(f" - Verify workspace '{workspace_name_or_id}' exists")
430
+ print(f" - Verify lakehouse '{lakehouse_name_or_id}' exists")
431
+ print(f" - Ensure tables exist in '{schema_name}' schema")
432
+ print(f" - Check tables are in Delta format")
433
+ print("=" * 70)
434
+ return 0
duckrun/stats.py CHANGED
@@ -147,7 +147,20 @@ def get_stats(duckrun_instance, source: str):
147
147
 
148
148
  try:
149
149
  dt = DeltaTable(table_path)
150
- xx = dt.get_add_actions(flatten=True).to_pydict()
150
+ add_actions = dt.get_add_actions(flatten=True)
151
+
152
+ # Convert to dict - compatible with both old and new deltalake versions
153
+ # Try to_pydict() first (old versions), fall back to to_pylist() (new versions)
154
+ try:
155
+ xx = add_actions.to_pydict()
156
+ except AttributeError:
157
+ # New version with arro3: use to_pylist() and convert to dict of lists
158
+ records = add_actions.to_pylist()
159
+ if records:
160
+ # Convert list of dicts to dict of lists
161
+ xx = {key: [record[key] for record in records] for key in records[0].keys()}
162
+ else:
163
+ xx = {}
151
164
 
152
165
  # Check if VORDER exists
153
166
  vorder = 'tags.VORDER' in xx.keys()
duckrun/writer.py CHANGED
@@ -1,18 +1,36 @@
1
1
  """
2
2
  Delta Lake writer functionality for duckrun - Spark-style write API
3
3
  """
4
- from deltalake import DeltaTable, write_deltalake
4
+ from deltalake import DeltaTable, write_deltalake, __version__ as deltalake_version
5
5
 
6
6
 
7
7
  # Row Group configuration for optimal Delta Lake performance
8
8
  RG = 8_000_000
9
9
 
10
+ # Check deltalake version once at module load
11
+ # Version 0.18.x and 0.19.x support engine parameter and row group optimization
12
+ # Version 0.20+ removed these features (rust only, no row groups)
13
+ _DELTALAKE_VERSION = tuple(map(int, deltalake_version.split('.')[:2]))
14
+ _IS_OLD_DELTALAKE = _DELTALAKE_VERSION < (0, 20)
15
+
10
16
 
11
17
  def _build_write_deltalake_args(path, df, mode, schema_mode=None, partition_by=None):
12
18
  """
13
- Build arguments for write_deltalake based on requirements:
14
- - If schema_mode='merge': use rust engine (no row group params)
15
- - Otherwise: use pyarrow engine with row group optimization (if supported)
19
+ Build arguments for write_deltalake based on requirements and version:
20
+
21
+ deltalake 0.18.2 - 0.19.x:
22
+ - Has 'engine' parameter (defaults to 'pyarrow')
23
+ - Has max_rows_per_file/max_rows_per_group/min_rows_per_group for optimization
24
+ - When mergeSchema=True: must set schema_mode='merge' + engine='rust', NO row group params
25
+ - When mergeSchema=False: use row group params, DON'T set engine (pyarrow is default)
26
+
27
+ deltalake 0.20+:
28
+ - Does NOT have 'engine' parameter (everything is rust, pyarrow deprecated)
29
+ - Does NOT have max_rows_per_file (row group optimization removed)
30
+ - When mergeSchema=True: must set schema_mode='merge'
31
+ - When mergeSchema=False: just write normally (no special params)
32
+
33
+ Uses version detection for simpler logic.
16
34
  """
17
35
  args = {
18
36
  'table_or_uri': path,
@@ -24,23 +42,24 @@ def _build_write_deltalake_args(path, df, mode, schema_mode=None, partition_by=N
24
42
  if partition_by:
25
43
  args['partition_by'] = partition_by
26
44
 
27
- # Engine selection based on schema_mode
28
45
  if schema_mode == 'merge':
29
- # Use rust engine for schema merging (no row group params supported)
46
+ # Schema merging mode - must explicitly set schema_mode='merge'
30
47
  args['schema_mode'] = 'merge'
31
- args['engine'] = 'rust'
32
- else:
33
- # Try to use pyarrow engine with row group optimization
34
- # Check if row group parameters are supported by inspecting function signature
35
- import inspect
36
- sig = inspect.signature(write_deltalake)
37
48
 
38
- if 'max_rows_per_file' in sig.parameters:
39
- # Older deltalake version - use row group optimization
49
+ if _IS_OLD_DELTALAKE:
50
+ # deltalake 0.18.2-0.19.x: must also set engine='rust' for schema merging
51
+ # Do NOT use row group params (they conflict with rust engine)
52
+ args['engine'] = 'rust'
53
+ # For version 0.20+: just schema_mode='merge' is enough, rust is default
54
+ else:
55
+ # Normal write mode (no schema merging)
56
+ if _IS_OLD_DELTALAKE:
57
+ # deltalake 0.18.2-0.19.x: use row group optimization
58
+ # DON'T set engine parameter - pyarrow is the default and works with row groups
40
59
  args['max_rows_per_file'] = RG
41
60
  args['max_rows_per_group'] = RG
42
61
  args['min_rows_per_group'] = RG
43
- # For newer versions, just use default parameters
62
+ # For version 0.20+: no optimization available (rust by default, no row group params supported)
44
63
 
45
64
  return args
46
65
 
@@ -113,7 +132,18 @@ class DeltaWriter:
113
132
  partition_by=self._partition_by
114
133
  )
115
134
 
116
- engine_info = f" (engine=rust, schema_mode=merge)" if self._schema_mode == 'merge' else " (engine=pyarrow)"
135
+ # Prepare info message based on version and settings
136
+ if self._schema_mode == 'merge':
137
+ if _IS_OLD_DELTALAKE:
138
+ engine_info = " (engine=rust, schema_mode=merge)"
139
+ else:
140
+ engine_info = " (schema_mode=merge, rust by default)"
141
+ else:
142
+ if _IS_OLD_DELTALAKE:
143
+ engine_info = " (engine=pyarrow, optimized row groups)"
144
+ else:
145
+ engine_info = " (engine=rust by default)"
146
+
117
147
  partition_info = f" partitioned by {self._partition_by}" if self._partition_by else ""
118
148
  print(f"Writing to Delta table: {schema}.{table} (mode={self._mode}){engine_info}{partition_info}")
119
149
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: duckrun
3
- Version: 0.2.8.dev0
3
+ Version: 0.2.9
4
4
  Summary: Lakehouse task runner powered by DuckDB for Microsoft Fabric
5
5
  Author: mim
6
6
  License: MIT
@@ -420,6 +420,37 @@ success = con.run(pipeline) # Returns True only if ALL tasks succeed
420
420
 
421
421
  This prevents downstream tasks from processing incomplete or corrupted data.
422
422
 
423
+ ### Semantic Model Deployment
424
+
425
+ Deploy Power BI semantic models directly from BIM files using DirectLake mode:
426
+
427
+ ```python
428
+ # Connect to lakehouse
429
+ con = duckrun.connect("Analytics/Sales.lakehouse/dbo")
430
+
431
+ # Deploy with auto-generated name (lakehouse_schema)
432
+ con.deploy("https://raw.githubusercontent.com/user/repo/main/model.bim")
433
+
434
+ # Deploy with custom name
435
+ con.deploy(
436
+ "https://raw.githubusercontent.com/user/repo/main/sales_model.bim",
437
+ dataset_name="Sales Analytics Model",
438
+ wait_seconds=10 # Wait for permission propagation
439
+ )
440
+ ```
441
+
442
+ **Features:**
443
+ - 🚀 **DirectLake Mode**: Deploys semantic models with DirectLake connection
444
+ - 🔄 **Automatic Configuration**: Auto-configures workspace, lakehouse, and schema connections
445
+ - 📦 **BIM from URL**: Load model definitions from GitHub or any accessible URL
446
+ - ⏱️ **Permission Handling**: Configurable wait time for permission propagation
447
+
448
+ **Use Cases:**
449
+ - Deploy semantic models as part of CI/CD pipelines
450
+ - Version control your semantic models in Git
451
+ - Automated model deployment across environments
452
+ - Streamline DirectLake model creation
453
+
423
454
  ### Delta Lake Optimization
424
455
 
425
456
  Duckrun automatically:
@@ -534,6 +565,12 @@ con.sql("""
534
565
 
535
566
  # 5. Download processed files for external systems
536
567
  con.download("processed_reports", "./exports", ['.csv'])
568
+
569
+ # 6. Deploy semantic model for Power BI
570
+ con.deploy(
571
+ "https://raw.githubusercontent.com/user/repo/main/sales_model.bim",
572
+ dataset_name="Sales Analytics"
573
+ )
537
574
  ```
538
575
 
539
576
  **This example demonstrates:**
@@ -541,8 +578,9 @@ con.download("processed_reports", "./exports", ['.csv'])
541
578
  - 🔄 **Pipeline orchestration** with SQL and Python tasks
542
579
  - ⚡ **Fast data exploration** with DuckDB
543
580
  - 💾 **Delta table creation** with Spark-style API
544
- - **Schema evolution** and partitioning
545
- - �📤 **File downloads** from OneLake Files
581
+ - 🔀 **Schema evolution** and partitioning
582
+ - 📤 **File downloads** from OneLake Files
583
+ - 📊 **Semantic model deployment** with DirectLake
546
584
 
547
585
  ## Schema Evolution & Partitioning Guide
548
586
 
@@ -0,0 +1,14 @@
1
+ duckrun/__init__.py,sha256=cTj6KQ6hKmgu1z7k9nhDcO5lct049luxjx1V0QnymCo,235
2
+ duckrun/auth.py,sha256=qPaLQ7InlV9leA9r6E6VEeYavFFoBi0zSN8m_l1aoQs,9545
3
+ duckrun/core.py,sha256=CrWMgA1QHvVF2AAlTlBlQ7VfKsuakcqZa4VuX2WJmik,39279
4
+ duckrun/files.py,sha256=Fvdjg3DyHJzIVzKo8M_j-eGz4zU61lOB38Y_onbQJkI,10137
5
+ duckrun/lakehouse.py,sha256=j--Z3zo8AOWt1GF9VzRosmmTAy6ey2D0LVubti58twU,14109
6
+ duckrun/runner.py,sha256=yrDxfy1RVkb8iK9GKGmIFZHzCvcO_0GVQlbng7Vw_iM,14171
7
+ duckrun/semantic_model.py,sha256=jmTrS15WmhU3rQfdpLII1wm3EORdQfqQxOhqOSyXB_w,17305
8
+ duckrun/stats.py,sha256=CXfb2DWF3PgOckelJooU0y-BAsNT9NFDfDYEmo0mUQQ,10473
9
+ duckrun/writer.py,sha256=svUuPCYOhrz299NgnpTKhARKjfej0PxnoND2iPDSypk,8098
10
+ duckrun-0.2.9.dist-info/licenses/LICENSE,sha256=-DeQQwdbCbkB4507ZF3QbocysB-EIjDtaLexvqRkGZc,1083
11
+ duckrun-0.2.9.dist-info/METADATA,sha256=T4hEXLJELzqhPWDJtez42co8bNbaNgAabywoxFW0hC4,20623
12
+ duckrun-0.2.9.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
13
+ duckrun-0.2.9.dist-info/top_level.txt,sha256=BknMEwebbUHrVAp3SC92ps8MPhK7XSYsaogTvi_DmEU,8
14
+ duckrun-0.2.9.dist-info/RECORD,,
@@ -1,13 +0,0 @@
1
- duckrun/__init__.py,sha256=XA85pL2vK1AkmBic8e7WxeqNvcd6SjFX4zsQpImDO6E,230
2
- duckrun/auth.py,sha256=qColLkvmk8S_qRAXLMGh_TgVeSPkv0j15dv55wgrX1o,9139
3
- duckrun/core.py,sha256=Ad7MgsWlEgW-qWddfjLsp72YvNxk_VmSC8_Q0qBQzpo,37335
4
- duckrun/files.py,sha256=Fvdjg3DyHJzIVzKo8M_j-eGz4zU61lOB38Y_onbQJkI,10137
5
- duckrun/lakehouse.py,sha256=j--Z3zo8AOWt1GF9VzRosmmTAy6ey2D0LVubti58twU,14109
6
- duckrun/runner.py,sha256=yrDxfy1RVkb8iK9GKGmIFZHzCvcO_0GVQlbng7Vw_iM,14171
7
- duckrun/stats.py,sha256=2FTqoQNVjD84-H1HjStHxZkOpAGKXS79M55B00pOlok,9804
8
- duckrun/writer.py,sha256=3UwuoH4yjcomBaTbRXOSjlA82jRhhjErkOWDCX7K7mw,6595
9
- duckrun-0.2.8.dev0.dist-info/licenses/LICENSE,sha256=-DeQQwdbCbkB4507ZF3QbocysB-EIjDtaLexvqRkGZc,1083
10
- duckrun-0.2.8.dev0.dist-info/METADATA,sha256=nr-rrHcmW7R2aNN2pVAsn8VF-4U-mBo30vhrdlaSYvE,19277
11
- duckrun-0.2.8.dev0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
12
- duckrun-0.2.8.dev0.dist-info/top_level.txt,sha256=BknMEwebbUHrVAp3SC92ps8MPhK7XSYsaogTvi_DmEU,8
13
- duckrun-0.2.8.dev0.dist-info/RECORD,,