Search in sources :

Example 61 with SqlIdentifier

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

the class SqlValidatorImpl method getNamespace.

private SqlValidatorNamespace getNamespace(SqlNode node, SqlValidatorScope scope) {
    if (node instanceof SqlIdentifier && scope instanceof DelegatingScope) {
        final SqlIdentifier id = (SqlIdentifier) node;
        final DelegatingScope idScope = (DelegatingScope) ((DelegatingScope) scope).getParent();
        return getNamespace(id, idScope);
    } else if (node instanceof SqlCall) {
        // Handle extended identifiers.
        final SqlCall call = (SqlCall) node;
        switch(call.getOperator().getKind()) {
            case TABLE_REF:
                return getNamespace(call.operand(0), scope);
            case EXTEND:
                final SqlNode operand0 = call.getOperandList().get(0);
                final SqlIdentifier identifier = operand0.getKind() == SqlKind.TABLE_REF ? ((SqlCall) operand0).operand(0) : (SqlIdentifier) operand0;
                final DelegatingScope idScope = (DelegatingScope) scope;
                return getNamespace(identifier, idScope);
            case AS:
                final SqlNode nested = call.getOperandList().get(0);
                switch(nested.getKind()) {
                    case TABLE_REF:
                    case EXTEND:
                        return getNamespace(nested, scope);
                }
                break;
        }
    }
    return getNamespace(node);
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 62 with SqlIdentifier

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

the class ParserImpl method parseIdentifier.

@Override
public UnresolvedIdentifier parseIdentifier(String identifier) {
    CalciteParser parser = calciteParserSupplier.get();
    SqlIdentifier sqlIdentifier = parser.parseIdentifier(identifier);
    return UnresolvedIdentifier.of(sqlIdentifier.names);
}
Also used : SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) CalciteParser(org.apache.flink.table.planner.parse.CalciteParser)

Example 63 with SqlIdentifier

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

the class SqlOperators method createUdfOperator.

private static SqlUserDefinedFunction createUdfOperator(String name, Method method, final SqlSyntax syntax, String funGroup, String jarPath) {
    Function function = ZetaSqlScalarFunctionImpl.create(method, funGroup, jarPath);
    final RelDataTypeFactory typeFactory = createTypeFactory();
    List<RelDataType> argTypes = new ArrayList<>();
    List<SqlTypeFamily> typeFamilies = new ArrayList<>();
    for (FunctionParameter o : function.getParameters()) {
        final RelDataType type = o.getType(typeFactory);
        argTypes.add(type);
        typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
    }
    final FamilyOperandTypeChecker typeChecker = OperandTypes.family(typeFamilies, i -> function.getParameters().get(i).isOptional());
    final List<RelDataType> paramTypes = toSql(typeFactory, argTypes);
    return new SqlUserDefinedFunction(new SqlIdentifier(name, SqlParserPos.ZERO), infer((ScalarFunction) function), InferTypes.explicit(argTypes), typeChecker, paramTypes, function) {

        @Override
        public SqlSyntax getSyntax() {
            return syntax;
        }
    };
}
Also used : FamilyOperandTypeChecker(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.FamilyOperandTypeChecker) SqlUserDefinedFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.validate.SqlUserDefinedFunction) Function(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function) SqlFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlFunction) AggregateFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.AggregateFunction) ScalarFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.ScalarFunction) SqlUserDefinedAggFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) SqlUserDefinedFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.validate.SqlUserDefinedFunction) ScalarFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.ScalarFunction) SqlTypeFamily(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeFamily) RelDataTypeFactory(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory) ArrayList(java.util.ArrayList) RelDataType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier) FunctionParameter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.FunctionParameter)

Example 64 with SqlIdentifier

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

the class SqlSetOptionBeam method execute.

@Override
public void execute(CalcitePrepare.Context context) {
    final SqlIdentifier name = getName();
    final SqlNode value = getValue();
    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
    if (!(pair.left.schema instanceof BeamCalciteSchema)) {
        throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.internal("Schema is not instanceof BeamCalciteSchema"));
    }
    BeamCalciteSchema schema = (BeamCalciteSchema) pair.left.schema;
    if (value != null) {
        schema.setPipelineOption(pair.right, SqlDdlNodes.getString(value));
    } else if ("ALL".equals(pair.right)) {
        schema.removeAllPipelineOptions();
    } else {
        schema.removePipelineOption(pair.right);
    }
}
Also used : CalciteSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema) BeamCalciteSchema(org.apache.beam.sdk.extensions.sql.impl.BeamCalciteSchema) SqlIdentifier(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier) BeamCalciteSchema(org.apache.beam.sdk.extensions.sql.impl.BeamCalciteSchema) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode)

