Search in sources :

Example 66 with HazelcastTable

use of com.hazelcast.jet.sql.impl.schema.HazelcastTable 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 67 with HazelcastTable

use of com.hazelcast.jet.sql.impl.schema.HazelcastTable in project hazelcast by hazelcast.

the class IndexScanMapPhysicalRel method projection.

public List<Expression<?>> projection(QueryParameterMetadata parameterMetadata) {
    PlanNodeSchema schema = OptUtils.schema(getTable());
    HazelcastTable table = getTable().unwrap(HazelcastTable.class);
    return project(schema, table.getProjects(), parameterMetadata);
}
Also used : PlanNodeSchema(com.hazelcast.sql.impl.plan.node.PlanNodeSchema) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable)

Example 68 with HazelcastTable

use of com.hazelcast.jet.sql.impl.schema.HazelcastTable in project hazelcast by hazelcast.

the class OptUtils method extractKeyConstantExpression.

@SuppressWarnings("checkstyle:AvoidNestedBlocks")
public static RexNode extractKeyConstantExpression(RelOptTable relTable, RexBuilder rexBuilder) {
    HazelcastTable table = relTable.unwrap(HazelcastTable.class);
    RexNode filter = table.getFilter();
    if (filter == null) {
        return null;
    }
    int keyIndex = findKeyIndex(table.getTarget());
    switch(filter.getKind()) {
        // WHERE __key = true, calcite simplifies to just `WHERE __key`
        case INPUT_REF:
            {
                return ((RexInputRef) filter).getIndex() == keyIndex ? rexBuilder.makeLiteral(true) : null;
            }
        // WHERE __key = false, calcite simplifies to `WHERE NOT __key`
        case NOT:
            {
                RexNode operand = ((RexCall) filter).getOperands().get(0);
                return operand.getKind() == SqlKind.INPUT_REF && ((RexInputRef) operand).getIndex() == keyIndex ? rexBuilder.makeLiteral(false) : null;
            }
        // __key = ...
        case EQUALS:
            {
                Tuple2<Integer, RexNode> constantExpressionByIndex = extractConstantExpression((RexCall) filter);
                // noinspection ConstantConditions
                return constantExpressionByIndex != null && constantExpressionByIndex.getKey() == keyIndex ? constantExpressionByIndex.getValue() : null;
            }
        default:
            return null;
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) Tuple2(com.hazelcast.jet.datamodel.Tuple2) RexInputRef(org.apache.calcite.rex.RexInputRef) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) RexNode(org.apache.calcite.rex.RexNode)

Example 69 with HazelcastTable

use of com.hazelcast.jet.sql.impl.schema.HazelcastTable 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 70 with HazelcastTable

use of com.hazelcast.jet.sql.impl.schema.HazelcastTable 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

HazelcastTable (com.hazelcast.jet.sql.impl.schema.HazelcastTable)70 Test (org.junit.Test)48 TableField (com.hazelcast.sql.impl.schema.TableField)8 MapTableField (com.hazelcast.sql.impl.schema.map.MapTableField)8 QueryDataType (com.hazelcast.sql.impl.type.QueryDataType)8 IndexScanMapPhysicalRel (com.hazelcast.jet.sql.impl.opt.physical.IndexScanMapPhysicalRel)7 RexNode (org.apache.calcite.rex.RexNode)7 QueryPath (com.hazelcast.sql.impl.extract.QueryPath)6 Parameters (junitparams.Parameters)6 OptimizerTestSupport (com.hazelcast.jet.sql.impl.opt.OptimizerTestSupport)5 HazelcastRelOptTable (com.hazelcast.jet.sql.impl.schema.HazelcastRelOptTable)5 ArrayList (java.util.ArrayList)5 FullScanPhysicalRel (com.hazelcast.jet.sql.impl.opt.physical.FullScanPhysicalRel)4 RelOptTable (org.apache.calcite.plan.RelOptTable)4 RelTraitSet (org.apache.calcite.plan.RelTraitSet)3 RelCollation (org.apache.calcite.rel.RelCollation)3 RelNode (org.apache.calcite.rel.RelNode)3 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)3 SqlValidatorTable (org.apache.calcite.sql.validate.SqlValidatorTable)3 RexBuilder (org.apache.calcite.rex.RexBuilder)2