Search in sources :

Example 1 with SqlValidatorTable

use of org.apache.calcite.sql.validate.SqlValidatorTable in project calcite by apache.

the class SqlToRelConverter method convertIdentifier.

private void convertIdentifier(Blackboard bb, SqlIdentifier id, SqlNodeList extendedColumns) {
    final SqlValidatorNamespace fromNamespace = validator.getNamespace(id).resolve();
    if (fromNamespace.getNode() != null) {
        convertFrom(bb, fromNamespace.getNode());
        return;
    }
    final String datasetName = datasetStack.isEmpty() ? null : datasetStack.peek();
    final boolean[] usedDataset = { false };
    RelOptTable table = SqlValidatorUtil.getRelOptTable(fromNamespace, catalogReader, datasetName, usedDataset);
    if (extendedColumns != null && extendedColumns.size() > 0) {
        assert table != null;
        final SqlValidatorTable validatorTable = table.unwrap(SqlValidatorTable.class);
        final List<RelDataTypeField> extendedFields = SqlValidatorUtil.getExtendedColumns(validator.getTypeFactory(), validatorTable, extendedColumns);
        table = table.extend(extendedFields);
    }
    final RelNode tableRel;
    if (config.isConvertTableAccess()) {
        tableRel = toRel(table);
    } else {
        tableRel = LogicalTableScan.create(cluster, table);
    }
    bb.setRoot(tableRel, true);
    if (usedDataset[0]) {
        bb.setDataset(datasetName);
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) NlsString(org.apache.calcite.util.NlsString) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlValidatorNamespace(org.apache.calcite.sql.validate.SqlValidatorNamespace)

Example 2 with SqlValidatorTable

use of org.apache.calcite.sql.validate.SqlValidatorTable in project druid by druid-io.

the class SqlResourceCollectorShuttle method visit.

@Override
public SqlNode visit(SqlIdentifier id) {
    // raw tables and views and such will have a IdentifierNamespace
    // since we are scoped to identifiers here, we should only pick up these
    SqlValidatorNamespace namespace = validator.getNamespace(id);
    if (namespace != null && namespace.isWrapperFor(IdentifierNamespace.class)) {
        SqlValidatorTable validatorTable = namespace.getTable();
        // this should not probably be null if the namespace was not null,
        if (validatorTable != null) {
            List<String> qualifiedNameParts = validatorTable.getQualifiedName();
            // 'schema'.'identifier'
            if (qualifiedNameParts.size() == 2) {
                final String schema = qualifiedNameParts.get(0);
                final String resourceName = qualifiedNameParts.get(1);
                final String resourceType = plannerContext.getSchemaResourceType(schema, resourceName);
                if (resourceType != null) {
                    resourceActions.add(new ResourceAction(new Resource(resourceName, resourceType), Action.READ));
                }
            } else if (qualifiedNameParts.size() > 2) {
                // Don't expect to see more than 2 names (catalog?).
                throw new ISE("Cannot analyze table idetifier %s", qualifiedNameParts);
            }
        }
    }
    return super.visit(id);
}
Also used : SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) Resource(org.apache.druid.server.security.Resource) ISE(org.apache.druid.java.util.common.ISE) SqlValidatorNamespace(org.apache.calcite.sql.validate.SqlValidatorNamespace) IdentifierNamespace(org.apache.calcite.sql.validate.IdentifierNamespace) ResourceAction(org.apache.druid.server.security.ResourceAction)

Example 3 with SqlValidatorTable

use of org.apache.calcite.sql.validate.SqlValidatorTable in project hazelcast by hazelcast.

the class SqlExtendedInsert method validate.

@Override
public void validate(SqlValidator validator, SqlValidatorScope scope) {
    SqlValidatorTable table0 = validator.getCatalogReader().getTable(tableNames());
    if (table0 == null) {
        super.validate(validator, scope);
        // should have failed with "Object not found"
        assert false;
    }
    HazelcastTable table = table0.unwrap(HazelcastTable.class);
    if (getTargetColumnList() == null) {
        RelDataType rowType = table.getRowType(validator.getTypeFactory());
        List<SqlNode> columnListWithoutHidden = new ArrayList<>();
        for (RelDataTypeField f : rowType.getFieldList()) {
            if (!table.isHidden(f.getName())) {
                columnListWithoutHidden.add(new SqlIdentifier(f.getName(), SqlParserPos.ZERO));
            }
        }
        overrideColumnList = new SqlNodeList(columnListWithoutHidden, SqlParserPos.ZERO);
    }
    super.validate(validator, scope);
    Map<String, TableField> fieldsMap = table.getTarget().getFields().stream().collect(Collectors.toMap(TableField::getName, f -> f));
    for (SqlNode fieldNode : getTargetColumnList()) {
        TableField field = fieldsMap.get(((SqlIdentifier) fieldNode).getSimple());
        if (field instanceof MapTableField) {
            QueryPath path = ((MapTableField) field).getPath();
            if (path.getPath() == null && field.getType().getTypeFamily() == QueryDataTypeFamily.OBJECT) {
                throw validator.newValidationError(fieldNode, RESOURCE.insertToTopLevelObject());
            }
        }
    }
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) SqlInsert(org.apache.calcite.sql.SqlInsert) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlWriter(org.apache.calcite.sql.SqlWriter) SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) RESOURCE(com.hazelcast.jet.sql.impl.parse.ParserResource.RESOURCE) Collectors(java.util.stream.Collectors) QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) ArrayList(java.util.ArrayList) TableField(com.hazelcast.sql.impl.schema.TableField) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) List(java.util.List) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode) ImmutableList(com.google.common.collect.ImmutableList) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Map(java.util.Map) QueryPath(com.hazelcast.sql.impl.extract.QueryPath) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) SqlNodeList(org.apache.calcite.sql.SqlNodeList) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) TableField(com.hazelcast.sql.impl.schema.TableField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) QueryPath(com.hazelcast.sql.impl.extract.QueryPath) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) SqlNodeList(org.apache.calcite.sql.SqlNodeList) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) SqlNode(org.apache.calcite.sql.SqlNode)

