>From Preetham Poluparthi <[email protected]>:

Preetham Poluparthi has uploaded this change for review. ( 
https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/20167 )


Change subject: Test init
......................................................................

Test init

Change-Id: I8661de95031f495de0a0ec43d6f9a0d791bab8b8
---
A 
asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/AdvisorAnalyzingTestExecutor.java
A 
asterixdb/asterix-app/src/test/java/org/apache/asterix/test/runtime/SqlppAdvisorAnalyzedExecutionTest.java
2 files changed, 237 insertions(+), 0 deletions(-)



  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb 
refs/changes/67/20167/1

diff --git 
a/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/AdvisorAnalyzingTestExecutor.java
 
b/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/AdvisorAnalyzingTestExecutor.java
new file mode 100644
index 0000000..676cce3
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/AdvisorAnalyzingTestExecutor.java
@@ -0,0 +1,138 @@
+/*
+ * 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.test.common;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.asterix.testframework.context.TestCaseContext;
+import org.apache.asterix.testframework.xml.TestCase;
+import org.apache.commons.io.IOUtils;
+
+import java.io.InputStream;
+import java.net.URI;
+import java.nio.charset.Charset;
+import java.util.List;
+import java.util.function.Predicate;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+public class AdvisorAnalyzingTestExecutor extends TestExecutor {
+    private Pattern loadPattern = 
Pattern.compile("(load)\\s+(dataset|collection)\\s+([a-zA-z0-9\\.`]+)\\s+using",
+            Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
+    private Pattern upsertPattern = 
Pattern.compile("^(upsert|insert)\\s+into\\s+([a-zA-z0-9\\.`]+)\\s*(\\(|as)?",
+            Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
+    private Pattern usePattern = 
Pattern.compile("use\\s+(dataverse\\s+)?([a-zA-z0-9\\.`]+)\\s*;",
+            Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
+
+    private Pattern queryPattern = Pattern.compile("select\\s+.*",
+            Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
+
+    public AdvisorAnalyzingTestExecutor() {
+        super("results_index_advisor");
+    }
+
+    @Override
+    public ExtractedResult executeSqlppUpdateOrDdl(String statement, 
TestCaseContext.OutputFormat outputFormat)
+            throws Exception {
+        Matcher dvMatcher = usePattern.matcher(statement);
+        String dv = "";
+        if (dvMatcher.find()) {
+            dv = dvMatcher.group(2) + ".";
+        }
+        Matcher dsMatcher = loadPattern.matcher(statement);
+        Matcher upsertMatcher = upsertPattern.matcher(statement);
+        ExtractedResult res = super.executeUpdateOrDdl(statement, 
outputFormat, getQueryServiceUri(SQLPP));
+        analyzeFromRegex(dsMatcher, dv, 3);
+        analyzeFromRegex(upsertMatcher, dv, 2);
+        return res;
+    }
+
+    @Override
+    public ExtractedResult executeSqlppUpdateOrDdl(String statement, 
TestCaseContext.OutputFormat outputFormat,
+                                                   TestCase.CompilationUnit 
cUnit) throws Exception {
+        Matcher dvMatcher = usePattern.matcher(statement);
+        String dv = "";
+        if (dvMatcher.find()) {
+            dv = dvMatcher.group(2) + ".";
+        }
+        Matcher dsMatcher = loadPattern.matcher(statement);
+        Matcher upsertMatcher = upsertPattern.matcher(statement);
+        ExtractedResult res = super.executeUpdateOrDdl(statement, 
outputFormat, getQueryServiceUri(SQLPP), cUnit);
+        analyzeFromRegex(dsMatcher, dv, 3);
+        analyzeFromRegex(upsertMatcher, dv, 2);
+        return res;
+    }
+
+    private void analyzeFromRegex(Matcher m, String dv, int pos) throws 
Exception {
+        while (m.find()) {
+            String ds = m.group(pos);
+            StringBuilder analyzeStmt = new StringBuilder();
+            analyzeStmt.append("ANALYZE DATASET ");
+            if (!ds.contains(".")) {
+                analyzeStmt.append(dv);
+            }
+            analyzeStmt.append(ds);
+            analyzeStmt.append(" WITH {\"sample-seed\": \"1000\"}");
+            analyzeStmt.append(";");
+            InputStream resultStream = 
executeQueryService(analyzeStmt.toString(), getQueryServiceUri(SQLPP),
+                    TestCaseContext.OutputFormat.CLEAN_JSON);
+            String resultStr = IOUtils.toString(resultStream, UTF_8);
+            JsonNode result = RESULT_NODE_READER.<ObjectNode> 
readValue(resultStr).get("status");
+            if (!"success".equals(result.asText())) {
+                JsonNode error = RESULT_NODE_READER.<ObjectNode> 
readValue(resultStr).get("errors");
+                throw new IllegalStateException("ANALYZE DATASET failed with 
error: " + error);
+            }
+        }
+    }
+
+    @Override
+    public InputStream executeQueryService(String str, 
TestCaseContext.OutputFormat fmt, URI uri, 
List<TestCase.CompilationUnit.Parameter> params,
+                                           boolean jsonEncoded, Charset 
responseCharset) throws Exception {
+        Matcher queryMatcher = queryPattern.matcher(str);
+        if (queryMatcher.find()) {
+            return super.executeQueryService(addAdvisePrefix(str), fmt, uri, 
params, jsonEncoded, responseCharset);
+        }
+        else {
+            return super.executeQueryService(str, fmt, uri, params, 
jsonEncoded, responseCharset);
+        }
+    }
+
+    public String addAdvisePrefix(String str) {
+            return "ADVISE " + str;
+        }
+
+
+    public InputStream executeQueryService(String str, 
TestCaseContext.OutputFormat fmt, URI uri, 
List<TestCase.CompilationUnit.Parameter> params,
+                                           
List<TestCase.CompilationUnit.Placeholder> placeholders, boolean jsonEncoded, 
Charset responseCharset,
+                                           Predicate<Integer> 
responseCodeValidator, boolean cancellable) throws Exception {
+
+
+        Matcher queryMatcher = queryPattern.matcher(str);
+        if (queryMatcher.find()) {
+            return super.executeQueryService(addAdvisePrefix(str), fmt, uri, 
params, placeholders, jsonEncoded,
+                    responseCharset, responseCodeValidator, cancellable);
+        } else {
+            return super.executeQueryService(str, fmt, uri, params, 
placeholders, jsonEncoded,
+                    responseCharset, responseCodeValidator, cancellable);
+        }
+    }
+
+}
diff --git 
a/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/runtime/SqlppAdvisorAnalyzedExecutionTest.java
 
b/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/runtime/SqlppAdvisorAnalyzedExecutionTest.java
new file mode 100644
index 0000000..d32db0c
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/runtime/SqlppAdvisorAnalyzedExecutionTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.test.runtime;
+
+import org.apache.asterix.common.api.INcApplicationContext;
+import org.apache.asterix.test.common.AdvisorAnalyzingTestExecutor;
+import org.apache.asterix.test.common.TestExecutor;
+import org.apache.asterix.testframework.context.TestCaseContext;
+import org.apache.hyracks.control.nc.NodeControllerService;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+@RunWith(Parameterized.class)
+public class SqlppAdvisorAnalyzedExecutionTest {
+    protected static final String TEST_CONFIG_FILE_NAME = 
"src/test/resources/cc-analyze.conf";
+    private final String[] denyList = { "synonym: synonym-01", "ddl: 
analyze-dataset-1", "misc: dump_index",
+            "array-index: composite-index-queries", "filters: upsert", 
"column: analyze-dataset",
+            "column: filter/boolean", "column: filter/sql-compat", "ddl: 
analyze-dataset-with-indexes",
+            "warnings: cardinality-hint-warning", "comparison: 
incomparable_types" };
+
+    @BeforeClass
+    public static void setUp() throws Exception {
+        final TestExecutor testExecutor = new AdvisorAnalyzingTestExecutor();
+        LangExecutionUtil.setUp(TEST_CONFIG_FILE_NAME, testExecutor);
+        setNcEndpoints(testExecutor);
+    }
+
+    @AfterClass
+    public static void tearDown() throws Exception {
+        LangExecutionUtil.tearDown();
+    }
+
+    @Parameterized.Parameters(name = "SqlppAdvisorAnalyzedExecutionTest 
{index}: {0}")
+    public static Collection<Object[]> tests() throws Exception {
+        return LangExecutionUtil.tests("only_sqlpp.xml", 
"testsuite_sqlpp.xml");
+    }
+
+    protected TestCaseContext tcCtx;
+
+    public SqlppAdvisorAnalyzedExecutionTest(TestCaseContext tcCtx) {
+        this.tcCtx = tcCtx;
+    }
+
+    @Test
+    public void test() throws Exception {
+        if (!Arrays.stream(denyList).anyMatch(s -> 
tcCtx.toString().contains(s))) {
+            LangExecutionUtil.test(tcCtx);
+        }
+    }
+
+    private static void setNcEndpoints(TestExecutor testExecutor) {
+        final NodeControllerService[] ncs = 
ExecutionTestUtil.integrationUtil.ncs;
+        final Map<String, InetSocketAddress> ncEndPoints = new HashMap<>();
+        final String ip = InetAddress.getLoopbackAddress().getHostAddress();
+        for (NodeControllerService nc : ncs) {
+            final String nodeId = nc.getId();
+            final INcApplicationContext appCtx = (INcApplicationContext) 
nc.getApplicationContext();
+            int apiPort = appCtx.getExternalProperties().getNcApiPort();
+            ncEndPoints.put(nodeId, InetSocketAddress.createUnresolved(ip, 
apiPort));
+        }
+        testExecutor.setNcEndPoints(ncEndPoints);
+    }
+
+}

--
To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/20167
To unsubscribe, or for help writing mail filters, visit 
https://asterix-gerrit.ics.uci.edu/settings

Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Change-Id: I8661de95031f495de0a0ec43d6f9a0d791bab8b8
Gerrit-Change-Number: 20167
Gerrit-PatchSet: 1
Gerrit-Owner: Preetham Poluparthi <[email protected]>
Gerrit-MessageType: newchange

Reply via email to