Search in sources :

Example 21 with AND

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

the class BigQueryFilter method isSupported.

/**
 * Check whether a {@code RexNode} is supported. As of right now BigQuery supports: 1. Complex
 * predicates (both conjunction and disjunction). 2. Comparison between a column and a literal.
 *
 * <p>TODO: Check if comparison between two columns is supported. Also over a boolean field.
 *
 * @param node A node to check for predicate push-down support.
 * @return A pair containing a boolean whether an expression is supported and the number of input
 *     references used by the expression.
 */
private Pair<Boolean, Integer> isSupported(RexNode node) {
    int numberOfInputRefs = 0;
    boolean isSupported = true;
    if (node instanceof RexCall) {
        RexCall compositeNode = (RexCall) node;
        // CAST, TRIM? and REVERSE? should be supported as well.
        if (!node.getKind().belongsTo(SUPPORTED_OPS)) {
            isSupported = false;
        } else {
            for (RexNode operand : compositeNode.getOperands()) {
                // All operands must be supported for a parent node to be supported.
                Pair<Boolean, Integer> childSupported = isSupported(operand);
                // (OR).
                if (!node.getKind().belongsTo(ImmutableSet.of(AND, OR))) {
                    numberOfInputRefs += childSupported.getRight();
                }
                // Predicate functions, where more than one field is involved are unsupported.
                isSupported = numberOfInputRefs < 2 && childSupported.getLeft();
            }
        }
    } else if (node instanceof RexInputRef) {
        numberOfInputRefs = 1;
    } else if (node instanceof RexLiteral) {
    // RexLiterals are expected, but no action is needed.
    } else {
        throw new UnsupportedOperationException("Encountered an unexpected node type: " + node.getClass().getSimpleName());
    }
    return Pair.of(isSupported, numberOfInputRefs);
}
Also used : RexCall(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexCall) RexLiteral(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral) RexInputRef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexInputRef) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)

Example 22 with AND

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind.AND 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)

Example 23 with AND

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

the class QueryReader method getQueryIdentifiers.

/**
 * Parse query and get all its identifiers.
 *
 * @param queryString
 * @return Set of SQL query identifiers as strings.
 * @throws SqlParseException
 */
public static Set<String> getQueryIdentifiers(String queryString) throws SqlParseException {
    SqlParser parser = SqlParser.create(queryString);
    SqlNode parsedQuery = parser.parseQuery();
    SqlTransformRunner.SqlIdentifierVisitor sqlVisitor = new SqlTransformRunner.SqlIdentifierVisitor();
    parsedQuery.accept(sqlVisitor);
    return sqlVisitor.getIdentifiers();
}
Also used : SqlParser(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParser) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode)

Example 24 with AND

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

the class BeamJoinRel method getBoundednessOfRelNode.

/**
 * This method returns the Boundedness of a RelNode. It is used during planning and applying
 * {@link org.apache.beam.sdk.extensions.sql.impl.rule.BeamCoGBKJoinRule} and {@link
 * org.apache.beam.sdk.extensions.sql.impl.rule.BeamSideInputJoinRule}
 *
 * <p>The Volcano planner works in a top-down fashion. It starts by transforming the root and move
 * towards the leafs of the plan. Due to this when transforming a logical join its inputs are
 * still in the logical convention. So, Recursively visit the inputs of the RelNode till
 * BeamIOSourceRel is encountered and propagate the boundedness upwards.
 *
 * <p>The Boundedness of each child of a RelNode is stored in a list. If any of the children are
 * Unbounded, the RelNode is Unbounded. Else, the RelNode is Bounded.
 *
 * @param relNode the RelNode whose Boundedness has to be determined
 * @return {@code PCollection.isBounded}
 */
public static PCollection.IsBounded getBoundednessOfRelNode(RelNode relNode) {
    if (relNode instanceof BeamRelNode) {
        return (((BeamRelNode) relNode).isBounded());
    }
    List<PCollection.IsBounded> boundednessOfInputs = new ArrayList<>();
    for (RelNode inputRel : relNode.getInputs()) {
        if (inputRel instanceof RelSubset) {
            // Consider the RelNode with best cost in the RelSubset. If best cost RelNode cannot be
            // determined, consider the first RelNode in the RelSubset
            RelNode rel = ((RelSubset) inputRel).getBest();
            if (rel == null) {
                rel = ((RelSubset) inputRel).getRelList().get(0);
            }
            boundednessOfInputs.add(getBoundednessOfRelNode(rel));
        } else {
            boundednessOfInputs.add(getBoundednessOfRelNode(inputRel));
        }
    }
    // If one of the input is Unbounded, the result is Unbounded.
    return (boundednessOfInputs.contains(PCollection.IsBounded.UNBOUNDED) ? PCollection.IsBounded.UNBOUNDED : PCollection.IsBounded.BOUNDED);
}
Also used : RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) RelSubset(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.volcano.RelSubset)

Example 25 with AND

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

the class BeamCalcRule method matches.

@Override
public boolean matches(RelOptRuleCall x) {
    /**
     * The Analytic Functions (a.k.a. window functions) match with both Calc and Window rules. So,
     * it is necessary to skip the Calc rule in order to execute the more suitable conversion
     * (BeamWindowRule).
     */
    boolean hasRexOver = false;
    List<RelNode> resList = x.getRelList();
    for (RelNode relNode : resList) {
        if (relNode instanceof LogicalCalc) {
            LogicalCalc logicalCalc = (LogicalCalc) relNode;
            for (RexNode rexNode : logicalCalc.getProgram().getExprList()) {
                if (rexNode instanceof RexOver) {
                    hasRexOver = true;
                    break;
                }
            }
        }
    }
    return !hasRexOver;
}
Also used : RexOver(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexOver) RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) LogicalCalc(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalCalc) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)

Aggregations

RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)16 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 RelNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode)9 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 Serializable (java.io.Serializable)7 Arrays (java.util.Arrays)7 RelDataType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType)7 IOException (java.io.IOException)6 Statement (java.sql.Statement)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Schema (org.apache.beam.sdk.schemas.Schema)6 PCollection (org.apache.beam.sdk.values.PCollection)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 StandardCharsets (java.nio.charset.StandardCharsets)5 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)5