Search in sources :

Example 66 with SqlIdentifier

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

Example 67 with SqlIdentifier

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

the class DruidPlanner method validateAndGetDataSourceForInsert.

/**
 * Extract target datasource from a {@link SqlInsert}, and also validate that the INSERT is of a form we support.
 * Expects the INSERT target to be either an unqualified name, or a name qualified by the default schema.
 */
private String validateAndGetDataSourceForInsert(final SqlInsert insert) throws ValidationException {
    if (insert.isUpsert()) {
        throw new ValidationException("UPSERT is not supported.");
    }
    if (insert.getTargetColumnList() != null) {
        throw new ValidationException("INSERT with target column list is not supported.");
    }
    final SqlIdentifier tableIdentifier = (SqlIdentifier) insert.getTargetTable();
    final String dataSource;
    if (tableIdentifier.names.isEmpty()) {
        // I don't think this can happen, but include a branch for it just in case.
        throw new ValidationException("INSERT requires target table.");
    } else if (tableIdentifier.names.size() == 1) {
        // Unqualified name.
        dataSource = Iterables.getOnlyElement(tableIdentifier.names);
    } else {
        // Qualified name.
        final String defaultSchemaName = Iterables.getOnlyElement(CalciteSchema.from(frameworkConfig.getDefaultSchema()).path(null));
        if (tableIdentifier.names.size() == 2 && defaultSchemaName.equals(tableIdentifier.names.get(0))) {
            dataSource = tableIdentifier.names.get(1);
        } else {
            throw new ValidationException(StringUtils.format("Cannot INSERT into [%s] because it is not a Druid datasource.", tableIdentifier));
        }
    }
    try {
        IdUtils.validateId("INSERT dataSource", dataSource);
    } catch (IllegalArgumentException e) {
        throw new ValidationException(e.getMessage());
    }
    return dataSource;
}
Also used : ValidationException(org.apache.calcite.tools.ValidationException) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 68 with SqlIdentifier

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

the class DruidSqlParserUtils method convertSqlNodeToGranularity.

/**
 * This method is used to extract the granularity from a SqlNode representing following function calls:
 * 1. FLOOR(__time TO TimeUnit)
 * 2. TIME_FLOOR(__time, 'PT1H')
 *
 * Validation on the sqlNode is contingent to following conditions:
 * 1. sqlNode is an instance of SqlCall
 * 2. Operator is either one of TIME_FLOOR or FLOOR
 * 3. Number of operands in the call are 2
 * 4. First operand is a SimpleIdentifier representing __time
 * 5. If operator is TIME_FLOOR, the second argument is a literal, and can be converted to the Granularity class
 * 6. If operator is FLOOR, the second argument is a TimeUnit, and can be mapped using {@link TimeUnits}
 *
 * Since it is to be used primarily while parsing the SqlNode, it is wrapped in {@code convertSqlNodeToGranularityThrowingParseExceptions}
 *
 * @param sqlNode SqlNode representing a call to a function
 * @return Granularity as intended by the function call
 * @throws ParseException SqlNode cannot be converted a granularity
 */
