Apache Drill - SQL Operations

Apache Drill is an open-source SQL-On-Everything engine. It allows SQL queries to be executed on any kind of data source, ranging from a simple CSV file to an advanced SQL and NoSQL database servers.
To execute a query in a Drill shell, open your terminal move to the Drill installed directory and then type the following command.
$ bin/drill-embedded

Then you will see the response as shown in the following program.
0: jdbc:drill:zk = local>

Now you can execute your queries. Otherwise you can run your queries through web console application to the url of http://localhost:8047. If you need any help info type the following command.
$ 0: jdbc:drill:zk = local> !help

Apache Drill supports the following list of data types.
BIGINT
8-byte signed integer in the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
BINARY
Variable-length byte string
BOOLEAN
True or false
DATE
Years, months, and days in YYYY-MM-DD format since 4713 BC
DECIMAL(p,s), DEC(p,s), or NUMERIC(p,s)*
38-digit precision number, precision is p, and scale is s
INTEGER or INT
4-byte signed integer in the range -2,147,483,648 to 2,147,483,647
INTERVAL
A day-time or year-month interval
SMALLINT
2-byte signed integer in the range -32,768 to 32,767
FLOAT
4-byte floating point number
DOUBLE
8-byte floating point number
TIME
24-hour based time in hours, minutes, seconds format: HH:mm:ss
TIMESTAMP
JDBC timestamp in year, month, date hour, minute, second, and optional milliseconds format: yyyy-MM-dd HH:mm:ss.SSS
CHARACTER VARYING, CHAR or VARCHAR
variable-length string. The default limit is 1 character. The maximum character limit is 2,147,483,647.
Let us continue with simple examples on the data types.
Apache Drill supports time functions in the range from 1971 to 2037. The processing logic of data types can be easily tested by “VALUES()” statement. The following query returns date, time and timestamp for the given values.
0: jdbc:drill:zk = local> select DATE '2016-04-07',
   TIME '12:12:23',TIMESTAMP '2016-04-07 12:12:23' from (values(1));
+-------------+-----------+------------------------+
|   EXPR$0    |  EXPR$1   |         EXPR$2         |
+-------------+-----------+------------------------+
| 2016-04-07  | 12:12:23  | 2016-04-07 12:12:23.0  |
+-------------+-----------+------------------------+

The INTERVALYEAR and INTERVALDAY internal types represent a period of time.
The INTERVALYEAR type specifies values from a year to a month.
The INTERVALDAY type specifies values from a day to seconds.
0: jdbc:drill:zk = local> select timestamp '2016-04-07 12:45:50' +
   interval '10' year(2) from (values(1));
+------------------------+
|         EXPR$0         |
+------------------------+
| 2026-04-07 12:45:50.0  |
+———————————————————————-+
1 row selected (0.251 seconds)

In the above query, INTERVAL keyword followed by 10 adds 10 years to the timestamp. The 2 in parentheses in YEAR(2) specifies the precision of the year interval, 2 digits in this case to support the ten interval.
0: jdbc:drill:zk = local> select timestamp '2016-04-07 12:45:52' +
   interval '1' day(1) from (values(1));
+------------------------+
|         EXPR$0         |
+------------------------+
| 2016-04-08 12:45:52.0  |
+———————————————————————-+

