Antlr Mutual Left Recursion - recursion

I would like to parse something like
f()[3]()[3] ... () or []
meaning fucntions that return arrays, which can be referenced and may contain functions themselves.
I tried
functionCall: (ID | arrayReference) '(' expressionList? ')';
arrayReference: (ID | functionCall) '[' arithmeticExpression ']';
but get mutual recursion errors. Is there an easy way to solve this?

Like this:
value
: value '[' arithmeticExpression ']' # arrayReference
| value '(' expressionList? ')' # functionCall
| ID #id
;

Related

(for-each-row scenario).in kusto

Query1
cluster(x).database('$systemdb').Operations
| where Operation == "DatabaseCreate" and Database contains "oci-"| where State =='Completed'
and StartedOn between (datetime(2020-04-07) .. 3d)
| distinct Database , StartedOn
| order by StartedOn desc
Output of my query1 is list of databases , now I have to pass each db value into query2 to get buildnumber
Query2:
set query_take_max_records=5000;
let view=datatable(Property:string,Value:dynamic)[];
let viewFile=datatable(FileName:string)[];
alias database db = cluster(x).database('y');
let latestInfoFile = toscalar((
union isfuzzy=true viewFile,database('db').['TextFileLogs']
| where FileName contains "AzureStackStampInformation"
| distinct FileName
| order by FileName
| take 1));
union isfuzzy=true view,(
database('db').['TextFileLogs']
| where FileName == latestInfoFile
| distinct LineNumber,FileLineContent
| order by LineNumber asc
| summarize StampInfo=(toobject(strcat_array(makelist(FileLineContent,100000), "\r\n")))
| mvexpand bagexpansion=array StampInfo
| project Property=tostring(StampInfo[0]), Value=StampInfo[1]
)|where Property contains "StampVersion" | project BuildNumber = Value;
database() function: is a special scoping function, and it does not support non-constant arguments due to security consideration.
As a result - you cannot use sub-query to fetch list of databases and then operate on this list as input for database() function.
This behavior is described at:
https://learn.microsoft.com/en-us/azure/kusto/query/databasefunction?pivots=azuredataexplorer
Syntax
database(stringConstant)
Arguments
stringConstant: Name of the database that is referenced. Database identified can be either DatabaseName or PrettyName. Argument has to be constant prior of query execution, i.e. cannot come from sub-query evaluation.

Kusto sub query selection using toscalar - returns only last matching record

I am referring sqlcheatsheet - Nested queries
Query 1:
traces
| where customDimensions.Domain == "someDomain"
| where message contains "some-text"
| project itemId=substring(itemId,indexof(itemId,"-"),strlen(itemId))
Result :
itemId
-c580-11e9-888a-8776d3f65945
-c580-11e9-888a-8776d3f65945
-c580-11e9-9b01-c3be0f4a2bf2
Query 2:
traces
| where customDimensions.Domain == "someDomain"
| where itemId has toscalar(
traces
| where customDimensions.Domain == "someDomain"
| where message contains "some-text"
| project itemId=substring(itemId,indexof(itemId,"-"),strlen(itemId)))
Result for the second query returns records matching only last record of sub query
ie:) > -c580-11e9-9b01-c3be0f4a2bf2
Question :
How get entire result set that has matching with all the three items.
My requirement is to take entire sequence of logs for a particular request.
To get that I have below inputs, I could able to take one log, from that I can find ItemId
The itemId looks like "b5066283-c7ea-11e9-9e9b-2ff40863cba4". Rest of all logs related to this request must have "-c7ea-11e9-9e9b-2ff40863cba4" this value. Only first part will get incremented like b5066284 , b5066285, b5066286 like that.
toscalar(), as its name implies, returns a scalar value.
Given a tabular argument with N columns and M rows it'll return the value in the 1st column and the 1st row.
For example: the following will return a single value - 1
let T = datatable(a:int, b:int, c:int)
[
1,2,3,
4,5,6,
7,8,9,
]
;
print toscalar(T)
If I understand the intention in your 2nd query correctly, you should be able to achieve your requirement by using has_any.
For example:
let T = datatable(item_id:string)
[
"c580-11e9-888a-8776d3f65945",
"c580-11e9-888a-8776d3f65945",
"c580-11e9-9b01-c3be0f4a2bf2",
]
;
T
| where item_id has_any (
(
T
| parse item_id with * "-" item_id
)
)

Escaping percent signs in Lua Sqlite3 Prepare Statement

I have a statement:
(db is a sqlite3 instance)
local stmt = db:prepare("SELECT id, name FROM table WHERE name LIKE '%?%'")
if stmt:bind_values("test") == sqlite3.OK then
....
end
However, I get this error:
Incorrect number of parameters to bind (1 given, 0 to bind)
It seems it is not seeing the ? as a parameter. I have tried all kinds of escaping from %% for the percent signs to \ and \\ and beyond...driving me crazy.
Anyone know how to fix this? Thank you!
You can't put parameters inside quotes:
correct: SELECT ... WHERE foo = ?
incorrect: SELECT ... WHERE foo = '?'
With the quotes, it's just a string that contains a question mark. Without the quotes, it's a placeholder.
You'll have to build up the LIKE in pieces, e.g.
... WHERE foo LIKE '%' || ? || '%'

ANTLR failure due to left recursion. How to solve?

Here is a simple grammar for an SQL select statement
grammar SQL;
#rulecatch {
//We want to stop parsing when SyntaxException is encountered
//So we re-throw the exception
catch (SyntaxException e) {
throw e;
}
}
eval
: sql_query
;
sql_query
: select_statement from_statement
| select_statement from_statement where_statement
;
select_statement
: 'select' attribute_list
;
from_statement
: 'from' table_name
;
where_statement
: 'where' attribute_name operator constant
;
attribute_list
: '*'
| attribute_name
| attribute_name ',' attribute_list?
;
table_name
: string_literal
;
attribute_name
: string_literal
;
operator
: '=' | '!=' | '>' | '>=' | '<' | '<='
;
constant
: INTEGER
| '"' string_literal '"'
;
fragment DIGIT: '0'..'9';
INTEGER: DIGIT+ ;
fragment LETTER: 'a'..'z'|'A'..'Z';
string_literal: LETTER | LETTER string_literal;
WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel=HIDDEN;};
The Tool is nagging about the sql_query definition as well as the attribute_list definition. Honestly I do not see any left recursion in my code to start with.
Could anyone explain what's going on?
No, ANTLR did not say your grammar is left recursive. It complained about the fact that some rules have non-LL(*) decisions due to recursive rule invocations. Rewrite the following rules as follows and you'll be alright:
sql_query
: select_statement from_statement where_statement?
;
attribute_list
: '*'
| attribute_name (',' attribute_list?)?
;

How do I add opening and closing brackets to the beginning and end of the data string in Sqlite

I have a column of text in sqlite that I have to select and add brackets to the beginning and end of each string of data. EG. column has "hello" I need to select it, adding brackets so it looks like "(hello)"
I have done this before in Sqlite, but can't remember how I did it. Any ideas would be really appreciated. I am sure it will be pretty simple.
Use || to concatenate strings:
SELECT '(' || 'hello' || ')';
SELECT '(' || column_name || ')' FROM table_name WHERE condition;

Resources