Getting error when using timestamp index in dask with snowflake - dask

I am trying read tables from snowflake and use merge_asof to perform point in time correct join. Here is the corresponding code:
next_failure = dd.read_sql_table('vm_next_failure', conn_url, index_col='ts')
errors = dd.read_sql_table('vm_errors', conn_url, index_col='ts')
ndf = dd.merge_asof(next_failure, errors, left_index=True, right_index='ts', by="machineid", suffixes=("_l", "_r"), allow_exact_matches=False)
Here is the error I get:
ProgrammingError: (snowflake.connector.errors.ProgrammingError) 252004: Failed processing pyformat-parameters: 255001: Binding data in type (timestamp) is not supported.
[SQL: SELECT vm_errors.ts, vm_errors.machineid, vm_errors.error1, vm_errors.error2, vm_errors.error3, vm_errors.error4, vm_errors.error5
FROM vm_errors
WHERE vm_errors.ts >= %(ts_1)s AND vm_errors.ts <= %(ts_2)s]
[parameters: {'ts_1': Timestamp('2015-01-01 06:00:00'), 'ts_2': Timestamp('2016-01-01 05:00:00')}]
(Background on this error at: http://sqlalche.me/e/13/f405)
Any thoughts on how to workaround this?

Looks like you need to replace Timestamp with TO_Timestamp.
Please run the following query in the snowflake:
select TO_Timestamp('2016-01-01 05:00:00')

Related

Snowflake Procedure (Assigning a value to the Variable in Task)

Would you please tell me how to assign a value to the variable on the Task? Here's the detailed scenario of what I'm trying to accomplish and what the error is.
To get the latest query_id of the below SQL statement, I am creating the following function.
CREATE OR REPLACE FUNCTION rejected_records_queryid()
RETURNS varchar
AS 'select query_id from snowflake.account_usage.query_history where QUERY_TEXT LIKE \'select 1, 2%\' AND EXECUTION_STATUS=\'SUCCESS\' order by START_TIME desc limit 1';
If I run this manually, the function works perfectly,
set qid = rejected_records_queryid();
Session variable value:
select $qid;
Please refer attached screenshot for the output.
Pic1
However, if I use the Task, the return values of the function will not be assigned to the session variable,
CREATE OR REPLACE TASK rejected_records_queryid1
WAREHOUSE = COMPUTE_WH
SCHEDULE = '1 MINUTE'
AS
set qid2 = rejected_records_queryid();
Change the task mode to resume,
ALTER TASK rejected_records_queryid1 RESUME;
The following error message appears after I check the value of the qid2 after Task run,
SQL compilation error: error line 1 at position 7 Session variable '$QID2' does not exist
Attached screenshot for the error message In order to use it in the below sql, I need the qid2 value to be assigned from the Task,
select * from table(result_scan($qid2))
Pic2
I would appreciate any help on how to assign the value to the variable in TASK or any other workarounds.

Snowflake Tasks causing error in timezone in queries

I am running a simple insert query inside a stored procedure with to_timeatamp_ntz("column value") along with other columns.
This works fine when I am running it with the snowflake UI and logged in with my account.
This works fine when I am calling it using python scripts from my visual studio instance.
The same stored procedure fails when it is being called by a scheduled task.
I am thinking if it has something to do with the user's timezone of 'System' vs my time zone.
Execution error in store procedure LOAD_Data(): Failed to cast variant
value "2019-11-27T13:42:03.221Z" to TIMESTAMP_NTZ At
Statement.execute, line 24 position 57
I tried to provide timezone as session parameters in task and in the stored proc but does not seem to be addressing the issue. Any ideas?
I'm guessing (since you didn't include the SQL statement that causes the error) that you are trying to bind a Date object when creating a Statement object. That won't work.
The only parameters you can bind are numbers, strings, null, and the special SfDate object that you can only get from a result set (to my knowledge). Most other parameters must be converted to string using mydate.toJSON(), JSON.stringify(myobj), etc., before binding, eg:
var stmt = snowflake.createStatement(
{ sqlText: `SELECT :1::TIMESTAMP_LTZ NOW`, binds: [(new Date).toJSON()] }
);
Date object errors can be misleading, because Date objects causing an error can be converted and displayed as strings in the error message.
I found the issue:
my Task was using a copy paste effect similar to this:
CREATE TASK TASK_LOAD_an_sp
WAREHOUSE = COMPUTE_WH
TIMEZONE = 'US/Eastern'
SCHEDULE = 'USING CRON 0/30 * * * * America/New_York'
TIMESTAMP_INPUT_FORMAT = 'YYYY-MM-DD HH24'
AS
Call LOAD_an_sp();
The Timestamp input format was causing this.

How to execute Report given the results of a previously executed Report in ABAP

