Search in sources :

Example 41 with EvalFunc

use of org.apache.pig.EvalFunc in project sketches-pig by DataSketches.

the class MurmurHash3Test method outputSchemaTestMurmurHash3Udf.

/**
 * Test the outputSchema method for MurmurHash3.
 * @throws IOException thrown by Pig
 */
@Test
public void outputSchemaTestMurmurHash3Udf() throws IOException {
    EvalFunc<Tuple> hashUdf = (EvalFunc<Tuple>) PigContext.instantiateFuncFromSpec(new FuncSpec(hashUdfName));
    Schema inputSchema = null;
    Schema nullOutputSchema = null;
    Schema outputSchema = null;
    Schema.FieldSchema outputOuterFs0 = null;
    Schema outputInnerSchema = null;
    Schema.FieldSchema outputInnerFs0 = null;
    Schema.FieldSchema outputInnerFs1 = null;
    Schema.FieldSchema outputInnerFs2 = null;
    nullOutputSchema = hashUdf.outputSchema(null);
    // CHARARRAY is one of many different input types
    inputSchema = Schema.generateNestedSchema(DataType.BAG, DataType.CHARARRAY);
    outputSchema = hashUdf.outputSchema(inputSchema);
    outputOuterFs0 = outputSchema.getField(0);
    outputInnerSchema = outputOuterFs0.schema;
    outputInnerFs0 = outputInnerSchema.getField(0);
    outputInnerFs1 = outputInnerSchema.getField(1);
    outputInnerFs2 = outputInnerSchema.getField(2);
    Assert.assertNull(nullOutputSchema, "Should be null");
    Assert.assertNotNull(outputOuterFs0, "outputSchema.getField(0) may not be null");
    String expected = "tuple";
    String result = DataType.findTypeName(outputOuterFs0.type);
    Assert.assertEquals(result, expected);
    expected = "long";
    Assert.assertNotNull(outputInnerFs0, "innerSchema.getField(0) may not be null");
    result = DataType.findTypeName(outputInnerFs0.type);
    Assert.assertEquals(result, expected);
    expected = "long";
    Assert.assertNotNull(outputInnerFs1, "innerSchema.getField(1) may not be null");
    result = DataType.findTypeName(outputInnerFs1.type);
    Assert.assertEquals(result, expected);
    expected = "int";
    Assert.assertNotNull(outputInnerFs2, "innerSchema.getField(2) may not be null");
    result = DataType.findTypeName(outputInnerFs2.type);
    Assert.assertEquals(result, expected);
    // print schemas
    // @formatter:off
    StringBuilder sb = new StringBuilder();
    sb.append("input schema: ").append(inputSchema).append(LS).append("output schema: ").append(outputSchema).append(LS).append("outputOuterFs: ").append(outputOuterFs0).append(", type: ").append(DataType.findTypeName(outputOuterFs0.type)).append(LS).append("outputInnerSchema: ").append(outputInnerSchema).append(LS).append("outputInnerFs0: ").append(outputInnerFs0).append(", type: ").append(DataType.findTypeName(outputInnerFs0.type)).append(LS).append("outputInnerFs1: ").append(outputInnerFs1).append(", type: ").append(DataType.findTypeName(outputInnerFs1.type)).append(LS).append("outputInnerFs2: ").append(outputInnerFs2).append(", type: ").append(DataType.findTypeName(outputInnerFs2.type)).append(LS);
    println(sb.toString());
// @formatter:on
// end print schemas
}
Also used : FuncSpec(org.apache.pig.FuncSpec) Schema(org.apache.pig.impl.logicalLayer.schema.Schema) EvalFunc(org.apache.pig.EvalFunc) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Example 42 with EvalFunc

use of org.apache.pig.EvalFunc in project sketches-pig by DataSketches.

the class MurmurHash3Test method checkExceptions2.

@Test(expectedExceptions = IllegalArgumentException.class)
public void checkExceptions2() throws IOException {
    EvalFunc<Tuple> hashUdf = (EvalFunc<Tuple>) PigContext.instantiateFuncFromSpec(new FuncSpec(hashUdfName));
    Tuple in, out;
    // seed must be INTEGER or LONG
    in = mTupleFactory.newTuple(2);
    in.set(0, new String("ABC"));
    in.set(1, new Double(9001));
    out = hashUdf.exec(in);
}
Also used : FuncSpec(org.apache.pig.FuncSpec) EvalFunc(org.apache.pig.EvalFunc) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Example 43 with EvalFunc

use of org.apache.pig.EvalFunc in project sketches-pig by DataSketches.

the class MurmurHash3Test method checkExceptions3.

@Test(expectedExceptions = IllegalArgumentException.class)
public void checkExceptions3() throws IOException {
    EvalFunc<Tuple> hashUdf = (EvalFunc<Tuple>) PigContext.instantiateFuncFromSpec(new FuncSpec(hashUdfName));
    Tuple in, out;
    // improper hash object = Tuple
    in = mTupleFactory.newTuple(1);
    in.set(0, in);
    out = hashUdf.exec(in);
}
Also used : FuncSpec(org.apache.pig.FuncSpec) EvalFunc(org.apache.pig.EvalFunc) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Example 44 with EvalFunc

use of org.apache.pig.EvalFunc in project sketches-pig by DataSketches.

the class MurmurHash3Test method checkExceptions1.

@Test
public void checkExceptions1() throws IOException {
    EvalFunc<Tuple> hashUdf = (EvalFunc<Tuple>) PigContext.instantiateFuncFromSpec(new FuncSpec(hashUdfName));
    Tuple in, out;
    // Empty input tuple
    in = mTupleFactory.newTuple(0);
    out = hashUdf.exec(in);
    Assert.assertNull(out);
}
Also used : FuncSpec(org.apache.pig.FuncSpec) EvalFunc(org.apache.pig.EvalFunc) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Aggregations

EvalFunc (org.apache.pig.EvalFunc)44 Test (org.testng.annotations.Test)44 Tuple (org.apache.pig.data.Tuple)35 HllSketch (com.yahoo.sketches.hll.HllSketch)18 DataBag (org.apache.pig.data.DataBag)16 DataByteArray (org.apache.pig.data.DataByteArray)16 FuncSpec (org.apache.pig.FuncSpec)12 ItemsSketch (com.yahoo.sketches.quantiles.ItemsSketch)2 BagFactory (org.apache.pig.data.BagFactory)2 TupleFactory (org.apache.pig.data.TupleFactory)2 DataToSketch (com.yahoo.sketches.pig.theta.DataToSketch)1 PigUtil.tupleToSketch (com.yahoo.sketches.pig.theta.PigUtil.tupleToSketch)1 Sketch (com.yahoo.sketches.theta.Sketch)1 Schema (org.apache.pig.impl.logicalLayer.schema.Schema)1