PostgreSQL Oracle 兼容性之 - substrb (基于字节的字符串截取)

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

PostgreSQL , substrb , 字节截取


背景

Oracle的substrb函数,用于基于字节流的截取,需要考虑多字节字符串的编码问题,未截取完整字符,则不截取。

https://docs.oracle.com/cd/B12037_01/olap.101/b10339/x_stddev004.htm

substr则用于基于字符串的截取。

PostgreSQL也可以支持类似的功能。

orafce插件

安装orafce插件,里面包含了大量的oracle兼容函数。

https://pgxn.org/dist/orafce/

postgres=# \df *.*substrb*  
                               List of functions  
   Schema   |  Name   | Result data type |    Argument data types     |  Type    
------------+---------+------------------+----------------------------+--------  
 pg_catalog | substrb | varchar2         | varchar2, integer          | normal  
 pg_catalog | substrb | varchar2         | varchar2, integer, integer | normal  
(2 rows)  

实际上这部分代码在PostgreSQL中已经存在,只是没有创建SQL函数。

src/backend/utils/adt/varlena.c

/*  
 * bytea_substr()  
 * Return a substring starting at the specified position.  
 * Cloned from text_substr and modified as required.  
 *  
 * Input:  
 *      - string  
 *      - starting position (is one-based)  
 *      - string length (optional)  
 *  
 * If the starting position is zero or less, then return from the start of the string  
 * adjusting the length to be consistent with the "negative start" per SQL.  
 * If the length is less than zero, an ERROR is thrown. If no third argument  
 * (length) is provided, the length to the end of the string is assumed.  
 */  
Datum  
bytea_substr(PG_FUNCTION_ARGS)  
{  
        PG_RETURN_BYTEA_P(bytea_substring(PG_GETARG_DATUM(0),  
                                                                          PG_GETARG_INT32(1),  
                                                                          PG_GETARG_INT32(2),  
                                                                          false));  
}  
  
  
static bytea *  
bytea_substring(Datum str,  
                                int S,  
                                int L,  
                                bool length_not_specified)  
{  
        int                     S1;                             /* adjusted start position */  
        int                     L1;                             /* adjusted substring length */  
  
        S1 = Max(S, 1);  
  
        if (length_not_specified)  
        {  
                /*  
                 * Not passed a length - DatumGetByteaPSlice() grabs everything to the  
                 * end of the string if we pass it a negative value for length.  
                 */  
                L1 = -1;  
        }  
        else  
        {  
                /* end position */  
                int                     E = S + L;  
  
                /*  
                 * A negative value for L is the only way for the end position to be  
                 * before the start. SQL99 says to throw an error.  
                 */  
                if (E < S)  
                        ereport(ERROR,  
                                        (errcode(ERRCODE_SUBSTRING_ERROR),  
                                         errmsg("negative substring length not allowed")));  
  
                /*  
                 * A zero or negative value for the end position can happen if the  
                 * start was negative or one. SQL99 says to return a zero-length  
                 * string.  
                 */  
                if (E < 1)  
                        return PG_STR_GET_BYTEA("");  
  
                L1 = E - S1;  
        }  
  
        /*  
         * If the start position is past the end of the string, SQL99 says to  
         * return a zero-length string -- DatumGetByteaPSlice() will do that for  
         * us. Convert to zero-based starting position  
         */  
        return DatumGetByteaPSlice(str, S1 - 1, L1);  
}  
postgres=# select octet_length(public.substrb('nihao 中国 abc你好'::varchar,2,9));
 octet_length 
--------------
            9
(1 row)

postgres=# select public.substrb('nihao 中国 abc你好'::varchar,2,9);
 substrb 
---------
 ihao 中
(1 row)

postgres=# select public.substrb('nihao 中国 abc你好'::varchar,2,10);
 substrb 
---------
 ihao 中
(1 row)

postgres=# select public.substrb('nihao 中国 abc你好'::varchar,2,8);
 substrb 
