use of org.apache.asterix.om.types.AbstractCollectionType in project asterixdb by apache.
the class StaticTypeCastUtil method rewriteFuncExpr.
/**
* This method is to recursively enforce required types, for the list type and the record type.
* The List constructor is very special because
* 1. a nested list in a list is of type List<ANY>;
* 2. a nested record in a list is of type Open_Record{}.
* The open record constructor is very special because
* 1. a nested list in the open part is of type List<ANY>;
* 2. a nested record in the open part is of type Open_Record{}.
* However, the bottom-up type inference (InferTypeRule in algebricks) did not infer that so we need this method to enforce the type.
* We do not want to break the generality of algebricks so this method is called in an ASTERIX rule: @ IntroduceStaticTypeCastRule} .
*
* @param funcExpr
* the function expression whose type needs to be top-down enforced
* @param reqType
* the required type inferred from parent operators/expressions
* @param inputType
* the current inferred
* @param env
* the type environment
* @return true if the type is casted; otherwise, false.
* @throws AlgebricksException
*/
public static boolean rewriteFuncExpr(AbstractFunctionCallExpression funcExpr, IAType reqType, IAType inputType, IVariableTypeEnvironment env) throws AlgebricksException {
/**
* sanity check: if there are list(ordered or unordered)/record variable expressions in the funcExpr, we will not do STATIC type casting
* because they are not "statically cast-able".
* instead, the record will be dynamically casted at the runtime
*/
if (funcExpr.getFunctionIdentifier() == BuiltinFunctions.UNORDERED_LIST_CONSTRUCTOR) {
if (reqType.equals(BuiltinType.ANY)) {
reqType = DefaultOpenFieldType.NESTED_OPEN_AUNORDERED_LIST_TYPE;
}
return rewriteListFuncExpr(funcExpr, (AbstractCollectionType) reqType, (AbstractCollectionType) inputType, env);
} else if (funcExpr.getFunctionIdentifier() == BuiltinFunctions.ORDERED_LIST_CONSTRUCTOR) {
if (reqType.equals(BuiltinType.ANY)) {
reqType = DefaultOpenFieldType.NESTED_OPEN_AORDERED_LIST_TYPE;
}
return rewriteListFuncExpr(funcExpr, (AbstractCollectionType) reqType, (AbstractCollectionType) inputType, env);
} else if (inputType.getTypeTag().equals(ATypeTag.OBJECT)) {
if (reqType.equals(BuiltinType.ANY)) {
reqType = DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE;
}
return rewriteRecordFuncExpr(funcExpr, (ARecordType) reqType, (ARecordType) inputType, env);
} else {
List<Mutable<ILogicalExpression>> args = funcExpr.getArguments();
boolean changed = false;
for (Mutable<ILogicalExpression> arg : args) {
ILogicalExpression argExpr = arg.getValue();
if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
AbstractFunctionCallExpression argFuncExpr = (AbstractFunctionCallExpression) argExpr;
IAType exprType = (IAType) env.getType(argFuncExpr);
changed = changed || rewriteFuncExpr(argFuncExpr, exprType, exprType, env);
}
}
if (!compatible(reqType, inputType)) {
throw new AlgebricksException("type mismatch, required: " + reqType.toString() + " actual: " + inputType.toString());
}
return changed;
}
}
use of org.apache.asterix.om.types.AbstractCollectionType in project asterixdb by apache.
the class TypeTranslator method addIncompleteCollectionTypeReference.
private static void addIncompleteCollectionTypeReference(AbstractCollectionType collType, TypeReferenceExpression tre, Map<TypeSignature, List<AbstractCollectionType>> incompleteItemTypes, String defaultDataverse) {
String typeName = tre.getIdent().second.getValue();
TypeSignature typeSignature = new TypeSignature(tre.getIdent().first == null ? defaultDataverse : tre.getIdent().first.getValue(), typeName);
List<AbstractCollectionType> typeList = incompleteItemTypes.get(typeSignature);
if (typeList == null) {
typeList = new LinkedList<>();
incompleteItemTypes.put(typeSignature, typeList);
}
typeList.add(collType);
}
use of org.apache.asterix.om.types.AbstractCollectionType in project asterixdb by apache.
the class NonTaggedFormatUtil method getTokenType.
public static IAType getTokenType(IAType keyType) throws AlgebricksException {
IAType type = keyType;
ATypeTag typeTag = keyType.getTypeTag();
// Extract item type from list.
if (typeTag == ATypeTag.MULTISET || typeTag == ATypeTag.ARRAY) {
AbstractCollectionType listType = (AbstractCollectionType) keyType;
if (!listType.isTyped()) {
throw new AlgebricksException("Cannot build an inverted index on untyped lists.)");
}
type = listType.getItemType();
}
return type;
}
use of org.apache.asterix.om.types.AbstractCollectionType in project asterixdb by apache.
the class NonTaggedGetItemResultType method getResultType.
@Override
protected IAType getResultType(ILogicalExpression expr, IAType... strippedInputTypes) throws AlgebricksException {
IAType type = strippedInputTypes[0];
if (type.getTypeTag() == ATypeTag.ANY) {
return BuiltinType.ANY;
}
IAType itemType = ((AbstractCollectionType) type).getItemType();
if (itemType.getTypeTag() == ATypeTag.ANY) {
return itemType;
}
// Could have out-of-bound access or null elements.
return AUnionType.createUnknownableType(itemType);
}
use of org.apache.asterix.om.types.AbstractCollectionType in project asterixdb by apache.
the class TypeResolverUtilTest method testOrderedListType.
@Test
public void testOrderedListType() {
// Constructs input types.
ARecordType leftRecordType = new ARecordType("null", new String[] { "a", "b" }, new IAType[] { BuiltinType.ASTRING, BuiltinType.AINT32 }, true, Collections.singleton("d"));
AOrderedListType leftListType = new AOrderedListType(leftRecordType, "null");
ARecordType rightRecordType = new ARecordType("null", new String[] { "b", "c" }, new IAType[] { BuiltinType.AINT32, BuiltinType.ABINARY }, true, Collections.singleton("e"));
AOrderedListType rightListType = new AOrderedListType(rightRecordType, "null");
// Gets the actual resolved type.
List<IAType> inputTypes = new ArrayList<>();
inputTypes.add(leftListType);
inputTypes.add(rightListType);
AbstractCollectionType resolvedType = (AbstractCollectionType) TypeResolverUtil.resolve(inputTypes);
ARecordType resolvedRecordType = (ARecordType) resolvedType.getItemType();
// Gets the expected generalized type.
Set<String> possibleAdditionalFields = new HashSet<>();
possibleAdditionalFields.add("a");
possibleAdditionalFields.add("c");
possibleAdditionalFields.add("d");
possibleAdditionalFields.add("e");
ARecordType expectedRecordType = new ARecordType(null, new String[] { "b" }, new IAType[] { BuiltinType.AINT32 }, true, possibleAdditionalFields);
AOrderedListType expectedListType = new AOrderedListType(expectedRecordType, null);
// Compares the resolved type and the expected type.
Assert.assertEquals(resolvedType, expectedListType);
Assert.assertEquals(resolvedRecordType.getAllPossibleAdditonalFieldNames(), expectedRecordType.getAllPossibleAdditonalFieldNames());
}
Aggregations