Search in sources :

Example 1 with SqlIdentifier

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

the class DrillCompoundIdentifier method getAsSqlNode.

public SqlNode getAsSqlNode() {
    if (ids.size() == 1) {
        return new SqlIdentifier(Collections.singletonList(ids.get(0).value), ids.get(0).parserPos);
    }
    int startIndex;
    SqlNode node;
    if (ids.get(1).isArray()) {
        // handle everything post zero index as item operator.
        startIndex = 1;
        node = new //
        SqlIdentifier(//
        ImmutableList.of(ids.get(0).value), //
        null, //
        ids.get(0).parserPos, ImmutableList.of(ids.get(0).parserPos));
    } else {
        // handle everything post two index as item operator.
        startIndex = 2;
        node = new //
        SqlIdentifier(//
        ImmutableList.of(ids.get(0).value, ids.get(1).value), //
        null, //
        ids.get(0).parserPos, ImmutableList.of(ids.get(0).parserPos, ids.get(1).parserPos));
    }
    for (int i = startIndex; i < ids.size(); i++) {
        node = ids.get(i).getNode(node);
    }
    return node;
}
Also used : SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 2 with SqlIdentifier

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

the class SqlSequenceValueOperator method validateCall.

@Override
public void validateCall(SqlCall call, SqlValidator validator, SqlValidatorScope scope, SqlValidatorScope operandScope) {
    List<SqlNode> operands = call.getOperandList();
    assert operands.size() == 1;
    assert operands.get(0) instanceof SqlIdentifier;
    SqlIdentifier id = (SqlIdentifier) operands.get(0);
    validator.validateSequenceValue(scope, id);
}
Also used : SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 3 with SqlIdentifier

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

the class SqlOperatorBaseTest method testSqlOperatorOverloading.

@Test
public void testSqlOperatorOverloading() {
    final SqlStdOperatorTable operatorTable = SqlStdOperatorTable.instance();
    for (SqlOperator sqlOperator : operatorTable.getOperatorList()) {
        String operatorName = sqlOperator.getName();
        List<SqlOperator> routines = new ArrayList<>();
        operatorTable.lookupOperatorOverloads(new SqlIdentifier(operatorName, SqlParserPos.ZERO), null, sqlOperator.getSyntax(), routines);
        Iterator<SqlOperator> iter = routines.iterator();
        while (iter.hasNext()) {
            SqlOperator operator = iter.next();
            if (!sqlOperator.getClass().isInstance(operator)) {
                iter.remove();
            }
        }
        assertThat(routines.size(), equalTo(1));
        assertThat(sqlOperator, equalTo(routines.get(0)));
    }
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) ArrayList(java.util.ArrayList) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) TimestampString(org.apache.calcite.util.TimestampString) SqlString(org.apache.calcite.sql.util.SqlString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlLimitsTest(org.apache.calcite.test.SqlLimitsTest) Test(org.junit.Test)

Example 4 with SqlIdentifier

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

the class SqlCreateTable method execute.

public void execute(CalcitePrepare.Context context) {
    final List<String> path = context.getDefaultSchemaPath();
    CalciteSchema schema = context.getRootSchema();
    for (String p : path) {
        schema = schema.getSubSchema(p, true);
    }
    final JavaTypeFactory typeFactory = new JavaTypeFactoryImpl();
    final RelDataTypeFactory.Builder builder = typeFactory.builder();
    for (Pair<SqlIdentifier, SqlDataTypeSpec> pair : nameTypes()) {
        builder.add(pair.left.getSimple(), pair.right.deriveType(typeFactory, true));
    }
    final RelDataType rowType = builder.build();
    schema.add(name.getSimple(), new MutableArrayTable(name.getSimple(), RelDataTypeImpl.proto(rowType)));
}
Also used : JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) SqlDataTypeSpec(org.apache.calcite.sql.SqlDataTypeSpec) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 5 with SqlIdentifier

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

the class RelToSqlConverter method visit.

/**
 * @see #dispatch
 */
public Result visit(Values e) {
    final List<Clause> clauses = ImmutableList.of(Clause.SELECT);
    final Map<String, RelDataType> pairs = ImmutableMap.of();
    final Context context = aliasContext(pairs, false);
    SqlNode query;
    final boolean rename = stack.size() <= 1 || !(Iterables.get(stack, 1).r instanceof TableModify);
    final List<String> fieldNames = e.getRowType().getFieldNames();
    if (!dialect.supportsAliasedValues() && rename) {
        // Oracle does not support "AS t (c1, c2)". So instead of
        // (VALUES (v0, v1), (v2, v3)) AS t (c0, c1)
        // we generate
        // SELECT v0 AS c0, v1 AS c1 FROM DUAL
        // UNION ALL
        // SELECT v2 AS c0, v3 AS c1 FROM DUAL
        List<SqlSelect> list = new ArrayList<>();
        for (List<RexLiteral> tuple : e.getTuples()) {
            final List<SqlNode> values2 = new ArrayList<>();
            final SqlNodeList exprList = exprList(context, tuple);
            for (Pair<SqlNode, String> value : Pair.zip(exprList, fieldNames)) {
                values2.add(SqlStdOperatorTable.AS.createCall(POS, value.left, new SqlIdentifier(value.right, POS)));
            }
            list.add(new SqlSelect(POS, null, new SqlNodeList(values2, POS), new SqlIdentifier("DUAL", POS), null, null, null, null, null, null, null));
        }
        if (list.size() == 1) {
            query = list.get(0);
        } else {
            query = SqlStdOperatorTable.UNION_ALL.createCall(new SqlNodeList(list, POS));
        }
    } else {
        // Generate ANSI syntax
        // (VALUES (v0, v1), (v2, v3))
        // or, if rename is required
        // (VALUES (v0, v1), (v2, v3)) AS t (c0, c1)
        final SqlNodeList selects = new SqlNodeList(POS);
        for (List<RexLiteral> tuple : e.getTuples()) {
            selects.add(ANONYMOUS_ROW.createCall(exprList(context, tuple)));
        }
        query = SqlStdOperatorTable.VALUES.createCall(selects);
        if (rename) {
            final List<SqlNode> list = new ArrayList<>();
            list.add(query);
            list.add(new SqlIdentifier("t", POS));
            for (String fieldName : fieldNames) {
                list.add(new SqlIdentifier(fieldName, POS));
            }
            query = SqlStdOperatorTable.AS.createCall(POS, list);
        }
    }
    return result(query, clauses, e, null);
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlSelect(org.apache.calcite.sql.SqlSelect) SqlNodeList(org.apache.calcite.sql.SqlNodeList) TableModify(org.apache.calcite.rel.core.TableModify) 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