@@ -69,18 +69,32 @@ public class RowManagerTest {
69
69
private static String [] docs = null ;
70
70
private static Map <String ,Object >[] litRows = null ;
71
71
private static Map <String ,Object >[] groupableRows = null ;
72
+ private static Map <String ,Object >[] numberRows = null ;
72
73
private static String [][] triples = null ;
73
74
private static RowStructure [] rowstructs = null ;
74
75
private static RowSetPart [] datatypeStyles = null ;
75
76
76
77
@ SuppressWarnings ("unchecked" )
77
78
@ BeforeClass
78
79
public static void beforeClass () throws IOException , InterruptedException {
79
- uris = new String []{"/rowtest/docJoin1.json" , "/rowtest/docJoin1.xml" , "/rowtest/docJoin1.txt" };
80
+ uris = new String []{"/rowtest/docJoin1.json" , "/rowtest/docJoin1.xml" , "/rowtest/docJoin1.txt" , "/rowtest/embedded.xml" };
80
81
docs = new String []{
81
82
"{\" a\" :{\" b\" :[\" c\" , 4]}}" ,
82
83
"<a><b>c</b>4</a>" ,
83
- "a b c 4"
84
+ "a b c 4" ,
85
+ "<doc xmlns:sem=\" http://marklogic.com/semantics\" >\n " +
86
+ " <hello>world</hello>\n " +
87
+ " <sem:triple>\n " +
88
+ " <sem:subject>http://marklogicsparql.com/id#5555</sem:subject>\n " +
89
+ " <sem:predicate>http://marklogicsparql.com/addressbook#firstName</sem:predicate>\n " +
90
+ " <sem:object datatype=\" http://www.w3.org/2001/XMLSchema#string\" >Jim</sem:object>\n " +
91
+ " </sem:triple>\n " +
92
+ " <sem:triple>\n " +
93
+ " <sem:subject>http://marklogicsparql.com/id#5555</sem:subject>\n " +
94
+ " <sem:predicate>http://marklogicsparql.com/addressbook#firstName</sem:predicate>\n " +
95
+ " <sem:object datatype=\" http://www.w3.org/2001/XMLSchema#string\" >Jim</sem:object>\n " +
96
+ " </sem:triple>\n " +
97
+ " </doc>"
84
98
};
85
99
86
100
litRows = new Map [3 ];
@@ -126,6 +140,24 @@ public static void beforeClass() throws IOException, InterruptedException {
126
140
row .put ("v" , "2" );
127
141
groupableRows [2 ] = row ;
128
142
143
+ numberRows = new Map [3 ];
144
+ row = new HashMap <>();
145
+ row .put ("r" , 3 );
146
+ row .put ("c1" , "a" );
147
+ row .put ("c2" , "x" );
148
+ numberRows [0 ] = row ;
149
+
150
+ row = new HashMap <>();
151
+ row .put ("r" , 5 );
152
+ row .put ("c1" , "b" );
153
+ row .put ("c2" , "x" );
154
+ numberRows [1 ] = row ;
155
+
156
+ row = new HashMap <>();
157
+ row .put ("r" , 7 );
158
+ row .put ("c1" , "a" );
159
+ numberRows [2 ] = row ;
160
+
129
161
triples = new String [][]{
130
162
new String []{"http://example.org/rowgraph/s1" , "http://example.org/rowgraph/p1" , "http://example.org/rowgraph/o1" },
131
163
new String []{"http://example.org/rowgraph/s1" , "http://example.org/rowgraph/p2" , "http://example.org/rowgraph/o2" },
@@ -158,6 +190,7 @@ public static void beforeClass() throws IOException, InterruptedException {
158
190
.add (uris [0 ], new StringHandle (docs [0 ]).withFormat (Format .JSON ))
159
191
.add (uris [1 ], new StringHandle (docs [1 ]).withFormat (Format .XML ))
160
192
.add (uris [2 ], new StringHandle (docs [2 ]).withFormat (Format .TEXT ))
193
+ .add (uris [3 ], new StringHandle (docs [3 ]).withFormat (Format .XML ))
161
194
.add ("/rowtest/triples1.xml" , new StringHandle (triplesXML ).withFormat (Format .TEXT ))
162
195
);
163
196
@@ -1283,6 +1316,70 @@ public void testRawQueryDSL() throws IOException {
1283
1316
String stringRoot = rowMgr .explain (builtPlan , new StringHandle ()).get ();
1284
1317
assertNotNull (new ObjectMapper ().readTree (stringRoot ));
1285
1318
}
1319
+ @ Test
1320
+ public void testSparqlOptions () throws IOException {
1321
+ String selectStmt = "PREFIX ad: <http://marklogicsparql.com/addressbook#> " +
1322
+ "SELECT ?firstName " +
1323
+ "WHERE {<#5555> ad:firstName ?firstName .}" ;
1324
+
1325
+ RowManager rowMgr = Common .client .newRowManager ();
1326
+ PlanBuilder pb = rowMgr .newPlanBuilder ();
1327
+ PlanSparqlOptions options = pb .sparqlOptions ().withDeduplicated (true ).withBase ("http://marklogicsparql.com/id#" );
1328
+ //System.out.println(options.getDeduplicated());
1329
+ PlanBuilder .ModifyPlan plan = pb .fromSparql (selectStmt , "sparql" , options );
1330
+ JacksonHandle jacksonHandle = new JacksonHandle ();
1331
+ jacksonHandle .setMimetype ("application/json" );
1332
+ rowMgr .resultDoc (plan , jacksonHandle );
1333
+ //System.out.println(jacksonHandle.toString());
1334
+
1335
+ JsonNode jsonBindingsNodes = jacksonHandle .get ().path ("rows" );
1336
+ JsonNode node = jsonBindingsNodes .path (0 );
1337
+ assertEquals (" nodes not returned from fromSparql method" , 1 , jsonBindingsNodes .size ());
1338
+ assertEquals ("Row 1 value incorrect" , "Jim" , node .path ("sparql.firstName" ).path ("value" ).asText ());
1339
+ }
1340
+ @ Test
1341
+ public void testSampleBy () throws IOException {
1342
+ RowManager rowMgr = Common .client .newRowManager ();
1343
+
1344
+ PlanBuilder p = rowMgr .newPlanBuilder ();
1345
+ PlanSampleByOptions options = p .sampleByOptions ().withLimit (2 );
1346
+
1347
+ PlanBuilder .ExportablePlan builtPlan =
1348
+ p .fromView ("opticUnitTest" , "musician" )
1349
+ .sampleBy (options );
1350
+
1351
+ RowSet <RowRecord > rows = rowMgr .resultRows (builtPlan );
1352
+ long count = rows .stream ().count ();
1353
+ assertEquals ("count doesn't match" , 2 , count );
1354
+ }
1355
+ @ Test
1356
+ public void testBucketGroup () {
1357
+ RowManager rowMgr = Common .client .newRowManager ();
1358
+
1359
+ PlanBuilder p = rowMgr .newPlanBuilder ();
1360
+ PlanBuilder .ExportablePlan builtPlan =
1361
+ p .fromLiterals (numberRows )
1362
+ .groupToArrays (
1363
+ p .bucketGroup (p .xs .string ("r" ), p .col ("r" ), p .xs .integerSeq (2 ,4 )),
1364
+ p .count ("numRows" )
1365
+ );
1366
+
1367
+ Iterator <RowRecord > rows = rowMgr .resultRows (builtPlan ).iterator ();
1368
+ assertTrue ("no rows" , rows .hasNext ());
1369
+ RowRecord row = rows .next ();
1370
+ assertFalse ("too many rows" , rows .hasNext ());
1371
+
1372
+ ObjectMapper mapper = new ObjectMapper ();
1373
+
1374
+ JsonNode expect =
1375
+ mapper .createArrayNode ()
1376
+ .add (mapper .createObjectNode ()
1377
+ .put ("r_bucket" , 1 ).put ("numRows" , 1 ))
1378
+ .add (mapper .createObjectNode ()
1379
+ .put ("r_bucket" , 2 ).put ("numRows" , 2 ));
1380
+ JsonNode actual = row .getContainer ("r" );
1381
+ assertEquals ("group unequal" , expect , actual );
1382
+ }
1286
1383
private void checkSingleRow (NodeList row , RowSetPart datatypeStyle ) {
1287
1384
assertEquals ("unexpected column count in XML" , 2 , row .getLength ());
1288
1385
Element testElement = (Element ) row .item (0 );
0 commit comments