@@ -69,18 +69,32 @@ public class RowManagerTest {
6969 private static String [] docs = null ;
7070 private static Map <String ,Object >[] litRows = null ;
7171 private static Map <String ,Object >[] groupableRows = null ;
72+ private static Map <String ,Object >[] numberRows = null ;
7273 private static String [][] triples = null ;
7374 private static RowStructure [] rowstructs = null ;
7475 private static RowSetPart [] datatypeStyles = null ;
7576
7677 @ SuppressWarnings ("unchecked" )
7778 @ BeforeClass
7879 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" };
8081 docs = new String []{
8182 "{\" a\" :{\" b\" :[\" c\" , 4]}}" ,
8283 "<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>"
8498 };
8599
86100 litRows = new Map [3 ];
@@ -126,6 +140,24 @@ public static void beforeClass() throws IOException, InterruptedException {
126140 row .put ("v" , "2" );
127141 groupableRows [2 ] = row ;
128142
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+
129161 triples = new String [][]{
130162 new String []{"http://example.org/rowgraph/s1" , "http://example.org/rowgraph/p1" , "http://example.org/rowgraph/o1" },
131163 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 {
158190 .add (uris [0 ], new StringHandle (docs [0 ]).withFormat (Format .JSON ))
159191 .add (uris [1 ], new StringHandle (docs [1 ]).withFormat (Format .XML ))
160192 .add (uris [2 ], new StringHandle (docs [2 ]).withFormat (Format .TEXT ))
193+ .add (uris [3 ], new StringHandle (docs [3 ]).withFormat (Format .XML ))
161194 .add ("/rowtest/triples1.xml" , new StringHandle (triplesXML ).withFormat (Format .TEXT ))
162195 );
163196
@@ -1283,6 +1316,70 @@ public void testRawQueryDSL() throws IOException {
12831316 String stringRoot = rowMgr .explain (builtPlan , new StringHandle ()).get ();
12841317 assertNotNull (new ObjectMapper ().readTree (stringRoot ));
12851318 }
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+ }
12861383 private void checkSingleRow (NodeList row , RowSetPart datatypeStyle ) {
12871384 assertEquals ("unexpected column count in XML" , 2 , row .getLength ());
12881385 Element testElement = (Element ) row .item (0 );
0 commit comments