Search in sources :

Example 1 with UDFYear

use of org.apache.hadoop.hive.ql.udf.UDFYear in project hive by apache.

the class TestVectorTimestampExpressions method compareToUDFYearLong.

private void compareToUDFYearLong(Timestamp t, int y) throws HiveException {
    UDFYear udf = new UDFYear();
    udf.initialize(new ObjectInspector[] { PrimitiveObjectInspectorFactory.writableTimestampObjectInspector });
    TimestampWritableV2 tsw = new TimestampWritableV2(org.apache.hadoop.hive.common.type.Timestamp.ofEpochMilli(t.getTime(), t.getNanos()));
    IntWritable res = (IntWritable) udf.evaluate(new GenericUDF.DeferredObject[] { new GenericUDF.DeferredJavaObject(tsw) });
    Assert.assertEquals(res.get(), y);
}
Also used : UDFYear(org.apache.hadoop.hive.ql.udf.UDFYear) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) IntWritable(org.apache.hadoop.io.IntWritable)

Example 2 with UDFYear

use of org.apache.hadoop.hive.ql.udf.UDFYear in project hive by apache.

the class TestVectorizationContext method testTimeStampUdfs.

@Test
public void testTimeStampUdfs() throws HiveException {
    ExprNodeGenericFuncDesc tsFuncExpr = new ExprNodeGenericFuncDesc();
    tsFuncExpr.setTypeInfo(TypeInfoFactory.intTypeInfo);
    ExprNodeColumnDesc colDesc1 = new ExprNodeColumnDesc(TypeInfoFactory.timestampTypeInfo, "a", "table", false);
    List<ExprNodeDesc> children = new ArrayList<ExprNodeDesc>();
    children.add(colDesc1);
    List<String> columns = new ArrayList<String>();
    columns.add("b");
    columns.add("a");
    VectorizationContext vc = new VectorizationContext("name", columns);
    // UDFYear
    GenericUDFBridge gudfBridge = new GenericUDFBridge("year", false, UDFYear.class.getName());
    tsFuncExpr.setGenericUDF(gudfBridge);
    tsFuncExpr.setChildren(children);
    VectorExpression ve = vc.getVectorExpression(tsFuncExpr);
    Assert.assertEquals(VectorUDFYearTimestamp.class, ve.getClass());
    // GenericUDFToUnixTimeStamp
    GenericUDFToUnixTimeStamp gudf = new GenericUDFToUnixTimeStamp();
    tsFuncExpr.setGenericUDF(gudf);
    tsFuncExpr.setTypeInfo(TypeInfoFactory.longTypeInfo);
    ve = vc.getVectorExpression(tsFuncExpr);
    Assert.assertEquals(VectorUDFUnixTimeStampTimestamp.class, ve.getClass());
}
Also used : ExprNodeColumnDesc(org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc) ArrayList(java.util.ArrayList) GenericUDFToUnixTimeStamp(org.apache.hadoop.hive.ql.udf.generic.GenericUDFToUnixTimeStamp) ExprNodeGenericFuncDesc(org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc) VectorExpression(org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpression) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc) UDFYear(org.apache.hadoop.hive.ql.udf.UDFYear) GenericUDFBridge(org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge) Test(org.junit.Test)

Example 3 with UDFYear

use of org.apache.hadoop.hive.ql.udf.UDFYear in project hive by apache.

the class TestVectorDateExpressions method compareToUDFYearDate.

private void compareToUDFYearDate(long t, int y) throws HiveException {
    UDFYear udf = new UDFYear();
    udf.initialize(new ObjectInspector[] { PrimitiveObjectInspectorFactory.writableTimestampObjectInspector });
    TimestampWritableV2 tsw = toTimestampWritable(t);
    IntWritable res = (IntWritable) udf.evaluate(new GenericUDF.DeferredObject[] { new GenericUDF.DeferredJavaObject(tsw) });
    Assert.assertEquals(res.get(), y);
}
Also used : UDFYear(org.apache.hadoop.hive.ql.udf.UDFYear) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) IntWritable(org.apache.hadoop.io.IntWritable)

Example 4 with UDFYear

use of org.apache.hadoop.hive.ql.udf.UDFYear in project hive by apache.

the class TestVectorTimestampExtract method doIfTestOneTimestampExtract.

