Java Code Examples for org.apache.calcite.sql.parser.SqlParseException

The following examples show how to use org.apache.calcite.sql.parser.SqlParseException. These examples are extracted from open source projects. 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 Project: Bats   Source File: SqlAdvisor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to parse a SQL statement and adds to the errorList if any syntax
 * error is found. This implementation uses {@link SqlParser}. Subclass can
 * re-implement this with a different parser implementation
 *
 * @param sql       A user-input sql statement to be parsed
 * @param errorList A {@link List} of error to be added to
 * @return {@link SqlNode } that is root of the parse tree, null if the sql
 * is not valid
 */
protected SqlNode collectParserError(
    String sql,
    List<ValidateErrorInfo> errorList) {
  try {
    return parseQuery(sql);
  } catch (SqlParseException e) {
    ValidateErrorInfo errInfo =
        new ValidateErrorInfo(
            e.getPos(),
            e.getMessage());

    // parser only returns 1 exception now
    errorList.add(errInfo);
    return null;
  }
}
 
Example 2
Source Project: calcite   Source File: AbstractMaterializedViewTest.java    License: Apache License 2.0 6 votes vote down vote up
private RelNode toRel(RelOptCluster cluster, SchemaPlus rootSchema,
    SchemaPlus defaultSchema, String sql) throws SqlParseException {
  final SqlParser parser = SqlParser.create(sql, SqlParser.Config.DEFAULT);
  final SqlNode parsed = parser.parseStmt();

  final CalciteCatalogReader catalogReader = new CalciteCatalogReader(
      CalciteSchema.from(rootSchema),
      CalciteSchema.from(defaultSchema).path(null),
      new JavaTypeFactoryImpl(), new CalciteConnectionConfigImpl(new Properties()));

  final SqlValidator validator = new ValidatorForTest(SqlStdOperatorTable.instance(),
      catalogReader, new JavaTypeFactoryImpl(), SqlConformanceEnum.DEFAULT);
  final SqlNode validated = validator.validate(parsed);
  final SqlToRelConverter.Config config = SqlToRelConverter.configBuilder()
      .withTrimUnusedFields(true)
      .withExpand(true)
      .withDecorrelationEnabled(true)
      .build();
  final SqlToRelConverter converter = new SqlToRelConverter(
      (rowType, queryString, schemaPath, viewPath) -> {
        throw new UnsupportedOperationException("cannot expand view");
      }, validator, catalogReader, cluster, StandardConvertletTable.INSTANCE, config);
  return converter.convertQuery(validated, false, true).rel;
}
 
Example 3
Source Project: alchemy   Source File: SqlParseUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static void parse(List<String> sqls, List<String> sources, List<String> udfs, List<String> sinks)
    throws SqlParseException {
    for (String sql : sqls) {
        SqlParser sqlParser = SqlParser.create(sql, CONFIG);
        SqlNode sqlNode = sqlParser.parseStmt();
        SqlKind kind = sqlNode.getKind();
        switch (kind){
            case INSERT:
                SqlInsert sqlInsert = (SqlInsert)sqlNode;
                addSink(sinks, findSinkName(sqlInsert));
                SqlSelect source = (SqlSelect) sqlInsert.getSource();
                parseSource(source, sources, udfs);
                break;
            case SELECT:
                parseSource((SqlSelect) sqlNode, sources, udfs);
                break;
            default:
                throw new IllegalArgumentException("It must be an insert SQL, sql:" + sql);
        }
    }
}
 
Example 4
Source Project: calcite   Source File: PlannerTest.java    License: Apache License 2.0 6 votes vote down vote up
private void emptyUnions32(UnionMergeRule... extraRules)
    throws SqlParseException, ValidationException, RelConversionException {
  String plan = "EnumerableUnion(all=[true])\n"
      + "  EnumerableValues(tuples=[[{ 1 }]])\n"
      + "  EnumerableValues(tuples=[[{ 2 }]])\n";

  checkUnionPruning("values(1)"
          + " union all values(2)"
          + " union all select * from (values(3)) where false",
      plan, extraRules);

  checkUnionPruning("values(1)"
          + " union all select * from (values(3)) where false"
          + " union all values(2)",
      plan, extraRules);

  checkUnionPruning("select * from (values(2)) where false"
          + " union all values(1)"
          + " union all values(2)",
      plan, extraRules);
}
 
