Search in sources :

Example 91 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project calcite by apache.

the class SqlTypeUtil method convertTypeToSpec.

/**
 * Converts an instance of RelDataType to an instance of SqlDataTypeSpec.
 *
 * @param type type descriptor
 * @return corresponding parse representation
 */
public static SqlDataTypeSpec convertTypeToSpec(RelDataType type) {
    SqlTypeName typeName = type.getSqlTypeName();
    // interval types, multiset types, etc
    assert typeName != null;
    SqlIdentifier typeIdentifier = new SqlIdentifier(typeName.name(), SqlParserPos.ZERO);
    String charSetName = null;
    if (inCharFamily(type)) {
        charSetName = type.getCharset().name();
    // TODO jvs 28-Dec-2004:  collation
    }
    if (typeName.allowsScale()) {
        return new SqlDataTypeSpec(typeIdentifier, type.getPrecision(), type.getScale(), charSetName, null, SqlParserPos.ZERO);
    } else if (typeName.allowsPrec()) {
        return new SqlDataTypeSpec(typeIdentifier, type.getPrecision(), -1, charSetName, null, SqlParserPos.ZERO);
    } else {
        return new SqlDataTypeSpec(typeIdentifier, -1, -1, charSetName, null, SqlParserPos.ZERO);
    }
}
Also used : SqlDataTypeSpec(org.apache.calcite.sql.SqlDataTypeSpec) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 92 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project calcite by apache.

the class AggChecker method visit.

