Java Code Examples for org.apache.calcite.sql.SqlExplain

The following examples show how to use org.apache.calcite.sql.SqlExplain. 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: ExplainHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public SqlNode rewrite(SqlNode sqlNode) throws RelConversionException, ForemanSetupException {
  SqlExplain node = unwrap(sqlNode, SqlExplain.class);
  SqlLiteral op = node.operand(2);
  SqlExplain.Depth depth = (SqlExplain.Depth) op.getValue();
  if (node.getDetailLevel() != null) {
    level = node.getDetailLevel();
  }
  switch (depth) {
  case LOGICAL:
    mode = ResultMode.LOGICAL;
    break;
  case PHYSICAL:
    mode = ResultMode.PHYSICAL;
    break;
  default:
    throw new UnsupportedOperationException("Unknown depth " + depth);
  }

  return node.operand(0);
}
 
Example 2
Source Project: Flink-CEPplus   Source File: SqlValidatorImpl.java    License: Apache License 2.0 5 votes vote down vote up
public List<List<String>> getFieldOrigins(SqlNode sqlQuery) {
	if (sqlQuery instanceof SqlExplain) {
		return Collections.emptyList();
	}
	final RelDataType rowType = getValidatedNodeType(sqlQuery);
	final int fieldCount = rowType.getFieldCount();
	if (!sqlQuery.isA(SqlKind.QUERY)) {
		return Collections.nCopies(fieldCount, null);
	}
	final List<List<String>> list = new ArrayList<>();
	for (int i = 0; i < fieldCount; i++) {
		list.add(getFieldOrigin(sqlQuery, i));
	}
	return ImmutableNullableList.copyOf(list);
}
 
Example 3
Source Project: flink   Source File: SqlValidatorImpl.java    License: Apache License 2.0 5 votes vote down vote up
public List<List<String>> getFieldOrigins(SqlNode sqlQuery) {
	if (sqlQuery instanceof SqlExplain) {
		return Collections.emptyList();
	}
	final RelDataType rowType = getValidatedNodeType(sqlQuery);
	final int fieldCount = rowType.getFieldCount();
	if (!sqlQuery.isA(SqlKind.QUERY)) {
		return Collections.nCopies(fieldCount, null);
	}
	final List<List<String>> list = new ArrayList<>();
	for (int i = 0; i < fieldCount; i++) {
		list.add(getFieldOrigin(sqlQuery, i));
	}
	return ImmutableNullableList.copyOf(list);
}
 
Example 4
Source Project: flink   Source File: SqlToOperationConverter.java    License: Apache License 2.0 5 votes vote down vote up
/** Convert EXPLAIN statement. */
private Operation convertExplain(SqlExplain sqlExplain) {
	Operation operation = convertSqlQuery(sqlExplain.getExplicandum());

	if (sqlExplain.getDetailLevel() != SqlExplainLevel.EXPPLAN_ATTRIBUTES ||
			sqlExplain.getDepth() != SqlExplain.Depth.PHYSICAL ||
			sqlExplain.getFormat() != SqlExplainFormat.TEXT) {
		throw new TableException("Only default behavior is supported now, EXPLAIN PLAN FOR xx");
	}

	return new ExplainOperation(operation);
}
 
Example 5
Source Project: flink   Source File: SqlToOperationConverter.java    License: Apache License 2.0 5 votes vote down vote up
/** Convert EXPLAIN statement. */
private Operation convertExplain(SqlExplain sqlExplain) {
	Operation operation = convertSqlQuery(sqlExplain.getExplicandum());

	if (sqlExplain.getDetailLevel() != SqlExplainLevel.EXPPLAN_ATTRIBUTES ||
			sqlExplain.getDepth() != SqlExplain.Depth.PHYSICAL ||
			sqlExplain.getFormat() != SqlExplainFormat.TEXT) {
		throw new TableException("Only default behavior is supported now, EXPLAIN PLAN FOR xx");
	}

	return new ExplainOperation(operation);
}
 
