Search in sources :

Example 51 with IAType

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

the class DatasetUtil method computeFilterTypeTraits.

public static ITypeTraits[] computeFilterTypeTraits(Dataset dataset, ARecordType itemType) throws AlgebricksException {
    if (dataset.getDatasetType() == DatasetType.EXTERNAL) {
        return null;
    }
    List<String> filterField = getFilterField(dataset);
    if (filterField == null) {
        return null;
    }
    ITypeTraits[] typeTraits = new ITypeTraits[1];
    IAType type = itemType.getSubFieldType(filterField);
    typeTraits[0] = TypeTraitProvider.INSTANCE.getTypeTrait(type);
    return typeTraits;
}
Also used : ITypeTraits(org.apache.hyracks.api.dataflow.value.ITypeTraits) AMutableString(org.apache.asterix.om.base.AMutableString) AString(org.apache.asterix.om.base.AString) IAType(org.apache.asterix.om.types.IAType)

Example 52 with IAType

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

use of org.apache.asterix.om.types.IAType 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)

Example 54 with IAType

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

the class FieldAccessByIndexEvalFactory method createScalarEvaluator.

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

        private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

        private DataOutput out = resultStorage.getDataOutput();

        private IPointable inputArg0 = new VoidPointable();

        private IPointable inputArg1 = new VoidPointable();

        private IScalarEvaluator eval0 = recordEvalFactory.createScalarEvaluator(ctx);

        private IScalarEvaluator eval1 = fieldIndexEvalFactory.createScalarEvaluator(ctx);

        private int fieldIndex;

        private int fieldValueOffset;

        private int fieldValueLength;

        private IAType fieldValueType;

        private ATypeTag fieldValueTypeTag;

        /*
             * inputArg0: the record
             * inputArg1: the index
             *
             * This method outputs into IHyracksTaskContext context [field type tag (1 byte)][the field data]
             */
        @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();
                if (serRecord[offset] != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                    throw new TypeMismatchException(BuiltinFunctions.FIELD_ACCESS_BY_INDEX, 0, serRecord[offset], ATypeTag.SERIALIZED_RECORD_TYPE_TAG);
                }
                eval1.evaluate(tuple, inputArg1);
                byte[] indexBytes = inputArg1.getByteArray();
                int indexOffset = inputArg1.getStartOffset();
                if (indexBytes[indexOffset] != ATypeTag.SERIALIZED_INT32_TYPE_TAG) {
                    throw new TypeMismatchException(BuiltinFunctions.FIELD_ACCESS_BY_INDEX, 1, indexBytes[offset], ATypeTag.SERIALIZED_INT32_TYPE_TAG);
                }
                fieldIndex = IntegerPointable.getInteger(indexBytes, indexOffset + 1);
                fieldValueType = recordType.getFieldTypes()[fieldIndex];
                fieldValueOffset = ARecordSerializerDeserializer.getFieldOffsetById(serRecord, offset, fieldIndex, nullBitmapSize, recordType.isOpen());
                if (fieldValueOffset == 0) {
                    // the field is null, we checked the null bit map
                    out.writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG);
                    result.set(resultStorage);
                    return;
                }
                if (fieldValueOffset < 0) {
                    // the field is missing, we checked the missing bit map
                    out.writeByte(ATypeTag.SERIALIZED_MISSING_TYPE_TAG);
                    result.set(resultStorage);
                    return;
                }
                if (fieldValueType.getTypeTag().equals(ATypeTag.UNION)) {
                    if (((AUnionType) fieldValueType).isUnknownableType()) {
                        fieldValueTypeTag = ((AUnionType) fieldValueType).getActualType().getTypeTag();
                        fieldValueLength = NonTaggedFormatUtil.getFieldValueLength(serRecord, fieldValueOffset, fieldValueTypeTag, false);
                        out.writeByte(fieldValueTypeTag.serialize());
                    } else {
                        // union .. the general case
                        throw new NotImplementedException();
                    }
                } else {
                    fieldValueTypeTag = fieldValueType.getTypeTag();
                    fieldValueLength = NonTaggedFormatUtil.getFieldValueLength(serRecord, fieldValueOffset, fieldValueTypeTag, false);
                    out.writeByte(fieldValueTypeTag.serialize());
                }
                out.write(serRecord, fieldValueOffset, fieldValueLength);
                result.set(resultStorage);
            } catch (IOException e) {
                throw new HyracksDataException(e);
            } catch (AsterixException e) {
                throw new HyracksDataException(e);
            }
        }
    };
}
Also used : DataOutput(java.io.DataOutput) AUnionType(org.apache.asterix.om.types.AUnionType) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) NotImplementedException(org.apache.hyracks.algebricks.common.exceptions.NotImplementedException) 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) ATypeTag(org.apache.asterix.om.types.ATypeTag) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) IAType(org.apache.asterix.om.types.IAType)

Example 55 with IAType

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

the class ValidateUtil method validateKeyFields.

/**
     * Validates the key fields that will be used as keys of an index.
     *
     * @param recType
     *            the record type
     * @param keyFieldNames
     *            a map of key fields that will be validated
     * @param keyFieldTypes
     *            a map of key types (if provided) that will be validated
     * @param indexType
     *            the type of the index that its key fields is being validated
     * @throws AlgebricksException
     */
