How to precompile regular expressions? - regex

The PostgreSQL's regular expressions are expressed as strings (text datatype),
SELECT regexp_matches('foobarbequebaz', '(bar)(beque)'::text);
so is natural to imagine it as a dynamic parameter... But no, is not possible to be dynamic... This query "failed to find conversion function from unknown to text",
SELECT regexp_matches('foobarbequebaz', (SELECT '(bar)(beque)') );
... So, we can imagine something intermediary to reuse regular expressions, as pre-compile it. It is possible with PostgreSQL v10?

The error message has nothing to do with "dynamic" values or "precompiled" values. It simply tells you that the result of (SELECT '(bar)(beque)') has an unknown data type, but regexp_matches() expects text
So you need to cast that result to text:
SELECT regexp_matches('foobarbequebaz', (SELECT '(bar)(beque)')::text );
If you want to pass the expressions from somewhere else, you can do it like this:
with list_of_expressions (expression) as (
values
('(bar)(beque)'),
('(foo)')
)
SELECT regexp_matches('foobarbequebaz', expression)
from list_of_expressions;
Of course list_of_expressions could also be a table in your database.

Related

PowerBI: Filter Using URL Query String Parameter in report

I applied advance filter in PowerBI desktop, It is working. Similarly, I want to apply "Contains" or "In" operator to filter a report using URL Query String Parameter. I want to filter with delimitated value. "eq" operator is working but "in/contains" operator is not working.
I tried like this:
lstReportFilter.Add(string.Format("&filter=Sheet1/TPId in |181|"));
I have taken reference from here: https://learn.microsoft.com/en-us/power-bi/collaborate-share/service-url-filters
Can you please correct me on what I am doing wrong here?
UPDATE:
I also tried with round bracket, but still its not working. The column have data with pipes, so I need to include pipe while filtering.
lstReportFilter.Add(string.Format("&filter=Sheet1/TPId in (|181|)"));
lstReportFilter.Add(string.Format("&filter=Sheet1/TPId in ('|181|')"));
PowerBI column data
IN Operator expects a comma seperated list of primitive values or a expression
eg:
~/Products?$filter=Name in ('Milk', 'Cheese')
~/Products?$filter=Name in RelevantProductNames
~/Products?$filter=ShipToAddress/CountryCode in MyShippers/Regions
~/Products?$filter=Name in Fully.Qualified.Namespace.MostPopularItemNames
It seems like your use case is the first example, but if you don't have multiple values you can simply use the EQ Operator
like: filter=Sheet1/TPId eq |181|
Reference:
https://learn.microsoft.com/odata/webapi/in-operator
https://learn.microsoft.com/power-bi/collaborate-share/service-url-filters

DynamoDB equivalent of SQL's key or attribute 'not in("value1", "value2"...)'

