Skip to content

Commit

Permalink
Merge pull request #62 from sirixdb/refactorings
Browse files Browse the repository at this point in the history
Refactorings
  • Loading branch information
JohannesLichtenberger authored Nov 15, 2023
2 parents 84b997c + 8168ebb commit 18cf5da
Show file tree
Hide file tree
Showing 18 changed files with 303 additions and 169 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</parent>
<groupId>io.sirix</groupId>
<artifactId>brackit</artifactId>
<version>0.4-SNAPSHOT</version>
<version>0.5-SNAPSHOT</version>
<name>Brackit Engine</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -85,7 +85,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<version>3.0.0</version>
<executions>
<execution>
<goals>
Expand Down
288 changes: 216 additions & 72 deletions src/main/java/io/brackit/query/ErrorCode.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ protected Optimizer getOptimizer(Map<QNm, Str> options) {
};
Query xq = new Query(cc,
"let $x:= 1 "
+ "let $y:= (for $a in (1 to 10) for $b in ($a to $a + 2) group by $b let $c := if ($x eq 1) then $b else () return <r b='{$c}' cnt='{count($a)}' vals='{$a}'/>) "
+ "return $y");
+ "let $y:= (for $a in (1 to 10) for $b in ($a to $a + 2) group by $b let $c := if ($x eq 1) then $b else () return <r b='{$c}' cnt='{count($a)}' vals='{$a}'/>) "
+ "return $y");
xq.prettyPrint();
xq.serialize(new BrackitQueryContext(), System.out);
}
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/io/brackit/query/compiler/parser/XQParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@
@SuppressWarnings("SameParameterValue")
public class XQParser extends Tokenizer {

private static final String[] RESERVED_FUNC_NAMES =
new String[] { "attribute", "comment", "document-node", "element", "empty-sequence", "function", "if", "item",
"namespace-node", "node", "processing-instruction", "schema-attribute", "schema-element", "switch", "text",
"typeswitch, array, object" };
private static final String[] RESERVED_FUNC_NAMES = new String[] { "attribute", "comment", "document-node", "element",
"empty-sequence", "function", "if", "item", "namespace-node", "node", "processing-instruction",
"schema-attribute", "schema-element", "switch", "text", "typeswitch, array, object" };

public class IllegalNestingException extends TokenizerException {
private final String expected;
Expand Down Expand Up @@ -712,9 +711,9 @@ private AST annotatedDecl() throws TokenizerException {
}
// perform look ahead
if (laSkipWS(la, "%") == null && laSymSkipWS(la, "variable") == null && laSymSkipWS(la, "function") == null
// Begin XQuery Update Facility 1.0
// Begin XQuery Update Facility 1.0
&& laSymSkipWS(la, "updating") == null
// End XQuery Update Facility 1.0
// End XQuery Update Facility 1.0
) {
return null;
}
Expand Down Expand Up @@ -3082,8 +3081,9 @@ private AST postFixExpr() throws TokenizerException {
AST arrayAccess = new AST(XQ.ArrayAccess);
arrayAccess.addChild(expr);
if (index.getType() == XQ.ArithmeticExpr && index.getChild(1).getValue() instanceof Int32 int32Index
&& int32Index.intValue() == -1 && index.getChild(2).getValue() instanceof Int32 int32Index2
&& int32Index2.intValue() > 0) {
&& int32Index.intValue() == -1 && index.getChild(2).getValue() instanceof Int32 int32Index2 && int32Index2
.intValue()
> 0) {
index = new AST(XQ.Int, new Int32(-1 * int32Index2.intValue()));
}
arrayAccess.addChild(index);
Expand Down Expand Up @@ -3438,8 +3438,8 @@ private AST commonContent() throws TokenizerException {

private AST dirElementContent(boolean checkBoundaryWS) throws TokenizerException {
Token la;
if (checkBoundaryWS && ((la = laSkipS("<")) != null || (la = laSkipS("{")) != null && la(la, "{") == null)
&& (la = laS()) != null) {
if (checkBoundaryWS && ((la = laSkipS("<")) != null || (la = laSkipS("{")) != null && la(la, "{") == null) && (la =
laS()) != null) {
consume(la);
AST boundaryWS = new AST(XQ.Str, la.string());
boundaryWS.setProperty("boundaryWS", true);
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/io/brackit/query/function/bit/Load.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ public Sequence execute(StaticContext sctx, QueryContext ctx, Sequence[] args) t
}
}

private void add(NodeStore store, NodeCollection<?> coll, Sequence resources)
throws DocumentException, IOException {
private void add(NodeStore store, NodeCollection<?> coll, Sequence resources) throws DocumentException, IOException {
if (resources instanceof Atomic) {
String r = ((Atomic) resources).stringValue();
coll.add(new DocumentParser(URIHandler.getInputStream(r)));
Expand All @@ -124,8 +123,7 @@ private void add(NodeStore store, NodeCollection<?> coll, Sequence resources)
}
}

private void create(NodeStore store, String name, Sequence resources)
throws DocumentException, IOException {
private void create(NodeStore store, String name, Sequence resources) throws DocumentException, IOException {
if (resources instanceof Atomic) {
String r = ((Atomic) resources).stringValue();
store.create(name, new DocumentParser(URIHandler.getInputStream(r)));
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/io/brackit/query/function/bit/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ private void add(NodeCollection<?> coll, Sequence nodes) throws DocumentExceptio
}
}

private void create(NodeStore store, String name, Sequence nodes)
throws DocumentException {
private void create(NodeStore store, String name, Sequence nodes) throws DocumentException {
if (nodes instanceof Node<?> n) {
store.create(name, new NodeStoreParser(n));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
*
* @author Johannes Lichtenberger
*/
public abstract class AbstractTemporalNode<E extends AbstractTemporalNode<E>> extends AbstractNode<AbstractTemporalNode<E>>
implements TemporalNode<AbstractTemporalNode<E>> {
public abstract class AbstractTemporalNode<E extends AbstractTemporalNode<E>> extends
AbstractNode<AbstractTemporalNode<E>> implements TemporalNode<AbstractTemporalNode<E>> {
}
29 changes: 10 additions & 19 deletions src/main/java/io/brackit/query/module/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,19 @@ public final class Functions {

// See XQuery Functions and Operators 3 The Error Function
predefine(new Error(new QNm(Namespaces.FN_NSURI, Namespaces.FN_PREFIX, "error"),
new Signature(new SequenceType(AnyItemType.ANY,
Cardinality.Zero))));
new Signature(new SequenceType(AnyItemType.ANY, Cardinality.Zero))));
predefine(new Error(new QNm(Namespaces.FN_NSURI, Namespaces.FN_PREFIX, "error"),
new Signature(new SequenceType(AnyItemType.ANY,
Cardinality.Zero),
new SequenceType(AtomicType.QNM,
Cardinality.One))));
new Signature(new SequenceType(AnyItemType.ANY, Cardinality.Zero),
new SequenceType(AtomicType.QNM, Cardinality.One))));
predefine(new Error(new QNm(Namespaces.FN_NSURI, Namespaces.FN_PREFIX, "error"),
new Signature(new SequenceType(AnyItemType.ANY,
Cardinality.Zero),
new SequenceType(AtomicType.QNM,
Cardinality.ZeroOrOne),
new SequenceType(AtomicType.STR,
Cardinality.One))));
new Signature(new SequenceType(AnyItemType.ANY, Cardinality.Zero),
new SequenceType(AtomicType.QNM, Cardinality.ZeroOrOne),
new SequenceType(AtomicType.STR, Cardinality.One))));
predefine(new Error(new QNm(Namespaces.FN_NSURI, Namespaces.FN_PREFIX, "error"),
new Signature(new SequenceType(AnyItemType.ANY,
Cardinality.Zero),
new SequenceType(AtomicType.QNM,
Cardinality.ZeroOrOne),
new SequenceType(AtomicType.STR, Cardinality.One),
new SequenceType(AnyItemType.ANY,
Cardinality.ZeroOrMany))));
new Signature(new SequenceType(AnyItemType.ANY, Cardinality.Zero),
new SequenceType(AtomicType.QNM, Cardinality.ZeroOrOne),
new SequenceType(AtomicType.STR, Cardinality.One),
new SequenceType(AnyItemType.ANY, Cardinality.ZeroOrMany))));

// See XQuery Functions and Operators 4 Trace Function
predefine(new Trace(new QNm(Namespaces.FN_NSURI, Namespaces.FN_PREFIX, "trace"),
Expand Down
13 changes: 6 additions & 7 deletions src/test/java/io/brackit/query/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,15 @@ public final class JsonTest extends XQueryBaseTest {
private static final String USER_PROFILES_JSON =
"""
{"first_name":"Sammy","last_name":"Shark","location":"Ocean","websites":[{"description":"work","URL":"https://www.digitalocean.com/"},{"description":"tutorials","URL":"https://www.digitalocean.com/community/tutorials"}],"social_media":[{"description":"twitter","link":"https://twitter.com/digitalocean"},{"description":"facebook","link":"https://www.facebook.com/DigitalOceanCloudHosting"},{"description":"github","link":"https://github.com/digitalocean"}]}
"""
.strip();
""".strip();

@Test
public void testNoJsoniqUsingXQuerySyntax() throws IOException {
final var query ="""
xquery version "3.0";
let $xml := <foo>bar</foo>
return $xml/foo[./text() eq 'bar']
""";
final var query = """
xquery version "3.0";
let $xml := <foo>bar</foo>
return $xml/foo[./text() eq 'bar']
""";
final var result = query(query);
assertEquals("", result);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/io/brackit/query/UpdateFacilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void transformIllegalExpressionSecond() {
@Test
public void transformModifyFirst() {
Sequence res = new Query(
"copy $c := <n><a/><a/></n> modify for $a in $c//a return replace node $a with <b/> return $c").execute(ctx);
"copy $c := <n><a/><a/></n> modify for $a in $c//a return replace node $a with <b/> return $c").execute(ctx);
Node<?> a = ctx.getNodeFactory().element(new QNm("n"));
a.append(Kind.ELEMENT, new QNm("b"), null);
a.append(Kind.ELEMENT, new QNm("b"), null);
Expand All @@ -135,7 +135,7 @@ public void transformModifySecond() {
@Test
public void transformTestTwoCopyVars() {
Sequence res = new Query(
"let $f := <a att='1'><b/></a> return copy $m := $f, $n := $f, $o := $f modify delete node $n/b return ($m, $n)").execute(ctx);
"let $f := <a att='1'><b/></a> return copy $m := $f, $n := $f, $o := $f modify delete node $n/b return ($m, $n)").execute(ctx);
Node<?> a1 = ctx.getNodeFactory().element(new QNm("a"));
a1.setAttribute(new QNm("att"), new Una("1"));
a1.append(Kind.ELEMENT, new QNm("b"), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ protected ModuleResolver getModuleResolver() {
};

final Query xq = new Query(chain,
"" + "import module namespace left='left'; "
+ "import module namespace right='right'; " + "right:foo() + left:foo()");
"" + "import module namespace left='left'; " + "import module namespace right='right'; "
+ "right:foo() + left:foo()");
Sequence result = xq.execute(ctx);
ResultChecker.check(new Int32(3), result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public class JoinTest extends XQueryBaseTest {

@Test
public void simpleNestedFor() {
Sequence res = new Query("for $a in (1,2,3) " + "for $b in (2,3,4) " + "where $a = $b " + "return $a").execute(
ctx);
Sequence res = new Query("for $a in (1,2,3) " + "for $b in (2,3,4) " + "where $a = $b " + "return $a").execute(ctx);
ResultChecker.dCheck(intSequence(2, 3), res);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ public void orderBy2() {

@Test
public void doubledNestedWithOrderBy() {
Sequence res = new Query("for $z in 1 " + "let $x := " + "for $a in (1 to 5) " + "let $b := "
+ " for $c in (2,4) " + " let $f := if ($c) then (2,4) else () " + " order by $c " + " let $d := "
+ " for $e in $f " + " let $g := 'ignore' " + " where $c = $a " + " return $e " + " return $d "
+ "return ($a,$b) " + "return $x").execute(ctx);
Sequence res = new Query("for $z in 1 " + "let $x := " + "for $a in (1 to 5) " + "let $b := " + " for $c in (2,4) "
+ " let $f := if ($c) then (2,4) else () " + " order by $c " + " let $d := " + " for $e in $f "
+ " let $g := 'ignore' " + " where $c = $a " + " return $e " + " return $d " + "return ($a,$b) "
+ "return $x").execute(ctx);
ResultChecker.dCheck(intSequence(1, 2, 2, 4, 3, 4, 2, 4, 5), res);
}

Expand Down
11 changes: 5 additions & 6 deletions src/test/java/io/brackit/query/expr/CastTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,21 @@ public void divideDayTimeDuration() {

@Test
public void dateTimeAddYearMonthDuration() {
Sequence res = new Query("xs:dateTime('1981-12-31T12:05:35.1234567') + xs:yearMonthDuration('P1Y2M')").execute(
ctx);
Sequence res = new Query("xs:dateTime('1981-12-31T12:05:35.1234567') + xs:yearMonthDuration('P1Y2M')").execute(ctx);
ResultChecker.dCheck(new DateTime((short) 1983, (byte) 2, (byte) 28, (byte) 12, (byte) 5, 35123456, null), res);
}

@Test
public void dateTimeAddYearMonthDuration2() {
Sequence res = new Query("xs:dateTime('1981-12-12T12:05:35.1234567') + xs:yearMonthDuration('-P1Y1M')").execute(
ctx);
ctx);
ResultChecker.dCheck(new DateTime((short) 1980, (byte) 11, (byte) 12, (byte) 12, (byte) 5, 35123456, null), res);
}

@Test
public void dateTimeAddDayTImeDuration() {
Sequence res = new Query("xs:dateTime('1981-12-12T12:05:35.1234567') + xs:dayTimeDuration('P19DT14H40.654321S')")
.execute(ctx);
.execute(ctx);
ResultChecker.dCheck(new DateTime((short) 1982, (byte) 1, (byte) 1, (byte) 2, (byte) 6, 15777777, null), res);
}

Expand Down Expand Up @@ -243,14 +242,14 @@ public void castDateFromString2() {
@Test
public void subtractDateTimes() {
Sequence res = new Query("xs:dateTime(\"2000-10-30T06:12:00\") - xs:dateTime(\"1999-11-28T09:00:00\")").execute(
ctx);
ctx);
ResultChecker.dCheck(new DTD(false, (short) 336, (byte) 21, (byte) 12, 0), res);
}

@Test
public void subtractDateTimes2() {
Sequence res = new Query("xs:dateTime(\"2000-10-30T06:12:00\") - xs:dateTime(\"1999-11-28T09:00:00Z\")").execute(
ctx);
ctx);
ResultChecker.dCheck(new DTD(false, (short) 336, (byte) 21, (byte) 12, 0), res);
}

Expand Down
Loading

0 comments on commit 18cf5da

Please sign in to comment.