Example 5
Source Project: calcite   Source File: PlannerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testValidateFails() throws SqlParseException {
  Planner planner = getPlanner(null);
  SqlNode parse =
      planner.parse("select * from \"emps\" where \"Xname\" like '%e%'");
  assertThat(Util.toLinux(parse.toString()),
      equalTo("SELECT *\n"
          + "FROM `emps`\n"
          + "WHERE `Xname` LIKE '%e%'"));

  try {
    SqlNode validate = planner.validate(parse);
    fail("expected error, got " + validate);
  } catch (ValidationException e) {
    assertThat(Throwables.getStackTraceAsString(e),
        containsString("Column 'Xname' not found in any table"));
    // ok
  }
}
 
Example 6
Source Project: kylin-on-parquet-v2   Source File: SqlParamsFinderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testParamFinder() throws SqlParseException {
    SqlParser sqlParser1 = SqlParser.create("POWER($0, $1) + AVG(LN($3)) + EXP($5)");
    SqlNode sqlPattern = sqlParser1.parseExpression();
    SqlParser sqlParser2 = SqlParser.create("POWER(3, POWER(2, POWER(2, 3))) + AVG(LN(EXP(4))) + EXP(CAST('2018-03-22' AS DATE))");
    SqlNode sqlCall = sqlParser2.parseExpression();

    SqlParamsFinder sqlParamsFinder = new SqlParamsFinder((SqlCall)sqlPattern, (SqlCall)sqlCall);
    Map<Integer, SqlNode> paramNodes =  sqlParamsFinder.getParamNodes();

    Assert.assertEquals("3", paramNodes.get(0).toString());
    Assert.assertEquals("POWER(2, POWER(2, 3))", paramNodes.get(1).toString());
    Assert.assertEquals("EXP(4)", paramNodes.get(3).toString());
    Assert.assertEquals("CAST('2018-03-22' AS DATE)", paramNodes.get(5).toString());

}
 
Example 7
Source Project: sylph   Source File: FlinkSqlParser.java    License: Apache License 2.0 6 votes vote down vote up
public void parser(String query, List<CreateTable> batchTablesList)
{
    Map<String, CreateTable> batchTables = batchTablesList.stream()
            .collect(Collectors.toMap(CreateTable::getName, v -> v));
    CalciteSqlParser sqlParser = new CalciteSqlParser(batchTables.keySet());

    List<Object> plan;
    try {
        plan = sqlParser.getPlan(query, sqlParserConfig);
    }
    catch (SqlParseException e) {
        throw throwsException(e);
    }

    List<String> registerViews = new ArrayList<>();
    try {
        translate(plan, batchTables, registerViews);
    }
    finally {
        //registerViews.forEach(tableName -> tableEnv.sqlQuery("drop table " + tableName));
    }
}
 
Example 8
Source Project: calcite   Source File: CalcitePrepareImpl.java    License: 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 9
Source Project: flink   Source File: CreateTableLikeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidNoSourceTable() throws SqlParseException {
	thrown.expect(SqlParseException.class);
	thrown.expectMessage("Encountered \"(\" at line 4, column 6.\n" +
		"Was expecting one of:\n" +
		"    <BRACKET_QUOTED_IDENTIFIER> ...\n" +
		"    <QUOTED_IDENTIFIER> ...\n" +
		"    <BACK_QUOTED_IDENTIFIER> ...\n" +
		"    <IDENTIFIER> ...\n" +
		"    <UNICODE_QUOTED_IDENTIFIER> ...\n");
	createFlinkParser(
		"CREATE TABLE t (\n" +
			"   a STRING\n" +
			")\n" +
			"LIKE (" +
			"   INCLUDING ALL" +
			")")
		.parseStmt();
}
 