I am trying to run some dynamoDB operations with AWS.DynamoDB.DocumentClient from aws-sdk module but I am unable to find an easy solution to select items where an attribute is not equals to an array of values.
e.g
attribute <> ["value1", "value2]
This is equivalent to a simple typical SQL operation in the form of:
select * from sometable where attribute not in("value1", "value2"...);
After trying out different ScanFilter and QueryFilter following the documentation here, it seems that the AttributeValueList for NE and NOT_CONTAINS does not accept multiple values.
I wish to arrive at the results as shown below without having to define multiple 'AND' queries
I have since arrived at this solution but it seems clumsy and I would have to write logic to create the filter condition string and ExpressionAttributeValues as the filter condition is dynamic.
FilterExpression: 'answer <> :answer1 AND answer <> :answer2',
ExpressionAttributeValues : {
':answer1' : "test1",
':answer2' : "test2"
}
I have therefore, 2 questions:
Is there a better way of doing this?
Is there a length limit to the string of KeyConditionExpression? I
am very sure there is but I cannot seem to find information with
regards to this.
There is no other way to achieve what you need. If all these values have something in common, and you know it in the write time, you can insert them with some kind of prefix and create GSI where they will be a sort key. In this case you'll be able to query them by prefix in the key condition expression. Otherwise, what you've suggested is your only option.
4KB for all of the expressions combined. As described in the Expression Parameters:
Expression parameters include ProjectionExpression, ConditionExpression, UpdateExpression, and FilterExpression.
The maximum length of any expression string is 4 KB. For example, the size of the ConditionExpression a=b is 3 bytes.

SQLite Select records which contain url

I am using Sqlite.
And I have table T with three columns: ID (int), Username (Text) and Message (Text).
I need something like this:
SELECT * FROM T WHERE Message CONTAINS <URL>
Is there a way to achieve this?
I read that Sqlite has function for handling regular expressions (regexp) but it must be implemented manually. And at this point implementing REGEXP is not an option.
The REGEXP operator is a special syntax for the regexp() user function. No regexp() user function is defined by default and so use of the REGEXP operator will normally result in an error message. If an application-defined SQL function named regexp is added at run-time, then the X REGEXP Y operator will be implemented as a call to regexp(Y,X).
try this :
SELECT * FROM T WHERE Message LIKE '%URL%';

RegEx in the select for DB2

I have a table with one column having a large json object in the format below. The column datatype is VARCHAR
column1
--------
{"key":"value",....}
I'm interested in the first value of the column data
in regex I can do it by .*?:(.*),.* with group(1) giving me the value
How can i use it in the select query
Don't do that, it's bad database design. Shred the keys and values to their own table as columns, or use the XML data type. XML would work fine because you can index the structure well, and you can use XPATH queries on the data. XPATH supports regexp natively.
You can use regular expression with xQuery, you just need to call the function matches from a SQL query or a FLORW query.
This is an example of how to use regular expressions from SQL:
db2 "with val as (
select t.text
from texts t
where xmlcast(xmlquery('fn:matches(\$TEXT,''^[A-Za-z 0-9]*$'')') as integer) = 0
)
select * from val"
For more information:
http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.xml.doc/doc/xqrfnmat.html
http://angocadb2.blogspot.fr/2014/04/regular-expressions-in-db2.html
DB2 doesn't have any built in regex functionality, unfortunately. I did find an article about how to add this with libraries:
http://www.ibm.com/developerworks/data/library/techarticle/0301stolze/0301stolze.html
Without regexes, this operation would be a mess. You could make a function that goes through the string character by character to find the first value. Or, if you will need to do more than this one operation, you could make a procedure that parses the json and throws it into a table of keys/values. Neither one sounds fun, though.
In DB2 for z/OS you will have to pass the variable to XMLQUERY with the PASSING option
db2 "with val as (
select t.text
from texts t
where xmlcast(xmlquery('fn:matches($TEXT,''^[A-Za-z 0-9]*$'')'
PASSING t.text as "TEXT") as integer) = 0
)
select * from val"

Bulk inserts with Sitecore Rocks

Is it possible to do a bulk insert with Sitecore Rocks? Something along the lines of SQL's
INSERT INTO TABLE1 SELECT COL1, COL2 FROM TABLE2
If so, what is the syntax? I'd like to add an item under any other item of a given template type.
I've tried using this syntax:
insert into (
##itemname,
##templateitem,
##path,
[etc.]
)
select
'Bulk-Add-Item',
//*[##id='{B2477E15-F54E-4DA1-B09D-825FF4D13F1D}'],
Path + '/Item',
[etc.]
To this, Query Analyzer responds:
"values" expected at position 440.
Please note that I have not found a working concatenation operator. For example,
Select ##item + '/value' from //sitecore/content/home/*
just returns '/value'. I've also tried ||, &&, and CONCATENATE without success.
There is apparently a way of doing bulk updates with CSV, but doing bulk updates directly from Sitecore Query Analyzer would be very useful
Currently you cannot do bulk inserts, but it is a really nice idea. I'll see what I can do.
Regarding the concatenation operator, this following works in the Query Analyzer:
select #Text + "/Value" from /sitecore/content/Home
This returns "Welcome to Sitecore/Value".
The ##item just returns empty, because it is not a valid system attribute.