Java Code Examples for org.apache.calcite.sql.parser.SqlParser#parseQuery()

The following examples show how to use org.apache.calcite.sql.parser.SqlParser#parseQuery() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BatsOptimizerTest.java    From Bats with Apache License 2.0 6 votes vote down vote up
static void testSqlParser() throws Exception {
    String sql = "select * from test where f1=1 or f2=2 order by f3 limit 2";
    SqlParser sqlParser = createSqlParser(sql);
    SqlNode sqlNode = sqlParser.parseQuery();
    System.out.println(sqlNode);
    System.out.println();

    // drill的parser不支持
    // sql = "insert into test(f1,f2,f3) values(1,2,3)";
    // sqlParser = createSqlParser(sql);
    // sqlNode = sqlParser.parseStmt();
    // System.out.println(sqlNode);
    // System.out.println();

    sql = "delete from test where f1=1";
    sqlNode = sqlParser.parseQuery(sql);
    System.out.println(sqlNode);
    System.out.println();

    sql = "call LTRIM('abc')";
    sqlNode = sqlParser.parseQuery(sql);
    System.out.println(sqlNode);
    System.out.println();
}
 
Example 2
Source File: SqlQueryParser.java    From quark with Apache License 2.0 6 votes vote down vote up
/**
 * Strips the dataSource name from the query
 * @param query
 * @param dataSource
 * @return
 * @throws QuarkException
 */
private String stripNamespace(final String query,
                              final DataSourceSchema dataSource)
    throws QuarkException {
  String result = query.replace("\n", " ");
  if (dataSource != null) {
    try {
      final SqlParser parser = getSqlParser(query);
      SqlNode node = parser.parseQuery();
      result = stripNamespace(node, dataSource.getName(),
          dataSource.getDataSource().getSqlDialect());
    } catch (Exception e) {
      LOG.warn("Exception while parsing the input query: " + e.getMessage());
    }
  }
  return result;
}
 
Example 3
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public RelRoot expandView(RelDataType rowType, String queryString,
    List<String> schemaPath, List<String> viewPath) {
  expansionDepth++;

  SqlParser parser = prepare.createParser(queryString);
  SqlNode sqlNode;
  try {
    sqlNode = parser.parseQuery();
  } catch (SqlParseException e) {
    throw new RuntimeException("parse failed", e);
  }
  // View may have different schema path than current connection.
  final CatalogReader catalogReader =
      this.catalogReader.withSchemaPath(schemaPath);
  SqlValidator validator = createSqlValidator(catalogReader);
  final SqlToRelConverter.Config config = SqlToRelConverter.configBuilder()
          .withTrimUnusedFields(true).build();
  SqlToRelConverter sqlToRelConverter =
      getSqlToRelConverter(validator, catalogReader, config);
  RelRoot root =
      sqlToRelConverter.convertQuery(sqlNode, true, false);

  --expansionDepth;
  return root;
}
 
Example 4
Source File: Hoist.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Hoists literals in a given SQL string, returning a {@link Hoisted}. */
public Hoisted hoist(String sql) {
  final List<Variable> variables = new ArrayList<>();
  final SqlParser parser = SqlParser.create(sql, config.parserConfig());
  final SqlNode node;
  try {
    node = parser.parseQuery();
  } catch (SqlParseException e) {
    throw new RuntimeException(e);
  }
  node.accept(new SqlShuttle() {
    @Override public SqlNode visit(SqlLiteral literal) {
      variables.add(new Variable(sql, variables.size(), literal));
      return super.visit(literal);
    }
  });
  return new Hoisted(sql, variables);
}
 
Example 5
Source File: PlannerImpl.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public RelRoot expandView(RelDataType rowType, String queryString,
                          List<String> schemaPath, List<String> viewPath) {
    if (planner == null) {
        ready();
    }
    SqlParser parser = SqlParser.create(queryString, parserConfig);
    SqlNode sqlNode;
    try {
        sqlNode = parser.parseQuery();
    } catch (SqlParseException e) {
        throw new RuntimeException("parse failed", e);
    }

    final CalciteCatalogReader catalogReader =
            createCatalogReader().withSchemaPath(schemaPath);
    final SqlValidator validator = createSqlValidator(catalogReader);

    final RexBuilder rexBuilder = createRexBuilder();
    final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
    final SqlToRelConverter.Config config = SqlToRelConverter
            .configBuilder()
            .withConfig(sqlToRelConverterConfig)
            .withTrimUnusedFields(false)
            .build();
    final SqlToRelConverter sqlToRelConverter =
            new SqlToRelConverter(this, validator,
                    catalogReader, cluster, convertletTable, config);

    final RelRoot root =
            sqlToRelConverter.convertQuery(sqlNode, true, false);
    final RelRoot root2 =
            root.withRel(sqlToRelConverter.flattenTypes(root.rel, true));
    final RelBuilder relBuilder =
            config.getRelBuilderFactory().create(cluster, null);
    return root2.withRel(
            RelDecorrelator.decorrelateQuery(root.rel, relBuilder));
}
 
