在DM 中,可以通过创建自定义的聚集函数来实现PostgreSQL的bit_or聚集函数功能:
首先,需要创建一个类型,该类型定义了一个累加方法(accumulate)和一个合并方法(merge):
CREATE OR REPLACE TYPE bit_or_agg_type AS OBJECT (
bit_or_result NUMBER(38),
STATIC FUNCTION ODCIAggregateInitialize(sctx IN OUT bit_or_agg_type)
RETURN NUMBER,
MEMBER FUNCTION ODCIAggregateIterate(self IN OUT bit_or_agg_type, value IN NUMBER)
RETURN NUMBER,
MEMBER FUNCTION ODCIAggregateTerminate(self IN bit_or_agg_type, returnValue OUT NUMBER, flags IN NUMBER)
RETURN NUMBER,
MEMBER FUNCTION ODCIAggregateMerge(self IN OUT bit_or_agg_type, ctx2 IN bit_or_agg_type)
RETURN NUMBER
);
然后,需要为此类型创建一个正文,该正文实现了这些方法:
CREATE OR REPLACE TYPE BODY bit_or_agg_type IS
STATIC FUNCTION ODCIAggregateInitialize(sctx IN OUT bit_or_agg_type)
RETURN NUMBER IS
BEGIN
sctx := bit_or_agg_type(NULL);
RETURN ODCIConst.Success;
END;
MEMBER FUNCTION ODCIAggregateIterate(self IN OUT bit_or_agg_type, value IN NUMBER)
RETURN NUMBER IS
BEGIN
self.bit_or_result := COALESCE(self.bit_or_result, 0) | value;
RETURN ODCIConst.Success;
END;
MEMBER FUNCTION ODCIAggregateTerminate(self IN bit_or_agg_type, returnValue OUT NUMBER, flags IN NUMBER)
RETURN NUMBER IS
BEGIN
returnValue := self.bit_or_result;
RETURN ODCIConst.Success;
END;
MEMBER FUNCTION ODCIAggregateMerge(self IN OUT bit_or_agg_type, ctx2 IN bit_or_agg_type)
RETURN NUMBER IS
BEGIN
self.bit_or_result := COALESCE(self.bit_or_result, 0) | COALESCE(ctx2.bit_or_result, 0);
RETURN ODCIConst.Success;
END;
END;
最后,创建一个使用该类型的聚集函数:
CREATE OR REPLACE FUNCTION bit_or(input NUMBER) RETURN NUMBER
PARALLEL_ENABLE AGGREGATE USING bit_or_agg_type;
现在,就可以使用这个bit_or_agg
函数来实现PostgreSQL的bit_or
聚集函数功能了。例如:
create table bits(
id int primary key auto_increment,
bin_value varbinary(8)
);
insert into bits(bin_value) values(0x05),(0x06);
commit;
select * from bits;
select bit_or(bin_value) from bits;
文章
阅读量
获赞