Search in sources :

Example 1 with AOrderedListType

use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.

the class TypeTranslator method computeOrderedListType.

private static AOrderedListType computeOrderedListType(TypeSignature typeSignature, OrderedListTypeDefinition oltd, Map<TypeSignature, IAType> typeMap, Map<TypeSignature, List<AbstractCollectionType>> incompleteItemTypes, Map<String, Map<ARecordType, List<Integer>>> incompleteFieldTypes, String defaultDataverse) throws AsterixException {
    TypeExpression tExpr = oltd.getItemTypeExpression();
    String typeName = typeSignature != null ? typeSignature.getName() : null;
    AOrderedListType aolt = new AOrderedListType(null, typeName);
    setCollectionItemType(tExpr, typeMap, incompleteItemTypes, incompleteFieldTypes, aolt, defaultDataverse);
    return aolt;
}
Also used : TypeExpression(org.apache.asterix.lang.common.expression.TypeExpression) AOrderedListType(org.apache.asterix.om.types.AOrderedListType)

Example 2 with AOrderedListType

use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.

the class DatatypeTupleTranslator method writeRecordType.

private void writeRecordType(Datatype instance, AbstractComplexType type, DataOutput out) throws HyracksDataException {
    ArrayBackedValueStorage fieldValue = new ArrayBackedValueStorage();
    ArrayBackedValueStorage itemValue = new ArrayBackedValueStorage();
    IARecordBuilder recordRecordBuilder = new RecordBuilder();
    IARecordBuilder fieldRecordBuilder = new RecordBuilder();
    ARecordType recType = (ARecordType) type;
    OrderedListBuilder listBuilder = new OrderedListBuilder();
    listBuilder.reset(new AOrderedListType(MetadataRecordTypes.FIELD_RECORDTYPE, null));
    IAType fieldType = null;
    for (int i = 0; i < recType.getFieldNames().length; i++) {
        fieldType = recType.getFieldTypes()[i];
        boolean fieldIsNullable = false;
        if (NonTaggedFormatUtil.isOptional(fieldType)) {
            fieldIsNullable = true;
            fieldType = ((AUnionType) fieldType).getActualType();
        }
        if (fieldType.getTypeTag().isDerivedType()) {
            handleNestedDerivedType(fieldType.getTypeName(), (AbstractComplexType) fieldType, instance, instance.getDataverseName(), instance.getDatatypeName());
        }
        itemValue.reset();
        fieldRecordBuilder.reset(MetadataRecordTypes.FIELD_RECORDTYPE);
        // write field 0
        fieldValue.reset();
        aString.setValue(recType.getFieldNames()[i]);
        stringSerde.serialize(aString, fieldValue.getDataOutput());
        fieldRecordBuilder.addField(MetadataRecordTypes.FIELD_ARECORD_FIELDNAME_FIELD_INDEX, fieldValue);
        // write field 1
        fieldValue.reset();
        aString.setValue(fieldType.getTypeName());
        stringSerde.serialize(aString, fieldValue.getDataOutput());
        fieldRecordBuilder.addField(MetadataRecordTypes.FIELD_ARECORD_FIELDTYPE_FIELD_INDEX, fieldValue);
        // write field 2
        fieldValue.reset();
        booleanSerde.serialize(fieldIsNullable ? ABoolean.TRUE : ABoolean.FALSE, fieldValue.getDataOutput());
        fieldRecordBuilder.addField(MetadataRecordTypes.FIELD_ARECORD_ISNULLABLE_FIELD_INDEX, fieldValue);
        // write record
        fieldRecordBuilder.write(itemValue.getDataOutput(), true);
        // add item to the list of fields
        listBuilder.addItem(itemValue);
    }
    recordRecordBuilder.reset(MetadataRecordTypes.RECORD_RECORDTYPE);
    // write field 0
    fieldValue.reset();
    booleanSerde.serialize(recType.isOpen() ? ABoolean.TRUE : ABoolean.FALSE, fieldValue.getDataOutput());
    recordRecordBuilder.addField(MetadataRecordTypes.RECORDTYPE_ARECORD_ISOPEN_FIELD_INDEX, fieldValue);
    // write field 1
    fieldValue.reset();
    listBuilder.write(fieldValue.getDataOutput(), true);
    recordRecordBuilder.addField(MetadataRecordTypes.RECORDTYPE_ARECORD_FIELDS_FIELD_INDEX, fieldValue);
    // write record
    recordRecordBuilder.write(out, true);
}
Also used : ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) RecordBuilder(org.apache.asterix.builders.RecordBuilder) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 3 with AOrderedListType