public static Granularity convertSqlNodeToGranularity(SqlNode sqlNode) throws ParseException {
    final String genericParseFailedMessageFormatString = "Encountered %s after PARTITIONED BY. " + "Expected HOUR, DAY, MONTH, YEAR, ALL TIME, FLOOR function or %s function";
    if (!(sqlNode instanceof SqlCall)) {
        throw new ParseException(StringUtils.format(genericParseFailedMessageFormatString, sqlNode.toString(), TimeFloorOperatorConversion.SQL_FUNCTION_NAME));
    }
    SqlCall sqlCall = (SqlCall) sqlNode;
    String operatorName = sqlCall.getOperator().getName();
    Preconditions.checkArgument("FLOOR".equalsIgnoreCase(operatorName) || TimeFloorOperatorConversion.SQL_FUNCTION_NAME.equalsIgnoreCase(operatorName), StringUtils.format("PARTITIONED BY clause only supports FLOOR(__time TO <unit> and %s(__time, period) functions", TimeFloorOperatorConversion.SQL_FUNCTION_NAME));
    List<SqlNode> operandList = sqlCall.getOperandList();
    Preconditions.checkArgument(operandList.size() == 2, StringUtils.format("%s in PARTITIONED BY clause must have two arguments", operatorName));
    // Check if the first argument passed in the floor function is __time
    SqlNode timeOperandSqlNode = operandList.get(0);
    Preconditions.checkArgument(timeOperandSqlNode.getKind().equals(SqlKind.IDENTIFIER), StringUtils.format("First argument to %s in PARTITIONED BY clause can only be __time", operatorName));
    SqlIdentifier timeOperandSqlIdentifier = (SqlIdentifier) timeOperandSqlNode;
    Preconditions.checkArgument(timeOperandSqlIdentifier.getSimple().equals(ColumnHolder.TIME_COLUMN_NAME), StringUtils.format("First argument to %s in PARTITIONED BY clause can only be __time", operatorName));
    // If the floor function is of form TIME_FLOOR(__time, 'PT1H')
    if (operatorName.equalsIgnoreCase(TimeFloorOperatorConversion.SQL_FUNCTION_NAME)) {
        SqlNode granularitySqlNode = operandList.get(1);
        Preconditions.checkArgument(granularitySqlNode.getKind().equals(SqlKind.LITERAL), "Second argument to TIME_FLOOR in PARTITIONED BY clause must be a period like 'PT1H'");
        String granularityString = SqlLiteral.unchain(granularitySqlNode).toValue();
        Period period;
        try {
            period = new Period(granularityString);
        } catch (IllegalArgumentException e) {
            throw new ParseException(StringUtils.format("%s is an invalid period string", granularitySqlNode.toString()));
        }
        return new PeriodGranularity(period, null, null);
    } else if ("FLOOR".equalsIgnoreCase(operatorName)) {
        // If the floor function is of form FLOOR(__time TO DAY)
        SqlNode granularitySqlNode = operandList.get(1);
        // In future versions of Calcite, this can be checked via
        // granularitySqlNode.getKind().equals(SqlKind.INTERVAL_QUALIFIER)
        Preconditions.checkArgument(granularitySqlNode instanceof SqlIntervalQualifier, "Second argument to the FLOOR function in PARTITIONED BY clause is not a valid granularity. " + "Please refer to the documentation of FLOOR function");
        SqlIntervalQualifier granularityIntervalQualifier = (SqlIntervalQualifier) granularitySqlNode;
        Period period = TimeUnits.toPeriod(granularityIntervalQualifier.timeUnitRange);
        Preconditions.checkNotNull(period, StringUtils.format("%s is not a valid granularity for ingestion", granularityIntervalQualifier.timeUnitRange.toString()));
        return new PeriodGranularity(period, null, null);
    }
    // Shouldn't reach here
    throw new ParseException(StringUtils.format(genericParseFailedMessageFormatString, sqlNode.toString(), TimeFloorOperatorConversion.SQL_FUNCTION_NAME));
}
Also used : SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) SqlCall(org.apache.calcite.sql.SqlCall) PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) Period(org.joda.time.Period) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 69 with SqlIdentifier

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

the class SqlCreateIndex method validate.

@Override
public void validate(SqlValidator validator, SqlValidatorScope scope) {
    if (getReplace()) {
        throw validator.newValidationError(this, RESOURCE.notSupported("OR REPLACE", "CREATE INDEX"));
    }
    Set<String> columnNames = new HashSet<>();
    for (SqlNode column : columns.getList()) {
        String name = ((SqlIdentifier) requireNonNull(column)).getSimple();
        if (!columnNames.add(name)) {
            throw validator.newValidationError(column, RESOURCE.duplicateIndexAttribute(name));
        }
    }
    IndexType indexType = getIndexType();
    if (!indexType.equals(IndexType.BITMAP) && !options.getList().isEmpty()) {
        throw validator.newValidationError(options, RESOURCE.unsupportedIndexType(indexType.name(), options().keySet().iterator().next()));
    }
    Set<String> optionNames = new HashSet<>();
    for (SqlNode option : options.getList()) {
        String name = ((SqlOption) option).keyString();
        if (!optionNames.add(name)) {
            throw validator.newValidationError(option, RESOURCE.duplicateOption(name));
        }
    }
}
Also used : SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) IndexType(com.hazelcast.config.IndexType) HashSet(java.util.HashSet) SqlNode(org.apache.calcite.sql.SqlNode)

Example 70 with SqlIdentifier

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

the class NamedOperandCheckerProgram method check.

public boolean check(HazelcastCallBinding callBinding, boolean throwOnFailure) {
    boolean res = true;
    SqlCall call = callBinding.getCall();
    SqlFunction operator = (SqlFunction) call.getOperator();
    for (int i = 0; i < call.operandCount(); i++) {
        SqlNode operand = call.operand(i);
        assert operand.getKind() == SqlKind.ARGUMENT_ASSIGNMENT;
        SqlIdentifier id = ((SqlCall) operand).operand(1);
        OperandChecker checker = findOperandChecker(id, operator);
        res &= checker.check(callBinding, false, i);
    }
    if (!res && throwOnFailure) {
        throw callBinding.newValidationSignatureError();
    }
    return res;
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlFunction(org.apache.calcite.sql.SqlFunction) 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