Example 10
Source Project: dremio-oss   Source File: TestJobsServiceUtil.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void convertExceptionToFailureInfo() {
  // Fake logger to not pollute logs
  org.slf4j.Logger logger = mock(org.slf4j.Logger.class);

  SqlParseException parseException = new SqlParseException("test message", new SqlParserPos(7, 42, 13, 57), null, null, null);
  UserException userException = SqlExceptionHelper.parseError("SELECT FOO", parseException)
      .build(logger);
  String verboseError = userException.getVerboseMessage(false);

  JobFailureInfo jobFailureInfo = JobsServiceUtil.toFailureInfo(verboseError);
  assertEquals(JobFailureInfo.Type.PARSE, jobFailureInfo.getType());

  assertEquals(1, jobFailureInfo.getErrorsList().size());

  JobFailureInfo.Error error = jobFailureInfo.getErrorsList().get(0);
  assertEquals("test message", error.getMessage());
  assertEquals(7, (int) error.getStartLine());
  assertEquals(42, (int) error.getStartColumn());
  assertEquals(13, (int) error.getEndLine());
  assertEquals(57, (int) error.getEndColumn());
}
 
Example 11
Source Project: dremio-oss   Source File: TestQueryInfo.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void convertExceptionToQueryErrors() {
  // Fake logger to not pollute logs
  org.slf4j.Logger logger = mock(org.slf4j.Logger.class);

  SqlParseException parseException = new SqlParseException("test message", new SqlParserPos(7, 42, 13, 57), null, null, null);
  UserException userException = SqlExceptionHelper.parseError("SELECT FOO", parseException)
      .build(logger);

  List<QueryError> errors = QueryError.of(userException);

  assertEquals(1, errors.size());

  QueryError error = errors.get(0);
  assertEquals("test message", error.getMessage());
  assertEquals(7, error.getRange().getStartLine());
  assertEquals(42, error.getRange().getStartColumn());
  assertEquals(13, error.getRange().getEndLine());
  assertEquals(57, error.getRange().getEndColumn());
}
 
Example 12
Source Project: quark   Source File: ParserFactory.java    License: Apache License 2.0 6 votes vote down vote up
public Parser getParser(String sql, Properties info)
    throws SQLException {
  SqlParser parser = SqlParser.create(sql,
      SqlParser.configBuilder()
          .setQuotedCasing(Casing.UNCHANGED)
          .setUnquotedCasing(Casing.UNCHANGED)
          .setQuoting(Quoting.DOUBLE_QUOTE)
          .setParserFactory(QuarkParserImpl.FACTORY)
          .build());
  SqlNode sqlNode;
  try {
    sqlNode = parser.parseStmt();
  } catch (SqlParseException e) {
    throw new RuntimeException(
        "parse failed: " + e.getMessage(), e);
  }
  if (sqlNode.getKind().equals(SqlKind.OTHER_DDL)) {
    return new DDLParser();
  } else  {
    return getSqlQueryParser(info);
  }
}
 
Example 13
Source Project: calcite   Source File: LexEscapeTest.java    License: Apache License 2.0 6 votes vote down vote up
private static void runProjectQueryWithLex(Lex lex, String sql)
    throws SqlParseException, ValidationException, RelConversionException {
  Config javaLex = SqlParser.configBuilder().setLex(lex).build();
  Planner planner = getPlanner(null, javaLex, Programs.ofRules(Programs.RULE_SET));
  SqlNode parse = planner.parse(sql);
  SqlNode validate = planner.validate(parse);
  RelNode convert = planner.rel(validate).rel;
  assertThat(convert, instanceOf(LogicalProject.class));
  List<RelDataTypeField> fields = convert.getRowType().getFieldList();
  // Get field type from sql text and validate we parsed it after validation.
  assertThat(fields.size(), is(4));
  assertThat(fields.get(0).getType().getSqlTypeName(), is(SqlTypeName.VARCHAR));
  assertThat(fields.get(1).getType().getSqlTypeName(), is(SqlTypeName.TIME));
  assertThat(fields.get(2).getType().getSqlTypeName(), is(SqlTypeName.INTEGER));
  assertThat(fields.get(3).getType().getSqlTypeName(), is(SqlTypeName.TIMESTAMP));
}
 
