Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@ public void write(int ordinal, Decimal input, int precision, int scale) {
// grow the global buffer before writing data.
holder.grow(16);

// always zero-out the 16-byte buffer
Platform.putLong(getBuffer(), cursor(), 0L);
Platform.putLong(getBuffer(), cursor() + 8, 0L);

// Make sure Decimal object has the same scale as DecimalType.
// Note that we may pass in null Decimal object to set null for it.
if (input == null || !input.changePrecision(precision, scale)) {
// zero-out the bytes
Platform.putLong(getBuffer(), cursor(), 0L);
Platform.putLong(getBuffer(), cursor() + 8, 0L);

BitSetMethods.set(getBuffer(), startingOffset, ordinal);
// keep the offset for future update
setOffsetAndSize(ordinal, 0);
Expand All @@ -200,8 +200,6 @@ public void write(int ordinal, Decimal input, int precision, int scale) {
final int numBytes = bytes.length;
assert numBytes <= 16;

zeroOutPaddingBytes(numBytes);

// Write the bytes to the variable length portion.
Platform.copyMemory(
bytes, Platform.BYTE_ARRAY_OFFSET, getBuffer(), cursor(), numBytes);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.spark.sql.catalyst.expressions.codegen

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.types.Decimal

class UnsafeWriterSuite extends SparkFunSuite {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnsafeWriterSuite -> UnsafeRowWriterSuite? Also, renaming the file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is necessary, as we may want to include here also tests for other UnsafeWriter in the future.

Copy link
Member

@dongjoon-hyun dongjoon-hyun Oct 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. We had better have both UnsafeRowWriterSuite and UnsafeWriterSuite in the future if needed.


test("SPARK-25538: zero-out all bits for decimals") {
// This decimal holds 8 bytes
val decimal1 = Decimal(0.431)
decimal1.changePrecision(38, 18)
// This decimal holds 11 bytes
val decimal2 = Decimal(123456789.1232456789)
decimal2.changePrecision(38, 18)
val unsafeRowWriter = new UnsafeRowWriter(1)
unsafeRowWriter.resetRowWriter()
unsafeRowWriter.write(0, decimal2, decimal2.precision, decimal2.scale)
unsafeRowWriter.reset()
unsafeRowWriter.write(0, decimal1, decimal1.precision, decimal1.scale)
val res = unsafeRowWriter.getRow
assert(res.getDecimal(0, decimal1.precision, decimal1.scale) == decimal1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think this assert is strong enough, we don't need to check the zero bytes below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would pass also before the fix, as only the first 8 bytes are read. I added it as an additional safety fix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then this doesn't demonstrate how this bug could affect correctness.

one better idea for this test: we create 2 row writers, one writes the decimal as what you did here, and the other one writes decimal 0.431 directly. Then we compare the 2 result rows and make sure they equals.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the suggestion @cloud-fan, I like this approach.

// Check that the bytes which are not used by decimal1 (but are allocated) are zero-ed out.
// The first 16 bytes are used for the offset and size, then 8 bytes contain the value of
// decimal1. So from byte 25 to byte 32 there is the leftover of decimal2 which should have
// been zero-ed.
assert(res.getBytes()(25) == 0x00)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a comment about how we get the 25?

}

}