You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@asterixdb.apache.org by AsterixDB Code Review <do...@asterix-gerrit.ics.uci.edu> on 2026/07/09 11:52:03 UTC
[L] Change in asterixdb[master]: [ASTERIXDB-3183][COMP] Add query plan cache
From Rithwik Koul <ri...@couchbase.com>:
Rithwik Koul has uploaded this change for review. ( https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/21412?usp=email )
Change subject: [ASTERIXDB-3183][COMP] Add query plan cache
......................................................................
[ASTERIXDB-3183][COMP] Add query plan cache
- user model changes: yes
- storage format changes: no
- interface changes: yes
Description: Parameterized queries can be utilized now when the value of the parameters are fixed.
Ex-ref: MB-72415
Change-Id: I126d55e391b7ba9c8212299a9645143ae0b70a75
---
M asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
M asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/RequestParameters.java
A asterixdb/asterix-app/src/test/java/org/apache/asterix/app/translator/RequestParametersCanonicalizeTest.java
M asterixdb/asterix-app/src/test/resources/runtimets/api.xml
A asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.001.delete.http
A asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.002.post.http
A asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.003.post.http
A asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.004.post.http
A asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.005.post.http
A asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.001.regexjson
A asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.002.regexjson
A asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.003.regexjson
A asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.004.regexjson
A asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.005.regexjson
M asterixdb/asterix-common/src/main/java/org/apache/asterix/common/cache/IQueryPlanCacheKey.java
M asterixdb/asterix-common/src/main/java/org/apache/asterix/common/cache/QueryPlanCacheKey.java
A asterixdb/asterix-common/src/test/java/org/apache/asterix/common/cache/QueryPlanCacheKeyTest.java
17 files changed, 375 insertions(+), 6 deletions(-)
git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/12/21412/1
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
index bf3d0fd..8a6f123 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
@@ -5763,18 +5763,18 @@
boolean isPlanCacheEnabled =
metadataProvider.getBooleanProperty(CompilerProperties.COMPILER_QUERY_PLAN_CACHE_KEY,
appCtx.getCompilerProperties().isQueryPlanCacheEnabled());
- return !isPlanCacheEnabled || requestParameters.isSkipQueryPlanCache()
- || requestParameters.getStatementParameters() != null;
+ return !isPlanCacheEnabled || requestParameters.isSkipQueryPlanCache();
}
private IQueryPlanCacheKey resolveQueryPlanCacheKey(MetadataProvider metadataProvider,
- IRequestParameters requestParameters, ClientRequest clientRequest) {
+ IRequestParameters requestParameters, ClientRequest clientRequest) throws HyracksDataException {
if (isIgnoreCache(metadataProvider, requestParameters)) {
return null;
}
final QueryPlanCacheKey baseKey = new QueryPlanCacheKey(requestParameters.getStatement(),
sessionConfig.isOptimize(), metadataProvider.getConfig(), sessionConfig.getMaxWarnings(),
- metadataProvider.getResultSetId().getId(), metadataProvider.getDefaultNamespace());
+ metadataProvider.getResultSetId().getId(), metadataProvider.getDefaultNamespace(),
+ RequestParameters.canonicalizeParameterValues(requestParameters.getStatementParameters()));
return buildQueryPlanCacheKey(baseKey, clientRequest);
}
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/RequestParameters.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/RequestParameters.java
index d1ec0b6..52d3e61 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/RequestParameters.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/RequestParameters.java
@@ -21,6 +21,7 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.HashMap;
+import java.util.HexFormat;
import java.util.Map;
import org.apache.asterix.common.api.IRequestReference;
@@ -261,6 +262,34 @@
return m;
}
+ /**
+ * Canonicalizes statement parameter values into a stable, value-comparable form for the query plan cache key.
+ * Each value is serialized with the type-tagged ANY serde (a leading {@link org.apache.asterix.om.types.ATypeTag}
+ * byte then the payload) and hex-encoded, so distinct types with equal payloads stay distinct and equal values
+ * compare equal via {@link String#equals} (which {@link IAObject} does not provide).
+ *
+ * @param inParams parameter name to boxed typed value, or {@code null}
+ * @return parameter name to type-tagged serialized-hex, or {@code null} when there are no parameters
+ */
+ public static Map<String, String> canonicalizeParameterValues(Map<String, IAObject> inParams)
+ throws HyracksDataException {
+ if (inParams == null || inParams.isEmpty()) {
+ return null;
+ }
+ ISerializerDeserializer serDe =
+ SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ANY);
+ ByteArrayAccessibleOutputStream buffer = new ByteArrayAccessibleOutputStream();
+ DataOutputStream bufferDataOutput = new DataOutputStream(buffer);
+ HexFormat hex = HexFormat.of();
+ Map<String, String> m = new HashMap<>();
+ for (Map.Entry<String, IAObject> me : inParams.entrySet()) {
+ buffer.reset();
+ serDe.serialize(me.getValue(), bufferDataOutput);
+ m.put(me.getKey(), hex.formatHex(buffer.toByteArray()));
+ }
+ return m;
+ }
+
public static int getStatementCategoryRestrictionMask(boolean readOnly) {
return readOnly ? Statement.Category.QUERY : NO_CATEGORY_RESTRICTION_MASK;
}
diff --git a/asterixdb/asterix-app/src/test/java/org/apache/asterix/app/translator/RequestParametersCanonicalizeTest.java b/asterixdb/asterix-app/src/test/java/org/apache/asterix/app/translator/RequestParametersCanonicalizeTest.java
new file mode 100644
index 0000000..cd97e9b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/java/org/apache/asterix/app/translator/RequestParametersCanonicalizeTest.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.asterix.app.translator;
+
+import java.util.Map;
+
+import org.apache.asterix.common.cache.QueryPlanCacheKey;
+import org.apache.asterix.common.metadata.DataverseName;
+import org.apache.asterix.common.metadata.Namespace;
+import org.apache.asterix.om.base.AInt64;
+import org.apache.asterix.om.base.AString;
+import org.apache.asterix.om.base.IAObject;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link RequestParameters#canonicalizeParameterValues}, verifying that type-tagged serialization keeps
+ * distinct parameter values (including equal payloads of different types) in distinct {@link QueryPlanCacheKey} slots.
+ */
+public class RequestParametersCanonicalizeTest {
+
+ private static final String QUERY = "SELECT $v AS v;";
+ private static final Namespace NAMESPACE =
+ new Namespace("Default", DataverseName.createBuiltinDataverseName("Default"));
+
+ private static QueryPlanCacheKey keyFor(Map<String, IAObject> params) throws Exception {
+ return new QueryPlanCacheKey(QUERY, true, Map.of(), 0L, 1L, NAMESPACE,
+ RequestParameters.canonicalizeParameterValues(params));
+ }
+
+ @Test
+ public void equalValuesCanonicalizeEqually() throws Exception {
+ Map<String, String> a = RequestParameters.canonicalizeParameterValues(Map.of("v", new AInt64(5)));
+ Map<String, String> b = RequestParameters.canonicalizeParameterValues(Map.of("v", new AInt64(5)));
+ Assert.assertEquals(a, b);
+ }
+
+ @Test
+ public void typeDistinctValuesCanonicalizeDistinctly() throws Exception {
+ // the leading type-tag byte separates AInt64(5) from AString("5") even though their text renders the same
+ Map<String, String> asInt = RequestParameters.canonicalizeParameterValues(Map.of("v", new AInt64(5)));
+ Map<String, String> asString = RequestParameters.canonicalizeParameterValues(Map.of("v", new AString("5")));
+ Assert.assertNotEquals(asInt, asString);
+ Assert.assertNotEquals(keyFor(Map.of("v", new AInt64(5))), keyFor(Map.of("v", new AString("5"))));
+ }
+
+ @Test
+ public void differingIntValuesGiveDistinctKeys() throws Exception {
+ Assert.assertNotEquals(keyFor(Map.of("v", new AInt64(5))), keyFor(Map.of("v", new AInt64(6))));
+ }
+
+ @Test
+ public void noParamsCanonicalizeToNull() throws Exception {
+ Assert.assertNull(RequestParameters.canonicalizeParameterValues(null));
+ Assert.assertNull(RequestParameters.canonicalizeParameterValues(Map.of()));
+ }
+}
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/api.xml b/asterixdb/asterix-app/src/test/resources/runtimets/api.xml
index a98103c..d45eb93 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/api.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/api.xml
@@ -134,5 +134,10 @@
<output-dir compare="Text">plan_cache_clear</output-dir>
</compilation-unit>
</test-case>
+ <test-case FilePath="api">
+ <compilation-unit name="plan_cache_parameterized">
+ <output-dir compare="Text">plan_cache_parameterized</output-dir>
+ </compilation-unit>
+ </test-case>
</test-group>
</test-suite>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.001.delete.http b/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.001.delete.http
new file mode 100644
index 0000000..157ceeb
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.001.delete.http
@@ -0,0 +1,19 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/admin/plancache
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.002.post.http b/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.002.post.http
new file mode 100644
index 0000000..fe2d4bd
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.002.post.http
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/query/service
+--body={"statement": "SELECT $pcv AS v;","$pcv": 1}
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.003.post.http b/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.003.post.http
new file mode 100644
index 0000000..fe2d4bd
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.003.post.http
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/query/service
+--body={"statement": "SELECT $pcv AS v;","$pcv": 1}
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.004.post.http b/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.004.post.http
new file mode 100644
index 0000000..f6a20d7
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.004.post.http
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/query/service
+--body={"statement": "SELECT $pcv AS v;","$pcv": 2}
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.005.post.http b/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.005.post.http
new file mode 100644
index 0000000..f6a20d7
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/api/plan_cache_parameterized/plan_cache_parameterized.005.post.http
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/query/service
+--body={"statement": "SELECT $pcv AS v;","$pcv": 2}
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.001.regexjson b/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.001.regexjson
new file mode 100644
index 0000000..2dc0713
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.001.regexjson
@@ -0,0 +1 @@
+{"status":"R{[0-9]+ query plan cache entries have been cleared\\.}"}
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.002.regexjson b/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.002.regexjson
new file mode 100644
index 0000000..f5283b0
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.002.regexjson
@@ -0,0 +1,20 @@
+{
+ "requestID": "R{.*}",
+ "signature": {
+ "*": "*"
+ },
+ "type": "application/x-adm",
+ "results": [ "{ \"v\": 1 }" ],
+ "plans": "R{.*}",
+ "status": "success",
+ "metrics": {
+ "elapsedTime": "R{.*}",
+ "executionTime": "R{.*}",
+ "compileTime": "R{.*}",
+ "queueWaitTime": "R{.*}",
+ "resultCount": 1,
+ "resultSize": "R{.*}",
+ "cachedPlan": "false",
+ "processedObjects": 0
+ }
+}
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.003.regexjson b/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.003.regexjson
new file mode 100644
index 0000000..e5f686f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.003.regexjson
@@ -0,0 +1,20 @@
+{
+ "requestID": "R{.*}",
+ "signature": {
+ "*": "*"
+ },
+ "type": "application/x-adm",
+ "results": [ "{ \"v\": 1 }" ],
+ "plans": "R{.*}",
+ "status": "success",
+ "metrics": {
+ "elapsedTime": "R{.*}",
+ "executionTime": "R{.*}",
+ "compileTime": "R{.*}",
+ "queueWaitTime": "R{.*}",
+ "resultCount": 1,
+ "resultSize": "R{.*}",
+ "cachedPlan": "true",
+ "processedObjects": 0
+ }
+}
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.004.regexjson b/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.004.regexjson
new file mode 100644
index 0000000..7da5892
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.004.regexjson
@@ -0,0 +1,20 @@
+{
+ "requestID": "R{.*}",
+ "signature": {
+ "*": "*"
+ },
+ "type": "application/x-adm",
+ "results": [ "{ \"v\": 2 }" ],
+ "plans": "R{.*}",
+ "status": "success",
+ "metrics": {
+ "elapsedTime": "R{.*}",
+ "executionTime": "R{.*}",
+ "compileTime": "R{.*}",
+ "queueWaitTime": "R{.*}",
+ "resultCount": 1,
+ "resultSize": "R{.*}",
+ "cachedPlan": "false",
+ "processedObjects": 0
+ }
+}
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.005.regexjson b/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.005.regexjson
new file mode 100644
index 0000000..87f5771
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/api/plan_cache_parameterized/plan_cache_parameterized.005.regexjson
@@ -0,0 +1,20 @@
+{
+ "requestID": "R{.*}",
+ "signature": {
+ "*": "*"
+ },
+ "type": "application/x-adm",
+ "results": [ "{ \"v\": 2 }" ],
+ "plans": "R{.*}",
+ "status": "success",
+ "metrics": {
+ "elapsedTime": "R{.*}",
+ "executionTime": "R{.*}",
+ "compileTime": "R{.*}",
+ "queueWaitTime": "R{.*}",
+ "resultCount": 1,
+ "resultSize": "R{.*}",
+ "cachedPlan": "true",
+ "processedObjects": 0
+ }
+}
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/cache/IQueryPlanCacheKey.java b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/cache/IQueryPlanCacheKey.java
index c2e882f..cf5388d 100644
--- a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/cache/IQueryPlanCacheKey.java
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/cache/IQueryPlanCacheKey.java
@@ -57,4 +57,10 @@
* @return the namespace that unqualified names resolve against
*/
Namespace defaultNamespace();
+
+ /**
+ * @return statement parameter values, each canonically (type-tagged) serialized, so a plan compiled for one set of
+ * argument values is reused only by requests supplying identical values
+ */
+ Map<String, String> statementParameters();
}
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/cache/QueryPlanCacheKey.java b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/cache/QueryPlanCacheKey.java
index 6b73d2a..debac00 100644
--- a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/cache/QueryPlanCacheKey.java
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/cache/QueryPlanCacheKey.java
@@ -33,12 +33,16 @@
* @param maxWarnings
* @param resultSetId
* @param defaultNamespace
+ * @param statementParameters
*/
public record QueryPlanCacheKey(String queryString, boolean optimize, Map<String, Object> config, long maxWarnings,
- long resultSetId, Namespace defaultNamespace) implements IQueryPlanCacheKey {
+ long resultSetId, Namespace defaultNamespace, Map<String, String> statementParameters)
+ implements IQueryPlanCacheKey {
public QueryPlanCacheKey {
- // defensive copy so the key stays stable if the source map is later mutated
+ // defensive copies so the key stays stable if the source maps are later mutated
config = Collections.unmodifiableMap(new HashMap<>(config));
+ statementParameters = statementParameters == null ? Map.of()
+ : Collections.unmodifiableMap(new HashMap<>(statementParameters));
}
}
diff --git a/asterixdb/asterix-common/src/test/java/org/apache/asterix/common/cache/QueryPlanCacheKeyTest.java b/asterixdb/asterix-common/src/test/java/org/apache/asterix/common/cache/QueryPlanCacheKeyTest.java
new file mode 100644
index 0000000..898e082
--- /dev/null
+++ b/asterixdb/asterix-common/src/test/java/org/apache/asterix/common/cache/QueryPlanCacheKeyTest.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.asterix.common.cache;
+
+import java.util.Map;
+
+import org.apache.asterix.common.metadata.DataverseName;
+import org.apache.asterix.common.metadata.Namespace;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link QueryPlanCacheKey}, focused on how statement parameter values participate in cache identity:
+ * a plan compiled for one set of argument values is reused only by requests supplying identical values.
+ */
+public class QueryPlanCacheKeyTest {
+
+ private static final String QUERY = "SELECT $v AS v;";
+ private static final boolean OPTIMIZE = true;
+ private static final long MAX_WARNINGS = 0L;
+ private static final long RESULT_SET_ID = 1L;
+ private static final Namespace NAMESPACE =
+ new Namespace("Default", DataverseName.createBuiltinDataverseName("Default"));
+
+ private static QueryPlanCacheKey keyWithParams(Map<String, String> statementParameters) {
+ return new QueryPlanCacheKey(QUERY, OPTIMIZE, Map.of(), MAX_WARNINGS, RESULT_SET_ID, NAMESPACE,
+ statementParameters);
+ }
+
+ @Test
+ public void equalParamsGiveEqualKeys() {
+ QueryPlanCacheKey a = keyWithParams(Map.of("v", "aa"));
+ QueryPlanCacheKey b = keyWithParams(Map.of("v", "aa"));
+ Assert.assertEquals(a, b);
+ Assert.assertEquals(a.hashCode(), b.hashCode());
+ }
+
+ @Test
+ public void differingParamValueGivesDistinctKey() {
+ // same query, same everything but one parameter's canonical value => a separate cache slot (a MISS)
+ QueryPlanCacheKey a = keyWithParams(Map.of("v", "aa"));
+ QueryPlanCacheKey b = keyWithParams(Map.of("v", "bb"));
+ Assert.assertNotEquals(a, b);
+ }
+
+ @Test
+ public void nullParamsNormalizeToEmpty() {
+ // null and no-parameter requests are intentionally the same cache slot
+ QueryPlanCacheKey nullParams = keyWithParams(null);
+ Assert.assertEquals(Map.of(), nullParams.statementParameters());
+ Assert.assertEquals(nullParams, keyWithParams(Map.of()));
+ Assert.assertEquals(nullParams.hashCode(), keyWithParams(Map.of()).hashCode());
+ }
+}
--
To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/21412?usp=email
To unsubscribe, or for help writing mail filters, visit https://asterix-gerrit.ics.uci.edu/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Change-Id: I126d55e391b7ba9c8212299a9645143ae0b70a75
Gerrit-Change-Number: 21412
Gerrit-PatchSet: 1
Gerrit-Owner: Rithwik Koul <ri...@couchbase.com>