Example 14
Source Project: calcite   Source File: AbstractSqlTester.java    License: Apache License 2.0 6 votes vote down vote up
protected void checkParseEx(Throwable e, String expectedMsgPattern, String sql) {
  try {
    throw e;
  } catch (SqlParseException spe) {
    String errMessage = spe.getMessage();
    if (expectedMsgPattern == null) {
      throw new RuntimeException("Error while parsing query:" + sql, spe);
    } else if (errMessage == null
        || !errMessage.matches(expectedMsgPattern)) {
      throw new RuntimeException("Error did not match expected ["
          + expectedMsgPattern + "] while parsing query ["
          + sql + "]", spe);
    }
  } catch (Throwable t) {
    throw new RuntimeException("Error while parsing query: " + sql, t);
  }
}
 
Example 15
Source Project: calcite   Source File: ExtensionDdlExecutor.java    License: Apache License 2.0 5 votes vote down vote up
/** Populates the table called {@code name} by executing {@code query}. */
protected static void populate(SqlIdentifier name, SqlNode query,
    CalcitePrepare.Context context) {
  // Generate, prepare and execute an "INSERT INTO table query" statement.
  // (It's a bit inefficient that we convert from SqlNode to SQL and back
  // again.)
  final FrameworkConfig config = Frameworks.newConfigBuilder()
      .defaultSchema(
          Objects.requireNonNull(
              Schemas.subSchema(context.getRootSchema(),
                  context.getDefaultSchemaPath())).plus())
      .build();
  final Planner planner = Frameworks.getPlanner(config);
  try {
    final StringBuilder buf = new StringBuilder();
    final SqlPrettyWriter w =
        new SqlPrettyWriter(
            SqlPrettyWriter.config()
                .withDialect(CalciteSqlDialect.DEFAULT)
                .withAlwaysUseParentheses(false),
            buf);
    buf.append("INSERT INTO ");
    name.unparse(w, 0, 0);
    buf.append(" ");
    query.unparse(w, 0, 0);
    final String sql = buf.toString();
    final SqlNode query1 = planner.parse(sql);
    final SqlNode query2 = planner.validate(query1);
    final RelRoot r = planner.rel(query2);
    final PreparedStatement prepare = context.getRelRunner().prepare(r.rel);
    int rowCount = prepare.executeUpdate();
    Util.discard(rowCount);
    prepare.close();
  } catch (SqlParseException | ValidationException
      | RelConversionException | SQLException e) {
    throw new RuntimeException(e);
  }
}
 
Example 16
Source Project: calcite   Source File: LexCaseSensitiveTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testCalciteCaseJoinMySqlAnsi()
    throws SqlParseException, ValidationException, RelConversionException {
  String sql = "select t.empid as EMPID, s.empid from\n"
      + "(select * from emps where emps.deptno > 100) t join\n"
      + "(select * from emps where emps.deptno < 200) s on t.empid = s.empid";
  runProjectQueryWithLex(Lex.MYSQL_ANSI, sql);
}
 
Example 17
Source Project: Bats   Source File: SqlConverter.java    License: Apache License 2.0 5 votes vote down vote up
public SqlNode parse(String sql) {
  try {
    SqlParser parser = SqlParser.create(sql, parserConfig);
    return parser.parseStmt();
  } catch (SqlParseException e) {
    UserException.Builder builder = UserException
        .parseError(e)
        .addContext("SQL Query", formatSQLParsingError(sql, e.getPos()));
    if (isInnerQuery) {
      builder.message("Failure parsing a view your query is dependent upon.");
    }
    throw builder.build(logger);
  }

}
 
Example 18
Source Project: flink   Source File: FlinkSqlParserImplTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = SqlParseException.class)
public void testInsertExtendedColumnAsStaticPartition2() {
	conformance0 = FlinkSqlConformance.HIVE;
	sql("insert into emps(x, y, z boolean) partition (z='ab') select * from emps")
		.node(new ValidationMatcher()
			.fails("Extended columns not allowed under the current SQL conformance level"));
}
 
