Search in sources :

Example 46 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList in project flink by apache.

the class SqlValidatorImpl method createSourceSelectForDelete.

/**
 * Creates the SELECT statement that putatively feeds rows into a DELETE statement to be
 * deleted.
 *
 * @param call Call to the DELETE operator
 * @return select statement
 */
protected SqlSelect createSourceSelectForDelete(SqlDelete call) {
    final SqlNodeList selectList = new SqlNodeList(SqlParserPos.ZERO);
    selectList.add(SqlIdentifier.star(SqlParserPos.ZERO));
    SqlNode sourceTable = call.getTargetTable();
    if (call.getAlias() != null) {
        sourceTable = SqlValidatorUtil.addAlias(sourceTable, call.getAlias().getSimple());
    }
    return new SqlSelect(SqlParserPos.ZERO, null, selectList, sourceTable, call.getCondition(), null, null, null, null, null, null, null);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 47 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList in project flink by apache.

the class SqlValidatorImpl method createSourceSelectForUpdate.

/**
 * Creates the SELECT statement that putatively feeds rows into an UPDATE statement to be
 * updated.
 *
 * @param call Call to the UPDATE operator
 * @return select statement
 */
protected SqlSelect createSourceSelectForUpdate(SqlUpdate call) {
    final SqlNodeList selectList = new SqlNodeList(SqlParserPos.ZERO);
    selectList.add(SqlIdentifier.star(SqlParserPos.ZERO));
    int ordinal = 0;
    for (SqlNode exp : call.getSourceExpressionList()) {
        // Force unique aliases to avoid a duplicate for Y with
        // SET X=Y
        String alias = SqlUtil.deriveAliasFromOrdinal(ordinal);
        selectList.add(SqlValidatorUtil.addAlias(exp, alias));
        ++ordinal;
    }
    SqlNode sourceTable = call.getTargetTable();
    if (call.getAlias() != null) {
        sourceTable = SqlValidatorUtil.addAlias(sourceTable, call.getAlias().getSimple());
    }
    return new SqlSelect(SqlParserPos.ZERO, null, selectList, sourceTable, call.getCondition(), null, null, null, null, null, null, null);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlNodeList(org.apache.calcite.sql.SqlNodeList) BitString(org.apache.calcite.util.BitString) SqlNode(org.apache.calcite.sql.SqlNode)

Example 48 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList in project flink by apache.

the class SqlValidatorImpl method validateOrderList.

/**
 * Validates the ORDER BY clause of a SELECT statement.
 *
 * @param select Select statement
 */
protected void validateOrderList(SqlSelect select) {
    // ORDER BY is validated in a scope where aliases in the SELECT clause
    // are visible. For example, "SELECT empno AS x FROM emp ORDER BY x"
    // is valid.
    SqlNodeList orderList = select.getOrderList();
    if (orderList == null) {
        return;
    }
    if (!shouldAllowIntermediateOrderBy()) {
        if (!cursorSet.contains(select)) {
            throw newValidationError(select, RESOURCE.invalidOrderByPos());
        }
    }
    final SqlValidatorScope orderScope = getOrderScope(select);
    Objects.requireNonNull(orderScope);
    List<SqlNode> expandList = new ArrayList<>();
    for (SqlNode orderItem : orderList) {
        SqlNode expandedOrderItem = expand(orderItem, orderScope);
        expandList.add(expandedOrderItem);
    }
    SqlNodeList expandedOrderList = new SqlNodeList(expandList, orderList.getParserPosition());
    select.setOrderBy(expandedOrderList);
    for (SqlNode orderItem : expandedOrderList) {
        validateOrderItem(select, orderItem);
    }
}
Also used : ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 49 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList in project flink by apache.

the class SqlToOperationConverter method convertCreateView.

/**
 * Convert CREATE VIEW statement.
 */
private Operation convertCreateView(SqlCreateView sqlCreateView) {
    final SqlNode query = sqlCreateView.getQuery();
    final SqlNodeList fieldList = sqlCreateView.getFieldList();
    UnresolvedIdentifier unresolvedIdentifier = UnresolvedIdentifier.of(sqlCreateView.fullViewName());
    ObjectIdentifier identifier = catalogManager.qualifyIdentifier(unresolvedIdentifier);
    String comment = sqlCreateView.getComment().map(c -> c.getNlsString().getValue()).orElse(null);
    CatalogView catalogView = convertViewQuery(query, fieldList.getList(), OperationConverterUtils.extractProperties(sqlCreateView.getProperties().orElse(null)), comment);
    return new CreateViewOperation(identifier, catalogView, sqlCreateView.isIfNotExists(), sqlCreateView.isTemporary());
}
Also used : SqlAlterTableReset(org.apache.flink.sql.parser.ddl.SqlAlterTableReset) ModifyOperation(org.apache.flink.table.operations.ModifyOperation) SqlShowCurrentCatalog(org.apache.flink.sql.parser.dql.SqlShowCurrentCatalog) UnresolvedIdentifier(org.apache.flink.table.catalog.UnresolvedIdentifier) SqlTableOption(org.apache.flink.sql.parser.ddl.SqlTableOption) FlinkPlannerImpl(org.apache.flink.table.planner.calcite.FlinkPlannerImpl) CatalogTable(org.apache.flink.table.catalog.CatalogTable) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) ShowCurrentDatabaseOperation(org.apache.flink.table.operations.ShowCurrentDatabaseOperation) SqlDropPartitions(org.apache.flink.sql.parser.ddl.SqlDropPartitions) SqlShowViews(org.apache.flink.sql.parser.dql.SqlShowViews) SqlAlterFunction(org.apache.flink.sql.parser.ddl.SqlAlterFunction) SqlEndStatementSet(org.apache.flink.sql.parser.dml.SqlEndStatementSet) SqlRichDescribeTable(org.apache.flink.sql.parser.dql.SqlRichDescribeTable) SqlShowPartitions(org.apache.flink.sql.parser.dql.SqlShowPartitions) Map(java.util.Map) SqlRemoveJar(org.apache.flink.sql.parser.ddl.SqlRemoveJar) FlinkHints(org.apache.flink.table.planner.hint.FlinkHints) CatalogPartitionImpl(org.apache.flink.table.catalog.CatalogPartitionImpl) SqlCreateTable(org.apache.flink.sql.parser.ddl.SqlCreateTable) SqlShowCreateTable(org.apache.flink.sql.parser.dql.SqlShowCreateTable) ExecutePlanOperation(org.apache.flink.table.operations.command.ExecutePlanOperation) DropDatabaseOperation(org.apache.flink.table.operations.ddl.DropDatabaseOperation) SqlUseModules(org.apache.flink.sql.parser.ddl.SqlUseModules) SqlUseDatabase(org.apache.flink.sql.parser.ddl.SqlUseDatabase) SinkModifyOperation(org.apache.flink.table.operations.SinkModifyOperation) SqlChangeColumn(org.apache.flink.sql.parser.ddl.SqlChangeColumn) ShowColumnsOperation(org.apache.flink.table.operations.ShowColumnsOperation) SqlKind(org.apache.calcite.sql.SqlKind) SqlAlterTable(org.apache.flink.sql.parser.ddl.SqlAlterTable) AlterTableOptionsOperation(org.apache.flink.table.operations.ddl.AlterTableOptionsOperation) AlterTableDropConstraintOperation(org.apache.flink.table.operations.ddl.AlterTableDropConstraintOperation) Set(java.util.Set) TableSchema(org.apache.flink.table.api.TableSchema) SqlAddReplaceColumns(org.apache.flink.sql.parser.ddl.SqlAddReplaceColumns) CompilePlanOperation(org.apache.flink.table.operations.ddl.CompilePlanOperation) CreateCatalogOperation(org.apache.flink.table.operations.ddl.CreateCatalogOperation) SqlDropDatabase(org.apache.flink.sql.parser.ddl.SqlDropDatabase) ShowCreateViewOperation(org.apache.flink.table.operations.ShowCreateViewOperation) OperationConverterUtils(org.apache.flink.table.planner.utils.OperationConverterUtils) UseCatalogOperation(org.apache.flink.table.operations.UseCatalogOperation) UseDatabaseOperation(org.apache.flink.table.operations.UseDatabaseOperation) CatalogFunction(org.apache.flink.table.catalog.CatalogFunction) FactoryUtil(org.apache.flink.table.factories.FactoryUtil) ShowCatalogsOperation(org.apache.flink.table.operations.ShowCatalogsOperation) CatalogDatabaseImpl(org.apache.flink.table.catalog.CatalogDatabaseImpl) SqlAlterTableAddConstraint(org.apache.flink.sql.parser.ddl.SqlAlterTableAddConstraint) RichSqlInsert(org.apache.flink.sql.parser.dml.RichSqlInsert) CreateViewOperation(org.apache.flink.table.operations.ddl.CreateViewOperation) ShowJarsOperation(org.apache.flink.table.operations.command.ShowJarsOperation) AlterDatabaseOperation(org.apache.flink.table.operations.ddl.AlterDatabaseOperation) CatalogDatabase(org.apache.flink.table.catalog.CatalogDatabase) SqlAlterViewRename(org.apache.flink.sql.parser.ddl.SqlAlterViewRename) QueryOperation(org.apache.flink.table.operations.QueryOperation) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier) SqlAlterDatabase(org.apache.flink.sql.parser.ddl.SqlAlterDatabase) CompileAndExecutePlanOperation(org.apache.flink.table.operations.CompileAndExecutePlanOperation) CatalogFunctionImpl(org.apache.flink.table.catalog.CatalogFunctionImpl) SqlUnloadModule(org.apache.flink.sql.parser.dql.SqlUnloadModule) DatabaseNotExistException(org.apache.flink.table.catalog.exceptions.DatabaseNotExistException) EndStatementSetOperation(org.apache.flink.table.operations.EndStatementSetOperation) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SqlShowCreateView(org.apache.flink.sql.parser.dql.SqlShowCreateView) SqlAlterView(org.apache.flink.sql.parser.ddl.SqlAlterView) UseModulesOperation(org.apache.flink.table.operations.UseModulesOperation) CatalogView(org.apache.flink.table.catalog.CatalogView) Catalog(org.apache.flink.table.catalog.Catalog) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) DropCatalogFunctionOperation(org.apache.flink.table.operations.ddl.DropCatalogFunctionOperation) Expander(org.apache.flink.table.planner.utils.Expander) CalciteSqlDialect(org.apache.calcite.sql.dialect.CalciteSqlDialect) ShowTablesOperation(org.apache.flink.table.operations.ShowTablesOperation) SqlDropTable(org.apache.flink.sql.parser.ddl.SqlDropTable) DescribeTableOperation(org.apache.flink.table.operations.DescribeTableOperation) SqlLoadModule(org.apache.flink.sql.parser.dql.SqlLoadModule) ShowCurrentCatalogOperation(org.apache.flink.table.operations.ShowCurrentCatalogOperation) FunctionScope(org.apache.flink.table.operations.ShowFunctionsOperation.FunctionScope) SqlCreateFunction(org.apache.flink.sql.parser.ddl.SqlCreateFunction) SqlAddJar(org.apache.flink.sql.parser.ddl.SqlAddJar) SqlCreateDatabase(org.apache.flink.sql.parser.ddl.SqlCreateDatabase) TableException(org.apache.flink.table.api.TableException) SqlTableConstraint(org.apache.flink.sql.parser.ddl.constraint.SqlTableConstraint) SqlAddPartitions(org.apache.flink.sql.parser.ddl.SqlAddPartitions) ShowFunctionsOperation(org.apache.flink.table.operations.ShowFunctionsOperation) SqlCompileAndExecutePlan(org.apache.flink.sql.parser.dml.SqlCompileAndExecutePlan) CreateDatabaseOperation(org.apache.flink.table.operations.ddl.CreateDatabaseOperation) CatalogPartitionSpec(org.apache.flink.table.catalog.CatalogPartitionSpec) SqlShowColumns(org.apache.flink.sql.parser.dql.SqlShowColumns) SqlCreateView(org.apache.flink.sql.parser.ddl.SqlCreateView) SqlStatementSet(org.apache.flink.sql.parser.dml.SqlStatementSet) SqlShowCurrentDatabase(org.apache.flink.sql.parser.dql.SqlShowCurrentDatabase) AlterPartitionPropertiesOperation(org.apache.flink.table.operations.ddl.AlterPartitionPropertiesOperation) SqlParser(org.apache.calcite.sql.parser.SqlParser) SqlShowFunctions(org.apache.flink.sql.parser.dql.SqlShowFunctions) SqlAlterTableOptions(org.apache.flink.sql.parser.ddl.SqlAlterTableOptions) TableSchemaUtils(org.apache.flink.table.utils.TableSchemaUtils) ShowPartitionsOperation(org.apache.flink.table.operations.ShowPartitionsOperation) Schema(org.apache.flink.table.api.Schema) SqlUseCatalog(org.apache.flink.sql.parser.ddl.SqlUseCatalog) SqlExecutePlan(org.apache.flink.sql.parser.dml.SqlExecutePlan) SqlShowDatabases(org.apache.flink.sql.parser.dql.SqlShowDatabases) SqlDropView(org.apache.flink.sql.parser.ddl.SqlDropView) AlterViewPropertiesOperation(org.apache.flink.table.operations.ddl.AlterViewPropertiesOperation) SqlNode(org.apache.calcite.sql.SqlNode) SqlUtil(org.apache.calcite.sql.SqlUtil) SetOperation(org.apache.flink.table.operations.command.SetOperation) RelHint(org.apache.calcite.rel.hint.RelHint) SqlAlterViewAs(org.apache.flink.sql.parser.ddl.SqlAlterViewAs) SqlAlterTableRename(org.apache.flink.sql.parser.ddl.SqlAlterTableRename) ManagedTableListener(org.apache.flink.table.catalog.ManagedTableListener) SqlDropCatalog(org.apache.flink.sql.parser.ddl.SqlDropCatalog) SqlDropFunction(org.apache.flink.sql.parser.ddl.SqlDropFunction) SqlShowCatalogs(org.apache.flink.sql.parser.dql.SqlShowCatalogs) LoadModuleOperation(org.apache.flink.table.operations.LoadModuleOperation) Operation(org.apache.flink.table.operations.Operation) AlterCatalogFunctionOperation(org.apache.flink.table.operations.ddl.AlterCatalogFunctionOperation) SqlAlterTableDropConstraint(org.apache.flink.sql.parser.ddl.SqlAlterTableDropConstraint) SqlCompilePlan(org.apache.flink.sql.parser.ddl.SqlCompilePlan) DropTempSystemFunctionOperation(org.apache.flink.table.operations.ddl.DropTempSystemFunctionOperation) ShowViewsOperation(org.apache.flink.table.operations.ShowViewsOperation) ShowDatabasesOperation(org.apache.flink.table.operations.ShowDatabasesOperation) SqlAlterTableCompact(org.apache.flink.sql.parser.ddl.SqlAlterTableCompact) FunctionLanguage(org.apache.flink.table.catalog.FunctionLanguage) StringUtils(org.apache.flink.util.StringUtils) Collectors(java.util.stream.Collectors) List(java.util.List) SqlShowModules(org.apache.flink.sql.parser.dql.SqlShowModules) ShowModulesOperation(org.apache.flink.table.operations.ShowModulesOperation) SqlRichExplain(org.apache.flink.sql.parser.dql.SqlRichExplain) SourceQueryOperation(org.apache.flink.table.operations.SourceQueryOperation) UnloadModuleOperation(org.apache.flink.table.operations.UnloadModuleOperation) ValidationException(org.apache.flink.table.api.ValidationException) DropTableOperation(org.apache.flink.table.operations.ddl.DropTableOperation) Optional(java.util.Optional) CatalogViewImpl(org.apache.flink.table.catalog.CatalogViewImpl) AlterViewAsOperation(org.apache.flink.table.operations.ddl.AlterViewAsOperation) RemoveJarOperation(org.apache.flink.table.operations.command.RemoveJarOperation) DropViewOperation(org.apache.flink.table.operations.ddl.DropViewOperation) SqlSet(org.apache.flink.sql.parser.ddl.SqlSet) CatalogManager(org.apache.flink.table.catalog.CatalogManager) SqlAlterViewProperties(org.apache.flink.sql.parser.ddl.SqlAlterViewProperties) BeginStatementSetOperation(org.apache.flink.table.operations.BeginStatementSetOperation) AddPartitionsOperation(org.apache.flink.table.operations.ddl.AddPartitionsOperation) RelRoot(org.apache.calcite.rel.RelRoot) HashMap(java.util.HashMap) AddJarOperation(org.apache.flink.table.operations.command.AddJarOperation) DropPartitionsOperation(org.apache.flink.table.operations.ddl.DropPartitionsOperation) AlterTableAddConstraintOperation(org.apache.flink.table.operations.ddl.AlterTableAddConstraintOperation) HashSet(java.util.HashSet) ExplainOperation(org.apache.flink.table.operations.ExplainOperation) ResetOperation(org.apache.flink.table.operations.command.ResetOperation) HintStrategyTable(org.apache.calcite.rel.hint.HintStrategyTable) SqlShowTables(org.apache.flink.sql.parser.dql.SqlShowTables) CatalogPartition(org.apache.flink.table.catalog.CatalogPartition) StatementSetOperation(org.apache.flink.table.operations.StatementSetOperation) SqlBeginStatementSet(org.apache.flink.sql.parser.dml.SqlBeginStatementSet) ResolvedCatalogTable(org.apache.flink.table.catalog.ResolvedCatalogTable) DropCatalogOperation(org.apache.flink.table.operations.ddl.DropCatalogOperation) ContextResolvedTable(org.apache.flink.table.catalog.ContextResolvedTable) SqlReset(org.apache.flink.sql.parser.ddl.SqlReset) SqlShowJars(org.apache.flink.sql.parser.dql.SqlShowJars) AlterTableRenameOperation(org.apache.flink.table.operations.ddl.AlterTableRenameOperation) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) SqlDialect(org.apache.calcite.sql.SqlDialect) SqlCreateCatalog(org.apache.flink.sql.parser.ddl.SqlCreateCatalog) SqlExecute(org.apache.flink.sql.parser.dml.SqlExecute) ShowCreateTableOperation(org.apache.flink.table.operations.ShowCreateTableOperation) AlterViewRenameOperation(org.apache.flink.table.operations.ddl.AlterViewRenameOperation) CreateCatalogFunctionOperation(org.apache.flink.table.operations.ddl.CreateCatalogFunctionOperation) CreateTempSystemFunctionOperation(org.apache.flink.table.operations.ddl.CreateTempSystemFunctionOperation) SqlNodeList(org.apache.calcite.sql.SqlNodeList) Collections(java.util.Collections) ShowCreateViewOperation(org.apache.flink.table.operations.ShowCreateViewOperation) CreateViewOperation(org.apache.flink.table.operations.ddl.CreateViewOperation) SqlNodeList(org.apache.calcite.sql.SqlNodeList) UnresolvedIdentifier(org.apache.flink.table.catalog.UnresolvedIdentifier) CatalogView(org.apache.flink.table.catalog.CatalogView) SqlNode(org.apache.calcite.sql.SqlNode) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier)

