Repository: hive Updated Branches: refs/heads/master 2d8e48c75 -> 45faab2f4
HIVE-19466: Update constraint violation error message(Vineet Garg, reviewed by Jesus Camacho Rodriguez) Project: http://git-wip-us.apache.org/repos/asf/hive/repo Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/45faab2f Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/45faab2f Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/45faab2f Branch: refs/heads/master Commit: 45faab2f4c6252971d7145e46aa336784e28d602 Parents: 2d8e48c Author: Vineet Garg <vg...@apache.org> Authored: Thu May 10 14:12:04 2018 -0700 Committer: Vineet Garg <vg...@apache.org> Committed: Thu May 10 14:12:04 2018 -0700 ---------------------------------------------------------------------- .../hadoop/hive/ql/exec/FunctionRegistry.java | 2 +- .../generic/GenericUDFEnforceConstraint.java | 77 ++++++++++++++++++++ .../GenericUDFEnforceNotNullConstraint.java | 77 -------------------- .../TestGenericUDFEnforceConstraint.java | 75 +++++++++++++++++++ .../TestGenericUDFEnforceNotNullConstraint.java | 75 ------------------- .../alter_notnull_constraint_violation.q.out | 2 +- .../insert_into_acid_notnull.q.out | 2 +- .../insert_into_notnull_constraint.q.out | 2 +- .../insert_multi_into_notnull.q.out | 2 +- .../insert_overwrite_notnull_constraint.q.out | 2 +- .../update_notnull_constraint.q.out | 2 +- 11 files changed, 159 insertions(+), 159 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hive/blob/45faab2f/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java ---------------------------------------------------------------------- diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java index 4611ce9..a1f549a 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java @@ -214,7 +214,7 @@ public final class FunctionRegistry { system.registerUDF("rand", UDFRand.class, false); system.registerGenericUDF("abs", GenericUDFAbs.class); system.registerGenericUDF("sq_count_check", GenericUDFSQCountCheck.class); - system.registerGenericUDF("enforce_constraint", GenericUDFEnforceNotNullConstraint.class); + system.registerGenericUDF("enforce_constraint", GenericUDFEnforceConstraint.class); system.registerGenericUDF("pmod", GenericUDFPosMod.class); system.registerUDF("ln", UDFLn.class, false); http://git-wip-us.apache.org/repos/asf/hive/blob/45faab2f/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEnforceConstraint.java ---------------------------------------------------------------------- diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEnforceConstraint.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEnforceConstraint.java new file mode 100644 index 0000000..aa0059b --- /dev/null +++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEnforceConstraint.java @@ -0,0 +1,77 @@ +/* + * 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.hadoop.hive.ql.udf.generic; + +import org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException; +import org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.io.BooleanWritable; + +/** + * GenericUDFAbs. + * + */ +@Description(name = "enforce_constraint", + value = "_FUNC_(x) - Internal UDF to enforce CHECK and NOT NULL constraint", + extended = "For internal use only") +public class GenericUDFEnforceConstraint extends GenericUDF { + private final BooleanWritable resultBool = new BooleanWritable(); + private transient BooleanObjectInspector boi; + + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { + if (arguments.length > 1) { + throw new UDFArgumentLengthException( + "Invalid number of arguments. enforce_constraint UDF expected one argument but received: " + + arguments.length); + } + + boi = (BooleanObjectInspector) arguments[0]; + return PrimitiveObjectInspectorFactory.writableBooleanObjectInspector; + } + + @Override + public Object evaluate(DeferredObject[] arguments) throws HiveException { + + Object a = arguments[0].get(); + boolean result = boi.get(a); + + if(!result) { + throw new DataConstraintViolationError( + "Either CHECK or NOT NULL constraint violated!"); + } + resultBool.set(true); + return resultBool; + } + + @Override + protected String getFuncName() { + return "enforce_constraint"; + } + + @Override + public String getDisplayString(String[] children) { + return getStandardDisplayString(getFuncName(), children); + } +} http://git-wip-us.apache.org/repos/asf/hive/blob/45faab2f/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEnforceNotNullConstraint.java ---------------------------------------------------------------------- diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEnforceNotNullConstraint.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEnforceNotNullConstraint.java deleted file mode 100644 index 6c8c6fd..0000000 --- a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEnforceNotNullConstraint.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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.hadoop.hive.ql.udf.generic; - -import org.apache.hadoop.hive.ql.exec.Description; -import org.apache.hadoop.hive.ql.exec.UDFArgumentException; -import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException; -import org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError; -import org.apache.hadoop.hive.ql.metadata.HiveException; -import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; -import org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector; -import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; -import org.apache.hadoop.io.BooleanWritable; - -/** - * GenericUDFAbs. - * - */ -@Description(name = "enforce_constraint", - value = "_FUNC_(x) - Internal UDF to enforce NOT NULL constraint", - extended = "For internal use only") -public class GenericUDFEnforceNotNullConstraint extends GenericUDF { - private final BooleanWritable resultBool = new BooleanWritable(); - private transient BooleanObjectInspector boi; - - @Override - public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { - if (arguments.length > 1) { - throw new UDFArgumentLengthException( - "Invalid number of arguments. enforce_constraint UDF expected one argument but received: " - + arguments.length); - } - - boi = (BooleanObjectInspector) arguments[0]; - return PrimitiveObjectInspectorFactory.writableBooleanObjectInspector; - } - - @Override - public Object evaluate(DeferredObject[] arguments) throws HiveException { - - Object a = arguments[0].get(); - boolean result = boi.get(a); - - if(!result) { - throw new DataConstraintViolationError( - "NOT NULL constraint violated!"); - } - resultBool.set(true); - return resultBool; - } - - @Override - protected String getFuncName() { - return "enforce_constraint"; - } - - @Override - public String getDisplayString(String[] children) { - return getStandardDisplayString(getFuncName(), children); - } -} http://git-wip-us.apache.org/repos/asf/hive/blob/45faab2f/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFEnforceConstraint.java ---------------------------------------------------------------------- diff --git a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFEnforceConstraint.java b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFEnforceConstraint.java new file mode 100644 index 0000000..a0da723 --- /dev/null +++ b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFEnforceConstraint.java @@ -0,0 +1,75 @@ +/* + * 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.hadoop.hive.ql.udf.generic; + +import junit.framework.TestCase; +import org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.io.BooleanWritable; + +/** + * Test class for {@link GenericUDFEnforceConstraint}. + */ +public class TestGenericUDFEnforceConstraint extends TestCase { + + public void testNull() throws HiveException { + try { + GenericUDFEnforceConstraint udf = new GenericUDFEnforceConstraint(); + ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableBooleanObjectInspector; + ObjectInspector[] arguments = {valueOI }; + udf.initialize(arguments); + + BooleanWritable input = new BooleanWritable(false); + GenericUDF.DeferredObject[] args = {new GenericUDF.DeferredJavaObject(input) }; + udf.evaluate(args); + fail("Unreachable line"); + } catch (DataConstraintViolationError e) { + //DataConstraintViolationError is expected + assertTrue(e.getMessage().contains("NOT NULL constraint violated!")); + } + } + + public void testInvalidArgumentsLength() throws HiveException { + try { + GenericUDFEnforceConstraint udf = new GenericUDFEnforceConstraint(); + ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.writableBooleanObjectInspector; + ObjectInspector valueOI2 = PrimitiveObjectInspectorFactory.writableBooleanObjectInspector; + ObjectInspector[] arguments = {valueOI1, valueOI2 }; + udf.initialize(arguments); + fail("Unreachable line"); + } catch (HiveException e) { + //HiveException is expected + assertTrue(e.getMessage().contains("Invalid number of arguments")); + } + } + + public void testCorrect() throws HiveException { + GenericUDFEnforceConstraint udf = new GenericUDFEnforceConstraint(); + ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableBooleanObjectInspector; + ObjectInspector[] arguments = {valueOI }; + udf.initialize(arguments); + + BooleanWritable input = new BooleanWritable(true); + GenericUDF.DeferredObject[] args = {new GenericUDF.DeferredJavaObject(input) }; + BooleanWritable writable = (BooleanWritable) udf.evaluate(args); + assertTrue("Not expected result: expected [true] actual [ " + writable.get() + " ]", writable.get()); + } +} http://git-wip-us.apache.org/repos/asf/hive/blob/45faab2f/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFEnforceNotNullConstraint.java ---------------------------------------------------------------------- diff --git a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFEnforceNotNullConstraint.java b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFEnforceNotNullConstraint.java deleted file mode 100644 index fc65bb6e..0000000 --- a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFEnforceNotNullConstraint.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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.hadoop.hive.ql.udf.generic; - -import junit.framework.TestCase; -import org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError; -import org.apache.hadoop.hive.ql.metadata.HiveException; -import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; -import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; -import org.apache.hadoop.io.BooleanWritable; - -/** - * Test class for {@link GenericUDFEnforceNotNullConstraint}. - */ -public class TestGenericUDFEnforceNotNullConstraint extends TestCase { - - public void testNull() throws HiveException { - try { - GenericUDFEnforceNotNullConstraint udf = new GenericUDFEnforceNotNullConstraint(); - ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableBooleanObjectInspector; - ObjectInspector[] arguments = {valueOI }; - udf.initialize(arguments); - - BooleanWritable input = new BooleanWritable(false); - GenericUDF.DeferredObject[] args = {new GenericUDF.DeferredJavaObject(input) }; - udf.evaluate(args); - fail("Unreachable line"); - } catch (DataConstraintViolationError e) { - //DataConstraintViolationError is expected - assertTrue(e.getMessage().contains("NOT NULL constraint violated!")); - } - } - - public void testInvalidArgumentsLength() throws HiveException { - try { - GenericUDFEnforceNotNullConstraint udf = new GenericUDFEnforceNotNullConstraint(); - ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.writableBooleanObjectInspector; - ObjectInspector valueOI2 = PrimitiveObjectInspectorFactory.writableBooleanObjectInspector; - ObjectInspector[] arguments = {valueOI1, valueOI2 }; - udf.initialize(arguments); - fail("Unreachable line"); - } catch (HiveException e) { - //HiveException is expected - assertTrue(e.getMessage().contains("Invalid number of arguments")); - } - } - - public void testCorrect() throws HiveException { - GenericUDFEnforceNotNullConstraint udf = new GenericUDFEnforceNotNullConstraint(); - ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableBooleanObjectInspector; - ObjectInspector[] arguments = {valueOI }; - udf.initialize(arguments); - - BooleanWritable input = new BooleanWritable(true); - GenericUDF.DeferredObject[] args = {new GenericUDF.DeferredJavaObject(input) }; - BooleanWritable writable = (BooleanWritable) udf.evaluate(args); - assertTrue("Not expected result: expected [true] actual [ " + writable.get() + " ]", writable.get()); - } -} http://git-wip-us.apache.org/repos/asf/hive/blob/45faab2f/ql/src/test/results/clientnegative/alter_notnull_constraint_violation.q.out ---------------------------------------------------------------------- diff --git a/ql/src/test/results/clientnegative/alter_notnull_constraint_violation.q.out b/ql/src/test/results/clientnegative/alter_notnull_constraint_violation.q.out index 65195dc..2445b5d 100644 --- a/ql/src/test/results/clientnegative/alter_notnull_constraint_violation.q.out +++ b/ql/src/test/results/clientnegative/alter_notnull_constraint_violation.q.out @@ -24,4 +24,4 @@ POSTHOOK: query: alter table t1 change j j int constraint nn0 not null enforced POSTHOOK: type: ALTERTABLE_RENAMECOL POSTHOOK: Input: default@t1 POSTHOOK: Output: default@t1 -FAILED: DataConstraintViolationError org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError: NOT NULL constraint violated! +FAILED: DataConstraintViolationError org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError: Either CHECK or NOT NULL constraint violated! http://git-wip-us.apache.org/repos/asf/hive/blob/45faab2f/ql/src/test/results/clientnegative/insert_into_acid_notnull.q.out ---------------------------------------------------------------------- diff --git a/ql/src/test/results/clientnegative/insert_into_acid_notnull.q.out b/ql/src/test/results/clientnegative/insert_into_acid_notnull.q.out index 172c933..777a087 100644 --- a/ql/src/test/results/clientnegative/insert_into_acid_notnull.q.out +++ b/ql/src/test/results/clientnegative/insert_into_acid_notnull.q.out @@ -10,4 +10,4 @@ POSTHOOK: query: create table acid_uami(i int, POSTHOOK: type: CREATETABLE POSTHOOK: Output: database:default POSTHOOK: Output: default@acid_uami -FAILED: DataConstraintViolationError org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError: NOT NULL constraint violated! +FAILED: DataConstraintViolationError org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError: Either CHECK or NOT NULL constraint violated! http://git-wip-us.apache.org/repos/asf/hive/blob/45faab2f/ql/src/test/results/clientnegative/insert_into_notnull_constraint.q.out ---------------------------------------------------------------------- diff --git a/ql/src/test/results/clientnegative/insert_into_notnull_constraint.q.out b/ql/src/test/results/clientnegative/insert_into_notnull_constraint.q.out index dd720fa..96feec0 100644 --- a/ql/src/test/results/clientnegative/insert_into_notnull_constraint.q.out +++ b/ql/src/test/results/clientnegative/insert_into_notnull_constraint.q.out @@ -6,4 +6,4 @@ POSTHOOK: query: create table nullConstraintCheck(i int NOT NULL enforced, j int POSTHOOK: type: CREATETABLE POSTHOOK: Output: database:default POSTHOOK: Output: default@nullConstraintCheck -FAILED: DataConstraintViolationError org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError: NOT NULL constraint violated! +FAILED: DataConstraintViolationError org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError: Either CHECK or NOT NULL constraint violated! http://git-wip-us.apache.org/repos/asf/hive/blob/45faab2f/ql/src/test/results/clientnegative/insert_multi_into_notnull.q.out ---------------------------------------------------------------------- diff --git a/ql/src/test/results/clientnegative/insert_multi_into_notnull.q.out b/ql/src/test/results/clientnegative/insert_multi_into_notnull.q.out index 1beeb26..74e112f 100644 --- a/ql/src/test/results/clientnegative/insert_multi_into_notnull.q.out +++ b/ql/src/test/results/clientnegative/insert_multi_into_notnull.q.out @@ -14,4 +14,4 @@ POSTHOOK: query: create table src_multi2 (i STRING, j STRING NOT NULL ENFORCED) POSTHOOK: type: CREATETABLE POSTHOOK: Output: database:default POSTHOOK: Output: default@src_multi2 -FAILED: DataConstraintViolationError org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError: NOT NULL constraint violated! +FAILED: DataConstraintViolationError org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError: Either CHECK or NOT NULL constraint violated! http://git-wip-us.apache.org/repos/asf/hive/blob/45faab2f/ql/src/test/results/clientnegative/insert_overwrite_notnull_constraint.q.out ---------------------------------------------------------------------- diff --git a/ql/src/test/results/clientnegative/insert_overwrite_notnull_constraint.q.out b/ql/src/test/results/clientnegative/insert_overwrite_notnull_constraint.q.out index dd720fa..96feec0 100644 --- a/ql/src/test/results/clientnegative/insert_overwrite_notnull_constraint.q.out +++ b/ql/src/test/results/clientnegative/insert_overwrite_notnull_constraint.q.out @@ -6,4 +6,4 @@ POSTHOOK: query: create table nullConstraintCheck(i int NOT NULL enforced, j int POSTHOOK: type: CREATETABLE POSTHOOK: Output: database:default POSTHOOK: Output: default@nullConstraintCheck -FAILED: DataConstraintViolationError org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError: NOT NULL constraint violated! +FAILED: DataConstraintViolationError org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError: Either CHECK or NOT NULL constraint violated! http://git-wip-us.apache.org/repos/asf/hive/blob/45faab2f/ql/src/test/results/clientnegative/update_notnull_constraint.q.out ---------------------------------------------------------------------- diff --git a/ql/src/test/results/clientnegative/update_notnull_constraint.q.out b/ql/src/test/results/clientnegative/update_notnull_constraint.q.out index 8748681..86bfc67 100644 --- a/ql/src/test/results/clientnegative/update_notnull_constraint.q.out +++ b/ql/src/test/results/clientnegative/update_notnull_constraint.q.out @@ -21,4 +21,4 @@ POSTHOOK: Output: default@acid_uami POSTHOOK: Lineage: acid_uami.de SCRIPT [] POSTHOOK: Lineage: acid_uami.i SCRIPT [] POSTHOOK: Lineage: acid_uami.vc SCRIPT [] -FAILED: DataConstraintViolationError org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError: NOT NULL constraint violated! +FAILED: DataConstraintViolationError org.apache.hadoop.hive.ql.exec.errors.DataConstraintViolationError: Either CHECK or NOT NULL constraint violated!