Python multimodal function library

Updated at:

Python Module provides a set of built-in multimodal processing functions. After loading, you can call them directly in Flink SQL without writing Python code.

Usage Limits

Only Realtime Compute Engine VVR 11.8 and later versions are supported.

Overview

Python Module exposes image and video processing functions as built-in scalar functions for SQL users. After loading the module, these functions can be used in SQL like UPPER, SUBSTRING, and other system functions.

Compared with the CREATE FUNCTION ... LANGUAGE PYTHON approach, Python Module has the following advantages:

  • Out-of-the-box: After executing LOAD MODULE python, functions are ready to use without registering UDFs individually.

  • No dependency distribution: Built-in functions are provided with Flink Python capabilities. No manual management of Python files is required.

  • Multimodal-ready: Image and video preprocessing and analysis can be completed directly in SQL.

Comparison

Python Module

CREATE FUNCTION ... LANGUAGE PYTHON

Registration

One-time loading with LOAD MODULE python

Register functions individually

Ease of Use

Low, out-of-the-box

Requires Python UDF files

Dependency Distribution

Automatic, no manual handling

Requires explicit management of Python files

Positioning

Built-in multimodal processing functions

User-defined Python logic

To use the platform-provided multimodal processing capabilities, we recommend Python Module. For fully customized logic, use Python UDF.

Usage

Load the Module

LOAD MODULE python;
USE MODULES python, core;

After execution, the functions in Python Module participate in function resolution of the current session.

Call functions

After loading the module, you can call image and video functions directly in SQL without executing CREATE FUNCTION.

The following example decodes image bytes into an image structure and resizes it to 224×224:

LOAD MODULE python;
USE MODULES python, core;

SELECT
    id,
    IMAGE_RESIZE(IMAGE_DECODE(image_bytes, 'raise', 'RGB'), 224, 224) AS image_224
FROM raw_images;

Parameter conventions

SQL functions accept only positional arguments, in the order defined by the signature. Optional parameters are shown in [ ]. For example, the signature of IMAGE_DECODE is:

IMAGE_DECODE(image_bytes [, on_error [, mode [, pixel_limit]]])

Follow these rules when calling the function:

  • If you only need default values, omit the trailing optional parameters.

  • To set a later optional parameter, also specify all preceding optional parameters.

  • To leave a preceding optional parameter unset, use NULL or the default value as a placeholder. NULL is passed as the argument value; its meaning depends on the parameter description and is not necessarily equivalent to the default.

  • Except for the data input column, configuration parameters (format, size, threshold, model name, language list, and so on) must be constants; table columns are not allowed.

-- Use the default on_error, mode, and pixel_limit
SELECT IMAGE_DECODE(img_bytes) FROM raw_images;

-- Set on_error and mode; use the default pixel_limit
SELECT IMAGE_DECODE(img_bytes, 'null', 'RGB') FROM raw_images;

-- Set on_error and pixel_limit only; use NULL as a placeholder for mode
SELECT IMAGE_DECODE(img_bytes, 'null', null, 67108864)
FROM raw_images;

-- Configuration parameters must be constants
SELECT IMAGE_RESIZE(img, 512, 512, 'lanczos') FROM images;

Examples

Extract Image Metadata

LOAD MODULE python;
USE MODULES python, core;

SELECT
  id,
  IMAGE_METADATA(image_bytes) AS meta
FROM raw_images;         

Decodes raw image bytes and extracts metadata such as width, height, number of channels, and format. Suitable for data cleansing and quality analysis.

Image Object Detection

LOAD MODULE python;
USE MODULES python, core;

SELECT
  id,
  IMAGE_DETECT_OBJECTS(IMAGE_DECODE(image_bytes, 'raise', 'RGB'), 'yolov8n', 0.1) AS objects
FROM raw_images;          

Performs object detection on decoded images using the yolov8n model with a confidence threshold of 0.1. Only detection results above the threshold are returned. Suitable for product recognition and content moderation.

Extract Video Metadata

LOAD MODULE python;
USE MODULES python, core;

SELECT
  id,
  VIDEO_METADATA(video_uri) AS meta
FROM source_video;         

Extracts metadata such as duration, resolution, frame rate, and codec from a video file URI.

Video Frame Extraction

LOAD MODULE python;
USE MODULES python, core;

SELECT
  id,
  VIDEO_EXTRACT_FRAMES(video_uri, 'sample', 1000, 8) AS sampled_frames
FROM source_video;

Extracts frames from a video at a sampling interval of 1000 milliseconds, returning up to 8 frames for subsequent image analysis or feature extraction.

Built-in Function List

The Python module provides built-in function implementations that cover image, video, and audio processing scenarios. For more function descriptions, see Image functions, Video functions, and Audio functions.