Overview
The semantic layer in Oxy is a powerful abstraction that transforms raw database schemas into business-friendly concepts. It provides a declarative way to define business logic, metrics, and relationships that AI agents can understand and use to answer analytical questions accurately. Think of it as a translation layer between your technical data infrastructure and business questions. Instead of requiring users or AI agents to understand complex database schemas and SQL, they can work with intuitive business concepts like “total revenue,” “customer segments,” or “monthly active users.”Oxy also supports a simpler semantic model format (
.sem.yml
) for straightforward use cases. See Semantic Model for a lighter-weight alternative.Key Benefits
- AI-Optimized: Provides rich context for AI agents to understand your data and generate accurate queries
- Declarative: Define business logic once in YAML, use it everywhere
- Consistent: Ensures everyone in your organization uses the same metric definitions
- Governed: Centralize data access, security, and quality rules
- Portable: Works across different BI tools and query engines
Core Concepts
The semantic layer consists of four main building blocks:Views
Views represent logical data models that define how to access and interpret data. They encapsulate the business logic needed to transform raw tables into meaningful business concepts. A view defines:- How to access data (table or custom SQL)
- What entities exist in the data
- Available dimensions (attributes)
- Available measures (metrics and aggregations)
Entities
Entities represent distinct objects or concepts in your data model (like customers, orders, or products). They enable automatic relationship discovery and intelligent joins between views.Dimensions
Dimensions are attributes that describe your entities. They’re used for grouping, filtering, and segmenting data (like order status, customer name, or product category).Measures
Measures are quantitative calculations and aggregations that provide business insights (like total revenue, average order value, or customer count).Topics
Topics are collections of related views organized by business domain. They help users discover and explore related data concepts together.Project Structure
Organize your semantic layer files in a clear directory structure:Creating Views
Views are the foundation of your semantic layer. They define how to access and interpret your data.Basic View Structure
Create a file with the.view.yml
extension in your semantics/views/
directory:
semantics/views/orders.view.yml
View Properties
Property | Type | Required | Description |
---|---|---|---|
name | string | Yes | Unique identifier for the view |
description | string | Yes | Human-readable description of what this view represents |
datasource | string | Yes | Name of the datasource to use |
table | string | Conditional | Database table reference (required if sql not specified) |
sql | string | Conditional | Custom SQL query (required if table not specified) |
entities | array | Yes | List of entities in this view |
dimensions | array | Yes | List of dimensions (attributes) |
measures | array | No | List of measures (aggregations) |
SQL-Based Views
For complex transformations, use custom SQL instead of a direct table reference:semantics/views/customer_metrics.view.yml
Defining Entities
Entities represent the core objects in your data and enable automatic joins between views.Entity Types
Primary Entity
Each view should have exactly one primary entity representing the main subject:Foreign Entity
Foreign entities reference objects primarily defined in other views:Entity Properties
Property | Type | Required | Description |
---|---|---|---|
name | string | Yes | Unique identifier for the entity |
type | string | Yes | Entity type: primary or foreign |
description | string | Yes | Human-readable description |
key | string | No* | The dimension that serves as the entity key (single key) |
keys | array[string] | No* | The dimensions that serve as the entity keys (composite keys) |
key
or keys
must be provided, but not both.
How Entities Enable Joins
When you define entities consistently across views, the semantic layer automatically understands relationships:keys
field:
- “Show total revenue by customer acquisition channel”
- “What’s the average order value for customers from email campaigns?”
Defining Dimensions
Dimensions are the descriptive attributes of your data used for filtering and grouping.Dimension Types
Oxy supports five dimension types:Type | Description | Example Values |
---|---|---|
string | Text and categorical data | ”pending”, “John Smith”, “Electronics” |
number | Numeric values | 42, 3.14, 1000 |
date | Date values | ”2024-03-15” |
datetime | Date and time values | ”2024-03-15T14:30:00Z” |
boolean | True/false values | true, false |
Dimension Properties
Property | Type | Required | Description |
---|---|---|---|
name | string | Yes | Unique identifier within the view |
type | string | Yes | Data type |
description | string | No | Human-readable description |
expr | string | Yes | SQL expression |
samples | array | No | Example values for documentation |
synonyms | array | No | Alternative names for natural language queries |
Examples
Using Synonyms
Synonyms help AI agents understand natural language queries:- “What’s the total sales this month?”
- “Show me income by region”
- “Compare gross revenue year over year”
Defining Measures
Measures provide quantitative insights through aggregations and calculations.Measure Types
Type | Description | Example |
---|---|---|
count | Count of records | Total number of orders |
sum | Sum of values | Total revenue |
average | Arithmetic mean | Average order value |
min | Minimum value | Smallest order amount |
max | Maximum value | Largest order amount |
count_distinct | Count of unique values | Number of unique customers |
median | Median (50th percentile) | Median order value |
custom | Custom SQL expression | Complex calculations |
Measure Properties
Property | Type | Required | Description |
---|---|---|---|
name | string | Yes | Unique identifier within the view |
type | string | Yes | Measure type |
description | string | No | Human-readable description |
expr | string | Conditional | SQL expression (required for most types) |
filters | array | No | Filters to apply to the measure |
samples | array | No | Example values for documentation |
synonyms | array | No | Alternative names |
Examples
Filtered Measures
Apply filters to create conditional aggregations:Custom Measures
For complex calculations, use thecustom
type:
Cross-Entity Measures
Reference dimensions or measures from related entities using{{entity.field}}
syntax:
Organizing with Topics
Topics group related views by business domain for better organization and discovery.Creating Topics
Create a file with the.topic.yml
extension in your semantics/topics/
directory:
semantics/topics/sales.topic.yml
Topic Properties
Property | Type | Required | Description |
---|---|---|---|
name | string | Yes | Unique identifier for the topic |
description | string | Yes | Description of the business domain |
views | array | Yes | List of view names in this topic |
Example Topics
semantics/topics/ecommerce.topic.yml
semantics/topics/marketing.topic.yml
semantics/topics/finance.topic.yml
Using the Semantic Layer
Once you’ve defined your semantic layer with views, entities, dimensions, and measures, you need to build it before use:oxy build
after creating or modifying semantic layer files to ensure your changes are picked up.
After building, you can use the semantic layer across different components of Oxy. The semantic layer provides a consistent, business-friendly interface for querying data.
In Agents
Add thesemantic_query
tool to your agent to enable it to query the semantic layer directly. The agent can then answer business questions using your defined metrics and dimensions.
Basic Setup
agents/analytics.agent.yml
Tool Configuration
Thesemantic_query
tool has the following properties:
Property | Type | Required | Description |
---|---|---|---|
name | string | Yes | Unique identifier for the tool |
type | string | Yes | Must be semantic_query |
topic | string | Yes | Name of the semantic topic to query |
Example Queries
Once configured, your agent can handle queries like:- “What’s the total revenue by customer segment?”
- “Show me the top 5 products by sales this month”
- “What’s the average order value for each acquisition channel?”
Multiple Topics
You can add multiplesemantic_query
tools for different topics:
In Workflows
Use thesemantic_query
task type in workflows to execute structured queries against your semantic layer. This is ideal for automated reporting, data pipelines, and scheduled analytics.
Basic Workflow Task
workflows/sales_report.workflow.yml
Semantic Query Task Properties
Property | Type | Required | Description |
---|---|---|---|
type | string | Yes | Must be semantic_query |
topic | string | Yes | Name of the semantic topic to query |
dimensions | array | No | List of dimensions to include (view.field format) |
measures | array | No | List of measures to calculate (view.field format) |
filters | array | No | Filters to apply to the query |
orders | array | No | Sort order for results |
limit | number | No | Maximum number of rows to return |
Field Referencing
Reference dimensions and measures using the formatview_name.field_name
:
Filtering Data
Apply filters to narrow down your results:Ordering Results
Control the sort order of your results:Advanced Example
workflows/customer_analysis.workflow.yml
In Routing Agents
Routing agents can include semantic topics as routes, enabling intelligent task routing based on semantic understanding.Adding Topics to Routes
agents/_routing.agent.yml
Complete Routing Example
agents/_data_router.agent.yml
Best Practices
Naming Conventions
- Views: Use lowercase, descriptive names (e.g.,
orders
,customer_metrics
) - Entities: Use singular nouns (e.g.,
customer
,order
,product
) - Dimensions: Use descriptive names without prefixes (e.g.,
order_date
notdim_order_date
) - Measures: Use descriptive names that indicate the metric (e.g.,
total_revenue
,avg_order_value
)
Documentation
- Always provide clear descriptions for views, entities, dimensions, and measures
- Include sample values for dimensions to help users understand the data
- Add synonyms for common alternative terms your users might use
- Document any business rules or calculations in measure descriptions
Organization
- Group related views into topics by business domain
- Keep views focused on a single primary entity
- Use foreign entities to establish relationships between views
- Organize files in a clear directory structure
Performance
- Use table references when possible instead of complex SQL
- Pre-aggregate data in SQL-based views when appropriate
- Index the key columns referenced in entity definitions
- Consider materializing complex SQL views in your database
Examples
E-commerce Semantic Layer
semantics/views/orders.view.yml
semantics/views/customers.view.yml
semantics/topics/ecommerce.topic.yml