Example 6
Source File: QuarkTestUtil.java    From quark with Apache License 2.0 5 votes vote down vote up
public static void checkSqlParsing(String sql, Properties info, String expectedSql,
    SqlDialect dialect)
    throws QuarkException, SqlParseException {
  SqlQueryParser parser = new SqlQueryParser(info);
  SqlParser sqlParser = parser.getSqlParser(sql);
  SqlNode sqlNode = sqlParser.parseQuery();
  String finalQuery = sqlNode.toSqlString(dialect).getSql();
  assertEquals(expectedSql, finalQuery.replace("\n", " "));
}
 
Example 7
Source File: PlannerImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelRoot expandView(RelDataType rowType, String queryString,
    List<String> schemaPath, List<String> viewPath) {
  if (planner == null) {
    ready();
  }
  SqlParser parser = SqlParser.create(queryString, parserConfig);
  SqlNode sqlNode;
  try {
    sqlNode = parser.parseQuery();
  } catch (SqlParseException e) {
    throw new RuntimeException("parse failed", e);
  }

  final CalciteCatalogReader catalogReader =
      createCatalogReader().withSchemaPath(schemaPath);
  final SqlValidator validator = createSqlValidator(catalogReader);

  final RexBuilder rexBuilder = createRexBuilder();
  final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
  final SqlToRelConverter.Config config = SqlToRelConverter
      .configBuilder()
      .withConfig(sqlToRelConverterConfig)
      .withTrimUnusedFields(false)
      .build();
  final SqlToRelConverter sqlToRelConverter =
      new SqlToRelConverter(this, validator,
          catalogReader, cluster, convertletTable, config);

  final RelRoot root =
      sqlToRelConverter.convertQuery(sqlNode, true, false);
  final RelRoot root2 =
      root.withRel(sqlToRelConverter.flattenTypes(root.rel, true));
  final RelBuilder relBuilder =
      config.getRelBuilderFactory().create(cluster, null);
  return root2.withRel(
      RelDecorrelator.decorrelateQuery(root.rel, relBuilder));
}
 
Example 8
Source File: BatsEngine.java    From Bats with Apache License 2.0 4 votes vote down vote up
public static SqlNode parse(String sql, SqlParser.Config config) throws SqlParseException {
    SqlParser sqlParser = SqlParser.create(sql, config);
    return sqlParser.parseQuery();
}
 
Example 9
Source File: BatsOptimizerTest.java    From Bats with Apache License 2.0 4 votes vote down vote up
static SqlNode parse(String sql) throws Exception {
    SqlParser sqlParser = createSqlParser(sql);
    return sqlParser.parseQuery();
}
 
Example 10
Source File: CalciteParser.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
public static SqlNode parse(String sql) throws SqlParseException {
    SqlParser.ConfigBuilder parserBuilder = SqlParser.configBuilder();
    SqlParser sqlParser = SqlParser.create(sql, parserBuilder.build());
    return sqlParser.parseQuery();
}
 
Example 11
Source File: CalciteParser.java    From kylin with Apache License 2.0 4 votes vote down vote up
public static SqlNode parse(String sql) throws SqlParseException {
    SqlParser.ConfigBuilder parserBuilder = SqlParser.configBuilder();
    SqlParser sqlParser = SqlParser.create(sql, parserBuilder.build());
    return sqlParser.parseQuery();
}
 
Example 12
Source File: AbstractSqlTester.java    From calcite with Apache License 2.0 4 votes vote down vote up
public SqlNode parseQuery(String sql) throws SqlParseException {
  SqlParser parser = factory.createParser(sql);
  return parser.parseQuery();
}
 
Example 13
Source File: SqlToRelTestBase.java    From calcite with Apache License 2.0 4 votes vote down vote up
public SqlNode parseQuery(String sql) throws Exception {
  final SqlParser.Config config =
      SqlParser.configBuilder().setConformance(getConformance()).build();
  SqlParser parser = SqlParser.create(sql, config);
  return parser.parseQuery();
}