Skip to content

Commit

Permalink
feat(query): Procedure Part2 support arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
TCeason committed Sep 14, 2024
1 parent 2ca0036 commit 3612083
Show file tree
Hide file tree
Showing 24 changed files with 675 additions and 349 deletions.
11 changes: 9 additions & 2 deletions src/meta/app/src/principal/procedure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct ProcedureIdent {
#[derive(Clone, Debug, PartialEq)]
pub struct ProcedureMeta {
pub return_types: Vec<DataType>,
pub arg_names: Vec<String>,
pub created_on: DateTime<Utc>,
pub updated_on: DateTime<Utc>,
pub script: String,
Expand All @@ -56,6 +57,7 @@ impl Default for ProcedureMeta {
fn default() -> Self {
ProcedureMeta {
return_types: vec![],
arg_names: vec![],
created_on: Utc::now(),
updated_on: Utc::now(),
script: "".to_string(),
Expand All @@ -69,8 +71,13 @@ impl Display for ProcedureMeta {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"Lanuage: {:?}, return_type: {:?}, CreatedOn: {:?}, Script: {:?}, Comment: {:?}",
self.procedure_language, self.return_types, self.created_on, self.script, self.comment
"Lanuage: {:?}, args {:?} return_type: {:?}, CreatedOn: {:?}, Script: {:?}, Comment: {:?}",
self.procedure_language,
self.arg_names,
self.return_types,
self.created_on,
self.script,
self.comment
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/meta/app/src/principal/procedure_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl ProcedureIdentity {
pub fn new(name: impl ToString, args: impl ToString) -> Self {
Self {
name: name.to_string(),
args: args.to_string(),
args: args.to_string().to_lowercase(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/meta/proto-conv/src/procedure_from_to_protobuf_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl FromToProto for mt::principal::ProcedureMeta {

let v = Self {
return_types,
arg_names: p.arg_names.clone(),
created_on: DateTime::<Utc>::from_pb(p.created_on)?,
updated_on: DateTime::<Utc>::from_pb(p.updated_on)?,
script: p.script,
Expand All @@ -94,6 +95,7 @@ impl FromToProto for mt::principal::ProcedureMeta {
ver: VER,
min_reader_ver: MIN_READER_VER,
return_types,
arg_names: self.arg_names.clone(),
created_on: self.created_on.to_pb()?,
updated_on: self.updated_on.to_pb()?,
script: self.script.to_string(),
Expand Down
3 changes: 2 additions & 1 deletion src/meta/proto-conv/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ const META_CHANGE_LOG: &[(u64, &str)] = &[
(105, "2024-08-05: Add: add Dictionary meta"),
(106, "2024-08-08: Add: add QueryTokenInfo"),
(107, "2024-08-09: Add: datatype.proto/DataType Geography type"),
(108, "2024-08-29: Add: procedure.proto: ProcedureMeta and ProcedureIdentity")
(108, "2024-08-29: Add: procedure.proto: ProcedureMeta and ProcedureIdentity"),
(109, "2024-08-29: Refactor: ProcedureMeta add arg_names"),
// Dear developer:
// If you're gonna add a new metadata version, you'll have to add a test for it.
// You could just copy an existing test file(e.g., `../tests/it/v024_table_meta.rs`)
Expand Down
1 change: 1 addition & 0 deletions src/meta/proto-conv/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ mod v105_dictionary_meta;
mod v106_query_token;
mod v107_geography_datatype;
mod v108_procedure;
mod v109_procedure_with_args;
9 changes: 5 additions & 4 deletions src/meta/proto-conv/tests/it/v108_procedure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ use crate::common;

#[test]
fn v108_procedure_meta() -> anyhow::Result<()> {
let procedure_meta_v108: Vec<u8> = vec![
34, 9, 146, 2, 0, 160, 6, 108, 168, 6, 24, 82, 23, 50, 48, 49, 52, 45, 49, 49, 45, 50, 56,
let procedure_meta_v108 = vec![
34, 9, 146, 2, 0, 160, 6, 109, 168, 6, 24, 82, 23, 50, 48, 49, 52, 45, 49, 49, 45, 50, 56,
32, 49, 50, 58, 48, 48, 58, 48, 57, 32, 85, 84, 67, 90, 23, 50, 48, 49, 52, 45, 49, 49, 45,
50, 57, 32, 49, 50, 58, 48, 48, 58, 48, 57, 32, 85, 84, 67, 98, 7, 102, 111, 111, 32, 98,
97, 114, 114, 3, 83, 81, 76, 160, 6, 108, 168, 6, 24,
97, 114, 114, 3, 83, 81, 76, 160, 6, 109, 168, 6, 24,
];

let want = || mt::ProcedureMeta {
return_types: vec![DataType::String],
arg_names: vec![],
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 9).unwrap(),
script: "".to_string(),
Expand All @@ -39,7 +40,7 @@ fn v108_procedure_meta() -> anyhow::Result<()> {
};

common::test_pb_from_to(func_name!(), want())?;
common::test_load_old(func_name!(), procedure_meta_v108.as_slice(), 108, want())
common::test_load_old(func_name!(), procedure_meta_v108.as_slice(), 109, want())
}

#[test]
Expand Down
44 changes: 44 additions & 0 deletions src/meta/proto-conv/tests/it/v109_procedure_with_args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2023 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use chrono::TimeZone;
use chrono::Utc;
use databend_common_expression::types::DataType;
use databend_common_meta_app::principal as mt;
use fastrace::func_name;

use crate::common;

#[test]
fn v109_procedure_meta() -> anyhow::Result<()> {
let procedure_meta_v109 = vec![
34, 9, 146, 2, 0, 160, 6, 109, 168, 6, 24, 42, 7, 109, 101, 115, 115, 97, 103, 101, 82, 23,
50, 48, 49, 52, 45, 49, 49, 45, 50, 56, 32, 49, 50, 58, 48, 48, 58, 48, 57, 32, 85, 84, 67,
90, 23, 50, 48, 49, 52, 45, 49, 49, 45, 50, 57, 32, 49, 50, 58, 48, 48, 58, 48, 57, 32, 85,
84, 67, 98, 7, 102, 111, 111, 32, 98, 97, 114, 114, 3, 83, 81, 76, 160, 6, 109, 168, 6, 24,
];

let want = || mt::ProcedureMeta {
return_types: vec![DataType::String],
arg_names: vec!["message".to_string()],
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 9).unwrap(),
script: "".to_string(),
comment: "foo bar".to_string(),
procedure_language: "SQL".to_string(),
};

common::test_pb_from_to(func_name!(), want())?;
common::test_load_old(func_name!(), procedure_meta_v109.as_slice(), 109, want())
}
1 change: 1 addition & 0 deletions src/meta/protos/proto/procedure.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ message ProcedureMeta {

// Procedure return type
repeated DataType return_types = 4;
repeated string arg_names = 5;

// The time database created.
string created_on = 10;
Expand Down
11 changes: 6 additions & 5 deletions src/query/ast/src/ast/statements/procedure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use derive_visitor::Drive;
use derive_visitor::DriveMut;

use crate::ast::write_comma_separated_list;
use crate::ast::write_comma_separated_string_list;
use crate::ast::CreateOption;
use crate::ast::Expr;
use crate::ast::TypeName;

#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
Expand Down Expand Up @@ -169,16 +169,17 @@ impl Display for DescProcedureStmt {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
pub struct CallProcedureStmt {
pub name: String,
pub args: Vec<String>,
pub args: Vec<Expr>,
}

impl Display for CallProcedureStmt {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "CALL PROCEDURE {}(", self.name)?;
write_comma_separated_string_list(f, self.args.clone())?;
let CallProcedureStmt { name, args } = self;
write!(f, "CALL PROCEDURE {}(", name)?;
write_comma_separated_list(f, args)?;
write!(f, ")")?;
Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions src/query/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2076,7 +2076,7 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
name: name.to_string(),
args_type: if let Some(args) = &args {
args.iter()
.map(|arg| arg.data_type.to_string())
.map(|arg| arg.data_type.to_string().to_lowercase())
.collect::<Vec<String>>()
.join(",")
} else {
Expand Down Expand Up @@ -2125,12 +2125,12 @@ pub fn statement_body(i: Input) -> IResult<Statement> {

let call_procedure = map(
rule! {
CALL ~ PROCEDURE ~ #ident ~ "(" ~ ")"
CALL ~ PROCEDURE ~ #ident ~ "(" ~ #comma_separated_list0(subexpr(0))? ~ ")"
},
|(_, _, name, _, _)| {
|(_, _, name, _, opt_args, _)| {
Statement::CallProcedure(CallProcedureStmt {
name: name.to_string(),
args: vec![],
args: opt_args.unwrap_or_default(),
})
},
);
Expand All @@ -2147,7 +2147,7 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
"".to_string()
} else {
args.iter()
.map(|arg| arg.to_string())
.map(|arg| arg.to_string().to_lowercase())
.collect::<Vec<String>>()
.join(",")
},
Expand Down
1 change: 1 addition & 0 deletions src/query/ast/tests/it/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ fn test_statement() {
r#"drop PROCEDURE p1()"#,
r#"drop PROCEDURE p1(int, string)"#,
r#"call PROCEDURE p1()"#,
r#"call PROCEDURE p1(1, 'x', '2022-02-02'::Date)"#,
r#"show PROCEDURES like 'p1%'"#,
r#"create PROCEDURE p1() returns string not null language sql comment = 'test' as $$
BEGIN
Expand Down
45 changes: 45 additions & 0 deletions src/query/ast/tests/it/testdata/stmt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23066,6 +23066,51 @@ CallProcedure(
)


---------- Input ----------
call PROCEDURE p1(1, 'x', '2022-02-02'::Date)
---------- Output ---------
CALL PROCEDURE p1(1, 'x', '2022-02-02'::DATE)
---------- AST ------------
CallProcedure(
CallProcedureStmt {
name: "p1",
args: [
Literal {
span: Some(
18..19,
),
value: UInt64(
1,
),
},
Literal {
span: Some(
21..24,
),
value: String(
"x",
),
},
Cast {
span: Some(
38..44,
),
expr: Literal {
span: Some(
26..38,
),
value: String(
"2022-02-02",
),
},
target_type: Date,
pg_style: true,
},
],
},
)


---------- Input ----------
show PROCEDURES like 'p1%'
---------- Output ---------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,7 @@ impl AccessChecker for PrivilegeAccess {
Plan::DescDatamaskPolicy(_) => {}
Plan::Begin => {}
Plan::ExecuteImmediate(_)
| Plan::CallProcedure(_)
| Plan::CreateProcedure(_)
| Plan::DropProcedure(_)
/*| Plan::ShowCreateProcedure(_)
Expand Down
Loading

0 comments on commit 3612083

Please sign in to comment.