Skip to content

Commit 510a6da

Browse files
authored
feat: support star "*" for merge into (#12906)
* add test first * support * and add more tests * fix clippy * fix check * fix check
1 parent fdbbcea commit 510a6da

File tree

6 files changed

+327
-97
lines changed

6 files changed

+327
-97
lines changed

src/query/ast/src/ast/statements/merge_into.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ impl Display for MergeUpdateExpr {
5252

5353
#[derive(Debug, Clone, PartialEq)]
5454
pub enum MatchOperation {
55-
Update { update_list: Vec<MergeUpdateExpr> },
55+
Update {
56+
update_list: Vec<MergeUpdateExpr>,
57+
is_star: bool,
58+
},
5659
Delete,
5760
}
5861

@@ -66,6 +69,7 @@ pub struct MatchedClause {
6669
pub struct InsertOperation {
6770
pub columns: Option<Vec<Identifier>>,
6871
pub values: Vec<Expr>,
72+
pub is_star: bool,
6973
}
7074

7175
#[derive(Debug, Clone, PartialEq)]
@@ -116,9 +120,16 @@ impl Display for MergeIntoStmt {
116120
write!(f, " THEN ")?;
117121

118122
match &match_clause.operation {
119-
MatchOperation::Update { update_list } => {
120-
write!(f, " UPDATE SET ")?;
121-
write_comma_separated_list(f, update_list)?;
123+
MatchOperation::Update {
124+
update_list,
125+
is_star,
126+
} => {
127+
if *is_star {
128+
write!(f, " UPDATE * ")?;
129+
} else {
130+
write!(f, " UPDATE SET ")?;
131+
write_comma_separated_list(f, update_list)?;
132+
}
122133
}
123134
MatchOperation::Delete => {
124135
write!(f, " DELETE ")?;

src/query/ast/src/parser/statement.rs

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,40 +2267,75 @@ fn match_operation(i: Input) -> IResult<MatchOperation> {
22672267
rule! {
22682268
UPDATE ~ SET ~ ^#comma_separated_list1(merge_update_expr)
22692269
},
2270-
|(_, _, update_list)| MatchOperation::Update { update_list },
2270+
|(_, _, update_list)| MatchOperation::Update {
2271+
update_list,
2272+
is_star: false,
2273+
},
2274+
),
2275+
map(
2276+
rule! {
2277+
UPDATE ~ "*"
2278+
},
2279+
|(_, _)| MatchOperation::Update {
2280+
update_list: Vec::new(),
2281+
is_star: true,
2282+
},
22712283
),
22722284
))(i)
22732285
}
22742286

22752287
pub fn unmatch_clause(i: Input) -> IResult<MergeOption> {
2276-
map(
2277-
rule! {
2278-
WHEN ~ NOT ~ MATCHED ~ (AND ~ ^#expr)? ~ THEN ~ INSERT ~ ( "(" ~ ^#comma_separated_list1(ident) ~ ^")" )?
2279-
~ VALUES ~ ^#row_values
2280-
},
2281-
|(_, _, _, expr_op, _, _, columns_op, _, values)| {
2282-
let selection = match expr_op {
2283-
Some(e) => Some(e.1),
2284-
None => None,
2285-
};
2286-
match columns_op {
2287-
Some(columns) => MergeOption::Unmatch(UnmatchedClause {
2288-
insert_operation: InsertOperation {
2289-
columns: Some(columns.1),
2290-
values,
2291-
},
2292-
selection,
2293-
}),
2294-
None => MergeOption::Unmatch(UnmatchedClause {
2288+
alt((
2289+
map(
2290+
rule! {
2291+
WHEN ~ NOT ~ MATCHED ~ (AND ~ ^#expr)? ~ THEN ~ INSERT ~ ( "(" ~ ^#comma_separated_list1(ident) ~ ^")" )?
2292+
~ VALUES ~ ^#row_values
2293+
},
2294+
|(_, _, _, expr_op, _, _, columns_op, _, values)| {
2295+
let selection = match expr_op {
2296+
Some(e) => Some(e.1),
2297+
None => None,
2298+
};
2299+
match columns_op {
2300+
Some(columns) => MergeOption::Unmatch(UnmatchedClause {
2301+
insert_operation: InsertOperation {
2302+
columns: Some(columns.1),
2303+
values,
2304+
is_star: false,
2305+
},
2306+
selection,
2307+
}),
2308+
None => MergeOption::Unmatch(UnmatchedClause {
2309+
insert_operation: InsertOperation {
2310+
columns: None,
2311+
values,
2312+
is_star: false,
2313+
},
2314+
selection,
2315+
}),
2316+
}
2317+
},
2318+
),
2319+
map(
2320+
rule! {
2321+
WHEN ~ NOT ~ MATCHED ~ (AND ~ ^#expr)? ~ THEN ~ INSERT ~ "*"
2322+
},
2323+
|(_, _, _, expr_op, _, _, _)| {
2324+
let selection = match expr_op {
2325+
Some(e) => Some(e.1),
2326+
None => None,
2327+
};
2328+
MergeOption::Unmatch(UnmatchedClause {
22952329
insert_operation: InsertOperation {
22962330
columns: None,
2297-
values,
2331+
values: Vec::new(),
2332+
is_star: true,
22982333
},
22992334
selection,
2300-
}),
2301-
}
2302-
},
2303-
)(i)
2335+
})
2336+
},
2337+
),
2338+
))(i)
23042339
}
23052340

23062341
pub fn add_column_option(i: Input) -> IResult<AddColumnOption> {

src/query/service/src/interpreters/interpreter_merge_into.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ impl MergeIntoInterpreter {
179179
} else {
180180
None
181181
};
182+
182183
let mut values_exprs = Vec::<RemoteExpr>::with_capacity(item.values.len());
183184

184185
for scalar_expr in &item.values {
@@ -208,6 +209,7 @@ impl MergeIntoInterpreter {
208209
} else {
209210
None
210211
};
212+
211213
// update
212214
let update_list = if let Some(update_list) = &item.update {
213215
// use update_plan to get exprs
@@ -224,7 +226,7 @@ impl MergeIntoInterpreter {
224226
let col_indices = if item.condition.is_none() {
225227
vec![]
226228
} else {
227-
// we don't need to real col_indices here, just give a
229+
// we don't need real col_indices here, just give a
228230
// dummy index, that's ok.
229231
vec![DUMMY_COL_INDEX]
230232
};
@@ -235,7 +237,6 @@ impl MergeIntoInterpreter {
235237
col_indices,
236238
Some(join_output_schema.num_fields()),
237239
)?;
238-
239240
let update_list = update_list
240241
.iter()
241242
.map(|(idx, remote_expr)| {

0 commit comments

Comments
 (0)