Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-4585]when run RelNode error,Confuse log info be thrown(xiong duan) #2402

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.calcite.linq4j.tree.Expressions;
import org.apache.calcite.materialize.Lattice;
import org.apache.calcite.materialize.MaterializationService;
import org.apache.calcite.plan.RelOptUtil;
import org.apache.calcite.prepare.CalciteCatalogReader;
import org.apache.calcite.rel.type.DelegatingTypeSystem;
import org.apache.calcite.rel.type.RelDataTypeSystem;
Expand Down Expand Up @@ -222,8 +223,14 @@ private CalcitePreparedStatement prepareStatement_(
server.getStatement(calcitePreparedStatement.handle).setSignature(signature);
return calcitePreparedStatement;
} catch (Exception e) {
throw Helper.INSTANCE.createException(
"Error while preparing statement [" + query.sql + "]", e);
if (null != query.rel) {
throw Helper.INSTANCE.createException(
"Error while preparing statement [" + System.lineSeparator()
+ RelOptUtil.toString(query.rel) + "]", e);
} else {
throw Helper.INSTANCE.createException(
"Error while preparing statement [" + query.sql + "]", e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@

import org.apache.calcite.adapter.java.ReflectiveSchema;
import org.apache.calcite.jdbc.CalciteConnection;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.tools.FrameworkConfig;
import org.apache.calcite.tools.Frameworks;
import org.apache.calcite.tools.RelBuilder;
import org.apache.calcite.tools.RelRunner;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

Expand All @@ -39,6 +46,7 @@
*/
public class ExceptionMessageTest {
private Connection conn;
private RelBuilder builder;

/**
* Simple reflective schema that provides valid and invalid entries.
Expand Down Expand Up @@ -76,7 +84,11 @@ public void setUp() throws SQLException {
SchemaPlus rootSchema = calciteConnection.getRootSchema();
rootSchema.add("test", new ReflectiveSchema(new TestSchema()));
calciteConnection.setSchema("test");
FrameworkConfig config = Frameworks.newConfigBuilder()
.defaultSchema(rootSchema)
.build();
this.conn = calciteConnection;
this.builder = RelBuilder.create(config);
}

private void runQuery(String sql) throws SQLException {
Expand All @@ -94,6 +106,20 @@ private void runQuery(String sql) throws SQLException {
}
}

private void runQuery(RelNode relNode) throws SQLException {
RelRunner relRunner = conn.unwrap(RelRunner.class);
PreparedStatement preparedStatement = relRunner.prepare(relNode);
try {
preparedStatement.executeQuery();
} finally {
try {
preparedStatement.close();
} catch (Exception e) {
fail("Error on close");
}
}
}

@Test void testValidQuery() throws SQLException {
// Just ensure that we're actually dealing with a valid connection
// to be sure that the results of the other tests can be trusted
Expand Down Expand Up @@ -141,4 +167,30 @@ private void runQuery(String sql) throws SQLException {
containsString("Object 'nonexistentTable' not found"));
}
}

@Test void testValidRelNodeQuery() throws SQLException {
final RelNode relNode = builder
.scan("test", "entries")
.project(builder.field("name"))
.build();
runQuery(relNode);
}

@Test void testRelNodeQueryException() throws SQLException {
NobiGo marked this conversation as resolved.
Show resolved Hide resolved
try {
final RelNode relNode = builder
.scan("test", "entries")
.project(builder.call(SqlStdOperatorTable.ABS, builder.field("name")))
.build();
runQuery(relNode);
fail("Query badEntries should result in an exception");
} catch (RuntimeException e) {
assertThat(
e.getMessage(), equalTo("java.sql.SQLException: "
+ "Error while preparing statement [" + System.lineSeparator()
+ "LogicalProject($f0=[ABS($1)])" + System.lineSeparator()
+ " LogicalTableScan(table=[[test, entries]])" + System.lineSeparator()
+ "]"));
}
}
}