SQLCtx can import semantic models from other popular semantic layer formats, letting you use your existing metric definitions with SQLCtx's query engine and features.

Install format dependencies when needed: `uv add "sqlctx[lookml]"`, `uv add "sqlctx[metricflow]"`, or `uv add "sqlctx[dax]"`. Native YAML, SQL, and Cube models work with the base package.

## Supported Formats

| Format | Import | Notes |
|--------|:------:|-------|
| **SQLCtx** (native) | ✅ | Full feature support |
| **Power BI TMDL/DAX** | ✅ | Measures, calculated columns and tables, relationships, preserved DAX, and TMDL export; install the `dax` extra |
| **Cube** | ✅ | Includes dimensions, measures, joins, segments, rolling windows, and pre-aggregations |
| **MetricFlow** (dbt) | ✅ | No native segments or hierarchies |
| **LookML** (Looker) | ✅ | Liquid templating (not Jinja) |
| **Hex** | ✅ | No segments or cross-model derived metrics |
| **Rill** | ✅ | No relationships, segments, or cross-model metrics; single-model only |
| **Superset** (Apache) | ✅ | No relationships in datasets |
| **Omni** | ✅ | Relationships in separate model file |
| **BSL** (Boring Semantic Layer) | ✅ | Ibis-style expressions; supports roundtrip export |
| **Malloy** | ✅ | Parsed via `.malloy` files |
| **Snowflake Cortex** | ✅ | Snowflake semantic model YAML |
| **OSI** (Open Semantic Interchange) | ✅ | Vendor-agnostic semantic model YAML; supports import/export |
| **AtScale SML** | ✅ | Repository-style SML import/export |
| **GoodData LDM** | ✅ | Cloud and legacy LDM JSON import/export |
| **Holistics AML** | ✅ | AML model import/export |
| **ThoughtSpot TML** | ✅ | Table and worksheet TML import/export |

## Feature Compatibility

This table shows which SQLCtx features are supported when importing from other formats:

| Feature | SQLCtx | Cube | MetricFlow | LookML | Hex | Rill | Superset | Omni | BSL | Notes |
|---------|------------|------|------------|--------|-----|------|----------|------|-----|-------|
| **Models** | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | All formats support models/tables |
| **Dimensions** | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | All formats support dimensions |
| **Simple Metrics** | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | All formats support sum, count, avg, min, max |
| **Time Dimensions** | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | All formats support time dimensions with granularity |
| **Relationships** | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | Rill/Superset: single-model only; Omni: in model file |
| **Derived Metrics** | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | All formats support calculated metrics |
| **Metric Filters** | ✅ | ✅ | ❌ | ✅ | ✅ | ⚠️ | ❌ | ✅ | ❌ | Rill has basic support; Superset/BSL lack filters |
| **Ratio Metrics** | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | Rill/Superset/BSL don't have native ratio metric type |
| **Segments** | ✅ | ✅ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | Only Cube and LookML have native segment support |
| **Cumulative Metrics** | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | Cube has rolling_window; MetricFlow has cumulative; others lack native support |
| **Time Comparison** | ✅ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | Only MetricFlow has native time comparison metrics |
| **Jinja Templates** | ⚠️ | ✅ | ✅ | ⚠️ | ✅ | ✅ | ✅ | ✅ | ❌ | SQLCtx renders templates only in filters; LookML uses Liquid; BSL uses Ibis |
| **Hierarchies** | ✅ | ⚠️ | ❌ | ⚠️ | ❌ | ❌ | ❌ | ⚠️ | ❌ | Cube/LookML/Omni: via drill_fields |
| **Inheritance** | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | Only LookML has native extends support |
| **Metadata Fields** | ✅ | ⚠️ | ⚠️ | ⚠️ | ⚠️ | ⚠️ | ✅ | ✅ | ✅ | Label and description support varies by format |
| **Parameters** | ⚠️ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | Deprecated; Python-only; filter interpolation only |
| **Ungrouped Queries** | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | SQLCtx-only feature |

**Legend:**

