Search in sources :

Example 51 with AsterixException

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

the class FieldAccessNestedEvalFactory method createScalarEvaluator.

@Override
public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
    return new IScalarEvaluator() {

        private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

        private final DataOutput out = resultStorage.getDataOutput();

        private final ByteArrayAccessibleOutputStream subRecordTmpStream = new ByteArrayAccessibleOutputStream();

        private final IPointable inputArg0 = new VoidPointable();

        private final IScalarEvaluator eval0 = recordEvalFactory.createScalarEvaluator(ctx);

        private final IPointable[] fieldPointables = new VoidPointable[fieldPath.size()];

        private final RuntimeRecordTypeInfo[] recTypeInfos = new RuntimeRecordTypeInfo[fieldPath.size()];

        @SuppressWarnings("unchecked")
        private final ISerializerDeserializer<ANull> nullSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ANULL);

        @SuppressWarnings("unchecked")
        private final ISerializerDeserializer<AMissing> missingSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AMISSING);

        {
            generateFieldsPointables();
            for (int index = 0; index < fieldPath.size(); ++index) {
                recTypeInfos[index] = new RuntimeRecordTypeInfo();
            }
        }

        @SuppressWarnings("unchecked")
        private void generateFieldsPointables() throws HyracksDataException {
            for (int i = 0; i < fieldPath.size(); i++) {
                ArrayBackedValueStorage storage = new ArrayBackedValueStorage();
                DataOutput out = storage.getDataOutput();
                AString as = new AString(fieldPath.get(i));
                SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(as.getType()).serialize(as, out);
                fieldPointables[i] = new VoidPointable();
                fieldPointables[i].set(storage);
            }
        }

        @Override
        public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
            try {
                resultStorage.reset();
                eval0.evaluate(tuple, inputArg0);
                byte[] serRecord = inputArg0.getByteArray();
                int offset = inputArg0.getStartOffset();
                int start = offset;
                int len = inputArg0.getLength();
                if (serRecord[start] != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                    throw new TypeMismatchException(BuiltinFunctions.FIELD_ACCESS_NESTED, 0, serRecord[start], ATypeTag.SERIALIZED_RECORD_TYPE_TAG);
                }
                int subFieldIndex = -1;
                int subFieldOffset = -1;
                int subFieldLength = -1;
                int nullBitmapSize = -1;
                IAType subType = recordType;
                recTypeInfos[0].reset(recordType);
                ATypeTag subTypeTag = ATypeTag.MISSING;
                boolean openField = false;
                int pathIndex = 0;
                // Moving through closed fields first.
                for (; pathIndex < fieldPointables.length; pathIndex++) {
                    if (subType.getTypeTag().equals(ATypeTag.UNION)) {
                        //enforced SubType
                        subType = ((AUnionType) subType).getActualType();
                        byte serializedTypeTag = subType.getTypeTag().serialize();
                        if (serializedTypeTag != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                            throw new UnsupportedTypeException(BuiltinFunctions.FIELD_ACCESS_NESTED.getName(), serializedTypeTag);
                        }
                        if (subType.getTypeTag() == ATypeTag.OBJECT) {
                            recTypeInfos[pathIndex].reset((ARecordType) subType);
                        }
                    }
                    subFieldIndex = recTypeInfos[pathIndex].getFieldIndex(fieldPointables[pathIndex].getByteArray(), fieldPointables[pathIndex].getStartOffset() + 1, fieldPointables[pathIndex].getLength() - 1);
                    if (subFieldIndex == -1) {
                        break;
                    }
                    nullBitmapSize = RecordUtil.computeNullBitmapSize((ARecordType) subType);
                    subFieldOffset = ARecordSerializerDeserializer.getFieldOffsetById(serRecord, start, subFieldIndex, nullBitmapSize, ((ARecordType) subType).isOpen());
                    if (subFieldOffset == 0) {
                        // the field is null, we checked the null bit map
                        // any path after null will return null.
                        nullSerde.serialize(ANull.NULL, out);
                        result.set(resultStorage);
                        return;
                    }
                    if (subFieldOffset < 0) {
                        // the field is missing, we checked the missing bit map
                        // any path after missing will return null.
                        missingSerde.serialize(AMissing.MISSING, out);
                        result.set(resultStorage);
                        return;
                    }
                    subType = ((ARecordType) subType).getFieldTypes()[subFieldIndex];
                    if (subType.getTypeTag() == ATypeTag.OBJECT && pathIndex + 1 < fieldPointables.length) {
                        // Move to the next Depth
                        recTypeInfos[pathIndex + 1].reset((ARecordType) subType);
                    }
                    if (subType.getTypeTag().equals(ATypeTag.UNION)) {
                        subTypeTag = ((AUnionType) subType).getActualType().getTypeTag();
                        subFieldLength = NonTaggedFormatUtil.getFieldValueLength(serRecord, subFieldOffset, subTypeTag, false);
                    } else {
                        subTypeTag = subType.getTypeTag();
                        subFieldLength = NonTaggedFormatUtil.getFieldValueLength(serRecord, subFieldOffset, subTypeTag, false);
                    }
                    if (pathIndex < fieldPointables.length - 1) {
                        //setup next iteration
                        subRecordTmpStream.reset();
                        subRecordTmpStream.write(subTypeTag.serialize());
                        subRecordTmpStream.write(serRecord, subFieldOffset, subFieldLength);
                        serRecord = subRecordTmpStream.getByteArray();
                        start = 0;
                    }
                    // type check
                    if (pathIndex < fieldPointables.length - 1 && serRecord[start] != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                        throw new UnsupportedTypeException(BuiltinFunctions.FIELD_ACCESS_NESTED, serRecord[start]);
                    }
                }
                // Moving through open fields after we hit the first open field.
                for (; pathIndex < fieldPointables.length; pathIndex++) {
                    openField = true;
                    subFieldOffset = ARecordSerializerDeserializer.getFieldOffsetByName(serRecord, start, len, fieldPointables[pathIndex].getByteArray(), fieldPointables[pathIndex].getStartOffset());
                    if (subFieldOffset < 0) {
                        out.writeByte(ATypeTag.SERIALIZED_MISSING_TYPE_TAG);
                        result.set(resultStorage);
                        return;
                    }
                    subTypeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(serRecord[subFieldOffset]);
                    subFieldLength = NonTaggedFormatUtil.getFieldValueLength(serRecord, subFieldOffset, subTypeTag, true) + 1;
                    if (pathIndex >= fieldPointables.length - 1) {
                        continue;
                    }
                    //setup next iteration
                    start = subFieldOffset;
                    len = subFieldLength;
                    // type check
                    if (serRecord[start] == ATypeTag.SERIALIZED_MISSING_TYPE_TAG) {
                        missingSerde.serialize(AMissing.MISSING, out);
                        result.set(resultStorage);
                        return;
                    }
                    if (serRecord[start] != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                        throw new UnsupportedTypeException(BuiltinFunctions.FIELD_ACCESS_NESTED.getName(), serRecord[start]);
                    }
                }
                // emit the final result.
                if (openField) {
                    result.set(serRecord, subFieldOffset, subFieldLength);
                } else {
                    out.writeByte(subTypeTag.serialize());
                    out.write(serRecord, subFieldOffset, subFieldLength);
                    result.set(resultStorage);
                }
            } catch (IOException | AsterixException e) {
                throw new HyracksDataException(e);
            }
        }
    };
}
Also used : DataOutput(java.io.DataOutput) AUnionType(org.apache.asterix.om.types.AUnionType) ByteArrayAccessibleOutputStream(org.apache.hyracks.data.std.util.ByteArrayAccessibleOutputStream) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AsterixException(org.apache.asterix.common.exceptions.AsterixException) ATypeTag(org.apache.asterix.om.types.ATypeTag) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) UnsupportedTypeException(org.apache.asterix.runtime.exceptions.UnsupportedTypeException) RuntimeRecordTypeInfo(org.apache.asterix.om.types.runtime.RuntimeRecordTypeInfo) AString(org.apache.asterix.om.base.AString) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 52 with AsterixException

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

