Understand SQL, Python, Java and JavaScript handlers
A handler is the code Snowflake executes when a routine is called. Language choice changes syntax, type mapping, packaging and runtime requirements.
Understand the idea
The SQL CREATE statement defines the routine contract, even when its implementation is written in another language. A Python def or Java class goes inside the handler body, not into an ordinary SELECT expression.
| Concept | What it means |
|---|---|
| SQL | Fits calculations and database-oriented scripting. |
| Python | Can reuse supported Python logic and packages. |
| Java | Uses a class and method handler with a supported runtime. |
| JavaScript | Uses JavaScript handler code and its numeric/type rules. |
A concrete example
Returns 5. The SQL call invokes Python inside Snowflake, not on your laptop.
Remember: Match the language to the requirement and understand its type/runtime contract.
Explain it step by step · full theory
Understand the idea
The SQL CREATE statement defines the routine contract, even when its implementation is written in another language. A Python def or Java class goes inside the handler body, not into an ordinary SELECT expression.
Start with SQL when it expresses the rule clearly. Choose another language for a specific library or processing need. Runtime versions and packages must be supported in your account; these are small inline examples with no external packages.
Follow the example
JavaScript numbers use floating-point semantics, so do not assume exact financial precision or large-integer safety. NULL mappings also differ across languages; inspect the language documentation rather than copying one handler unchanged into another language.
The following functions each add one. They teach the handler boundary, not a performance comparison. The Python/Java examples require supported runtime versions; check the current CREATE FUNCTION reference if your account rejects a version.
Words used in this lesson
- SQL
- Fits calculations and database-oriented scripting.
- Python
- Can reuse supported Python logic and packages.
- Java
- Uses a class and method handler with a supported runtime.
- JavaScript
- Uses JavaScript handler code and its numeric/type rules.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Understand SQL, Python, Java and JavaScript handlers
Start with the requirement
A handler is the code Snowflake executes when a routine is called. Language choice changes syntax, type mapping, packaging and runtime requirements.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Start with the requirement
A handler is the code Snowflake executes when a routine is called. Language choice changes syntax, type mapping, packaging and runtime requirements.
- Apply the rule
The SQL CREATE statement defines the routine contract, even when its implementation is written in another language. A Python def or Java class goes inside the handler body, not into an ordinary SELECT expression.
- Explain the result
Match the language to the requirement and understand its type/runtime contract.
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Before running this lesson
Manual practice in an authorized Snowflake learning account. Run lesson 1 setup first and select L2_OBJECTS_LAB.M10_LAB and your authorized warehouse in each session. Use fresh object names; do not replace existing objects. Read whole scripting blocks before running them. The website does not execute commands.
Python scalar handler
CREATE FUNCTION M10_PY_PLUS_ONE(N FLOAT) RETURNS FLOAT
LANGUAGE PYTHON RUNTIME_VERSION='3.11' HANDLER='plus_one' AS $$
def plus_one(n):
return None if n is None else n + 1
$$;
SELECT M10_PY_PLUS_ONE(4);What to expect and why
Returns 5. The SQL call invokes Python inside Snowflake, not on your laptop.
Java class and method handler
CREATE FUNCTION M10_JAVA_PLUS_ONE(N FLOAT) RETURNS FLOAT
LANGUAGE JAVA RUNTIME_VERSION='11' HANDLER='Adder.plusOne' AS $$
class Adder {
public static Double plusOne(Double n) { return n == null ? null : n + 1; }
}
$$;
SELECT M10_JAVA_PLUS_ONE(4);What to expect and why
Returns 5 when the runtime is supported. HANDLER identifies the class method.
JavaScript inline handler
CREATE FUNCTION M10_JS_PLUS_ONE(N FLOAT) RETURNS FLOAT
LANGUAGE JAVASCRIPT AS $$ return N === undefined ? undefined : N + 1; $$;
SELECT M10_JS_PLUS_ONE(4);What to expect and why
Returns 5. Unquoted argument N is uppercase in this JavaScript handler.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Why not use this FLOAT example as a money library?
Compare your reasoning
It demonstrates handlers; exact financial calculations need an explicit decimal precision and rounding contract.