use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project streamline by hortonworks.
the class RuleParser method parse.
public void parse() {
try {
SchemaPlus schema = Frameworks.createRootSchema(true);
FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(schema).build();
Planner planner = Frameworks.getPlanner(config);
SqlSelect sqlSelect = (SqlSelect) planner.parse(sql);
// FROM
streams = parseStreams(sqlSelect);
// SELECT
projection = parseProjection(sqlSelect);
// WHERE
condition = parseCondition(sqlSelect);
// GROUP BY
groupBy = parseGroupBy(sqlSelect);
// HAVING
having = parseHaving(sqlSelect);
} catch (Exception ex) {
LOG.error("Got Exception while parsing rule {}", sql);
throw new RuntimeException(ex);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project drill by apache.
the class TestSqlBracketlessSyntax method checkComplexExpressionParsing.
@Test
public void checkComplexExpressionParsing() throws Exception {
FrameworkConfig config = //
Frameworks.newConfigBuilder().parserConfig(SqlParser.configBuilder().setLex(Lex.MYSQL).setIdentifierMaxLength(PlannerSettings.DEFAULT_IDENTIFIER_MAX_LENGTH).setParserFactory(DrillParserImpl.FACTORY).build()).defaultSchema(//
CalciteSchema.createRootSchema(false, false).plus()).convertletTable(//
DrillConvertletTable.INSTANCE).build();
Planner planner = Frameworks.getPlanner(config);
SqlNode node = planner.parse("" + "select a[4].c \n" + "from x.y.z \n" + "where a.c.b = 5 and x[2] = 7 \n" + "group by d \n" + "having a.c < 5 \n" + "order by x.a.a.a.a.a");
String expected = "SELECT `a`[4]['c']\n" + "FROM `x`.`y`.`z`\n" + "WHERE `a`.`c`['b'] = 5 AND `x`[2] = 7\n" + "GROUP BY `d`\n" + "HAVING `a`.`c` < 5\n" + "ORDER BY `x`.`a`['a']['a']['a']['a']";
SqlNode rewritten = node.accept(new CompoundIdentifierConverter(false));
String rewrittenQuery = rewritten.toString();
DrillAssert.assertMultiLineStringEquals(expected, rewrittenQuery);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project samza by apache.
the class SamzaSqlQueryParser method parseQuery.
public static QueryInfo parseQuery(String sql) {
Planner planner = createPlanner();
SqlNode sqlNode;
// Having semi-colons at the end of sql statement is a valid syntax in standard sql but not for Calcite parser.
// Hence, removing trailing semi-colon before passing sql statement to Calcite parser.
sql = sql.replaceAll(TRAILING_SEMI_COLON_REGEX, "");
try {
sqlNode = planner.parse(sql);
} catch (SqlParseException e) {
String errorMsg = SamzaSqlValidator.formatErrorString(sql, e);
LOG.error(errorMsg, e);
throw new SamzaException(errorMsg, e);
}
String sink;
String selectQuery;
ArrayList<String> sources;
if (sqlNode instanceof SqlInsert) {
SqlInsert sqlInsert = (SqlInsert) sqlNode;
sink = sqlInsert.getTargetTable().toString();
if (sqlInsert.getSource() instanceof SqlSelect) {
SqlSelect sqlSelect = (SqlSelect) sqlInsert.getSource();
selectQuery = sqlSelect.toString();
LOG.info("Parsed select query {} from sql {}", selectQuery, sql);
sources = getSourcesFromSelectQuery(sqlSelect);
} else {
String msg = String.format("Sql query is not of the expected format. Select node expected, found %s", sqlInsert.getSource().getClass().toString());
LOG.error(msg);
throw new SamzaException(msg);
}
} else {
String msg = String.format("Sql query is not of the expected format. Insert node expected, found %s", sqlNode.getClass().toString());
LOG.error(msg);
throw new SamzaException(msg);
}
return new QueryInfo(selectQuery, sources, sink, sql);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project storm by apache.
the class StormSqlContext method explain.
public String explain(String query) throws SqlParseException, ValidationException, RelConversionException {
FrameworkConfig config = buildFrameWorkConfig();
Planner planner = Frameworks.getPlanner(config);
SqlNode parse = planner.parse(query);
SqlNode validate = planner.validate(parse);
RelNode tree = planner.convert(validate);
return StormRelUtils.explain(tree, SqlExplainLevel.ALL_ATTRIBUTES);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project beam by apache.
the class ThreeTablesSchema method transform.
private RelNode transform(String sql, RuleSet prepareRules) throws Exception {
final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
final SchemaPlus defSchema = rootSchema.add("tt", new ThreeTablesSchema());
final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(SqlParser.Config.DEFAULT).defaultSchema(defSchema).traitDefs(ConventionTraitDef.INSTANCE, RelCollationTraitDef.INSTANCE).programs(Programs.of(prepareRules)).build();
Planner planner = Frameworks.getPlanner(config);
SqlNode parse = planner.parse(sql);
SqlNode validate = planner.validate(parse);
RelRoot planRoot = planner.rel(validate);
RelNode planBefore = planRoot.rel;
RelTraitSet desiredTraits = planBefore.getTraitSet().replace(EnumerableConvention.INSTANCE);
return planner.transform(0, desiredTraits, planBefore);
}
Aggregations