Skip to content

Commit 1e80c2f

Browse files
committed
GH-5447 improve group by and value factory
1 parent 8fe8094 commit 1e80c2f

9 files changed

Lines changed: 78 additions & 6 deletions

File tree

core/common/iterator/src/main/java/org/eclipse/rdf4j/common/iteration/CloseableIteration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import java.util.Iterator;
1515
import java.util.stream.Stream;
1616

17+
import org.eclipse.rdf4j.model.Statement;
18+
1719
/**
1820
* An {@link CloseableIteration} that can be closed to free resources that it is holding. CloseableIterations
1921
* automatically free their resources when exhausted. If not read until exhaustion or if you want to make sure the
@@ -33,6 +35,8 @@
3335
*/
3436
public interface CloseableIteration<E> extends Iterator<E>, AutoCloseable {
3537

38+
EmptyIteration<Statement> EMPTY_STATEMENT_ITERATION = new EmptyIteration<>();
39+
3640
/**
3741
* Convert the results to a Java 8 Stream.
3842
*

core/common/iterator/src/main/java/org/eclipse/rdf4j/common/iteration/DualUnionIteration.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ public DualUnionIteration(Comparator<E> cmp,
5050
public static <E> CloseableIteration<? extends E> getWildcardInstance(
5151
CloseableIteration<? extends E> leftIteration, CloseableIteration<? extends E> rightIteration) {
5252

53-
if (rightIteration instanceof EmptyIteration) {
53+
if (leftIteration == EMPTY_STATEMENT_ITERATION) {
54+
return rightIteration;
55+
} else if (rightIteration == EMPTY_STATEMENT_ITERATION) {
56+
return leftIteration;
57+
} else if (rightIteration instanceof EmptyIteration) {
5458
return leftIteration;
5559
} else if (leftIteration instanceof EmptyIteration) {
5660
return rightIteration;
@@ -63,7 +67,11 @@ public static <E> CloseableIteration<? extends E> getWildcardInstance(
6367
public static <E> CloseableIteration<? extends E> getWildcardInstance(Comparator<E> cmp,
6468
CloseableIteration<? extends E> leftIteration, CloseableIteration<? extends E> rightIteration) {
6569

66-
if (rightIteration instanceof EmptyIteration) {
70+
if (leftIteration == EMPTY_STATEMENT_ITERATION) {
71+
return rightIteration;
72+
} else if (rightIteration == EMPTY_STATEMENT_ITERATION) {
73+
return leftIteration;
74+
} else if (rightIteration instanceof EmptyIteration) {
6775
return leftIteration;
6876
} else if (leftIteration instanceof EmptyIteration) {
6977
return rightIteration;
@@ -75,7 +83,11 @@ public static <E> CloseableIteration<? extends E> getWildcardInstance(Comparator
7583
public static <E> CloseableIteration<E> getInstance(CloseableIteration<E> leftIteration,
7684
CloseableIteration<E> rightIteration) {
7785

78-
if (rightIteration instanceof EmptyIteration) {
86+
if (leftIteration == EMPTY_STATEMENT_ITERATION) {
87+
return rightIteration;
88+
} else if (rightIteration == EMPTY_STATEMENT_ITERATION) {
89+
return leftIteration;
90+
} else if (rightIteration instanceof EmptyIteration) {
7991
return leftIteration;
8092
} else if (leftIteration instanceof EmptyIteration) {
8193
return rightIteration;

core/model-api/src/main/java/org/eclipse/rdf4j/model/ValueFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ public interface ValueFactory {
155155
*/
156156
Literal createLiteral(long value);
157157

158+
/**
159+
* Creates a new typed numerical literal representing the specified value.
160+
*
161+
* @param value The value for the literal.
162+
* @param xsd The XSD datatype to use.
163+
*/
164+
default Literal createLiteral(long value, CoreDatatype.XSD xsd) {
165+
return createLiteral(Long.toString(value), xsd);
166+
}
167+
158168
/**
159169
* Creates a new <var>xsd:float</var>-typed literal representing the specified value.
160170
*

core/model-api/src/main/java/org/eclipse/rdf4j/model/base/AbstractLiteral.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,10 @@ private static String toString(double value) {
402402
this(value, Long.toString(value), CoreDatatype.XSD.LONG);
403403
}
404404

405+
NumberLiteral(long value, CoreDatatype.XSD datatype) {
406+
this(value, Long.toString(value), datatype);
407+
}
408+
405409
NumberLiteral(float value) {
406410
this(value, toString(value), CoreDatatype.XSD.FLOAT);
407411
}

core/model-api/src/main/java/org/eclipse/rdf4j/model/base/AbstractValueFactory.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,36 @@ public Literal createLiteral(String label, CoreDatatype datatype) {
128128
throw new IllegalArgumentException("reserved datatype <" + datatype + ">");
129129
}
130130

131+
if (datatype == CoreDatatype.XSD.INTEGER) {
132+
if (label.length() <= 3 && !label.startsWith("+") && !label.startsWith("-")) {
133+
try {
134+
int v = Integer.parseInt(label);
135+
if (v >= 0 && v <= 999) {
136+
return smallIntLiterals[v];
137+
}
138+
} catch (NumberFormatException e) {
139+
// ignore
140+
}
141+
}
142+
} else if (datatype == CoreDatatype.XSD.BOOLEAN) {
143+
if ("true".equals(label) || "1".equals(label)) {
144+
return TRUE;
145+
} else if ("false".equals(label) || "0".equals(label)) {
146+
return FALSE;
147+
}
148+
}
149+
131150
return new TypedLiteral(label, datatype);
132151
}
133152

153+
static TypedLiteral[] smallIntLiterals = new TypedLiteral[1000];
154+
155+
static {
156+
for (int i = 0; i <= 999; i++) {
157+
smallIntLiterals[i] = new TypedLiteral(i + "", CoreDatatype.XSD.INTEGER);
158+
}
159+
}
160+
134161
@Override
135162
public Literal createLiteral(String label, IRI datatype, CoreDatatype coreDatatype) {
136163
Objects.requireNonNull(label, "Label may not be null");
@@ -182,6 +209,11 @@ public Literal createLiteral(long value) {
182209
return new NumberLiteral(value);
183210
}
184211

212+
@Override
213+
public Literal createLiteral(long value, CoreDatatype.XSD datatype) {
214+
return new NumberLiteral(value, datatype);
215+
}
216+
185217
@Override
186218
public Literal createLiteral(float value) {
187219
return new NumberLiteral(value);

core/model/src/main/java/org/eclipse/rdf4j/model/impl/SimpleValueFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ public Literal createLiteral(long value) {
165165
return createIntegerLiteral(value, CoreDatatype.XSD.LONG);
166166
}
167167

168+
@Override
169+
public Literal createLiteral(long value, CoreDatatype.XSD xsd) {
170+
assert xsd.isIntegerDatatype();
171+
return createIntegerLiteral(value, xsd);
172+
}
173+
168174
/**
169175
* Calls {@link #createNumericLiteral(Number, IRI)} with the supplied value and datatype as parameters.
170176
*/

core/model/src/main/java/org/eclipse/rdf4j/model/impl/ValidatingValueFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ public Literal createLiteral(long value) {
175175
return delegate.createLiteral(value);
176176
}
177177

178+
@Override
179+
public Literal createLiteral(long value, CoreDatatype.XSD xsd) {
180+
return delegate.createLiteral(value, xsd);
181+
}
182+
178183
@Override
179184
public Literal createLiteral(float value) {
180185
return delegate.createLiteral(value);

core/queryalgebra/evaluation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/iterator/GroupIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ public CountCollector(ValueFactory vf) {
505505

506506
@Override
507507
public Value getFinalValue() {
508-
return vf.createLiteral(Long.toString(value), CoreDatatype.XSD.INTEGER);
508+
return vf.createLiteral(value, CoreDatatype.XSD.INTEGER);
509509
}
510510
}
511511

core/sail/base/src/main/java/org/eclipse/rdf4j/sail/base/SailDatasetImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class SailDatasetImpl implements SailDataset {
4646

4747
private static final EmptyIteration<Triple> TRIPLE_EMPTY_ITERATION = new EmptyIteration<>();
4848
private static final EmptyIteration<Namespace> NAMESPACES_EMPTY_ITERATION = new EmptyIteration<>();
49-
private static final EmptyIteration<Statement> STATEMENT_EMPTY_ITERATION = new EmptyIteration<>();
5049

5150
/**
5251
* {@link SailDataset} of the backing {@link SailSource}.
@@ -286,7 +285,7 @@ public CloseableIteration<? extends Statement> getStatements(Resource subj, IRI
286285
} else if (iter != null) {
287286
return iter;
288287
} else {
289-
return STATEMENT_EMPTY_ITERATION;
288+
return CloseableIteration.EMPTY_STATEMENT_ITERATION;
290289
}
291290
}
292291

0 commit comments

Comments
 (0)