Search in sources :

Example 41 with SqlIdentifier

use of org.apache.calcite.sql.SqlIdentifier in project drill by apache.

the class ShowFilesHandler method getPlan.

@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ForemanSetupException {
    SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
    SchemaPlus drillSchema = defaultSchema;
    SqlShowFiles showFiles = unwrap(sqlNode, SqlShowFiles.class);
    SqlIdentifier from = showFiles.getDb();
    String fromDir = null;
    // Show files can be used without from clause, in which case we display the files in the default schema
    if (from != null) {
        // We are not sure if the full from clause is just the schema or includes table name,
        // first try to see if the full path specified is a schema
        drillSchema = SchemaUtilites.findSchema(defaultSchema, from.names);
        if (drillSchema == null) {
            // Entire from clause is not a schema, try to obtain the schema without the last part of the specified clause.
            drillSchema = SchemaUtilites.findSchema(defaultSchema, from.names.subList(0, from.names.size() - 1));
            // Listing for specific directory: show files in dfs.tmp.specific_directory
            fromDir = from.names.get((from.names.size() - 1));
        }
        if (drillSchema == null) {
            throw UserException.validationError().message("Invalid FROM/IN clause [%s]", from.toString()).build(logger);
        }
    }
    WorkspaceSchema wsSchema;
    try {
        wsSchema = (WorkspaceSchema) drillSchema.unwrap(AbstractSchema.class).getDefaultSchema();
    } catch (ClassCastException e) {
        throw UserException.validationError().message("SHOW FILES is supported in workspace type schema only. Schema [%s] is not a workspace schema.", SchemaUtilites.getSchemaPath(drillSchema)).build(logger);
    }
    Path endPath = fromDir == null ? new Path(wsSchema.getDefaultLocation()) : new Path(wsSchema.getDefaultLocation(), fromDir);
    // add URI to the path to ensure that directory objects are skipped (see S3AFileSystem.listStatus method)
    Path path = new Path(wsSchema.getFS().getUri().toString(), endPath);
    List<ShowFilesCommandResult> records = FileSystemUtil.listAllSafe(wsSchema.getFS(), path, false).stream().map(fileStatus -> new ShowFilesCommandResult(new Records.File(wsSchema.getFullSchemaName(), wsSchema, fileStatus))).collect(Collectors.toList());
    return DirectPlan.createDirectPlan(context.getCurrentEndpoint(), records, ShowFilesCommandResult.class);
}
Also used : Path(org.apache.hadoop.fs.Path) SchemaUtilites(org.apache.drill.exec.planner.sql.SchemaUtilites) SqlShowFiles(org.apache.drill.exec.planner.sql.parser.SqlShowFiles) SchemaPlus(org.apache.calcite.schema.SchemaPlus) Records(org.apache.drill.exec.store.ischema.Records) Logger(org.slf4j.Logger) UserException(org.apache.drill.common.exceptions.UserException) Timestamp(java.sql.Timestamp) WorkspaceSchema(org.apache.drill.exec.store.dfs.WorkspaceSchemaFactory.WorkspaceSchema) Collectors(java.util.stream.Collectors) FileSystemUtil(org.apache.drill.exec.util.FileSystemUtil) List(java.util.List) SqlNode(org.apache.calcite.sql.SqlNode) ForemanSetupException(org.apache.drill.exec.work.foreman.ForemanSetupException) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) DirectPlan(org.apache.drill.exec.planner.sql.DirectPlan) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) PhysicalPlan(org.apache.drill.exec.physical.PhysicalPlan) Path(org.apache.hadoop.fs.Path) WorkspaceSchema(org.apache.drill.exec.store.dfs.WorkspaceSchemaFactory.WorkspaceSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlShowFiles(org.apache.drill.exec.planner.sql.parser.SqlShowFiles) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) Records(org.apache.drill.exec.store.ischema.Records)

Example 42 with SqlIdentifier

use of org.apache.calcite.sql.SqlIdentifier in project drill by apache.

the class AnalyzeTableHandler method getColumnList.

/* Generates the column list specified in the ANALYZE statement */
private SqlNodeList getColumnList(final SqlAnalyzeTable sqlAnalyzeTable) {
    SqlNodeList columnList = sqlAnalyzeTable.getFieldList();
    if (columnList == null || columnList.size() <= 0) {
        columnList = new SqlNodeList(SqlParserPos.ZERO);
        columnList.add(new SqlIdentifier(SchemaPath.STAR_COLUMN.rootName(), SqlParserPos.ZERO));
    }
    /*final SqlNodeList columnList = new SqlNodeList(SqlParserPos.ZERO);
    final List<String> fields = sqlAnalyzeTable.getFieldNames();
    if (fields == null || fields.size() <= 0) {
      columnList.add(new SqlIdentifier(SchemaPath.STAR_COLUMN.rootName(), SqlParserPos.ZERO));
    } else {
      for(String field : fields) {
        columnList.add(new SqlIdentifier(field, SqlParserPos.ZERO));
      }
    }*/
    return columnList;
}
Also used : SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 43 with SqlIdentifier

use of org.apache.calcite.sql.SqlIdentifier in project drill by apache.

the class DrillCompoundIdentifier method getAsSqlNode.

