Skip to content

Commit f2b82d0

Browse files
committed
MB-70691: allow extractddl to filter functions by bucket
- When a bucket filter is specified, only return scoped functions belonging to that bucket; exclude global functions. Global functions are still included when no filter is specified. - Strip surrounding backticks from the filter parameter so that backtick-quoted bucket names (e.g. `travel-sample`) are handled correctly. - Update test files to add new test cases and modify previous ones. Change-Id: I191c06aa1c940acf183fb564b6ecd68b0ef42e36 Reviewed-on: https://review.couchbase.org/c/query/+/241663 Well-Formed: Restriction Checker Reviewed-by: Sitaram Vemulapalli <sitaram.vemulapalli@couchbase.com> Tested-by: Sitaram Vemulapalli <sitaram.vemulapalli@couchbase.com>
1 parent e7f1224 commit f2b82d0

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

expression/func_general.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,13 +474,24 @@ func (this *ExtractDDL) Evaluate(item value.Value, context Context) (value.Value
474474
with = value.NewValue(map[string]interface{}{})
475475
}
476476

477+
// Strip surrounding backticks from filter if present (e.g., '`travel-sample`' -> 'travel-sample')
478+
filterSpecified := filter != nil && filter.ToString() != ""
479+
if filterSpecified {
480+
filterStr := strings.Trim(filter.ToString(), "`")
481+
if filterStr != "" {
482+
filter = value.NewValue(filterStr)
483+
} else {
484+
filterSpecified = false
485+
}
486+
}
487+
477488
res := make([]interface{}, 0, 32)
478489
args := make(value.Values, 0, 1)
479490

480491
var buf strings.Builder
481492
buf.Grow(128) // Pre-allocate buffer for the initial query
482493
buf.WriteString("SELECT DISTINCT RAW name FROM system:keyspaces WHERE `namespace` = 'default' AND `bucket` IS NOT VALUED ")
483-
if filter != nil && filter.ToString() != "" {
494+
if filterSpecified {
484495
buf.WriteString(" AND name LIKE ?")
485496
args = append(args, filter)
486497
}
@@ -544,8 +555,8 @@ func (this *ExtractDDL) Evaluate(item value.Value, context Context) (value.Value
544555
}
545556
}
546557

547-
// Extract global functions first (independent of buckets)
548-
if flags&_FUNCTION_INFO != 0 {
558+
// Extract global functions first (independent of buckets), but only when no specific bucket filter is specified
559+
if flags&_FUNCTION_INFO != 0 && !filterSpecified {
549560
stmt := "SELECT functions FROM system:functions " +
550561
"WHERE functions.identity.type = 'global' " +
551562
"ORDER BY functions.identity.name"

test/gsi/test_cases/extractddl/case_extractddl_basic.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
"results": [
77
{
88
"ddl_statements": [
9-
"CREATE OR REPLACE FUNCTION `add_numbers`(`a`, `b`) LANGUAGE INLINE AS a + b;",
10-
"CREATE OR REPLACE FUNCTION `ejs1`() LANGUAGE JAVASCRIPT AS \"ej1\" AT \"lib1\";",
11-
"CREATE OR REPLACE FUNCTION `no_param_func`() LANGUAGE INLINE AS 42;",
12-
"CREATE OR REPLACE FUNCTION `test_func`() LANGUAGE JAVASCRIPT AS \"function test_func() { return \"hello\"; }\";",
13-
"CREATE OR REPLACE FUNCTION `test_inline_func`(`x`) LANGUAGE INLINE AS x * 2;",
14-
"CREATE OR REPLACE FUNCTION `variadic_func`(...) LANGUAGE INLINE AS args[0] + args[1];",
159
"CREATE BUCKET `customer` WITH {'evictionPolicy':'fullEviction','numVBuckets':128,'ramQuota':250,'storageBackend':'magma'};",
1610
"CREATE INDEX `test_index` ON `customer`(`name`) ;",
1711
"CREATE SEQUENCE `default`:`customer`.`_default`.`test_sequence` START WITH 100 CACHE 10 NO CYCLE INCREMENT BY 5 MINVALUE -9223372036854775808;",
@@ -73,6 +67,18 @@
7367
"description": "Test ExtractDDL with function flag for customer bucket",
7468
"statements": "SELECT EXTRACTDDL('customer', {'flags': ['function']}) AS ddl_statements",
7569
"ordered": true,
70+
"results": [
71+
{
72+
"ddl_statements": [
73+
"CREATE OR REPLACE FUNCTION `customer`.`_default`.`scoped_multiply`(`x`, `y`) LANGUAGE INLINE AS x * y;"
74+
]
75+
}
76+
]
77+
},
78+
{
79+
"description": "Test ExtractDDL with global scope for function flag",
80+
"statements": "SELECT EXTRACTDDL('', {'flags': ['function']}) AS ddl_statements;",
81+
"ordered": true,
7682
"results": [
7783
{
7884
"ddl_statements": [

0 commit comments

Comments
 (0)