Search in sources :

Example 26 with SqlNode

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

the class SqlAbstractGroupFunction method validateCall.

@Override
public void validateCall(SqlCall call, SqlValidator validator, SqlValidatorScope scope, SqlValidatorScope operandScope) {
    super.validateCall(call, validator, scope, operandScope);
    final SelectScope selectScope = SqlValidatorUtil.getEnclosingSelectScope(scope);
    assert selectScope != null;
    final SqlSelect select = selectScope.getNode();
    if (!validator.isAggregate(select)) {
        throw validator.newValidationError(call, Static.RESOURCE.groupingInAggregate(getName()));
    }
    final AggregatingSelectScope aggregatingSelectScope = SqlValidatorUtil.getEnclosingAggregateSelectScope(scope);
    if (aggregatingSelectScope == null) {
        // We're probably in the GROUP BY clause
        throw validator.newValidationError(call, Static.RESOURCE.groupingInWrongClause(getName()));
    }
    for (SqlNode operand : call.getOperandList()) {
        if (scope instanceof OrderByScope) {
            operand = validator.expandOrderExpr(select, operand);
        } else {
            operand = validator.expand(operand, scope);
        }
        if (!aggregatingSelectScope.resolved.get().isGroupingExpr(operand)) {
            throw validator.newValidationError(operand, Static.RESOURCE.groupingArgument(getName()));
        }
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SelectScope(org.apache.calcite.sql.validate.SelectScope) AggregatingSelectScope(org.apache.calcite.sql.validate.AggregatingSelectScope) OrderByScope(org.apache.calcite.sql.validate.OrderByScope) AggregatingSelectScope(org.apache.calcite.sql.validate.AggregatingSelectScope) SqlNode(org.apache.calcite.sql.SqlNode)

Example 27 with SqlNode

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

the class SqlTesterImpl method getResultType.

public RelDataType getResultType(String sql) {
    SqlValidator validator = getValidator();
    SqlNode n = parseAndValidate(validator, sql);
    return validator.getValidatedNodeType(n);
}
Also used : SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlNode(org.apache.calcite.sql.SqlNode)

Example 28 with SqlNode

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

the class SqlTesterImpl method checkFieldOrigin.

public void checkFieldOrigin(String sql, String fieldOriginList) {
    SqlValidator validator = getValidator();
    SqlNode n = parseAndValidate(validator, sql);
    final List<List<String>> list = validator.getFieldOrigins(n);
    final StringBuilder buf = new StringBuilder("{");
    int i = 0;
    for (List<String> strings : list) {
        if (i++ > 0) {
            buf.append(", ");
        }
        if (strings == null) {
            buf.append("null");
        } else {
            int j = 0;
            for (String s : strings) {
                if (j++ > 0) {
                    buf.append('.');
                }
                buf.append(s);
            }
        }
    }
    buf.append("}");
    assertEquals(fieldOriginList, buf.toString());
}
Also used : SqlValidator(org.apache.calcite.sql.validate.SqlValidator) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 29 with SqlNode

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

the class SqlTesterImpl method checkIntervalConv.

public void checkIntervalConv(String sql, String expected) {
    SqlValidator validator = getValidator();
    final SqlCall n = (SqlCall) parseAndValidate(validator, sql);
    SqlNode node = null;
    for (int i = 0; i < n.operandCount(); i++) {
        node = stripAs(n.operand(i));
        if (node instanceof SqlCall) {
            node = ((SqlCall) node).operand(0);
            break;
        }
    }
    assertNotNull(node);
    SqlIntervalLiteral intervalLiteral = (SqlIntervalLiteral) node;
    SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) intervalLiteral.getValue();
    long l = interval.getIntervalQualifier().isYearMonth() ? SqlParserUtil.intervalToMonths(interval) : SqlParserUtil.intervalToMillis(interval);
    String actual = l + "";
    assertEquals(expected, actual);
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlIntervalLiteral(org.apache.calcite.sql.SqlIntervalLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Example 30 with SqlNode

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

the class InterpreterTest method testInterpretUnionAll.

/**
 * Tests executing a UNION ALL query using an interpreter.
 */
@Test
public void testInterpretUnionAll() throws Exception {
    rootSchema.add("simple", new ScannableTableTest.SimpleTable());
    SqlNode parse = planner.parse("select * from \"simple\"\n" + "union all\n" + "select * from \"simple\"\n");
    SqlNode validate = planner.validate(parse);
    RelNode convert = planner.rel(validate).rel;
    final Interpreter interpreter = new Interpreter(dataContext, convert);
    assertRows(interpreter, "[0]", "[10]", "[20]", "[30]", "[0]", "[10]", "[20]", "[30]");
}
Also used : Interpreter(org.apache.calcite.interpreter.Interpreter) RelNode(org.apache.calcite.rel.RelNode) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Aggregations

SqlNode (org.apache.calcite.sql.SqlNode)510 RelDataType (org.apache.calcite.rel.type.RelDataType)141 SqlNodeList (org.apache.calcite.sql.SqlNodeList)98 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)84 SqlCall (org.apache.calcite.sql.SqlCall)81 ArrayList (java.util.ArrayList)78 RelNode (org.apache.calcite.rel.RelNode)60 Test (org.junit.Test)59 BitString (org.apache.calcite.util.BitString)46 SqlSelect (org.apache.calcite.sql.SqlSelect)45 SqlWriter (org.apache.calcite.sql.SqlWriter)42 RexNode (org.apache.calcite.rex.RexNode)40 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)33 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)25 SqlLiteral (org.apache.calcite.sql.SqlLiteral)23 SqlOperator (org.apache.calcite.sql.SqlOperator)23 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)22 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)21 ImmutableList (com.google.common.collect.ImmutableList)20 SqlValidator (org.apache.calcite.sql.validate.SqlValidator)20