MCPcopy Create free account
hub / github.com/BaseXdb/basex / opt

Method opt

basex-core/src/main/java/org/basex/query/expr/Lookup.java:76–117  ·  view source on GitHub ↗

Rewrites the lookup to another expression. @param cc compilation context @return optimized or original expression @throws QueryException query exception

(final CompileContext cc)

Source from the content-addressed store, hash-verified

74 * @throws QueryException query exception
75 */
76 private Expr opt(final CompileContext cc) throws QueryException {
77 final Expr input = exprs[0], keys = exprs[1];
78 final long is = input.size();
79 final long ks = keys.seqType().mayBeWrapped() || keys.has(Flag.NDT) ? -1 : keys.size();
80 if(ks == 0) return keys;
81
82 final Type it = input.seqType().type;
83 final boolean map = it instanceof MapType, array = it instanceof ArrayType;
84 if(map || array) {
85 /* REWRITE LOOKUP:
86 * MAP?* → map:items(MAP)
87 * ARRAY?* → array:items(MAP)
88 * MAP?KEY → map:get(INPUT, KEY)
89 * ARRAY?KEY → array:get(INPUT, KEY) */
90 final QueryBiFunction<Expr, Expr, Expr> rewrite = (in, arg) -> keys == WILDCARD ?
91 cc.function(map ? Function._MAP_ITEMS : Function._ARRAY_ITEMS, info, in) :
92 cc.function(map ? Function._MAP_GET : Function._ARRAY_GET, info, in, arg);
93
94 // single key
95 if(ks == 1) {
96 // single input: INPUT?KEY → REWRITE(INPUT, KEY)
97 if(is == 1) return rewrite.apply(input, keys);
98 // multiple inputs: INPUTS?KEY → INPUTS ! REWRITE(., KEY)
99 return SimpleMap.get(cc, info, input,
100 cc.get(input, true, () -> rewrite.apply(ContextValue.get(cc, info), keys)));
101 }
102
103 // multiple deterministic keys, inputs are values or variable references
104 if(ks != -1 && (input instanceof Value || input instanceof VarRef)) {
105 // single input: INPUT?KEYS → KEYS ! REWRITE(INPUT, .)
106 if(is == 1) return SimpleMap.get(cc, info, keys,
107 cc.get(keys, true, () -> rewrite.apply(input, ContextValue.get(cc, info))));
108 // multiple inputs: INPUT?KEYS → for $item in INPUT return KEYS ! REWRITE($item, .)
109 final FLWORBuilder flwor = new FLWORBuilder(1, cc, info);
110 final Expr next = cc.get(keys, true, () ->
111 rewrite.apply(flwor.ref(flwor.item), ContextValue.get(cc, info)));
112 final Expr rtrn = SimpleMap.get(cc, info, keys, next);
113 return flwor.finish(input, null, rtrn);
114 }
115 }
116 return this;
117 }
118
119 @Override
120 public Iter iter(final QueryContext qc) throws QueryException {

Callers 1

optimizeMethod · 0.95

Calls 11

sizeMethod · 0.95
seqTypeMethod · 0.95
hasMethod · 0.95
getMethod · 0.95
getMethod · 0.95
refMethod · 0.95
finishMethod · 0.95
mayBeWrappedMethod · 0.80
applyMethod · 0.65
getMethod · 0.65
functionMethod · 0.45

Tested by

no test coverage detected