Example 19
Source Project: calcite   Source File: LexCaseSensitiveTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testCalciteCaseJoinMySql()
    throws SqlParseException, ValidationException, RelConversionException {
  String sql = "select t.empid as EMPID, s.empid from\n"
      + "(select * from emps where emps.deptno > 100) t join\n"
      + "(select * from emps where emps.deptno < 200) s on t.empid = s.empid";
  runProjectQueryWithLex(Lex.MYSQL, sql);
}
 
Example 20
Source Project: alchemy   Source File: MysqlSideFunction.java    License: Apache License 2.0 5 votes vote down vote up
public MysqlSideFunction(SideTable sideTableInfo, MysqlSideProperties mysqlProperties) throws SqlParseException {
    this.sideTableInfo = sideTableInfo;
    this.sql = modifySql(sideTableInfo);
    this.vertx = createVertx();
    this.sqlClient = createClient(mysqlProperties, vertx);
    this.cache = create(this.sideTableInfo.getSide());
}
 
Example 21
Source Project: alchemy   Source File: MysqlSideFunction.java    License: Apache License 2.0 5 votes vote down vote up
private String modifySql(SideTable sideTable) throws SqlParseException {
    SqlParser.Config config = SqlParser.configBuilder().setLex(Lex.MYSQL).build();
    SqlParser sqlParser = SqlParser.create(sideTable.getSql(), config);
    SqlNode sqlNode = sqlParser.parseStmt();
    if (SqlKind.SELECT != sqlNode.getKind()) {
        throw new UnsupportedOperationException(
            "MysqlAsyncReqRow only support query sql, sql:" + sideTable.getSql());
    }
    SqlSelect sqlSelect = (SqlSelect)sqlNode;
    SqlNode whereNode = sqlSelect.getWhere();
    SqlBinaryOperator and = new SqlBinaryOperator("AND", SqlKind.AND, 24, true,
        ReturnTypes.BOOLEAN_NULLABLE_OPTIMIZED, InferTypes.BOOLEAN, OperandTypes.BOOLEAN_BOOLEAN);
    List<SqlBasicCall> conditionNodes = createConditionNodes(sideTable.getConditions(), sideTable.getSideAlias());
    List<SqlNode> nodes = new ArrayList<>();
    nodes.addAll(conditionNodes);
    if (whereNode != null) {
        nodes.add(whereNode);
    } else {
        SqlBinaryOperator equal = new SqlBinaryOperator("=", SqlKind.EQUALS, 30, true, ReturnTypes.BOOLEAN_NULLABLE,
            InferTypes.FIRST_KNOWN, OperandTypes.COMPARABLE_UNORDERED_COMPARABLE_UNORDERED);
        SqlBasicCall andEqual
            = new SqlBasicCall(equal, SideParser.createEqualNodes(SqlKind.AND), new SqlParserPos(0, 0));
        nodes.add(andEqual);
    }
    SqlBasicCall sqlBasicCall
        = new SqlBasicCall(and, nodes.toArray(new SqlNode[nodes.size()]), new SqlParserPos(0, 0));
    sqlSelect.setWhere(sqlBasicCall);
    return sqlSelect.toString();
}
 
Example 22
private Predicate getPredicate(String predicateString) {
  FilterContext filter;
  try {
    filter = QueryContextConverterUtils.getFilter(CalciteSqlParser.compileToExpression(predicateString));
  } catch (SqlParseException e) {
    throw new IllegalArgumentException("Invalid predicate string: " + predicateString);
  }
  // TODO: Add support for complex predicates with AND/OR.
  Preconditions.checkArgument(filter.getType() == FilterContext.Type.PREDICATE, "Invalid predicate string: %s",
      predicateString);
  return filter.getPredicate();
}
 
