use of org.apache.asterix.om.exceptions.InvalidExpressionException in project asterixdb by apache.
the class RecordRemoveFieldsTypeComputer method getPathFromConstantExpression.
private void getPathFromConstantExpression(String funcName, ILogicalExpression expression, Set<String> fieldNameSet, List<List<String>> pathList) throws AlgebricksException {
ConstantExpression ce = (ConstantExpression) expression;
if (!(ce.getValue() instanceof AsterixConstantValue)) {
throw new InvalidExpressionException(funcName, 1, ce, LogicalExpressionTag.CONSTANT);
}
IAObject item = ((AsterixConstantValue) ce.getValue()).getObject();
ATypeTag type = item.getType().getTypeTag();
switch(type) {
case STRING:
String fn = ((AString) item).getStringValue();
fieldNameSet.add(fn);
break;
case ARRAY:
AOrderedList pathOrdereList = (AOrderedList) item;
String fieldName = ((AString) pathOrdereList.getItem(0)).getStringValue();
fieldNameSet.add(fieldName);
List<String> path = new ArrayList<>();
for (int i = 0; i < pathOrdereList.size(); i++) {
path.add(((AString) pathOrdereList.getItem(i)).getStringValue());
}
pathList.add(path);
break;
default:
throw new UnsupportedTypeException(funcName, type);
}
}
use of org.apache.asterix.om.exceptions.InvalidExpressionException in project asterixdb by apache.
the class ClosedRecordConstructorResultType method computeType.
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
String funcName = f.getFunctionIdentifier().getName();
/**
* if type has been top-down propagated, use the enforced type
*/
ARecordType type = (ARecordType) TypeCastUtils.getRequiredType(f);
if (type != null) {
return type;
}
int n = f.getArguments().size() / 2;
String[] fieldNames = new String[n];
IAType[] fieldTypes = new IAType[n];
int i = 0;
Iterator<Mutable<ILogicalExpression>> argIter = f.getArguments().iterator();
while (argIter.hasNext()) {
ILogicalExpression e1 = argIter.next().getValue();
ILogicalExpression e2 = argIter.next().getValue();
IAType e2Type = (IAType) env.getType(e2);
if (e2Type.getTypeTag() == ATypeTag.UNION) {
AUnionType unionType = (AUnionType) e2Type;
e2Type = AUnionType.createUnknownableType(unionType.getActualType());
}
fieldTypes[i] = e2Type;
fieldNames[i] = ConstantExpressionUtil.getStringConstant(e1);
if (fieldNames[i] == null) {
throw new InvalidExpressionException(funcName, 2 * i, e1, LogicalExpressionTag.CONSTANT);
}
i++;
}
return new ARecordType(null, fieldNames, fieldTypes, false);
}
use of org.apache.asterix.om.exceptions.InvalidExpressionException 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.exceptions.InvalidExpressionException in project asterixdb by apache.
the class RecordRemoveFieldsTypeComputer method getListFromExpression.
private List<String> getListFromExpression(String funcName, ILogicalExpression expression) throws AlgebricksException {
AbstractFunctionCallExpression funcExp = (AbstractFunctionCallExpression) expression;
List<Mutable<ILogicalExpression>> args = funcExp.getArguments();
List<String> list = new ArrayList<>();
for (Mutable<ILogicalExpression> arg : args) {
// At this point all elements has to be a constant
// Input list has only one level of nesting (list of list or list of strings)
ConstantExpression ce = (ConstantExpression) arg.getValue();
if (!(ce.getValue() instanceof AsterixConstantValue)) {
throw new InvalidExpressionException(funcName, 1, ce, LogicalExpressionTag.CONSTANT);
}
IAObject item = ((AsterixConstantValue) ce.getValue()).getObject();
ATypeTag type = item.getType().getTypeTag();
if (type == ATypeTag.STRING) {
list.add(((AString) item).getStringValue());
} else {
throw new UnsupportedTypeException(funcName, type);
}
}
return list;
}
use of org.apache.asterix.om.exceptions.InvalidExpressionException in project asterixdb by apache.
the class RecordRemoveFieldsTypeComputer method computeTypeFromNonConstantExpression.
private void computeTypeFromNonConstantExpression(String funcName, ILogicalExpression expression, Set<String> fieldNameSet, List<List<String>> pathList) throws AlgebricksException {
AbstractFunctionCallExpression funcExp = (AbstractFunctionCallExpression) expression;
List<Mutable<ILogicalExpression>> args = funcExp.getArguments();
for (Mutable<ILogicalExpression> arg : args) {
ILogicalExpression le = arg.getValue();
switch(le.getExpressionTag()) {
case CONSTANT:
getPathFromConstantExpression(funcName, le, fieldNameSet, pathList);
break;
case FUNCTION_CALL:
getPathFromFunctionExpression(funcName, le, fieldNameSet, pathList);
break;
default:
throw new InvalidExpressionException(funcName, 1, le, LogicalExpressionTag.CONSTANT, LogicalExpressionTag.FUNCTION_CALL);
}
}
}
Aggregations