|
| 1 | +package io.substrait.isthmus.sql; |
| 2 | + |
| 3 | +import io.substrait.isthmus.SubstraitTypeSystem; |
| 4 | +import java.util.List; |
| 5 | +import java.util.stream.Collectors; |
| 6 | +import org.apache.calcite.jdbc.JavaTypeFactoryImpl; |
| 7 | +import org.apache.calcite.plan.RelOptCluster; |
| 8 | +import org.apache.calcite.plan.RelOptPlanner; |
| 9 | +import org.apache.calcite.plan.RelOptTable; |
| 10 | +import org.apache.calcite.plan.hep.HepPlanner; |
| 11 | +import org.apache.calcite.plan.hep.HepProgram; |
| 12 | +import org.apache.calcite.prepare.Prepare; |
| 13 | +import org.apache.calcite.rel.RelNode; |
| 14 | +import org.apache.calcite.rel.RelRoot; |
| 15 | +import org.apache.calcite.rel.rules.CoreRules; |
| 16 | +import org.apache.calcite.rex.RexBuilder; |
| 17 | +import org.apache.calcite.sql.SqlNode; |
| 18 | +import org.apache.calcite.sql.parser.SqlParseException; |
| 19 | +import org.apache.calcite.sql.validate.SqlValidator; |
| 20 | +import org.apache.calcite.sql2rel.SqlToRelConverter; |
| 21 | +import org.apache.calcite.sql2rel.StandardConvertletTable; |
| 22 | + |
| 23 | +/** |
| 24 | + * Substrait flavoured SQL processor provided as a utility for testing and experimentation, |
| 25 | + * utilizing {@link SubstraitSqlStatementParser} and {@link SubstraitSqlValidator} |
| 26 | + */ |
| 27 | +public class SubstraitSqlToCalcite { |
| 28 | + |
| 29 | + /** |
| 30 | + * Converts a SQL statement to a Calcite {@link RelRoot}. |
| 31 | + * |
| 32 | + * @param sqlStatement a SQL statement string |
| 33 | + * @param catalogReader the {@link Prepare.CatalogReader} for finding tables/views referenced in |
| 34 | + * the SQL statement |
| 35 | + * @return a {@link RelRoot} corresponding to the given SQL statement |
| 36 | + * @throws SqlParseException if there is an error while parsing the SQL statement |
| 37 | + */ |
| 38 | + public static RelRoot convertQuery(String sqlStatement, Prepare.CatalogReader catalogReader) |
| 39 | + throws SqlParseException { |
| 40 | + SqlValidator validator = new SubstraitSqlValidator(catalogReader); |
| 41 | + return convertQuery(sqlStatement, catalogReader, validator, createDefaultRelOptCluster()); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Converts a SQL statement to a Calcite {@link RelRoot}. |
| 46 | + * |
| 47 | + * @param sqlStatement a SQL statement |
| 48 | + * @param catalogReader the {@link Prepare.CatalogReader} for finding tables/views referenced in |
| 49 | + * the SQL statement |
| 50 | + * @param validator the {@link SqlValidator} used to validate the SQL statement. Allows for |
| 51 | + * additional control of SQL functions and operators via {@link |
| 52 | + * SqlValidator#getOperatorTable()} |
| 53 | + * @param cluster the {@link RelOptCluster} used when creating {@link RelNode}s during statement |
| 54 | + * processing. Calcite expects that the {@link RelOptCluster} used during statement processing |
| 55 | + * is the same as that used during query optimization. |
| 56 | + * @return {@link RelRoot} corresponding to the given SQL statement |
| 57 | + * @throws SqlParseException if there is an error while parsing the SQL statement string |
| 58 | + */ |
| 59 | + public static RelRoot convertQuery( |
| 60 | + String sqlStatement, |
| 61 | + Prepare.CatalogReader catalogReader, |
| 62 | + SqlValidator validator, |
| 63 | + RelOptCluster cluster) |
| 64 | + throws SqlParseException { |
| 65 | + List<SqlNode> sqlNodes = SubstraitSqlStatementParser.parseStatements(sqlStatement); |
| 66 | + if (sqlNodes.size() != 1) { |
| 67 | + throw new IllegalArgumentException( |
| 68 | + String.format("Expected one statement, found: %d", sqlNodes.size())); |
| 69 | + } |
| 70 | + List<RelRoot> relRoots = convert(sqlNodes, catalogReader, validator, cluster); |
| 71 | + // as there was only 1 statement, there should only be 1 root |
| 72 | + return relRoots.get(0); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Converts one or more SQL statements to a List of {@link RelRoot}, with one {@link RelRoot} per |
| 77 | + * statement. |
| 78 | + * |
| 79 | + * @param sqlStatements a string containing one or more SQL statements |
| 80 | + * @param catalogReader the {@link Prepare.CatalogReader} for finding tables/views referenced in |
| 81 | + * the SQL statements |
| 82 | + * @return a list of {@link RelRoot}s corresponding to the given SQL statements |
| 83 | + * @throws SqlParseException if there is an error while parsing the SQL statements |
| 84 | + */ |
| 85 | + public static List<RelRoot> convertQueries( |
| 86 | + String sqlStatements, Prepare.CatalogReader catalogReader) throws SqlParseException { |
| 87 | + SqlValidator validator = new SubstraitSqlValidator(catalogReader); |
| 88 | + return convertQueries(sqlStatements, catalogReader, validator, createDefaultRelOptCluster()); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Converts one or more SQL statements to a List of {@link RelRoot}, with one {@link RelRoot} per |
| 93 | + * statement. |
| 94 | + * |
| 95 | + * @param sqlStatements a string containing one or more SQL statements |
| 96 | + * @param catalogReader the {@link Prepare.CatalogReader} for finding tables/views referenced in |
| 97 | + * the SQL statements |
| 98 | + * @param validator the {@link SqlValidator} used to validate SQL statements. Allows for |
| 99 | + * additional control of SQL functions and operators via {@link |
| 100 | + * SqlValidator#getOperatorTable()} |
| 101 | + * @param cluster the {@link RelOptCluster} used when creating {@link RelNode}s during statement |
| 102 | + * processing. Calcite expects that the {@link RelOptCluster} used during statement processing |
| 103 | + * is the same as that used during query optimization. |
| 104 | + * @return a list of {@link RelRoot}s corresponding to the given SQL statements |
| 105 | + * @throws SqlParseException if there is an error while parsing the SQL statements |
| 106 | + */ |
| 107 | + public static List<RelRoot> convertQueries( |
| 108 | + String sqlStatements, |
| 109 | + Prepare.CatalogReader catalogReader, |
| 110 | + SqlValidator validator, |
| 111 | + RelOptCluster cluster) |
| 112 | + throws SqlParseException { |
| 113 | + List<SqlNode> sqlNodes = SubstraitSqlStatementParser.parseStatements(sqlStatements); |
| 114 | + return convert(sqlNodes, catalogReader, validator, cluster); |
| 115 | + } |
| 116 | + |
| 117 | + static List<RelRoot> convert( |
| 118 | + List<SqlNode> sqlNodes, |
| 119 | + Prepare.CatalogReader catalogReader, |
| 120 | + SqlValidator validator, |
| 121 | + RelOptCluster cluster) { |
| 122 | + RelOptTable.ViewExpander viewExpander = null; |
| 123 | + SqlToRelConverter converter = |
| 124 | + new SqlToRelConverter( |
| 125 | + viewExpander, |
| 126 | + validator, |
| 127 | + catalogReader, |
| 128 | + cluster, |
| 129 | + StandardConvertletTable.INSTANCE, |
| 130 | + SqlToRelConverter.CONFIG); |
| 131 | + // apply validation |
| 132 | + boolean needsValidation = true; |
| 133 | + // query is the root of the tree |
| 134 | + boolean top = true; |
| 135 | + return sqlNodes.stream() |
| 136 | + .map( |
| 137 | + sqlNode -> |
| 138 | + removeRedundantProjects(converter.convertQuery(sqlNode, needsValidation, top))) |
| 139 | + .collect(Collectors.toList()); |
| 140 | + } |
| 141 | + |
| 142 | + static RelOptCluster createDefaultRelOptCluster() { |
| 143 | + RexBuilder rexBuilder = |
| 144 | + new RexBuilder(new JavaTypeFactoryImpl(SubstraitTypeSystem.TYPE_SYSTEM)); |
| 145 | + HepProgram program = HepProgram.builder().build(); |
| 146 | + RelOptPlanner emptyPlanner = new HepPlanner(program); |
| 147 | + return RelOptCluster.create(emptyPlanner, rexBuilder); |
| 148 | + } |
| 149 | + |
| 150 | + static RelRoot removeRedundantProjects(RelRoot root) { |
| 151 | + return root.withRel(removeRedundantProjects(root.rel)); |
| 152 | + } |
| 153 | + |
| 154 | + static RelNode removeRedundantProjects(RelNode root) { |
| 155 | + // The Calcite RelBuilder, when constructing Project that does not modify its inputs in any way, |
| 156 | + // simply elides it. The PROJECT_REMOVE rule can be used to remove such projects from Rel trees. |
| 157 | + // This facilitates roundtrip testing. |
| 158 | + HepProgram program = HepProgram.builder().addRuleInstance(CoreRules.PROJECT_REMOVE).build(); |
| 159 | + HepPlanner planner = new HepPlanner(program); |
| 160 | + planner.setRoot(root); |
| 161 | + return planner.findBestExp(); |
| 162 | + } |
| 163 | +} |
0 commit comments