From a2aa78d7f4997be515c8e3d9a9795dc88a21c5d7 Mon Sep 17 00:00:00 2001 From: Zhao Yang Date: Mon, 10 Dec 2018 14:48:55 +0800 Subject: [PATCH] Avoid decimal-serializer.toString() creating huge string patch by Zhao Yang; reviewed by Benjamin Lerer for CASSANDRA-14925 --- CHANGES.txt | 1 + .../serializers/DecimalSerializer.java | 2 +- .../serializers/DecimalSerializerTest.java | 51 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 test/unit/org/apache/cassandra/serializers/DecimalSerializerTest.java diff --git a/CHANGES.txt b/CHANGES.txt index d560d9117e..53883bcb55 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 3.0.24: + * Fix DecimalDeserializer#toString OOM (CASSANDRA-14925) * Extend the exclusion of replica filtering protection to other indices instead of just SASI (CASSANDRA-16311) * Synchronize transaction logs for JBOD (CASSANDRA-16225) * Fix the counting of cells per partition (CASSANDRA-16259) diff --git a/src/java/org/apache/cassandra/serializers/DecimalSerializer.java b/src/java/org/apache/cassandra/serializers/DecimalSerializer.java index 819789f372..e7e178b9e6 100644 --- a/src/java/org/apache/cassandra/serializers/DecimalSerializer.java +++ b/src/java/org/apache/cassandra/serializers/DecimalSerializer.java @@ -67,7 +67,7 @@ public class DecimalSerializer implements TypeSerializer public String toString(BigDecimal value) { - return value == null ? "" : value.toPlainString(); + return value == null ? "" : value.toString(); } public Class getType() diff --git a/test/unit/org/apache/cassandra/serializers/DecimalSerializerTest.java b/test/unit/org/apache/cassandra/serializers/DecimalSerializerTest.java new file mode 100644 index 0000000000..83d30cec19 --- /dev/null +++ b/test/unit/org/apache/cassandra/serializers/DecimalSerializerTest.java @@ -0,0 +1,51 @@ +/** + * 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.cassandra.serializers; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +import java.math.BigDecimal; + +public class DecimalSerializerTest +{ + @Test + public void testDecimalScaleStringify() + { + BigDecimal d = new BigDecimal("1"); + assertEquals("1", DecimalSerializer.instance.toString(d)); + + d = new BigDecimal("-1"); + assertEquals("-1", DecimalSerializer.instance.toString(d)); + + int scale = Integer.MAX_VALUE - 100; + d = new BigDecimal("1e" + scale); + assertEquals("1E+" + scale, DecimalSerializer.instance.toString(d)); + + d = new BigDecimal("-1e" + scale); + assertEquals("-1E+" + scale, DecimalSerializer.instance.toString(d)); + + d = new BigDecimal("1e-" + scale); + assertEquals("1E-" + scale, DecimalSerializer.instance.toString(d)); + + d = new BigDecimal("-1e-" + scale); + assertEquals("-1E-" + scale, DecimalSerializer.instance.toString(d)); + } +}