This is an automated email from the ASF dual-hosted git repository. jhyde pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/calcite.git
commit 342aeba1087857df48891655cf0de41d5f3479c7 Author: Julian Hyde <[email protected]> AuthorDate: Tue Jun 1 12:49:14 2021 -0700 [CALCITE-4628] If SqlImplementor fails, include the RelNode in the exception Add a test case for a bug that was fixed by "[CALCITE-4524] Make some fields non-nullable (SqlSelect.selectList, DataContext.getTypeFactory)". --- .../org/apache/calcite/rel/rel2sql/SqlImplementor.java | 10 ++++++++-- .../calcite/rel/rel2sql/RelToSqlConverterTest.java | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java index bf4a720..0751ea8 100644 --- a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java +++ b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java @@ -18,6 +18,7 @@ package org.apache.calcite.rel.rel2sql; import org.apache.calcite.linq4j.Ord; import org.apache.calcite.linq4j.tree.Expressions; +import org.apache.calcite.plan.RelOptUtil; import org.apache.calcite.rel.RelCollation; import org.apache.calcite.rel.RelFieldCollation; import org.apache.calcite.rel.RelNode; @@ -160,8 +161,13 @@ public abstract class SqlImplementor { } /** Visits a relational expression that has no parent. */ - public final Result visitRoot(RelNode e) { - return visitInput(holder(e), 0); + public final Result visitRoot(RelNode r) { + try { + return visitInput(holder(r), 0); + } catch (Error | RuntimeException e) { + throw Util.throwAsRuntime("Error while converting RelNode to SqlNode:\n" + + RelOptUtil.toString(r), e); + } } /** Creates a relational expression that has {@code r} as its input. */ diff --git a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java index 6959648..50f66c6 100644 --- a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java +++ b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java @@ -5104,6 +5104,23 @@ class RelToSqlConverterTest { .withPostgresql().ok(expectedPostgresql); } + /** As {@link #testValuesEmpty()} but with extra {@code SUBSTRING}. Before + * <a href="https://issues.apache.org/jira/browse/CALCITE-4524">[CALCITE-4524] + * Make some fields non-nullable</a> was fixed, this case would fail with + * {@code java.lang.IndexOutOfBoundsException}. */ + @Test void testValuesEmpty2() { + final String sql0 = "select *\n" + + "from (values (1, 'a'), (2, 'bb')) as t(x, y)\n" + + "limit 0"; + final String sql = "SELECT SUBSTRING(y, 1, 1) FROM (" + sql0 + ") t"; + final RuleSet rules = + RuleSets.ofList(PruneEmptyRules.SORT_FETCH_ZERO_INSTANCE); + final String expected = "SELECT SUBSTRING(`Y` FROM 1 FOR 1)\n" + + "FROM (SELECT NULL AS `X`, NULL AS `Y`) AS `t`\n" + + "WHERE 1 = 0"; + sql(sql).optimize(rules, null).withMysql().ok(expected); + } + /** Test case for * <a href="https://issues.apache.org/jira/browse/CALCITE-3840">[CALCITE-3840] * Re-aliasing of VALUES that has column aliases produces wrong SQL in the
