Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/meta/app/src/principal/user_defined_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ pub struct UserDefinedFunction {
pub description: String,
pub definition: UDFDefinition,
pub created_on: DateTime<Utc>,
pub update_on: DateTime<Utc>,
}

impl UserDefinedFunction {
Expand All @@ -168,14 +169,16 @@ impl UserDefinedFunction {
definition: &str,
description: &str,
) -> Self {
let now = Utc::now();
Self {
name: name.to_string(),
description: description.to_string(),
definition: UDFDefinition::LambdaUDF(LambdaUDF {
parameters,
definition: definition.to_string(),
}),
created_on: Utc::now(),
created_on: now,
update_on: now,
}
}

Expand All @@ -191,6 +194,7 @@ impl UserDefinedFunction {
description: &str,
immutable: Option<bool>,
) -> Self {
let now = Utc::now();
Self {
name: name.to_string(),
description: description.to_string(),
Expand All @@ -204,7 +208,8 @@ impl UserDefinedFunction {
return_type,
immutable,
}),
created_on: Utc::now(),
created_on: now,
update_on: now,
}
}

Expand All @@ -219,6 +224,7 @@ impl UserDefinedFunction {
description: &str,
immutable: Option<bool>,
) -> Self {
let now = Utc::now();
Self {
name: name.to_string(),
description: description.to_string(),
Expand All @@ -233,7 +239,8 @@ impl UserDefinedFunction {
packages: vec![],
immutable,
}),
created_on: Utc::now(),
created_on: now,
update_on: now,
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/meta/proto-conv/src/udf_from_to_protobuf_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,14 +546,21 @@ impl FromToProto for mt::UserDefinedFunction {
}
};

let created_on = match p.created_on {
Some(c) => DateTime::<Utc>::from_pb(c)?,
None => DateTime::<Utc>::default(),
};
let update_on = match p.update_on {
Some(u) => DateTime::<Utc>::from_pb(u)?,
None => created_on,
};

Ok(mt::UserDefinedFunction {
name: p.name,
description: p.description,
definition: udf_def,
created_on: match p.created_on {
Some(c) => DateTime::<Utc>::from_pb(c)?,
None => DateTime::<Utc>::default(),
},
created_on,
update_on,
})
}

Expand Down Expand Up @@ -589,6 +596,7 @@ impl FromToProto for mt::UserDefinedFunction {
description: self.description.clone(),
definition: Some(udf_def),
created_on: Some(self.created_on.to_pb()?),
update_on: Some(self.update_on.to_pb()?),
})
}
}
1 change: 1 addition & 0 deletions src/meta/proto-conv/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ const META_CHANGE_LOG: &[(u64, &str)] = &[
(157, "2025-10-22: Add: TableDataType TimestampTz"),
(158, "2025-10-22: Add: Server UDTF"),
(159, "2025-11-18: Add: Grant/OwnershipMaskingPolicyObject and masking policy privileges"),
(160, "2025-11-27: Add: udf.proto/UserDefinedFunction add update_on field"),
// 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 @@ -151,3 +151,4 @@ mod v156_data_mask_args;
mod v157_type_timestamp_tz;
mod v158_udtf_server;
mod v159_grant_object_masking_policy;
mod v160_udf_update_on;
1 change: 1 addition & 0 deletions src/meta/proto-conv/tests/it/v058_udf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fn test_decode_v57_udf() -> anyhow::Result<()> {
immutable: None,
}),
created_on: DateTime::<Utc>::default(),
update_on: DateTime::<Utc>::default(),
};

common::test_pb_from_to(func_name!(), want())?;
Expand Down
2 changes: 2 additions & 0 deletions src/meta/proto-conv/tests/it/v079_udf_created_on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ fn test_decode_v79_udf_python() -> anyhow::Result<()> {
immutable: None,
}),
created_on: DateTime::<Utc>::from_timestamp(1702603569, 0).unwrap(),
update_on: DateTime::<Utc>::from_timestamp(1702603569, 0).unwrap(),
};

common::test_pb_from_to(func_name!(), want())?;
Expand All @@ -91,6 +92,7 @@ fn test_decode_v79_udf_sql() -> anyhow::Result<()> {
definition: "(p) -> (NOT is_null(p))".to_string(),
}),
created_on: DateTime::<Utc>::from_timestamp(170267984, 0).unwrap(),
update_on: DateTime::<Utc>::from_timestamp(170267984, 0).unwrap(),
};

common::test_pb_from_to(func_name!(), want())?;
Expand Down
3 changes: 3 additions & 0 deletions src/meta/proto-conv/tests/it/v081_udf_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn test_decode_v81_udf_python() -> anyhow::Result<()> {
immutable: None,
}),
created_on: DateTime::<Utc>::from_timestamp(1702603569, 0).unwrap(),
update_on: DateTime::<Utc>::from_timestamp(1702603569, 0).unwrap(),
};