My problem is the following:
I have one report called Y5000112.
My colleagues always execute it manually once with selection screen variant V1 and then execute it a second time with variant V2 adding the results of the first execution to the selection.
Those results in this case are PERNR.
My goal:
Automate this - execute that query twice with one click and automatically fill the PERNR selection of the second execution with the PERNR results of the first execution.
I found out how to trigger a report execution and after that another one, how to set it to a certain variant and got this far - [EDIT] after the first answer I got a bit further but I still have no idea how to loop through my results and put them into the next Report submit:
DATA: t_list TYPE TABLE OF abaplist.
* lt_seltab TYPE TABLE OF rsparams,
* ls_selline LIKE LINE OF lt_seltab.
SUBMIT Y5000114
USING SELECTION-SET 'MA OPLAN TEST'
EXPORTING LIST TO MEMORY
AND RETURN.
CALL FUNCTION 'LIST_FROM_MEMORY'
TABLES
listobject = t_list
EXCEPTIONS
not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE 'Unable to get list from memory'.
ELSE.
* I want to fill ls_seltab here with all pernr (table pa0020) but I haven't got a clue how to do this
* LOOP AT t_list.
* WRITE /t_list.
* ENDLOOP.
SUBMIT Y5000114
* WITH-SELECTION-TABLE ls_seltab
USING SELECTION-SET 'MA OPLAN TEST2'
AND RETURN.
ENDIF.
P.S.
I'm not very familiar with ABAP so if I didn't provide enough Information just let me know in the comments and I'll try to find out whatever you need to know in order to solve this.
Here's my imaginary JS-Code that can express very generally what I'm trying to accomplish.
function submitAndReturnExport(Reportname,VariantName,OptionalPernrSelection)
{...return resultObject;}
var t_list = submitAndReturnExport("Y5000114","MA OPLAN TEST");
var pernrArr = [];
for (var i in t_list)
{
pernrArr.push(t_list[i]["pernr"]);
}
submitAndReturnExport("Y5000114","MA OPLAN TEST2",pernrArr);
It's not that easy as it supposed to, so there won't be any one-line snippet. There is no standard way of getting results from report. Try EXPORTING LIST TO MEMORY clause, but consider that the report may need to be adapted:
SUBMIT [report_name]
WITH SELECTION-TABLE [rspar_tab]
EXPORTING LIST TO MEMORY
AND RETURN.
The result of the above statement should be read from memory and adapted for output:
call function 'LIST_FROM_MEMORY'
TABLES
listobject = t_list
EXCEPTIONS
not_found = 1
others = 2.
if sy-subrc <> 0.
message 'Unable to get list from memory' type 'E'.
endif.
call function 'WRITE_LIST'
TABLES
listobject = t_list
EXCEPTIONS
EMPTY_LIST = 1
OTHERS = 2
.
if sy-subrc <> 0.
message 'Unable to write list' type 'E'.
endif.
Another (and more efficient approach, IMHO) is to gain access to resulting grid via class cl_salv_bs_runtime_info. See the example here
P.S. Executing the same report with different parameters which are mutually-dependent (output pars of 1st iteration = input pars for the 2nd) is definitely a bad design, and those manipulations should be done internally. As for me one'd better rethink the whole architecture of the report.

SQLite.swift joins missing information

I am trying to join a few tables in swift using the SQLite.swift library and I am having a few problems. The below code shows the issue:
I first join the tables:
let ext_tasks = tasks.join(tasktypes, on: tasktypes[tty_id] == tasks[task_type])
.join(preUsers, on: preUsers[Worker_ID] == task_ownerID)
.order(task_dueDate)
.filter(task_ownerID == iUserID)
At this point I have the tables joined and all the fields from tasks, task types and preUsers are there properly.
Following I run the second join statement:
let ext_equipment = equipment.join(equipment_type, on: equipment_type[eqt_id] == equipment[equ_type])
.join(facilities, on: facilities[fac_id] == equipment[equ_facid])
.join(eqowners, on: eqowners[Worker_ID] == equipment[equ_owner])
.order(equ_make)
.filter(equ_status >= 0)
Again, after running this statement I can access all the fields from the equipment, equipment_type, facilities and eqowners tables, up to here everything is working as expected...
However then I try to join the two above results using:
let comp_tasks = ext_tasks.join(ext_equipment, on: ext_equipment[equ_id] == ext_tasks[task_eqID])
.order(task_dueDate)
Here is where I run into a problem, it seems like the tables coming from the ext_tasks query are there but only the equipment table from the ext_equipment query is available.
Further when I try to access the "facilities"."facility_name" field, I get the following message saying that the field does not exist:
fatal error: no such column '"Facilities"."Facility_Name"' in columns:
["Equipment"."EquipmentType_ID", "Equipment"."Equipment_ID", "Equipment"."Equipment_Owner_ID", "Equipment"."Facility_ID", "Equipment"."Status", "PreUsers"."Client_ID", "PreUsers"."Date_LastLogin", "PreUsers"."First_Name", "TaskTypes"."TaskType_ID", "TaskTypes"."Type", "Tasks"."Equipment_ID", "Tasks"."Owner_ID", "Tasks"."Priority", "Tasks"."Time_Complete", "Tasks"."Time_Start"]:
(I removed some of the columns in there to shorten the answer, there are 53 fields in total shown) but as you can see only tables from the first join + the equipment table are shown, the joins from the second query (equipment_type, facilities and eqowners) are nowhere to be seen
I would appreciate if someone could let me know why this is happening, this has been killing my brain for hours and I just can't figure it out...
Thanks in advance!

Generic Parsing Error

In a 4D application, I have the following lines of code:
Begin SQL
UPDATE Keys
SET Desc = :$desc,
KeyStamp = :$key_stamp
WHERE KeyTitle = :$key_title
End SQL
When trying to run the code, the following error is displayed:
Generic parsing error. Parsing failed in or around the following
substring interval - ( 16, 23 ) - ... SET Desc = ...
Does anyone see the problem with the code? Keys is not a keyword or anything, is it?
Seems like someone forgot that Desc is a keyword for "descending." The solution is to rename the column and update all references to that column. Otherwise, the column cannot be referenced in SQL.

Resources