private void doIfTestOneTimestampExtract(Random random, String dateTimeStringTypeName, String extractFunctionName) throws Exception {
    TypeInfo dateTimeStringTypeInfo = TypeInfoUtils.getTypeInfoFromTypeString(dateTimeStringTypeName);
    PrimitiveCategory dateTimeStringPrimitiveCategory = ((PrimitiveTypeInfo) dateTimeStringTypeInfo).getPrimitiveCategory();
    boolean isStringFamily = (dateTimeStringPrimitiveCategory == PrimitiveCategory.STRING || dateTimeStringPrimitiveCategory == PrimitiveCategory.CHAR || dateTimeStringPrimitiveCategory == PrimitiveCategory.VARCHAR);
    List<GenerationSpec> generationSpecList = new ArrayList<GenerationSpec>();
    List<DataTypePhysicalVariation> explicitDataTypePhysicalVariationList = new ArrayList<DataTypePhysicalVariation>();
    List<String> columns = new ArrayList<String>();
    int columnNum = 1;
    ExprNodeDesc col1Expr;
    if (!isStringFamily) {
        generationSpecList.add(GenerationSpec.createSameType(dateTimeStringTypeInfo));
    } else {
        generationSpecList.add(GenerationSpec.createStringFamilyOtherTypeValue(dateTimeStringTypeInfo, TypeInfoFactory.timestampTypeInfo));
    }
    explicitDataTypePhysicalVariationList.add(DataTypePhysicalVariation.NONE);
    String columnName = "col" + (columnNum++);
    col1Expr = new ExprNodeColumnDesc(dateTimeStringTypeInfo, columnName, "table", false);
    columns.add(columnName);
    VectorRandomRowSource rowSource = new VectorRandomRowSource();
    rowSource.initGenerationSpecSchema(random, generationSpecList, /* maxComplexDepth */
    0, /* allowNull */
    true, /* isUnicodeOk */
    true, explicitDataTypePhysicalVariationList);
    List<ExprNodeDesc> children = new ArrayList<ExprNodeDesc>();
    children.add(col1Expr);
    // ----------------------------------------------------------------------------------------------
    String[] columnNames = columns.toArray(new String[0]);
    Object[][] randomRows = rowSource.randomRows(100000);
    VectorRandomBatchSource batchSource = VectorRandomBatchSource.createInterestingBatches(random, rowSource, randomRows, null);
    if (dateTimeStringPrimitiveCategory == PrimitiveCategory.DATE && (extractFunctionName.equals("hour") || extractFunctionName.equals("minute") || extractFunctionName.equals("second"))) {
        return;
    }
    final GenericUDF udf;
    switch(extractFunctionName) {
        case "day":
            udf = new UDFDayOfMonth();
            break;
        case "dayofweek":
            GenericUDFBridge dayOfWeekUDFBridge = new GenericUDFBridge();
            dayOfWeekUDFBridge.setUdfClassName(UDFDayOfWeek.class.getName());
            udf = dayOfWeekUDFBridge;
            break;
        case "hour":
            udf = new UDFHour();
            break;
        case "minute":
            udf = new UDFMinute();
            break;
        case "month":
            udf = new UDFMonth();
            break;
        case "second":
            udf = new UDFSecond();
            break;
        case "yearweek":
            GenericUDFBridge weekOfYearUDFBridge = new GenericUDFBridge();
            weekOfYearUDFBridge.setUdfClassName(UDFWeekOfYear.class.getName());
            udf = weekOfYearUDFBridge;
            break;
        case "year":
            udf = new UDFYear();
            break;
        default:
            throw new RuntimeException("Unexpected extract function name " + extractFunctionName);
    }
    ExprNodeGenericFuncDesc exprDesc = new ExprNodeGenericFuncDesc(TypeInfoFactory.intTypeInfo, udf, children);
    final int rowCount = randomRows.length;
    Object[][] resultObjectsArray = new Object[TimestampExtractTestMode.count][];
    for (int i = 0; i < TimestampExtractTestMode.count; i++) {
        Object[] resultObjects = new Object[rowCount];
        resultObjectsArray[i] = resultObjects;
        TimestampExtractTestMode timestampExtractTestMode = TimestampExtractTestMode.values()[i];
        switch(timestampExtractTestMode) {
            case ROW_MODE:
                if (!doRowCastTest(dateTimeStringTypeInfo, columns, children, exprDesc, randomRows, rowSource.rowStructObjectInspector(), resultObjects)) {
                    return;
                }
                break;
            case ADAPTOR:
            case VECTOR_EXPRESSION:
                if (!doVectorCastTest(dateTimeStringTypeInfo, columns, columnNames, rowSource.typeInfos(), rowSource.dataTypePhysicalVariations(), children, exprDesc, timestampExtractTestMode, batchSource, resultObjects)) {
                    return;
                }
                break;
            default:
                throw new RuntimeException("Unexpected IF statement test mode " + timestampExtractTestMode);
        }
    }
    for (int i = 0; i < rowCount; i++) {
        // Row-mode is the expected value.
        Object expectedResult = resultObjectsArray[0][i];
        for (int v = 1; v < TimestampExtractTestMode.count; v++) {
            Object vectorResult = resultObjectsArray[v][i];
            if (expectedResult == null || vectorResult == null) {
                if (expectedResult != null || vectorResult != null) {
                    Assert.fail("Row " + i + " dateTimeStringTypeName " + dateTimeStringTypeName + " extractFunctionName " + extractFunctionName + " " + TimestampExtractTestMode.values()[v] + " result is NULL " + (vectorResult == null ? "YES" : "NO result " + vectorResult.toString()) + " does not match row-mode expected result is NULL " + (expectedResult == null ? "YES" : "NO result " + expectedResult.toString()) + " row values " + Arrays.toString(randomRows[i]));
                }
            } else {
                if (!expectedResult.equals(vectorResult)) {
                    Assert.fail("Row " + i + " dateTimeStringTypeName " + dateTimeStringTypeName + " extractFunctionName " + extractFunctionName + " " + TimestampExtractTestMode.values()[v] + " result " + vectorResult.toString() + " (" + vectorResult.getClass().getSimpleName() + ")" + " does not match row-mode expected result " + expectedResult.toString() + " (" + expectedResult.getClass().getSimpleName() + ")" + " row values " + Arrays.toString(randomRows[i]));
                }
            }
        }
    }
}
Also used : UDFDayOfMonth(org.apache.hadoop.hive.ql.udf.UDFDayOfMonth) ArrayList(java.util.ArrayList) UDFYear(org.apache.hadoop.hive.ql.udf.UDFYear) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) GenericUDFBridge(org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge) DataTypePhysicalVariation(org.apache.hadoop.hive.common.type.DataTypePhysicalVariation) UDFMinute(org.apache.hadoop.hive.ql.udf.UDFMinute) ExprNodeColumnDesc(org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) UDFHour(org.apache.hadoop.hive.ql.udf.UDFHour) VectorRandomBatchSource(org.apache.hadoop.hive.ql.exec.vector.VectorRandomBatchSource) UDFDayOfWeek(org.apache.hadoop.hive.ql.udf.UDFDayOfWeek) UDFWeekOfYear(org.apache.hadoop.hive.ql.udf.UDFWeekOfYear) ExprNodeGenericFuncDesc(org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) GenerationSpec(org.apache.hadoop.hive.ql.exec.vector.VectorRandomRowSource.GenerationSpec) GenericUDF(org.apache.hadoop.hive.ql.udf.generic.GenericUDF) UDFSecond(org.apache.hadoop.hive.ql.udf.UDFSecond) UDFMonth(org.apache.hadoop.hive.ql.udf.UDFMonth) VectorRandomRowSource(org.apache.hadoop.hive.ql.exec.vector.VectorRandomRowSource)