common::test_pb_from_to(func_name!(), want())?;
Expand All @@ -91,6 +92,7 @@ fn test_decode_v81_udf_sql() -> anyhow::Result<()> {
definition: "(p) -> (NOT is_null(p))".to_string(),
}),
created_on: DateTime::<Utc>::from_timestamp(170267984, 0).unwrap(),
update_on: DateTime::<Utc>::from_timestamp(170267984, 0).unwrap(),
};

common::test_pb_from_to(func_name!(), want())?;
Expand Down Expand Up @@ -124,6 +126,7 @@ fn test_decode_udf_script() -> anyhow::Result<()> {
immutable: None,
}),
created_on: DateTime::<Utc>::default(),
update_on: DateTime::<Utc>::default(),
};

common::test_pb_from_to(func_name!(), want())?;
Expand Down
1 change: 1 addition & 0 deletions src/meta/proto-conv/tests/it/v115_add_udaf_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ fn test_decode_v115_add_udaf_script() -> anyhow::Result<()> {
packages: vec![],
}),
created_on: DateTime::<Utc>::default(),
update_on: DateTime::<Utc>::default(),
};

common::test_pb_from_to(func_name!(), want())?;
Expand Down
1 change: 1 addition & 0 deletions src/meta/proto-conv/tests/it/v130_udf_imports_packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fn test_decode_v130_udf_script() -> anyhow::Result<()> {
immutable: None,
}),
created_on: DateTime::<Utc>::default(),
update_on: DateTime::<Utc>::default(),
};

common::test_pb_from_to(func_name!(), want())?;
Expand Down
2 changes: 2 additions & 0 deletions src/meta/proto-conv/tests/it/v135_udf_immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ fn test_decode_v135_udf_server() -> anyhow::Result<()> {
immutable: Some(true),
}),
created_on: DateTime::<Utc>::default(),
update_on: DateTime::<Utc>::default(),
};

common::test_pb_from_to(func_name!(), want())?;
Expand Down Expand Up @@ -101,6 +102,7 @@ fn test_decode_v135_udf_script() -> anyhow::Result<()> {
immutable: Some(true),
}),
created_on: DateTime::<Utc>::default(),
update_on: DateTime::<Utc>::default(),
};

common::test_pb_from_to(func_name!(), want())?;
Expand Down
1 change: 1 addition & 0 deletions src/meta/proto-conv/tests/it/v143_udtf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fn test_decode_v143_udtf() -> anyhow::Result<()> {
sql: "select * from t1".to_string(),
}),
created_on: DateTime::<Utc>::from_timestamp(1702603569, 0).unwrap(),
update_on: DateTime::<Utc>::from_timestamp(1702603569, 0).unwrap(),
};

common::test_pb_from_to(func_name!(), want())?;
Expand Down
1 change: 1 addition & 0 deletions src/meta/proto-conv/tests/it/v144_scalar_udf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fn test_decode_v144_scalar_udf() -> anyhow::Result<()> {
definition: "CURRENT_DATE".to_string(),
}),
created_on: DateTime::<Utc>::from_timestamp(1702603569, 0).unwrap(),
update_on: DateTime::<Utc>::from_timestamp(1702603569, 0).unwrap(),
};

common::test_pb_from_to(func_name!(), want())?;
Expand Down
1 change: 1 addition & 0 deletions src/meta/proto-conv/tests/it/v152_external_udf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fn test_decode_v152_external_udf() -> anyhow::Result<()> {
immutable: None,
}),
created_on: DateTime::<Utc>::default(),
update_on: DateTime::<Utc>::default(),
};
common::test_pb_from_to(func_name!(), want())?;
common::test_load_old(
Expand Down
1 change: 1 addition & 0 deletions src/meta/proto-conv/tests/it/v158_udtf_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn test_decode_v158_server_udtf() -> anyhow::Result<()> {
immutable: None,
}),
created_on: DateTime::<Utc>::from_timestamp(1702603569, 0).unwrap(),
update_on: DateTime::<Utc>::from_timestamp(1702603569, 0).unwrap(),
};

common::test_pb_from_to(func_name!(), want())?;
Expand Down
54 changes: 54 additions & 0 deletions src/meta/proto-conv/tests/it/v160_udf_update_on.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2025 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::DateTime;
use chrono::Utc;
use databend_common_meta_app::principal::LambdaUDF;
use databend_common_meta_app::principal::UDFDefinition;
use databend_common_meta_app::principal::UserDefinedFunction;
use fastrace::func_name;

use crate::common;

