use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project sqlg by pietermartin.
the class SchemaTableTree method removeObsoleteHasContainers.
/**
* remove "has" containers that are not valid anymore
*
* @param schemaTableTree the current table tree
*/
private void removeObsoleteHasContainers(final SchemaTableTree schemaTableTree) {
Set<HasContainer> toRemove = new HashSet<>();
for (HasContainer hasContainer : schemaTableTree.hasContainers) {
if (hasContainer.getKey().equals(label.getAccessor())) {
toRemove.add(hasContainer);
}
if (Existence.NULL.equals(hasContainer.getBiPredicate())) {
// we checked that a non existing property was null, that's fine
if (!this.getFilteredAllTables().get(schemaTableTree.getSchemaTable().toString()).containsKey(hasContainer.getKey())) {
toRemove.add(hasContainer);
}
}
}
schemaTableTree.hasContainers.removeAll(toRemove);
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project sqlg by pietermartin.
the class SchemaTableTree method bulkWithJoin.
private String bulkWithJoin(SqlgGraph sqlgGraph) {
StringBuilder sb = new StringBuilder();
List<HasContainer> bulkHasContainers = this.hasContainers.stream().filter(h -> SqlgUtil.isBulkWithinAndOut(sqlgGraph, h)).collect(Collectors.toList());
for (HasContainer hasContainer : bulkHasContainers) {
P<List<Object>> predicate = (P<List<Object>>) hasContainer.getPredicate();
Collection<Object> withInList = predicate.getValue();
Set<Object> withInOuts = new HashSet<>(withInList);
Map<String, PropertyType> columns = new HashMap<>();
Object next = withInOuts.iterator().next();
if (next instanceof RecordId) {
next = ((RecordId) next).getId();
}
if (hasContainer.getBiPredicate() == Contains.within) {
columns.put(WITHIN, PropertyType.from(next));
} else if (hasContainer.getBiPredicate() == Contains.without) {
columns.put(WITHOUT, PropertyType.from(next));
} else {
throw new UnsupportedOperationException("Only Contains.within and Contains.without is supported!");
}
if (hasContainer.getBiPredicate() == Contains.within) {
sb.append(" INNER JOIN\n\t");
} else {
// left join and in the where clause add a IS NULL, to find the values not in the right hand table
sb.append(" LEFT JOIN\n\t");
}
sb.append("(VALUES ");
boolean first = true;
for (Object withInOutValue : withInOuts) {
if (!first) {
sb.append(", ");
}
first = false;
if (withInOutValue instanceof RecordId) {
withInOutValue = ((RecordId) withInOutValue).getId();
}
sb.append("(");
PropertyType propertyType = PropertyType.from(withInOutValue);
sb.append(sqlgGraph.getSqlDialect().valueToValuesString(propertyType, withInOutValue));
sb.append(")");
}
sb.append(") as tmp");
sb.append(this.rootSchemaTableTree().tmpTableAliasCounter);
sb.append("(");
if (hasContainer.getBiPredicate() == Contains.within) {
sb.append(WITHIN);
} else {
sb.append(WITHOUT);
}
sb.append(") ");
sb.append(" on ");
sb.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(this.getSchemaTable().getSchema()));
sb.append(".");
sb.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(this.getSchemaTable().getTable()));
sb.append(".");
if (hasContainer.getKey().equals(T.id.getAccessor())) {
sb.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes("ID"));
} else {
sb.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(hasContainer.getKey()));
}
if (hasContainer.getBiPredicate() == Contains.within) {
sb.append(" = tmp");
sb.append(this.rootSchemaTableTree().tmpTableAliasCounter++);
sb.append(".within");
} else {
sb.append(" = tmp");
sb.append(this.rootSchemaTableTree().tmpTableAliasCounter++);
sb.append(".without");
}
}
return sb.toString();
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project sqlg by pietermartin.
the class SchemaTableTree method toWhereClause.
private String toWhereClause(SqlgGraph sqlgGraph, MutableBoolean printedWhere) {
final StringBuilder result = new StringBuilder();
if (sqlgGraph.getSqlDialect().supportsBulkWithinOut()) {
for (HasContainer hasContainer : this.hasContainers) {
if (!SqlgUtil.isBulkWithin(sqlgGraph, hasContainer)) {
if (!printedWhere.booleanValue()) {
printedWhere.setTrue();
result.append("\nWHERE\n\t(");
} else {
result.append(" AND (");
}
WhereClause whereClause = WhereClause.from(hasContainer.getPredicate());
result.append(" ").append(whereClause.toSql(sqlgGraph, this, hasContainer)).append(")");
}
}
} else {
for (HasContainer hasContainer : this.getHasContainers()) {
if (!printedWhere.booleanValue()) {
printedWhere.setTrue();
result.append("\nWHERE\n\t(");
} else {
result.append(" AND (");
}
WhereClause whereClause = WhereClause.from(hasContainer.getPredicate());
result.append(" ").append(whereClause.toSql(sqlgGraph, this, hasContainer)).append(")");
if (sqlgGraph.getSqlDialect().isMariaDb()) {
result.append(" COLLATE latin1_general_cs");
}
}
}
for (AndOrHasContainer andOrHasContainer : this.andOrHasContainers) {
if (!printedWhere.booleanValue()) {
printedWhere.setTrue();
result.append("\nWHERE ");
} else {
result.append(" AND ");
}
andOrHasContainer.toSql(sqlgGraph, this, result);
}
return result.toString();
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project sqlg by pietermartin.
the class SqlgHasStepStrategy method apply.
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
// noinspection OptionalGetWithoutIsPresent
if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
return;
}
List<HasStep> hasSteps = TraversalHelper.getStepsOfAssignableClass(HasStep.class, traversal);
for (HasStep<?> hasStep : hasSteps) {
SqlgHasStep sqlgHasStep = new SqlgHasStep(hasStep.getTraversal(), hasStep.getHasContainers().toArray(new HasContainer[] {}));
for (String label : hasStep.getLabels()) {
sqlgHasStep.addLabel(label);
}
TraversalHelper.replaceStep(hasStep, sqlgHasStep, hasStep.getTraversal());
}
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer in project sqlg by pietermartin.
the class SqlgWhereStrategy method apply.
@SuppressWarnings("resource")
@Override
public void apply(Admin<?, ?> traversal) {
if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
return;
}
SqlgGraph sqlgGraph = (SqlgGraph) traversal.getGraph().get();
// The query will read from the cache if this is for a cached vertex
if (sqlgGraph.features().supportsBatchMode() && sqlgGraph.tx().isInNormalBatchMode()) {
sqlgGraph.tx().flush();
}
List<Step<?, ?>> steps = new ArrayList(traversal.asAdmin().getSteps());
ListIterator<Step<?, ?>> stepIterator = steps.listIterator();
// get all steps per label
Map<String, Object> stepsByLabel = new HashMap<>();
stepIterator = steps.listIterator();
Step<?, ?> previous = null;
int idx = 0;
while (stepIterator.hasNext()) {
Step<?, ?> step = stepIterator.next();
captureLabels(step, stepsByLabel);
if (step instanceof WherePredicateStep<?>) {
WherePredicateStep<?> wps = (WherePredicateStep<?>) step;
if (wps.getPredicate().isPresent() && wps.getPredicate().get().getBiPredicate() instanceof FullText) {
Object referTo = previous;
if (wps.getStartKey().isPresent()) {
referTo = stepsByLabel.get(wps.getStartKey().get());
}
if (referTo instanceof SqlgGraphStep<?, ?>) {
SqlgGraphStep<?, ?> sgs = (SqlgGraphStep<?, ?>) referTo;
if (sgs.getReplacedSteps().size() > 0) {
referTo = sgs.getReplacedSteps().get(sgs.getReplacedSteps().size() - 1);
}
}
if (referTo instanceof ReplacedStep<?, ?>) {
ReplacedStep<?, ?> rs = (ReplacedStep<?, ?>) referTo;
rs.addHasContainer(new HasContainer("__dummy__", wps.getPredicate().get()));
traversal.removeStep(idx);
}
}
}
previous = step;
idx++;
}
}
Aggregations