Lua: an extension programming language
http://www.lua.org/
Lua is a powerful, fast, lightweight, embeddable scripting
language. Lua combines simple procedural syntax with powerful
data description constructs based on associative arrays and
extensible
semantics.
Lua
is
dynamically
typed,
runs
by
interpreting bytecode for a register-based virtual machine, and has automatic memory
management with incremental garbage collection, making it ideal for configuration,
scripting, and rapid prototyping.
Lua is an extension programming language designed to support general procedural
programming with data description facilities. It also offers good support for object-oriented
programming, functional programming, and data-driven programming.
Installation
nzLua are installed in the directory /nz/extensions/nz/nzlua/bin
export PATH=”$PATH:/nz/extensions/nz/nzlua/bin”
nzcm -i nzlua
nzcm -r nzlua -d inza
[nz@netezza examples]$ nzcm
-i nzlua
There is nothing to install.
[nz@netezza examples]$ nzcm -r nzlua -d inza
1
Cartridge 'nzlua' is already registered in 'INZA'.
[nz@netezza examples]$
Using nzLua
The nzl command in the /nz/extensions/nz/nzlua/bin directory is used to compile and install
nzLua scripts. This section details the Unix and database permissions required to use the nzl
program, as well as other options available with the nzl program.
Compiling and installing an nzLua script is very simple. The /nz/extensions/nz/nzlua/examples
directory has many sample nzLua programs that can be used as learning aids as well as
verification
that nzLua has been installed correctly
cd /nz/extensions/nz/nzlua/examples
nzl nzlua_version.nzl
nzsql -c "select nzlua_version()"
[nz@netezza examples]$ nzl nzlua_version.nzl
Compiling: nzlua_version.nzl
####################################################################
UdxName
=
nzlua_version
UdxType
=
UDF
Arguments
=
Result
=
VARCHAR(10)
Dependencies =
Comment
INZA.INZA.LIBNZLUA_3_0_1
= Returns nzLua version information
NZUDXCOMPILE OPTIONS: (--nullcall --unfenced --mem 2m)
CREATE FUNCTION
COMMENT
[nz@netezza examples]$ nzsql -c "select nzlua_version ()"
2
NZLUA_VERSION
--------------3.0.1
(1 row)
Compiling and Installing a Program
Compiling and installing an nzLua script is very simple. The /nz/extensions/nz/nzlua/examples
directory has many sample nzLua programs that can be used as learning aids as well as
verification that nzLua has been installed correctly
Here is an example of compiling the nzlua_version.nzl script and then using the nzlua_version()
UDF it creates. The installation directory always includes the version number of nzLua that has
been installed. In this case the directory shown is for nzLua 2.0.0.
cd /nz/extensions/nz/nzlua/examples
nzl nzlua_version.nzl
nzsql -c "select nzlua_version()"
[nz@netezza ~]$ nzsql -c "select nzlua_version()"
NZLUA_VERSION
--------------3.0.1
(1 row)
[nz@netezza ~]$
The nzlua Command Line Program
In the example below, the nzlua command line program was invoked using the - argument
(nzlua - <<-END). When passing a script to the nzlua command line program using a HERE
document in a shell script, the - option must be used so that nzlua will abort and return an
error code back to the shell script if an error occurs. If the - option is not used, nzlua will
operate in interactive mode and therefore will not exit when an error occurs.
3
[nz@netezza kangcs]$ cat nzlua1.sh
nzlua - <<-END
print("Hello world!")
for i=1,10 do
print( "i = " || i )
end
END
[nz@netezza kangcs]$ ./nzlua1.sh
Hello world!
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i = 10
[nz@netezza kangcs]$
The nzlua Table Function
The nzlua table function is created during the install process in the INZA database. This
function allows arbitrary nzLua code to be executed inside of the Netezza appliance by an
external application directly from a SQL statement. This function is very useful for developing
and testing code as well as for ETL scripting.
LABDB.ADMIN(ADMIN)=>
select *
from table(inza..nzlua('
function processRow(x)
4
t={}
for i=1,x do
t[i] = { i, i*i }
end
return t
end
function getShape()
columns={}
columns[1] = { "x", integer }
columns[2] = { "x_squared", integer }
return columns
end',
11));
X
| X_SQUARED
----+----------1|
1
2|
4
3|
9
4|
16
5|
25
6|
36
7|
49
8|
64
9|
81
10 |
100
11 |
121
(11 rows)
System Views
5
_v_library
LABDB.ADMIN(ADMIN)=> select library from _v_library
LABDB.ADMIN(ADMIN)-> ;
LIBRARY
---------BOTANLIB
(1 row)
_v_function
LABDB.ADMIN(ADMIN)=> select function, dependencies, arguments, returns
LABDB.ADMIN(ADMIN)-> from _v_function
LABDB.ADMIN(ADMIN)-> where dependencies like '%NZLUA%'
LABDB.ADMIN(ADMIN)-> order by 1;
FUNCTION|DEPENDENCIES | ARGUMENTS|RETURNS
---------------+--------------------------+------------------------------------------------+----------------------ISDATE| INZA.INZA.LIBNZLUA_3_0_1 | (CHARACTER VARYING(40), CHARACTER VARYING(40))
| BOOLEAN
ISNUMBER| INZA.INZA.LIBNZLUA_3_0_1 | (CHARACTER VARYING(ANY)) | BOOLEAN
NZLUA| INZA.INZA.LIBNZLUA_3_0_1 | ()| TABLE(ANY)
NZLUA_VERSION | INZA.INZA.LIBNZLUA_3_0_1 | ()| CHARACTER VARYING(10)
(4 rows)
Lexical Conventions
1.Lua is a case-sensitive language: and is a reserved word, but And and AND are two different,
valid names.
2.Lua is a dynamically typed language. This means that variables do not have types; only
values do. There are no type definitions in the language. All values carry their own type.
Lua provides automatic conversion between string and number values at run time.
3.Variables are places that store values. There are three kinds of variables in Lua: global
6
variables, local variables, and table fields.A single name can denote a global variable or a local
variable (or a function's formal parameter, which is a particular kin
var ::= Name
chunk ::= {statement [`;´]}
Control Structures
statement ::= while exp do block end
statement ::= repeat block until exp
statement ::= if exp then block
{elseif exp then block}
[else block]
end
nzLua Functions
In addition to the standard functions offered by Lua, nzLua provides many functions which
extend the capabilities of Lua or are designed to be more familiar to developers who know SQL.
These functions are divided into the following groups:
split_to_columns
LABDB.ADMIN(ADMIN)=> select * from testsplit;
CONTENTS
-------------------seoul;korea;AP;WW;
kang;chang;sung;
(2 rows)
LABDB.ADMIN(ADMIN)=>
select tr.* from testsplit t, table (
split_to_columns (t.contents,
';',1) ) tr;
S1
7
------kang
seoul
(2 rows)
LABDB.ADMIN(ADMIN)=>
select tr.* from testsplit t, table (
split_to_columns (t.contents,
select tr.* from testsplit t, table (
split_to_columns (t.contents,
select tr.* from testsplit t, table (
split_to_columns (t.contents,
';',2) ) tr;
S1
| S2
-------+------seoul | korea
kang | chang
(2 rows)
LABDB.ADMIN(ADMIN)=>
';',3) ) tr;
S1
| S2
| S3
-------+-------+-----seoul | korea | AP
kang | chang | sung
(2 rows)
LABDB.ADMIN(ADMIN)=>
';',4) ) tr;
S1
| S2
| S3 | S4
-------+-------+------+---kang | chang | sung |
seoul | korea | AP
| WW
split_to_rows
Compiling: split_to_rows.nzl
####################################################################
UdxName
=
split_to_rows
UdxType
=
UDTF
Arguments
=
VARCHAR(ANY),VARCHAR(ANY)
Result
= TABLE(POS INTEGER,STR VARCHAR(255))
Dependencies = INZA.INZA.LIBNZLUA_3_0_1
8
NZUDXCOMPILE OPTIONS: (--unfenced --mem 2m --version 2)
CREATE FUNCTION
[nz@netezza examples]$
LABDB.ADMIN(ADMIN)=>
select tr.* from testsplit t, table (
split_to_rows (t.contents, ';') )
tr;
POS | STR
-----+------1 | kang
2 | chang
3 | sung
4|
1 | seoul
2 | korea
3 | AP
4 | WW
5|
(9 rows)
words
nz@netezza examples]$ pwd
/nz/extensions/nz/nzlua/examples
[nz@netezza examples]$
[nz@netezza examples]$ nzl words.nzl
Compiling: words.nzl
####################################################################
UdxName
=
words
UdxType
=
UDTF
Arguments
=
VARCHAR(ANY)
Result
= TABLE(ID INTEGER,WORD VARCHAR(255))
9
Dependencies = INZA.INZA.LIBNZLUA_3_0_1
NZUDXCOMPILE OPTIONS: (--unfenced --mem 2m --version 2)
CREATE FUNCTION
[nz@netezza examples]$
LABDB.ADMIN(ADMIN)=> select * from testsplit;
CONTENTS
-------------------kang;chang;sung;
seoul;korea;AP;WW;
close your eyes
(3 rows)
LABDB.ADMIN(ADMIN)=> select tr.* from testsplit t, table ( words (t.contents) ) tr;
ID | WORD
----+------1 | kang
2 | chang
3 | sung
1 | seoul
2 | korea
3 | AP
4 | WW
1 | close
2 | your
3 | eyes
(10 rows)
LABDB.ADMIN(ADMIN)=>
10
regexp_split_rows
[nz@netezza examples]$ nzl regexp_split_rows.nzl
Compiling: regexp_split_rows.nzl
####################################################################
UdxName
=
regexp_split_rows
UdxType
=
UDTF
Arguments
=
VARCHAR(ANY),VARCHAR(ANY)
Result
=
TABLE(POS INTEGER,STR VARCHAR(255))
Dependencies =
INZA.INZA.LIBNZLUA_3_0_1
NZUDXCOMPILE OPTIONS: (--unfenced --mem 2m --version 2)
CREATE FUNCTION
[nz@netezza examples]$
LABDB.ADMIN(ADMIN)=> select * from testsplit;
CONTENTS
-------------------kang;chang;sung;
seoul;korea;AP;WW;
close your eyes
(3 rows)
LABDB.ADMIN(ADMIN)=> select tr.* from testsplit t, table ( regexp_split_rows (t.contents,';') )
tr;
POS |
STR
-----+----------------1 | close your eyes
11
1 | kang
2 | chang
3 | sung
4|
1 | seoul
2 | korea
3 | AP
4 | WW
5|
(10 rows)
unique_chars
[nz@netezza examples]$ nzl udf_example03.nzl
Compiling: udf_example03.nzl
####################################################################
UdxName
=
unique_chars
UdxType
=
UDF
Arguments
=
VARCHAR(ANY)
Result
= INTEGER
Dependencies = INZA.INZA.LIBNZLUA_3_0_1
Comment
= Third example UDF
NZUDXCOMPILE OPTIONS: (--nullcall --unfenced --mem 2m)
CREATE FUNCTION
COMMENT
[nz@netezza examples]$ vi udf_example03.nzl
[nz@netezza examples]$
LABDB.ADMIN(ADMIN)=> select unique_chars('abc123zzzz');
UNIQUE_CHARS
-------------7
(1 row)
12
LABDB.ADMIN(ADMIN)=>
Source: unique_chars
COMMENT
[nz@netezza examples]$ vi udf_example03.nzl
[nz@netezza examples]$ cat udf_example03.nzl
-- (c) Copyright IBM Corporation 2011
--- count the number of distinct letters in a string
-- USAGE: unique_chars('abc123zzzz')
-function unique_chars( str )
count = 0
chars = {}
for ch in string.gmatch(str, "." ) do
if chars[ch] == null then
chars[ch] = 1
count = count + 1
end
end
return count
end
function getType()
return udf
end
function getName()
return "unique_chars"
13
end
function getArgs()
return {{ "", varchar(any) }}
end
function getResult()
return "integer"
end
function getComment()
return "Third example UDF"
end
decode_timestamp
[nz@netezza examples]$ nzl udtf_example03.nzl
Compiling: udtf_example03.nzl
####################################################################
UdxName
=
decode_timestamp
UdxType
=
Arguments
= TIMESTAMP
Result
UDTF
= TABLE(YEAR INTEGER,MONTH INTEGER,DAY INTEGER,HOURS
INTEGER,MINUTES INTEGER,SECONDS INTEGER,MS INTEGER)
Dependencies = INZA.INZA.LIBNZLUA_3_0_1
Comment
= Third example UDTF
NZUDXCOMPILE OPTIONS: (--unfenced --mem 2m --version 2)
CREATE FUNCTION
COMMENT
[nz@netezza examples]$
LABDB.ADMIN(ADMIN)=> select * from table (decode_timestamp (current_timestamp));
YEAR | MONTH | DAY | HOURS | MINUTES | SECONDS | MS
------+-------+-----+-------+---------+---------+---2014 |
4 | 26 |
0|
37 |
18 | 0
14
(1 row)
LABDB.ADMIN(ADMIN)=>
SAMPLE : to_date(to_char(ts, 'YYYY-MM-DD'), 'YYYY-MM-DD')
-- Compile
[nz@netezza examples]$ nzl FN_GET
Compiling: FN_GET.nzl
####################################################################
UdxName
=
FN_GETE
UdxType
=
Arguments
= TIMESTAMP
Result
UDF
= TIMESTAMP
Dependencies = INZA.INZA.LIBNZLUA_3_0_1
Comment
=
FN_GETE UDF
NZUDXCOMPILE OPTIONS: (--nullcall --unfenced --mem 2m)
CREATE FUNCTION
COMMENT
-- DEMO
[nz@netezza examples]$ nzsql -c "select FN_GETE(current_timestamp)"
FN_GETE
--------------------2014-05-19 00:00:00
(1 row)
-- Source
[nz@netezza examples]$ cat FN_GET.nzl
-- (c) Copyright IBM Corporation 2011
--
15
-- add a number of days to a timestamp
-- USAGE: add_days(timestamp, days)
--- nzLua allows the primary function name to either be "evaluate" or the name
-- of the function as defined by getName()
function FN_GETE( ts )
return to_date(to_char(ts, 'YYYY-MM-DD'), 'YYYY-MM-DD')
end
function getType()
return udf
end
function getName()
return "FN_GETE"
end
function getArgs()
args = {}
args[1] = { "", timestamp }
return args
end
function getResult()
return timestamp
end
function getComment()
return "FN_GETE UDF"
end
[nz@netezza examples]$
SAMPLE : to_char(ts, 'YYYYMMDD')
-- Compile
[nz@netezza examples]$ nzl FN_GET
Compiling: FN_GET.nzl
####################################################################
UdxName
UdxType
=
=
FN_GET
UDF
16
Arguments
Result
= TIMESTAMP
= CHAR(8)
Dependencies = INZA.INZA.LIBNZLUA_3_0_1
Comment
=
FN_GET
UDF
NZUDXCOMPILE OPTIONS: (--nullcall --unfenced --mem 2m)
CREATE FUNCTION
COMMENT
-- Demo
[nz@netezza examples]$ nzsql -c "select FN_GET(current_timestamp)"
FN_GET
----------------20140519
(1 row)
-- Source
[nz@netezza examples]$ cat FN_GET.nzl
-- (c) Copyright IBM Corporation 2011
--- add a number of days to a timestamp
-- USAGE: add_days(timestamp, days)
--- nzLua allows the primary function name to either be "evaluate" or the name
-- of the function as defined by getName()
function FN_GET( ts )
return to_char(ts, 'YYYYMMDD')
end
function getType()
return udf
end
function getName()
return "FN_GET"
end
function getArgs()
args = {}
17
args[1] = { "", timestamp }
return args
end
function getResult()
return char(8)
end
function getComment()
return "FN_GET UDF"
end
[nz@netezza examples]$
SAMPLE :
-- Compile
-- FN_GET (BLOCKID , DATETIME , OPNETIME , CLOSEDTIME )
[nz@netezza examples]$ nzl FN_GET
Compiling: FN_GET.nzl
####################################################################
UdxName
=
FN_GET
UdxType
=
Arguments
= CHAR(3),TIMESTAMP,TIMESTAMP,TIMESTAMP
Result
UDF
= CHAR(10)
Dependencies = INZA.INZA.LIBNZLUA_3_0_1
Comment
=
FN_GET
UDF
NZUDXCOMPILE OPTIONS: (--nullcall --unfenced --mem 2m)
CREATE FUNCTION
COMMENT
-- Demo
[nz@netezza examples]$ nzsql -c "select FN_GET( 'CWB', now(), now(), now())"
18
FN_GET
--------------------------------20140519
(1 row)
-- Source
[nz@netezza examples]$ cat FN_GET.nzl
function FN_GET( BLOCKID , DATETIME , OPNETIME , CLOSEDTIME)
if BLOCKID == 'CWB' then
if CLOSEDTIME < DATETIME
if DATETIME < OPNETIME
then
then
return to_char(add_months( CLOSEDTIME, 1), 'YYYY-MM')||'-01'
else
return to_char(DATETIME, 'YYYYMMDD')
end
else
return to_char(DATETIME, 'YYYYMMDD')
end
else
if (CLOSEDTIME < DATETIME and (to_char(CLOSEDTIME, 'YYYYMMDD') ==
to_char(DATETIME, 'YYYYMMDD')))
then
if to_char(DATETIME, 'YYYY-MM') == to_char ( current_date, 'YYYYMM') then
return to_char(DATETIME, 'YYYYMMDD')
else
return to_char(add_months( CLOSEDTIME, 1), 'YYYY-MM')||'-01'
end
end
end
end
function getType()
return udf
end
function getName()
return "FN_GET"
end
19
function getArgs()
args={}
args[1] = { "", char(3) }
args[2] = { "", timestamp }
args[3] = { "", timestamp }
args[4] = { "", timestamp }
return args
end
function getResult()
return char(10)
end
function getComment()
return "FN_GET UDF"
end
[nz@netezza examples]$
20
21
22
© Copyright 2026 Paperzz