Here INTERVAL ‘1’ indicates that two days will be added from that specified day.
The following operators are used in Apache Drill to perform the desired operations.
Logical Operators
AND, BETWEEN, IN , LIKE , NOT , OR
Comparison Operators
<, > , <= , >= , = , <> , IS NULL , IS NOT NULL , IS FALSE , IS NOT FALSE , IS TRUE , IS NOT TRUE,
Pattern Matching Operator - LIKE
Math Operators
+,-,*,/
Subquery Operators
EXISTS, IN
Apache Drill scalar functions supports Math and Trig functions. Most scalar functions use data types such as INTEGER, BIGINT, FLOAT and DOUBLE.
The following table describes the list of “Math functions” in Apache Drill.
ABS(x)
Returns the absolute value of the input argument x.
CBRT(x)
Returns the cubic root of x.
CEIL(x)
Returns the smallest integer not less than x.
CEILING(x)
Same as CEIL.
DEGREES(x)
Converts x radians to degrees.
EXP(x)
Returns e (Euler's number) to the power of x.
FLOOR(x)
Returns the largest integer not greater than x.
LOG(x, y)
Returns log base x to the y power.
Now let’s run queries for the scalar functions. The Drill scalar functions can be easily tested by the values() statement, otherwise you can also use the select statement.
Apache Drill supports the following trig functions and these functions’ return type is a floating point value.
SIN(x)
Sine of angle x in radians
ACOS(x)
Inverse cosine of angle x in radians
COSH()
Hyperbolic cosine of hyperbolic angle x in radians
In Apache Drill, you can cast or convert data to the required type for moving data from one data source to another. Drill also supports the following functions for casting and converting data types −
CAST(x AS y)
CONVERT_TO(x,y)
CONVERT_FROM(x,y)
Apache Drill supports time functions based on the Gregorian calendar and in the range from 1971 to 2037. The following table describes the list of Date/Time functions.
AGE(x [, y ] )
INTERVALDAY
or
INTERVALYEAR
CURRENT_DATE
CURRENT_TIME
CURRENT_TIMESTAMP
DATE_SUB(x,y)
EXTRACT(x FROM y)
Extracts a time unit from a date or timestamp expression (y).
This must be one of the following values: SECOND, MINUTE, HOUR, DAY, MONTH, and YEAR.
LOCALTIME
LOCALTIMESTAMP
NOW()
TIMEOFDAY()
Apache Drill supports the following list of string functions.
BYTE_SUBSTR(x,y [, z ] )
CHAR_LENGTH(x)
CONCAT(x,y)
INITCAP(x)
LENGTH(x)
LOWER(x)
LPAD(x,y [ , z ] )
The value of x is filled in the front (the left-hand side) with the value of z until the total length of the value is equal y’s length.
If no z value then blanks are used to fill the position.
LTRIM(x)
POSITION( x IN y)
REGEXP_REPLACE(x,y,z)
RPAD(x,y,z)
RTRIM(x)
STRPOS(x,y)
SUBSTR(x,y,z)
UPPER(x)
Apache Drill supports the following list of null handling functions.
COALESCE(x, y [ , y ]... )
NULLIF(x,y )
The following table describes the list of “Math functions” in Apache Drill.
E()
Returns 2.718281828459045.
LOG(x)
Returns the natural log (base e) of x.
LOG10(x)
Returns the common log of x.
LSHIFT(x, y)
Shifts the binary x by y times to the left.
MOD(x, y)
Returns the remainder of x divided by y.
NEGATIVE(x)
Returns x as a negative number.
PI
Returns pi.
POW(x, y)
Returns the value of x to the y power.
RADIANS
Converts x degrees to radians.
RAND
Returns a random number from 0-1.
ROUND(x)
Rounds to the nearest integer.
ROUND(x, y)
Rounds x to y decimal places. Return type is decimal.
RSHIFT(x, y)
Shifts the binary x by y times to the right.
SIGN(x)
Returns the sign of x.
SQRT(x)
Returns the square root of x.
TRUNC(x)
Apache Drill supports the following trig functions and these functions’ return type is a floating point value.
COS(x)
Cosine of angle x in radians
TAN(x)
Tangent of angle x in radians
ASIN(x)
Inverse sine of angle x in radians
ATAN(x)
Inverse tangent of angle x in radians
SINH()
Hyperbolic sine of hyperbolic angle x in radians
TANH()
Hyperbolic tangent of hyperbolic angle x in radians
The following table describes the list of Date/Time functions.
DATE_ADD(x,y)
Returns the sum of the sum of a date/time and a number of days/hours, or of a date/time and date/time interval.
Where,
x - date, time or timestamp
y - integer or an interval expression.
DATE_PART(x,y)
Returns a field of a date, time, timestamp, or interval.
where,
x - year, month, day, hour, minute, or second
y - date, time, timestamp, or interval literal
UNIX_TIMESTAMP ( [ x] )
If x is specified as timestamp then the number of seconds since the UNIX epoch and the timestamp x is returned.
If x is not specified then it returns the number of seconds since the UNIX epoch (January 1, 1970 at 00:00:00).

    46 Lectures 
     3.5 hours 

 Arnab Chakraborty

    23 Lectures 
     1.5 hours 

 Mukund Kumar Mishra

    16 Lectures 
     1 hours 

 Nilay Mehta

    52 Lectures 
     1.5 hours 

 Bigdata Engineer

    14 Lectures 
     1 hours 

 Bigdata Engineer

    23 Lectures 
     1 hours 

 Bigdata Engineer
 Print
 Add Notes
 Bookmark this page