Sharing Queries

How to re-use queries across your organization and projects.

️ Setup Required

The following examples assume you've already completed Writing Queries.

Creating a View

Fenl expressions can be shared and re-used by creating a view. A view is a named expression. In subsequent Fenl expressions, the view's name is synonymous with the view's expression. Views are persisted in the Kaskada platform and are accessible to any query.

To create a view, we'll start by describing the expression we'd like to name. In this case, we're interested in some purchase statistics for each user. This definition depends on business logic and might require some iteration to get just right, so we'll use the IPython "fenlmagic" extension to explore the results of our query.

%%fenl --var purchase_stats
{
    time: Purchase.purchase_time,
    entity: Purchase.customer_id,
    max_amount: Purchase.amount | max(),
    min_amount: Purchase.amount | min(),
}

Notice that we added --var purchase_stats to the beginning of the magic block; this causes the extension to assign the query string to the variable purchase_stats when the block is run.

️ Usage Note

The fenlmagic extension is designed to make it easy to interactively explore your dataset. The python client exposes the full functionality of the Kaskada API and is better suited to tasks such as managing Views, Tables, and making multiple queries with different query variables.

We can use this variable to create a view using the Python client without re-typing the expression:

from kaskada import view

view.create_view(
    view_name = "PurchaseStats",
    expression = purchase_stats,
)

️ Idiomatic Kaskada

We like to use CamelCase for view names. This is the same naming convention as we use for tables, and helps to communicate that we're referring to a persistent resource in the Kaskada system, as opposed to a temporary local value created as part of a query.

We've now created a view named PurchaseStats. We can verify it was created successfully by searching for views matching the name:

view.list_views(search = "Purchase")
{
  "views": {
    [
      { 
        "view_name": "PurchaseStats",
        "expression": "{
          time: Purchase.purchase_time,
          entity: Purchase.customer_id,
          max_amount: Purchase.amount | max(),
          min_amount: Purchase.amount | min()
        }"
      }
    ] 
  }
}

Using a view

Now that we've created a view, let's look at how the view can be used. We can use a view's name anywhere we could use the view's expression - the only restriction placed on views is that they must be valid Fenl expressions.

Here's an example of using a view to filter the values produced by an expression:

%%fenl
{
  time: Purchase.purchase_time,
  entity: Purchase.customer_id,
  total_purchases: Purchase.amount | sum(),
} | when(PurchaseStats.max_amount > 100)

Views may reference other views, so we could give this expression a name and create a view for it as well if we wanted to.

Views are useful any time you need to share or re-use expressions:

  • Cleaning operations
  • Common business logic
  • Final feature vectors

For more help with views, see Reference - Working with Views


© Copyright 2021 Kaskada, Inc. All rights reserved. Privacy Policy

Kaskada products are protected by patents in the United States, and Kaskada is currently seeking protection internationally with pending applications.