Skip to content

Commit

Permalink
Merge branch 'main' into import_upper_optimizer_for_merge_into
Browse files Browse the repository at this point in the history
  • Loading branch information
JackTan25 authored Dec 15, 2023
2 parents 805efcd + 8f4a558 commit 7bf55f3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 48 deletions.
7 changes: 3 additions & 4 deletions src/query/sql/src/planner/plans/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,11 @@ impl UpdatePlan {
}
}

let mut right = right.ok_or_else(|| ErrorCode::Internal("It's a bug"))?;
let right_data_type = right.data_type()?;
let right = right.ok_or_else(|| ErrorCode::Internal("It's a bug"))?;

// corner case: for merge into, if target_table's fields are not null, when after bind_join, it will
// change into nullable, so we need to cast this.
right = wrap_cast_scalar(&right, &right_data_type, target_type)?;
// change into nullable, so we need to cast this. but we will do cast after all macthed clauses,please
// see `cast_data_type_for_merge()`.

ScalarExpr::FunctionCall(FunctionCall {
span: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1062,5 +1062,32 @@ select * from null_target order by a,b;
1 a3
2 a2

## issue#13972
statement ok
create table tt1_(a bool not null, b int not null, c int not null);

statement ok
insert into tt1_ values(true, 10, 11),(false, 20, 21);

query TT
MERGE INTO tt1_ USING
(SELECT
657 AS cc0,
658 AS cc1
) AS tRIA7K(cc0, cc1) ON (
cc0 < cc1)
WHEN MATCHED AND FALSE THEN UPDATE SET
a = FALSE,
b = 332366211
WHEN MATCHED AND a THEN DELETE
WHEN NOT MATCHED AND TRUE THEN INSERT (b, c) VALUES(10, 20);
----
0 0 1

query TTT
select * from tt1_;
----
0 20 21

statement ok
set enable_experimental_merge_into = 0;
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
---CSV
"a""b",1.0,"['a""b']","{""k"":""v""}","2044-05-06 10:25:02.868894",10.01,"{'k1':10,'k2':20}","('a',5)"
\N,2.0,"['a''b']","[1]","2044-05-06 10:25:02.868894",-10.01,"{}","('b',10)"
SUCCESS
null
---TSV
a"b 1.0 ['a"b'] {"k":"v"} 2044-05-06 10:25:02.868894 10.01 {'k1':10,'k2':20} ('a',5)
\N 2.0 ['a''b'] [1] 2044-05-06 10:25:02.868894 -10.01 {} ('b',10)
SUCCESS
null
---NDJSON
{"a":"a\"b","b":1.0,"c":["a\"b"],"d":{"k":"v"},"e":"2044-05-06 10:25:02.868894","f":10.01,"g":{"k1":10,"k2":20},"h":{"1":"a","2":5}}
{"a":null,"b":2.0,"c":["a'b"],"d":[1],"e":"2044-05-06 10:25:02.868894","f":-10.01,"g":{},"h":{"1":"b","2":10}}
SUCCESS
null
29 changes: 16 additions & 13 deletions tests/suites/1_stateful/05_formats/05_00_01_load_unload_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,28 @@ insert_data() {

test_format() {
echo "---${1}"
rm -rf /tmp/test_load_unload
mkdir /tmp/test_load_unload
echo "drop stage if exists s1" | $BENDSQL_CLIENT_CONNECT
echo "create stage s1 url='fs:///tmp/test_load_unload/'" | $BENDSQL_CLIENT_CONNECT
echo "truncate table test_load_unload" | $BENDSQL_CLIENT_CONNECT
insert_data
rm -f /tmp/test_load_unload2.txt /tmp/test_load_unload.txt

curl -s -u root: -XPOST "http://localhost:${QUERY_CLICKHOUSE_HTTP_HANDLER_PORT}/?enable_clickhouse_handler=1" \
-d "select * from test_load_unload FORMAT ${1}" > /tmp/test_load_unload.txt
# insert
insert_data

cat /tmp/test_load_unload.txt
# unload1
echo "copy into @s1/unload1/ from test_load_unload file_format=(type=${1})" | $BENDSQL_CLIENT_CONNECT
mv `ls /tmp/test_load_unload/unload1/*` /tmp/test_load_unload/unload1.txt
cat /tmp/test_load_unload/unload1.txt

# load unload1
echo "truncate table test_load_unload" | $BENDSQL_CLIENT_CONNECT
echo "copy into test_load_unload from @s1/unload1.txt file_format=(type=${1}) force=true" | $BENDSQL_CLIENT_CONNECT

curl -sH "insert_sql:insert into test_load_unload file_format = (type = ${1})" \
-F "upload=@/tmp/test_load_unload.txt" \
-u root: -XPUT "http://localhost:${QUERY_HTTP_HANDLER_PORT}/v1/streaming_load" | jq -r '.state, .error'

curl -s -u root: -XPOST "http://localhost:${QUERY_CLICKHOUSE_HTTP_HANDLER_PORT}/?enable_clickhouse_handler=1" \
-d "select * from test_load_unload FORMAT ${1}" > /tmp/test_load_unload2.txt

diff /tmp/test_load_unload2.txt /tmp/test_load_unload.txt
# unload2
echo "copy into @s1/unload2/ from test_load_unload file_format=(type=${1})" | $BENDSQL_CLIENT_CONNECT
mv `ls /tmp/test_load_unload/unload2/*` /tmp/test_load_unload/unload2.txt
diff /tmp/test_load_unload/unload1.txt /tmp/test_load_unload/unload2.txt
}

test_format "CSV"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,41 @@ insert_data() {
}

test_format() {
rm -rf /tmp/test_load_unload
mkdir /tmp/test_load_unload
echo "drop stage if exists s1" | $BENDSQL_CLIENT_CONNECT
echo "create stage s1 url='fs:///tmp/test_load_unload/'" | $BENDSQL_CLIENT_CONNECT
echo "truncate table test_load_unload" | $BENDSQL_CLIENT_CONNECT

# insert data
insert_data

# unload clickhouse
curl -s -u root: -XPOST "http://localhost:${QUERY_CLICKHOUSE_HTTP_HANDLER_PORT}/?enable_clickhouse_handler=1" \
-d "select * from test_load_unload FORMAT ${1}" > /tmp/test_load_unload.parquet
# unload1
echo "copy into @s1/unload1/ from test_load_unload" | $BENDSQL_CLIENT_CONNECT
mv `ls /tmp/test_load_unload/unload1/*` /tmp/test_load_unload/unload1.parquet

echo "truncate table test_load_unload" | $BENDSQL_CLIENT_CONNECT

# load streaming
curl -sH "insert_sql:insert into test_load_unload file_format = (type = ${1})" \
-F "upload=@/tmp/test_load_unload.parquet" \
-F "upload=@/tmp/test_load_unload/unload1.parquet" \
-u root: -XPUT "http://localhost:${QUERY_HTTP_HANDLER_PORT}/v1/streaming_load" | grep -c "SUCCESS"

# unload clickhouse again
curl -s -u root: -XPOST "http://localhost:${QUERY_CLICKHOUSE_HTTP_HANDLER_PORT}/?enable_clickhouse_handler=1" \
-d "select * from test_load_unload FORMAT ${1}" > /tmp/test_load_unload2.parquet
# unload2
echo "copy into @s1/unload2/ from test_load_unload" | $BENDSQL_CLIENT_CONNECT
mv `ls /tmp/test_load_unload/unload2/*` /tmp/test_load_unload/unload2.parquet

echo "truncate table test_load_unload" | $BENDSQL_CLIENT_CONNECT

# copy into table
echo "copy into test_load_unload from 'fs:///tmp/test_load_unload.parquet' file_format = (type = ${1});" | $BENDSQL_CLIENT_CONNECT

# unload clickhouse again
curl -s -u root: -XPOST "http://localhost:${QUERY_CLICKHOUSE_HTTP_HANDLER_PORT}/?enable_clickhouse_handler=1" \
-d "select * from test_load_unload FORMAT ${1}" > /tmp/test_load_unload3.parquet

# copy into stage
rm -rf /tmp/test_load_unload_fs
echo "drop stage if exists data_fs;" | $BENDSQL_CLIENT_CONNECT
echo "create stage data_fs url = 'fs:///tmp/test_load_unload_fs/' FILE_FORMAT = (type = ${1});" | $BENDSQL_CLIENT_CONNECT
echo "copy into @data_fs from test_load_unload file_format = (type = ${1});" | $BENDSQL_CLIENT_CONNECT
# load with copy into table
echo "copy into test_load_unload from @s1/unload1.parquet force=true;" | $BENDSQL_CLIENT_CONNECT

# unload clickhouse again from stage
curl -s -u root: -XPOST "http://localhost:${QUERY_CLICKHOUSE_HTTP_HANDLER_PORT}/?enable_clickhouse_handler=1" \
-d "select * from @data_fs FORMAT ${1}" > /tmp/test_load_unload4.parquet
# unload3
echo "copy into @s1/unload3/ from test_load_unload" | $BENDSQL_CLIENT_CONNECT
mv `ls /tmp/test_load_unload/unload3/*` /tmp/test_load_unload/unload3.parquet

diff /tmp/test_load_unload2.parquet /tmp/test_load_unload.parquet
diff /tmp/test_load_unload3.parquet /tmp/test_load_unload.parquet
diff /tmp/test_load_unload4.parquet /tmp/test_load_unload.parquet
diff /tmp/test_load_unload/unload2.parquet /tmp/test_load_unload/unload1.parquet
diff /tmp/test_load_unload/unload3.parquet /tmp/test_load_unload/unload1.parquet
echo "truncate table test_load_unload" | $BENDSQL_CLIENT_CONNECT
}

Expand Down

0 comments on commit 7bf55f3

Please sign in to comment.