- ✅ Full support - feature fully supported on import
- ⚠️ Partial support - feature works with limitations
- ❌ Not supported - feature not available in source format

Power BI TMDL/DAX, Malloy, Snowflake Cortex, OSI, AtScale SML, GoodData LDM, Holistics AML, and ThoughtSpot TML are supported for import. Detailed mapping notes for each are included below.

## Importing into SQLCtx

### Quick Start: Auto-Discovery

The easiest way to load semantic models from any format:

```python doc-validate=lint
from sqlctx import SemanticLayer, load_from_directory

# Point at a directory with mixed formats
layer = SemanticLayer(connection="duckdb:///data.db")
load_from_directory(layer, "semantic_models/")

# That's it! Automatically:
# - Discovers .sql, .lkml, .malloy, .json, .aml, .tml, and .yml/.yaml files
# - Detects formats: SQLCtx, Cube, MetricFlow, LookML, Hex, BSL, Snowflake Cortex, Omni, Rill, Superset, OSI, GoodData, Holistics, ThoughtSpot
# - Detects AtScale SML repositories by catalog/object_type structure
# - Parses with the right adapter
# - Infers relationships from foreign key naming
# - Builds the join graph
```

#### How Relationship Inference Works

`load_from_directory()` automatically infers relationships based on foreign key naming conventions:

- `orders.customer_id` → `customers.id` (many-to-one)
- `line_items.order_id` → `orders.id` (many-to-one)
- `products.category_id` → `categories.id` (many-to-one)

It tries both singular and plural forms, so `customer_id` will match both `customer` and `customers` tables.

Reverse relationships (one-to-many) are automatically added to the target model.

### Manual Adapter Usage

For more control over the import process, you can use adapters directly:

#### From Cube

Read Cube.js semantic models into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.cube import CubeAdapter

# Import from Cube YAML
adapter = CubeAdapter()
graph = adapter.parse("cube/schema/Orders.yml")

# Query with SQLCtx
from sqlctx import SemanticLayer
layer = SemanticLayer()
layer.graph = graph
result = layer.sql("SELECT orders.revenue FROM orders")
```

```bash doc-test
uv run --with sqlctx python -c "from sqlctx.adapters.cube import CubeAdapter; print(CubeAdapter.__name__)"
```

```text doc-expected
CubeAdapter
```

### From MetricFlow

Read dbt MetricFlow models into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.metricflow import MetricFlowAdapter

# Import from MetricFlow YAML
adapter = MetricFlowAdapter()
graph = adapter.parse("models/metrics/")  # Directory of YAML files

# Query with SQLCtx
layer = SemanticLayer()
layer.graph = graph
result = layer.sql("SELECT orders.revenue FROM orders")
```

### From LookML

Read Looker LookML views into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.lookml import LookMLAdapter

# Import from LookML
adapter = LookMLAdapter()
graph = adapter.parse("views/orders.lkml")  # Single file or directory

# Query with SQLCtx
from sqlctx import SemanticLayer
layer = SemanticLayer()
layer.graph = graph
result = layer.sql("SELECT orders.revenue FROM orders")
```

### From Hex

Read Hex semantic models into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.hex import HexAdapter

# Import from Hex YAML
adapter = HexAdapter()
graph = adapter.parse("hex/models/")  # Directory of YAML files

# Query with SQLCtx
from sqlctx import SemanticLayer
layer = SemanticLayer()
layer.graph = graph
result = layer.sql("SELECT orders.revenue FROM orders")
```

### From Rill

Read Rill metrics views into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.rill import RillAdapter

# Import from Rill YAML
adapter = RillAdapter()
graph = adapter.parse("rill/metrics/")  # Directory of YAML files

# Query with SQLCtx
from sqlctx import SemanticLayer
layer = SemanticLayer()
layer.graph = graph
result = layer.compile(metrics=["orders.revenue"])
```

### From Superset

Read Apache Superset datasets into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.superset import SupersetAdapter

# Import from Superset YAML
adapter = SupersetAdapter()
graph = adapter.parse("superset/datasets/")  # Directory of YAML files

# Query with SQLCtx
from sqlctx import SemanticLayer
layer = SemanticLayer()
layer.graph = graph
result = layer.sql("SELECT total_revenue FROM orders")
```

