@PublicPreview public class UDTFRegistration extends Object
Session.udtf() returns an object of this class.
To register an UDTF, you must:
The next sections describe these steps in more detail.
JavaUDTF0, JavaUDTF1,
etc.), where "n" specifies the number of input arguments for your UDTF. For example, if your UDTF
passes in 3 input arguments, implements the JavaUDTF3
interface.
In your class, implement the following four methods:
StructType object
that describes the schema for the returned rows.
StructType object
that describes the types of the input parameters. If your process() method passes
in Map arguments, you must implement the inputSchema() method. If the
method does not pass in Map arguments, implementing inputSchema() is
optional.
When a UDTF is called, the rows are grouped into partitions before they are passed to the UDTF:
For an explanation of partitions, see Table Functions and Partitions
The arguments passed to the registered UDTF are passed to process(). For each argument passed to the UDTF, you must have a corresponding argument in the signature of the process() method. Make sure that the type of the argument in the process() method matches the Snowflake data type of the corresponding argument in the UDTF.
Snowflake supports the following data types for the parameters for a UDTF:
| SQL Type | Java Type | Notes |
|---|---|---|
| NUMBER | java.lang.Short | Supported |
| NUMBER | java.lang.Integer | Supported |
| NUMBER | java.lang.Long | Supported |
| FLOAT | java.lang.Float | Supported |
| DOUBLE | java.lang.Double | Supported |
| NUMBER | java.math.BigDecimal | Supported |
| VARCHAR | String | Supported |
| BOOL | java.lang.Boolean | Supported |
| DATE | java.sql.Date | Supported |
| TIMESTAMP | java.sql.Timestamp | Supported |
| BINARY | byte[] | Supported |
| ARRAY | String[] or Variant[] | Supported String[] or Variant[] |
| OBJECT | Map<String, String> or Map<String, Variant> | Supported Map<String, String> or Map<String, Variant> |
| VARIANT | Variant | Supported |
You can use this method to generate output rows, based on any state information that you aggregate in the process() method.
Construct and return a StructType object that uses
an Array of StructField objects to specify the
Snowflake data type of each field in a returned row.
Snowflake supports the following DataTypes for the
input types:
Snowpark DataTypes | Java Type | Notes |
|---|---|---|
| ShortType | java.lang.Short | Supported |
| IntegerType | java.lang.Integer | Supported |
| LongType | java.lang.Long | Supported |
| FloatType | java.lang.Float | Supported |
| DoubleType | java.lang.Double | Supported |
| DecimalType | java.math.BigDecimal | Supported |
| StringType | String | Supported |
| BooleanType | java.lang.Boolean | Supported |
| DateType | java.sql.Date | Supported |
| TimestampType | java.sql.Timestamp | Supported |
| BinaryType | byte[] | Supported |
| ArrayType(StringType) or ArrayType(Variant) | String[] or Variant[] | Supported String[] or Variant[] |
| MapType(StringTyoe, StringType) or MapType(StringTyoe, VariantType) | Map<String, String> or Map<String, Variant> | Supported Map<String, String> or Map<String, Variant> |
| VariantType | Variant | Supported |
Construct and return a StructType object that uses
an Array of StructField objects to specify the data
type of each parameter for process().
Snowflake supports the following DataTypes for input
types
| Java Type | DataTypes | Notes |
|---|---|---|
| java.lang.Boolean | BooleanType | Supported |
| java.lang.Short | ShortType | Supported |
| java.lang.Integer | IntegerType | Supported |
| java.lang.Long | LongType | Supported |
| java.lang.Float | FloatType | Supported |
| java.lang.Double | DoubleType | Supported |
| java.math.BigDecimal | DecimalType | Supported |
| String | StringType | Supported |
| java.sql.Date | DateType | Supported |
| java.sql.Timestamp | TimestampType | Supported |
| byte[] | BinaryType | Supported |
| String[] | ArrayType(StringType) | Supported |
| Variant[] | ArrayType(Variant) | Supported |
| Map<String, String> | MapType(StringType, StringType) | Supported |
| Map<String, Variant> | MapType(StringType, VariantType) | Supported |
| com.snowflake.snowpark_java.types.Variant | VariantType | Supported |
The UDTF passes in 2 arguments, so the class implements JavaUDTF2.
The arguments start and count specify the starting number for the row and the
number of rows to generate.
import java.util.stream.Stream;
import com.snowflake.snowpark_java.types.*;
import com.snowflake.snowpark_java.udtf.*;
class MyRangeUdtf implements JavaUDTF2<Integer, Integer> {
public StructType outputSchema() {
return StructType.create(new StructField("C1", DataTypes.IntegerType));
}
// Because the process() method in this example does not pass in Map arguments,
// implementing the inputSchema() method is optional.
public StructType inputSchema() {
return StructType.create(
new StructField("start_value", DataTypes.IntegerType),
new StructField("value_count", DataTypes.IntegerType));
}
public Stream<Row> endPartition() {
return Stream.empty();
}
public Stream<Row> process(Integer start, Integer count) {
Stream.Builder<Row> builder = Stream.builder();
for (int i = start; i < start + count ; i++) {
builder.add(Row.create(i));
}
return builder.build();
}
}
// Use the MyRangeUdtf defined in previous example.
TableFunction tableFunction = session.udtf().registerTemporary("myUdtf", new MyRangeUdtf());
session.tableFunction(tableFunction, Functions.lit(10), Functions.lit(5)).show();
registerPermanent().
When registering a permanent UDTF, you must specify a stage where the registration method will upload the JAR files for the UDTF and its dependencies. For example:
// Use the MyRangeUdtf defined in previous example.
TableFunction tableFunction = session.udtf().registerPermanent("myUdtf", new MyRangeUdtf(), "@myStage");
session.tableFunction(tableFunction, Functions.lit(10), Functions.lit(5)).show();
registerTemporary(JavaUDTF udtf) to
create an anonymous UDTF instead. For example:
// Use the MyRangeUdtf defined in previous example.
TableFunction tableFunction = session.udtf().registerTemporary(new MyRangeUdtf());
session.tableFunction(tableFunction, Functions.lit(10), Functions.lit(5)).show();
| Modifier and Type | Method and Description |
|---|---|
TableFunction |
registerPermanent(String funcName,
JavaUDTF udtf,
String stageLocation)
Registers an UDTF instance as a Snowflake UDTF.
|
TableFunction |
registerTemporary(JavaUDTF udtf)
Registers an UDTF instance as a temporary anonymous UDTF that is scoped to this session.
|
TableFunction |
registerTemporary(String funcName,
JavaUDTF udtf)
Registers an UDTF instance as a temporary Snowflake UDTF that you plan to use in the session.
|
@PublicPreview public TableFunction registerTemporary(JavaUDTF udtf)
udtf - The UDTF instance to be registered@PublicPreview public TableFunction registerTemporary(String funcName, JavaUDTF udtf)
funcName - The name that you want to use to refer to the UDTF.udtf - The UDTF instance to be registered@PublicPreview public TableFunction registerPermanent(String funcName, JavaUDTF udtf, String stageLocation)
The function uploads the JAR files that the UDTF depends upon to the specified stage. Each JAR file is uploaded to a subdirectory named after the MD5 checksum for the file.
If you register multiple UDTFs and specify the same stage location, any dependent JAR files used by those functions will only be uploaded once. The JAR file for the UDTF code itself will be uploaded to a subdirectory named after the UDTF.
funcName - The name that you want to use to refer to the UDTF.udtf - The UDTF instance to be registered.stageLocation - Stage location where the JAR files for the UDTF and its dependencies
should be uploaded© 2022 Snowflake Inc. All Rights Reserved