use of org.apache.asterix.om.types.AOrderedListType 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.AOrderedListType 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.AOrderedListType in project asterixdb by apache.
the class FieldAccessNestedResultType method checkOrderedList.
private void checkOrderedList(String funcName, IAType type) throws AlgebricksException {
AOrderedListType listType = (AOrderedListType) type;
ATypeTag itemTypeTag = listType.getItemType().getTypeTag();
if (itemTypeTag != ATypeTag.STRING && itemTypeTag != ATypeTag.ANY) {
throw new UnsupportedItemTypeException(funcName, itemTypeTag);
}
}
use of org.apache.asterix.om.types.AOrderedListType 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());
}
use of org.apache.asterix.om.types.AOrderedListType in project asterixdb by apache.
the class ClassAdParser method writeFieldValueToBuffer.
private void writeFieldValueToBuffer(IAType fieldType, DataOutput out, String name, ExprTree tree, ClassAd pAd) throws IOException, AsterixException {
Value val;
switch(tree.getKind()) {
case ATTRREF_NODE:
case CLASSAD_NODE:
case EXPR_ENVELOPE:
case EXPR_LIST_NODE:
case FN_CALL_NODE:
case OP_NODE:
val = objectPool.valuePool.get();
if (pAd.evaluateAttr(name, val)) {
} else {
// just write the expr
val = ((Literal) pAd.getAttrList().get(name + "Expr")).getValue();
}
break;
case LITERAL_NODE:
val = ((Literal) tree.getTree()).getValue();
break;
default:
throw new HyracksDataException("Unknown Expression type detected: " + tree.getKind());
}
if (fieldType != null) {
if (NonTaggedFormatUtil.isOptional(fieldType)) {
fieldType = ((AUnionType) fieldType).getActualType();
}
}
switch(val.getValueType()) {
case ABSOLUTE_TIME_VALUE:
if (checkType(ATypeTag.DATETIME, fieldType)) {
parseDateTime(val, out);
} else {
throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
}
break;
case BOOLEAN_VALUE:
if (checkType(ATypeTag.BOOLEAN, fieldType)) {
booleanSerde.serialize(val.getBoolVal() ? ABoolean.TRUE : ABoolean.FALSE, out);
} else {
throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
}
break;
case CLASSAD_VALUE:
if (checkType(ATypeTag.OBJECT, fieldType)) {
IAType objectType = getComplexType(fieldType, ATypeTag.OBJECT);
ClassAd classad = val.getClassadVal();
parseRecord((ARecordType) objectType, classad, out);
} else {
throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
}
break;
case ERROR_VALUE:
case STRING_VALUE:
case UNDEFINED_VALUE:
if (checkType(ATypeTag.STRING, fieldType)) {
parseString(val, out);
} else {
throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
}
break;
case INTEGER_VALUE:
if (checkType(ATypeTag.BIGINT, fieldType)) {
if (fieldType == null || fieldType.getTypeTag() == ATypeTag.BIGINT) {
aInt64.setValue(val.getLongVal());
int64Serde.serialize(aInt64, out);
} else if (fieldType.getTypeTag() == ATypeTag.INTEGER) {
aInt32.setValue((int) val.getLongVal());
int32Serde.serialize(aInt32, out);
} else if (fieldType.getTypeTag() == ATypeTag.DOUBLE) {
aDouble.setValue(val.getLongVal());
doubleSerde.serialize(aDouble, out);
} else if (fieldType.getTypeTag() == ATypeTag.SMALLINT) {
aInt16.setValue((short) val.getLongVal());
int16Serde.serialize(aInt16, out);
} else if (fieldType.getTypeTag() == ATypeTag.TINYINT) {
aInt8.setValue((byte) val.getLongVal());
int8Serde.serialize(aInt8, out);
} else if (fieldType.getTypeTag() == ATypeTag.FLOAT) {
aFloat.setValue(val.getLongVal());
floatSerde.serialize(aFloat, out);
}
} else if (checkType(ATypeTag.DATETIME, fieldType)) {
// Classad uses Linux Timestamps (s instead of ms)
aDateTime.setValue(val.getLongVal() * 1000);
datetimeSerde.serialize(aDateTime, out);
} else if (checkType(ATypeTag.DURATION, fieldType)) {
// Classad uses Linux Timestamps (s instead of ms)
aDuration.setValue(0, val.getLongVal() * 1000);
durationSerde.serialize(aDuration, out);
} else if (checkType(ATypeTag.INTEGER, fieldType)) {
aInt32.setValue((int) val.getLongVal());
int32Serde.serialize(aInt32, out);
} else if (checkType(ATypeTag.DOUBLE, fieldType)) {
aDouble.setValue(val.getLongVal());
doubleSerde.serialize(aDouble, out);
} else {
throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
}
break;
case LIST_VALUE:
case SLIST_VALUE:
IAType objectType;
if (checkType(ATypeTag.MULTISET, fieldType)) {
objectType = getComplexType(fieldType, ATypeTag.MULTISET);
parseUnorderedList((AUnorderedListType) objectType, val, out);
} else if (checkType(ATypeTag.ARRAY, fieldType)) {
objectType = getComplexType(fieldType, ATypeTag.ARRAY);
parseOrderedList((AOrderedListType) objectType, val, out);
} else {
throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
}
break;
case REAL_VALUE:
if (checkType(ATypeTag.DOUBLE, fieldType)) {
if (fieldType == null || fieldType.getTypeTag() == ATypeTag.DOUBLE) {
aDouble.setValue(val.getDoubleVal());
doubleSerde.serialize(aDouble, out);
} else if (fieldType.getTypeTag() == ATypeTag.INTEGER) {
aInt32.setValue((int) val.getDoubleVal());
int32Serde.serialize(aInt32, out);
} else if (fieldType.getTypeTag() == ATypeTag.BIGINT) {
aInt64.setValue((long) val.getDoubleVal());
int64Serde.serialize(aInt64, out);
} else if (fieldType.getTypeTag() == ATypeTag.SMALLINT) {
aInt16.setValue((short) val.getDoubleVal());
int16Serde.serialize(aInt16, out);
} else if (fieldType.getTypeTag() == ATypeTag.TINYINT) {
aInt8.setValue((byte) val.getDoubleVal());
int8Serde.serialize(aInt8, out);
} else if (fieldType.getTypeTag() == ATypeTag.FLOAT) {
aFloat.setValue((float) val.getDoubleVal());
floatSerde.serialize(aFloat, out);
}
} else if (checkType(ATypeTag.INTEGER, fieldType)) {
aInt32.setValue((int) val.getDoubleVal());
int32Serde.serialize(aInt32, out);
} else if (checkType(ATypeTag.BIGINT, fieldType)) {
aInt64.setValue((long) val.getDoubleVal());
int64Serde.serialize(aInt64, out);
} else if (checkType(ATypeTag.DATETIME, fieldType)) {
// Classad uses Linux Timestamps (s instead of ms)
aDateTime.setValue(val.getLongVal() * 1000);
datetimeSerde.serialize(aDateTime, out);
} else if (checkType(ATypeTag.DURATION, fieldType)) {
// Classad uses Linux Timestamps (s instead of ms)
aDuration.setValue(0, (long) (val.getDoubleVal() * 1000.0));
durationSerde.serialize(aDuration, out);
} else {
throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
}
break;
case RELATIVE_TIME_VALUE:
if (checkType(ATypeTag.DURATION, fieldType)) {
parseDuration(val, out);
} else {
throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
}
break;
default:
throw new HyracksDataException("unknown data type " + val.getValueType());
}
}
Aggregations