Example 50 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList in project flink by apache.

the class ParserImpl method parse.

/**
 * When parsing statement, it first uses {@link ExtendedParser} to parse statements. If {@link
 * ExtendedParser} fails to parse statement, it uses the {@link CalciteParser} to parse
 * statements.
 *
 * @param statement input statement.
 * @return parsed operations.
 */
@Override
public List<Operation> parse(String statement) {
    CalciteParser parser = calciteParserSupplier.get();
    FlinkPlannerImpl planner = validatorSupplier.get();
    Optional<Operation> command = EXTENDED_PARSER.parse(statement);
    if (command.isPresent()) {
        return Collections.singletonList(command.get());
    }
    // parse the sql query
    // use parseSqlList here because we need to support statement end with ';' in sql client.
    SqlNodeList sqlNodeList = parser.parseSqlList(statement);
    List<SqlNode> parsed = sqlNodeList.getList();
    Preconditions.checkArgument(parsed.size() == 1, "only single statement supported");
    return Collections.singletonList(SqlToOperationConverter.convert(planner, catalogManager, parsed.get(0)).orElseThrow(() -> new TableException("Unsupported query: " + statement)));
}
Also used : TableException(org.apache.flink.table.api.TableException) FlinkPlannerImpl(org.apache.flink.table.planner.calcite.FlinkPlannerImpl) SqlNodeList(org.apache.calcite.sql.SqlNodeList) Operation(org.apache.flink.table.operations.Operation) CalciteParser(org.apache.flink.table.planner.parse.CalciteParser) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlNodeList (org.apache.calcite.sql.SqlNodeList)123 SqlNode (org.apache.calcite.sql.SqlNode)97 ArrayList (java.util.ArrayList)45 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)43 RelDataType (org.apache.calcite.rel.type.RelDataType)39 SqlCall (org.apache.calcite.sql.SqlCall)30 SqlSelect (org.apache.calcite.sql.SqlSelect)29 BitString (org.apache.calcite.util.BitString)23 RexNode (org.apache.calcite.rex.RexNode)13 SqlLiteral (org.apache.calcite.sql.SqlLiteral)11 ImmutableList (com.google.common.collect.ImmutableList)10 List (java.util.List)10 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)10 NlsString (org.apache.calcite.util.NlsString)9 RelNode (org.apache.calcite.rel.RelNode)8 SqlUpdate (org.apache.calcite.sql.SqlUpdate)8 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)7 SqlWriter (org.apache.calcite.sql.SqlWriter)7 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)7 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)6