Macros
Macros allow you to transform features into callable functions, making your feature logic reusable with different input values. This powerful capability enables more flexible and modular feature engineering.
Basic Syntax
The MACRO function transforms a feature into a callable function by specifying which of its dependencies should be treated as function parameters.
MACRO(<feature expression> USING INPUT <input1>, <input2>, ...)
Example: Circle Area Calculation
In this example, we create a macro function to calculate the area of a circle with different radius values:
The AREA_CIRCLE_FUNCTION
macro takes the RADIUS
input and produces a callable function. We then invoke this function with different radius values (2 and 5) to calculate the corresponding circle areas.
Inline Expressions
You can use expressions directly in macro definitions without having to name intermediate features:
This example shows that you can:
- Use expressions directly inside the MACRO function
- Use expressions as arguments when calling the macro function
Multiple Parameters
Macros can accept multiple input parameters:
Here, we define a macro that calculates rectangle area and accepts both length and width as parameters.
Polymorphism
Macros are currently not polymorphic, meaning that they can only accept one type for each input parameter, the one defined in the INPUT() declaration.
To support these capabilities, you currently need to use UDFs instead.
Chaining syntax
Macros support chaining syntax. The chained expression becomes the first argument of the macro function.
Here, we define a macro that calculates rectangle area and accepts both length and width as parameters.
Best Practices
- Use macros when you need to reuse feature logic with different input values
- Consider macros for parameterizing complex calculations
- Name your macro functions descriptively to indicate their purpose
- When working with multiple inputs, make sure to maintain the correct order when calling the macro