private boolean hasTargetWithSortedRids(QueryPlanningInfo info) { if (info.target == null) { return false; } if (info.target.getItem() == null) { return false; } if (info.target.getItem().getIdentifier() != null) { return true; } else if (info.target.getItem().getCluster() != null) { return true; } else if (info.target.getItem().getClusterList() != null) { return true; } return false; }
public void validate() throws OCommandSQLParsingException { // for(OTraverseProjectionItem projection:projections) { // // projection. validate(); // if (projection.isExpand() && groupBy != null) { // throw new OCommandSQLParsingException("expand() cannot be used together with GROUP BY"); // } // // } if (target.getItem().getStatement() != null) { target.getItem().getStatement().validate(); } }
@Override protected OLuceneFullTextIndex searchForIndex(OFromClause target, OCommandContext ctx, OExpression... args) { OFromItem item = target.getItem(); String className = item.getIdentifier().getStringValue(); return searchForIndex(ctx, className); }
@Override protected OLuceneFullTextIndex searchForIndex(OFromClause target, OCommandContext ctx, OExpression... args) { OFromItem item = target.getItem(); OIdentifier identifier = item.getIdentifier(); return searchForIndex(identifier.getStringValue(), ctx, args); }
private OLuceneFullTextIndex searchForIndex(OFromClause target, OCommandContext ctx) { OFromItem item = target.getItem(); String className = item.getIdentifier().getStringValue(); return searchForIndex(ctx, className); }
private OResult createNewRecord(OFromClause commandTarget, OWhereClause initialFilter) { if (commandTarget.getItem().getIdentifier() == null) { throw new OCommandExecutionException("Cannot execute UPSERT on target '" + commandTarget + "'"); } ODocument doc = new ODocument(commandTarget.getItem().getIdentifier().getStringValue()); OUpdatableResult result = new OUpdatableResult(doc); if (initialFilter != null) { setContent(result, initialFilter); } return result; }
protected OLuceneSpatialIndex searchForIndex(OFromClause target, OExpression[] args) { OMetadata dbMetadata = getDb().getMetadata(); OFromItem item = target.getItem(); OIdentifier identifier = item.getIdentifier(); String fieldName = args[0].toString(); String className = identifier.getStringValue(); List<OLuceneSpatialIndex> indices = dbMetadata .getSchema() .getClass(className) .getIndexes() .stream() .filter(idx -> idx instanceof OLuceneSpatialIndex) .map(idx -> (OLuceneSpatialIndex) idx) .filter(idx -> intersect(idx.getDefinition().getFields(), Arrays.asList(fieldName))) .collect(Collectors.toList()); if (indices.size() > 1) { throw new IllegalArgumentException("too many indices matching given field name: " + String.join(",", fieldName)); } return indices.size() == 0 ? null : indices.get(0); }
@Override protected OLuceneFullTextIndex searchForIndex(OFromClause target, OCommandContext ctx, OExpression... args) { List<String> fieldNames = (List<String>) args[0].execute((OIdentifiable) null, ctx); OFromItem item = target.getItem(); String className = item.getIdentifier().getStringValue(); return searchForIndex(className, ctx, fieldNames); }
private void handleClassAsTarget(OSelectExecutionPlan plan, OFromClause queryTarget, OCommandContext ctx, boolean profilingEnabled) { OIdentifier identifier = queryTarget.getItem().getIdentifier(); Boolean orderByRidAsc = null;//null: no order. true: asc, false:desc FetchFromClassExecutionStep fetcher = new FetchFromClassExecutionStep(identifier.getStringValue(), null, ctx, orderByRidAsc, profilingEnabled); plan.chain(fetcher); }
private boolean handleHardwiredCountOnIndex(OSelectExecutionPlan result, QueryPlanningInfo info, OCommandContext ctx, boolean profilingEnabled) { OIndexIdentifier targetIndex = info.target == null ? null : info.target.getItem().getIndex(); if (targetIndex == null) { return false; } if (info.distinct || info.expand) { return false; } if (info.preAggregateProjection != null) { return false; } if (!isCountStar(info)) { return false; } if (!isMinimalQuery(info)) { return false; } result.chain(new CountFromIndexStep(targetIndex, info.projection.getAllAliases().iterator().next(), ctx, profilingEnabled)); return true; }
private boolean handleHardwiredCountOnClass(OSelectExecutionPlan result, QueryPlanningInfo info, OCommandContext ctx, boolean profilingEnabled) { OIdentifier targetClass = info.target == null ? null : info.target.getItem().getIdentifier(); if (targetClass == null) { return false; } if (info.distinct || info.expand) { return false; } if (info.preAggregateProjection != null) { return false; } if (!isCountStar(info)) { return false; } if (!isMinimalQuery(info)) { return false; } result.chain(new CountFromClassStep(targetClass, info.projection.getAllAliases().iterator().next(), ctx, profilingEnabled)); return true; }
private static void rewriteIndexChainsAsSubqueries(QueryPlanningInfo info, OCommandContext ctx) { if (ctx == null || ctx.getDatabase() == null) { return; } if (info.whereClause != null && info.target != null && info.target.getItem().getIdentifier() != null) { OClass clazz = ctx.getDatabase().getMetadata().getSchema().getClass(info.target.getItem().getIdentifier().getStringValue()); if (clazz != null) { info.whereClause.getBaseExpression().rewriteIndexChainsAsSubqueries(ctx, clazz); } } }
select.getTarget().setItem(new OFromItem(-1)); if (clazz != null) { select.getTarget().getItem().setIdentifier(new OIdentifier(clazz)); } else if (cluster != null) { select.getTarget().getItem().setCluster(new OCluster(cluster)); } else if (rid != null) { select.getTarget().getItem().setRids(Collections.singletonList(rid));
private void validateStatement(OSelectStatement statement) { if (statement.getProjection() != null) { if (statement.getProjection().getItems().size() > 1) { throw new OCommandExecutionException("Projections cannot be used in live query " + statement); } else if (statement.getProjection().getItems().get(0).isAll()) { throw new OCommandExecutionException("Projections cannot be used in live query " + statement); } } if (statement.getTarget().getItem().getIdentifier() == null && statement.getTarget().getItem().getRids() == null) { throw new OCommandExecutionException("Live queries can only be executed against a Class or on RIDs" + statement); } if (statement.getOrderBy() != null) { throw new OCommandExecutionException("Live queries do not support ORDER BY " + statement); } if (statement.getGroupBy() != null) { throw new OCommandExecutionException("Live queries do not support GROUP BY " + statement); } if (statement.getSkip() != null || statement.getLimit() != null) { throw new OCommandExecutionException("Live queries do not support SKIP/LIMIT " + statement); } }
protected OLuceneSpatialIndex searchForIndexOLd(OFromClause target, OExpression[] args) { // TODO Check if target is a class otherwise exception OFromItem item = target.getItem(); OIdentifier identifier = item.getIdentifier(); String fieldName = args[0].toString(); Set<OIndex<?>> indexes = getDb().getMetadata().getIndexManager() .getClassInvolvedIndexes(identifier.getStringValue(), fieldName); for (OIndex<?> index : indexes) { if (index.getInternal() instanceof OLuceneSpatialIndex) { return (OLuceneSpatialIndex) index; } } return null; }
public ODeleteExecutionPlan createExecutionPlan(OCommandContext ctx, boolean enableProfiling) { ODeleteExecutionPlan result = new ODeleteExecutionPlan(ctx); if (handleIndexAsTarget(result, fromClause.getItem().getIndex(), whereClause, ctx)) { if (limit != null) { throw new OCommandExecutionException("Cannot apply a LIMIT on a delete from index"); } if (returnBefore) { throw new OCommandExecutionException("Cannot apply a RETURN BEFORE on a delete from index"); } } else { handleTarget(result, ctx, this.fromClause, this.whereClause, enableProfiling); handleLimit(result, ctx, this.limit, enableProfiling); } handleCastToVertex(result, ctx, enableProfiling); handleDelete(result, ctx, enableProfiling); handleReturn(result, ctx, this.returnBefore, enableProfiling); return result; }
private void handleClassAsTarget(OSelectExecutionPlan plan, Set<String> filterClusters, OFromClause from, QueryPlanningInfo info, OCommandContext ctx, boolean profilingEnabled) { OIdentifier identifier = from.getItem().getIdentifier(); if (handleClassAsTargetWithIndexedFunction(plan, filterClusters, identifier, info, ctx, profilingEnabled)) { plan.chain(new FilterByClassStep(identifier, ctx, profilingEnabled)); return; } if (handleClassAsTargetWithIndex(plan, identifier, filterClusters, info, ctx, profilingEnabled)) { plan.chain(new FilterByClassStep(identifier, ctx, profilingEnabled)); return; } if (info.orderBy != null && handleClassWithIndexForSortOnly(plan, identifier, filterClusters, info, ctx, profilingEnabled)) { plan.chain(new FilterByClassStep(identifier, ctx, profilingEnabled)); return; } Boolean orderByRidAsc = null;//null: no order. true: asc, false:desc if (isOrderByRidAsc(info)) { orderByRidAsc = true; } else if (isOrderByRidDesc(info)) { orderByRidAsc = false; } FetchFromClassExecutionStep fetcher = new FetchFromClassExecutionStep(identifier.getStringValue(), filterClusters, info, ctx, orderByRidAsc, profilingEnabled); if (orderByRidAsc != null && info.serverToClusters.size() == 1) { info.orderApplied = true; } plan.chain(fetcher); }
public ODeleteExecutionPlan createExecutionPlan(OCommandContext ctx, boolean enableProfiling) { ODeleteExecutionPlan result = new ODeleteExecutionPlan(ctx); if (handleIndexAsTarget(result, fromClause.getItem().getIndex(), whereClause, ctx,enableProfiling)) { if (limit != null) { throw new OCommandExecutionException("Cannot apply a LIMIT on a delete from index"); } if (unsafe) { throw new OCommandExecutionException("Cannot apply a UNSAFE on a delete from index"); } if (returnBefore) { throw new OCommandExecutionException("Cannot apply a RETURN BEFORE on a delete from index"); } handleReturn(result, ctx, this.returnBefore, enableProfiling); } else { handleTarget(result, ctx, this.fromClause, this.whereClause, enableProfiling); handleUnsafe(result, ctx, this.unsafe, enableProfiling); handleLimit(result, ctx, this.limit, enableProfiling); handleDelete(result, ctx, enableProfiling); handleReturn(result, ctx, this.returnBefore, enableProfiling); } return result; }
private void handleFetchFromTarger(OSelectExecutionPlan result, OCommandContext ctx, boolean profilingEnabled) { OFromItem target = this.target == null ? null : this.target.getItem(); if (target == null) { handleNoTarget(result, ctx, profilingEnabled); } else if (target.getIdentifier() != null) { handleClassAsTarget(result, this.target, ctx, profilingEnabled); } else if (target.getCluster() != null) { handleClustersAsTarget(result, Collections.singletonList(target.getCluster()), ctx, profilingEnabled); } else if (target.getClusterList() != null) { handleClustersAsTarget(result, target.getClusterList().toListOfClusters(), ctx, profilingEnabled); } else if (target.getStatement() != null) { handleSubqueryAsTarget(result, target.getStatement(), ctx, profilingEnabled); } else if (target.getFunctionCall() != null) { // handleFunctionCallAsTarget(result, target.getFunctionCall(), ctx);//TODO throw new OCommandExecutionException("function call as target is not supported yet"); } else if (target.getInputParam() != null) { handleInputParamAsTarget(result, target.getInputParam(), ctx, profilingEnabled); } else if (target.getIndex() != null) { handleIndexAsTarget(result, target.getIndex(), ctx, profilingEnabled); } else if (target.getMetadata() != null) { handleMetadataAsTarget(result, target.getMetadata(), ctx, profilingEnabled); } else if (target.getRids() != null && target.getRids().size() > 0) { handleRidsAsTarget(result, target.getRids(), ctx, profilingEnabled); } else { throw new UnsupportedOperationException(); } }
private OSelectStatement indexChainToStatement(OModifier modifier, OClass clazz, OExpression right, OCommandContext ctx) { OClass queryClass = clazz; OSelectStatement result = new OSelectStatement(-1); result.target = new OFromClause(-1); result.target.setItem(new OFromItem(-1)); result.target.getItem().identifier = new OIdentifier(queryClass.getName()); result.whereClause = new OWhereClause(-1); OBinaryCondition base = new OBinaryCondition(-1); result.whereClause.baseExpression = new ONotBlock(-1); ((ONotBlock) result.whereClause.baseExpression).sub = base; ((ONotBlock) result.whereClause.baseExpression).negate = false; base.left = new OExpression(-1); base.left.mathExpression = new OBaseExpression(-1); ((OBaseExpression) base.left.mathExpression).identifier = new OBaseIdentifier(modifier.suffix.identifier); ((OBaseExpression) base.left.mathExpression).modifier = modifier.next == null ? null : modifier.next.copy(); base.operator = new OEqualsCompareOperator(-1); base.right = right.copy(); return result; } }