-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize decimal state serializers for small value case
Given that many decimal aggregations (sum, avg) stay in the long range, aggregation state serializer can be optimized for this case, limiting the number of bytes per position significantly (3-4X) at the cost of small cpu overhead during serialization and deserialization.
- Loading branch information
1 parent
aee2179
commit 182b44e
Showing
5 changed files
with
266 additions
and
26 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
96 changes: 96 additions & 0 deletions
96
...o/trino/operator/aggregation/state/TestLongDecimalWithOverflowAndLongStateSerializer.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,96 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package io.trino.operator.aggregation.state; | ||
|
||
import io.trino.spi.block.Block; | ||
import io.trino.spi.block.BlockBuilder; | ||
import io.trino.spi.block.VariableWidthBlockBuilder; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertFalse; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
public class TestLongDecimalWithOverflowAndLongStateSerializer | ||
{ | ||
private static final LongDecimalWithOverflowAndLongStateFactory STATE_FACTORY = new LongDecimalWithOverflowAndLongStateFactory(); | ||
|
||
@Test(dataProvider = "input") | ||
public void testSerde(long low, long high, long overflow, long count, int expectedLength) | ||
{ | ||
LongDecimalWithOverflowAndLongState state = STATE_FACTORY.createSingleState(); | ||
state.getDecimalArray()[0] = high; | ||
state.getDecimalArray()[1] = low; | ||
state.setOverflow(overflow); | ||
state.setLong(count); | ||
state.setNotNull(); | ||
|
||
LongDecimalWithOverflowAndLongState outState = roundTrip(state, expectedLength); | ||
|
||
assertTrue(outState.isNotNull()); | ||
assertEquals(outState.getDecimalArray()[0], high); | ||
assertEquals(outState.getDecimalArray()[1], low); | ||
assertEquals(outState.getOverflow(), overflow); | ||
assertEquals(outState.getLong(), count); | ||
} | ||
|
||
@Test | ||
public void testNullSerde() | ||
{ | ||
// state is created null | ||
LongDecimalWithOverflowAndLongState state = STATE_FACTORY.createSingleState(); | ||
|
||
LongDecimalWithOverflowAndLongState outState = roundTrip(state, 0); | ||
|
||
assertFalse(outState.isNotNull()); | ||
} | ||
|
||
private LongDecimalWithOverflowAndLongState roundTrip(LongDecimalWithOverflowAndLongState state, int expectedLength) | ||
{ | ||
LongDecimalWithOverflowAndLongStateSerializer serializer = new LongDecimalWithOverflowAndLongStateSerializer(); | ||
BlockBuilder out = new VariableWidthBlockBuilder(null, 1, 0); | ||
|
||
serializer.serialize(state, out); | ||
|
||
Block serialized = out.build(); | ||
assertEquals(serialized.getSliceLength(0), expectedLength * Long.BYTES); | ||
LongDecimalWithOverflowAndLongState outState = STATE_FACTORY.createSingleState(); | ||
serializer.deserialize(serialized, 0, outState); | ||
return outState; | ||
} | ||
|
||
@DataProvider | ||
public Object[][] input() | ||
{ | ||
return new Object[][] { | ||
{3, 0, 0, 1, 1}, | ||
{3, 5, 0, 1, 2}, | ||
{3, 5, 7, 1, 4}, | ||
{3, 0, 0, 2, 3}, | ||
{3, 5, 0, 2, 4}, | ||
{3, 5, 7, 2, 4}, | ||
{3, 0, 7, 1, 3}, | ||
{3, 0, 7, 2, 3}, | ||
{0, 0, 0, 1, 1}, | ||
{0, 5, 0, 1, 2}, | ||
{0, 5, 7, 1, 4}, | ||
{0, 0, 0, 2, 3}, | ||
{0, 5, 0, 2, 4}, | ||
{0, 5, 7, 2, 4}, | ||
{0, 0, 7, 1, 3}, | ||
{0, 0, 7, 2, 3} | ||
}; | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
.../java/io/trino/operator/aggregation/state/TestLongDecimalWithOverflowStateSerializer.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,86 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package io.trino.operator.aggregation.state; | ||
|
||
import io.trino.spi.block.Block; | ||
import io.trino.spi.block.BlockBuilder; | ||
import io.trino.spi.block.VariableWidthBlockBuilder; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertFalse; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
public class TestLongDecimalWithOverflowStateSerializer | ||
{ | ||
private static final LongDecimalWithOverflowStateFactory STATE_FACTORY = new LongDecimalWithOverflowStateFactory(); | ||
|
||
@Test(dataProvider = "input") | ||
public void testSerde(long low, long high, long overflow, int expectedLength) | ||
{ | ||
LongDecimalWithOverflowState state = STATE_FACTORY.createSingleState(); | ||
state.getDecimalArray()[0] = high; | ||
state.getDecimalArray()[1] = low; | ||
state.setOverflow(overflow); | ||
state.setNotNull(); | ||
|
||
LongDecimalWithOverflowState outState = roundTrip(state, expectedLength); | ||
|
||
assertTrue(outState.isNotNull()); | ||
assertEquals(outState.getDecimalArray()[0], high); | ||
assertEquals(outState.getDecimalArray()[1], low); | ||
assertEquals(outState.getOverflow(), overflow); | ||
} | ||
|
||
@Test | ||
public void testNullSerde() | ||
{ | ||
// state is created null | ||
LongDecimalWithOverflowState state = STATE_FACTORY.createSingleState(); | ||
|
||
LongDecimalWithOverflowState outState = roundTrip(state, 0); | ||
|
||
assertFalse(outState.isNotNull()); | ||
} | ||
|
||
private LongDecimalWithOverflowState roundTrip(LongDecimalWithOverflowState state, int expectedLength) | ||
{ | ||
LongDecimalWithOverflowStateSerializer serializer = new LongDecimalWithOverflowStateSerializer(); | ||
BlockBuilder out = new VariableWidthBlockBuilder(null, 1, 0); | ||
|
||
serializer.serialize(state, out); | ||
|
||
Block serialized = out.build(); | ||
assertEquals(serialized.getSliceLength(0), expectedLength * Long.BYTES); | ||
LongDecimalWithOverflowState outState = STATE_FACTORY.createSingleState(); | ||
serializer.deserialize(serialized, 0, outState); | ||
return outState; | ||
} | ||
|
||
@DataProvider | ||
public Object[][] input() | ||
{ | ||
return new Object[][] { | ||
{3, 0, 0, 1}, | ||
{3, 5, 0, 2}, | ||
{3, 5, 7, 3}, | ||
{3, 0, 7, 3}, | ||
{0, 0, 0, 1}, | ||
{0, 5, 0, 2}, | ||
{0, 5, 7, 3}, | ||
{0, 0, 7, 3} | ||
}; | ||
} | ||
} |
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