CREATE TABLE nation (
    n_nationkey int NOT NULL,
    n_name character(25) NOT NULL,
    n_regionkey int NOT NULL,
    n_comment character varying(152) NOT NULL
);

CREATE TABLE region (
    r_regionkey int NOT NULL,
    r_name character(25) NOT NULL,
    r_comment character varying(152) NOT NULL
);

CREATE TABLE part (
    p_partkey int NOT NULL,
    p_name character varying(55) NOT NULL,
    p_mfgr character(25) NOT NULL,
    p_brand character(10) NOT NULL,
    p_type character varying(25) NOT NULL,
    p_size int NOT NULL,
    p_container character(10) NOT NULL,
    p_retailprice numeric(13,2) NOT NULL,
    p_comment character varying(23) NOT NULL
);

CREATE TABLE supplier (
    s_suppkey int NOT NULL,
    s_name character(25) NOT NULL,
    s_address character varying(40) NOT NULL,
    s_nationkey int NOT NULL,
    s_phone character(15) NOT NULL,
    s_acctbal numeric(13,2) NOT NULL,
    s_comment character varying(101) NOT NULL
);

CREATE TABLE partsupp (
    ps_partkey int NOT NULL,
    ps_suppkey int NOT NULL,
    ps_availqty int NOT NULL,
    ps_supplycost numeric(13,2) NOT NULL,
    ps_comment character varying(199) NOT NULL
);

CREATE TABLE customer (
    c_custkey int NOT NULL,
    c_name character varying(25) NOT NULL,
    c_address character varying(40) NOT NULL,
    c_nationkey int NOT NULL,
    c_phone character(15) NOT NULL,
    c_acctbal numeric(13,2) NOT NULL,
    c_mktsegment character(10) NOT NULL,
    c_comment character varying(117) NOT NULL
);

CREATE OR REPLACE FUNCTION create_parts(name TEXT, num INT)
RETURNS VOID AS $$
DECLARE
   counter INTEGER := 0 ;
BEGIN
 FOR counter IN 0..num-1  LOOP
 EXECUTE format('CREATE  TABLE %1$s_prt_%3$s PARTITION OF %1$s FOR VALUES WITH (MODULUS %2$s, REMAINDER %3$s)', name, num, counter);
 END LOOP ;
END ;
$$ LANGUAGE plpgsql;


CREATE  TABLE orders (
    o_orderkey bigint NOT NULL,
    o_custkey int NOT NULL,
    o_orderstatus character(1) NOT NULL,
    o_totalprice numeric(13,2) NOT NULL,
    o_orderdate date NOT NULL,
    o_orderpriority character(15) NOT NULL,
    o_clerk character(15) NOT NULL,
    o_shippriority int NOT NULL,
    o_comment character varying(79) NOT NULL
) PARTITION BY HASH (o_orderkey);

SELECT create_parts('orders', 32);

CREATE  TABLE lineitem (
    l_orderkey bigint NOT NULL,
    l_partkey int NOT NULL,
    l_suppkey int NOT NULL,
    l_linenumber int NOT NULL,
    l_quantity numeric(13,2) NOT NULL,
    l_extendedprice numeric(13,2) NOT NULL,
    l_discount numeric(13,2) NOT NULL,
    l_tax numeric(13,2) NOT NULL,
    l_returnflag character(1) NOT NULL,
    l_linestatus character(1) NOT NULL,
    l_shipdate date NOT NULL,
    l_commitdate date NOT NULL,
    l_receiptdate date NOT NULL,
    l_shipinstruct character(25) NOT NULL,
    l_shipmode character(10) NOT NULL,
    l_comment character varying(44) NOT NULL
) PARTITION BY HASH (l_orderkey);

SELECT create_parts('lineitem', 32);