the class GetRecordFieldsEvalFactory method createScalarEvaluator.

@Override
public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
    return new IScalarEvaluator() {

        private final ARecordPointable recordPointable = (ARecordPointable) ARecordPointable.FACTORY.createPointable();

        private IPointable inputArg0 = new VoidPointable();

        private IScalarEvaluator eval0 = recordEvalFactory.createScalarEvaluator(ctx);

        private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

        private DataOutput out = resultStorage.getDataOutput();

        private RecordFieldsUtil rfu = new RecordFieldsUtil();

        @Override
        public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
            resultStorage.reset();
            eval0.evaluate(tuple, inputArg0);
            byte[] data = inputArg0.getByteArray();
            int offset = inputArg0.getStartOffset();
            int len = inputArg0.getLength();
            if (data[offset] != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                throw new TypeMismatchException(BuiltinFunctions.GET_RECORD_FIELDS, 0, data[offset], ATypeTag.SERIALIZED_RECORD_TYPE_TAG);
            }
            recordPointable.set(data, offset, len);
            try {
                rfu.processRecord(recordPointable, recordType, out, 0);
            } catch (IOException e) {
                throw new HyracksDataException(e);
            } catch (AsterixException e) {
                throw new HyracksDataException(e);
            }
            result.set(resultStorage);
        }
    };
}
Also used : DataOutput(java.io.DataOutput) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) 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) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AsterixException(org.apache.asterix.common.exceptions.AsterixException) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) ARecordPointable(org.apache.asterix.om.pointables.nonvisitor.ARecordPointable)

