The workspace allows data analysis using SQL and Python.

When you open the workspace, a src directory exists in the project root. Place and run SQL and Python files in this src directory.

The morph_project.yml file is set to source_paths: src. It is possible to change the directory where the source code is placed by changing this value.

Execution of source code

The SQL or Python you have written can be executed using the ‘RUN’ button located in the top right-hand corner of the editor, as shown in the image below.

When executed, the corresponding Morph CLI log is output in the terminal and the data from the execution is displayed in the ‘Result’ tab in the right sidebar.

Implementing source code

SQL and Python on Morph is run by the Morph CLI installed on a VM on the workspace. It builds on the DB and standard Python scripts used, and provides extensions to allow more powerful data analysis by the user.

In addition, in Morph, files for analysis are named at runtime. Naming makes it easier to access the results of that file from other files. This mechanism is a very important part of the Morph framework.

Note that duplicate names of this name at runtime will result in a compile error.

SQL

You can use jinja2 to write SQL, and within jinja you can use Morph’s own config, a function for writing metadata. In config, you can describe the processing of the function. This allows you to leave the processing details for team members and for the Morph AI to interpret the developer’s intentions in detail.

In the SQL below, the config function is used to name and describe the process in detail. Using jinja in this way allows metadata to be written directly in the SQL.

{{
	config(
		name="example_sql_cell",
		description="Example SQL cell",
	)
}}

select * from customers limit 10

If the config function is undefined or not named by name, the sql file name is used as the name. Example: example_sql_cell.sql, in which case example_sql_cell is used as the name.

In Morph, it is forbidden to write a semicolon at the end of SQL to prevent multiple SQLs in one SQL file. If you have written one, an error message will be displayed, so delete the semi-colon and run the program again.

For detailed config settings and other grammar rules, see the following detailed pages:

Python

In Python files, functions can be registered as functions that can be executed on Morph by adding the annotation @morph.func to the function. This mechanism allows Python files to contain and execute multiple functions in a single file.

As with SQL, Python files can describe the name and description of a function by setting name and description to the arguments of this @morph.func annotation.

In addition, @morph.load_data can be used to load the results of other SQL/Python executions directly into variables. The loaded variables can be referenced from within the context argument. In the following example, the result of an SQL execution called example_sql_cell is read in by a Python function called example_python_cell, which returns the accessed result as is.

The context variable contains data in a dictionary type with the same name as the name of the read target.

import pandas as pd

import morph
from morph import MorphGlobalContext

@morph.func(
    name="example_python_cell",
    description="Example Python cell",
)
@morph.load_data("example_sql_cell")
def main(context: MorphGlobalContext) -> pd.DataFrame:
    sql_result_df = context.data["example_sql_cell"]
    return sql_result_df

Python functions must always be given @morph.func, but arguments such as name are not required. If not specified, the function name is used as-is as the name.

For more information on convenience functions, including detailed settings for @morph.func / @morph.load_data and other annotations, see the details page below: