Java Code Examples for org.apache.calcite.sql.SqlNodeList#EMPTY

The following examples show how to use org.apache.calcite.sql.SqlNodeList#EMPTY . 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: RichSqlInsert.java    From flink with Apache License 2.0 6 votes vote down vote up
public RichSqlInsert(SqlParserPos pos,
		SqlNodeList keywords,
		SqlNodeList extendedKeywords,
		SqlNode targetTable,
		SqlNode source,
		SqlNodeList columnList,
		SqlNodeList staticPartitions) {
	super(pos, keywords, targetTable, source, columnList);
	this.extendedKeywords = extendedKeywords;
	this.staticPartitions = staticPartitions;
	if (targetTable instanceof SqlTableRef) {
		SqlTableRef tableRef = (SqlTableRef) targetTable;
		this.targetTableID = tableRef.operand(0);
		this.tableHints = tableRef.operand(1);
	} else {
		this.targetTableID = targetTable;
		this.tableHints = SqlNodeList.EMPTY;
	}
}
 
Example 2
Source File: RelToSqlConverter.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Result visit(TableFunctionScan e) {
  final List<SqlNode> inputSqlNodes = new ArrayList<>();
  final int inputSize = e.getInputs().size();
  for (int i = 0; i < inputSize; i++) {
    Result child = visitChild(i, e.getInput(i));
    inputSqlNodes.add(child.asStatement());
  }
  final Context context = tableFunctionScanContext(inputSqlNodes);
  SqlNode callNode = context.toSql(null, e.getCall());
  // Convert to table function call, "TABLE($function_name(xxx))"
  SqlNode tableCall = new SqlBasicCall(
      SqlStdOperatorTable.COLLECTION_TABLE,
      new SqlNode[]{callNode},
      SqlParserPos.ZERO);
  SqlNode select = new SqlSelect(
      SqlParserPos.ZERO, null, null, tableCall,
      null, null, null, null, null, null, null, SqlNodeList.EMPTY);
  return result(select, ImmutableList.of(Clause.SELECT), e, null);
}
 
Example 3
Source File: MysqlSqlDialect.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public SqlNode rewriteSingleValueExpr(SqlNode aggCall) {
  final SqlNode operand = ((SqlBasicCall) aggCall).operand(0);
  final SqlLiteral nullLiteral = SqlLiteral.createNull(SqlParserPos.ZERO);
  final SqlNode unionOperand = new SqlSelect(SqlParserPos.ZERO, SqlNodeList.EMPTY,
      SqlNodeList.of(nullLiteral), null, null, null, null, SqlNodeList.EMPTY, null, null, null);
  // For MySQL, generate
  //   CASE COUNT(*)
  //   WHEN 0 THEN NULL
  //   WHEN 1 THEN <result>
  //   ELSE (SELECT NULL UNION ALL SELECT NULL)
  //   END
  final SqlNode caseExpr =
      new SqlCase(SqlParserPos.ZERO,
          SqlStdOperatorTable.COUNT.createCall(SqlParserPos.ZERO, operand),
          SqlNodeList.of(
              SqlLiteral.createExactNumeric("0", SqlParserPos.ZERO),
              SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO)),
          SqlNodeList.of(
              nullLiteral,
              operand),
          SqlStdOperatorTable.SCALAR_QUERY.createCall(SqlParserPos.ZERO,
              SqlStdOperatorTable.UNION_ALL
                  .createCall(SqlParserPos.ZERO, unionOperand, unionOperand)));

  LOGGER.debug("SINGLE_VALUE rewritten into [{}]", caseExpr);

  return caseExpr;
}
 
Example 4
Source File: DrillSqlWorker.java    From Bats with Apache License 2.0 5 votes vote down vote up
private static SqlNode wrapWithAutoLimit(SqlNode sqlNode, int queryMaxRows) {
  SqlNumericLiteral autoLimitLiteral = SqlLiteral.createExactNumeric(String.valueOf(queryMaxRows), SqlParserPos.ZERO);
  if (sqlNode.getKind() == SqlKind.ORDER_BY) {
    SqlOrderBy orderBy = (SqlOrderBy) sqlNode;
    return new SqlOrderBy(orderBy.getParserPosition(), orderBy.query, orderBy.orderList, orderBy.offset, autoLimitLiteral);
  }
  return new SqlOrderBy(SqlParserPos.ZERO, sqlNode, SqlNodeList.EMPTY, null, autoLimitLiteral);
}
 
Example 5
Source File: JdbcTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
SqlString generateSql() {
  final SqlNodeList selectList = SqlNodeList.SINGLETON_STAR;
  SqlSelect node =
      new SqlSelect(SqlParserPos.ZERO, SqlNodeList.EMPTY, selectList,
          tableName(), null, null, null, null, null, null, null, null);
  final SqlWriterConfig config = SqlPrettyWriter.config()
      .withAlwaysUseParentheses(true)
      .withDialect(jdbcSchema.dialect);
  final SqlPrettyWriter writer = new SqlPrettyWriter(config);
  node.unparse(writer, 0, 0);
  return writer.toSqlString();
}
 