Example 53 with AsterixException

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

the class LocalFSInputStreamFactory method configureFileSplits.

private void configureFileSplits(ICcApplicationContext appCtx, String[] splits) throws AsterixException {
    INodeResolver resolver = getNodeResolver();
    Map<InetAddress, Set<String>> ncMap = RuntimeUtils.getForcedNodeControllerMap(appCtx);
    Set<String> ncs = ncMap.values().stream().flatMap(Collection::stream).collect(Collectors.toSet());
    inputFileSplits = new UnmanagedFileSplit[splits.length];
    String node;
    String path;
    int count = 0;
    String trimmedValue;
    for (String splitPath : splits) {
        trimmedValue = splitPath.trim();
        if (!trimmedValue.contains("://")) {
            throw new AsterixException("Invalid path: " + splitPath + "\nUsage- path=\"Host://Absolute File Path\"");
        }
        node = resolver.resolveNode(appCtx, trimmedValue.split(":")[0], ncMap, ncs);
        path = trimmedValue.split("://")[1];
        inputFileSplits[count++] = new UnmanagedFileSplit(node, path);
    }
}
Also used : TreeSet(java.util.TreeSet) Set(java.util.Set) AsterixException(org.apache.asterix.common.exceptions.AsterixException) INodeResolver(org.apache.asterix.external.api.INodeResolver) UnmanagedFileSplit(org.apache.hyracks.api.io.UnmanagedFileSplit) InetAddress(java.net.InetAddress) AlgebricksAbsolutePartitionConstraint(org.apache.hyracks.algebricks.common.constraints.AlgebricksAbsolutePartitionConstraint)

Example 54 with AsterixException

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

the class SocketClientInputStreamFactory method configure.

@Override
public void configure(IServiceContext serviceCtx, Map<String, String> configuration) throws AsterixException {
    try {
        this.serviceCtx = serviceCtx;
        this.sockets = new ArrayList<>();
        String socketsValue = configuration.get(ExternalDataConstants.KEY_SOCKETS);
        if (socketsValue == null) {
            throw new IllegalArgumentException("\'sockets\' parameter not specified as part of adapter configuration");
        }
        String[] socketsArray = socketsValue.split(",");
        for (String socket : socketsArray) {
            String[] socketTokens = socket.split(":");
            String host = socketTokens[0].trim();
            int port = Integer.parseInt(socketTokens[1].trim());
            InetAddress[] resolved;
            resolved = SystemDefaultDnsResolver.INSTANCE.resolve(host);
            Pair<String, Integer> p = new Pair<>(resolved[0].getHostAddress(), port);
            sockets.add(p);
        }
    } catch (UnknownHostException e) {
        throw new AsterixException(e);
    }
}
Also used : AsterixException(org.apache.asterix.common.exceptions.AsterixException) UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress) AlgebricksAbsolutePartitionConstraint(org.apache.hyracks.algebricks.common.constraints.AlgebricksAbsolutePartitionConstraint) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 55 with AsterixException

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

the class ExternalDataUtils method createExternalParserFactory.

public static IDataParserFactory createExternalParserFactory(ILibraryManager libraryManager, String dataverse, String parserFactoryName) throws AsterixException {
    try {
        String library = parserFactoryName.substring(0, parserFactoryName.indexOf(ExternalDataConstants.EXTERNAL_LIBRARY_SEPARATOR));
        ClassLoader classLoader = libraryManager.getLibraryClassLoader(dataverse, library);
        return (IDataParserFactory) classLoader.loadClass(parserFactoryName.substring(parserFactoryName.indexOf(ExternalDataConstants.EXTERNAL_LIBRARY_SEPARATOR) + 1)).newInstance();
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw new AsterixException("Failed to create an external parser factory", e);
    }
}
Also used : AsterixException(org.apache.asterix.common.exceptions.AsterixException) IDataParserFactory(org.apache.asterix.external.api.IDataParserFactory)

Aggregations

AsterixException (org.apache.asterix.common.exceptions.AsterixException)67 IOException (java.io.IOException)27 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)26 DataOutput (java.io.DataOutput)15 IPointable (org.apache.hyracks.data.std.api.IPointable)15 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)15 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)14 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)14 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)14 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)14 ATypeTag (org.apache.asterix.om.types.ATypeTag)10 IAType (org.apache.asterix.om.types.IAType)10 ARecordType (org.apache.asterix.om.types.ARecordType)9 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)9 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)9 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)8 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)8 List (java.util.List)7 InputStream (java.io.InputStream)5 CompilationException (org.apache.asterix.common.exceptions.CompilationException)5