Apache Drill Supports Stored Procedure & Function - apache-drill

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.

Related

Equivalent Functions for JSON/B Operators in Postgresql

I've tried to dig through the documentation and can't seem to find anything for what I'm looking for. Are there equivalent functions for the various JSON/B operators (->, ->>, #>, ?, etc) in PostgreSQL?
Edit: To clarify, I would like to know if it is possible to have the following grouped queries return the same result:
SELECT '{"foo": "bar"}'::json->>'foo'; -- 'bar'
SELECT json_get_value('{"foo": "bar"}'::json, 'foo'); -- 'bar'
SELECT '{"foo": "bar"}'::jsonb ? 'foo'; -- t
SELECT jsonb_key_exists('{"foo": "bar"}'::jsonb, 'foo'); -- t
You can use the system catalogs to discover the function equivalent to each operator.
select * from pg_catalog.pg_operator where oprname ='?';
This shows that the function is named "jsonb_exists". Some operators are overloaded and will give more than one function, you have to look at the argument types to distinguish them.
Every operator has a function 'behind' it. That function may or may not be documented in its own right.
AFAIK there are two functions which are equivalents to #> and #>> operators.
These are:
#> json_extract_path
#>> json_extract_path_text
->> json_extract_path_text -- credits: #a_horse_with_no_name
Discover more in the docs
Other than that you could extract json to a table and take values you need using regular SQL query vs a table using json_each or json_each_text.
Same thing with checking if a key exists in a JSON would be to use json_object_keys and also query the table which comes out of it.
If you need to wrap things up in a different language / using ORM then what you could do is move the data retrieval logic to a PL/SQL procedure and just execute it and obtain prepared data from it.
Obviously, you could also build your own functions that would implement the behaviour of forementioned operators, but the question is: is it really worth it? It will definitely be slower.

Is there a REGEXP_LIKE equivalent function in 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.

How to find all user-defined (not extension-related) functions?

There is a similar question, but it is ambiguous, and the accepted answer suggests that the question is slightly different from mine.
How to find user defined functions not belonging to any extension such as PostGIS? Linked question's answer provided a query that returns most of the PostGIS functions (noise for my purpose) and I didn't understand it well enough to change it to return only my functions (lack of detailed explanation why it works the way it does and how to change the settings).
Right now I have no C functions and all my functions are in schema public - you can use this fact, but make it clear how to release these constraints. If exact list of extensions is important, assume just PostGIS for now, but explain how to add others to the list if it is not self-evident from the code.
As commented by #Craig, dependencies are stored in pg_catalog.pg_depend.
The query can look like this (Postgres 11 or later):
SELECT p.proname AS function_name
, pg_get_function_identity_arguments(p.oid) AS parameter_list
, pg_get_functiondef(p.oid) AS function_def -- CREATE FUNCTION statement
FROM pg_proc p
LEFT JOIN pg_depend d ON d.objid = p.oid
AND d.deptype = 'e' -- would depend on extension
WHERE p.pronamespace = 'public'::regnamespace -- your schema(s) of interest
AND d.objid IS NULL -- no such dependency
AND p.prokind = 'f'; -- only plain functions
This excludes all functions depending on an extension from the result. The manual about the dependency type deptype = 'e':
DEPENDENCY_EXTENSION (e)
The dependent object is a member of the extension that is the
referenced object (see pg_extension). The dependent object can be
dropped only via DROP EXTENSION on the referenced object. Functionally
this dependency type acts the same as an internal dependency, but it's
kept separate for clarity and to simplify pg_dump.
And p.prokind = 'f' restricts the result to plain functions. The manual:
f for a normal function, p for a procedure, a for an aggregate function, or w for a window function
That's new in Postgres 11. For Postgres 10 or older use instead:
SELECT ...
...
AND NOT proisagg -- no aggregate functions
AND NOT proiswindow -- no window functions
There were no procedures, yet.
Find pg_get_function_identity_arguments() and pg_get_functiondef() in the manual here. Related:
How to get function parameter lists (so I can drop a function)

How can I create a LINQ2SQL query that doesn't use MILLISECOND in the call to DATEDIFF?

By default if you compare two dates in a LINQ2SQL query, the resulting SQL will be
DATEDIFF(MILLISECOND, .....)
which requires using BIGINT as well and usually some CONVERT calls depending on what you're doing. As an example, try looking at the SQL output if you write
(DateTime1 - DateTime2).Days
It's a mess!
I would just like to call DATEDIFF(DAY, ...) for example. Is this possible?
It is possible! It turns out there are a few nice methods located in System.Data.Linq.SqlClient that do exactly this.
DateDiffDay
DateDiffMinute
DateDiffMillisecond
.....
The resulting call to DATEDIFF will contain the appropriate first parameter and you can forget about all the calls to CONVERT.

MySQL Date and Time functions don't exist

I have installed WampServer 2.0 with MySQL 5.1.33.
I can do Numeric and String functions like
SELECT ABS(-2)orSELECT LOWER('ASD')
but with Date and Time Functions such as
SELECT CURDATE()orSELECT NOW()
I get
Error : no such function: CURDATE
Am I doing something wrong, is there anything I need to install?
Any help about where to start investigating?
There is no error message from MySQL with the text "No such function." I just did a grep on the whole source tree of MySQL 5.1, and that string does not occur anywhere (except in one comment).
My thought is that you aren't using MySQL, you're using SQLite. Because I can reproduce that error when I run the SQLite command-line shell:
$ sqlite3
sqlite> select curdate();
Error: no such function: curdate
sqlite> select now();
Error: no such function: now
In SQLite, the function to get the current date is simply date():
sqlite> select date();
2010-01-02
Many functions are different in SQlite and in MySQL (or any other product - if you except core functions, most functions provide the same functionality but with a different syntax). There is an open-source "compatibility library" implementing a large number of MySQL functions in SQLite on the Kansas State University CIS website
My inclination is to run a mysql repair install. if that didn't work, then i'd try to wamp reinst.
If SQLite had the same features as SQL, it would be amazing. But fortunately for query types with date comparisons it is possible to use existing SQLite functions.
An example of how I was able to carry out my query was as follows:
Using an interface type class declaring my ObjectDAO: 'PartidoDAO'.
#Query("select * from partido where fpartido>=DATE() and epartido = 'Santiago Bernabeu' ORDER BY fpartido LIMIT 1")
Partido getMoreRecentPartido();
If you want more information about functions related to date data types consult the web I invite you to consult this resource.
SQLite Date Functions.