Search in sources :

Example 31 with ARecordType

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

the class FeedMetadataUtil method validateFeed.

public static void validateFeed(Feed feed, MetadataTransactionContext mdTxnCtx, ICcApplicationContext appCtx) throws AsterixException {
    try {
        String adapterName = feed.getAdapterName();
        Map<String, String> configuration = feed.getAdapterConfiguration();
        ARecordType adapterOutputType = getOutputType(feed, configuration, ExternalDataConstants.KEY_TYPE_NAME);
        ARecordType metaType = getOutputType(feed, configuration, ExternalDataConstants.KEY_META_TYPE_NAME);
        ExternalDataUtils.prepareFeed(configuration, feed.getDataverseName(), feed.getFeedName());
        // Get adapter from metadata dataset <Metadata dataverse>
        DatasourceAdapter adapterEntity = MetadataManager.INSTANCE.getAdapter(mdTxnCtx, MetadataConstants.METADATA_DATAVERSE_NAME, adapterName);
        // Get adapter from metadata dataset <The feed dataverse>
        if (adapterEntity == null) {
            adapterEntity = MetadataManager.INSTANCE.getAdapter(mdTxnCtx, feed.getDataverseName(), adapterName);
        }
        AdapterType adapterType;
        IAdapterFactory adapterFactory;
        if (adapterEntity != null) {
            adapterType = adapterEntity.getType();
            String adapterFactoryClassname = adapterEntity.getClassname();
            switch(adapterType) {
                case INTERNAL:
                    adapterFactory = (IAdapterFactory) Class.forName(adapterFactoryClassname).newInstance();
                    break;
                case EXTERNAL:
                    String[] anameComponents = adapterName.split("#");
                    String libraryName = anameComponents[0];
                    ClassLoader cl = appCtx.getLibraryManager().getLibraryClassLoader(feed.getDataverseName(), libraryName);
                    adapterFactory = (IAdapterFactory) cl.loadClass(adapterFactoryClassname).newInstance();
                    break;
                default:
                    throw new AsterixException("Unknown Adapter type " + adapterType);
            }
            adapterFactory.setOutputType(adapterOutputType);
            adapterFactory.setMetaType(metaType);
            adapterFactory.configure(appCtx.getServiceContext(), configuration);
        } else {
            AdapterFactoryProvider.getAdapterFactory(appCtx.getServiceContext(), adapterName, configuration, adapterOutputType, metaType);
        }
        if (metaType == null && configuration.containsKey(ExternalDataConstants.KEY_META_TYPE_NAME)) {
            metaType = getOutputType(feed, configuration, ExternalDataConstants.KEY_META_TYPE_NAME);
            if (metaType == null) {
                throw new AsterixException("Unknown specified feed meta output data type " + configuration.get(ExternalDataConstants.KEY_META_TYPE_NAME));
            }
        }
        if (adapterOutputType == null) {
            if (!configuration.containsKey(ExternalDataConstants.KEY_TYPE_NAME)) {
                throw new AsterixException("Unspecified feed output data type");
            }
            adapterOutputType = getOutputType(feed, configuration, ExternalDataConstants.KEY_TYPE_NAME);
            if (adapterOutputType == null) {
                throw new AsterixException("Unknown specified feed output data type " + configuration.get(ExternalDataConstants.KEY_TYPE_NAME));
            }
        }
    } catch (Exception e) {
        throw new AsterixException("Invalid feed parameters. Exception Message:" + e.getMessage(), e);
    }
}
Also used : DatasourceAdapter(org.apache.asterix.metadata.entities.DatasourceAdapter) AsterixException(org.apache.asterix.common.exceptions.AsterixException) AdapterType(org.apache.asterix.external.api.IDataSourceAdapter.AdapterType) IAdapterFactory(org.apache.asterix.external.api.IAdapterFactory) ARecordType(org.apache.asterix.om.types.ARecordType) AsterixException(org.apache.asterix.common.exceptions.AsterixException) ACIDException(org.apache.asterix.common.exceptions.ACIDException) MetadataException(org.apache.asterix.metadata.MetadataException) CompilationException(org.apache.asterix.common.exceptions.CompilationException) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) RemoteException(java.rmi.RemoteException)

Example 32 with ARecordType

use of org.apache.asterix.om.types.ARecordType 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 33 with ARecordType

use of org.apache.asterix.om.types.ARecordType 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 34 with ARecordType

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

the class InvertedIndexResourceFactoryProvider method getTokenizerFactory.