use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.

the class DatatypeTupleTranslator method createDataTypeFromARecord.

private Datatype createDataTypeFromARecord(ARecord datatypeRecord) throws MetadataException {
    String dataverseName = ((AString) datatypeRecord.getValueByPos(MetadataRecordTypes.DATATYPE_ARECORD_DATAVERSENAME_FIELD_INDEX)).getStringValue();
    String datatypeName = ((AString) datatypeRecord.getValueByPos(MetadataRecordTypes.DATATYPE_ARECORD_DATATYPENAME_FIELD_INDEX)).getStringValue();
    IAType type = BuiltinTypeMap.getBuiltinType(datatypeName);
    if (type == null) {
        // Derived Type
        ARecord derivedTypeRecord = (ARecord) datatypeRecord.getValueByPos(MetadataRecordTypes.DATATYPE_ARECORD_DERIVED_FIELD_INDEX);
        DerivedTypeTag tag = DerivedTypeTag.valueOf(((AString) derivedTypeRecord.getValueByPos(MetadataRecordTypes.DERIVEDTYPE_ARECORD_TAG_FIELD_INDEX)).getStringValue());
        boolean isAnonymous = ((ABoolean) derivedTypeRecord.getValueByPos(MetadataRecordTypes.DERIVEDTYPE_ARECORD_ISANONYMOUS_FIELD_INDEX)).getBoolean();
        switch(tag) {
            case RECORD:
                {
                    ARecord recordType = (ARecord) derivedTypeRecord.getValueByPos(MetadataRecordTypes.DERIVEDTYPE_ARECORD_RECORD_FIELD_INDEX);
                    boolean isOpen = ((ABoolean) recordType.getValueByPos(MetadataRecordTypes.RECORDTYPE_ARECORD_ISOPEN_FIELD_INDEX)).getBoolean().booleanValue();
                    int numberOfFields = ((AOrderedList) recordType.getValueByPos(MetadataRecordTypes.RECORDTYPE_ARECORD_FIELDS_FIELD_INDEX)).size();
                    IACursor cursor = ((AOrderedList) recordType.getValueByPos(MetadataRecordTypes.RECORDTYPE_ARECORD_FIELDS_FIELD_INDEX)).getCursor();
                    String[] fieldNames = new String[numberOfFields];
                    IAType[] fieldTypes = new IAType[numberOfFields];
                    int fieldId = 0;
                    String fieldTypeName;
                    while (cursor.next()) {
                        ARecord field = (ARecord) cursor.get();
                        fieldNames[fieldId] = ((AString) field.getValueByPos(MetadataRecordTypes.FIELD_ARECORD_FIELDNAME_FIELD_INDEX)).getStringValue();
                        fieldTypeName = ((AString) field.getValueByPos(MetadataRecordTypes.FIELD_ARECORD_FIELDTYPE_FIELD_INDEX)).getStringValue();
                        boolean isNullable = ((ABoolean) field.getValueByPos(MetadataRecordTypes.FIELD_ARECORD_ISNULLABLE_FIELD_INDEX)).getBoolean().booleanValue();
                        fieldTypes[fieldId] = BuiltinTypeMap.getTypeFromTypeName(metadataNode, jobId, dataverseName, fieldTypeName, isNullable);
                        fieldId++;
                    }
                    return new Datatype(dataverseName, datatypeName, new ARecordType(datatypeName, fieldNames, fieldTypes, isOpen), isAnonymous);
                }
            case UNORDEREDLIST:
                {
                    String unorderedlistTypeName = ((AString) derivedTypeRecord.getValueByPos(MetadataRecordTypes.DERIVEDTYPE_ARECORD_UNORDEREDLIST_FIELD_INDEX)).getStringValue();
                    return new Datatype(dataverseName, datatypeName, new AUnorderedListType(BuiltinTypeMap.getTypeFromTypeName(metadataNode, jobId, dataverseName, unorderedlistTypeName, false), datatypeName), isAnonymous);
                }
            case ORDEREDLIST:
                {
                    String orderedlistTypeName = ((AString) derivedTypeRecord.getValueByPos(MetadataRecordTypes.DERIVEDTYPE_ARECORD_ORDEREDLIST_FIELD_INDEX)).getStringValue();
                    return new Datatype(dataverseName, datatypeName, new AOrderedListType(BuiltinTypeMap.getTypeFromTypeName(metadataNode, jobId, dataverseName, orderedlistTypeName, false), datatypeName), isAnonymous);
                }
            default:
                throw new UnsupportedOperationException("Unsupported derived type: " + tag);
        }
    }
    return new Datatype(dataverseName, datatypeName, type, false);
}
Also used : ABoolean(org.apache.asterix.om.base.ABoolean) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) AString(org.apache.asterix.om.base.AString) IACursor(org.apache.asterix.om.base.IACursor) AUnorderedListType(org.apache.asterix.om.types.AUnorderedListType) Datatype(org.apache.asterix.metadata.entities.Datatype) ARecord(org.apache.asterix.om.base.ARecord) AString(org.apache.asterix.om.base.AString) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 4 with AOrderedListType