Example 6
Source Project: dremio-oss   Source File: ExplainHandler.java    License: Apache License 2.0 4 votes vote down vote up
@Override
  public List<Explain> toResult(String sql, SqlNode sqlNode) throws Exception {
    try {
      final SqlExplain node = SqlNodeUtil.unwrap(sqlNode, SqlExplain.class);
      final SqlLiteral op = node.operand(2);
      final SqlExplain.Depth depth = (SqlExplain.Depth) op.getValue();

      final ResultMode mode;
      SqlExplainLevel level = SqlExplainLevel.ALL_ATTRIBUTES;

      if (node.getDetailLevel() != null) {
        level = node.getDetailLevel();
      }

      switch (depth) {
      case LOGICAL:
        mode = ResultMode.LOGICAL;
        break;
      case PHYSICAL:
        mode = ResultMode.PHYSICAL;
        break;
      default:
        throw new UnsupportedOperationException("Unknown depth " + depth);
      }

      final SqlNode innerNode = node.operand(0);

//      try(DisabledBlock block = toggle.openDisabledBlock()){
        Rel drel;
        final ConvertedRelNode convertedRelNode = PrelTransformer.validateAndConvert(config, innerNode);
        final RelDataType validatedRowType = convertedRelNode.getValidatedRowType();
        final RelNode queryRelNode = convertedRelNode.getConvertedNode();

        PrelTransformer.log("Calcite", queryRelNode, logger, null);
        drel = PrelTransformer.convertToDrel(config, queryRelNode, validatedRowType);

        if (mode == ResultMode.LOGICAL) {
          return Collections.singletonList(new Explain(RelOptUtil.toString(drel, level)));
        }

        final Pair<Prel, String> convertToPrel = PrelTransformer.convertToPrel(config, drel);
        final String text = convertToPrel.getValue();
        return Collections.singletonList(new Explain(text));
//      }
    } catch (Exception ex){
      throw SqlExceptionHelper.coerceException(logger, sql, ex, true);
    }
  }
 
Example 7
Source Project: flink   Source File: SqlToOperationConverter.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * This is the main entrance for executing all kinds of DDL/DML {@code SqlNode}s, different
 * SqlNode will have it's implementation in the #convert(type) method whose 'type' argument
 * is subclass of {@code SqlNode}.
 *
 * @param flinkPlanner     FlinkPlannerImpl to convert sql node to rel node
 * @param sqlNode          SqlNode to execute on
 */
public static Optional<Operation> convert(
		FlinkPlannerImpl flinkPlanner,
		CatalogManager catalogManager,
		SqlNode sqlNode) {
	// validate the query
	final SqlNode validated = flinkPlanner.validate(sqlNode);
	SqlToOperationConverter converter = new SqlToOperationConverter(flinkPlanner, catalogManager);
	if (validated instanceof SqlCreateTable) {
		return Optional.of(converter.convertCreateTable((SqlCreateTable) validated));
	} else if (validated instanceof SqlDropTable) {
		return Optional.of(converter.convertDropTable((SqlDropTable) validated));
	} else if (validated instanceof SqlAlterTable) {
		return Optional.of(converter.convertAlterTable((SqlAlterTable) validated));
	} else if (validated instanceof SqlCreateFunction) {
		return Optional.of(converter.convertCreateFunction((SqlCreateFunction) validated));
	} else if (validated instanceof SqlAlterFunction) {
		return Optional.of(converter.convertAlterFunction((SqlAlterFunction) validated));
	} else if (validated instanceof SqlDropFunction) {
		return Optional.of(converter.convertDropFunction((SqlDropFunction) validated));
	} else if (validated instanceof RichSqlInsert) {
		SqlNodeList targetColumnList = ((RichSqlInsert) validated).getTargetColumnList();
		if (targetColumnList != null && targetColumnList.size() != 0) {
			throw new ValidationException("Partial inserts are not supported");
		}
		return Optional.of(converter.convertSqlInsert((RichSqlInsert) validated));
	} else if (validated instanceof SqlUseCatalog) {
		return Optional.of(converter.convertUseCatalog((SqlUseCatalog) validated));
	} else if (validated instanceof SqlUseDatabase) {
		return Optional.of(converter.convertUseDatabase((SqlUseDatabase) validated));
	} else if (validated instanceof SqlCreateDatabase) {
		return Optional.of(converter.convertCreateDatabase((SqlCreateDatabase) validated));
	} else if (validated instanceof SqlDropDatabase) {
		return Optional.of(converter.convertDropDatabase((SqlDropDatabase) validated));
	} else if (validated instanceof SqlAlterDatabase) {
		return Optional.of(converter.convertAlterDatabase((SqlAlterDatabase) validated));
	} else if (validated instanceof SqlShowCatalogs) {
		return Optional.of(converter.convertShowCatalogs((SqlShowCatalogs) validated));
	} else if (validated instanceof SqlShowDatabases) {
		return Optional.of(converter.convertShowDatabases((SqlShowDatabases) validated));
	} else if (validated instanceof SqlShowTables) {
		return Optional.of(converter.convertShowTables((SqlShowTables) validated));
	} else if (validated instanceof SqlShowFunctions) {
		return Optional.of(converter.convertShowFunctions((SqlShowFunctions) validated));
	} else if (validated instanceof SqlCreateView) {
		return Optional.of(converter.convertCreateView((SqlCreateView) validated));
	} else if (validated instanceof SqlDropView) {
		return Optional.of(converter.convertDropView((SqlDropView) validated));
	} else if (validated instanceof SqlShowViews) {
		return Optional.of(converter.convertShowViews((SqlShowViews) validated));
	} else if (validated instanceof SqlExplain) {
		return Optional.of(converter.convertExplain((SqlExplain) validated));
	} else if (validated instanceof SqlRichDescribeTable) {
		return Optional.of(converter.convertDescribeTable((SqlRichDescribeTable) validated));
	} else if (validated.getKind().belongsTo(SqlKind.QUERY)) {
		return Optional.of(converter.convertSqlQuery(validated));
	} else {
		return Optional.empty();
	}
}
 