public SqlNode getAsSqlNode(boolean allowNoTableRefCompoundIdentifier) {
    if (ids.size() == 1) {
        return new SqlIdentifier(Collections.singletonList(ids.get(0).value), ids.get(0).parserPos);
    }
    int startIndex;
    SqlNode node;
    if (ids.get(1).isArray()) {
        // handle everything post zero index as item operator.
        startIndex = 1;
        node = new SqlIdentifier(ImmutableList.of(ids.get(0).value), null, ids.get(0).parserPos, ImmutableList.of(ids.get(0).parserPos));
    } else {
        if (allowNoTableRefCompoundIdentifier) {
            // For certain statements e.g. ANALYZE which only reference one table, compound column names may be referenced
            // without the table reference. For such cases, handle everything post one index as item operator.
            startIndex = 1;
            node = new SqlIdentifier(// Replaces star by empty string. See SqlIdentifier#isStar()
            ImmutableList.of(STAR_TO_EMPTY.apply(ids.get(0).value)), null, ids.get(0).parserPos, ImmutableList.of(ids.get(0).parserPos));
        } else {
            // handle everything post two index as item operator.
            startIndex = 2;
            node = new SqlIdentifier(// Replaces star by empty string. See SqlIdentifier#isStar()
            ImmutableList.of(ids.get(0).value, STAR_TO_EMPTY.apply(ids.get(1).value)), null, ids.get(0).parserPos, ImmutableList.of(ids.get(0).parserPos, ids.get(1).parserPos));
        }
    }
    for (int i = startIndex; i < ids.size(); i++) {
        node = ids.get(i).getNode(node);
    }
    return node;
}
Also used : SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 44 with SqlIdentifier

use of org.apache.calcite.sql.SqlIdentifier in project drill by apache.

the class DrillSqlResetOption method getOperandList.

@Override
public List<SqlNode> getOperandList() {
    List<SqlNode> operandList = new ArrayList<>();
    SqlIdentifier scopeIdentifier = (this.getScope() == null) ? null : new SqlIdentifier(this.getScope(), SqlParserPos.ZERO);
    operandList.add(scopeIdentifier);
    operandList.add(this.getName());
    return ImmutableNullableList.copyOf(operandList);
}
Also used : ArrayList(java.util.ArrayList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 45 with SqlIdentifier

use of org.apache.calcite.sql.SqlIdentifier in project drill by apache.

the class UnsupportedOperatorsVisitor method detectMultiplePartitions.

/**
 * Disable multiple partitions in a SELECT-CLAUSE
 * If multiple partitions are defined in the query,
 * SqlUnsupportedException would be thrown to inform
 * @param sqlSelect SELECT-CLAUSE in the query
 */
private void detectMultiplePartitions(SqlSelect sqlSelect) {
    for (SqlNode nodeInSelectList : sqlSelect.getSelectList()) {
        // enter the first operand of AS operator
        if (nodeInSelectList.getKind() == SqlKind.AS && (((SqlCall) nodeInSelectList).getOperandList().get(0).getKind() == SqlKind.OVER)) {
            nodeInSelectList = ((SqlCall) nodeInSelectList).getOperandList().get(0);
        }
        if (nodeInSelectList.getKind() != SqlKind.OVER) {
            continue;
        }
        // This is used to keep track of the window function which has been defined
        SqlNode definedWindow = null;
        SqlNode window = ((SqlCall) nodeInSelectList).operand(1);
        // which is defined in the window list
        if (window instanceof SqlIdentifier) {
            // Expand the SqlIdentifier as the expression defined in the window list
            for (SqlNode sqlNode : sqlSelect.getWindowList()) {
                if (((SqlWindow) sqlNode).getDeclName().equalsDeep(window, Litmus.IGNORE)) {
                    window = sqlNode;
                    break;
                }
            }
            assert !(window instanceof SqlIdentifier) : "Identifier should have been expanded as a window defined in the window list";
        }
        // In a SELECT-SCOPE, only a partition can be defined
        if (definedWindow == null) {
            definedWindow = window;
        } else {
            if (!definedWindow.equalsDeep(window, Litmus.IGNORE)) {
                unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.FUNCTION, "Multiple window definitions in a single SELECT list is not currently supported \n" + "See Apache Drill JIRA: DRILL-3196");
                throw new UnsupportedOperationException();
            }
        }
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)131 SqlNode (org.apache.calcite.sql.SqlNode)84 RelDataType (org.apache.calcite.rel.type.RelDataType)46 SqlNodeList (org.apache.calcite.sql.SqlNodeList)43 ArrayList (java.util.ArrayList)41 SqlCall (org.apache.calcite.sql.SqlCall)32 BitString (org.apache.calcite.util.BitString)28 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)21 SqlSelect (org.apache.calcite.sql.SqlSelect)21 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)12 SchemaPlus (org.apache.calcite.schema.SchemaPlus)11 SqlOperator (org.apache.calcite.sql.SqlOperator)11 NlsString (org.apache.calcite.util.NlsString)11 List (java.util.List)9 Map (java.util.Map)9 RelOptTable (org.apache.calcite.plan.RelOptTable)9 RexNode (org.apache.calcite.rex.RexNode)9 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)9 ImmutableList (com.google.common.collect.ImmutableList)8 RelNode (org.apache.calcite.rel.RelNode)7