提交 9ba97664 authored 作者: noelgrandin@gmail.com's avatar noelgrandin@gmail.com

Issue 552: Implement BIT_AND and BIT_OR aggregate functions

上级 25b8313e
......@@ -2466,6 +2466,26 @@ The returned value is of the same data type as the parameter.
AVG(X)
"
"Functions (Aggregate)","BIT_AND","
BIT_AND(expression)
","
The bitwise AND of all non-null values.
If no rows are selected, the result is NULL.
Aggregates are only allowed in select statements.
","
BIT_AND(ID)
"
"Functions (Aggregate)","BIT_OR","
BIT_OR(expression)
","
The bitwise OR of all non-null values.
If no rows are selected, the result is NULL.
Aggregates are only allowed in select statements.
","
BIT_OR(ID)
"
"Functions (Aggregate)","BOOL_AND","
BOOL_AND(boolean)
","
......
......@@ -23,6 +23,7 @@ Change Log
</li><li>Fix bug in PageStore#commit method - when the ignoreBigLog flag was set,
the logic that cleared the flag could never be reached, resulting in performance degradation.
Reported by Alexander Nesterov.
</li><li>Issue 552: Implement BIT_AND and BIT_OR aggregate functions
</li></ul>
<h2>Version 1.4.184 Beta (2014-12-19)</h2>
......
......@@ -106,15 +106,25 @@ public class Aggregate extends Expression {
*/
static final int BOOL_AND = 12;
/**
* The aggregate type for BOOL_OR(expression).
*/
static final int BIT_OR = 13;
/**
* The aggregate type for BOOL_AND(expression).
*/
static final int BIT_AND = 14;
/**
* The aggregate type for SELECTIVITY(expression).
*/
static final int SELECTIVITY = 13;
static final int SELECTIVITY = 15;
/**
* The aggregate type for HISTOGRAM(expression).
*/
static final int HISTOGRAM = 14;
static final int HISTOGRAM = 16;
private static final HashMap<String, Integer> AGGREGATES = New.hashMap();
......@@ -170,6 +180,8 @@ public class Aggregate extends Expression {
addAggregate("EVERY", BOOL_AND);
addAggregate("SELECTIVITY", SELECTIVITY);
addAggregate("HISTOGRAM", HISTOGRAM);
addAggregate("BIT_OR", BIT_OR);
addAggregate("BIT_AND", BIT_AND);
}
private static void addAggregate(String name, int type) {
......@@ -435,6 +447,12 @@ public class Aggregate extends Expression {
displaySize = ValueBoolean.DISPLAY_SIZE;
scale = 0;
break;
case BIT_AND:
case BIT_OR:
if (!DataType.supportsAdd(dataType)) {
throw DbException.get(ErrorCode.SUM_OR_AVG_ON_WRONG_DATATYPE_1, getSQL());
}
break;
default:
DbException.throwInternalError("type=" + type);
}
......@@ -540,6 +558,12 @@ public class Aggregate extends Expression {
case BOOL_OR:
text = "BOOL_OR";
break;
case BIT_AND:
text = "BIT_AND";
break;
case BIT_OR:
text = "BIT_OR";
break;
default:
throw DbException.throwInternalError("type=" + type);
}
......
......@@ -108,6 +108,20 @@ class AggregateDataDefault extends AggregateData {
v.getBoolean().booleanValue());
}
break;
case Aggregate.BIT_AND:
if (value == null) {
value = v.convertTo(dataType);
} else {
value = ValueLong.get(value.getLong() & v.getLong()).convertTo(dataType);
}
break;
case Aggregate.BIT_OR:
if (value == null) {
value = v.convertTo(dataType);
} else {
value = ValueLong.get(value.getLong() | v.getLong()).convertTo(dataType);
}
break;
default:
DbException.throwInternalError("type=" + aggregateType);
}
......@@ -124,6 +138,8 @@ class AggregateDataDefault extends AggregateData {
case Aggregate.SUM:
case Aggregate.MIN:
case Aggregate.MAX:
case Aggregate.BIT_OR:
case Aggregate.BIT_AND:
case Aggregate.BOOL_OR:
case Aggregate.BOOL_AND:
v = value;
......
......@@ -322,6 +322,12 @@ SELECT BOOL_OR(X>4) FROM SYSTEM_RANGE(1,6);
> TRUE;
SELECT BOOL_AND(X>4) FROM SYSTEM_RANGE(1,6);
> FALSE;
SELECT BIT_OR(X) FROM SYSTEM_RANGE(1,6);
> 7;
SELECT BIT_AND(X) FROM SYSTEM_RANGE(1,6);
> 0;
SELECT BIT_AND(X) FROM SYSTEM_RANGE(1,1);
> 1;
CREATE TABLE TEST(ID IDENTITY);
ALTER TABLE TEST ALTER COLUMN ID RESTART WITH ? {1:10};
INSERT INTO TEST VALUES(NULL);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论