Search in sources :

Example 6 with RuntimeDataException

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

the class LocalFileSystemUtils method traverse.

public static void traverse(final List<File> files, File root, final String expression, final LinkedList<Path> dirs) throws IOException {
    final Path path = root.toPath();
    if (!Files.exists(path)) {
        throw new RuntimeDataException(ErrorCode.UTIL_LOCAL_FILE_SYSTEM_UTILS_PATH_NOT_FOUND, path.toString());
    }
    if (!Files.isDirectory(path)) {
        validateAndAdd(path, expression, files);
    }
    Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs) throws IOException {
            if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
                return FileVisitResult.TERMINATE;
            }
            if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
                if (dirs != null) {
                    dirs.add(path);
                }
                //get immediate children files
                File[] content = path.toFile().listFiles();
                for (File file : content) {
                    if (!file.isDirectory()) {
                        validateAndAdd(file.toPath(), expression, files);
                    }
                }
            } else {
                // Path is a file, add to list of files if it matches the expression
                validateAndAdd(path, expression, files);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 7 with RuntimeDataException

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

the class TweetParser method writeRecord.

public void writeRecord(JsonNode obj, DataOutput out, ARecordType curRecType) throws IOException {
    IAType[] curTypes = null;
    String[] curFNames = null;
    int fieldN;
    int attrIdx;
    int expectedFieldsCount = 0;
    ArrayBackedValueStorage fieldValueBuffer = getTempBuffer();
    ArrayBackedValueStorage fieldNameBuffer = getTempBuffer();
    IARecordBuilder recBuilder = getRecordBuilder();
    if (curRecType != null) {
        curTypes = curRecType.getFieldTypes();
        curFNames = curRecType.getFieldNames();
        for (IAType curType : curTypes) {
            if (!(curType instanceof AUnionType)) {
                expectedFieldsCount++;
            }
        }
    }
    recBuilder.reset(curRecType);
    recBuilder.init();
    if (curRecType != null && !curRecType.isOpen()) {
        // closed record type
        fieldN = curFNames.length;
        for (int iter1 = 0; iter1 < fieldN; iter1++) {
            fieldValueBuffer.reset();
            DataOutput fieldOutput = fieldValueBuffer.getDataOutput();
            if (obj.get(curFNames[iter1]).isNull() && !(curTypes[iter1] instanceof AUnionType)) {
                if (curRecType.isClosedField(curFNames[iter1])) {
                    throw new RuntimeDataException(ErrorCode.PARSER_TWEET_PARSER_CLOSED_FIELD_NULL, curFNames[iter1]);
                } else {
                    continue;
                }
            } else {
                if (writeField(obj.get(curFNames[iter1]), curTypes[iter1], fieldOutput)) {
                    recBuilder.addField(iter1, fieldValueBuffer);
                }
            }
        }
    } else {
        //open record type
        int closedFieldCount = 0;
        IAType curFieldType = null;
        String attrName;
        Iterator<String> iter = obj.fieldNames();
        while (iter.hasNext()) {
            attrName = iter.next();
            if (obj.get(attrName) == null || obj.get(attrName).isNull() || obj.size() == 0) {
                continue;
            }
            attrIdx = checkAttrNameIdx(curFNames, attrName);
            if (curRecType != null) {
                curFieldType = curRecType.getFieldType(attrName);
            }
            fieldValueBuffer.reset();
            fieldNameBuffer.reset();
            DataOutput fieldOutput = fieldValueBuffer.getDataOutput();
            if (writeField(obj.get(attrName), curFieldType, fieldOutput)) {
                if (attrIdx == -1) {
                    aString.setValue(attrName);
                    stringSerde.serialize(aString, fieldNameBuffer.getDataOutput());
                    recBuilder.addField(fieldNameBuffer, fieldValueBuffer);
                } else {
                    recBuilder.addField(attrIdx, fieldValueBuffer);
                    closedFieldCount++;
                }
            }
        }
        if (curRecType != null && closedFieldCount < expectedFieldsCount) {
            throw new HyracksDataException("Non-null field is null");
        }
    }
    recBuilder.write(out, true);
}
Also used : DataOutput(java.io.DataOutput) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) AUnionType(org.apache.asterix.om.types.AUnionType) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IAType(org.apache.asterix.om.types.IAType) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 8 with RuntimeDataException

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

