Using the <local> and <global> Escape Syntax with Nested Queries

A query that is nested within a data-aware procedure may be executed on local member data, or it may be re-distributed to the GemFire XD cluster and executed on multiple GemFire XD members. The <local> and <global> escape syntax can be used to override the default behavior for nested queries.

If a data-aware procedure is invoked without specifying a WHERE clause in the CALL statement, then by default GemFire XD treats a nested query in the procedure implementation as a "global" query. This means that the nested query is distributed to the GemFire XD cluster and accesses table data from all GemFire XD members that host data for the table. Note that global queries can return duplicate results with a single procedure call, because each member that executes the procedure body invokes a separate global query.

If a procedure is invoked using a WHERE clause, then by default GemFire XD treats the nested query in the procedure implementation as a "local" query. This means that the query only accesses the local partitioned table data that is assigned to the procedure on the GemFire XD member where the procedure executes.

You can override this default behavior by specifying either the "<local>" or "<global>" escape syntax at the very beginning of a nested query string in your procedure implementation.

For example, the following nested queries always operate with local table data, even you invoke the procedure without specifying a WHERE clause:
Connection cxn = context.getConnection();
Statement stmt = cxn.createStatement();
resultSet1[0] = stmt.executeQuery("<local> select * from Bar where foo > 42");
resultSet2[0] = stmt.executeQuery("<local> select * from Bar where foo <= 42");
Note: When you use the ON TABLE clause to invoke a procedure, any <local> queries within the procedure body target only the primary data for a partitioned table on the local node. For replicated tables, a local query accesses only a single replica. In both cases, this avoids returning duplicate values in the procedure.

When you use ON ALL to invoke a procedure, <local> queries execute only the local member for partitioned table data. However, <local> queries against replicated tables return duplicate results.