use of org.apache.asterix.om.types.ARecordType in project asterixdb by apache.
the class RecordAddFieldsTypeComputer 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());
ARecordType inputRecordType = TypeComputeUtils.extractRecordType(type0);
if (inputRecordType == null) {
throw new TypeMismatchException(funcName, 0, type0.getTypeTag(), ATypeTag.OBJECT);
}
ILogicalExpression arg1 = funcExpr.getArguments().get(1).getValue();
IAType type1 = (IAType) env.getType(arg1);
AOrderedListType inputOrderedListType = TypeComputeUtils.extractOrderedListType(type1);
if (inputOrderedListType == null) {
return inputRecordType;
}
boolean unknownable = TypeHelper.canBeUnknown(type0) || TypeHelper.canBeUnknown(type1);
Map<String, IAType> additionalFields = new HashMap<>();
List<String> resultFieldNames = new ArrayList<>();
List<IAType> resultFieldTypes = new ArrayList<>();
resultFieldNames.addAll(Arrays.asList(inputRecordType.getFieldNames()));
Collections.sort(resultFieldNames);
for (String fieldName : resultFieldNames) {
if (inputRecordType.getFieldType(fieldName).getTypeTag() == ATypeTag.OBJECT) {
ARecordType nestedType = (ARecordType) inputRecordType.getFieldType(fieldName);
//Deep Copy prevents altering of input types
resultFieldTypes.add(nestedType.deepCopy(nestedType));
} else {
resultFieldTypes.add(inputRecordType.getFieldType(fieldName));
}
}
if (!containsVariable(arg1)) {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) arg1;
List<Mutable<ILogicalExpression>> args = f.getArguments();
String fieldName = null;
IAType fieldType = null;
// Iterating through the orderlist input
for (Mutable<ILogicalExpression> arg : args) {
AbstractFunctionCallExpression recConsExpr = (AbstractFunctionCallExpression) arg.getValue();
ARecordType rtype = TypeComputeUtils.extractRecordType((IAType) env.getType(recConsExpr));
if (rtype != null) {
String[] fn = rtype.getFieldNames();
IAType[] ft = rtype.getFieldTypes();
for (int j = 0; j < fn.length; j++) {
if (fn[j].equals(FIELD_NAME_NAME)) {
ILogicalExpression fieldNameExpr = recConsExpr.getArguments().get(j).getValue();
if (ConstantExpressionUtil.getStringConstant(fieldNameExpr) == null) {
throw new InvalidExpressionException(funcName, 1, fieldNameExpr, LogicalExpressionTag.CONSTANT);
}
// Get the actual "field-name" string
fieldName = ConstantExpressionUtil.getStringArgument(recConsExpr, j + 1);
} else if (fn[j].equals(FIELD_VALUE_VALUE)) {
fieldType = ft[j];
}
}
if (fieldName != null) {
additionalFields.put(fieldName, fieldType);
}
}
}
if (!additionalFields.isEmpty()) {
Iterator<Map.Entry<String, IAType>> it = additionalFields.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, IAType> entry = it.next();
resultFieldNames.add(entry.getKey());
resultFieldTypes.add(entry.getValue());
}
}
}
// If variable ignore, deal with the addition at runtime
String resultTypeName = "appended(" + inputRecordType.getTypeName() + ")";
int n = resultFieldNames.size();
IAType resultType = new ARecordType(resultTypeName, resultFieldNames.toArray(new String[n]), resultFieldTypes.toArray(new IAType[n]), true);
if (unknownable) {
resultType = AUnionType.createUnknownableType(resultType);
}
return resultType;
}
use of org.apache.asterix.om.types.ARecordType in project asterixdb by apache.
the class FieldAccessNestedResultType method getResultType.
@Override
protected IAType getResultType(ILogicalExpression expr, IAType... strippedInputTypes) throws AlgebricksException {
IAType firstArgType = strippedInputTypes[0];
if (firstArgType.getTypeTag() != ATypeTag.OBJECT) {
return BuiltinType.ANY;
}
AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
ILogicalExpression arg1 = funcExpr.getArguments().get(1).getValue();
if (arg1.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
return BuiltinType.ANY;
}
ConstantExpression ce = (ConstantExpression) arg1;
IAObject v = ((AsterixConstantValue) ce.getValue()).getObject();
List<String> fieldPath = new ArrayList<>();
if (v.getType().getTypeTag() == ATypeTag.ARRAY) {
for (int i = 0; i < ((AOrderedList) v).size(); i++) {
fieldPath.add(((AString) ((AOrderedList) v).getItem(i)).getStringValue());
}
} else {
fieldPath.add(((AString) v).getStringValue());
}
ARecordType recType = (ARecordType) firstArgType;
IAType fieldType = recType.getSubFieldType(fieldPath);
return fieldType == null ? BuiltinType.ANY : fieldType;
}
use of org.apache.asterix.om.types.ARecordType in project asterixdb by apache.
the class RecordMergeTypeComputer method computeType.
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
String funcName = f.getFunctionIdentifier().getName();
IAType t0 = (IAType) env.getType(f.getArguments().get(0).getValue());
IAType t1 = (IAType) env.getType(f.getArguments().get(1).getValue());
boolean unknownable = TypeHelper.canBeUnknown(t0) || TypeHelper.canBeUnknown(t1);
ARecordType recType0 = TypeComputeUtils.extractRecordType(t0);
if (recType0 == null) {
throw new TypeMismatchException(funcName, 0, t0.getTypeTag(), ATypeTag.OBJECT);
}
ARecordType recType1 = TypeComputeUtils.extractRecordType(t1);
if (recType1 == null) {
throw new TypeMismatchException(funcName, 1, t1.getTypeTag(), ATypeTag.OBJECT);
}
List<String> resultFieldNames = new ArrayList<>();
for (String fieldName : recType0.getFieldNames()) {
resultFieldNames.add(fieldName);
}
Collections.sort(resultFieldNames);
List<IAType> resultFieldTypes = new ArrayList<>();
for (String fieldName : resultFieldNames) {
if (recType0.getFieldType(fieldName).getTypeTag() == ATypeTag.OBJECT) {
ARecordType nestedType = (ARecordType) recType0.getFieldType(fieldName);
//Deep Copy prevents altering of input types
resultFieldTypes.add(nestedType.deepCopy(nestedType));
} else {
resultFieldTypes.add(recType0.getFieldType(fieldName));
}
}
List<String> additionalFieldNames = new ArrayList<>();
List<IAType> additionalFieldTypes = new ArrayList<>();
String[] fieldNames = recType1.getFieldNames();
IAType[] fieldTypes = recType1.getFieldTypes();
for (int i = 0; i < fieldNames.length; ++i) {
int pos = Collections.binarySearch(resultFieldNames, fieldNames[i]);
if (pos >= 0) {
IAType resultFieldType = resultFieldTypes.get(pos);
if (resultFieldType.getTypeTag() != fieldTypes[i].getTypeTag()) {
throw new CompilationException(ErrorCode.COMPILATION_DUPLICATE_FIELD_NAME, fieldNames[i]);
}
// Assuming fieldTypes[i].getTypeTag() = resultFieldType.getTypeTag()
if (fieldTypes[i].getTypeTag() == ATypeTag.OBJECT) {
resultFieldTypes.set(pos, mergedNestedType(fieldNames[i], fieldTypes[i], resultFieldType));
}
} else {
additionalFieldNames.add(fieldNames[i]);
additionalFieldTypes.add(fieldTypes[i]);
}
}
resultFieldNames.addAll(additionalFieldNames);
resultFieldTypes.addAll(additionalFieldTypes);
String resultTypeName = "merged(" + recType0.getTypeName() + ", " + recType1.getTypeName() + ")";
boolean isOpen = recType0.isOpen() || recType1.isOpen();
IAType resultType = new ARecordType(resultTypeName, resultFieldNames.toArray(new String[] {}), resultFieldTypes.toArray(new IAType[] {}), isOpen);
if (unknownable) {
resultType = AUnionType.createUnknownableType(resultType);
}
return resultType;
}
use of org.apache.asterix.om.types.ARecordType in project asterixdb by apache.
the class RecordRemoveFieldsTypeComputer method addField.
private void addField(ARecordType inputRecordType, String fieldName, List<String> resultFieldNames, List<IAType> resultFieldTypes) throws AlgebricksException {
resultFieldNames.add(fieldName);
if (inputRecordType.getFieldType(fieldName).getTypeTag() == ATypeTag.OBJECT) {
ARecordType nestedType = (ARecordType) inputRecordType.getFieldType(fieldName);
//Deep Copy prevents altering of input types
resultFieldTypes.add(nestedType.deepCopy(nestedType));
} else {
resultFieldTypes.add(inputRecordType.getFieldType(fieldName));
}
}
use of org.apache.asterix.om.types.ARecordType in project asterixdb by apache.
the class AOrderedListSerializerDeserializerTest method test.
@Test
public void test() {
// Generates types.
ARecordType addrRecordType = SerializerDeserializerTestUtils.generateAddressRecordType();
ARecordType employeeType = SerializerDeserializerTestUtils.generateEmployeeRecordType(addrRecordType);
AOrderedListType employeeListType = new AOrderedListType(employeeType, "employee_list");
//Generates records.
ARecord[] records = SerializerDeserializerTestUtils.generateRecords(addrRecordType, employeeType);
// Generates lists
AOrderedList[] lists = new AOrderedList[4];
for (int index = 0; index < lists.length; ++index) {
lists[index] = new AOrderedList(employeeListType, Arrays.asList(records));
}
AOrderedListSerializerDeserializer serde = new AOrderedListSerializerDeserializer(employeeListType);
// Run four test threads to serialize/deserialize lists concurrently.
SerializerDeserializerTestUtils.concurrentSerDeTestRun(serde, lists);
}
Aggregations