the class ATypeHierarchy method convertNumericTypeByteArray.

// convert a numeric value in a byte array to the target type value
public static void convertNumericTypeByteArray(byte[] sourceByteArray, int s1, int l1, ATypeTag targetTypeTag, DataOutput out, boolean strictDemote) throws IOException {
    ATypeTag sourceTypeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(sourceByteArray[s1]);
    if (sourceTypeTag != targetTypeTag) {
        // source tag can be promoted to target tag (e.g. tag1: SMALLINT, tag2: INTEGER)
        if (canPromote(sourceTypeTag, targetTypeTag)) {
            ITypeConvertComputer convertComputer = ATypeHierarchy.getTypePromoteComputer(sourceTypeTag, targetTypeTag);
            convertComputer.convertType(sourceByteArray, s1 + 1, l1 - 1, out);
        // source tag can be demoted to target tag
        } else if (canDemote(sourceTypeTag, targetTypeTag)) {
            ITypeConvertComputer convertComputer = ATypeHierarchy.getTypeDemoteComputer(sourceTypeTag, targetTypeTag, strictDemote);
            convertComputer.convertType(sourceByteArray, s1 + 1, l1 - 1, out);
        } else {
            throw new RuntimeDataException(ErrorCode.TYPE_CONVERT, sourceTypeTag, targetTypeTag);
        }
    }
}
Also used : ATypeTag(org.apache.asterix.om.types.ATypeTag) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 9 with RuntimeDataException

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

the class ATypeHierarchy method convertNumericTypeObject.

// Type Casting from source Object to an Object with Target type
public static IAObject convertNumericTypeObject(IAObject sourceObject, ATypeTag targetTypeTag, boolean strictDemote) throws HyracksDataException {
    ATypeTag sourceTypeTag = sourceObject.getType().getTypeTag();
    if (sourceTypeTag == targetTypeTag) {
        return sourceObject;
    }
    ITypeConvertComputer convertComputer = null;
    if (canPromote(sourceTypeTag, targetTypeTag)) {
        convertComputer = ATypeHierarchy.getTypePromoteComputer(sourceTypeTag, targetTypeTag);
    } else if (canDemote(sourceTypeTag, targetTypeTag)) {
        convertComputer = ATypeHierarchy.getTypeDemoteComputer(sourceTypeTag, targetTypeTag, strictDemote);
    }
    if (convertComputer == null) {
        throw new RuntimeDataException(ErrorCode.TYPE_CONVERT, sourceTypeTag, targetTypeTag);
    }
    return convertComputer.convertType(sourceObject);
}
Also used : ATypeTag(org.apache.asterix.om.types.ATypeTag) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 10 with RuntimeDataException

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