public Void visit(SqlCall call) {
    final SqlValidatorScope scope = scopes.peek();
    if (call.getOperator().isAggregator()) {
        if (distinct) {
            if (scope instanceof AggregatingSelectScope) {
                SqlNodeList selectList = ((SqlSelect) scope.getNode()).getSelectList();
                // Check if this aggregation function is just an element in the select
                for (SqlNode sqlNode : selectList) {
                    if (sqlNode.getKind() == SqlKind.AS) {
                        sqlNode = ((SqlCall) sqlNode).operand(0);
                    }
                    if (validator.expand(sqlNode, scope).equalsDeep(call, Litmus.IGNORE)) {
                        return null;
                    }
                }
            }
            // Cannot use agg fun in ORDER BY clause if have SELECT DISTINCT.
            SqlNode originalExpr = validator.getOriginal(call);
            final String exprString = originalExpr.toString();
            throw validator.newValidationError(call, RESOURCE.notSelectDistinctExpr(exprString));
        }
        // BY deptno'
        return null;
    }
    if (call.getKind() == SqlKind.FILTER) {
        call.operand(0).accept(this);
        return null;
    }
    // Visit the operand in window function
    if (call.getKind() == SqlKind.OVER) {
        for (SqlNode operand : call.<SqlCall>operand(0).getOperandList()) {
            operand.accept(this);
        }
        // Check the OVER clause
        final SqlNode over = call.operand(1);
        if (over instanceof SqlCall) {
            over.accept(this);
        } else if (over instanceof SqlIdentifier) {
            // Check the corresponding SqlWindow in WINDOW clause
            final SqlWindow window = scope.lookupWindow(((SqlIdentifier) over).getSimple());
            window.getPartitionList().accept(this);
            window.getOrderList().accept(this);
        }
    }
    if (isGroupExpr(call)) {
        // This call matches an expression in the GROUP BY clause.
        return null;
    }
    final SqlCall groupCall = SqlStdOperatorTable.convertAuxiliaryToGroupCall(call);
    if (groupCall != null) {
        if (isGroupExpr(groupCall)) {
            // TUMBLE(rowtime, INTERVAL '1' HOUR')
            return null;
        }
        throw validator.newValidationError(groupCall, RESOURCE.auxiliaryWithoutMatchingGroupCall(call.getOperator().getName(), groupCall.getOperator().getName()));
    }
    if (call.isA(SqlKind.QUERY)) {
        // references to forbidden columns.
        return null;
    }
    // Switch to new scope.
    SqlValidatorScope newScope = scope.getOperandScope(call);
    scopes.push(newScope);
    // Visit the operands (only expressions).
    call.getOperator().acceptCall(this, call, true, ArgHandlerImpl.<Void>instance());
    // Restore scope.
    scopes.pop();
    return null;
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlCall(org.apache.calcite.sql.SqlCall) SqlWindow(org.apache.calcite.sql.SqlWindow) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 93 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project calcite by apache.

the class SqlCreateForeignSchema method execute.

public void execute(CalcitePrepare.Context context) {
    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
    final SchemaPlus subSchema0 = pair.left.plus().getSubSchema(pair.right);
    if (subSchema0 != null) {
        if (!getReplace() && !ifNotExists) {
            throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.schemaExists(pair.right));
        }
    }
    final Schema subSchema;
    final String libraryName;
    if (type != null) {
        Preconditions.checkArgument(library == null);
        final String typeName = (String) value(this.type);
        final JsonSchema.Type type = Util.enumVal(JsonSchema.Type.class, typeName.toUpperCase(Locale.ROOT));
        if (type != null) {
            switch(type) {
                case JDBC:
                    libraryName = JdbcSchema.Factory.class.getName();
                    break;
                default:
                    libraryName = null;
            }
        } else {
            libraryName = null;
        }
        if (libraryName == null) {
            throw SqlUtil.newContextException(this.type.getParserPosition(), RESOURCE.schemaInvalidType(typeName, Arrays.toString(JsonSchema.Type.values())));
        }
    } else {
        Preconditions.checkArgument(library != null);
        libraryName = (String) value(library);
    }
    final SchemaFactory schemaFactory = AvaticaUtils.instantiatePlugin(SchemaFactory.class, libraryName);
    final Map<String, Object> operandMap = new LinkedHashMap<>();
    for (Pair<SqlIdentifier, SqlNode> option : options(optionList)) {
        operandMap.put(option.left.getSimple(), value(option.right));
    }
    subSchema = schemaFactory.create(pair.left.plus(), pair.right, operandMap);
    pair.left.add(pair.right, subSchema);
}
Also used : SchemaFactory(org.apache.calcite.schema.SchemaFactory) JdbcSchema(org.apache.calcite.adapter.jdbc.JdbcSchema) Schema(org.apache.calcite.schema.Schema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) JsonSchema(org.apache.calcite.model.JsonSchema) JsonSchema(org.apache.calcite.model.JsonSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SchemaFactory(org.apache.calcite.schema.SchemaFactory) NlsString(org.apache.calcite.util.NlsString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) LinkedHashMap(java.util.LinkedHashMap) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlNode(org.apache.calcite.sql.SqlNode)

Example 94 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project calcite by apache.

the class SqlAdvisorValidator method registerId.

private void registerId(SqlIdentifier id, SqlValidatorScope scope) {
    for (int i = 0; i < id.names.size(); i++) {
        final SqlParserPos subPos = id.getComponentParserPosition(i);
        SqlIdentifier subId = i == id.names.size() - 1 ? id : new SqlIdentifier(id.names.subList(0, i + 1), subPos);
        idPositions.put(subPos.toString(), new IdInfo(scope, subId));
    }
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 95 with SqlIdentifier

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier in project flink by apache.

the class RichSqlHiveInsert method unparse.

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
    writer.startList(SqlWriter.FrameTypeEnum.SELECT);
    String insertKeyword = "INSERT INTO";
    if (isUpsert()) {
        insertKeyword = "UPSERT INTO";
    } else if (isOverwrite()) {
        insertKeyword = "INSERT OVERWRITE";
    }
    writer.sep(insertKeyword);
    final int opLeft = getOperator().getLeftPrec();
    final int opRight = getOperator().getRightPrec();
    getTargetTable().unparse(writer, opLeft, opRight);
    if (getTargetColumnList() != null) {
        getTargetColumnList().unparse(writer, opLeft, opRight);
    }
    writer.newlineAndIndent();
    if (allPartKeys != null && allPartKeys.size() > 0) {
        writer.keyword("PARTITION");
        SqlWriter.Frame frame = writer.startList("(", ")");
        for (SqlNode node : allPartKeys) {
            writer.sep(",", false);
            SqlIdentifier partKey = (SqlIdentifier) node;
            SqlProperty spec = partKeyToSpec.get(partKey);
            if (spec != null) {
                spec.unparse(writer, leftPrec, rightPrec);
            } else {
                partKey.unparse(writer, leftPrec, rightPrec);
            }
        }
        writer.endList(frame);
        writer.newlineAndIndent();
    }
    getSource().unparse(writer, 0, 0);
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) SqlProperty(org.apache.flink.sql.parser.SqlProperty) 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