Apache Hive : HPL/SQL Reference

11 documents

Apache Hive : HPL/SQL Reference

Hive Hybrid Procedural SQL On Hadoop (HPL/SQL) is a tool that implements procedural SQL for Hive. It is available in Hive 2.0.0 (HIVE-11055).

HPL/SQL is an open source tool (Apache License 2.0) that implements procedural SQL language for Apache Hive, SparkSQL, Impala as well as any other SQL-on-Hadoop implementation, any NoSQL and any RDBMS.

HPL/SQL is a hybrid and heterogeneous language that understands syntaxes and semantics of almost any existing procedural SQL dialect, and you can use with any database, for example, running existing Oracle PL/SQL code on Apache Hive and Microsoft SQL Server, or running Transact-SQL on Oracle, Cloudera Impala or Amazon Redshift.

HPL/SQL language is compatible to a large extent with Oracle PL/SQL, ANSI/ISO SQL/PSM (IBM DB2, MySQL, Teradata i.e), PostgreSQL PL/pgSQL (Netezza), Transact-SQL (Microsoft SQL Server and Sybase) that allows you leveraging existing SQL/DWH skills and familiar approach to implement data warehouse solutions on Hadoop. It also facilitates migration of existing business logic to Hadoop.

HPL/SQL is an efficient way to implement ETL processes in Hadoop.

Initially it was provided as a stand alone command line tool. As a part of (HIVE-24230) HPL/SQL has been re-architected to an integrated part of HiveServer (HS2). From a JDBC client, such as Beeline, you connect to HiveServer. The interpreter executes the abstract syntax tree (AST) from the parser. Hive metastore securely stores the function and procedure code permanently. The procedure is loaded and cached on demand to the interpreter’s memory when needed. You can close the session or restart HiveServer without losing the definitions.

Getting Started

User Guide

Language Elements

Operators

Attributes and Built-in Variables

NameDescription
ACTIVITY_COUNTNumber of rows affected by last SQL statement
HOSTCODEReturn code of the last OS command
SQLCODEReturn code of the last SQL statement
SQLSTATEReturn status of the last SQL statement

Statements

StatementDescription
ALLOCATE CURSORAllocate cursor for procedure result set
ASSOCIATE RESULT SET LOCATORDefine locators for procedure result sets
BREAKExit a loop
CALLExecute a stored procedure
CLOSEClose a cursor
CMPCompare data in tables
COPYCopy data between tables and files
COPY FROM FTPCopy FTP files to Hadoop compatible file system
COPY FROM LOCALCopy local files to Hadoop compatible file system
CREATE DATABASECreate a database
CREATE FUNCTIONCreate a user-defined SQL function
CREATE LOCAL TEMPORARY TABLECreate a session-level temporary table
CREATE PACKAGECreate a program package
CREATE PROCEDURECreate a user-defined SQL procedure
CREATE TABLECreate a table
CREATE VOLATILE TABLECreate a session-level temporary table
DECLAREDeclare a variable
DECLARE CONDITIONDeclare a condition
DECLARE CURSORDeclare a cursor
DECLARE HANDLERDeclare a condition handler
DECLARE TEMPORARY TABLEDeclare a temporary table
DESCRIBEDescribe a database object
DROP DATABASEDrop a database
DROP TABLEDrop a table
EXEC / EXECUTE / EXECUTE IMMEDIATEExecute a dynamic SQL statement or procedure
EXIT WHENExit a loop
FETCHFetch the next row from a cursor
FOR cursorFOR statement (Cursor loop)
FOR rangeFOR statement (Integer range)
GET DIAGNOSTICSGet execution information
HOSTExecute an OS command or run an external process
IFIF statement
INCLUDEInclude statements from another script
INSERTINSERT statement
INSERT DIRECTORYWrite query results to a file
LEAVEExit a loop
OPENOpen a cursor
LOOPUnconditional loop
MAP OBJECTMap object name to a connection profile
NULLNo operation (no-op) statement
PRINTPrint a line
RESIGNALResignal the exception
RETURNReturn from a routine
SELECTSELECT statement
SELECT INTOAssign values from a query
SIGNALRaise a condition or exception
SETAssign a value to a variable
SET Session OptionSet a session option
TRUNCATETruncate a table
UPDATEUPDATE statement
USEChange the default database
VALUES INTOAssign a value to a variable
WHILEWhile loop

