I would like to build a stored procedure that is used in a pipeline. (pipeline ingesting into a SP).
For simplicity, i have limited to only 1 input var.
I’ve tried many different combination
declare a query variable that get the value from the loop.
create a Table function that returns 1 value, but i get : Cannot call table valued function fn_inventory_fact_date_key in a scalar expression
i also tried to assign the variable inside the loop using the function.
Any help would be greatly appreciated.
CREATE PROCEDURE `sp_inventory_fact_import`(batch query
(
p_ztimestamp timestamp not NULL
)
)
RETURNS void
AS
Declare v_timestamp timestamp;
Declare v_time_id INT;
Declare v_time_of_day INT;
Declare v_date INT;
-- DECLARE t query(i INT) = SELECT time_of_day_key FROM time_of_day_dim WHERE 24hr = v_time_of_day;
-- DECLARE d query(j INT) = SELECT date_key FROM date_dim WHERE date = v_date;
-- DECLARE v_time_of_day_key int ;
-- DECLARE v_date_key int ;
BEGIN
FOR r IN collect(batch) LOOP
v_timestamp = r.p_ztimestamp;
v_date = DATE(r.p_ztimestamp);
v_time_of_day = HOUR(r.p_ztimestamp);
-- v_time_of_day_key = fn_inventory_fact_date_key(v_time_of_day) ;
-- v_date_key = fn_inventory_fact_date_key(v_date) ;
INSERT INTO inventory_fact (
date_key ,
time_of_day_key ,
ztimestamp ,
total_count
)
SELECT
fn_inventory_fact_date_key(SCALAR(v_date)),
fn_inventory_fact_timeofday_key(SCALAR(v_date)),
-- v_date_key,
-- v_time_of_day_key,
-- SCALAR(d),
-- SCALAR(t),
v_timestamp ;
I see that you also have a Support ticket open for this request. We’ll continue to assist you in the Support ticket. Feel free to post the answer here to help others, once we determine a good solution.
SCALAR() must take a query type value as an argument. In some of your code you are passing a type that is not a query to SCALAR().
SCALAR() cannot appear in a SQL SELECT statement. You need to call it in an assignment statement, typically.
TVFs can be called in the FROM clause of a query, not on the right of an assignment.
It’s a best practice to use the datetime type and not the timestamp type inside SP logic. Timestamp is a special type to be reserved only for columns that are to be updated with a new time value automatically when a row is updated–and even that is subjects to change. See the docs on data types for a warning.
Hi Hanson,
I tried to use TVF and it did not work either. As for the timestamp, that’s how the data is stored in the files in S3. In data warehouse, it’s common practice to store timestamp UTC in order to be able to convert to different timezones.
This is the latest i have and I still get the following error :
ERROR_MESSAGE: QUERY variable argument to the SCALAR builtin returned zero rows
It looks like the SP is unable to pass v_time_of_day or v_date from the Loop.
Declare v_time_of_day INT;
Declare v_date INT;
DECLARE t query(i INT) = SELECT time_of_day_key FROM time_of_day_dim WHERE 24hr = v_time_of_day;
DECLARE d query(j INT) = SELECT date_key FROM date_dim WHERE date = v_date;
DECLARE v_time_of_day_key int ;
DECLARE v_date_key int ;
BEGIN
FOR r IN collect(batch) LOOP
v_timestamp = r.p_ztimestamp;
v_date = DATE(r.p_ztimestamp);
v_time_of_day = HOUR(r.p_ztimestamp);
v_fw_pod_number = r.p_fw_pod_number ;
.. redacted
v_date_key = SCALAR(d);
v_time_of_day_key = SCALAR(t);
INSERT INTO inventory_fact (
date_key ,
time_of_day_key ,
ztimestamp ,
... redacted
)
SELECT
v_date_key,
v_time_of_day_key,
v_timestamp,