Skip to content

Commit 0c9e89a

Browse files
authored
Merge pull request #1898 from marklogic/feature/MLE-23743-implement-fromDocs
MLE-23473 Implement fromDocs
2 parents e4bd082 + 0f9b30c commit 0c9e89a

File tree

18 files changed

+681
-0
lines changed

18 files changed

+681
-0
lines changed

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

Lines changed: 84 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,58 @@ 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+
* @since 8.1.0; requires MarkLogic 12.1 or higher.
417+
*/
418+
public abstract AccessPlan fromDocs(String query, String contextPath, PlanColumnBuilder columnSpec);
419+
/**
420+
* 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.
421+
* @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.
422+
* @param contextPath XPath applied to each matched document; each result becomes a row.
423+
* @param columnSpec The column definitions created by using op:column-builder.
424+
* @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.
425+
* @return a AccessPlan object
426+
* @since 8.1.0; requires MarkLogic 12.1 or higher.
427+
*/
428+
public abstract AccessPlan fromDocs(String query, String contextPath, PlanColumnBuilder columnSpec, String qualifier);
429+
/**
430+
* 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.
431+
* @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.
432+
* @param contextPath XPath applied to each matched document; each result becomes a row.
433+
* @param columnSpec The column definitions created by using op:column-builder.
434+
* @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.
435+
* @return a AccessPlan object
436+
* @since 8.1.0; requires MarkLogic 12.1 or higher.
437+
*/
438+
public abstract AccessPlan fromDocs(CtsQueryExpr query, String contextPath, PlanColumnBuilder columnSpec, String qualifier);
439+
/**
440+
* 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.
441+
* @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.
442+
* @param contextPath XPath applied to each matched document; each result becomes a row.
443+
* @param columnSpec The column definitions created by using op:column-builder.
444+
* @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.
445+
* @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.
446+
* @return a AccessPlan object
447+
* @since 8.1.0; requires MarkLogic 12.1 or higher.
448+
*/
449+
public abstract AccessPlan fromDocs(String query, String contextPath, PlanColumnBuilder columnSpec, String qualifier, PlanSystemColumn systemCol);
450+
/**
451+
* 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.
452+
* @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.
453+
* @param contextPath XPath applied to each matched document; each result becomes a row.
454+
* @param columnSpec The column definitions created by using op:column-builder.
455+
* @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.
456+
* @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.
457+
* @param namespaces Namespaces prefix (key) and uri (value).
458+
* @return a AccessPlan object
459+
* @since 8.1.0; requires MarkLogic 12.1 or higher.
460+
*/
461+
public abstract AccessPlan fromDocs(String query, String contextPath, PlanColumnBuilder columnSpec, String qualifier, PlanSystemColumn systemCol, PlanNamespaceBindingsSeq namespaces);
462+
/**
406463
* 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.
407464
* @param base The base URI to be prepended to the name.
408465
* @return a PlanPrefixer object
@@ -1130,6 +1187,11 @@ protected PlanBuilder(
11301187
*/
11311188
public abstract PlanCase when(ServerExpression condition, ServerExpression... value);
11321189
/**
1190+
* 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.
1191+
* @return a PlanContextExprCall object
1192+
*/
1193+
public abstract PlanContextExprCall context();
1194+
/**
11331195
* 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.
11341196
* <p>
11351197
* Provides a client interface to the <a href="http://docs.marklogic.com/op:xpath" target="mlserverdoc">op:xpath</a> server function.
@@ -1167,6 +1229,24 @@ protected PlanBuilder(
11671229
* @return a server expression with the <a href="{@docRoot}/doc-files/types/node.html">node</a> server data type
11681230
*/
11691231
public abstract ServerExpression xpath(PlanColumn column, ServerExpression path, PlanNamespaceBindingsSeq namespaceBindings);
1232+
/**
1233+
* 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.
1234+
* <p>
1235+
* Provides a client interface to the <a href="http://docs.marklogic.com/op:xpath" target="mlserverdoc">op:xpath</a> server function.
1236+
* @param expression The server expression (such as op:context()) from which to extract the child nodes.
1237+
* @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>)
1238+
* @return a server expression with the <a href="{@docRoot}/doc-files/types/node.html">node</a> server data type
1239+
*/
1240+
public abstract ServerExpression xpath(ServerExpression expression, String path);
1241+
/**
1242+
* 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.
1243+
* <p>
1244+
* Provides a client interface to the <a href="http://docs.marklogic.com/op:xpath" target="mlserverdoc">op:xpath</a> server function.
1245+
* @param expression The server expression (such as op:context()) from which to extract the child nodes.
1246+
* @param path An XPath to apply to each node. (of <a href="{@docRoot}/doc-files/types/xs_string.html">xs:string</a>)
1247+
* @return a server expression with the <a href="{@docRoot}/doc-files/types/node.html">node</a> server data type
1248+
*/
1249+
public abstract ServerExpression xpath(ServerExpression expression, ServerExpression path);
11701250
/**
11711251
* This function constructs a JSON document with the root content, which must be exactly one JSON object or array node.
11721252
* <p>
@@ -2048,13 +2128,15 @@ public interface ModifyPlan extends PreparePlan, PlanBuilderBase.ModifyPlanBase
20482128
* @param start The column is the starting node of the traversal. The column can be named with a string or a column function such as op:col, op:view-col, or op:schema-col, or constructed from an expression with the op:as function. See {@link PlanBuilder#col(XsStringVal)}
20492129
* @param end The column is the end node of the traversal. The column can be named with a string or a column function such as op:col, op:view-col, or op:schema-col, or constructed from an expression with the op:as function. See {@link PlanBuilder#col(XsStringVal)}
20502130
* @return a ModifyPlan object
2131+
* @since 8.1.0; requires MarkLogic 12.1 or higher.
20512132
*/
20522133
public abstract ModifyPlan transitiveClosure(String start, String end);
20532134
/**
20542135
* This method performs a transitive closure operation over a graph-like structure, identifying all reachable node pairs from a given start node to an end node through one or more intermediate steps. A set of (start, end) node pairs where a path exists between them with a length between minLength and maxLength, inclusive. This models the SPARQL one-or-more (+) operator, enabling recursive or chained relationships to be queried efficiently.
20552136
* @param start The column is the starting node of the traversal. The column can be named with a string or a column function such as op:col, op:view-col, or op:schema-col, or constructed from an expression with the op:as function. See {@link PlanBuilder#col(XsStringVal)}
20562137
* @param end The column is the end node of the traversal. The column can be named with a string or a column function such as op:col, op:view-col, or op:schema-col, or constructed from an expression with the op:as function. See {@link PlanBuilder#col(XsStringVal)}
20572138
* @return a ModifyPlan object
2139+
* @since 8.1.0; requires MarkLogic 12.1 or higher.
20582140
*/
20592141
public abstract ModifyPlan transitiveClosure(PlanExprCol start, PlanExprCol end);
20602142
/**
@@ -2063,6 +2145,7 @@ public interface ModifyPlan extends PreparePlan, PlanBuilderBase.ModifyPlanBase
20632145
* @param end The column is the end node of the traversal. The column can be named with a string or a column function such as op:col, op:view-col, or op:schema-col, or constructed from an expression with the op:as function. See {@link PlanBuilder#col(XsStringVal)}
20642146
* @param options This is either an array of strings or an object containing keys and values for the options to this operator. Options include: min-length This option is the minimum number of steps (edges) required in the path. It should be a non-negative integer, and the default is 1.max-length This option Maximum number of steps (edges) allowed in the path. It should be a non-negative integer, and the default is unlimited.
20652147
* @return a ModifyPlan object
2148+
* @since 8.1.0; requires MarkLogic 12.1 or higher.
20662149
*/
20672150
public abstract ModifyPlan transitiveClosure(String start, String end, PlanTransitiveClosureOptions options);
20682151
/**
@@ -2071,6 +2154,7 @@ public interface ModifyPlan extends PreparePlan, PlanBuilderBase.ModifyPlanBase
20712154
* @param end The column is the end node of the traversal. The column can be named with a string or a column function such as op:col, op:view-col, or op:schema-col, or constructed from an expression with the op:as function. See {@link PlanBuilder#col(XsStringVal)}
20722155
* @param options This is either an array of strings or an object containing keys and values for the options to this operator. Options include: min-length This option is the minimum number of steps (edges) required in the path. It should be a non-negative integer, and the default is 1.max-length This option Maximum number of steps (edges) allowed in the path. It should be a non-negative integer, and the default is unlimited.
20732156
* @return a ModifyPlan object
2157+
* @since 8.1.0; requires MarkLogic 12.1 or higher.
20742158
*/
20752159
public abstract ModifyPlan transitiveClosure(PlanExprCol start, PlanExprCol end, PlanTransitiveClosureOptions options);
20762160
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public interface VecExpr {
156156
* Provides a client interface to the <a href="http://docs.marklogic.com/vec:precision" target="mlserverdoc">vec:precision</a> server function.
157157
* @param vector The input vector to reduce precision. Can be a vector or an empty sequence. (of <a href="{@docRoot}/doc-files/types/vec_vector.html">vec:vector</a>)
158158
* @return a server expression with the <a href="{@docRoot}/doc-files/types/vec_vector.html">vec:vector</a> server data type
159+
* @since 8.1.0; requires MarkLogic 12.1 or higher.
159160
*/
160161
public ServerExpression precision(ServerExpression vector);
161162
/**
@@ -165,6 +166,7 @@ public interface VecExpr {
165166
* @param vector The input vector to reduce precision. Can be a vector or an empty sequence. (of <a href="{@docRoot}/doc-files/types/vec_vector.html">vec:vector</a>)
166167
* @param precision The number of mantissa bits to preserve (9-32 inclusive). Default is 16. Higher values preserve more precision. If the value is outside the valid range, throw VEC-INVALIDPRECISION. (of <a href="{@docRoot}/doc-files/types/xs_unsignedInt.html">xs:unsignedInt</a>)
167168
* @return a server expression with the <a href="{@docRoot}/doc-files/types/vec_vector.html">vec:vector</a> server data type
169+
* @since 8.1.0; requires MarkLogic 12.1 or higher.
168170
*/
169171
public ServerExpression precision(ServerExpression vector, ServerExpression precision);
170172
/**
@@ -210,6 +212,7 @@ public interface VecExpr {
210212
* Provides a client interface to the <a href="http://docs.marklogic.com/vec:trunc" target="mlserverdoc">vec:trunc</a> server function.
211213
* @param vector The input vector to truncate. (of <a href="{@docRoot}/doc-files/types/vec_vector.html">vec:vector</a>)
212214
* @return a server expression with the <a href="{@docRoot}/doc-files/types/vec_vector.html">vec:vector</a> server data type
215+
* @since 8.1.0; requires MarkLogic 12.1 or higher.
213216
*/
214217
public ServerExpression trunc(ServerExpression vector);
215218
/**
@@ -219,6 +222,7 @@ public interface VecExpr {
219222
* @param vector The input vector to truncate. (of <a href="{@docRoot}/doc-files/types/vec_vector.html">vec:vector</a>)
220223
* @param n The numbers of decimal places to truncate to. The default is 0. Negative values cause that many digits to the left of the decimal point to be truncated. (of <a href="{@docRoot}/doc-files/types/xs_int.html">xs:int</a>)
221224
* @return a server expression with the <a href="{@docRoot}/doc-files/types/vec_vector.html">vec:vector</a> server data type
225+
* @since 8.1.0; requires MarkLogic 12.1 or higher.
222226
*/
223227
public ServerExpression trunc(ServerExpression vector, int n);
224228
/**
@@ -228,6 +232,7 @@ public interface VecExpr {
228232
* @param vector The input vector to truncate. (of <a href="{@docRoot}/doc-files/types/vec_vector.html">vec:vector</a>)
229233
* @param n The numbers of decimal places to truncate to. The default is 0. Negative values cause that many digits to the left of the decimal point to be truncated. (of <a href="{@docRoot}/doc-files/types/xs_int.html">xs:int</a>)
230234
* @return a server expression with the <a href="{@docRoot}/doc-files/types/vec_vector.html">vec:vector</a> server data type
235+
* @since 8.1.0; requires MarkLogic 12.1 or higher.
231236
*/
232237
public ServerExpression trunc(ServerExpression vector, ServerExpression n);
233238
/**
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
@SuppressWarnings("unchecked")
14+
class ColumnBuilderImpl extends BaseTypeImpl.BaseCallImpl implements PlanColumnBuilder, BaseTypeImpl.BaseArgImpl {
15+
16+
ColumnBuilderImpl() {
17+
super("op", "suboperators",
18+
new BaseTypeImpl.BaseArgImpl[]{
19+
new BaseTypeImpl.BaseCallImpl("op", "column-builder", new BaseTypeImpl.BaseArgImpl[]{})
20+
});
21+
}
22+
23+
private ColumnBuilderImpl(List<BaseTypeImpl.BaseArgImpl> args) {
24+
super("op", "suboperators", args.toArray(new BaseTypeImpl.BaseArgImpl[]{}));
25+
}
26+
27+
public PlanColumnBuilder addColumn(String name) {
28+
return addArg("add-column", name);
29+
}
30+
31+
public PlanColumnBuilder xpath(String path) {
32+
return addArg("xpath", path);
33+
}
34+
35+
public PlanColumnBuilder type(String type) {
36+
return addArg("type", type);
37+
}
38+
39+
public PlanColumnBuilder nullable(boolean nullable) {
40+
return addArg("nullable", new XsValueImpl.BooleanValImpl(nullable));
41+
}
42+
43+
public PlanColumnBuilder expr(ServerExpression expression) {
44+
return addArg("expr", expression);
45+
}
46+
47+
public PlanColumnBuilder defaultValue(String value) {
48+
return addArg("default", value);
49+
}
50+
51+
public PlanColumnBuilder collation(String collation) {
52+
return addArg("collation", collation);
53+
}
54+
55+
public PlanColumnBuilder dimension(int dimension) {
56+
return addArg("dimension", dimension);
57+
}
58+
59+
public PlanColumnBuilder coordinateSystem(String coordinateSystem) {
60+
return addArg("coordinate-system", coordinateSystem);
61+
}
62+
63+
private PlanColumnBuilder addArg(String functionName, Object... args) {
64+
BaseTypeImpl.BaseArgImpl newArg = new BaseTypeImpl.BaseCallImpl(
65+
"op", functionName, makeArgs(args)
66+
);
67+
List<BaseTypeImpl.BaseArgImpl> newArgs = new ArrayList<>();
68+
newArgs.addAll(Arrays.asList(getArgsImpl()));
69+
newArgs.add(newArg);
70+
return new ColumnBuilderImpl(newArgs);
71+
}
72+
73+
private BaseTypeImpl.BaseArgImpl[] makeArgs(Object... args) {
74+
List<BaseTypeImpl.BaseArgImpl> argList = new ArrayList<>();
75+
for (Object arg : args) {
76+
if (arg instanceof BaseTypeImpl.BaseArgImpl) {
77+
argList.add((BaseTypeImpl.BaseArgImpl) arg);
78+
} else {
79+
// Use Literal for plain values (strings, numbers, etc.)
80+
argList.add(new BaseTypeImpl.Literal(arg));
81+
}
82+
}
83+
return argList.toArray(new BaseTypeImpl.BaseArgImpl[]{});
84+
}
85+
}

0 commit comments

Comments
 (0)