kusto query language -passing parameter value to user defined function - azure-data-explorer

I am trying to send the output from table recordtime value to Function1 as a parameter value. but i am getting below error for Function1()
semantic error: SEM0100: 'toscalar' operator: Failed to resolve scalar expression named 'recordtime'.
I tried with recordtime as passing as tostring() and todatetime()
Function definitions:
Function1(fromdate string,recordtime string)
Function2(fromdate string,enddatetime string)
Main (startdate,enddatetime,id)
Function Main (startdate,enddatetime,id){
TestTable
| where (fromdate >= datetime(startdate)
and todate <= datetime(enddatetime))
and deviceid == id
| summarize cnt = count(), recordtime = Timestamp
| extend getdates = case (
cnt == 1,toscalar(Function1(startdate,recordtime)),
cnt == 2,toscalar(Function2(startdate,enddatetime )),
"Out of range"
)
| Project getdates
}
Function1 and Function2 returns single array like ([{"fromdate":"2020-03-11T16:39:47.6730000Z"},{"todate":"2020-03-11T16:44:23.8800000Z"}]
any suggestions,
Thanks in advance

Function1 is a tabular function and therefore can't be called in the middle of a query in that way.
If Function1 "functionally returns a scalar", then move the toscalar() inside the Function1, so you can remove toscalar when you call it and you can call that function on a query column.

Related

Display a message under certain criteria instead of results in Kusto

In Kusto, I want to display a message to the user depending on certain criteria. For example
isempty(['_tenant'])
| print "Note: ", "You must select a tenant"
else???
Events
| where tenant == ['_tenant']
| ...
The criteria is different for each query, as well as the message.
A different way to do it is to do a union where each leg of the union is mutually exclusive. The catch is that a function must return a consistent schema regardless of input. So you'll end up with both a Status column and an x column in this example.
let myFunc = (y:long) {
union
(
print Status = "Y must be greater than 0"
| where y > 0
),
(
range x from 1 to 10 step 1
| where y <= 0
)
};
myFunc(-1)
It sounds like you might be looking for the assert() function: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/assert-function
let checkLength = (len:long, s:string)
{
assert(len > 0, "Length must be greater than zero") and
strlen(s) > len
};
datatable(input:string)
[
'123',
'4567'
]
| where checkLength(len=long(-1), input)

table counterpart of column_ifexists()

We do have a function column_ifexists() which refers to a certain column if it exists, otherwise it refers to another option if we provide. Is there a similar function for table? I want to refer to a table and run some logic against it in the query , if the table exists , but if it doesn't exist, there shouldn't be a failure -- it should simply return no data.
e.g.
table_ifexists('sometable') | ...<logic>...
Please note that the fields referenced in the query should be defined in the dummy table, otherwise in case of non-existing table, the query will yield an exception
Failed to resolve scalar expression named '...'
In the following example these fields are StartTime, EndTime & EventType
Table exists
let requested_table = "StormEvents";
let dummy_table = datatable (StartTime:datetime, EndTime:datetime, EventType:string)[];
union isfuzzy=true table(requested_table), dummy_table
| where EndTime - StartTime > 30d
| summarize count() by EventType
EventType
count_
Drought
1635
Flood
20
Heat
14
Wildfire
4
Fiddle
Table does not exist
let requested_table = "StormEventsXXX";
let dummy_table = datatable (StartTime:datetime, EndTime:datetime, EventType:string)[];
union isfuzzy=true table(requested_table), dummy_table
| where EndTime - StartTime > 30d
| summarize count() by EventType
EventType
count_
Fiddle

Snowflake, Recursive CTE , Getting error String 'AAAA_50>BBBB_47>CCCC_92' is too long and would be truncated in 'CONCAT'

I am creating a recursive CTE in snowflake for getting complete path an getting following error:
String 'AAAA_50>BBBB_47>CCCC_92' is too long and would be truncated in 'CONCAT'
My script is as follows: (it works fine for 2 levels, starts failing for 3rd level)
with recursive plant
(child_col,parent_col,val )
as
(
select child_col, '' parent_col , trim(child_col) from My_view
where condition1 = 'AAA'
union all
select A.child_col,A.parent_col,
concat(trim(A.child_col),'>')||trim(val)
from My_view A
JOIN plant as B ON trim(B.child_col) = trim(A.parent_col)
)
select distinct * from plant
Most likely the child_col data type is defined as VARCHAR (N), this type is being passed on. Because CONCAT Returns:
The data type of the returned value is the same as the data type of
the input value(s).
Try to explicitly cast a type to a string like this cast(trim(child_col) as string):
Full code:
with recursive plant (child_col,parent_col,val )
as (
select child_col, '' parent_col , cast(trim(child_col) as string)
from My_view
where condition1 = 'AAA'
union all
select A.child_col, A.parent_col, concat(trim(A.child_col),'>')||trim(val)
from My_view A
join plant as B ON trim(B.child_col) = trim(A.parent_col)
)
select distinct * from plant
Remember that recursion in Snowflake is limited to 100 loops by default.
If you want to increase them, you need to contact support.
Reference: CONCAT Troubleshooting a Recursive CTE

SQLITE Select results into a string

I am looking for a method to return the results of an SQLite query as a single string for use in an internal trigger.
Something like Python's 'somestring'.join() method.
Table: foo
id | name
1 | "foo"
2 | "bar"
3 | "bro"
Then a select statement:
MAGIC_STRING_CONCAT_FUNCTION(SELECT id FROM foo,",");
To return
"1,2,3"
You're looking for the group_concat function:
group_concat((SELECT id FROM foo), ",");
The following is the description of the function group_concat from the documentation:
group_concat(X) group_concat(X,Y)
The group_concat() function returns a string which is the
concatenation of all non-NULL values of X. If parameter Y is present
then it is used as the separator between instances of X. A comma (",")
is used as the separator if Y is omitted. The order of the
concatenated elements is arbitrary.

Linq-to-Sql count() returning 0 even if there is row returning

It is not very clear to me why I am not able to get the number of rows returned using linq-to-sql
I had this query to use as validation:
var obj1 = (from c in context.sistema_DocType_Index
where c.indexId == id
select c).First();
if(obj1 != null) {}
I was getting a null exception using First() method if there was no row returned. Ok, so I decided to use Count().
var obj1 = (from c in context.sistema_DocType_Index
where c.indexId == id
select c).Count();
if(obj1 > 0) {}
I have 3 rows returning from the database but the Count() gives me 0.
Why is that?
you can use Any();method. which is the best use for this case. Any()
if( YourDataCollection.Any(SomeCOndtion==SOmeValue))
{
// do some logic
}

Resources