Create Stored Procedures

This part of the tutorial shows how to create stored procedures that are used to load data using pipelines.

Note

The SQL Editor only runs the queries that you select, so ensure you have them all selected before selecting Run.

Run the following SQL commands to create the stored procedures:

USE martech;
DELIMITER //
CREATE OR REPLACE PROCEDURE process_locations(_batch QUERY(subscriber_id BIGINT(20), offset_x DOUBLE, offset_y DOUBLE)) AS
DECLARE
_expanded QUERY(city_id BIGINT, subscriber_id BIGINT, lonlat GEOGRAPHYPOINT) = SELECT
city_id, subscriber_id,
GEOGRAPHY_POINT(
GEOGRAPHY_LONGITUDE(center) + (offset_x * diameter),
GEOGRAPHY_LATITUDE(center) + (offset_y * diameter)
) AS lonlat
FROM _batch, cities;
BEGIN
INSERT INTO subscribers (city_id, subscriber_id, current_location)
SELECT city_id, subscriber_id, lonlat
FROM _expanded
ON DUPLICATE KEY UPDATE current_location = VALUES(current_location);
INSERT INTO locations (city_id, subscriber_id, ts, lonlat, olc_8)
SELECT
city_id,
subscriber_id,
now(6) AS ts,
lonlat,
encode_open_location_code(lonlat, 8) AS olc_8
FROM _expanded;
END //
CREATE OR REPLACE PROCEDURE process_purchases(_batch QUERY(subscriber_id BIGINT(20), vendor TEXT)) AS
BEGIN
INSERT INTO purchases (city_id, subscriber_id, ts, vendor)
SELECT city_id, subscriber_id, now(6) AS ts, vendor
FROM _batch, cities;
END //
CREATE OR REPLACE PROCEDURE process_requests(_batch QUERY(subscriber_id BIGINT(20), domain TEXT)) AS
BEGIN
INSERT INTO requests (city_id, subscriber_id, ts, domain)
SELECT city_id, subscriber_id, now(6) AS ts, domain
FROM _batch, cities;
END//
CREATE OR REPLACE PROCEDURE prune_segments(_until datetime(6)) AS
BEGIN
DELETE FROM subscriber_segments WHERE expires_at <= _until;
END //
CREATE OR REPLACE PROCEDURE run_matching_process(_interval enum('second','minute','hour','day','week','month')) RETURNS BIGINT(20) AS
DECLARE
_ts DATETIME = NOW(6);
_count BIGINT;
BEGIN
INSERT INTO notifications SELECT _ts, * FROM match_offers_to_subscribers(_interval);
_count = row_count();
INSERT INTO subscribers_last_notification
SELECT city_id, subscriber_id, ts
FROM notifications
WHERE ts = _ts
ON DUPLICATE KEY UPDATE last_notification = _ts;
RETURN _count;
END //
CREATE OR REPLACE PROCEDURE update_segments(_since DATETIME(6), _until DATETIME(6)) AS
BEGIN
INSERT INTO subscriber_segments
SELECT * FROM dynamic_subscriber_segments(_since, _until)
ON DUPLICATE KEY UPDATE expires_at = VALUES(expires_at);
END //
CREATE OR REPLACE PROCEDURE update_sessions(_session_id TEXT, _lease_duration_sections INT(11)) AS
DECLARE
_num_alive_controllers QUERY(c INT) =
SELECT COUNT(*) FROM sessions
WHERE is_controller AND expires_at > NOW(6);
_num_transactions QUERY(i INT) = SELECT @@trancount;
BEGIN
INSERT INTO sessions
SET
session_id = _session_id,
expires_at = NOW() + INTERVAL _lease_duration_sections SECOND
ON DUPLICATE KEY UPDATE expires_at = VALUES(expires_at);
START TRANSACTION;
IF SCALAR(_num_alive_controllers) = 0 THEN
UPDATE sessions
SET is_controller = (session_id = _session_id);
END IF;
ECHO SELECT
session_id, is_controller, expires_at
FROM sessions
WHERE session_id = _session_id;
COMMIT;
DELETE FROM sessions
WHERE NOW(6) > (expires_at + INTERVAL (_lease_duration_sections * 2) SECOND);
EXCEPTION
WHEN OTHERS THEN
IF SCALAR(_num_transactions) > 0 THEN
ROLLBACK;
END IF;
END //
DELIMITER ;

Last modified: October 10, 2024

Was this article helpful?

Verification instructions

Note: You must install cosign to verify the authenticity of the SingleStore file.

Use the following steps to verify the authenticity of singlestoredb-server, singlestoredb-toolbox, singlestoredb-studio, and singlestore-client SingleStore files that have been downloaded.

You may perform the following steps on any computer that can run cosign, such as the main deployment host of the cluster.

  1. (Optional) Run the following command to view the associated signature files.

    curl undefined
  2. Download the signature file from the SingleStore release server.

    • Option 1: Click the Download Signature button next to the SingleStore file.

    • Option 2: Copy and paste the following URL into the address bar of your browser and save the signature file.

    • Option 3: Run the following command to download the signature file.

      curl -O undefined
  3. After the signature file has been downloaded, run the following command to verify the authenticity of the SingleStore file.

    echo -n undefined |
    cosign verify-blob --certificate-oidc-issuer https://oidc.eks.us-east-1.amazonaws.com/id/CCDCDBA1379A5596AB5B2E46DCA385BC \
    --certificate-identity https://kubernetes.io/namespaces/freya-production/serviceaccounts/job-worker \
    --bundle undefined \
    --new-bundle-format -
    Verified OK