Skip to content

Commit 8ff9d03

Browse files
committed
MLE-23473 Implement fromDocs
Add implementation of optic fromDocs to java client. Includes new context method to indicate current row for expressions.
1 parent 52634bb commit 8ff9d03

File tree

9 files changed

+876
-0
lines changed

9 files changed

+876
-0
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/expression/PlanBuilder.java

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ protected PlanBuilder(
247247
*/
248248
public abstract PatchBuilder patchBuilder(XsStringVal contextPath, Map<String,String> namespaces);
249249
/**
250+
* Create column definitions which can be used in op:from-docs. Below functions are used to create column definitions. op:add-column, op:type, op:xpath, op:expr, op:nullable, op:default, op:dimension, op:coordinate-system, op:units, op:collation.
251+
* @return a PlanColumnBuilder object
252+
*/
253+
public abstract PlanColumnBuilder columnBuilder();
254+
/**
250255
* This function creates a placeholder for a literal value in an expression or as the offset or max for a limit. The op:result function throws in an error if the binding parameter does not specify a literal value for the parameter.
251256
* <p>
252257
* Provides a client interface to the <a href="http://docs.marklogic.com/op:param" target="mlserverdoc">op:param</a> server function.
@@ -403,6 +408,91 @@ protected PlanBuilder(
403408
*/
404409
public abstract AccessPlan fromView(XsStringVal schema, XsStringVal view, XsStringVal qualifierName, PlanSystemColumn sysCols);
405410
/**
411+
* This function dynamically maps semi-structured data (JSON/XML) into rows and columns without deploying a TDE template. It enables ad-hoc queries similar to Virtual Template Views but with additional flexibility, such as node output and advanced column customization.
412+
* @param query Query to select documents for row generation. The query can be a cts:query or a string as a shortcut for a cts:word-query.
413+
* @param contextPath XPath applied to each matched document; each result becomes a row.
414+
* @param columnSpec The column definitions created by using op:column-builder.
415+
* @return a AccessPlan object
416+
*/
417+
public abstract AccessPlan fromDocs(String query, String contextPath, PlanColumnBuilder columnSpec);
418+
/**
419+
* This function dynamically maps semi-structured data (JSON/XML) into rows and columns without deploying a TDE template. It enables ad-hoc queries similar to Virtual Template Views but with additional flexibility, such as node output and advanced column customization.
420+
* @param query Query to select documents for row generation. The query can be a cts:query or a string as a shortcut for a cts:word-query.
421+
* @param contextPath XPath applied to each matched document; each result becomes a row.
422+
* @param columnSpec The column definitions created by using op:column-builder.
423+
* @return a AccessPlan object
424+
*/
425+
public abstract AccessPlan fromDocs(CtsQueryExpr query, XsStringVal contextPath, PlanColumnBuilder columnSpec);
426+
/**
427+
* This function dynamically maps semi-structured data (JSON/XML) into rows and columns without deploying a TDE template. It enables ad-hoc queries similar to Virtual Template Views but with additional flexibility, such as node output and advanced column customization.
428+
* @param query Query to select documents for row generation. The query can be a cts:query or a string as a shortcut for a cts:word-query.
429+
* @param contextPath XPath applied to each matched document; each result becomes a row.
430+
* @param columnSpec The column definitions created by using op:column-builder.
431+
* @param qualifier Specifies a name for qualifying the column names in place of the combination of the schema and view names. Use cases for the qualifier include self joins. Using an empty string removes all qualification from the column names.
432+
* @return a AccessPlan object
433+
*/
434+
public abstract AccessPlan fromDocs(String query, String contextPath, PlanColumnBuilder columnSpec, String qualifier);
435+
/**
436+
* This function dynamically maps semi-structured data (JSON/XML) into rows and columns without deploying a TDE template. It enables ad-hoc queries similar to Virtual Template Views but with additional flexibility, such as node output and advanced column customization.
437+
* @param query Query to select documents for row generation. The query can be a cts:query or a string as a shortcut for a cts:word-query.
438+
* @param contextPath XPath applied to each matched document; each result becomes a row.
439+
* @param columnSpec The column definitions created by using op:column-builder.
440+
* @param qualifier Specifies a name for qualifying the column names in place of the combination of the schema and view names. Use cases for the qualifier include self joins. Using an empty string removes all qualification from the column names.
441+
* @return a AccessPlan object
442+
*/
443+
public abstract AccessPlan fromDocs(CtsQueryExpr query, String contextPath, PlanColumnBuilder columnSpec, String qualifier);
444+
/**
445+
* This function dynamically maps semi-structured data (JSON/XML) into rows and columns without deploying a TDE template. It enables ad-hoc queries similar to Virtual Template Views but with additional flexibility, such as node output and advanced column customization.
446+
* @param query Query to select documents for row generation. The query can be a cts:query or a string as a shortcut for a cts:word-query.
447+
* @param contextPath XPath applied to each matched document; each result becomes a row.
448+
* @param columnSpec The column definitions created by using op:column-builder.
449+
* @param qualifier Specifies a name for qualifying the column names in place of the combination of the schema and view names. Use cases for the qualifier include self joins. Using an empty string removes all qualification from the column names.
450+
* @return a AccessPlan object
451+
*/
452+
public abstract AccessPlan fromDocs(CtsQueryExpr query, XsStringVal contextPath, PlanColumnBuilder columnSpec, XsStringVal qualifier);
453+
/**
454+
* This function dynamically maps semi-structured data (JSON/XML) into rows and columns without deploying a TDE template. It enables ad-hoc queries similar to Virtual Template Views but with additional flexibility, such as node output and advanced column customization.
455+
* @param query Query to select documents for row generation. The query can be a cts:query or a string as a shortcut for a cts:word-query.
456+
* @param contextPath XPath applied to each matched document; each result becomes a row.
457+
* @param columnSpec The column definitions created by using op:column-builder.
458+
* @param qualifier Specifies a name for qualifying the column names in place of the combination of the schema and view names. Use cases for the qualifier include self joins. Using an empty string removes all qualification from the column names.
459+
* @param systemCol An optional named fragment id column returned by op:fragment-id-col. One use case for fragment ids is in joins with lexicons or document content.
460+
* @return a AccessPlan object
461+
*/
462+
public abstract AccessPlan fromDocs(String query, String contextPath, PlanColumnBuilder columnSpec, String qualifier, PlanSystemColumn systemCol);
463+
/**
464+
* This function dynamically maps semi-structured data (JSON/XML) into rows and columns without deploying a TDE template. It enables ad-hoc queries similar to Virtual Template Views but with additional flexibility, such as node output and advanced column customization.
465+
* @param query Query to select documents for row generation. The query can be a cts:query or a string as a shortcut for a cts:word-query.
466+
* @param contextPath XPath applied to each matched document; each result becomes a row.
467+
* @param columnSpec The column definitions created by using op:column-builder.
468+
* @param qualifier Specifies a name for qualifying the column names in place of the combination of the schema and view names. Use cases for the qualifier include self joins. Using an empty string removes all qualification from the column names.
469+
* @param systemCol An optional named fragment id column returned by op:fragment-id-col. One use case for fragment ids is in joins with lexicons or document content.
470+
* @return a AccessPlan object
471+
*/
472+
public abstract AccessPlan fromDocs(CtsQueryExpr query, XsStringVal contextPath, PlanColumnBuilder columnSpec, XsStringVal qualifier, PlanSystemColumn systemCol);
473+
/**
474+
* This function dynamically maps semi-structured data (JSON/XML) into rows and columns without deploying a TDE template. It enables ad-hoc queries similar to Virtual Template Views but with additional flexibility, such as node output and advanced column customization.
475+
* @param query Query to select documents for row generation. The query can be a cts:query or a string as a shortcut for a cts:word-query.
476+
* @param contextPath XPath applied to each matched document; each result becomes a row.
477+
* @param columnSpec The column definitions created by using op:column-builder.
478+
* @param qualifier Specifies a name for qualifying the column names in place of the combination of the schema and view names. Use cases for the qualifier include self joins. Using an empty string removes all qualification from the column names.
479+
* @param systemCol An optional named fragment id column returned by op:fragment-id-col. One use case for fragment ids is in joins with lexicons or document content.
480+
* @param namespaces Namespaces prefix (key) and uri (value).
481+
* @return a AccessPlan object
482+
*/
483+
public abstract AccessPlan fromDocs(String query, String contextPath, PlanColumnBuilder columnSpec, String qualifier, PlanSystemColumn systemCol, PlanNamespaceBindingsSeq namespaces);
484+
/**
485+
* This function dynamically maps semi-structured data (JSON/XML) into rows and columns without deploying a TDE template. It enables ad-hoc queries similar to Virtual Template Views but with additional flexibility, such as node output and advanced column customization.
486+
* @param query Query to select documents for row generation. The query can be a cts:query or a string as a shortcut for a cts:word-query.
487+
* @param contextPath XPath applied to each matched document; each result becomes a row.
488+
* @param columnSpec The column definitions created by using op:column-builder.
489+
* @param qualifier Specifies a name for qualifying the column names in place of the combination of the schema and view names. Use cases for the qualifier include self joins. Using an empty string removes all qualification from the column names.
490+
* @param systemCol An optional named fragment id column returned by op:fragment-id-col. One use case for fragment ids is in joins with lexicons or document content.
491+
* @param namespaces Namespaces prefix (key) and uri (value).
492+
* @return a AccessPlan object
493+
*/
494+
public abstract AccessPlan fromDocs(CtsQueryExpr query, XsStringVal contextPath, PlanColumnBuilder columnSpec, XsStringVal qualifier, PlanSystemColumn systemCol, PlanNamespaceBindingsSeq namespaces);
495+
/**
406496
* This function factory returns a new function that takes a name parameter and returns a sem:iri, prepending the specified base URI onto the name.
407497
* @param base The base URI to be prepended to the name.
408498
* @return a PlanPrefixer object
@@ -1130,6 +1220,11 @@ protected PlanBuilder(
11301220
*/
11311221
public abstract PlanCase when(ServerExpression condition, ServerExpression... value);
11321222
/**
1223+
* This helper function returns the node from the current processing row. It is to be used in op:xpath, to reference the 'current item' instead of a doc column.
1224+
* @return a PlanContextExprCall object
1225+
*/
1226+
public abstract PlanContextExprCall context();
1227+
/**
11331228
* This function extracts a sequence of child nodes from a column with node values -- especially, the document nodes from a document join. The path is an XPath (specified as a string) to apply to each node to generate a sequence of nodes as an expression value.
11341229
* <p>
11351230
* Provides a client interface to the <a href="http://docs.marklogic.com/op:xpath" target="mlserverdoc">op:xpath</a> server function.
@@ -1167,6 +1262,24 @@ protected PlanBuilder(
11671262
* @return a server expression with the <a href="{@docRoot}/doc-files/types/node.html">node</a> server data type
11681263
*/
11691264
public abstract ServerExpression xpath(PlanColumn column, ServerExpression path, PlanNamespaceBindingsSeq namespaceBindings);
1265+
/**
1266+
* This function extracts a sequence of child nodes from a server expression (such as op:context()) with node values. The path is an XPath (specified as a string) to apply to each node to generate a sequence of nodes as an expression value.
1267+
* <p>
1268+
* Provides a client interface to the <a href="http://docs.marklogic.com/op:xpath" target="mlserverdoc">op:xpath</a> server function.
1269+
* @param expression The server expression (such as op:context()) from which to extract the child nodes.
1270+
* @param path An XPath (specified as a string) to apply to each node. (of <a href="{@docRoot}/doc-files/types/xs_string.html">xs:string</a>)
1271+
* @return a server expression with the <a href="{@docRoot}/doc-files/types/node.html">node</a> server data type
1272+
*/
1273+
public abstract ServerExpression xpath(ServerExpression expression, String path);
1274+
/**
1275+
* This function extracts a sequence of child nodes from a server expression (such as op:context()) with node values. The path is an XPath to apply to each node to generate a sequence of nodes as an expression value.
1276+
* <p>
1277+
* Provides a client interface to the <a href="http://docs.marklogic.com/op:xpath" target="mlserverdoc">op:xpath</a> server function.
1278+
* @param expression The server expression (such as op:context()) from which to extract the child nodes.
1279+
* @param path An XPath to apply to each node. (of <a href="{@docRoot}/doc-files/types/xs_string.html">xs:string</a>)
1280+
* @return a server expression with the <a href="{@docRoot}/doc-files/types/node.html">node</a> server data type
1281+
*/
1282+
public abstract ServerExpression xpath(ServerExpression expression, ServerExpression path);
11701283
/**
11711284
* This function constructs a JSON document with the root content, which must be exactly one JSON object or array node.
11721285
* <p>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
3+
*/
4+
package com.marklogic.client.impl;
5+
6+
import com.marklogic.client.type.PlanColumnBuilder;
7+
import com.marklogic.client.type.ServerExpression;
8+
import com.marklogic.client.type.XsStringVal;
9+
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
14+
@SuppressWarnings("unchecked")
15+
class ColumnBuilderImpl extends BaseTypeImpl.BaseCallImpl implements PlanColumnBuilder, BaseTypeImpl.BaseArgImpl {
16+
17+
ColumnBuilderImpl() {
18+
super("op", "suboperators",
19+
new BaseTypeImpl.BaseArgImpl[]{
20+
new BaseTypeImpl.BaseCallImpl("op", "column-builder", new BaseTypeImpl.BaseArgImpl[]{})
21+
});
22+
}
23+
24+
private ColumnBuilderImpl(List<BaseTypeImpl.BaseArgImpl> args) {
25+
super("op", "suboperators", args.toArray(new BaseTypeImpl.BaseArgImpl[]{}));
26+
}
27+
28+
public PlanColumnBuilder addColumn(String name) {
29+
return addArg("add-column", name);
30+
}
31+
32+
public PlanColumnBuilder addColumn(XsStringVal name) {
33+
return addArg("add-column", name);
34+
}
35+
36+
public PlanColumnBuilder xpath(String path) {
37+
return addArg("xpath", path);
38+
}
39+
40+
public PlanColumnBuilder xpath(XsStringVal path) {
41+
return addArg("xpath", path);
42+
}
43+
44+
public PlanColumnBuilder type(String type) {
45+
return addArg("type", type);
46+
}
47+
48+
public PlanColumnBuilder type(XsStringVal type) {
49+
return addArg("type", type);
50+
}
51+
52+
public PlanColumnBuilder nullable(boolean nullable) {
53+
return addArg("nullable", new XsValueImpl.BooleanValImpl(nullable));
54+
}
55+
56+
public PlanColumnBuilder expr(ServerExpression expression) {
57+
return addArg("expr", expression);
58+
}
59+
60+
public PlanColumnBuilder defaultValue(String value) {
61+
return addArg("default", value);
62+
}
63+
64+
public PlanColumnBuilder defaultValue(XsStringVal value) {
65+
return addArg("default", value);
66+
}
67+
68+
public PlanColumnBuilder collation(String collation) {
69+
return addArg("collation", collation);
70+
}
71+
72+
public PlanColumnBuilder collation(XsStringVal collation) {
73+
return addArg("collation", collation);
74+
}
75+
76+
public PlanColumnBuilder dimension(int dimension) {
77+
return addArg("dimension", dimension);
78+
}
79+
80+
public PlanColumnBuilder coordinateSystem(String coordinateSystem) {
81+
return addArg("coordinate-system", coordinateSystem);
82+
}
83+
84+
public PlanColumnBuilder coordinateSystem(XsStringVal coordinateSystem) {
85+
return addArg("coordinate-system", coordinateSystem);
86+
}
87+
88+
private PlanColumnBuilder addArg(String functionName, Object... args) {
89+
BaseTypeImpl.BaseArgImpl newArg = new BaseTypeImpl.BaseCallImpl(
90+
"op", functionName, makeArgs(args)
91+
);
92+
List<BaseTypeImpl.BaseArgImpl> newArgs = new ArrayList<>();
93+
newArgs.addAll(Arrays.asList(getArgsImpl()));
94+
newArgs.add(newArg);
95+
return new ColumnBuilderImpl(newArgs);
96+
}
97+
98+
private BaseTypeImpl.BaseArgImpl[] makeArgs(Object... args) {
99+
List<BaseTypeImpl.BaseArgImpl> argList = new ArrayList<>();
100+
for (Object arg : args) {
101+
if (arg instanceof BaseTypeImpl.BaseArgImpl) {
102+
argList.add((BaseTypeImpl.BaseArgImpl) arg);
103+
} else {
104+
// Use Literal for plain values (strings, numbers, etc.)
105+
argList.add(new BaseTypeImpl.Literal(arg));
106+
}
107+
}
108+
return argList.toArray(new BaseTypeImpl.BaseArgImpl[]{});
109+
}
110+
}

0 commit comments

Comments
 (0)