Example 23
Source Project: calcite   Source File: PlannerImpl.java    License: 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 24
Source Project: calcite   Source File: PlannerImpl.java    License: Apache License 2.0 5 votes vote down vote up
public SqlNode parse(final Reader reader) throws SqlParseException {
  switch (state) {
  case STATE_0_CLOSED:
  case STATE_1_RESET:
    ready();
  }
  ensure(State.STATE_2_READY);
  SqlParser parser = SqlParser.create(reader, parserConfig);
  SqlNode sqlNode = parser.parseStmt();
  state = State.STATE_3_PARSED;
  return sqlNode;
}
 
Example 25
Source Project: alchemy   Source File: SqlParseUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static String parseSinkName(String sql) throws SqlParseException {
    SqlParser sqlParser = SqlParser.create(sql, CONFIG);
    SqlNode sqlNode = sqlParser.parseStmt();
    SqlKind sqlKind = sqlNode.getKind();
    if (sqlKind != SqlKind.INSERT) {
        throw new IllegalArgumentException("It must be an insert SQL, sql:" + sql);
    }
    return findSinkName((SqlInsert)sqlNode);
}
 
Example 26
Source Project: kylin-on-parquet-v2   Source File: PushDownExecutor.java    License: Apache License 2.0 5 votes vote down vote up
private Pair<List<List<String>>, List<SelectedColumnMeta>> queryBySingleRunner(IPushDownRunner runner,
        String project, String sql, String defaultSchema, SQLException sqlException, boolean isSelect,
        boolean isPrepare) throws Exception {

    logger.debug("Query Pushdown runner {}", runner);

    // default schema in calcite does not apply to other engines.
    // since this is a universql requirement, it's not implemented as a converter
    if (defaultSchema != null && !defaultSchema.equals("DEFAULT")) {
        String completed = sql;
        try {
            completed = PushDownUtil.schemaCompletion(sql, defaultSchema);
        } catch (SqlParseException e) {
            // fail to parse the pushdown sql, ignore
            logger.debug("fail to do schema completion on the pushdown sql, ignore it.", e.getMessage());
        }
        if (!sql.equals(completed)) {
            logger.info("the query is converted to {} after schema completion", completed);
            sql = completed;
        }
    }

    sql = runner.convertSql(kylinConfig, sql, project, defaultSchema, isPrepare);

    List<List<String>> returnRows = Lists.newArrayList();
    List<SelectedColumnMeta> returnColumnMeta = Lists.newArrayList();

    if (isSelect) {
        runner.executeQuery(sql, returnRows, returnColumnMeta);
    }
    if (!isSelect && !isPrepare && kylinConfig.isPushDownUpdateEnabled()) {
        runner.executeUpdate(sql);
    }
    return Pair.newPair(returnRows, returnColumnMeta);
}
 
Example 27
Source Project: kylin-on-parquet-v2   Source File: PushDownUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSchemaCompletionWithComplexSubquery() throws SqlParseException {
    String sql = "SELECT a, b " + "FROM (" + "   SELECT c, d, sum(p) " + "   FROM table1 t1, DB.table2 t2 "
            + "   WHERE t1.c > t2.d " + "   GROUP BY t.e" + "   HAVING sum(p) > 100" + "   ORDER BY t2.f" + ") at1 "
            + "INNER JOIN table3 t3 " + "ON at1.c = t3.c " + "WHERE t3.d > 0 " + "ORDER BY t3.e";

    String exceptSQL = "SELECT a, b " + "FROM (" + "   SELECT c, d, sum(p) "
            + "   FROM EDW.table1 t1, DB.table2 t2 " + "   WHERE t1.c > t2.d " + "   GROUP BY t.e"
            + "   HAVING sum(p) > 100" + "   ORDER BY t2.f" + ") at1 " + "INNER JOIN EDW.table3 t3 "
            + "ON at1.c = t3.c " + "WHERE t3.d > 0 " + "ORDER BY t3.e";
    Assert.assertEquals(exceptSQL, PushDownUtil.schemaCompletion(sql, "EDW"));
}
 