---------
 ihao 中
(1 row)

postgres=# select public.substrb('nihao 中国 abc你好'::varchar,2,6);
 substrb 
---------
 ihao 
(1 row)

postgres=# select public.substrb('nihao 中国 abc你好'::varchar,2,7);
 substrb 
---------
 ihao 
(1 row)

postgres=# select public.substrb('nihao 中国 abc你好'::varchar,2,9);
 substrb 
---------
 ihao 中
(1 row)

postgres=# select public.substrb('nihao 中国 abc你好'::varchar,2,10);
 substrb 
---------
 ihao 中
(1 row)
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
2月前
|
Oracle 关系型数据库 分布式数据库
PolarDB常见问题之PolarDB(Oracle兼容版) 执行命令报错如何解决
PolarDB是阿里云推出的下一代关系型数据库,具有高性能、高可用性和弹性伸缩能力,适用于大规模数据处理场景。本汇总囊括了PolarDB使用中用户可能遭遇的一系列常见问题及解答,旨在为数据库管理员和开发者提供全面的问题指导,确保数据库平稳运行和优化使用体验。
|
2月前
|
关系型数据库 分布式数据库 数据库
PolarDB PostgreSQL版:Oracle兼容的高性能数据库
PolarDB PostgreSQL版是一款高性能的数据库,具有与Oracle兼容的特性。它采用了分布式架构,可以轻松处理大量的数据,同时还支持多种数据类型和函数,具有高可用性和可扩展性。它还提供了丰富的管理工具和性能优化功能,为企业提供了可靠的数据存储和处理解决方案。PolarDB PostgreSQL版在数据库领域具有很高的竞争力,可以满足各种企业的需求。
|
8月前
|
Oracle 关系型数据库 数据库
PostgreSQL和Oracle两种数据库有啥区别?如何选择?
PostgreSQL和Oracle两种数据库有啥区别?如何选择?
217 0
|
5月前
|
SQL Oracle 关系型数据库
Oracle,Postgresql等数据库使用
Oracle,Postgresql等数据库简单使用
135 0
Oracle,Postgresql等数据库使用
|
8月前
|
Oracle 关系型数据库 分布式数据库
如何从Oracle迁移到PolarDB(ADAM)(二)
如何从Oracle迁移到PolarDB(ADAM)(二)
130 0
|
8月前
|
SQL Oracle 关系型数据库
Polar DB-O (兼容 Oracle 语法版本)和Polar DB PostgreSQL 版本概述(二)
Polar DB-O (兼容 Oracle 语法版本)和Polar DB PostgreSQL 版本概述(二)
729 0
|
5天前
|
DataWorks Oracle 关系型数据库
DataWorks操作报错合集之尝试从Oracle数据库同步数据到TDSQL的PG版本,并遇到了与RAW字段相关的语法错误,该怎么处理
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
18 0
|
12天前
|
存储 Oracle 网络协议
Oracle 11gR2学习之二(创建数据库及OEM管理篇)
Oracle 11gR2学习之二(创建数据库及OEM管理篇)
|
16天前
|
Oracle 网络协议 关系型数据库
异地使用PLSQL远程连接访问Oracle数据库【内网穿透】
异地使用PLSQL远程连接访问Oracle数据库【内网穿透】
|
16天前
|
SQL Oracle 安全
Oracle数据库中的事务和锁
【4月更文挑战第19天】Oracle数据库的事务和锁是确保数据完整性和并发控制的核心机制。事务遵循ACID原则,保证操作的原子性、一致性、隔离性和持久性。通过COMMIT或ROLLBACK来管理事务更改。锁包括共享锁(读)、排他锁(写)、行级锁和表级锁,用于控制并发访问。自动锁机制在DML操作时生效,防止数据冲突。事务和锁共同维护数据库的稳定和安全。

相关产品

  • 云原生数据库 PolarDB
  • 推荐镜像

    更多