forked from apache/paimon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Support product function for agg table (apache#2572)
- Loading branch information
Showing
4 changed files
with
208 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
paimon-core/src/main/java/org/apache/paimon/mergetree/compact/aggregate/FieldProductAgg.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
|
||
package org.apache.paimon.mergetree.compact.aggregate; | ||
|
||
import org.apache.paimon.data.Decimal; | ||
import org.apache.paimon.types.DataType; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import static org.apache.paimon.data.Decimal.fromBigDecimal; | ||
|
||
/** product value aggregate a field of a row. */ | ||
public class FieldProductAgg extends FieldAggregator { | ||
|
||
public static final String NAME = "product"; | ||
|
||
public FieldProductAgg(DataType dataType) { | ||
super(dataType); | ||
} | ||
|
||
@Override | ||
String name() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public Object agg(Object accumulator, Object inputField) { | ||
Object product; | ||
|
||
if (accumulator == null || inputField == null) { | ||
product = (accumulator == null ? inputField : accumulator); | ||
} else { | ||
// ordered by type root definition | ||
switch (fieldType.getTypeRoot()) { | ||
case DECIMAL: | ||
Decimal mergeFieldDD = (Decimal) accumulator; | ||
Decimal inFieldDD = (Decimal) inputField; | ||
assert mergeFieldDD.scale() == inFieldDD.scale() | ||
: "Inconsistent scale of aggregate Decimal!"; | ||
assert mergeFieldDD.precision() == inFieldDD.precision() | ||
: "Inconsistent precision of aggregate Decimal!"; | ||
BigDecimal bigDecimal = mergeFieldDD.toBigDecimal(); | ||
BigDecimal bigDecimal1 = inFieldDD.toBigDecimal(); | ||
BigDecimal mul = bigDecimal.multiply(bigDecimal1); | ||
product = fromBigDecimal(mul, mergeFieldDD.precision(), mergeFieldDD.scale()); | ||
break; | ||
case TINYINT: | ||
product = (byte) ((byte) accumulator * (byte) inputField); | ||
break; | ||
case SMALLINT: | ||
product = (short) ((short) accumulator * (short) inputField); | ||
break; | ||
case INTEGER: | ||
product = (int) accumulator * (int) inputField; | ||
break; | ||
case BIGINT: | ||
product = (long) accumulator * (long) inputField; | ||
break; | ||
case FLOAT: | ||
product = (float) accumulator * (float) inputField; | ||
break; | ||
case DOUBLE: | ||
product = (double) accumulator * (double) inputField; | ||
break; | ||
default: | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
return product; | ||
} | ||
|
||
@Override | ||
public Object retract(Object accumulator, Object inputField) { | ||
Object product; | ||
|
||
if (accumulator == null || inputField == null) { | ||
product = (accumulator == null ? inputField : accumulator); | ||
} else { | ||
switch (fieldType.getTypeRoot()) { | ||
case DECIMAL: | ||
Decimal mergeFieldDD = (Decimal) accumulator; | ||
Decimal inFieldDD = (Decimal) inputField; | ||
assert mergeFieldDD.scale() == inFieldDD.scale() | ||
: "Inconsistent scale of aggregate Decimal!"; | ||
assert mergeFieldDD.precision() == inFieldDD.precision() | ||
: "Inconsistent precision of aggregate Decimal!"; | ||
BigDecimal bigDecimal = mergeFieldDD.toBigDecimal(); | ||
BigDecimal bigDecimal1 = inFieldDD.toBigDecimal(); | ||
BigDecimal div = bigDecimal.divide(bigDecimal1); | ||
product = fromBigDecimal(div, mergeFieldDD.precision(), mergeFieldDD.scale()); | ||
break; | ||
case TINYINT: | ||
product = (byte) ((byte) accumulator / (byte) inputField); | ||
break; | ||
case SMALLINT: | ||
product = (short) ((short) accumulator / (short) inputField); | ||
break; | ||
case INTEGER: | ||
product = (int) accumulator / (int) inputField; | ||
break; | ||
case BIGINT: | ||
product = (long) accumulator / (long) inputField; | ||
break; | ||
case FLOAT: | ||
product = (float) accumulator / (float) inputField; | ||
break; | ||
case DOUBLE: | ||
product = (double) accumulator / (double) inputField; | ||
break; | ||
default: | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
return product; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters