Search in sources :

Example 26 with RuntimeDataException

use of org.apache.asterix.common.exceptions.RuntimeDataException in project asterixdb by apache.

the class RecordMergeDescriptor method createEvaluatorFactory.

@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
    return new IScalarEvaluatorFactory() {

        private static final long serialVersionUID = 1L;

        @Override
        public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
            final PointableAllocator pa = new PointableAllocator();
            final IVisitablePointable vp0 = pa.allocateRecordValue(inRecType0);
            final IVisitablePointable vp1 = pa.allocateRecordValue(inRecType1);
            final IPointable argPtr0 = new VoidPointable();
            final IPointable argPtr1 = new VoidPointable();
            final IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);
            final IScalarEvaluator eval1 = args[1].createScalarEvaluator(ctx);
            final List<RecordBuilder> rbStack = new ArrayList<>();
            final ArrayBackedValueStorage tabvs = new ArrayBackedValueStorage();
            return new IScalarEvaluator() {

                private final RuntimeRecordTypeInfo runtimeRecordTypeInfo = new RuntimeRecordTypeInfo();

                private final DeepEqualAssessor deepEqualAssesor = new DeepEqualAssessor();

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    eval1.evaluate(tuple, argPtr1);
                    vp0.set(argPtr0);
                    vp1.set(argPtr1);
                    ARecordVisitablePointable rp0 = (ARecordVisitablePointable) vp0;
                    ARecordVisitablePointable rp1 = (ARecordVisitablePointable) vp1;
                    try {
                        mergeFields(outRecType, rp0, rp1, true, 0);
                        rbStack.get(0).write(out, true);
                    } catch (IOException | AsterixException e) {
                        throw new HyracksDataException(e);
                    }
                    result.set(resultStorage);
                }

                private void mergeFields(ARecordType combinedType, ARecordVisitablePointable leftRecord, ARecordVisitablePointable rightRecord, boolean openFromParent, int nestedLevel) throws IOException, AsterixException, HyracksDataException {
                    if (rbStack.size() < (nestedLevel + 1)) {
                        rbStack.add(new RecordBuilder());
                    }
                    rbStack.get(nestedLevel).reset(combinedType);
                    rbStack.get(nestedLevel).init();
                    //Add all fields from left record
                    for (int i = 0; i < leftRecord.getFieldNames().size(); i++) {
                        IVisitablePointable leftName = leftRecord.getFieldNames().get(i);
                        IVisitablePointable leftValue = leftRecord.getFieldValues().get(i);
                        IVisitablePointable leftType = leftRecord.getFieldTypeTags().get(i);
                        boolean foundMatch = false;
                        for (int j = 0; j < rightRecord.getFieldNames().size(); j++) {
                            IVisitablePointable rightName = rightRecord.getFieldNames().get(j);
                            IVisitablePointable rightValue = rightRecord.getFieldValues().get(j);
                            IVisitablePointable rightType = rightRecord.getFieldTypeTags().get(j);
                            // Check if same fieldname
                            if (PointableHelper.isEqual(leftName, rightName) && !deepEqualAssesor.isEqual(leftValue, rightValue)) {
                                //Field was found on the right and are subrecords, merge them
                                if (PointableHelper.sameType(ATypeTag.OBJECT, rightType) && PointableHelper.sameType(ATypeTag.OBJECT, leftType)) {
                                    //We are merging two sub records
                                    addFieldToSubRecord(combinedType, leftName, leftValue, rightValue, openFromParent, nestedLevel);
                                    foundMatch = true;
                                } else {
                                    throw new RuntimeDataException(ErrorCode.DUPLICATE_FIELD_NAME, getIdentifier());
                                }
                            }
                        }
                        if (!foundMatch) {
                            addFieldToSubRecord(combinedType, leftName, leftValue, null, openFromParent, nestedLevel);
                        }
                    }
                    //Repeat for right side (ignoring duplicates this time)
                    for (int j = 0; j < rightRecord.getFieldNames().size(); j++) {
                        IVisitablePointable rightName = rightRecord.getFieldNames().get(j);
                        IVisitablePointable rightValue = rightRecord.getFieldValues().get(j);
                        boolean foundMatch = false;
                        for (int i = 0; i < leftRecord.getFieldNames().size(); i++) {
                            IVisitablePointable leftName = leftRecord.getFieldNames().get(i);
                            if (rightName.equals(leftName)) {
                                foundMatch = true;
                            }
                        }
                        if (!foundMatch) {
                            addFieldToSubRecord(combinedType, rightName, rightValue, null, openFromParent, nestedLevel);
                        }
                    }
                }

                /*
                     * Takes in a record type, field name, and the field values (which are record) from two records
                     * Merges them into one record of combinedType
                     * And adds that record as a field to the Record in subrb
                     * the second value can be null, indicated that you just add the value of left as a field to subrb
                     *
                     */
                private void addFieldToSubRecord(ARecordType combinedType, IVisitablePointable fieldNamePointable, IVisitablePointable leftValue, IVisitablePointable rightValue, boolean openFromParent, int nestedLevel) throws IOException, AsterixException, HyracksDataException {
                    runtimeRecordTypeInfo.reset(combinedType);
                    int pos = runtimeRecordTypeInfo.getFieldIndex(fieldNamePointable.getByteArray(), fieldNamePointable.getStartOffset() + 1, fieldNamePointable.getLength() - 1);
                    //Add the merged field
                    if (combinedType != null && pos >= 0) {
                        if (rightValue == null) {
                            rbStack.get(nestedLevel).addField(pos, leftValue);
                        } else {
                            mergeFields((ARecordType) combinedType.getFieldTypes()[pos], (ARecordVisitablePointable) leftValue, (ARecordVisitablePointable) rightValue, false, nestedLevel + 1);
                            tabvs.reset();
                            rbStack.get(nestedLevel + 1).write(tabvs.getDataOutput(), true);
                            rbStack.get(nestedLevel).addField(pos, tabvs);
                        }
                    } else {
                        if (rightValue == null) {
                            rbStack.get(nestedLevel).addField(fieldNamePointable, leftValue);
                        } else {
                            mergeFields(DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE, (ARecordVisitablePointable) leftValue, (ARecordVisitablePointable) rightValue, false, nestedLevel + 1);
                            tabvs.reset();
                            rbStack.get(nestedLevel + 1).write(tabvs.getDataOutput(), true);
                            rbStack.get(nestedLevel).addField(fieldNamePointable, tabvs);
                        }
                    }
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) ArrayList(java.util.ArrayList) RecordBuilder(org.apache.asterix.builders.RecordBuilder) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) ARecordVisitablePointable(org.apache.asterix.om.pointables.ARecordVisitablePointable) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AsterixException(org.apache.asterix.common.exceptions.AsterixException) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) DeepEqualAssessor(org.apache.asterix.runtime.evaluators.comparisons.DeepEqualAssessor) RuntimeRecordTypeInfo(org.apache.asterix.om.types.runtime.RuntimeRecordTypeInfo) ARecordType(org.apache.asterix.om.types.ARecordType) PointableAllocator(org.apache.asterix.om.pointables.PointableAllocator) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 27 with RuntimeDataException

use of org.apache.asterix.common.exceptions.RuntimeDataException in project asterixdb by apache.

the class DelimitedDataParser method parseRecord.

private void parseRecord() throws HyracksDataException {
    recBuilder.reset(recordType);
    recBuilder.init();
    areAllNullFields = true;
    for (int i = 0; i < valueParsers.length; ++i) {
        try {
            if (!cursor.nextField()) {
                break;
            }
        } catch (IOException e) {
            throw new HyracksDataException(e);
        }
        fieldValueBuffer.reset();
        try {
            if (cursor.fStart == cursor.fEnd && recordType.getFieldTypes()[i].getTypeTag() != ATypeTag.STRING && recordType.getFieldTypes()[i].getTypeTag() != ATypeTag.NULL) {
                // empty string
                if (!NonTaggedFormatUtil.isOptional(recordType.getFieldTypes()[i])) {
                    throw new RuntimeDataException(ErrorCode.PARSER_DELIMITED_NONOPTIONAL_NULL, cursor.recordCount, cursor.fieldCount);
                }
                fieldValueBufferOutput.writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG);
            } else {
                fieldValueBufferOutput.writeByte(fieldTypeTags[i]);
                // Eliminate doule quotes in the field that we are going to parse
                if (cursor.isDoubleQuoteIncludedInThisField) {
                    cursor.eliminateDoubleQuote(cursor.buffer, cursor.fStart, cursor.fEnd - cursor.fStart);
                    cursor.fEnd -= cursor.doubleQuoteCount;
                    cursor.isDoubleQuoteIncludedInThisField = false;
                }
                valueParsers[i].parse(cursor.buffer, cursor.fStart, cursor.fEnd - cursor.fStart, fieldValueBufferOutput);
                areAllNullFields = false;
            }
            if (fldIds[i] < 0) {
                recBuilder.addField(nameBuffers[i], fieldValueBuffer);
            } else {
                recBuilder.addField(fldIds[i], fieldValueBuffer);
            }
        } catch (IOException e) {
            throw new HyracksDataException(e);
        }
    }
}
Also used : IOException(java.io.IOException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 28 with RuntimeDataException

use of org.apache.asterix.common.exceptions.RuntimeDataException in project asterixdb by apache.

the class HiveRecordParser method parseUnorderedList.

private void parseUnorderedList(AUnorderedListType uoltype, Object obj, ListObjectInspector oi) throws IOException {
    UnorderedListBuilder unorderedListBuilder = getUnorderedListBuilder();
    IAType itemType = null;
    if (uoltype != null)
        itemType = uoltype.getItemType();
    byte tagByte = itemType.getTypeTag().serialize();
    unorderedListBuilder.reset(uoltype);
    int n = oi.getListLength(obj);
    for (int i = 0; i < n; i++) {
        Object element = oi.getListElement(obj, i);
        ObjectInspector eoi = oi.getListElementObjectInspector();
        if (element == null) {
            throw new RuntimeDataException(ErrorCode.PARSER_HIVE_NULL_VALUE_IN_LIST);
        }
        listItemBuffer.reset();
        final DataOutput dataOutput = listItemBuffer.getDataOutput();
        dataOutput.writeByte(tagByte);
        parseItem(itemType, element, eoi, dataOutput, true);
        unorderedListBuilder.addItem(listItemBuffer);
    }
    unorderedListBuilder.write(fieldValueBuffer.getDataOutput(), true);
}
Also used : DataOutput(java.io.DataOutput) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) IntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector) BooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector) ShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) LongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) FloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) ByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector) DoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector) TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) UnorderedListBuilder(org.apache.asterix.builders.UnorderedListBuilder) IAType(org.apache.asterix.om.types.IAType) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 29 with RuntimeDataException

use of org.apache.asterix.common.exceptions.RuntimeDataException in project asterixdb by apache.

the class DataflowUtils method getTupleForwarder.

public static ITupleForwarder getTupleForwarder(Map<String, String> configuration, FeedLogManager feedLogManager) throws HyracksDataException {
    ITupleForwarder.TupleForwardPolicy policyType = null;
    String propValue = configuration.get(ITupleForwarder.FORWARD_POLICY);
    if (ExternalDataUtils.isFeed(configuration)) {
        // TODO pass this value in the configuration and avoid this check for feeds
        policyType = TupleForwardPolicy.FEED;
    } else if (propValue == null) {
        policyType = TupleForwardPolicy.FRAME_FULL;
    } else {
        policyType = TupleForwardPolicy.valueOf(propValue.trim().toUpperCase());
    }
    switch(policyType) {
        case FEED:
            return new FeedTupleForwarder(feedLogManager);
        case FRAME_FULL:
            return new FrameFullTupleForwarder();
        case COUNTER_TIMER_EXPIRED:
            return CounterTimerTupleForwarder.create(configuration);
        case RATE_CONTROLLED:
            return RateControlledTupleForwarder.create(configuration);
        default:
            throw new RuntimeDataException(ErrorCode.UTIL_DATAFLOW_UTILS_UNKNOWN_FORWARD_POLICY);
    }
}
Also used : ITupleForwarder(org.apache.asterix.external.api.ITupleForwarder) FrameFullTupleForwarder(org.apache.asterix.external.dataflow.FrameFullTupleForwarder) FeedTupleForwarder(org.apache.asterix.external.dataflow.FeedTupleForwarder) TupleForwardPolicy(org.apache.asterix.external.api.ITupleForwarder.TupleForwardPolicy) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 30 with RuntimeDataException

use of org.apache.asterix.common.exceptions.RuntimeDataException in project asterixdb by apache.

the class ExternalDataUtils method createExternalInputStreamFactory.

public static IInputStreamFactory createExternalInputStreamFactory(ILibraryManager libraryManager, String dataverse, String stream) throws HyracksDataException {
    try {
        String libraryName = getLibraryName(stream);
        String className = getExternalClassName(stream);
        ClassLoader classLoader = getClassLoader(libraryManager, dataverse, libraryName);
        return ((IInputStreamFactory) (classLoader.loadClass(className).newInstance()));
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw new RuntimeDataException(ErrorCode.UTIL_EXTERNAL_DATA_UTILS_FAIL_CREATE_STREAM_FACTORY, e);
    }
}
Also used : RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Aggregations

RuntimeDataException (org.apache.asterix.common.exceptions.RuntimeDataException)33 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)15 IOException (java.io.IOException)12 DataOutput (java.io.DataOutput)7 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)7 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)7 IPointable (org.apache.hyracks.data.std.api.IPointable)7 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)7 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)7 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)7 ATypeTag (org.apache.asterix.om.types.ATypeTag)6 IAType (org.apache.asterix.om.types.IAType)6 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)6 AsterixException (org.apache.asterix.common.exceptions.AsterixException)5 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)4 AUnionType (org.apache.asterix.om.types.AUnionType)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 RecordBuilder (org.apache.asterix.builders.RecordBuilder)2 IInputStreamFactory (org.apache.asterix.external.api.IInputStreamFactory)2