the class RecordAddFieldsDescriptor 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 allocator = new PointableAllocator();
            final IVisitablePointable vp0 = allocator.allocateRecordValue(inRecType);
            final IVisitablePointable vp1 = allocator.allocateListValue(inListType);
            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 ArrayBackedValueStorage fieldNamePointable = new ArrayBackedValueStorage();
            final ArrayBackedValueStorage fieldValuePointer = new ArrayBackedValueStorage();
            final PointableHelper pointableHelper = new PointableHelper();
            try {
                pointableHelper.serializeString("field-name", fieldNamePointable, true);
                pointableHelper.serializeString("field-value", fieldValuePointer, true);
            } catch (AsterixException e) {
                throw new HyracksDataException(e);
            }
            return new IScalarEvaluator() {

                // the default 32k frame size
                public static final int TABLE_FRAME_SIZE = 32768;

                // the default 32k frame size
                public static final int TABLE_SIZE = 100;

                private final RecordBuilder recordBuilder = new RecordBuilder();

                private final RuntimeRecordTypeInfo requiredRecordTypeInfo = new RuntimeRecordTypeInfo();

                private final IBinaryHashFunction putHashFunc = ListItemBinaryHashFunctionFactory.INSTANCE.createBinaryHashFunction();

                private final IBinaryHashFunction getHashFunc = ListItemBinaryHashFunctionFactory.INSTANCE.createBinaryHashFunction();

                private final BinaryEntry keyEntry = new BinaryEntry();

                private final BinaryEntry valEntry = new BinaryEntry();

                private final IVisitablePointable tempValReference = allocator.allocateEmpty();

                private final IBinaryComparator cmp = ListItemBinaryComparatorFactory.INSTANCE.createBinaryComparator();

                private BinaryHashMap hashMap = new BinaryHashMap(TABLE_SIZE, TABLE_FRAME_SIZE, putHashFunc, getHashFunc, cmp);

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    recordBuilder.reset(outRecType);
                    requiredRecordTypeInfo.reset(outRecType);
                    eval0.evaluate(tuple, argPtr0);
                    eval1.evaluate(tuple, argPtr1);
                    // Make sure we get a valid record
                    byte typeTag0 = argPtr0.getByteArray()[argPtr0.getStartOffset()];
                    if (typeTag0 != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, typeTag0, ATypeTag.SERIALIZED_RECORD_TYPE_TAG);
                    }
                    // Make sure we get a valid list
                    byte typeTag1 = argPtr1.getByteArray()[argPtr1.getStartOffset()];
                    if (typeTag1 != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 1, typeTag1, ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG);
                    }
                    vp0.set(argPtr0);
                    vp1.set(argPtr1);
                    ARecordVisitablePointable recordPointable = (ARecordVisitablePointable) vp0;
                    AListVisitablePointable listPointable = (AListVisitablePointable) vp1;
                    // Initialize our hashmap
                    int tableSize = recordPointable.getFieldNames().size() + listPointable.getItems().size();
                    // Thus avoiding unnecessary object construction
                    if (hashMap == null || tableSize > TABLE_SIZE) {
                        hashMap = new BinaryHashMap(tableSize, TABLE_FRAME_SIZE, putHashFunc, getHashFunc, cmp);
                    } else {
                        hashMap.clear();
                    }
                    addFields(recordPointable, listPointable);
                    recordBuilder.write(out, true);
                    result.set(resultStorage);
                }

                private void addFields(ARecordVisitablePointable inputRecordPointer, AListVisitablePointable listPointable) throws HyracksDataException {
                    List<IVisitablePointable> inputRecordFieldNames = inputRecordPointer.getFieldNames();
                    List<IVisitablePointable> inputRecordFieldValues = inputRecordPointer.getFieldValues();
                    List<IVisitablePointable> inputFields = listPointable.getItems();
                    IVisitablePointable namePointable = null;
                    IVisitablePointable valuePointable = null;
                    int numInputRecordFields = inputRecordFieldNames.size();
                    try {
                        // Add original record without duplicate checking
                        for (int i = 0; i < numInputRecordFields; ++i) {
                            IVisitablePointable fnp = inputRecordFieldNames.get(i);
                            IVisitablePointable fvp = inputRecordFieldValues.get(i);
                            int pos = requiredRecordTypeInfo.getFieldIndex(fnp.getByteArray(), fnp.getStartOffset() + 1, fnp.getLength() - 1);
                            if (pos >= 0) {
                                recordBuilder.addField(pos, fvp);
                            } else {
                                recordBuilder.addField(fnp, fvp);
                            }
                            keyEntry.set(fnp.getByteArray(), fnp.getStartOffset(), fnp.getLength());
                            valEntry.set(fvp.getByteArray(), fvp.getStartOffset(), fvp.getLength());
                            hashMap.put(keyEntry, valEntry);
                        }
                        // Get the fields from a list of records
                        for (int i = 0; i < inputFields.size(); i++) {
                            if (!PointableHelper.sameType(ATypeTag.OBJECT, inputFields.get(i))) {
                                throw new AsterixException("Expected list of record, got " + PointableHelper.getTypeTag(inputFields.get(i)));
                            }
                            List<IVisitablePointable> names = ((ARecordVisitablePointable) inputFields.get(i)).getFieldNames();
                            List<IVisitablePointable> values = ((ARecordVisitablePointable) inputFields.get(i)).getFieldValues();
                            // Get name and value of the field to be added
                            // Use loop to account for the cases where users switches the order of the fields
                            IVisitablePointable fieldName;
                            for (int j = 0; j < names.size(); j++) {
                                fieldName = names.get(j);
                                // if fieldName is "field-name" then read the name
                                if (PointableHelper.byteArrayEqual(fieldNamePointable, fieldName)) {
                                    namePointable = values.get(j);
                                } else {
                                    // otherwise the fieldName is "field-value". Thus, read the value
                                    valuePointable = values.get(j);
                                }
                            }
                            if (namePointable == null || valuePointable == null) {
                                throw new InvalidDataFormatException(getIdentifier(), "fields to be added");
                            }
                            // Check that the field being added is a valid field
                            int pos = requiredRecordTypeInfo.getFieldIndex(namePointable.getByteArray(), namePointable.getStartOffset() + 1, namePointable.getLength() - 1);
                            keyEntry.set(namePointable.getByteArray(), namePointable.getStartOffset(), namePointable.getLength());
                            // Check if already in our built record
                            BinaryEntry entry = hashMap.get(keyEntry);
                            if (entry != null) {
                                tempValReference.set(entry.getBuf(), entry.getOffset(), entry.getLength());
                                // If value is not equal throw conflicting duplicate field, otherwise ignore
                                if (!PointableHelper.byteArrayEqual(valuePointable, tempValReference)) {
                                    throw new RuntimeDataException(ErrorCode.DUPLICATE_FIELD_NAME, getIdentifier());
                                }
                            } else {
                                if (pos > -1) {
                                    recordBuilder.addField(pos, valuePointable);
                                } else {
                                    recordBuilder.addField(namePointable, valuePointable);
                                }
                                valEntry.set(valuePointable.getByteArray(), valuePointable.getStartOffset(), valuePointable.getLength());
                                hashMap.put(keyEntry, valEntry);
                            }
                        }
                    } catch (AsterixException e) {
                        throw new HyracksDataException(e);
                    }
                }
            };
        }
    };
}
Also used : BinaryEntry(org.apache.hyracks.data.std.util.BinaryEntry) DataOutput(java.io.DataOutput) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IBinaryComparator(org.apache.hyracks.api.dataflow.value.IBinaryComparator) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) InvalidDataFormatException(org.apache.asterix.runtime.exceptions.InvalidDataFormatException) ARecordVisitablePointable(org.apache.asterix.om.pointables.ARecordVisitablePointable) AListVisitablePointable(org.apache.asterix.om.pointables.AListVisitablePointable) AsterixException(org.apache.asterix.common.exceptions.AsterixException) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) PointableAllocator(org.apache.asterix.om.pointables.PointableAllocator) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException) IBinaryHashFunction(org.apache.hyracks.api.dataflow.value.IBinaryHashFunction) RecordBuilder(org.apache.asterix.builders.RecordBuilder) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) PointableHelper(org.apache.asterix.runtime.evaluators.functions.PointableHelper) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) RuntimeRecordTypeInfo(org.apache.asterix.om.types.runtime.RuntimeRecordTypeInfo) BinaryHashMap(org.apache.asterix.runtime.evaluators.functions.BinaryHashMap)

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