Is there a REGEXP_LIKE equivalent function in Apache Drill? - apache-drill

Anything in Drill which can be similar to:
REGEXP_LIKE ( <COLUMN>, <REGULAR_EXPRESSION< ) RETURNS BOOLEAN ?

The following code should mimic REGEXP_LIKE in drill. Just make sure to replace '1' with a text string that does not occur in the column and that the regular expression captures the entire string (by appending .*) .
SELECT CASE WHEN REGEXP_REPLACE(<COLUMN>, <REGULAR_EXPRESSION>,'1') = '1' THEN true ELSE false END

REGEXP_LIKE is not supported as per drill docs.
If you can proceed with wildcard, you can check ILIKE.
ILIKE ( <COLUMN>, <VALUE> ) RETURNS BOOLEAN
Sample command:
SELECT * FROM employee WHERE ILIKE(name, '%DEV%')"
You can ask the community if somebody has written any UDF for this.
Or you can write your own UDF for this function. Check drill docs for details of writing a UDF.

Related

Select * from Select

Its very weird situation I know, nut I have got myself into it somehow. I have to connect to some other system service by passing some parameters in url.
In their service they are creating some query using parameter I pass.
For my case I have to pass 'Select' as a parameter name which is actually some class name on their side. So they end up in creating query as Select * from select
and some condition.
On execution I am getting error response as:
'There was a syntax error in a SQL query or filter expression at line
1, position 186. Saw \"Select\" but expected
'..SQL: \"SELECT col1, col2 FROM Select AS D where
some condition.
Can somebody help me on this.
Since Select is reserved word, you have to escape it by enclosing in backticks characters in order for MySQL to process your query:
select * from `select`
Its recommended not to use MySQL reserved keywords.. but if its necessary there is a solution..
Use this, it will work for you :
select * from yourdatabasename.select

Apache Drill Supports Stored Procedure & Function

I want to know whether Apache Drill Supports Stored Procedure and Functions or not.? If Yes Please give some example.
You can use built in functions :
Math and Trig:
functions like ABS(x),
CEIL(x),
CEILING(x),
DEGREES(x),
EXP(x),
FLOOR(x),
LOG(x) & many more. Check docs.
Example:
SELECT ABS(`integer`) FROM dfs.`/Users/drill/input2.json`;
Data Type Conversion:
functions like CAST
CONVERT_TO and CONVERT_FROM, etc. Check docs.
Example:
SELECT CAST('1' as DECIMAL(28, 2)) FROM (VALUES(1));
Date/Time Functions and Arithmetic:
functions like CURRENT_TIME TIME, CURRENT_TIMESTAMP, DATE_ADD, etc. Check docs.
Example:
SELECT DATE_ADD(date '2015-05-15', 2) FROM (VALUES(1));
String Manipulation:
functions like CONCAT, ILIKE, INITCAP, LENGTH, etc. Check docs.
Example:
SELECT CHAR_LENGTH('Drill rocks') FROM (VALUES(1));
Aggregate and Aggregate Statistical:
functions like AVG(expression), MAX(expression), COUNT(*), etc. Check docs.
Example:
SELECT AVG(ALL salary) FROM cp.`employee.json` WHERE employee_id IN (1139, 1140, 1141);
Functions for Handling Nulls:
COALESCE & NULLIF. Check docs.
Example:
SELECT NULLIF(d9, d18) FROM alltypes limit 1;
SQL Window Functions are also supported. Check Details.
Nested Data Functions are there to FLATTEN, Find Repeated counts,etc. Check Details.
Query Directory Functions like MAXDIR, MINDIR. Check Details.
Apart from that, you can develop your own function.
Check Tutorials.

MySql explode/in_array functionalilty

In my table I have a field with data such as 1,61,34, and I need to see if a variable is in that.
So far I have this
SELECT id, name FROM siv_forms WHERE LOCATE(TheVariable, siteIds) > 0
Which works, with the exception that if the siteIds were 2,61,53, and TheVariable was 1, it would return the row as there is a 1 in 61. Is there anyway around this using native MySql, or would I need to just loop the results in PHP and filter the siteIds that way?
I've looked through the list of string functions in MySql and can't see anything that would do what I'm after.
Try with find_in_set function.
SELECT id, name FROM siv_forms WHERE find_in_set(TheVariable, siteIds);
Check Manual for find_in_set function.

Dynamic Values in 'IN' operator

I have a select statement in which the WHERE clause's IN operator. The query works properly as long as some values are passed to question mark (passed from java program). But when there are no values passed, I get a syntax error.
select
this_.categoryAddressMapId as category1_1_0_,
this_.categoryId as categoryId1_0_,
this_.addressId as addressId1_0_
from
icapcheckmyphotos.category_address_map this_ <br>
where
this_.addressId in (
?
)
When there are no parameters passed, I need null set. Please suggest how to modify the where clause. Thanks in advance
Modify your java program. Your choices are to not run the query if there are no addressIds, or ensure that it passes at least one value. If addressId is an integer field, pass -1 or something like that. Personally, I like the first option but it depends on your requirements.
how about doing sometiong like
where (? is null) OR this_.addressId in ( ? )
you may need to add some special treatment to the first part of the OR if your columns does accept NULLS
//pseudo code
...
WHERE 1
if(!null) {
AND this_.addressId in ('stuff')
}

NVL2 function does not exist? mysql query

I'm trying to do some queries but I keep getting errors, now I'm thinking that there is something wrong with the mysql installation. Can anybody tell me if there is an error in this query?
SELECT settings.ID,
settings.name,
settings.description,
NVL2(userSettings.value, userSettings.value, settings.default)
FROM settings
LEFT OUTER JOIN userSettings ON (settings.ID = userSettings.settingID)
The error I get says the function databaseX.NVL2 does not exist
I recommend staying away from vendor specific functions when a ANSI standard equivalent alternative is available. NVL and IFNULL for example can (often) be replaced with COALESCE.
You can also use CASE WHEN, which means a lot more typing on the downside, but the upside is that people with background in SQL Server for example won't have to deal with Oracles DECODE() or NVL or NVL2, because the logic is right there in the code.
That's probably because NVL2 is an Oracle function, not a MySQL function. I believe the function you are looking for in MySQL would be COALESCE()
As #Eric Petroelje mentioned NVL2() is Oracle function, not MySQL. However MySQL has its own equivalent that can be used in this case: IFNULL():
SELECT ... IFNULL(userSettings.value, settings.default) ...
After several try&error probes, I found this method to emulate Oracle's NVL2 function. It's not very elegant, but it works
SELECT IF(LENGTH(ISNULL(FieldName, '')) > 0, 'Not NULL Value', 'Null Value') FROM TableName
I think this can help you
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3. IF() returns a numeric or string value, depending on the context in which it is used.
Alternatively you can substitute NVL2 to:
IF (userSettings.value IS NULL, userSettings.value, settings.default)