Functions

FunctionDescription
CASTConvert to data type
CHARConvert to string
COALESCEReturn first non-NULL value
CONCATConcatenate two or more strings
CURRENT_DATEGet the current date (year, month and day)
CURRENT_TIMESTAMPGet the current date and time
CURRENT_USERGet the current user name
DATEConvert to date
DECODEImplement IF-THEN-ELSE logic
DBMS_OUTPUT.PUT_LINEPrint a line
FROM_UNIXTIMEConvert number of seconds since 1970-01-01 to timestamp
INSTRFind index of substring in a string
LOWERConvert a string to lower case
LENString length excluding trailing blanks
LENGTHString length
MAX_PART_STRINGGet max partition value (string)
MIN_PART_STRINGGet min partition value (string)
MAX_PART_INTGet max partition value (int)
MIN_PART_INTGet min partition value (int)
MAX_PART_DATEGet max partition value (date)
MIN_PART_DATEGet min partition value (date)
NOWGet the current date and time
NVLReturn first non-NULL value
NVL2If 1st operand is null return 3rd otherwise 2nd operand
PART_LOCGet the location of a partition
PART_COUNTGet the number of partitions
PART_COUNT_BYGet the number of partitions (group by)
REPLACEReplace a string
SUBSTRReturn a substring
SUBSTRINGReturn a substring
SYSDATEGet the current date and time
TIMESTAMP_ISOConvert string to timestamp
TO_CHARConvert to string
TO_TIMESTAMPConvert string to timestamp
TRIMRemove leading and trailing characters
UNIX_TIMESTAMPGet the current date and time in seconds since 1970-01-01
UPPERConvert a string to upper case
USERGet the current user name

Commands

CommandDescription
! cmd;Execute an OS command or run an external process

File I/O Operations

Compatibility

Apache Hive : HPL/SQL - Get Started

Quick guide how to start using HPL/SQL.

You can enable and use HPL/SQL from any host or third-party tool that can make a JDBC connection to HiveServer. Beeline is a popular client for use with HPL/SQL because other third-party tools do not show you some of the error messages about syntax mistakes.

Enabling HPL/SQL in the beeline connection string

After setting up a client to connect to HiveServer, you append mode=hplsql to the JDBC URL that connects the client to HiveServer.

Apache Hive : HPL/SQL - Key Features

HPL/SQL key features:

  • Flow of Control Statements (FOR, WHILE, IF, CASE, LOOP, LEAVE, RETURN)
  • Functions, procedures, and packages
  • Built-in functions (string manipulations, datetime functions, conversions)
  • Exception handling and conditions
  • Constants and variable, assignment (DECLARE count INT := 1)
  • Processing results using a CURSOR
  • On-the-fly SQL Conversion
  • UDF to run HPL/SQL scripts from Hive queries
(SELECT hplsql('mycustomfunc(:1)', name) FROM users;)

Apache Hive : Why HPL/SQL

The role of Hadoop in Data Warehousing is huge. But to implement comprehensive ETL, reporting, analytics and data mining processes you not only need distributed processing engines such as MapReduce, Spark or Tez, you also need a way to express comprehensive business rules.

1. Business Logic Driver and Advanced Error Handling

HPL/SQL allows you to implement business logic using variables, expressions, flow-of-control statements and iterations. HPL/SQL supports error handling using exceptions and condition handlers. You can develop programs that manages and controls distributed processes but at the same time is not a bottleneck of the system.

Functions

Aug 12, 2026

Operators

Aug 12, 2026