### From Omni

Read Omni Analytics views into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.omni import OmniAdapter

# Import from Omni YAML views
adapter = OmniAdapter()
graph = adapter.parse("omni/")  # Directory with views/ subdirectory and model.yaml

# Query with SQLCtx
from sqlctx import SemanticLayer
layer = SemanticLayer()
layer.graph = graph
result = layer.sql("SELECT total_revenue FROM orders")
```

### From BSL

Read [Boring Semantic Layer](https://github.com/boringdata/boring-semantic-layer) models into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.bsl import BSLAdapter

# Import from BSL YAML
adapter = BSLAdapter()
graph = adapter.parse("bsl/models/")  # Directory of YAML files

# Query with SQLCtx
from sqlctx import SemanticLayer
layer = SemanticLayer()
layer.graph = graph
result = layer.sql("SELECT orders.revenue FROM orders")

# Export back to BSL (roundtrip supported)
adapter.export(graph, "output/bsl_models.yml")
```

### From Malloy

Read Malloy semantic models into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.malloy import MalloyAdapter

# Import from Malloy
adapter = MalloyAdapter()
graph = adapter.parse("models/")  # Directory or single .malloy file

# Query with SQLCtx
from sqlctx import SemanticLayer
layer = SemanticLayer()
layer.graph = graph
result = layer.sql("SELECT orders.revenue FROM orders")
```

### From Snowflake Cortex

Read Snowflake Cortex semantic model YAML into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.snowflake import SnowflakeAdapter

# Import from Snowflake semantic model YAML
adapter = SnowflakeAdapter()
graph = adapter.parse("snowflake/semantic_model.yaml")  # File or directory

# Query with SQLCtx
from sqlctx import SemanticLayer
layer = SemanticLayer()
layer.graph = graph
result = layer.sql("SELECT orders.total_revenue FROM orders")
```

### From OSI (Open Semantic Interchange)

Read OSI YAML semantic models into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.osi import OSIAdapter

# Import from OSI YAML
adapter = OSIAdapter()
graph = adapter.parse("osi/semantic_model.yaml")  # File or directory

# Query with SQLCtx
from sqlctx import SemanticLayer
layer = SemanticLayer()
layer.graph = graph
result = layer.sql("SELECT orders.total_revenue FROM orders")

# Export back to OSI (roundtrip supported)
adapter.export(graph, "output/osi_semantic_model.yml")
```

### From AtScale SML

Read AtScale SML repositories into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.atscale_sml import AtScaleSMLAdapter

# Import from an AtScale SML repository directory
adapter = AtScaleSMLAdapter()
graph = adapter.parse("atscale_repo/")

# Query with SQLCtx
from sqlctx import SemanticLayer
layer = SemanticLayer()
layer.graph = graph
result = layer.sql("SELECT sales.total_sales FROM sales")

# Export back to SML repository structure
adapter.export(graph, "output/atscale_repo/")
```

### From GoodData LDM

Read GoodData LDM JSON into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.gooddata import GoodDataAdapter

# Import from GoodData LDM JSON (cloud or legacy)
adapter = GoodDataAdapter()
graph = adapter.parse("gooddata/ldm.json")  # File or directory of JSON files

# Query with SQLCtx
from sqlctx import SemanticLayer
layer = SemanticLayer()
layer.graph = graph
result = layer.sql("SELECT orders.total_revenue FROM orders")

# Export back to GoodData LDM JSON
adapter.export(graph, "output/gooddata_ldm.json")
```

### From Holistics AML

Read Holistics AML semantic models into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.holistics import HolisticsAdapter

# Import from Holistics AML
adapter = HolisticsAdapter()
graph = adapter.parse("holistics/")  # Directory or single .aml file

# Query with SQLCtx
from sqlctx import SemanticLayer
layer = SemanticLayer()
layer.graph = graph
result = layer.sql("SELECT orders.total_revenue FROM orders")

# Export back to AML files
adapter.export(graph, "output/holistics/")
```