Example 65 with SqlIdentifier

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

the class BigQueryTable method generateRowRestrictions.

private String generateRowRestrictions(Schema schema, List<RexNode> supported) {
    assert !supported.isEmpty();
    final IntFunction<SqlNode> field = i -> new SqlIdentifier(schema.getField(i).getName(), SqlParserPos.ZERO);
    // TODO: BigQuerySqlDialectWithTypeTranslation can be replaced with BigQuerySqlDialect after
    // updating vendor Calcite version.
    SqlImplementor.Context context = new BeamSqlUnparseContext(field);
    // Create a single SqlNode from a list of RexNodes
    SqlNode andSqlNode = null;
    for (RexNode node : supported) {
        SqlNode sqlNode = context.toSql(null, node);
        if (andSqlNode == null) {
            andSqlNode = sqlNode;
            continue;
        }
        // AND operator must have exactly 2 operands.
        andSqlNode = SqlStdOperatorTable.AND.createCall(SqlParserPos.ZERO, ImmutableList.of(andSqlNode, sqlNode));
    }
    return andSqlNode.toSqlString(BeamBigQuerySqlDialect.DEFAULT).getSql();
}
Also used : Arrays(java.util.Arrays) PBegin(org.apache.beam.sdk.values.PBegin) Experimental(org.apache.beam.sdk.annotations.Experimental) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode) SqlIdentifier(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier) LoggerFactory(org.slf4j.LoggerFactory) ConversionOptions(org.apache.beam.sdk.io.gcp.bigquery.BigQueryUtils.ConversionOptions) BigQueryOptions(org.apache.beam.sdk.io.gcp.bigquery.BigQueryOptions) SqlImplementor(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.rel2sql.SqlImplementor) SqlParserPos(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserPos) SelectHelpers(org.apache.beam.sdk.schemas.utils.SelectHelpers) DefaultTableFilter(org.apache.beam.sdk.extensions.sql.meta.DefaultTableFilter) ProjectSupport(org.apache.beam.sdk.extensions.sql.meta.ProjectSupport) Table(org.apache.beam.sdk.extensions.sql.meta.Table) FieldAccessDescriptor(org.apache.beam.sdk.schemas.FieldAccessDescriptor) BigInteger(java.math.BigInteger) Row(org.apache.beam.sdk.values.Row) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode) IntFunction(java.util.function.IntFunction) SchemaBaseBeamTable(org.apache.beam.sdk.extensions.sql.meta.SchemaBaseBeamTable) BigQueryHelpers(org.apache.beam.sdk.io.gcp.bigquery.BigQueryHelpers) Logger(org.slf4j.Logger) BigQueryIO(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO) SchemaCoder(org.apache.beam.sdk.schemas.SchemaCoder) BeamSqlTableFilter(org.apache.beam.sdk.extensions.sql.meta.BeamSqlTableFilter) SqlStdOperatorTable(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.fun.SqlStdOperatorTable) IOException(java.io.IOException) Method(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TypedRead.Method) BigQueryUtils(org.apache.beam.sdk.io.gcp.bigquery.BigQueryUtils) PCollection(org.apache.beam.sdk.values.PCollection) Collectors(java.util.stream.Collectors) Schema(org.apache.beam.sdk.schemas.Schema) Serializable(java.io.Serializable) TypedRead(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TypedRead) VisibleForTesting(org.apache.beam.vendor.calcite.v1_28_0.com.google.common.annotations.VisibleForTesting) POutput(org.apache.beam.sdk.values.POutput) List(java.util.List) WriteDisposition(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.WriteDisposition) BeamTableStatistics(org.apache.beam.sdk.extensions.sql.impl.BeamTableStatistics) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) SqlIdentifier(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier) SqlImplementor(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.rel2sql.SqlImplementor) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)

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