use of org.apache.asterix.om.types.IAType in project asterixdb by apache.
the class JSONDeserializerForTypes method convertFromJSON.
/**
* Deserialize an arbitrary JSON representation of a type.
*
* @param typeInJSON
* the JSON representation of the type.
* @return an valid AsterixDB type.
* @throws Exception
*/
public static IAType convertFromJSON(JsonNode typeInJSON) throws Exception {
String typeName = typeInJSON.get("type").asText();
// Deals with ordered list.
if (typeName.equals(AOrderedListType.class.getName())) {
IAType itemType = convertFromJSON(typeInJSON.get("item-type"));
return new AOrderedListType(itemType, "ordered-list");
}
// Deals with unordered list.
if (typeName.equals(AUnorderedListType.class.getName())) {
IAType itemType = convertFromJSON(typeInJSON.get("item-type"));
return new AUnorderedListType(itemType, "unordered-list");
}
// Deals with Union Type.
if (typeName.equals(AUnionType.class.getName())) {
List<IAType> unionTypes = new ArrayList<IAType>();
JsonNode fields = typeInJSON.get("fields");
for (int i = 0; i < fields.size(); i++) {
JsonNode fieldType = fields.get(i);
unionTypes.add(convertFromJSON(fieldType));
}
return new AUnionType(unionTypes, "union");
}
// Deals with record types.
if (typeName.equals(ARecordType.class.getName())) {
String name = typeInJSON.get("name").asText();
boolean openType = typeInJSON.get("open").asBoolean();
JsonNode fields = typeInJSON.get("fields");
String[] fieldNames = new String[fields.size()];
IAType[] fieldTypes = new IAType[fields.size()];
for (int i = 0; i < fields.size(); ++i) {
JsonNode field = fields.get(i);
List<String> names = Lists.newArrayList(field.fieldNames());
String fieldName = names.get(0);
fieldNames[i] = fieldName;
fieldTypes[i] = convertFromJSON(field.get(fieldName));
}
return new ARecordType(name, fieldNames, fieldTypes, openType);
}
// Deals with primitive types.
Class<?> cl = BuiltinType.class;
Field typeField = cl.getDeclaredField(typeName.toUpperCase());
return (IAType) typeField.get(null);
}
use of org.apache.asterix.om.types.IAType 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.IAType in project asterixdb by apache.
the class RecordRemoveFieldsTypeComputer method computeType.
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expression;
String funcName = funcExpr.getFunctionIdentifier().getName();
IAType type0 = (IAType) env.getType(funcExpr.getArguments().get(0).getValue());
List<List<String>> pathList = new ArrayList<>();
Set<String> fieldNameSet = new HashSet<>();
Deque<String> fieldPathStack = new ArrayDeque<>();
ARecordType inputRecordType = getRecordTypeFromType(funcName, type0);
if (inputRecordType == null) {
return BuiltinType.ANY;
}
AbstractLogicalExpression arg1 = (AbstractLogicalExpression) funcExpr.getArguments().get(1).getValue();
IAType inputListType = (IAType) env.getType(arg1);
AOrderedListType inputOrderedListType = TypeComputeUtils.extractOrderedListType(inputListType);
if (inputOrderedListType == null) {
throw new TypeMismatchException(funcName, 1, inputListType.getTypeTag(), ATypeTag.ARRAY);
}
ATypeTag tt = inputOrderedListType.getItemType().getTypeTag();
if (tt == ATypeTag.STRING) {
// If top-fieldlist
if (setFieldNameSet(arg1, fieldNameSet)) {
return buildOutputType(fieldPathStack, inputRecordType, fieldNameSet, pathList);
} else {
return DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE;
}
} else {
// tt == ATypeTag.ANY, meaning the list is nested
computeTypeFromNonConstantExpression(funcName, arg1, fieldNameSet, pathList);
IAType resultType = buildOutputType(fieldPathStack, inputRecordType, fieldNameSet, pathList);
return resultType;
}
}
use of org.apache.asterix.om.types.IAType in project asterixdb by apache.
the class RecordRemoveFieldsTypeComputer method buildOutputType.
private IAType buildOutputType(Deque<String> fieldPathStack, ARecordType inputRecordType, Set<String> fieldNameSet, List<List<String>> pathList) throws AlgebricksException {
List<String> resultFieldNames = new ArrayList<>();
List<IAType> resultFieldTypes = new ArrayList<>();
String[] fieldNames = inputRecordType.getFieldNames();
IAType[] fieldTypes = inputRecordType.getFieldTypes();
for (int i = 0; i < fieldNames.length; i++) {
if (!fieldNameSet.contains(fieldNames[i])) {
// The main field is to be kept
addField(inputRecordType, fieldNames[i], resultFieldNames, resultFieldTypes);
} else if (!pathList.isEmpty() && fieldTypes[i].getTypeTag() == ATypeTag.OBJECT) {
ARecordType subRecord = (ARecordType) fieldTypes[i];
fieldPathStack.push(fieldNames[i]);
subRecord = deepCheckAndCopy(fieldPathStack, subRecord, pathList, inputRecordType.isOpen());
fieldPathStack.pop();
if (subRecord != null) {
resultFieldNames.add(fieldNames[i]);
resultFieldTypes.add(subRecord);
}
}
}
int n = resultFieldNames.size();
String resultTypeName = "result-record(" + inputRecordType.getTypeName() + ")";
return new ARecordType(resultTypeName, resultFieldNames.toArray(new String[n]), resultFieldTypes.toArray(new IAType[n]), // Make the output type open always
true);
}
use of org.apache.asterix.om.types.IAType in project asterixdb by apache.
the class RecordRemoveFieldsTypeComputer method deepCheckAndCopy.
/*
A method to deep copy a record the path validation
i.e., keep only fields that are valid
*/
private ARecordType deepCheckAndCopy(Deque<String> fieldPath, ARecordType srcRecType, List<List<String>> pathList, boolean isOpen) throws AlgebricksException {
// Make sure the current path is valid before going further
if (isRemovePath(fieldPath, pathList)) {
return null;
}
String[] srcFieldNames = srcRecType.getFieldNames();
IAType[] srcFieldTypes = srcRecType.getFieldTypes();
List<IAType> destFieldTypes = new ArrayList<>();
List<String> destFieldNames = new ArrayList<>();
for (int i = 0; i < srcFieldNames.length; i++) {
fieldPath.push(srcFieldNames[i]);
if (!isRemovePath(fieldPath, pathList)) {
if (srcFieldTypes[i].getTypeTag() == ATypeTag.OBJECT) {
ARecordType subRecord = (ARecordType) srcFieldTypes[i];
subRecord = deepCheckAndCopy(fieldPath, subRecord, pathList, isOpen);
if (subRecord != null) {
destFieldNames.add(srcFieldNames[i]);
destFieldTypes.add(subRecord);
}
} else {
destFieldNames.add(srcFieldNames[i]);
destFieldTypes.add(srcFieldTypes[i]);
}
}
fieldPath.pop();
}
int n = destFieldNames.size();
if (n == 0) {
return null;
}
return new ARecordType(srcRecType.getTypeName(), destFieldNames.toArray(new String[n]), destFieldTypes.toArray(new IAType[n]), isOpen);
}
Aggregations