Skip to content

Commit

Permalink
Use gist embed instead
Browse files Browse the repository at this point in the history
  • Loading branch information
mlonsk committed Aug 22, 2024
1 parent b82f0d4 commit 8c83d47
Showing 1 changed file with 2 additions and 70 deletions.
72 changes: 2 additions & 70 deletions common/Datasets/direct-lake-dataset.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,73 +61,5 @@ Running the script is possible when Tabular Editor is connected to a semantic mo
> [!NOTE]
> After deploying the newly converted Import-mode model, you will need to specify the credentials for accessing the Lakehouse to refresh data into the model.
### Convert Direct Lake-mode model to Import-mode
```csharp
// **********************************************************************************
// Convert Direct Lake-mode model to Import-mode
// ---------------------------------------------
//
// When this script is executed on a semantic model, it will:
//
// - Loop through all tables. Any table that contains exactly 1 partition, which
// is in Direct Lake mode, will have its partition replaced by an equivalent
// Import-mode partition.
// - Set the collation of the model to null (default)
//
// Remarks:
//
// - The Import-mode partitions will use the SQL endpoint of the Lakehouse.
// - The script assumes that the Shared Expression which specifies the SQL endpoint
// is called "DatabaseQuery".
// - Because TE2 does not expose the "SchemaName" property on EntityPartition
// objects, we have to use reflection to access the underlying TOM objects.
//
// Compatibility:
// TE2.x, TE3.x
// **********************************************************************************
using System.Reflection;

const string mImportTemplate =
@"let
Source = DatabaseQuery,
Data = Source{{[Schema=""{0}"",Item=""{1}""]}}[Data]
in
Data";

foreach(var table in Model.Tables)
{
// Direct Lake-mode tables only have 1 partition...
if(table.Partitions.Count != 1) continue;

// ...which should be in "DirectLake" mode:
var partition = table.Partitions[0];
if(partition.Mode != ModeType.DirectLake) continue;

// Tabular Editor unfortunately doesn't expose the SchemaName property of EntityPartitionSources,
// so we'll have to use reflection to access the underlying TOM object.
var pMetadataObjct = typeof(Partition).GetProperty("MetadataObject", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
var tomPartition = pMetadataObjct.GetValue(partition) as Microsoft.AnalysisServices.Tabular.Partition;
var tomPartitionSource = tomPartition.Source as Microsoft.AnalysisServices.Tabular.EntityPartitionSource;

// Table does not have an EntityPartitionSource, meaning it is not a Direct Lake table
// (shouldn't happen, since we already checked for DirectLake mode above...)
if(tomPartitionSource == null) continue;

var schemaName = tomPartitionSource.SchemaName;
var tableName = tomPartitionSource.EntityName;

// Rename the original (Direct Lake) partition (as we can't have two partitions with the same name):
var partitionName = partition.Name;
partition.Name += "_old";

// Add the new (Import) partition:
table.AddMPartition(partitionName, string.Format(mImportTemplate, schemaName, tableName));

// Delete the old (Direct Lake) partition):
partition.Delete();
}

// Update model collation:
Model.Collation = null;
```
### C# Script to convert Direct Lake model to Import Mode
<script src="https://gist.github.com/otykier/ac53aac2a2f22838c5b2019fd45aaa98.js"></script>

0 comments on commit 8c83d47

Please sign in to comment.