public static void validateKeyFields(ARecordType recType, ARecordType metaRecType, List<List<String>> keyFieldNames, List<Integer> keySourceIndicators, List<IAType> keyFieldTypes, IndexType indexType) throws AlgebricksException {
    List<IAType> fieldTypes = KeyFieldTypeUtil.getKeyTypes(recType, metaRecType, keyFieldNames, keySourceIndicators);
    int pos = 0;
    boolean openFieldCompositeIdx = false;
    for (IAType fieldType : fieldTypes) {
        List<String> fieldName = keyFieldNames.get(pos);
        if (fieldType == null) {
            fieldType = keyFieldTypes.get(pos);
            if (keyFieldTypes.get(pos) == BuiltinType.AMISSING) {
                throw new AsterixException("A field with this name  \"" + fieldName + "\" could not be found.");
            }
        } else if (openFieldCompositeIdx) {
            throw new AsterixException("A closed field \"" + fieldName + "\" could be only in a prefix part of the composite index, containing opened field.");
        }
        if (keyFieldTypes.get(pos) != BuiltinType.AMISSING && fieldType.getTypeTag() != keyFieldTypes.get(pos).getTypeTag()) {
            throw new AsterixException("A field \"" + fieldName + "\" is already defined with the type \"" + fieldType + "\"");
        }
        switch(indexType) {
            case BTREE:
                switch(fieldType.getTypeTag()) {
                    case TINYINT:
                    case SMALLINT:
                    case INTEGER:
                    case BIGINT:
                    case FLOAT:
                    case DOUBLE:
                    case STRING:
                    case BINARY:
                    case DATE:
                    case TIME:
                    case DATETIME:
                    case UNION:
                    case UUID:
                    case YEARMONTHDURATION:
                    case DAYTIMEDURATION:
                        break;
                    default:
                        throw new AsterixException("The field \"" + fieldName + "\" which is of type " + fieldType.getTypeTag() + " cannot be indexed using the BTree index.");
                }
                break;
            case RTREE:
                switch(fieldType.getTypeTag()) {
                    case POINT:
                    case LINE:
                    case RECTANGLE:
                    case CIRCLE:
                    case POLYGON:
                    case UNION:
                        break;
                    default:
                        throw new AsterixException("The field \"" + fieldName + "\" which is of type " + fieldType.getTypeTag() + " cannot be indexed using the RTree index.");
                }
                break;
            case LENGTH_PARTITIONED_NGRAM_INVIX:
                switch(fieldType.getTypeTag()) {
                    case STRING:
                    case UNION:
                        break;
                    default:
                        throw new AsterixException("The field \"" + fieldName + "\" which is of type " + fieldType.getTypeTag() + " cannot be indexed using the Length Partitioned N-Gram index.");
                }
                break;
            case LENGTH_PARTITIONED_WORD_INVIX:
                switch(fieldType.getTypeTag()) {
                    case STRING:
                    case MULTISET:
                    case ARRAY:
                    case UNION:
                        break;
                    default:
                        throw new AsterixException("The field \"" + fieldName + "\" which is of type " + fieldType.getTypeTag() + " cannot be indexed using the Length Partitioned Keyword index.");
                }
                break;
            case SINGLE_PARTITION_NGRAM_INVIX:
                switch(fieldType.getTypeTag()) {
                    case STRING:
                    case UNION:
                        break;
                    default:
                        throw new AsterixException("The field \"" + fieldName + "\" which is of type " + fieldType.getTypeTag() + " cannot be indexed using the N-Gram index.");
                }
                break;
            case SINGLE_PARTITION_WORD_INVIX:
                switch(fieldType.getTypeTag()) {
                    case STRING:
                    case MULTISET:
                    case ARRAY:
                    case UNION:
                        break;
                    default:
                        throw new AsterixException("The field \"" + fieldName + "\" which is of type " + fieldType.getTypeTag() + " cannot be indexed using the Keyword index.");
                }
                break;
            default:
                throw new AsterixException("Invalid index type: " + indexType + ".");
        }
        pos++;
    }
}
Also used : AsterixException(org.apache.asterix.common.exceptions.AsterixException) IAType(org.apache.asterix.om.types.IAType)

Aggregations

IAType (org.apache.asterix.om.types.IAType)190 ARecordType (org.apache.asterix.om.types.ARecordType)73 ArrayList (java.util.ArrayList)64 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)42 ATypeTag (org.apache.asterix.om.types.ATypeTag)40 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)37 List (java.util.List)32 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)32 AUnionType (org.apache.asterix.om.types.AUnionType)31 AString (org.apache.asterix.om.base.AString)28 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)27 Mutable (org.apache.commons.lang3.mutable.Mutable)25 Pair (org.apache.hyracks.algebricks.common.utils.Pair)24 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)20 Dataset (org.apache.asterix.metadata.entities.Dataset)18 AsterixException (org.apache.asterix.common.exceptions.AsterixException)17 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)16 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)16 IVisitablePointable (org.apache.asterix.om.pointables.base.IVisitablePointable)15 IVariableTypeEnvironment (org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)15