ctubbsii commented on a change in pull request #2535:
URL: https://github.com/apache/accumulo/pull/2535#discussion_r816305874



##########
File path: 
core/src/main/java/org/apache/accumulo/core/client/lexicoder/BigDecimalLexicoder.java
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.accumulo.core.client.lexicoder;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+import 
org.apache.accumulo.core.clientImpl.lexicoder.FixedByteArrayOutputStream;
+
+public class BigDecimalLexicoder extends AbstractLexicoder<BigDecimal> {
+
+  BigIntegerLexicoder bigIntegerLexicoder = new BigIntegerLexicoder();

Review comment:
       Can this be private final?

##########
File path: 
core/src/test/java/org/apache/accumulo/core/client/lexicoder/BigDecimalLexicoderTest.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.accumulo.core.client.lexicoder;
+
+import java.math.BigDecimal;
+import java.util.Arrays;
+
+import org.apache.accumulo.core.clientImpl.lexicoder.AbstractLexicoderTest;
+import org.junit.jupiter.api.Test;
+
+public class BigDecimalLexicoderTest extends AbstractLexicoderTest {
+
+  @Test
+  public void testSortOrder() {
+
+    assertSortOrder(new BigDecimalLexicoder(), BigDecimal::compareTo,
+        Arrays.asList(new BigDecimal("2.0"), new BigDecimal("2.00"), new 
BigDecimal("2.000"),
+            new BigDecimal("-3.000"), new BigDecimal("-2.00"), new 
BigDecimal("0.0000"),
+            new BigDecimal("0.1"), new BigDecimal("0.10"), new 
BigDecimal("-65537.000"),
+            new BigDecimal("-65537.00"), new BigDecimal("-65537.0")));
+

Review comment:
       But also, shouldn't we expect these to be ordered numerically? These are 
all mixed up. I don't think that's what users are going to want.

##########
File path: 
core/src/main/java/org/apache/accumulo/core/client/lexicoder/BigDecimalLexicoder.java
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.accumulo.core.client.lexicoder;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+import 
org.apache.accumulo.core.clientImpl.lexicoder.FixedByteArrayOutputStream;
+
+public class BigDecimalLexicoder extends AbstractLexicoder<BigDecimal> {
+
+  BigIntegerLexicoder bigIntegerLexicoder = new BigIntegerLexicoder();
+
+  @Override
+  public BigDecimal decode(byte[] b) {
+    // This concrete implementation is provided for binary compatibility, 
since the corresponding
+    // superclass method has type-erased return type Object. See ACCUMULO-3789 
and #1285.
+    return super.decode(b);
+  }
+
+  @Override
+  public byte[] encode(BigDecimal bd) {
+
+    try {
+      int scale = bd.scale();
+      BigInteger bigInt = bd.unscaledValue();
+      byte[] encodedbigInt = bigIntegerLexicoder.encode(bigInt);
+      byte[] ret = new byte[4 + encodedbigInt.length];
+      int len = ret.length;
+      DataOutputStream dos = new DataOutputStream(new 
FixedByteArrayOutputStream(ret));
+      len = len ^ 0x80000000;
+      scale = scale ^ 0x80000000;
+      dos.write(encodedbigInt);
+      dos.writeInt(scale);
+      dos.close();
+      return ret;
+    } catch (IOException e) {
+      throw new RuntimeException(e);

Review comment:
       There is an UncheckedIOException that would be suitable instead of a 
generic RTE.

##########
File path: 
core/src/test/java/org/apache/accumulo/core/client/lexicoder/BigDecimalLexicoderTest.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.accumulo.core.client.lexicoder;
+
+import java.math.BigDecimal;
+import java.util.Arrays;
+
+import org.apache.accumulo.core.clientImpl.lexicoder.AbstractLexicoderTest;
+import org.junit.jupiter.api.Test;
+
+public class BigDecimalLexicoderTest extends AbstractLexicoderTest {
+
+  @Test
+  public void testSortOrder() {
+
+    assertSortOrder(new BigDecimalLexicoder(), BigDecimal::compareTo,

Review comment:
       I'm not sure BigDecimal's compareTo method is a sufficient comparator 
here. It will treat things as equal if they are equal in magnitude, but with 
different scale. However, when we serialize, we want to preserve the scale. We 
should want to keep the order predictable, so the scale must be considered as 
part of the comparison. This test may pass, but so would other orderings. But, 
we need to ensure that the test fails if other orderings pass.

##########
File path: 
core/src/test/java/org/apache/accumulo/core/client/lexicoder/BigDecimalLexicoderTest.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.accumulo.core.client.lexicoder;
+
+import java.math.BigDecimal;
+import java.util.Arrays;
+
+import org.apache.accumulo.core.clientImpl.lexicoder.AbstractLexicoderTest;
+import org.junit.jupiter.api.Test;
+
+public class BigDecimalLexicoderTest extends AbstractLexicoderTest {
+
+  @Test
+  public void testSortOrder() {
+
+    assertSortOrder(new BigDecimalLexicoder(), BigDecimal::compareTo,
+        Arrays.asList(new BigDecimal("2.0"), new BigDecimal("2.00"), new 
BigDecimal("2.000"),
+            new BigDecimal("-3.000"), new BigDecimal("-2.00"), new 
BigDecimal("0.0000"),
+            new BigDecimal("0.1"), new BigDecimal("0.10"), new 
BigDecimal("-65537.000"),
+            new BigDecimal("-65537.00"), new BigDecimal("-65537.0")));
+
+  }
+
+  @Test
+  public void testDecode() {
+    assertDecodes(new BigDecimalLexicoder(), BigDecimal.valueOf(-3.000));

Review comment:
       BigDecimalLexicoder can be constructed once and reused. It should be 
stateless, so we don't need more than one for the test.

##########
File path: 
core/src/main/java/org/apache/accumulo/core/client/lexicoder/BigDecimalLexicoder.java
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.accumulo.core.client.lexicoder;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+import 
org.apache.accumulo.core.clientImpl.lexicoder.FixedByteArrayOutputStream;
+
+public class BigDecimalLexicoder extends AbstractLexicoder<BigDecimal> {
+
+  BigIntegerLexicoder bigIntegerLexicoder = new BigIntegerLexicoder();
+
+  @Override
+  public BigDecimal decode(byte[] b) {
+    // This concrete implementation is provided for binary compatibility, 
since the corresponding
+    // superclass method has type-erased return type Object. See ACCUMULO-3789 
and #1285.
+    return super.decode(b);
+  }
+
+  @Override
+  public byte[] encode(BigDecimal bd) {
+
+    try {
+      int scale = bd.scale();
+      BigInteger bigInt = bd.unscaledValue();
+      byte[] encodedbigInt = bigIntegerLexicoder.encode(bigInt);
+      byte[] ret = new byte[4 + encodedbigInt.length];
+      int len = ret.length;
+      DataOutputStream dos = new DataOutputStream(new 
FixedByteArrayOutputStream(ret));
+      len = len ^ 0x80000000;
+      scale = scale ^ 0x80000000;
+      dos.write(encodedbigInt);
+      dos.writeInt(scale);
+      dos.close();

Review comment:
       This can be a try-with-resources block.

##########
File path: 
core/src/test/java/org/apache/accumulo/core/client/lexicoder/BigDecimalLexicoderTest.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.accumulo.core.client.lexicoder;
+
+import java.math.BigDecimal;
+import java.util.Arrays;
+
+import org.apache.accumulo.core.clientImpl.lexicoder.AbstractLexicoderTest;
+import org.junit.jupiter.api.Test;
+
+public class BigDecimalLexicoderTest extends AbstractLexicoderTest {
+
+  @Test
+  public void testSortOrder() {
+
+    assertSortOrder(new BigDecimalLexicoder(), BigDecimal::compareTo,
+        Arrays.asList(new BigDecimal("2.0"), new BigDecimal("2.00"), new 
BigDecimal("2.000"),
+            new BigDecimal("-3.000"), new BigDecimal("-2.00"), new 
BigDecimal("0.0000"),
+            new BigDecimal("0.1"), new BigDecimal("0.10"), new 
BigDecimal("-65537.000"),
+            new BigDecimal("-65537.00"), new BigDecimal("-65537.0")));
+

Review comment:
       This might be more readable (formatting might be off):
   
   ```suggestion
           List.of("2.0", "2.00", "2.000", "-3.000", "-2.00", "0.0000", "0.1", 
"0.10", "-65537.000", "-65537.00", "-65537.0")
               .stream().map(BigDecimal::new).collect(Collectors.toList()));
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to