Example 6
Source File: MysqlSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public SqlNode rewriteSingleValueExpr(SqlNode aggCall) {
  final SqlNode operand = ((SqlBasicCall) aggCall).operand(0);
  final SqlLiteral nullLiteral = SqlLiteral.createNull(SqlParserPos.ZERO);
  final SqlNode unionOperand = new SqlSelect(SqlParserPos.ZERO, SqlNodeList.EMPTY,
      SqlNodeList.of(nullLiteral), null, null, null, null,
      SqlNodeList.EMPTY, null, null, null, SqlNodeList.EMPTY);
  // For MySQL, generate
  //   CASE COUNT(*)
  //   WHEN 0 THEN NULL
  //   WHEN 1 THEN <result>
  //   ELSE (SELECT NULL UNION ALL SELECT NULL)
  //   END
  final SqlNode caseExpr =
      new SqlCase(SqlParserPos.ZERO,
          SqlStdOperatorTable.COUNT.createCall(SqlParserPos.ZERO, operand),
          SqlNodeList.of(
              SqlLiteral.createExactNumeric("0", SqlParserPos.ZERO),
              SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO)),
          SqlNodeList.of(
              nullLiteral,
              operand),
          SqlStdOperatorTable.SCALAR_QUERY.createCall(SqlParserPos.ZERO,
              SqlStdOperatorTable.UNION_ALL
                  .createCall(SqlParserPos.ZERO, unionOperand, unionOperand)));

  LOGGER.debug("SINGLE_VALUE rewritten into [{}]", caseExpr);

  return caseExpr;
}
 
Example 7
Source File: AnalyzeTableHandler.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public PhysicalPlan getPlan(SqlNode sqlNode)
    throws ValidationException, RelConversionException, IOException, ForemanSetupException {
  final SqlAnalyzeTable sqlAnalyzeTable = unwrap(sqlNode, SqlAnalyzeTable.class);

  verifyNoUnsupportedFunctions(sqlAnalyzeTable);

  SqlIdentifier tableIdentifier = sqlAnalyzeTable.getTableIdentifier();
  SqlSelect scanSql = new SqlSelect(
      SqlParserPos.ZERO,              /* position */
      SqlNodeList.EMPTY,              /* keyword list */
      getColumnList(sqlAnalyzeTable), /* select list */
      tableIdentifier,                /* from */
      null,                           /* where */
      null,                           /* group by */
      null,                           /* having */
      null,                           /* windowDecls */
      null,                           /* orderBy */
      null,                           /* offset */
      null                            /* fetch */
  );

  final ConvertedRelNode convertedRelNode = validateAndConvert(rewrite(scanSql));
  final RelDataType validatedRowType = convertedRelNode.getValidatedRowType();

  final RelNode relScan = convertedRelNode.getConvertedNode();
  final String tableName = sqlAnalyzeTable.getName();
  final AbstractSchema drillSchema = SchemaUtilites.resolveToDrillSchema(
      config.getConverter().getDefaultSchema(), sqlAnalyzeTable.getSchemaPath());
  Table table = SqlHandlerUtil.getTableFromSchema(drillSchema, tableName);

  if (table == null) {
    throw UserException.validationError()
        .message("No table with given name [%s] exists in schema [%s]", tableName,
            drillSchema.getFullSchemaName())
        .build(logger);
  }

  if(! (table instanceof DrillTable)) {
    return DrillStatsTable.notSupported(context, tableName);
  }

  if (table instanceof DrillTable) {
    DrillTable drillTable = (DrillTable) table;
    final Object selection = drillTable.getSelection();
    if (!(selection instanceof FormatSelection)) {
      return DrillStatsTable.notSupported(context, tableName);
    }
    // Do not support non-parquet tables
    FormatSelection formatSelection = (FormatSelection) selection;
    FormatPluginConfig formatConfig = formatSelection.getFormat();
    if (!((formatConfig instanceof ParquetFormatConfig)
          || ((formatConfig instanceof NamedFormatPluginConfig)
               && ((NamedFormatPluginConfig) formatConfig).name.equals("parquet")))) {
      return DrillStatsTable.notSupported(context, tableName);
    }

    FileSystemPlugin plugin = (FileSystemPlugin) drillTable.getPlugin();
    DrillFileSystem fs = new DrillFileSystem(plugin.getFormatPlugin(
        formatSelection.getFormat()).getFsConf());

    Path selectionRoot = formatSelection.getSelection().getSelectionRoot();
    if (!selectionRoot.toUri().getPath().endsWith(tableName) || !fs.getFileStatus(selectionRoot).isDirectory()) {
      return DrillStatsTable.notSupported(context, tableName);
    }
    // Do not recompute statistics, if stale
    Path statsFilePath = new Path(selectionRoot, DotDrillType.STATS.getEnding());
    if (fs.exists(statsFilePath) && !isStatsStale(fs, statsFilePath)) {
     return DrillStatsTable.notRequired(context, tableName);
    }
  }
  // Convert the query to Drill Logical plan and insert a writer operator on top.
  DrillRel drel = convertToDrel(relScan, drillSchema, tableName, sqlAnalyzeTable.getSamplePercent());
  Prel prel = convertToPrel(drel, validatedRowType);
  logAndSetTextPlan("Drill Physical", prel, logger);
  PhysicalOperator pop = convertToPop(prel);
  PhysicalPlan plan = convertToPlan(pop);
  log("Drill Plan", plan, logger);

  return plan;
}