forked from GoogleCloudPlatform/bigquery-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cw_substrb.sqlx
28 lines (27 loc) · 1.26 KB
/
cw_substrb.sqlx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
config { hasOutput: true }
/*
* Copyright 2022 Google LLC
*
* 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.
*/
/** Similar to Vertica SUBSTRB function, which treats the multibyte character string as a string of octets (bytes). */
CREATE OR REPLACE FUNCTION ${self()}(str STRING, startpos INT64 /* 1-based */, extent INT64 /* 1-based */) RETURNS STRING
OPTIONS (
description="Similar to Vertica SUBSTRB function, which treats the multibyte character string as a string of octets (bytes)."
)
AS (
(WITH octets AS (
SELECT oct FROM UNNEST(TO_CODE_POINTS(CAST(str as bytes))) AS oct WITH OFFSET off WHERE (off+1) >= startpos and (off+1) < startpos+extent ORDER BY off
)
SELECT SAFE_CONVERT_BYTES_TO_STRING(CODE_POINTS_TO_BYTES(ARRAY_AGG(oct))) FROM octets
));