#[test]
fn test_decode_v160_udf_update_on() -> anyhow::Result<()> {
let user_defined_function_v160 = vec![
10, 15, 117, 100, 102, 95, 119, 105, 116, 104, 95, 117, 112, 100, 97, 116, 101, 18, 17,
117, 100, 102, 32, 119, 105, 116, 104, 32, 116, 114, 97, 99, 107, 105, 110, 103, 26, 22,
10, 1, 112, 18, 10, 40, 112, 41, 32, 45, 62, 32, 40, 112, 41, 160, 6, 160, 1, 168, 6, 24,
42, 23, 50, 48, 50, 51, 45, 49, 50, 45, 49, 53, 32, 48, 49, 58, 50, 54, 58, 48, 57, 32, 85,
84, 67, 90, 23, 50, 48, 50, 51, 45, 49, 50, 45, 49, 53, 32, 48, 49, 58, 52, 50, 58, 53, 48,
32, 85, 84, 67, 160, 6, 160, 1, 168, 6, 24,
];

let want = || UserDefinedFunction {
name: "udf_with_update".to_string(),
description: "udf with tracking".to_string(),
definition: UDFDefinition::LambdaUDF(LambdaUDF {
parameters: vec!["p".to_string()],
definition: "(p) -> (p)".to_string(),
}),
created_on: DateTime::<Utc>::from_timestamp(1702603569, 0).unwrap(),
update_on: DateTime::<Utc>::from_timestamp(1702604570, 0).unwrap(),
};

common::test_pb_from_to(func_name!(), want())?;
common::test_load_old(
func_name!(),
user_defined_function_v160.as_slice(),
160,
want(),
)?;
Ok(())
}
2 changes: 2 additions & 0 deletions src/meta/protos/proto/udf.proto
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,6 @@ message UserDefinedFunction {
}
// The time udf created.
optional string created_on = 5;
// The time udf updated.
optional string update_on = 11;
}
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ DB.Table: 'system'.'columns', Table: columns-table_id:1, ver:0, Engine: SystemCo
| 'unique_constraint_schema' | 'information_schema' | 'referential_constraints' | 'Nullable(String)' | 'VARCHAR' | '' | '' | 'YES' | '' |
| 'unit' | 'system' | 'caches' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' |
| 'update_on' | 'system' | 'roles' | 'Timestamp' | 'TIMESTAMP' | '' | '' | 'NO' | '' |
| 'update_on' | 'system' | 'user_functions' | 'Timestamp' | 'TIMESTAMP' | '' | '' | 'NO' | '' |
| 'update_on' | 'system' | 'users' | 'Nullable(Timestamp)' | 'TIMESTAMP' | '' | '' | 'YES' | '' |
| 'update_rule' | 'information_schema' | 'referential_constraints' | 'Nullable(String)' | 'VARCHAR' | '' | '' | 'YES' | '' |
| 'updated_on' | 'system' | 'constraints' | 'Nullable(Timestamp)' | 'TIMESTAMP' | '' | '' | 'YES' | '' |
Expand Down
2 changes: 1 addition & 1 deletion src/query/sql/src/planner/binder/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Binder {
let (show_limit, limit_str) = get_show_options(show_options, None);
// rewrite show user functions to select * FROM default.system.user_functions ...
let query = format!(
"SELECT name, is_aggregate, description, arguments, language, created_on FROM default.system.user_functions {} ORDER BY name {}",
"SELECT name, is_aggregate, description, arguments, language, created_on, update_on FROM default.system.user_functions {} ORDER BY name {}",
show_limit, limit_str,
);
self.bind_rewrite_to_query(bind_context, &query, RewriteKind::ShowFunctions)
Expand Down
7 changes: 7 additions & 0 deletions src/query/sql/src/planner/binder/udf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl Binder {
definition: definition.to_string(),
}),
created_on: Utc::now(),
update_on: Utc::now(),
})
}
UDFDefinition::UDFServer {
Expand Down Expand Up @@ -161,6 +162,7 @@ impl Binder {
immutable: *immutable,
}),
created_on: Utc::now(),
update_on: Utc::now(),
})
}
UDFDefinition::UDAFServer { .. } => unimplemented!(),
Expand Down Expand Up @@ -193,6 +195,7 @@ impl Binder {
description,
definition,
created_on: Utc::now(),
update_on: Utc::now(),
})
}
UDFDefinition::UDAFScript {
Expand Down Expand Up @@ -222,6 +225,7 @@ impl Binder {
description,
definition,
created_on: Utc::now(),
update_on: Utc::now(),
})
}
UDFDefinition::UDTFSql {
Expand Down Expand Up @@ -256,6 +260,7 @@ impl Binder {
sql: sql.to_string(),
}),
created_on: Utc::now(),
update_on: Utc::now(),
})
}
UDFDefinition::UDTFServer {
Expand Down Expand Up @@ -300,6 +305,7 @@ impl Binder {
immutable: *immutable,
}),
created_on: Utc::now(),
update_on: Utc::now(),
})
}
UDFDefinition::ScalarUDF {
Expand All @@ -326,6 +332,7 @@ impl Binder {
definition: definition.clone(),
}),
created_on: Utc::now(),
update_on: Utc::now(),
})
}
}
Expand Down
Loading
Loading