[HUDI-1659] Basic Implement Of Spark Sql Support For Hoodie (#2645)
Main functions: Support create table for hoodie. Support CTAS. Support Insert for hoodie. Including dynamic partition and static partition insert. Support MergeInto for hoodie. Support DELETE Support UPDATE Both support spark2 & spark3 based on DataSourceV1. Main changes: Add sql parser for spark2. Add HoodieAnalysis for sql resolve and logical plan rewrite. Add commands implementation for CREATE TABLE、INSERT、MERGE INTO & CTAS. In order to push down the update&insert logical to the HoodieRecordPayload for MergeInto, I make same change to the HoodieWriteHandler and other related classes. 1、Add the inputSchema for parser the incoming record. This is because the inputSchema for MergeInto is different from writeSchema as there are some transforms in the update& insert expression. 2、Add WRITE_SCHEMA to HoodieWriteConfig to pass the write schema for merge into. 3、Pass properties to HoodieRecordPayload#getInsertValue to pass the insert expression and table schema. Verify this pull request Add TestCreateTable for test create hoodie tables and CTAS. Add TestInsertTable for test insert hoodie tables. Add TestMergeIntoTable for test merge hoodie tables. Add TestUpdateTable for test update hoodie tables. Add TestDeleteTable for test delete hoodie tables. Add TestSqlStatement for test supported ddl/dml currently.
This commit is contained in:
@@ -0,0 +1,255 @@
|
||||
|
||||
# SET OPTION
|
||||
set hoodie.insert.shuffle.parallelism = 1;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
|
||||
set hoodie.upsert.shuffle.parallelism = 1;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
|
||||
set hoodie.delete.shuffle.parallelism = 1;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
|
||||
|
||||
# CTAS
|
||||
|
||||
create table h0 using hudi options(type = '${tableType}')
|
||||
as select 1 as id, 'a1' as name, 10 as price;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
|
||||
select id, name, price from h0;
|
||||
+-----------+
|
||||
| 1 a1 10 |
|
||||
+-----------+
|
||||
|
||||
create table h0_p using hudi partitioned by(dt)
|
||||
options(type = '${tableType}')
|
||||
as select cast('2021-05-07 00:00:00' as timestamp) as dt,
|
||||
1 as id, 'a1' as name, 10 as price;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
|
||||
select id, name, price, cast(dt as string) from h0_p;
|
||||
+--------------------------------+
|
||||
| 1 a1 10 2021-05-07 00:00:00 |
|
||||
+--------------------------------+
|
||||
|
||||
|
||||
# CREATE TABLE
|
||||
|
||||
create table h1 (
|
||||
id bigint,
|
||||
name string,
|
||||
price double,
|
||||
ts bigint
|
||||
) using hudi
|
||||
options (
|
||||
type = '${tableType}',
|
||||
primaryKey = 'id',
|
||||
preCombineField = 'ts'
|
||||
)
|
||||
location '${tmpDir}/h1';
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
|
||||
create table h1_p (
|
||||
id bigint,
|
||||
name string,
|
||||
price double,
|
||||
ts bigint,
|
||||
dt string
|
||||
) using hudi
|
||||
partitioned by (dt)
|
||||
options (
|
||||
type = '${tableType}',
|
||||
primaryKey = 'id',
|
||||
preCombineField = 'ts'
|
||||
)
|
||||
location '${tmpDir}/h1_p';
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
|
||||
|
||||
# INSERT/UPDATE/MERGE/DELETE
|
||||
|
||||
insert into h1 values(1, 'a1', 10, 1000);
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
insert into h1 values(2, 'a2', 11, 1000);
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
|
||||
# insert static partition
|
||||
insert into h1_p partition(dt = '2021-05-07') select * from h1;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
select id, name, price, ts, dt from h1_p order by id;
|
||||
+---------------------------+
|
||||
| 1 a1 10.0 1000 2021-05-07 |
|
||||
| 2 a2 11.0 1000 2021-05-07 |
|
||||
+---------------------------+
|
||||
|
||||
# insert overwrite table
|
||||
insert overwrite table h1_p partition(dt = '2021-05-07') select * from h1 limit 10;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
select id, name, price, ts, dt from h1_p order by id;
|
||||
+---------------------------+
|
||||
| 1 a1 10.0 1000 2021-05-07 |
|
||||
| 2 a2 11.0 1000 2021-05-07 |
|
||||
+---------------------------+
|
||||
|
||||
# insert dynamic partition
|
||||
insert into h1_p
|
||||
select id, concat('a', id) as name, price, ts, dt
|
||||
from (
|
||||
select id + 2 as id, price + 2 as price, ts, '2021-05-08' as dt from h1
|
||||
)
|
||||
union all
|
||||
select 5 as id, 'a5' as name, 10 as price, 1000 as ts, '2021-05-08' as dt;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
select id, name, price, ts, dt from h1_p order by id;
|
||||
+---------------------------+
|
||||
| 1 a1 10.0 1000 2021-05-07 |
|
||||
| 2 a2 11.0 1000 2021-05-07 |
|
||||
| 3 a3 12.0 1000 2021-05-08 |
|
||||
| 4 a4 13.0 1000 2021-05-08 |
|
||||
| 5 a5 10.0 1000 2021-05-08 |
|
||||
+---------------------------+
|
||||
|
||||
# update table
|
||||
update h1_p set price = price * 2 where id % 2 = 1;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
select id, name, price, ts, dt from h1_p order by id;
|
||||
+---------------------------+
|
||||
| 1 a1 20.0 1000 2021-05-07 |
|
||||
| 2 a2 11.0 1000 2021-05-07 |
|
||||
| 3 a3 24.0 1000 2021-05-08 |
|
||||
| 4 a4 13.0 1000 2021-05-08 |
|
||||
| 5 a5 20.0 1000 2021-05-08 |
|
||||
+---------------------------+
|
||||
|
||||
update h1 set price = if (id %2 = 1, price * 2, price);
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
select id, name, price, ts from h1;
|
||||
+----------------+
|
||||
| 1 a1 20.0 1000 |
|
||||
| 2 a2 11.0 1000 |
|
||||
+----------------+
|
||||
|
||||
# delete table
|
||||
delete from h1_p where id = 5;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
select count(1) from h1_p;
|
||||
+----------+
|
||||
| 4 |
|
||||
+----------+
|
||||
|
||||
# merge into
|
||||
merge into h1_p t0
|
||||
using (
|
||||
select *, '2021-05-07' as dt from h1
|
||||
) s0
|
||||
on t0.id = s0.id
|
||||
when matched then update set id = s0.id, name = s0.name, price = s0.price *2, ts = s0.ts, dt = s0.dt
|
||||
when not matched then insert *;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
select id, name, price, ts, dt from h1_p order by id;
|
||||
+---------------------------+
|
||||
| 1 a1 40.0 1000 2021-05-07 |
|
||||
| 2 a2 22.0 1000 2021-05-07 |
|
||||
| 3 a3 24.0 1000 2021-05-08 |
|
||||
| 4 a4 13.0 1000 2021-05-08 |
|
||||
+---------------------------+
|
||||
|
||||
merge into h1_p t0
|
||||
using (
|
||||
select 5 as _id, 'a5' as _name, 10 as _price, 1000 as _ts, '2021-05-08' as dt
|
||||
) s0
|
||||
on s0._id = t0.id
|
||||
when matched then update set *
|
||||
when not matched then insert (id, name, price, ts, dt) values(_id, _name, _price, _ts, s0.dt);
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
select id, name, price, ts, dt from h1_p order by id;
|
||||
+---------------------------+
|
||||
| 1 a1 40.0 1000 2021-05-07 |
|
||||
| 2 a2 22.0 1000 2021-05-07 |
|
||||
| 3 a3 24.0 1000 2021-05-08 |
|
||||
| 4 a4 13.0 1000 2021-05-08 |
|
||||
| 5 a5 10.0 1000 2021-05-08 |
|
||||
+---------------------------+
|
||||
|
||||
merge into h1_p t0
|
||||
using (
|
||||
select 1 as id, '_delete' as name, 10 as price, 1000 as ts, '2021-05-07' as dt
|
||||
union
|
||||
select 2 as id, '_update' as name, 12 as price, 1001 as ts, '2021-05-07' as dt
|
||||
union
|
||||
select 6 as id, '_insert' as name, 10 as price, 1000 as ts, '2021-05-08' as dt
|
||||
) s0
|
||||
on s0.id = t0.id
|
||||
when matched and name = '_update'
|
||||
then update set id = s0.id, name = s0.name, price = s0.price, ts = s0.ts, dt = s0.dt
|
||||
when matched and name = '_delete' then delete
|
||||
when not matched and name = '_insert' then insert *;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
select id, name, price, ts, dt from h1_p order by id;
|
||||
+--------------------------------+
|
||||
| 2 _update 12.0 1001 2021-05-07 |
|
||||
| 3 a3 24.0 1000 2021-05-08 |
|
||||
| 4 a4 13.0 1000 2021-05-08 |
|
||||
| 5 a5 10.0 1000 2021-05-08 |
|
||||
| 6 _insert 10.0 1000 2021-05-08 |
|
||||
+--------------------------------+
|
||||
|
||||
# DROP TABLE
|
||||
drop table h0;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
|
||||
drop table h0_p;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
|
||||
drop table h1;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
|
||||
drop table h1_p;
|
||||
+----------+
|
||||
| ok |
|
||||
+----------+
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user