Example 28
Source Project: kylin-on-parquet-v2   Source File: PushDownUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testWithSyntax() throws SqlParseException {
    String ori = "WITH tmp AS\n" + //
            "  (SELECT *\n" + //
            "   FROM t) \n" + //
            "SELECT a1\n" + //
            "FROM (\n" + //
            "  WITH tmp2 AS\n" + //
            "    (SELECT *\n" + //
            "     FROM t) \n" + //
            "  SELECT a1\n" + //
            "  FROM t2\n" + //
            "  ORDER BY c_customer_id\n" + //
            ")\n" + //
            "ORDER BY c_customer_id limit 5"; //
    String expected = "WITH tmp AS\n" + //
            "  (SELECT *\n" + //
            "   FROM EDW.t) \n" + //
            "SELECT a1\n" + //
            "FROM (\n" + //
            "  WITH tmp2 AS\n" + //
            "    (SELECT *\n" + //
            "     FROM EDW.t) \n" + //
            "  SELECT a1\n" + //
            "  FROM EDW.t2\n" + //
            "  ORDER BY c_customer_id\n" + //
            ")\n" + //
            "ORDER BY c_customer_id limit 5"; //
    Assert.assertEquals(expected, PushDownUtil.schemaCompletion(ori, "EDW"));
}
 
Example 29
Source Project: kylin-on-parquet-v2   Source File: CalciteParser.java    License: Apache License 2.0 5 votes vote down vote up
public static SqlNode getOnlySelectNode(String sql) {
    SqlNodeList selectList = null;
    try {
        selectList = ((SqlSelect) CalciteParser.parse(sql)).getSelectList();
    } catch (SqlParseException e) {
        throw new RuntimeException(
                "Failed to parse expression \'" + sql + "\', please make sure the expression is valid", e);
    }

    Preconditions.checkArgument(selectList.size() == 1,
            "Expression is invalid because size of select list exceeds one");

    return selectList.get(0);
}
 
Example 30
Source Project: kylin-on-parquet-v2   Source File: JdbcConnectorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBasics() throws SQLException, SqlParseException {
    Assert.assertNotNull(connector);
    Assert.assertNotNull(connector.getJdbcDriver());
    Assert.assertNotNull(connector.getJdbcUrl());
    Assert.assertNotNull(connector.getJdbcUser());
    Assert.assertNotNull(connector.getJdbcPassword());

    try (Connection conn = connector.getConnection()) {
        Assert.assertNotNull(conn);
        Assert.assertTrue(!conn.isClosed());
    }

    Assert.assertNotNull(connector.convertSql("select 1"));
    Assert.assertFalse(connector.listDatabases().isEmpty());
    Assert.assertFalse(connector.listDatabases().contains("EDW"));
    Assert.assertTrue(connector.listDatabases().contains("DEFAULT"));
    Assert.assertFalse(connector.listTables("DEFAULT").isEmpty());
    Assert.assertTrue(connector.listColumns("DEFAULT", "TEST_KYLIN_FACT").next());
    Assert.assertNotNull(connector.buildSqlToCreateSchema("NEW_SCHEMA"));
    Assert.assertNotNull(connector.buildSqlToCreateTable("NEW_TABLE", new LinkedHashMap<String, String>()));
    Assert.assertNotNull(connector.buildSqlToCreateView("NEW_VIEW", "select 1"));
    Assert.assertNotNull(connector.buildSqlToLoadDataFromLocal("TABLE", "/tmp"));

    connector.executeUpdate("select 1"); // expected no exceptions

    SqlConverter.IConfigurer configurer = connector.getSqlConverter().getConfigurer();
    Assert.assertTrue(configurer.allowFetchNoRows());
    Assert.assertTrue(configurer.allowNoOffset());
    Assert.assertTrue(configurer.allowNoOrderByWithFetch());
    Assert.assertFalse(configurer.skipHandleDefault());
    Assert.assertFalse(configurer.skipDefaultConvert());

    Assert.assertEquals(Types.DOUBLE, connector.toKylinTypeId("DOUBLE PRECISION", 0));
    Assert.assertEquals(Types.DOUBLE, connector.toKylinTypeId("double PRECISION", 0));
    Assert.assertEquals(Types.VARCHAR, connector.toKylinTypeId("CHARACTER VARYING", 0));
}