use of org.apache.asterix.om.types.ARecordType in project asterixdb by apache.
the class LoadRecordFieldsRule method findFieldByIndexFromRecordConstructor.
// Resolves field expression by index-based access.
private static ILogicalExpression findFieldByIndexFromRecordConstructor(Object index, AbstractFunctionCallExpression fce, IVariableTypeEnvironment typeEnvironment) throws AlgebricksException {
Integer fieldIndex = (Integer) index;
ARecordType recordType = (ARecordType) typeEnvironment.getType(fce);
String[] closedFieldNames = recordType.getFieldNames();
return closedFieldNames.length > fieldIndex ? findFieldByNameFromRecordConstructor(closedFieldNames[fieldIndex], fce) : null;
}
use of org.apache.asterix.om.types.ARecordType in project asterixdb by apache.
the class PushFieldAccessRule method isAccessToIndexedField.
@SuppressWarnings("unchecked")
private boolean isAccessToIndexedField(AssignOperator assign, IOptimizationContext context) throws AlgebricksException {
AbstractFunctionCallExpression accessFun = (AbstractFunctionCallExpression) assign.getExpressions().get(0).getValue();
ILogicalExpression e0 = accessFun.getArguments().get(0).getValue();
if (e0.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
return false;
}
LogicalVariable var = ((VariableReferenceExpression) e0).getVariableReference();
if (context.findPrimaryKey(var) == null) {
// not referring to a dataset record
return false;
}
AbstractLogicalOperator op = assign;
while (op.getInputs().size() == 1 && op.getOperatorTag() != LogicalOperatorTag.DATASOURCESCAN) {
op = (AbstractLogicalOperator) op.getInputs().get(0).getValue();
}
if (op.getOperatorTag() != LogicalOperatorTag.DATASOURCESCAN) {
return false;
}
DataSourceScanOperator scan = (DataSourceScanOperator) op;
LogicalVariable recVar = scan.getVariables().get(scan.getVariables().size() - 1);
if (recVar != var) {
return false;
}
MetadataProvider mp = (MetadataProvider) context.getMetadataProvider();
DataSourceId asid = ((IDataSource<DataSourceId>) scan.getDataSource()).getId();
Dataset dataset = mp.findDataset(asid.getDataverseName(), asid.getDatasourceName());
if (dataset == null) {
throw new AlgebricksException("Dataset " + asid.getDatasourceName() + " not found.");
}
if (dataset.getDatasetType() != DatasetType.INTERNAL) {
return false;
}
final Integer pos = ConstantExpressionUtil.getIntConstant(accessFun.getArguments().get(1).getValue());
if (pos != null) {
String tName = dataset.getItemTypeName();
IAType t = mp.findType(dataset.getItemTypeDataverseName(), tName);
if (t.getTypeTag() != ATypeTag.OBJECT) {
return false;
}
ARecordType rt = (ARecordType) t;
if (pos >= rt.getFieldNames().length) {
return false;
}
}
List<Index> datasetIndexes = mp.getDatasetIndexes(dataset.getDataverseName(), dataset.getDatasetName());
boolean hasSecondaryIndex = false;
for (Index index : datasetIndexes) {
if (index.isSecondaryIndex()) {
hasSecondaryIndex = true;
break;
}
}
return hasSecondaryIndex;
}
use of org.apache.asterix.om.types.ARecordType in project asterixdb by apache.
the class IntroduceDynamicTypeCastRule method compatible.
/**
* Check whether the required record type and the input type is compatible
*
* @param reqType
* @param inputType
* @return true if compatible; false otherwise
* @throws AlgebricksException
*/
public static boolean compatible(ARecordType reqType, IAType inputType) throws AlgebricksException {
if (inputType.getTypeTag() == ATypeTag.ANY) {
return false;
}
if (inputType.getTypeTag() != ATypeTag.OBJECT) {
throw new AlgebricksException("The input type " + inputType + " is not a valid record type!");
}
ARecordType inputRecType = (ARecordType) inputType;
if (reqType.isOpen() != inputRecType.isOpen()) {
return false;
}
IAType[] reqTypes = reqType.getFieldTypes();
String[] reqFieldNames = reqType.getFieldNames();
IAType[] inputTypes = inputRecType.getFieldTypes();
String[] inputFieldNames = ((ARecordType) inputType).getFieldNames();
if (reqTypes.length != inputTypes.length) {
return false;
}
for (int i = 0; i < reqTypes.length; i++) {
if (!reqFieldNames[i].equals(inputFieldNames[i])) {
return false;
}
IAType reqTypeInside = reqTypes[i];
if (NonTaggedFormatUtil.isOptional(reqTypes[i])) {
reqTypeInside = ((AUnionType) reqTypes[i]).getActualType();
}
IAType inputTypeInside = inputTypes[i];
if (NonTaggedFormatUtil.isOptional(inputTypes[i])) {
if (!NonTaggedFormatUtil.isOptional(reqTypes[i])) {
/** if the required type is not optional, the two types are incompatible */
return false;
}
inputTypeInside = ((AUnionType) inputTypes[i]).getActualType();
}
if (inputTypeInside.getTypeTag() != ATypeTag.MISSING && !reqTypeInside.equals(inputTypeInside)) {
return false;
}
}
return true;
}
use of org.apache.asterix.om.types.ARecordType in project asterixdb by apache.
the class AccessMethodUtils method appendPrimaryIndexTypes.
public static void appendPrimaryIndexTypes(Dataset dataset, IAType itemType, IAType metaItemType, List<Object> target) throws AlgebricksException {
ARecordType recordType = (ARecordType) itemType;
ARecordType metaRecordType = (ARecordType) metaItemType;
target.addAll(KeyFieldTypeUtil.getPartitoningKeyTypes(dataset, recordType, metaRecordType));
// Adds data record type.
target.add(itemType);
// Adds meta record type if any.
if (dataset.hasMetaPart()) {
target.add(metaItemType);
}
}
use of org.apache.asterix.om.types.ARecordType in project asterixdb by apache.
the class ResolveVariableRule method findCandidatePathsForExpr.
// Recursively finds candidate paths under an expression.
private Set<Pair<ILogicalExpression, List<String>>> findCandidatePathsForExpr(String unresolvedVarName, IAType pathType, ILogicalExpression expr, List<String> parentPath) throws AlgebricksException {
Set<Pair<ILogicalExpression, List<String>>> varAccessCandidates = new HashSet<>();
IAType type = pathType;
if (type.getTypeTag() == ATypeTag.UNION) {
type = ((AUnionType) type).getActualType();
}
ATypeTag tag = type.getTypeTag();
if (tag == ATypeTag.ANY) {
List<String> path = new ArrayList<>(parentPath);
path.add(unresolvedVarName);
varAccessCandidates.add(new Pair<>(expr, path));
}
if (tag == ATypeTag.OBJECT) {
ARecordType recordType = (ARecordType) type;
if (recordType.canContainField(unresolvedVarName)) {
// If the field name is possible.
List<String> path = new ArrayList<>(parentPath);
path.add(unresolvedVarName);
varAccessCandidates.add(new Pair<>(expr, path));
} else {
// Recursively identified possible paths.
String[] fieldNames = recordType.getFieldNames();
IAType[] fieldTypes = recordType.getFieldTypes();
for (int index = 0; index < fieldNames.length; ++index) {
List<String> path = new ArrayList<>(parentPath);
path.add(fieldNames[index]);
varAccessCandidates.addAll(findCandidatePathsForExpr(unresolvedVarName, fieldTypes[index], expr, path));
}
}
}
return varAccessCandidates;
}
Aggregations