You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@drill.apache.org by cg...@apache.org on 2021/02/03 05:12:49 UTC
[drill] branch master updated: DRILL-7812: Updated hashCode
Function for MaterializedField (#2117)
This is an automated email from the ASF dual-hosted git repository.
cgivre pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/drill.git
The following commit(s) were added to refs/heads/master by this push:
new e8bb50d DRILL-7812: Updated hashCode Function for MaterializedField (#2117)
e8bb50d is described below
commit e8bb50d23c31729216a08c41351b407e4d4237e6
Author: Rymar Maksym <mr...@cybervisiontech.com>
AuthorDate: Wed Feb 3 07:12:38 2021 +0200
DRILL-7812: Updated hashCode Function for MaterializedField (#2117)
* DRILL-7812: updated hashCode function for MaterializedField
* Add test. Fixed vulnerability of NullPointerException
* Removed unused import
---
.../drill/exec/record/MaterializedField.java | 3 +-
.../drill/exec/record/TestMaterializedField.java | 91 ++++++++++++++++++++++
2 files changed, 93 insertions(+), 1 deletion(-)
diff --git a/exec/vector/src/main/java/org/apache/drill/exec/record/MaterializedField.java b/exec/vector/src/main/java/org/apache/drill/exec/record/MaterializedField.java
index 40fe40e..19e0ea3 100644
--- a/exec/vector/src/main/java/org/apache/drill/exec/record/MaterializedField.java
+++ b/exec/vector/src/main/java/org/apache/drill/exec/record/MaterializedField.java
@@ -189,7 +189,8 @@ public class MaterializedField {
@Override
public int hashCode() {
- return Objects.hash(this.name, this.type, this.children);
+ String name = this.name == null ? null : this.name.toLowerCase();
+ return Objects.hash(name, this.type);
}
/**
diff --git a/exec/vector/src/test/java/org/apache/drill/exec/record/TestMaterializedField.java b/exec/vector/src/test/java/org/apache/drill/exec/record/TestMaterializedField.java
new file mode 100644
index 0000000..10c69ec
--- /dev/null
+++ b/exec/vector/src/test/java/org/apache/drill/exec/record/TestMaterializedField.java
@@ -0,0 +1,91 @@
+/*
+ * 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.drill.exec.record;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+import org.apache.drill.common.types.Types;
+import org.junit.Test;
+
+public class TestMaterializedField {
+
+ @Test
+ public void testHashCodeContact() {
+ MaterializedField childField = MaterializedField.create(new String("child"), Types.OPTIONAL_BIT);
+ MaterializedField field = MaterializedField.create(new String("field"), Types.OPTIONAL_INT);
+ MaterializedField equalField = MaterializedField.create(new String("field"), Types.OPTIONAL_INT);
+ field.addChild(childField);
+ equalField.addChild(childField);
+
+ //equal fields
+ assertEquals(field.hashCode(), equalField.hashCode());
+
+ //equal fields with different case of field name
+ equalField = MaterializedField.create(new String("FIELD"), Types.OPTIONAL_INT);
+ equalField.addChild(childField);
+ assertEquals(field.hashCode(), equalField.hashCode());
+
+ //not equal fields
+ MaterializedField differentField = MaterializedField.create(new String("other"), Types.OPTIONAL_BIT);
+ differentField.addChild(childField);
+ assertNotEquals(field.hashCode(), differentField.hashCode());
+
+ //no NullPointerException on field with null name
+ field = MaterializedField.create(null, Types.OPTIONAL_INT);
+ field.hashCode();
+ }
+
+ @Test
+ public void testEqualsContract() {
+ MaterializedField childField = MaterializedField.create(new String("child"), Types.OPTIONAL_BIT);
+ MaterializedField field = MaterializedField.create(new String("field"), Types.OPTIONAL_INT);
+ MaterializedField equalField = MaterializedField.create(new String("field"), Types.OPTIONAL_INT);
+ field.addChild(childField);
+ equalField.addChild(childField);
+
+ //reflexivity
+ assertEquals(field, field);
+
+ //symmetry
+ assertEquals(field, equalField);
+ assertEquals(equalField, field);
+
+ //comparison with null
+ assertNotEquals(field, null);
+
+ //different type
+ MaterializedField differentField = MaterializedField.create(new String("field"), Types.OPTIONAL_BIT);
+ differentField.addChild(childField);
+ assertNotEquals(field, differentField);
+
+ //different name
+ differentField = MaterializedField.create(new String("other"), Types.OPTIONAL_INT);
+ differentField.addChild(childField);
+ assertNotEquals(field, differentField);
+
+ //different child
+ equalField.addChild(MaterializedField.create("field", Types.OPTIONAL_BIT));
+ assertEquals(field, equalField);
+
+ //different name case
+ equalField = MaterializedField.create("FIELD", Types.OPTIONAL_INT);
+ equalField.addChild(childField);
+ assertEquals(field, equalField);
+ }
+}