Aggregations

UDFYear (org.apache.hadoop.hive.ql.udf.UDFYear)4 ArrayList (java.util.ArrayList)2 ExprNodeColumnDesc (org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc)2 ExprNodeDesc (org.apache.hadoop.hive.ql.plan.ExprNodeDesc)2 ExprNodeGenericFuncDesc (org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc)2 GenericUDFBridge (org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge)2 TimestampWritableV2 (org.apache.hadoop.hive.serde2.io.TimestampWritableV2)2 IntWritable (org.apache.hadoop.io.IntWritable)2 DataTypePhysicalVariation (org.apache.hadoop.hive.common.type.DataTypePhysicalVariation)1 VectorRandomBatchSource (org.apache.hadoop.hive.ql.exec.vector.VectorRandomBatchSource)1 VectorRandomRowSource (org.apache.hadoop.hive.ql.exec.vector.VectorRandomRowSource)1 GenerationSpec (org.apache.hadoop.hive.ql.exec.vector.VectorRandomRowSource.GenerationSpec)1 VectorExpression (org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpression)1 UDFDayOfMonth (org.apache.hadoop.hive.ql.udf.UDFDayOfMonth)1 UDFDayOfWeek (org.apache.hadoop.hive.ql.udf.UDFDayOfWeek)1 UDFHour (org.apache.hadoop.hive.ql.udf.UDFHour)1 UDFMinute (org.apache.hadoop.hive.ql.udf.UDFMinute)1 UDFMonth (org.apache.hadoop.hive.ql.udf.UDFMonth)1 UDFSecond (org.apache.hadoop.hive.ql.udf.UDFSecond)1 UDFWeekOfYear (org.apache.hadoop.hive.ql.udf.UDFWeekOfYear)1