use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.

the class StringSplitDescriptor 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 {
            return new IScalarEvaluator() {

                // Argument evaluators.
                private final IScalarEvaluator stringEval = args[0].createScalarEvaluator(ctx);

                private final IScalarEvaluator patternEval = args[1].createScalarEvaluator(ctx);

                // Argument pointers.
                private final IPointable argString = new VoidPointable();

                private final IPointable argPattern = new VoidPointable();

                private final UTF8StringPointable argStrPtr = new UTF8StringPointable();

                private final UTF8StringPointable argPatternPtr = new UTF8StringPointable();

                // For an output string item.
                private final ArrayBackedValueStorage itemStorge = new ArrayBackedValueStorage();

                private final DataOutput itemOut = itemStorge.getDataOutput();

                private final byte[] tempLengthArray = new byte[5];

                // For the output list of strings.
                private final AOrderedListType intListType = new AOrderedListType(BuiltinType.ASTRING, null);

                private final OrderedListBuilder listBuilder = new OrderedListBuilder();

                private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private final DataOutput out = resultStorage.getDataOutput();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    try {
                        resultStorage.reset();
                        // Calls argument evaluators.
                        stringEval.evaluate(tuple, argString);
                        patternEval.evaluate(tuple, argPattern);
                        // Gets the bytes of the source string.
                        byte[] srcString = argString.getByteArray();
                        int srcOffset = argString.getStartOffset();
                        int srcLen = argString.getLength();
                        // Type check for the first argument.
                        if (srcString[srcOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 0, srcString[srcOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        // Gets the bytes of the pattern string.
                        byte[] patternString = argPattern.getByteArray();
                        int patternOffset = argPattern.getStartOffset();
                        int patternLen = argPattern.getLength();
                        // Type check for the second argument.
                        if (patternString[patternOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 1, patternString[patternOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        // Sets the UTF8 String pointables.
                        argStrPtr.set(srcString, srcOffset + 1, srcLen - 1);
                        argPatternPtr.set(patternString, patternOffset + 1, patternLen - 1);
                        // Gets the string length of the source string.
                        int inputStringLen = UTF8StringUtil.getUTFLength(srcString, srcOffset + 1);
                        int inputStringStart = srcOffset + 1 + UTF8StringUtil.getNumBytesToStoreLength(inputStringLen);
                        // Gets the string length of the pattern string.
                        int inputPatternLen = UTF8StringUtil.getUTFLength(patternString, patternOffset + 1);
                        // Handles the case that the pattern is "".
                        boolean emptyStringPattern = inputPatternLen == 0;
                        // Builds a list of strings.
                        listBuilder.reset(intListType);
                        int itemStrStart = 0;
                        int nextMatchStart;
                        while (itemStrStart < inputStringLen && (nextMatchStart = UTF8StringPointable.find(argStrPtr, argPatternPtr, false, itemStrStart)) >= 0) {
                            // Adds an item string.
                            addItemString(srcString, inputStringStart, itemStrStart, emptyStringPattern ? nextMatchStart + 1 : nextMatchStart);
                            itemStrStart = nextMatchStart + (emptyStringPattern ? 1 : inputPatternLen);
                        }
                        if (!emptyStringPattern) {
                            addItemString(srcString, inputStringStart, itemStrStart, inputStringLen);
                        }
                        listBuilder.write(out, true);
                        result.set(resultStorage);
                    } catch (IOException e1) {
                        throw new HyracksDataException(e1);
                    }
                }

                private void addItemString(byte[] srcString, int inputStringStart, int itemStartOffset, int nextMatchStart) throws IOException {
                    int itemLen = nextMatchStart - itemStartOffset;
                    int cbytes = UTF8StringUtil.encodeUTF8Length(itemLen, tempLengthArray, 0);
                    itemStorge.reset();
                    itemOut.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                    itemOut.write(tempLengthArray, 0, cbytes);
                    if (itemLen > 0) {
                        itemOut.write(srcString, inputStringStart + itemStartOffset, itemLen);
                    }
                    listBuilder.addItem(itemStorge);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) 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) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)

Example 5 with AOrderedListType

use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.

the class StringToCodePointDescriptor 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 {
            return new IScalarEvaluator() {

                protected final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                protected final DataOutput out = resultStorage.getDataOutput();

                protected final IPointable argPtr = new VoidPointable();

                protected final IScalarEvaluator stringEval = args[0].createScalarEvaluator(ctx);

                protected final AOrderedListType intListType = new AOrderedListType(BuiltinType.AINT64, null);

                private OrderedListBuilder listBuilder = new OrderedListBuilder();

                private ArrayBackedValueStorage inputVal = new ArrayBackedValueStorage();

                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<AInt64> int64Serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);

                private final AMutableInt64 aInt64 = new AMutableInt64(0);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    try {
                        resultStorage.reset();
                        stringEval.evaluate(tuple, argPtr);
                        byte[] serString = argPtr.getByteArray();
                        int offset = argPtr.getStartOffset();
                        if (serString[offset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            int len = UTF8StringUtil.getUTFLength(serString, offset + 1);
                            int start = offset + 1 + UTF8StringUtil.getNumBytesToStoreLength(len);
                            int pos = 0;
                            listBuilder.reset(intListType);
                            while (pos < len) {
                                int codePoint = UTF8StringUtil.UTF8ToCodePoint(serString, start + pos);
                                pos += UTF8StringUtil.charSize(serString, start + pos);
                                inputVal.reset();
                                aInt64.setValue(codePoint);
                                int64Serde.serialize(aInt64, inputVal.getDataOutput());
                                listBuilder.addItem(inputVal);
                            }
                            listBuilder.write(out, true);
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, serString[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        result.set(resultStorage);
                    } catch (IOException e1) {
                        throw new HyracksDataException(e1);
                    }
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) 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) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutableInt64(org.apache.asterix.om.base.AMutableInt64)

Aggregations

AOrderedListType (org.apache.asterix.om.types.AOrderedListType)26 IAType (org.apache.asterix.om.types.IAType)16 ARecordType (org.apache.asterix.om.types.ARecordType)13 ArrayList (java.util.ArrayList)9 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)8 OrderedListBuilder (org.apache.asterix.builders.OrderedListBuilder)7 DataOutput (java.io.DataOutput)6 AString (org.apache.asterix.om.base.AString)6 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)6 AOrderedList (org.apache.asterix.om.base.AOrderedList)5 AUnionType (org.apache.asterix.om.types.AUnionType)5 Test (org.junit.Test)5 IOException (java.io.IOException)4 ATypeTag (org.apache.asterix.om.types.ATypeTag)4 AUnorderedListType (org.apache.asterix.om.types.AUnorderedListType)4 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)4 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)4 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)4 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)4 IPointable (org.apache.hyracks.data.std.api.IPointable)4