### From ThoughtSpot TML

Read ThoughtSpot TML tables/worksheets into SQLCtx:

```python doc-validate=lint
from sqlctx.adapters.thoughtspot import ThoughtSpotAdapter

# Import from ThoughtSpot TML
adapter = ThoughtSpotAdapter()
graph = adapter.parse("thoughtspot/")  # Directory or single .tml/.yaml file

# Query with SQLCtx
from sqlctx import SemanticLayer
layer = SemanticLayer()
layer.graph = graph
result = layer.sql("SELECT sales.total_revenue FROM sales")

# Export back to ThoughtSpot TML
adapter.export(graph, "output/thoughtspot/")
```

## Import Mapping

These sections describe how each format's concepts map to SQLCtx when importing.

### Cube

- `cubes` → `models`
- `dimensions` → `dimensions`
- `measures` → `metrics`
- `joins` → `relationships` (inferred from join definitions)
- `${CUBE}` placeholder → `{model}` placeholder
- `segments` → `segments` (native support)
- Calculated measures (type=number) → derived metrics
- `rolling_window` → cumulative metrics

### MetricFlow

- `semantic_models` → `models`
- `entities` → inferred `relationships`
- `dimensions` → `dimensions`
- `measures` → model-level `metrics`
- `metrics` (graph-level) → graph-level `metrics`
- Segments/hierarchies from `meta` field → preserved

### LookML

- `views` → `models`
- `explores` → `relationships` (parsed from join definitions)
- `dimensions` → `dimensions`
- `dimension_group` → multiple time dimensions (one per timeframe)
- `measures` → `metrics`
- `filters` (view-level) → `segments`
- `derived_table` → model with SQL
- `${TABLE}` placeholder → `{model}` placeholder
- Measure filters parsed from `filters__all`
- Foreign keys extracted from `sql_on` in explore joins

### Hex

- Model `id` and `base_sql_table`/`base_sql_query` → `models`
- `dimensions` with `expr_sql` or `expr_calc` → `dimensions`
- `measures` with `func`/`func_sql`/`func_calc` → `metrics`
- `relations` with `join_sql` → `relationships`
- Measure `filters` (inline or referenced) → metric filters
- `unique: true` dimensions → primary key detection
- `timestamp_tz`/`timestamp_naive`/`date` types → time dimensions

### Rill

- `metrics_view` (type) → `models`
- `dimensions` with `column`/`expression` → `dimensions`
- `measures` with `expression` → `metrics`
- `timeseries` column → time dimension
- `smallest_time_grain` → time dimension granularity
- Derived measures (`type: derived`) → derived metrics
- Simple aggregation expressions parsed with sqlglot

### Superset

- `table_name` → model `name`
- `schema` + `table_name` → model `table`
- `sql` → model `sql` (for virtual datasets)
- `columns` → `dimensions`
- `metrics` → model `metrics`
- `main_dttm_col` → time dimension detection
- `verbose_name` → `label` field
- `is_dttm` flag → time dimension type
- `metric_type` → aggregation mapping (count, sum, avg, etc.)

### Omni

- `name` (view) → model `name`
- `schema` + `table_name` → model `table`
- `sql` → model `sql` (for SQL-based views)
- `dimensions` → `dimensions`
- `measures` with `aggregate_type` → `metrics`
- `timeframes` → time dimension granularity
- `label` → model `description` (if no description field)
- `${TABLE}` placeholder → `{model}` placeholder
- `${view.field}` references → simplified field references
- Measure `filters` → metric filters
- `relationships` (from model.yaml) → model relationships

### BSL (Boring Semantic Layer)

