QueryFilter
Filters query rows specified in filter criteria This BIF will invoke the callback function for each row in the query, passing the row as a struct.
If the callback returns true, the row will be included in the new query.
If the callback returns false, the row will be excluded from the new query.
If the callback requires strict arguments, it will only receive the row as a struct.
If the callback does not require strict arguments, it will receive the row as a struct, the row number (1-based), and the query itself.
Parallel Execution
If the parallel
argument is set to true, and no max_threads
are sent, the filter will be executed in parallel using a ForkJoinPool with parallel streams.
If max_threads
is specified, it will create a new ForkJoinPool with the specified number of threads to run the filter in parallel, and destroy it after the operation is complete.
Please note that this may not be the most efficient way to filter, as it will create a new ForkJoinPool for each invocation of the BIF. You may want to consider using a shared ForkJoinPool for better performance.
Method Signature
QueryFilter(query=[query], callback=[function:Predicate], parallel=[boolean], maxThreads=[integer])
Arguments
query
query
true
The query to get filtered
callback
function:Predicate
true
The function to invoke for each item. The function will be passed 3 arguments: the query row as a struct, the row number, the query. You can alternatively pass a Java Predicate which will only receive the 1st arg.
parallel
boolean
false
Whether to run the filter in parallel. Defaults to false. If true, the filter will be run in parallel using a ForkJoinPool.
false
maxThreads
integer
false
The maximum number of threads to use when running the filter in parallel. If not passed it will use the default number of threads for the ForkJoinPool. If parallel is false, this argument is ignored.
Examples
Filter a query
news = queryNew( "id,type,title", "integer,varchar,varchar" );
queryAddRow( news, [
{
ID : 1,
TYPE : "book",
TITLE : "Cloud Atlas"
},
{
ID : 2,
TYPE : "book",
TITLE : "Lord of The Rings"
},
{
ID : 3,
TYPE : "film",
TITLE : "Men in Black"
}
] );
books = queryFilter( news, ( Any _news ) => {
return _news.TYPE == "book";
} );
writeDump( queryColumnData( books, "title" ).toList( ", " ) );
Result: Cloud Atlas, Lord of The Rings
Filter a query as member function
news = queryNew( "id,type,title", "integer,varchar,varchar" );
queryAddRow( news, [
{
ID : 1,
TYPE : "book",
TITLE : "Cloud Atlas"
},
{
ID : 2,
TYPE : "book",
TITLE : "Lord of The Rings"
},
{
ID : 3,
TYPE : "film",
TITLE : "Men in Black"
}
] );
books = news.filter( ( Any _news ) => {
return _news.TYPE == "book";
} );
writeDump( queryColumnData( books, "title" ).toList( ", " ) );
Result: Cloud Atlas, Lord of The Rings
Additional Examples
people = QueryNew( "name,dob", "varchar,date", [
[
"Susi",
CreateDate( 1970, 1, 1 ),
0
],
[
"Urs",
CreateDate( 1995, 1, 1 ),
0
],
[
"Fred",
CreateDate( 1960, 1, 1 ),
0
],
[
"Jim",
CreateDate( 1988, 1, 1 ),
0
],
[
"Bob",
CreateDate( 1988, 1, 1 ),
0
]
] );
Dump( var=people, label="people - original query" );
/* Output:
*
* | name | dob |
* ------------------------------
* | Susi | 1970-01-01 00:00:00 |
* | Urs | 1995-01-01 00:00:00 |
* | Fred | 1960-01-01 00:00:00 |
* | Jim | 1988-01-01 00:00:00 |
* | Bob | 1988-01-01 00:00:00 |
*/
// filter - born in 1988
qryPeopleBornIn1988 = people.filter( ( Any row, Any rowNumber, Any qryData ) => {
return Year( row.DOB ) == 1988;
} );
dump( var=qryPeopleBornIn1988, label="qryPeopleBornIn1988 - Born in 1988" );
/* Output:
*
* | name | dob |
* ------------------------------
* | Jim | 1988-01-01 00:00:00 |
* | Bob | 1988-01-01 00:00:00 |
*/
<bx:script>
q = QueryNew( "name, description" );
bx:loop times=3 {
getFunctionList().each( ( Any f ) => {
var fd = getFunctionData( arguments.F );
var r = QueryAddRow( q );
QuerySetCell( q, "name", fd.NAME, r );
QuerySetCell( q, "description", fd.DESCRIPTION, r );
} );
}
dump( var=q.RECORDCOUNT, label="demo data set size" );
s = "the";
</bx:script>
<bx:timer type="outline" label="Query of Query">
<bx:query dbtype="query" name="q1">
select name, description
from q
where description like <bx:queryparam value="%#s#%" sqltype="varchar">
</bx:query>
</bx:timer>
<bx:dump var="#q1.RECORDCOUNT#">
<bx:timer type="outline" label="query.filter() with scoped variables">
<bx:script>
q2 = q.filter( ( Any row ) => {
return (arguments.ROW.DESCRIPTION contains s);
} );
</bx:script>
</bx:timer>
<bx:dump var="#q2.RECORDCOUNT#">
<bx:timer type="outline" label="query.filter() without unscoped variables">
<bx:script>
q3 = q.filter( ( Any row ) => {
return (row.DESCRIPTION contains s);
} );
</bx:script>
</bx:timer>
<bx:dump var="#q3.RECORDCOUNT#">
Related
Last updated
Was this helpful?