private static IBinaryTokenizerFactory getTokenizerFactory(Dataset dataset, Index index, ARecordType recordType, ARecordType metaType) throws AlgebricksException {
    int numPrimaryKeys = dataset.getPrimaryKeys().size();
    int numSecondaryKeys = index.getKeyFieldNames().size();
    IndexType indexType = index.getIndexType();
    // Sanity checks.
    if (numPrimaryKeys > 1) {
        throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_INDEX_FOR_DATASET_WITH_COMPOSITE_PRIMARY_INDEX, indexType, RecordUtil.toFullyQualifiedName(dataset.getDataverseName(), dataset.getDatasetName()));
    }
    if (numSecondaryKeys > 1) {
        throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_INDEX_NUM_OF_FIELD, numSecondaryKeys, indexType, 1);
    }
    ARecordType sourceType;
    List<Integer> keySourceIndicators = index.getKeyFieldSourceIndicators();
    if (keySourceIndicators == null || keySourceIndicators.get(0) == 0) {
        sourceType = recordType;
    } else {
        sourceType = metaType;
    }
    Pair<IAType, Boolean> keyTypePair = Index.getNonNullableOpenFieldType(index.getKeyFieldTypes().get(0), index.getKeyFieldNames().get(0), sourceType);
    IAType secondaryKeyType = keyTypePair.first;
    // and add the choice to the index metadata.
    return NonTaggedFormatUtil.getBinaryTokenizerFactory(secondaryKeyType.getTypeTag(), indexType, index.getGramLength());
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) IndexType(org.apache.asterix.common.config.DatasetConfig.IndexType) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 35 with ARecordType

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

the class InvertedIndexResourceFactoryProvider method getTokenTypeTraits.

private static ITypeTraits[] getTokenTypeTraits(Dataset dataset, Index index, ARecordType recordType, ARecordType metaType) throws AlgebricksException {
    int numPrimaryKeys = dataset.getPrimaryKeys().size();
    int numSecondaryKeys = index.getKeyFieldNames().size();
    IndexType indexType = index.getIndexType();
    // Sanity checks.
    if (numPrimaryKeys > 1) {
        throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_INDEX_FOR_DATASET_WITH_COMPOSITE_PRIMARY_INDEX, indexType, RecordUtil.toFullyQualifiedName(dataset.getDataverseName(), dataset.getDatasetName()));
    }
    if (numSecondaryKeys > 1) {
        throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_INDEX_NUM_OF_FIELD, numSecondaryKeys, indexType, 1);
    }
    boolean isPartitioned = indexType == IndexType.LENGTH_PARTITIONED_WORD_INVIX || indexType == IndexType.LENGTH_PARTITIONED_NGRAM_INVIX;
    ARecordType sourceType;
    List<Integer> keySourceIndicators = index.getKeyFieldSourceIndicators();
    if (keySourceIndicators == null || keySourceIndicators.get(0) == 0) {
        sourceType = recordType;
    } else {
        sourceType = metaType;
    }
    Pair<IAType, Boolean> keyTypePair = Index.getNonNullableOpenFieldType(index.getKeyFieldTypes().get(0), index.getKeyFieldNames().get(0), sourceType);
    IAType secondaryKeyType = keyTypePair.first;
    int numTokenFields = (!isPartitioned) ? numSecondaryKeys : numSecondaryKeys + 1;
    ITypeTraits[] tokenTypeTraits = new ITypeTraits[numTokenFields];
    tokenTypeTraits[0] = NonTaggedFormatUtil.getTokenTypeTrait(secondaryKeyType);
    if (isPartitioned) {
        // The partitioning field is hardcoded to be a short *without* an Asterix type tag.
        tokenTypeTraits[1] = ShortPointable.TYPE_TRAITS;
    }
    return tokenTypeTraits;
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) ITypeTraits(org.apache.hyracks.api.dataflow.value.ITypeTraits) IndexType(org.apache.asterix.common.config.DatasetConfig.IndexType) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Aggregations

ARecordType (org.apache.asterix.om.types.ARecordType)105 IAType (org.apache.asterix.om.types.IAType)73 ArrayList (java.util.ArrayList)48 List (java.util.List)24 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)22 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)20 Dataset (org.apache.asterix.metadata.entities.Dataset)19 AString (org.apache.asterix.om.base.AString)19 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)19 Test (org.junit.Test)16 AsterixException (org.apache.asterix.common.exceptions.AsterixException)15 Index (org.apache.asterix.metadata.entities.Index)15 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)15 CompilationException (org.apache.asterix.common.exceptions.CompilationException)13 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)13 Mutable (org.apache.commons.lang3.mutable.Mutable)13 IOException (java.io.IOException)12 MetadataException (org.apache.asterix.metadata.MetadataException)12 AUnionType (org.apache.asterix.om.types.AUnionType)11 Pair (org.apache.hyracks.algebricks.common.utils.Pair)10