@Override public boolean isEqual(Object v1, Object v2) { HiveDecimal d1 = PrimitiveObjectInspectorUtils.getHiveDecimal(v1, (PrimitiveObjectInspector) expressionDef.getOI()); HiveDecimal d2 = PrimitiveObjectInspectorUtils.getHiveDecimal(v2, (PrimitiveObjectInspector) expressionDef.getOI()); if ( d1 != null && d2 != null ) { return d1.equals(d2); } return d1 == null && d2 == null; // True if both are null } }
@Override public boolean isEqual(Object v1, Object v2) { HiveDecimal d1 = PrimitiveObjectInspectorUtils.getHiveDecimal(v1, (PrimitiveObjectInspector) expressionDef.getOI()); HiveDecimal d2 = PrimitiveObjectInspectorUtils.getHiveDecimal(v2, (PrimitiveObjectInspector) expressionDef.getOI()); if ( d1 != null && d2 != null ) { return d1.equals(d2); } return d1 == null && d2 == null; // True if both are null } }
@Test public void testCastLongToDecimal() throws HiveException { VectorizedRowBatch b = getBatchLongDecimal(); VectorExpression expr = new CastLongToDecimal(0, 1); expr.evaluate(b); DecimalColumnVector r = (DecimalColumnVector) b.cols[1]; assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("0"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-1"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("99999999999999"))); }
@Test public void testCastStringToDecimal() throws HiveException { VectorizedRowBatch b = getBatchStringDecimal(); VectorExpression expr = new CastStringToDecimal(0, 1); expr.evaluate(b); DecimalColumnVector r = (DecimalColumnVector) b.cols[1]; assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("1.10"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-2.20"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("99999999999999.0"))); }
@Test public void testDecimalColModuloDecimalColumn() throws HiveException { VectorizedRowBatch b = getVectorizedRowBatch3DecimalCols(); DecimalColumnVector in1 = (DecimalColumnVector) b.cols[1]; for (int i = 0; i < 3; i++) { in1.vector[i].set(HiveDecimal.create("0.50")); } VectorExpression expr = new DecimalColModuloDecimalColumn(0, 1, 2); expr.evaluate(b); DecimalColumnVector r = (DecimalColumnVector) b.cols[2]; assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("0.20"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-0.30"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("0"))); }
@Test public void testCastDoubleToDecimal() throws HiveException { VectorizedRowBatch b = getBatchDoubleDecimal(); VectorExpression expr = new CastDoubleToDecimal(0, 1); expr.evaluate(b); DecimalColumnVector r = (DecimalColumnVector) b.cols[1]; HiveDecimal hd0 = HiveDecimal.create("0.0"); if (!hd0.equals(r.vector[0].getHiveDecimal())) { assertTrue(false); } HiveDecimal hd1 = HiveDecimal.create("-1.0"); if (!hd1.equals(r.vector[1].getHiveDecimal())) { assertTrue(false); } HiveDecimal hd2 = HiveDecimal.create("99999999999999"); if (!hd2.equals(r.vector[2].getHiveDecimal())) { assertTrue(false); } }
@Test public void testDecimalColSubtractDecimalColumn() throws HiveException { VectorizedRowBatch b = getVectorizedRowBatch3DecimalCols(); VectorExpression expr = new DecimalColSubtractDecimalColumn(0, 1, 2); DecimalColumnVector r = (DecimalColumnVector) b.cols[2]; // test without nulls expr.evaluate(b); assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("0.20"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-4.30"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("-1.00"))); // test that underflow produces NULL b = getVectorizedRowBatch3DecimalCols(); DecimalColumnVector c0 = (DecimalColumnVector) b.cols[0]; c0.vector[0].set(HiveDecimal.create("-9999999999999999.99")); // set to min possible value r = (DecimalColumnVector) b.cols[2]; expr.evaluate(b); // will cause underflow for result at position 0, must yield NULL assertTrue(!r.noNulls && r.isNull[0]); }
@Test public void testDecimalScalarModuloDecimalColumn() throws HiveException { VectorizedRowBatch b = getVectorizedRowBatch3DecimalCols(); HiveDecimal d = HiveDecimal.create("2.00"); VectorExpression expr = new DecimalScalarModuloDecimalColumn(d, 0, 2); // test without nulls expr.evaluate(b); DecimalColumnVector r = (DecimalColumnVector) b.cols[2]; assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("0.80"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("2.00"))); assertFalse(r.noNulls); // entry 2 will be null due to zero-divide assertTrue(r.isNull[2]); // try again with some different data values DecimalColumnVector in = (DecimalColumnVector) b.cols[0]; expr = new DecimalScalarModuloDecimalColumn(d, 0, 2); in.vector[0].set(HiveDecimal.create("0.50")); in.vector[1].set(HiveDecimal.create("0.80")); in.vector[2].set(HiveDecimal.create("0.70")); expr.evaluate(b); assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("0.00"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("0.40"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("0.60"))); }
private IntWritable evaluate(HiveDecimal exprValue, HiveDecimal minValue, HiveDecimal maxValue, int numBuckets) { Preconditions.checkArgument(numBuckets > 0, "numBuckets in width_bucket function must be above 0"); Preconditions.checkArgument(!maxValue.equals(minValue), "maxValue cannot be equal to minValue in width_bucket function"); if (maxValue.compareTo(minValue) > 0) { if (exprValue.compareTo(minValue) < 0) { output.set(0); } else if (exprValue.compareTo(maxValue) >= 0) { output.set(numBuckets + 1); } else { output.set(HiveDecimal.create(numBuckets).multiply(exprValue.subtract(minValue)).divide( maxValue.subtract(minValue)).add(HiveDecimal.ONE).intValue()); } } else { if (exprValue.compareTo(minValue) > 0) { output.set(0); } else if (exprValue.compareTo(maxValue) <= 0) { output.set(numBuckets + 1); } else { output.set(HiveDecimal.create(numBuckets).multiply(minValue.subtract(exprValue)).divide( minValue.subtract(maxValue)).add(HiveDecimal.ONE).intValue()); } } return output; }
@Test public void testDecimalColMultiplyDecimalColumn() throws HiveException { VectorizedRowBatch b = getVectorizedRowBatch3DecimalCols(); VectorExpression expr = new DecimalColMultiplyDecimalColumn(0, 1, 2); DecimalColumnVector r = (DecimalColumnVector) b.cols[2]; // test without nulls expr.evaluate(b); assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("1.20"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-3.30"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("0.00"))); // test that underflow produces NULL b = getVectorizedRowBatch3DecimalCols(); DecimalColumnVector c0 = (DecimalColumnVector) b.cols[0]; c0.vector[0].set(HiveDecimal.create("9999999999999999.99")); // set to max possible value DecimalColumnVector c1 = (DecimalColumnVector) b.cols[1]; c1.vector[0].set(HiveDecimal.create("2.00")); r = (DecimalColumnVector) b.cols[2]; expr.evaluate(b); // will cause overflow for result at position 0, must yield NULL assertTrue(!r.noNulls && r.isNull[0]); }
@Test public void testDecimalScalarMultiplyDecimalColumn() throws HiveException { VectorizedRowBatch b = getVectorizedRowBatch3DecimalCols(); HiveDecimal d = HiveDecimal.create(2); VectorExpression expr = new DecimalScalarMultiplyDecimalColumn(d, 0, 2); // test without nulls expr.evaluate(b); DecimalColumnVector r = (DecimalColumnVector) b.cols[2]; assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("2.40"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-6.60"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("0"))); // test that overflow produces null b = getVectorizedRowBatch3DecimalCols(); DecimalColumnVector in = (DecimalColumnVector) b.cols[0]; in.vector[0].set(HiveDecimal.create("9999999999999999.99")); // set to max possible value expr.evaluate(b); r = (DecimalColumnVector) b.cols[2]; assertFalse(r.noNulls); assertTrue(r.isNull[0]); }
@Test public void testDecimalColSubtractDecimalScalar() throws HiveException { VectorizedRowBatch b = getVectorizedRowBatch3DecimalCols(); HiveDecimal d = HiveDecimal.create(1); VectorExpression expr = new DecimalColSubtractDecimalScalar(0, d, 2); // test without nulls expr.evaluate(b); DecimalColumnVector r = (DecimalColumnVector) b.cols[2]; assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("0.20"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-4.30"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("-1"))); // test that underflow produces null b = getVectorizedRowBatch3DecimalCols(); DecimalColumnVector in = (DecimalColumnVector) b.cols[0]; in.vector[0].set(HiveDecimal.create("-9999999999999999.99")); // set to min possible value expr.evaluate(b); r = (DecimalColumnVector) b.cols[2]; assertFalse(r.noNulls); assertTrue(r.isNull[0]); }
@Test public void testDecimalColMultiplyDecimalScalar() throws HiveException { VectorizedRowBatch b = getVectorizedRowBatch3DecimalCols(); HiveDecimal d = HiveDecimal.create(2); VectorExpression expr = new DecimalColMultiplyDecimalScalar(0, d, 2); // test without nulls expr.evaluate(b); DecimalColumnVector r = (DecimalColumnVector) b.cols[2]; assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("2.40"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-6.60"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("0"))); // test that overflow produces null b = getVectorizedRowBatch3DecimalCols(); DecimalColumnVector in = (DecimalColumnVector) b.cols[0]; in.vector[0].set(HiveDecimal.create("9999999999999999.99")); // set to max possible value expr.evaluate(b); r = (DecimalColumnVector) b.cols[2]; assertFalse(r.noNulls); assertTrue(r.isNull[0]); }
@Test public void testDecimalScalarSubtractDecimalColumn() throws HiveException { VectorizedRowBatch b = getVectorizedRowBatch3DecimalCols(); HiveDecimal d = HiveDecimal.create(1); VectorExpression expr = new DecimalScalarSubtractDecimalColumn(d, 0, 2); // test without nulls expr.evaluate(b); DecimalColumnVector r = (DecimalColumnVector) b.cols[2]; assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("-0.20"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("4.30"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("1"))); // test that overflow produces null b = getVectorizedRowBatch3DecimalCols(); DecimalColumnVector in = (DecimalColumnVector) b.cols[0]; in.vector[0].set(HiveDecimal.create("-9999999999999999.99")); // set to min possible value expr.evaluate(b); r = (DecimalColumnVector) b.cols[2]; assertFalse(r.noNulls); assertTrue(r.isNull[0]); }
@Test public void testDecimalColModuloDecimalScalar() throws HiveException { VectorizedRowBatch b = getVectorizedRowBatch3DecimalCols(); HiveDecimal d = HiveDecimal.create("2.00"); VectorExpression expr = new DecimalColModuloDecimalScalar(0, d, 2); // test without nulls expr.evaluate(b); DecimalColumnVector r = (DecimalColumnVector) b.cols[2]; assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("1.20"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-1.30"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("0"))); // try again with some different data values and divisor DecimalColumnVector in = (DecimalColumnVector) b.cols[0]; in.vector[0].set(HiveDecimal.create("15.40")); in.vector[1].set(HiveDecimal.create("-17.20")); in.vector[2].set(HiveDecimal.create("70.00")); d = HiveDecimal.create("4.75"); expr = new DecimalColModuloDecimalScalar(0, d, 2); expr.evaluate(b); assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("1.15"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-2.95"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("3.50"))); // try a zero-divide to show a repeating NULL is produced d = HiveDecimal.create("0.00"); expr = new DecimalColModuloDecimalScalar(0, d, 2); expr.evaluate(b); assertFalse(r.noNulls); assertTrue(r.isNull[0]); assertTrue(r.isRepeating); }
assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("2.20"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-2.30"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("1"))); r = (DecimalColumnVector) b.cols[2]; assertTrue(r.isRepeating); assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("2.20")));
assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("2.20"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-2.30"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("1"))); r = (DecimalColumnVector) b.cols[2]; assertTrue(r.isRepeating); assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("2.20")));
assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("0.6"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-1.65"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("0"))); r = (DecimalColumnVector) b.cols[2]; assertTrue(r.isRepeating); assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("0.6")));