Skip to content

Commit

Permalink
simple ppl query works
Browse files Browse the repository at this point in the history
Signed-off-by: Peng Huo <[email protected]>
  • Loading branch information
penghuo committed Oct 21, 2024
1 parent 52cbf9b commit 52d7fe0
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
package org.opensearch.sql.calcite;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.calcite.jdbc.CalciteConnection;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.schema.SchemaPlus;
Expand All @@ -21,8 +26,13 @@
import org.apache.calcite.tools.RelRunner;
import org.opensearch.client.node.NodeClient;
import org.opensearch.sql.analysis.AnalysisContext;
import org.opensearch.sql.ast.expression.DataType;
import org.opensearch.sql.ast.tree.UnresolvedPlan;
import org.opensearch.sql.common.response.ResponseListener;
import org.opensearch.sql.data.model.ExprStringValue;
import org.opensearch.sql.data.model.ExprValueUtils;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.data.type.ExprType;
import org.opensearch.sql.executor.ExecutionContext;
import org.opensearch.sql.executor.ExecutionEngine;
import org.opensearch.sql.opensearch.client.OpenSearchClient;
Expand Down Expand Up @@ -80,13 +90,17 @@ public void execute(UnresolvedPlan plan, ExecutionContext context,
RelNode relNode = analyzer.relBuilder.build();
RelRunner runner = connection.unwrap(RelRunner.class);

List<String> result = new ArrayList<>();
try (ResultSet resultSet = runner.prepareStatement(relNode).executeQuery()) {
while (resultSet.next()) {
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println("Name: " + name + ", Age: " + age);
}
new CalciteHelper.ResultSetFormatter().toStringList(resultSet, result);
}
Schema schema =
new Schema(ImmutableList.of(new Schema.Column("_MAP", "_MAP",
ExprCoreType.STRING)));
QueryResponse queryResponse = new QueryResponse(schema,
result.stream().map(s -> ExprValueUtils.tupleValue(ImmutableMap.of("_MAP", s
))).collect(Collectors.toList()), null);
listener.onResponse(queryResponse);
return null;
} catch (SQLException e) {
throw new RuntimeException(e);
Expand Down
121 changes: 121 additions & 0 deletions opensearch/src/main/java/org/opensearch/sql/calcite/CalciteHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.calcite;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CalciteHelper {

/** Matches a number with at least four zeros after the point. */
private static final Pattern TRAILING_ZERO_PATTERN =
Pattern.compile("-?[0-9]+\\.([0-9]*[1-9])?(00000*[0-9][0-9]?)");

/** Matches a number with at least four nines after the point. */
private static final Pattern TRAILING_NINE_PATTERN =
Pattern.compile("-?[0-9]+\\.([0-9]*[0-8])?(99999*[0-9][0-9]?)");

/** Converts a {@link ResultSet} to string. */
static class ResultSetFormatter {
final StringBuilder buf = new StringBuilder();

public ResultSetFormatter resultSet(ResultSet resultSet)
throws SQLException {
final ResultSetMetaData metaData = resultSet.getMetaData();
while (resultSet.next()) {
rowToString(resultSet, metaData);
buf.append("\n");
}
return this;
}

/** Converts one row to a string. */
ResultSetFormatter rowToString(ResultSet resultSet,
ResultSetMetaData metaData) throws SQLException {
int n = metaData.getColumnCount();
if (n > 0) {
for (int i = 1;; i++) {
buf.append(metaData.getColumnLabel(i))
.append("=")
.append(adjustValue(resultSet.getString(i)));
if (i == n) {
break;
}
buf.append("; ");
}
}
return this;
}

protected String adjustValue(String string) {
if (string != null) {
string = correctRoundedFloat(string);
}
return string;
}

/** Removes floating-point rounding errors from the end of a string.
*
* <p>{@code 12.300000006} becomes {@code 12.3};
* {@code -12.37999999991} becomes {@code -12.38}. */
public static String correctRoundedFloat(String s) {
if (s == null) {
return s;
}
final Matcher m = TRAILING_ZERO_PATTERN.matcher(s);
if (m.matches()) {
s = s.substring(0, s.length() - m.group(2).length());
}
final Matcher m2 = TRAILING_NINE_PATTERN.matcher(s);
if (m2.matches()) {
s = s.substring(0, s.length() - m2.group(2).length());
if (s.length() > 0) {
final char c = s.charAt(s.length() - 1);
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
// '12.3499999996' became '12.34', now we make it '12.35'
s = s.substring(0, s.length() - 1) + (char) (c + 1);
break;
case '.':
// '12.9999991' became '12.', which we leave as is.
break;
}
}
}
return s;
}

public Collection<String> toStringList(ResultSet resultSet,
Collection<String> list) throws SQLException {
final ResultSetMetaData metaData = resultSet.getMetaData();
while (resultSet.next()) {
rowToString(resultSet, metaData);
list.add(buf.toString());
buf.setLength(0);
}
return list;
}

/** Flushes the buffer and returns its previous contents. */
public String string() {
String s = buf.toString();
buf.setLength(0);
return s;
}
}
}

0 comments on commit 52d7fe0

Please sign in to comment.