Skip to content

Commit

Permalink
OAK-9625 Support ordered index for first value of a multi-valued prop…
Browse files Browse the repository at this point in the history
…erty, node name, and path
  • Loading branch information
thomasmueller committed Nov 26, 2021
1 parent 2e924b8 commit b893d20
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public String toString() {
public void bindSelector(SourceImpl source) {
selector = source.getExistingSelector(selectorName);
}

@Override
public PropertyExistenceImpl getPropertyExistence() {
return null;
}

@Override
public Set<SelectorImpl> getSelectors() {
return Collections.singleton(selector);
Expand All @@ -79,7 +79,7 @@ public PropertyValue currentProperty() {
// TODO reverse namespace remapping?
return PropertyValues.newString(localName);
}

static String getLocalName(String name) {
int colon = name.indexOf(':');
// TODO LOCALNAME: evaluation of local name might not be correct
Expand All @@ -96,26 +96,30 @@ public void restrict(FilterImpl f, Operator operator, PropertyValue v) {
return;
}
String name = NodeNameImpl.getName(query, v);
if (name != null && f.getSelector().equals(selector)
&& NodeNameImpl.supportedOperator(operator)) {
f.restrictProperty(QueryConstants.RESTRICTION_LOCAL_NAME,
operator, PropertyValues.newString(name));
if (name != null && f.getSelector().equals(selector)) {
if (NodeNameImpl.supportedOperator(operator)) {
f.restrictProperty(QueryConstants.RESTRICTION_LOCAL_NAME,
operator, PropertyValues.newString(name));
}
String fn = getFunction(f.getSelector());
f.restrictProperty(QueryConstants.FUNCTION_RESTRICTION_PREFIX + fn,
operator, v, PropertyType.STRING);
}
}

@Override
public void restrictList(FilterImpl f, List<PropertyValue> list) {
// optimizations of type "LOCALNAME(..) IN(A, B)" are not supported
}

@Override
public String getFunction(SelectorImpl s) {
if (!s.equals(selector)) {
return null;
}
return "@" + QueryConstants.RESTRICTION_LOCAL_NAME;
}

@Override
public boolean supportsRangeConditions() {
return false;
Expand All @@ -125,7 +129,7 @@ public boolean supportsRangeConditions() {
public boolean canRestrictSelector(SelectorImpl s) {
return s.equals(selector);
}

@Override
int getPropertyType() {
return PropertyType.STRING;
Expand All @@ -135,7 +139,7 @@ int getPropertyType() {
public DynamicOperandImpl createCopy() {
return new NodeLocalNameImpl(selectorName);
}

@Override
public OrderEntry getOrderEntry(SelectorImpl s, OrderingImpl o) {
if (!s.equals(selector)) {
Expand All @@ -144,8 +148,8 @@ public OrderEntry getOrderEntry(SelectorImpl s, OrderingImpl o) {
}
return new OrderEntry(
QueryConstants.FUNCTION_RESTRICTION_PREFIX + getFunction(s),
Type.STRING,
o.isDescending() ?
Type.STRING,
o.isDescending() ?
OrderEntry.Order.DESCENDING : OrderEntry.Order.ASCENDING);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ public void bindSelector(SourceImpl source) {
public boolean supportsRangeConditions() {
return false;
}

@Override
public PropertyExistenceImpl getPropertyExistence() {
return null;
}

@Override
public Set<SelectorImpl> getSelectors() {
return Collections.singleton(selector);
Expand All @@ -97,14 +97,18 @@ public void restrict(FilterImpl f, Operator operator, PropertyValue v) {
return;
}
String name = getName(query, v);
if (name != null && f.getSelector().equals(selector)
&& NodeNameImpl.supportedOperator(operator)) {
String localName = NodeLocalNameImpl.getLocalName(name);
f.restrictProperty(QueryConstants.RESTRICTION_LOCAL_NAME,
operator, PropertyValues.newString(localName));
if (name != null && f.getSelector().equals(selector)) {
if (NodeNameImpl.supportedOperator(operator)) {
String localName = NodeLocalNameImpl.getLocalName(name);
f.restrictProperty(QueryConstants.RESTRICTION_LOCAL_NAME,
operator, PropertyValues.newString(localName));
}
String fn = getFunction(f.getSelector());
f.restrictProperty(QueryConstants.FUNCTION_RESTRICTION_PREFIX + fn,
operator, v, PropertyType.STRING);
}
}

@Override
public void restrictList(FilterImpl f, List<PropertyValue> list) {
// optimizations of type "NAME(..) IN(A, B)" are not supported
Expand Down Expand Up @@ -178,7 +182,7 @@ int getPropertyType() {
public DynamicOperandImpl createCopy() {
return new NodeNameImpl(selectorName);
}

@Override
public OrderEntry getOrderEntry(SelectorImpl s, OrderingImpl o) {
if (!s.equals(selector)) {
Expand All @@ -187,8 +191,8 @@ public OrderEntry getOrderEntry(SelectorImpl s, OrderingImpl o) {
}
return new OrderEntry(
QueryConstants.FUNCTION_RESTRICTION_PREFIX + getFunction(s),
Type.STRING,
o.isDescending() ?
Type.STRING,
o.isDescending() ?
OrderEntry.Order.DESCENDING : OrderEntry.Order.ASCENDING);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,89 +32,91 @@
* Test filter conditions.
*/
public class FilterTest {

private final SQL2Parser p = SQL2ParserTest.createTestSQL2Parser();

private Filter createFilter(String xpath) throws ParseException {
String sql = new XPathToSQL2Converter().convert(xpath);
QueryImpl q = (QueryImpl) p.parse(sql);
return q.createFilter(true);
}

private Filter createFilterSQL(String sql) throws ParseException {
QueryImpl q = (QueryImpl) p.parse(sql);
return q.createFilter(true);
}

@Test
public void functionBasedIndex() throws Exception {
String sql2 = "select [jcr:path] from [nt:base] where lower([test]) = 'hello'";
assertEquals("Filter(query=select [jcr:path] from [nt:base] " +
"where lower([test]) = 'hello', " +
"path=*, property=[" +
assertEquals("Filter(query=select [jcr:path] from [nt:base] " +
"where lower([test]) = 'hello', " +
"path=*, property=[" +
"function*lower*@test=[hello], " +
"test=[is not null]])", createFilterSQL(sql2).toString());

sql2 = "select [jcr:path] from [nt:base] where upper([test]) = 'HELLO'";
assertEquals("Filter(query=select [jcr:path] from [nt:base] " +
"where upper([test]) = 'HELLO', " +
"path=*, property=[" +
assertEquals("Filter(query=select [jcr:path] from [nt:base] " +
"where upper([test]) = 'HELLO', " +
"path=*, property=[" +
"function*upper*@test=[HELLO], " +
"test=[is not null]])", createFilterSQL(sql2).toString());

sql2 = "select [jcr:path] from [nt:base] where upper(name()) = 'ACME:TEST'";
assertEquals("Filter(query=select [jcr:path] from [nt:base] " +
"where upper(name()) = 'ACME:TEST', " +
"path=*, property=[" +
assertEquals("Filter(query=select [jcr:path] from [nt:base] " +
"where upper(name()) = 'ACME:TEST', " +
"path=*, property=[" +
"function*upper*@:name=[ACME:TEST]])", createFilterSQL(sql2).toString());

sql2 = "select [jcr:path] from [nt:base] where lower(localname()) > 'test'";
assertEquals("Filter(query=select [jcr:path] from [nt:base] " +
"where lower(localname()) > 'test', " +
"path=*, property=[" +
assertEquals("Filter(query=select [jcr:path] from [nt:base] " +
"where lower(localname()) > 'test', " +
"path=*, property=[" +
"function*lower*@:localname=[(test..]])", createFilterSQL(sql2).toString());

sql2 = "select [jcr:path] from [nt:base] where length([test]) <= 10";
assertEquals("Filter(query=select [jcr:path] from [nt:base] " +
"where length([test]) <= 10, " +
"path=*, property=[function*length*@test=[..10]], " +
assertEquals("Filter(query=select [jcr:path] from [nt:base] " +
"where length([test]) <= 10, " +
"path=*, property=[function*length*@test=[..10]], " +
"test=[is not null]])", createFilterSQL(sql2).toString());

sql2 = "select [jcr:path] from [nt:base] where length([data/test]) > 2";
assertEquals("Filter(query=select [jcr:path] from [nt:base] " +
"where length([data/test]) > 2, " +
"path=*, property=[data/test=[is not null], " +
assertEquals("Filter(query=select [jcr:path] from [nt:base] " +
"where length([data/test]) > 2, " +
"path=*, property=[data/test=[is not null], " +
"function*length*@data/test=[(2..]])", createFilterSQL(sql2).toString());
}

@Test
public void oak4170() throws ParseException {
String sql2 = "select * from [nt:unstructured] where CONTAINS([jcr:content/metadata/comment], 'december')";
Filter f = createFilterSQL(sql2);
String plan = f.toString();
// with the "property is not null" restriction, it would be:
// assertEquals("Filter(query=select * from [nt:unstructured] " +
// "where CONTAINS([jcr:content/metadata/comment], 'december') " +
// "fullText=jcr:content/metadata/comment:\"december\", " +
// assertEquals("Filter(query=select * from [nt:unstructured] " +
// "where CONTAINS([jcr:content/metadata/comment], 'december') " +
// "fullText=jcr:content/metadata/comment:\"december\", " +
// "path=*, property=[jcr:content/metadata/comment=[is not null]])", plan);
assertEquals("Filter(query=select * from [nt:unstructured] " +
"where CONTAINS([jcr:content/metadata/comment], 'december') " +
"fullText=jcr:content/metadata/comment:\"december\", " +
assertEquals("Filter(query=select * from [nt:unstructured] " +
"where CONTAINS([jcr:content/metadata/comment], 'december') " +
"fullText=jcr:content/metadata/comment:\"december\", " +
"path=*)", plan);
assertEquals(f.getPropertyRestrictions().toString(), 0, f.getPropertyRestrictions().size());
f.getPropertyRestriction("jcr:content/metadata/comment");
}

@Test
public void localName() throws Exception {
Filter f = createFilterSQL("select * from [nt:base] where localname() = 'resource'");
assertEquals("[resource]", f.getPropertyRestrictions(":localname").toString());
assertEquals("[resource]", f.getPropertyRestrictions("function*@:localname").toString());
}

@Test
public void name() throws Exception {
Filter f = createFilter("//*[fn:name() = 'nt:resource']");
assertEquals("[resource]", f.getPropertyRestrictions(":localname").toString());
assertEquals("[nt:resource]", f.getPropertyRestrictions("function*@:name").toString());
}

@Test
Expand All @@ -123,7 +125,7 @@ public void mvp() throws Exception {
Filter f = createFilter("//*[(@prop = 'aaa' and @prop = 'bbb' and @prop = 'ccc')]");
assertFalse(f.isAlwaysFalse());
}

@Test
public void isNull() throws Exception {
// this can refer to a multi-valued property
Expand Down

0 comments on commit b893d20

Please sign in to comment.