use of org.apache.asterix.metadata.declared.DataSource in project asterixdb by apache.
the class IntroduceLSMComponentFilterRule method getDataset.
private Dataset getDataset(AbstractLogicalOperator op, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator descendantOp = (AbstractLogicalOperator) op.getInputs().get(0).getValue();
while (descendantOp != null) {
if (descendantOp.getOperatorTag() == LogicalOperatorTag.DATASOURCESCAN) {
DataSourceScanOperator dataSourceScanOp = (DataSourceScanOperator) descendantOp;
DataSource ds = (DataSource) dataSourceScanOp.getDataSource();
if (ds.getDatasourceType() != DataSource.Type.INTERNAL_DATASET) {
return null;
}
return ((DatasetDataSource) ds).getDataset();
} else if (descendantOp.getOperatorTag() == LogicalOperatorTag.UNNEST_MAP) {
UnnestMapOperator unnestMapOp = (UnnestMapOperator) descendantOp;
ILogicalExpression unnestExpr = unnestMapOp.getExpressionRef().getValue();
if (unnestExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) unnestExpr;
FunctionIdentifier fid = f.getFunctionIdentifier();
String dataverseName;
String datasetName;
if (BuiltinFunctions.EXTERNAL_LOOKUP.equals(fid)) {
dataverseName = AccessMethodUtils.getStringConstant(f.getArguments().get(0));
datasetName = AccessMethodUtils.getStringConstant(f.getArguments().get(1));
} else if (fid.equals(BuiltinFunctions.INDEX_SEARCH)) {
AccessMethodJobGenParams jobGenParams = new AccessMethodJobGenParams();
jobGenParams.readFromFuncArgs(f.getArguments());
dataverseName = jobGenParams.dataverseName;
datasetName = jobGenParams.datasetName;
} else {
throw new AlgebricksException("Unexpected function for Unnest Map: " + fid);
}
return ((MetadataProvider) context.getMetadataProvider()).findDataset(dataverseName, datasetName);
}
}
if (descendantOp.getInputs().isEmpty()) {
break;
}
descendantOp = (AbstractLogicalOperator) descendantOp.getInputs().get(0).getValue();
}
return null;
}
use of org.apache.asterix.metadata.declared.DataSource in project asterixdb by apache.
the class IntroduceStaticTypeCastForInsertRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
/**
* pattern match: sink/insert/assign record type is propagated from
* insert data source to the record-constructor expression
*/
if (context.checkIfInDontApplySet(this, opRef.getValue())) {
return false;
}
context.addToDontApplySet(this, opRef.getValue());
AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
List<LogicalVariable> producedVariables = new ArrayList<LogicalVariable>();
LogicalVariable oldRecordVariable;
if (op1.getOperatorTag() != LogicalOperatorTag.DELEGATE_OPERATOR && op1.getOperatorTag() != LogicalOperatorTag.SINK) {
return false;
}
if (op1.getOperatorTag() == LogicalOperatorTag.DELEGATE_OPERATOR) {
DelegateOperator eOp = (DelegateOperator) op1;
if (!(eOp.getDelegate() instanceof CommitOperator)) {
return false;
}
}
AbstractLogicalOperator op2 = (AbstractLogicalOperator) op1.getInputs().get(0).getValue();
if (op2.getOperatorTag() != LogicalOperatorTag.INSERT_DELETE_UPSERT) {
return false;
}
InsertDeleteUpsertOperator insertDeleteOp = (InsertDeleteUpsertOperator) op2;
if (insertDeleteOp.getOperation() == InsertDeleteUpsertOperator.Kind.DELETE) {
return false;
}
/**
* get required record type
*/
InsertDeleteUpsertOperator insertDeleteOperator = (InsertDeleteUpsertOperator) op2;
DataSource dataSource = (DataSource) insertDeleteOperator.getDataSource();
IAType requiredRecordType = dataSource.getItemType();
List<LogicalVariable> usedVariables = new ArrayList<LogicalVariable>();
insertDeleteOperator.getPayloadExpression().getValue().getUsedVariables(usedVariables);
// empty
if (usedVariables.size() == 0) {
return false;
}
oldRecordVariable = usedVariables.get(0);
LogicalVariable inputRecordVar = usedVariables.get(0);
IVariableTypeEnvironment env = insertDeleteOperator.computeOutputTypeEnvironment(context);
IAType inputRecordType = (IAType) env.getVarType(inputRecordVar);
AbstractLogicalOperator currentOperator = (AbstractLogicalOperator) op2.getInputs().get(0).getValue();
/**
* find the assign operator for the "input record" to the insert_delete
* operator
*/
do {
context.addToDontApplySet(this, currentOperator);
if (currentOperator.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
AssignOperator assignOp = (AssignOperator) currentOperator;
producedVariables.clear();
VariableUtilities.getProducedVariables(currentOperator, producedVariables);
int position = producedVariables.indexOf(oldRecordVariable);
/**
* set the top-down propagated type
*/
if (position >= 0) {
AssignOperator originalAssign = (AssignOperator) currentOperator;
List<Mutable<ILogicalExpression>> expressionRefs = originalAssign.getExpressions();
ILogicalExpression expr = expressionRefs.get(position).getValue();
if (expr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
// fail but just return false
if (TypeCastUtils.getRequiredType(funcExpr) != null) {
context.computeAndSetTypeEnvironmentForOperator(assignOp);
return false;
}
IVariableTypeEnvironment assignEnv = assignOp.computeOutputTypeEnvironment(context);
StaticTypeCastUtil.rewriteFuncExpr(funcExpr, requiredRecordType, inputRecordType, assignEnv);
}
context.computeAndSetTypeEnvironmentForOperator(originalAssign);
}
}
if (currentOperator.getInputs().size() > 0) {
currentOperator = (AbstractLogicalOperator) currentOperator.getInputs().get(0).getValue();
} else {
break;
}
} while (currentOperator != null);
return true;
}
use of org.apache.asterix.metadata.declared.DataSource in project asterixdb by apache.
the class IntroduceRandomPartitioningFeedComputationRule method rewritePre.
@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
ILogicalOperator op = opRef.getValue();
if (!op.getOperatorTag().equals(LogicalOperatorTag.ASSIGN)) {
return false;
}
ILogicalOperator opChild = op.getInputs().get(0).getValue();
if (!opChild.getOperatorTag().equals(LogicalOperatorTag.DATASOURCESCAN)) {
return false;
}
DataSourceScanOperator scanOp = (DataSourceScanOperator) opChild;
DataSource dataSource = (DataSource) scanOp.getDataSource();
if (dataSource.getDatasourceType() != DataSource.Type.FEED) {
return false;
}
final FeedDataSource feedDataSource = (FeedDataSource) dataSource;
FeedConnection feedConnection = feedDataSource.getFeedConnection();
if (feedConnection.getAppliedFunctions() == null || feedConnection.getAppliedFunctions().size() == 0) {
return false;
}
ExchangeOperator exchangeOp = new ExchangeOperator();
INodeDomain domain = new INodeDomain() {
@Override
public boolean sameAs(INodeDomain domain) {
return domain == this;
}
@Override
public Integer cardinality() {
return feedDataSource.getComputeCardinality();
}
};
exchangeOp.setPhysicalOperator(new RandomPartitionExchangePOperator(domain));
op.getInputs().get(0).setValue(exchangeOp);
exchangeOp.getInputs().add(new MutableObject<ILogicalOperator>(scanOp));
ExecutionMode em = ((AbstractLogicalOperator) scanOp).getExecutionMode();
exchangeOp.setExecutionMode(em);
exchangeOp.computeDeliveredPhysicalProperties(context);
context.computeAndSetTypeEnvironmentForOperator(exchangeOp);
AssignOperator assignOp = (AssignOperator) opRef.getValue();
AssignPOperator assignPhyOp = (AssignPOperator) assignOp.getPhysicalOperator();
assignPhyOp.setCardinalityConstraint(domain.cardinality());
return true;
}
use of org.apache.asterix.metadata.declared.DataSource in project asterixdb by apache.
the class MetaKeyExpressionReferenceTransform method visit.
private ILogicalExpressionReferenceTransformWithCondition visit(Mutable<ILogicalOperator> opRef) throws AlgebricksException {
ILogicalOperator op = opRef.getValue();
// Reaches NTS or ETS.
if (op.getInputs().size() == 0) {
return NoOpExpressionReferenceTransform.INSTANCE;
}
// Datascan returns an useful transform if the meta part presents in the dataset.
if (op.getOperatorTag() == LogicalOperatorTag.DATASOURCESCAN) {
DataSourceScanOperator scanOp = (DataSourceScanOperator) op;
ILogicalExpressionReferenceTransformWithCondition inputTransfomer = visit(op.getInputs().get(0));
DataSource dataSource = (DataSource) scanOp.getDataSource();
List<ILogicalExpressionReferenceTransformWithCondition> transformers = null;
List<LogicalVariable> allVars = scanOp.getVariables();
LogicalVariable dataVar = dataSource.getDataRecordVariable(allVars);
LogicalVariable metaVar = dataSource.getMetaVariable(allVars);
LogicalExpressionReferenceTransform currentTransformer = null;
// https://issues.apache.org/jira/browse/ASTERIXDB-1618
if (dataSource.getDatasourceType() != DataSource.Type.EXTERNAL_DATASET && dataSource.getDatasourceType() != DataSource.Type.INTERNAL_DATASET && dataSource.getDatasourceType() != DataSource.Type.LOADABLE) {
IMutationDataSource mds = (IMutationDataSource) dataSource;
if (mds.isChange()) {
transformers = new ArrayList<>();
transformers.add(new MetaKeyExpressionReferenceTransform(mds.getPkVars(allVars), mds.getKeyAccessExpression()));
} else if (metaVar != null) {
transformers = new ArrayList<>();
transformers.add(new MetaKeyToFieldAccessTransform(metaVar));
}
}
if (!dataSource.hasMeta() && transformers == null) {
return inputTransfomer;
}
if (metaVar != null) {
currentTransformer = new LogicalExpressionReferenceTransform(dataVar, metaVar);
}
if (inputTransfomer.equals(NoOpExpressionReferenceTransform.INSTANCE) && transformers == null) {
return currentTransformer;
} else if (inputTransfomer.equals(NoOpExpressionReferenceTransform.INSTANCE) && currentTransformer == null) {
return transformers.get(0);
} else {
// Requires an argument variable to resolve ambiguity.
if (transformers == null) {
transformers = new ArrayList<>();
}
if (!inputTransfomer.equals(NoOpExpressionReferenceTransform.INSTANCE)) {
inputTransfomer.setVariableRequired();
transformers.add(inputTransfomer);
}
currentTransformer.setVariableRequired();
transformers.add(currentTransformer);
return new CompositeExpressionReferenceTransform(transformers);
}
}
// Visits children in the depth-first order.
List<ILogicalExpressionReferenceTransformWithCondition> transformers = new ArrayList<>();
for (Mutable<ILogicalOperator> childRef : op.getInputs()) {
ILogicalExpressionReferenceTransformWithCondition transformer = visit(childRef);
if (!transformer.equals(NoOpExpressionReferenceTransform.INSTANCE)) {
transformers.add(transformer);
}
}
ILogicalExpressionReferenceTransformWithCondition currentTransformer = null;
if (transformers.size() == 0) {
currentTransformer = NoOpExpressionReferenceTransform.INSTANCE;
} else if (transformers.size() == 1) {
currentTransformer = transformers.get(0);
} else {
// Transformers in a CompositeTransformer should require argument variable check.
for (ILogicalExpressionReferenceTransformWithCondition transformer : transformers) {
transformer.setVariableRequired();
}
currentTransformer = new CompositeExpressionReferenceTransform(transformers);
}
rewritten |= op.acceptExpressionTransform(currentTransformer);
return currentTransformer;
}
use of org.apache.asterix.metadata.declared.DataSource in project asterixdb by apache.
the class OptimizableOperatorSubTree method setDatasetAndTypeMetadata.
/**
* Find the dataset corresponding to the datasource scan in the metadata.
* Also sets recordType to be the type of that dataset.
*/
public boolean setDatasetAndTypeMetadata(MetadataProvider metadataProvider) throws AlgebricksException {
String dataverseName = null;
String datasetName = null;
Dataset ds = null;
ARecordType rType = null;
List<Mutable<ILogicalOperator>> sourceOpRefs = new ArrayList<>();
List<DataSourceType> dsTypes = new ArrayList<>();
sourceOpRefs.add(getDataSourceRef());
dsTypes.add(getDataSourceType());
// If there are multiple datasources in the subtree, we need to find the dataset for these.
if (getIxJoinOuterAdditionalDataSourceRefs() != null) {
for (int i = 0; i < getIxJoinOuterAdditionalDataSourceRefs().size(); i++) {
sourceOpRefs.add(getIxJoinOuterAdditionalDataSourceRefs().get(i));
dsTypes.add(getIxJoinOuterAdditionalDataSourceTypes().get(i));
}
}
for (int i = 0; i < sourceOpRefs.size(); i++) {
switch(dsTypes.get(i)) {
case DATASOURCE_SCAN:
DataSourceScanOperator dataSourceScan = (DataSourceScanOperator) sourceOpRefs.get(i).getValue();
IDataSource<?> datasource = dataSourceScan.getDataSource();
if (datasource instanceof DataSource) {
byte dsType = ((DataSource) datasource).getDatasourceType();
if (dsType != DataSource.Type.INTERNAL_DATASET && dsType != DataSource.Type.EXTERNAL_DATASET) {
return false;
}
}
Pair<String, String> datasetInfo = AnalysisUtil.getDatasetInfo(dataSourceScan);
dataverseName = datasetInfo.first;
datasetName = datasetInfo.second;
break;
case PRIMARY_INDEX_LOOKUP:
AbstractUnnestOperator unnestMapOp = (AbstractUnnestOperator) sourceOpRefs.get(i).getValue();
ILogicalExpression unnestExpr = unnestMapOp.getExpressionRef().getValue();
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) unnestExpr;
AccessMethodJobGenParams jobGenParams = new AccessMethodJobGenParams();
jobGenParams.readFromFuncArgs(f.getArguments());
datasetName = jobGenParams.getDatasetName();
dataverseName = jobGenParams.getDataverseName();
break;
case EXTERNAL_SCAN:
UnnestMapOperator externalScan = (UnnestMapOperator) sourceOpRefs.get(i).getValue();
datasetInfo = AnalysisUtil.getExternalDatasetInfo(externalScan);
dataverseName = datasetInfo.first;
datasetName = datasetInfo.second;
break;
case COLLECTION_SCAN:
if (i != 0) {
getIxJoinOuterAdditionalDatasets().add(null);
getIxJoinOuterAdditionalRecordTypes().add(null);
}
continue;
case NO_DATASOURCE:
default:
return false;
}
if (dataverseName == null || datasetName == null) {
return false;
}
// Find the dataset corresponding to the datasource in the metadata.
ds = metadataProvider.findDataset(dataverseName, datasetName);
if (ds == null) {
throw CompilationException.create(ErrorCode.NO_METADATA_FOR_DATASET, datasetName);
}
// Get the record type for that dataset.
IAType itemType = metadataProvider.findType(ds.getItemTypeDataverseName(), ds.getItemTypeName());
if (itemType.getTypeTag() != ATypeTag.OBJECT) {
if (i == 0) {
return false;
} else {
getIxJoinOuterAdditionalDatasets().add(null);
getIxJoinOuterAdditionalRecordTypes().add(null);
}
}
rType = (ARecordType) itemType;
// Get the meta record type for that dataset.
IAType metaItemType = metadataProvider.findType(ds.getMetaItemTypeDataverseName(), ds.getMetaItemTypeName());
// First index is always the primary datasource in this subtree.
if (i == 0) {
setDataset(ds);
setRecordType(rType);
setMetaRecordType((ARecordType) metaItemType);
} else {
getIxJoinOuterAdditionalDatasets().add(ds);
getIxJoinOuterAdditionalRecordTypes().add(rType);
}
dataverseName = null;
datasetName = null;
ds = null;
rType = null;
}
return true;
}
Aggregations