Skip to content

Commit

Permalink
fix #344
Browse files Browse the repository at this point in the history
Correct incorrect logic for generating where clauses.
  • Loading branch information
pieter committed Mar 28, 2019
1 parent d468290 commit 3a4c3ac
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 40 deletions.
69 changes: 39 additions & 30 deletions sqlg-core/src/main/java/org/umlg/sqlg/sql/parse/WhereClause.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ String toSql(SqlgGraph sqlgGraph, SchemaTableTree schemaTableTree, HasContainer
prefix += ".";
prefix += sqlgGraph.getSqlDialect().maybeWrapInQoutes(schemaTableTree.getSchemaTable().getTable());

if (p.getValue() instanceof PropertyReference && p.getBiPredicate() instanceof Compare){
result += prefix + "." + sqlgGraph.getSqlDialect().maybeWrapInQoutes(hasContainer.getKey());
String column= prefix + "." + sqlgGraph.getSqlDialect().maybeWrapInQoutes(((PropertyReference)p.getValue()).getColumnName());
result += compareToSql((Compare) p.getBiPredicate(),column);
return result;
if (p.getValue() instanceof PropertyReference && p.getBiPredicate() instanceof Compare) {
result += prefix + "." + sqlgGraph.getSqlDialect().maybeWrapInQoutes(hasContainer.getKey());
String column = prefix + "." + sqlgGraph.getSqlDialect().maybeWrapInQoutes(((PropertyReference) p.getValue()).getColumnName());
result += compareToSql((Compare) p.getBiPredicate(), column);
return result;
} else if (p.getBiPredicate() instanceof Compare) {
if (hasContainer.getKey().equals(T.id.getAccessor())) {
if (schemaTableTree.isHasIDPrimaryKey()) {
Expand Down Expand Up @@ -80,7 +80,7 @@ String toSql(SqlgGraph sqlgGraph, SchemaTableTree schemaTableTree, HasContainer
result += prefix + "." + sqlgGraph.getSqlDialect().maybeWrapInQoutes(identifier);
result += containsToSql((Contains) p.getBiPredicate(), ((Collection<?>) p.getValue()).size());
if (i++ < schemaTableTree.getIdentifiers().size()) {
result += " AND ";
result += " OR ";
}
}
}
Expand Down Expand Up @@ -125,15 +125,15 @@ String toSql(SqlgGraph sqlgGraph, SchemaTableTree schemaTableTree, HasContainer
prefix += "." + sqlgGraph.getSqlDialect().maybeWrapInQoutes(hasContainer.getKey());
result += textToSql(sqlgGraph.getSqlDialect(), prefix, (Text) p.getBiPredicate());
return result;
} else if (p.getBiPredicate() instanceof FullText){
prefix += "." + sqlgGraph.getSqlDialect().maybeWrapInQoutes(hasContainer.getKey());
FullText ft=(FullText)p.getBiPredicate();
result += sqlgGraph.getSqlDialect().getFullTextQueryText(ft, prefix);
return result;
} else if (p.getBiPredicate() instanceof Existence){
result += prefix + "." + sqlgGraph.getSqlDialect().maybeWrapInQoutes(hasContainer.getKey());
result += " "+p.getBiPredicate().toString();
return result;
} else if (p.getBiPredicate() instanceof FullText) {
prefix += "." + sqlgGraph.getSqlDialect().maybeWrapInQoutes(hasContainer.getKey());
FullText ft = (FullText) p.getBiPredicate();
result += sqlgGraph.getSqlDialect().getFullTextQueryText(ft, prefix);
return result;
} else if (p.getBiPredicate() instanceof Existence) {
result += prefix + "." + sqlgGraph.getSqlDialect().maybeWrapInQoutes(hasContainer.getKey());
result += " " + p.getBiPredicate().toString();
return result;
}
throw new IllegalStateException("Unhandled BiPredicate " + p.getBiPredicate().toString());
}
Expand All @@ -157,7 +157,7 @@ private static String compareToSql(Compare compare) {
}
}

private static String compareToSql(Compare compare,String column) {
private static String compareToSql(Compare compare, String column) {
switch (compare) {
case eq:
return " = " + column;
Expand All @@ -176,7 +176,7 @@ private static String compareToSql(Compare compare,String column) {
}
}


private static String containsToSql(Contains contains, int size) {
String result;
if (size == 1) {
Expand Down Expand Up @@ -288,18 +288,27 @@ public void putKeyValueMap(HasContainer hasContainer, Multimap<String, Object> k
}
}
} else {
int i = 0;
for (String identifier : schemaTableTree.getIdentifiers()) {
for (Object value : values) {
if (hasContainer.getKey().equals(T.id.getAccessor())) {
keyValueMap.put(identifier, ((RecordId) value).getIdentifiers().get(i));
} else {
keyValueMap.put(hasContainer.getKey(), value);
for (Object value : values) {
if (hasContainer.getKey().equals(T.id.getAccessor())) {
int i = 0;
for (String identifier : schemaTableTree.getIdentifiers()) {
keyValueMap.put(identifier, ((RecordId) value).getIdentifiers().get(i++));
}
} else {
keyValueMap.put(hasContainer.getKey(), value);
}
i++;
}

// for (String identifier : schemaTableTree.getIdentifiers()) {
// for (Object value : values) {
// if (hasContainer.getKey().equals(T.id.getAccessor())) {
// keyValueMap.put(identifier, ((RecordId) value).getIdentifiers().get(i));
// break;
// } else {
// keyValueMap.put(hasContainer.getKey(), value);
// }
// }
// i++;
// }
}
} else if (p.getBiPredicate() == Text.contains || p.getBiPredicate() == Text.ncontains ||
p.getBiPredicate() == Text.containsCIS || p.getBiPredicate() == Text.ncontainsCIS) {
Expand All @@ -308,13 +317,13 @@ public void putKeyValueMap(HasContainer hasContainer, Multimap<String, Object> k
keyValueMap.put(hasContainer.getKey(), hasContainer.getValue() + "%");
} else if (p.getBiPredicate() == Text.endsWith || p.getBiPredicate() == Text.nendsWith) {
keyValueMap.put(hasContainer.getKey(), "%" + hasContainer.getValue());
} else if (p.getBiPredicate() instanceof Existence){
// no value
} else if (p.getBiPredicate() instanceof Existence) {
// no value
} else if (hasContainer.getKey().equals(T.id.getAccessor()) &&
hasContainer.getValue() instanceof RecordId &&
!((RecordId)hasContainer.getValue()).hasSequenceId()) {
!((RecordId) hasContainer.getValue()).hasSequenceId()) {

RecordId recordId = (RecordId)hasContainer.getValue();
RecordId recordId = (RecordId) hasContainer.getValue();
int i = 0;
for (Object identifier : recordId.getIdentifiers()) {
keyValueMap.put(schemaTableTree.getIdentifiers().get(i++), identifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1614,8 +1614,10 @@ public void writeStreamingEdge(
writer.write(((RecordId) outVertex.id()).getID().toString());
writer.write(COPY_COMMAND_DELIMITER);
} else {
int count = 0;
for (String identifier : outVertexLabel.getIdentifiers()) {
Object value = outVertex.value(identifier);
// Object value = outVertex.value(identifier);
Object value = ((RecordId)outVertex.id()).getID().getIdentifiers().get(count++);
PropertyType propertyType = outVertexLabel.getProperty(identifier).orElseThrow(
() -> new IllegalStateException(String.format("identifier %s must be present on %s", identifier, outVertexLabel.getFullName()))
).getPropertyType();
Expand All @@ -1627,8 +1629,10 @@ public void writeStreamingEdge(
writer.write(((RecordId) inVertex.id()).getID().toString());
} else {
int i = 1;
int count = 0;
for (String identifier : inVertexLabel.getIdentifiers()) {
Object value = inVertex.value(identifier);
// Object value = inVertex.value(identifier);
Object value = ((RecordId)inVertex.id()).getID().getIdentifiers().get(count++);
PropertyType propertyType = inVertexLabel.getProperty(identifier).orElseThrow(
() -> new IllegalStateException(String.format("identifier %s must be present on %s", identifier, inVertexLabel.getFullName()))
).getPropertyType();
Expand Down
11 changes: 7 additions & 4 deletions sqlg-test/src/main/java/org/umlg/sqlg/AnyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.umlg.sqlg.test.batch.TestBatchServerSideEdgeCreation;
import org.umlg.sqlg.test.usersuppliedpk.topology.TestSimpleJoinGremlin;
import org.umlg.sqlg.test.usersuppliedpk.topology.TestMultipleIDQuery;

/**
* Date: 2014/07/16
* Time: 12:10 PM
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
TestSimpleJoinGremlin.class,
TestBatchServerSideEdgeCreation.class,
TestMultipleIDQuery.class
// TestDeletedVertex.class,
// TestGremlinCompileWithHas.class,
// TestTopology.class
// TestSimpleJoinGremlin.class,
// TestBatchServerSideEdgeCreation.class,
// TestPartitioning.class,
// TestLoadEdge.class
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.umlg.sqlg.test.gremlincompile;

import org.apache.commons.collections4.set.ListOrderedSet;
import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.Pop;
import org.apache.tinkerpop.gremlin.process.traversal.Scope;
Expand All @@ -16,10 +17,7 @@
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.umlg.sqlg.structure.RecordId;
import org.umlg.sqlg.structure.SchemaTable;
import org.umlg.sqlg.structure.SqlgGraph;
import org.umlg.sqlg.structure.SqlgVertex;
import org.umlg.sqlg.structure.*;
import org.umlg.sqlg.test.BaseTest;

import java.util.*;
Expand All @@ -39,6 +37,53 @@ public static void beforeClass() {
}
}

@Test
public void testMultipleHasWithinUserSuppliedIds() {
this.sqlgGraph.getTopology().getPublicSchema().ensureVertexLabelExist(
"TestHierarchy",
new LinkedHashMap<String, PropertyType>() {{
put("column1", PropertyType.STRING);
put("column2", PropertyType.STRING);
put("name", PropertyType.STRING);
}},
ListOrderedSet.listOrderedSet(Arrays.asList("column1", "column2"))
);
this.sqlgGraph.tx().commit();
this.sqlgGraph.addVertex(T.label, "TestHierarchy", "column1", "a1", "column2", "a2", "name", "name1");
this.sqlgGraph.addVertex(T.label, "TestHierarchy", "column1", "b1", "column2", "b2", "name", "name1");
this.sqlgGraph.addVertex(T.label, "TestHierarchy", "column1", "c1", "column2", "c2", "name", "name1");
this.sqlgGraph.addVertex(T.label, "TestHierarchy", "column1", "d1", "column2", "d2", "name", "name1");
this.sqlgGraph.tx().commit();

List<String> values1 = Collections.singletonList("");
List<String> values2 = Collections.singletonList("");
List<Vertex> vertexList = this.sqlgGraph.traversal().V()
.hasLabel("TestHierarchy")
.has("column1", P.within(values1))
.has("column2", P.within(values2))
.toList();
Assert.assertEquals(0, vertexList.size());

values1 = Arrays.asList("a1", "b1", "c1");
values2 = Arrays.asList("a2", "b2", "c2");

vertexList = this.sqlgGraph.traversal().V()
.hasLabel("TestHierarchy")
.has("column1", P.within(values1))
.has("column2", P.within(values2))
.toList();
Assert.assertEquals(3, vertexList.size());

values1 = Arrays.asList("a1");
values2 = Arrays.asList("a2", "b2", "c2");
vertexList = this.sqlgGraph.traversal().V()
.hasLabel("TestHierarchy")
.has("column1", P.within(values1))
.has("column2", P.within(values2))
.toList();
Assert.assertEquals(1, vertexList.size());
}

@Test
public void testHasPropertyWithLabel() {
this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
Expand Down

0 comments on commit 3a4c3ac

Please sign in to comment.