Example 4 with SqlValidatorTable

use of org.apache.calcite.sql.validate.SqlValidatorTable in project hazelcast by hazelcast.

the class HazelcastSqlValidator method containsStreamingSource.

/**
 * Goes over all the referenced tables in the given {@link SqlNode}
 * and returns true if any of them uses a streaming connector.
 */
public boolean containsStreamingSource(SqlNode node) {
    class FindStreamingTablesVisitor extends SqlBasicVisitor<Void> {

        boolean found;

        @Override
        public Void visit(SqlIdentifier id) {
            SqlValidatorTable table = getCatalogReader().getTable(id.names);
            // not every identifier is a table
            if (table != null) {
                HazelcastTable hazelcastTable = table.unwrap(HazelcastTable.class);
                if (hazelcastTable.getTarget() instanceof ViewTable) {
                    found = ((ViewTable) hazelcastTable.getTarget()).isStream();
                    return null;
                }
                SqlConnector connector = getJetSqlConnector(hazelcastTable.getTarget());
                if (connector.isStream()) {
                    found = true;
                    return null;
                }
            }
            return super.visit(id);
        }

        @Override
        public Void visit(SqlCall call) {
            SqlOperator operator = call.getOperator();
            if (operator instanceof HazelcastTableSourceFunction) {
                if (((HazelcastTableSourceFunction) operator).isStream()) {
                    found = true;
                    return null;
                }
            }
            return super.visit(call);
        }
    }
    FindStreamingTablesVisitor visitor = new FindStreamingTablesVisitor();
    node.accept(visitor);
    return visitor.found;
}
Also used : ViewTable(com.hazelcast.jet.sql.impl.connector.virtual.ViewTable) SqlCall(org.apache.calcite.sql.SqlCall) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) HazelcastTableSourceFunction(com.hazelcast.jet.sql.impl.schema.HazelcastTableSourceFunction) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlConnectorUtil.getJetSqlConnector(com.hazelcast.jet.sql.impl.connector.SqlConnectorUtil.getJetSqlConnector) SqlConnector(com.hazelcast.jet.sql.impl.connector.SqlConnector) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) SqlBasicVisitor(org.apache.calcite.sql.util.SqlBasicVisitor)

Example 5 with SqlValidatorTable

use of org.apache.calcite.sql.validate.SqlValidatorTable in project hazelcast by hazelcast.

the class HazelcastSqlValidator method isHiddenColumn.

private boolean isHiddenColumn(SqlNode node, SelectScope scope) {
    if (!(node instanceof SqlIdentifier)) {
        return false;
    }
    SqlIdentifier identifier = (SqlIdentifier) node;
    String fieldName = extractFieldName(identifier, scope);
    if (fieldName == null) {
        return false;
    }
    SqlValidatorTable table = scope.fullyQualify(identifier).namespace.getTable();
    if (table == null) {
        return false;
    }
    HazelcastTable unwrappedTable = table.unwrap(HazelcastTable.class);
    if (unwrappedTable == null) {
        return false;
    }
    return unwrappedTable.isHidden(fieldName);
}
Also used : SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable)

Aggregations

SqlValidatorTable (org.apache.calcite.sql.validate.SqlValidatorTable)5 HazelcastTable (com.hazelcast.jet.sql.impl.schema.HazelcastTable)3 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)3 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)2 SqlValidatorNamespace (org.apache.calcite.sql.validate.SqlValidatorNamespace)2 ImmutableList (com.google.common.collect.ImmutableList)1 SqlConnector (com.hazelcast.jet.sql.impl.connector.SqlConnector)1 SqlConnectorUtil.getJetSqlConnector (com.hazelcast.jet.sql.impl.connector.SqlConnectorUtil.getJetSqlConnector)1 ViewTable (com.hazelcast.jet.sql.impl.connector.virtual.ViewTable)1 RESOURCE (com.hazelcast.jet.sql.impl.parse.ParserResource.RESOURCE)1 HazelcastTableSourceFunction (com.hazelcast.jet.sql.impl.schema.HazelcastTableSourceFunction)1 QueryPath (com.hazelcast.sql.impl.extract.QueryPath)1 TableField (com.hazelcast.sql.impl.schema.TableField)1 MapTableField (com.hazelcast.sql.impl.schema.map.MapTableField)1 QueryDataTypeFamily (com.hazelcast.sql.impl.type.QueryDataTypeFamily)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 RelOptTable (org.apache.calcite.plan.RelOptTable)1