- Model keys (top-level YAML) → `models`
- `table` → model `table`
- `dimensions` → `dimensions`
- `measures` → `metrics`
- `joins` → `relationships`
- `_.column` expression → `sql: "column"`
- `_.column.sum()` → `agg: sum, sql: "column"`
- `_.column.mean()` → `agg: avg, sql: "column"`
- `_.column.nunique()` → `agg: count_distinct, sql: "column"`
- `_.count()` → `agg: count`
- `_.column.year()` → `EXTRACT(YEAR FROM column)`
- `is_time_dimension: true` → `type: "time"`
- `smallest_time_grain: "TIME_GRAIN_DAY"` → `granularity: "day"`
- `is_entity: true` → primary key detection
- Calc measures (referencing other measures) → derived metrics

### Malloy

- `source` definitions → `models`
- `dimension` fields → `dimensions` (type inferred from expression/name)
- `measure` fields → `metrics` (aggregates and derived expressions)
- `join_one` / `join_many` → `relationships`
- Source-level `where` clauses → `segments`
- `pick ... when ... else ...` → SQL `CASE` expressions
- Imported `.malloy` files are resolved recursively (with cycle protection)

### Snowflake Cortex

- `tables` → `models`
- `base_table` (database/schema/table) → model `table`
- `dimensions` → `dimensions` (categorical/numeric/boolean by data type)
- `time_dimensions` → `dimensions` with `type: "time"`
- `facts` (`default_aggregation`) → simple `metrics`
- Table `metrics` expressions → simple or derived `metrics`
- Table `filters` → `segments`
- Semantic-model-level `relationships` → model `relationships`

### OSI (Open Semantic Interchange)

- `semantic_model[].datasets` → `models`
- Dataset `fields` → `dimensions` (time via `dimension.is_time`)
- `semantic_model[].metrics` → graph-level `metrics`
- `semantic_model[].relationships` → model `relationships` (many-to-one)
- Multi-column keys in `primary_key`, `unique_keys`, and relationships are preserved
- `ai_context` and `custom_extensions` map into SQLCtx `meta` fields

### AtScale SML

- `dataset` objects → `models`
- `dimension` level attributes/hierarchies → `dimensions` (including parent hierarchy links)
- `metric` and `metric_calc` objects → `metrics`
- `model` relationships → model `relationships`
- `aggregates` → `pre_aggregations`
- `drillthroughs` → metric `drill_fields`

### GoodData LDM

- LDM `datasets` → `models`
- `attributes` → `dimensions`
- `facts` → `metrics`
- Dataset `references` → model `relationships`
- `dateInstances` / `dateDimensions` → date models with time dimensions
- Cloud and legacy JSON structures are both supported

### Holistics AML

- `Model` blocks → `models`
- `dimension` blocks → `dimensions`
- `measure` blocks → `metrics`
- `Relationship` blocks and dataset relationship configs → model `relationships`
- `@sql`/`@aql` definitions → SQL expressions (AQL translated to SQL)
- `extend`, `PartialModel`, and `use` imports are resolved during parse

### ThoughtSpot TML

- `table` TML columns → model `dimensions` and `metrics`
- `worksheet` TML tables/joins/formulas/worksheet columns → SQL-backed model with relationships
- `joins_with` and worksheet joins → model `relationships`
- Date bucket settings → time dimension granularity
- Unsupported aggregation functions are imported as derived metric SQL

## Validating Imports

Always validate after importing:

```python doc-validate=lint
# Import
graph = adapter.parse("source.yml")

# Verify models loaded
print(f"Loaded {len(graph.models)} models")
for name, model in graph.models.items():
    print(f"  {name}: {len(model.metrics)} metrics, {len(model.dimensions)} dimensions")

# Verify metrics
print(f"Loaded {len(graph.metrics)} graph-level metrics")

# Test query generation
layer = SemanticLayer()
layer.graph = graph
sql = layer.compile(metrics=["orders.revenue"])
print("Generated SQL:", sql)
```

## Getting Help

If you encounter issues with format conversion:

1. Check the [compatibility table](#feature-compatibility) for known limitations
2. Validate your source format is correctly structured
3. Test with a simple model first before converting complex definitions
4. File an issue at [github.com/sidequery/sqlctx](https://github.com/sidequery/sqlctx/issues) with:
   - Source format and file
   - Expected vs actual behavior
   - Generated SQL or error messages