Example 8
Source Project: flink   Source File: SqlToOperationConverter.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * This is the main entrance for executing all kinds of DDL/DML {@code SqlNode}s, different
 * SqlNode will have it's implementation in the #convert(type) method whose 'type' argument
 * is subclass of {@code SqlNode}.
 *
 * @param flinkPlanner FlinkPlannerImpl to convertCreateTable sql node to rel node
 * @param catalogManager CatalogManager to resolve full path for operations
 * @param sqlNode SqlNode to execute on
 */
public static Optional<Operation> convert(
		FlinkPlannerImpl flinkPlanner,
		CatalogManager catalogManager,
		SqlNode sqlNode) {
	// validate the query
	final SqlNode validated = flinkPlanner.validate(sqlNode);
	SqlToOperationConverter converter = new SqlToOperationConverter(flinkPlanner, catalogManager);
	if (validated instanceof SqlCreateTable) {
		return Optional.of(converter.createTableConverter.convertCreateTable((SqlCreateTable) validated));
	} else if (validated instanceof SqlDropTable) {
		return Optional.of(converter.convertDropTable((SqlDropTable) validated));
	} else if (validated instanceof SqlAlterTable) {
		return Optional.of(converter.convertAlterTable((SqlAlterTable) validated));
	} else if (validated instanceof SqlAlterView) {
		return Optional.of(converter.convertAlterView((SqlAlterView) validated));
	} else if (validated instanceof SqlCreateFunction) {
		return Optional.of(converter.convertCreateFunction((SqlCreateFunction) validated));
	} else if (validated instanceof SqlAlterFunction) {
		return Optional.of(converter.convertAlterFunction((SqlAlterFunction) validated));
	} else if (validated instanceof SqlDropFunction) {
		return Optional.of(converter.convertDropFunction((SqlDropFunction) validated));
	} else if (validated instanceof RichSqlInsert) {
		return Optional.of(converter.convertSqlInsert((RichSqlInsert) validated));
	} else if (validated instanceof SqlUseCatalog) {
		return Optional.of(converter.convertUseCatalog((SqlUseCatalog) validated));
	} else if (validated instanceof SqlUseDatabase) {
		return Optional.of(converter.convertUseDatabase((SqlUseDatabase) validated));
	} else if (validated instanceof SqlCreateDatabase) {
		return Optional.of(converter.convertCreateDatabase((SqlCreateDatabase) validated));
	} else if (validated instanceof SqlDropDatabase) {
		return Optional.of(converter.convertDropDatabase((SqlDropDatabase) validated));
	} else if (validated instanceof SqlAlterDatabase) {
		return Optional.of(converter.convertAlterDatabase((SqlAlterDatabase) validated));
	} else if (validated instanceof SqlCreateCatalog) {
		return Optional.of(converter.convertCreateCatalog((SqlCreateCatalog) validated));
	} else if (validated instanceof SqlDropCatalog) {
		return Optional.of(converter.convertDropCatalog((SqlDropCatalog) validated));
	} else if (validated instanceof SqlShowCatalogs) {
		return Optional.of(converter.convertShowCatalogs((SqlShowCatalogs) validated));
	} else if (validated instanceof SqlShowDatabases) {
		return Optional.of(converter.convertShowDatabases((SqlShowDatabases) validated));
	} else if (validated instanceof SqlShowTables) {
		return Optional.of(converter.convertShowTables((SqlShowTables) validated));
	} else if (validated instanceof SqlShowFunctions) {
		return Optional.of(converter.convertShowFunctions((SqlShowFunctions) validated));
	} else if (validated instanceof SqlCreateView) {
		return Optional.of(converter.convertCreateView((SqlCreateView) validated));
	} else if (validated instanceof SqlDropView) {
		return Optional.of(converter.convertDropView((SqlDropView) validated));
	} else if (validated instanceof SqlShowViews) {
		return Optional.of(converter.convertShowViews((SqlShowViews) validated));
	} else if (validated instanceof SqlExplain) {
		return Optional.of(converter.convertExplain((SqlExplain) validated));
	} else if (validated instanceof SqlRichDescribeTable) {
		return Optional.of(converter.convertDescribeTable((SqlRichDescribeTable) validated));
	} else if (validated.getKind().belongsTo(SqlKind.QUERY)) {
		return Optional.of(converter.convertSqlQuery(validated));
	} else {
		return Optional.empty();
	}
}