Search in sources :

Example 36 with SqlParserPos

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserPos in project drill by apache.

the class TestDrillSQLWorker method testErrorFormatting.

@Test
public void testErrorFormatting() {
    String sql = "Select * from Foo\n" + "where tadadidada;\n";
    validateFormattedIs(sql, new SqlParserPos(1, 2), "SQL Query: Select * from Foo\n" + "            ^\n" + "where tadadidada;");
    validateFormattedIs(sql, new SqlParserPos(2, 2), "SQL Query: Select * from Foo\n" + "where tadadidada;\n" + " ^");
    validateFormattedIs(sql, new SqlParserPos(1, 10), "SQL Query: Select * from Foo\n" + "                    ^\n" + "where tadadidada;");
    validateFormattedIs(sql, new SqlParserPos(-11, -10), "SQL Query: Select * from Foo\nwhere tadadidada;");
    validateFormattedIs(sql, new SqlParserPos(0, 10), "SQL Query: Select * from Foo\nwhere tadadidada;");
    validateFormattedIs(sql, new SqlParserPos(100, 10), "SQL Query: Select * from Foo\nwhere tadadidada;");
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) Test(org.junit.Test) SqlTest(org.apache.drill.categories.SqlTest)

Example 37 with SqlParserPos

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

the class SqlCoalesceFunction method rewriteCall.

// ~ Methods ----------------------------------------------------------------
// override SqlOperator
public SqlNode rewriteCall(SqlValidator validator, SqlCall call) {
    // check DISTINCT/ALL
    validateQuantifier(validator, call);
    List<SqlNode> operands = call.getOperandList();
    if (operands.size() == 1) {
        // No CASE needed
        return operands.get(0);
    }
    SqlParserPos pos = call.getParserPosition();
    SqlNodeList whenList = new SqlNodeList(pos);
    SqlNodeList thenList = new SqlNodeList(pos);
    for (SqlNode operand : Util.skipLast(operands)) {
        whenList.add(SqlStdOperatorTable.IS_NOT_NULL.createCall(pos, operand));
        thenList.add(SqlNode.clone(operand));
    }
    SqlNode elseExpr = Util.last(operands);
    assert call.getFunctionQuantifier() == null;
    return SqlCase.createSwitched(pos, null, whenList, thenList, elseExpr);
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 38 with SqlParserPos

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

the class SqlLiteralChainOperator method validateCall.

public void validateCall(SqlCall call, SqlValidator validator, SqlValidatorScope scope, SqlValidatorScope operandScope) {
    // per the SQL std, each string fragment must be on a different line
    final List<SqlNode> operandList = call.getOperandList();
    for (int i = 1; i < operandList.size(); i++) {
        SqlParserPos prevPos = operandList.get(i - 1).getParserPosition();
        final SqlNode operand = operandList.get(i);
        SqlParserPos pos = operand.getParserPosition();
        if (pos.getLineNum() <= prevPos.getLineNum()) {
            throw validator.newValidationError(operand, RESOURCE.stringFragsOnSameLine());
        }
    }
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlNode(org.apache.calcite.sql.SqlNode)

Example 39 with SqlParserPos

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

the class SqlValidatorImpl method expandStar.

private boolean expandStar(List<SqlNode> selectItems, Set<String> aliases, List<Map.Entry<String, RelDataType>> types, boolean includeSystemVars, SelectScope scope, SqlNode node) {
    if (!(node instanceof SqlIdentifier)) {
        return false;
    }
    final SqlIdentifier identifier = (SqlIdentifier) node;
    if (!identifier.isStar()) {
        return false;
    }
    final SqlParserPos startPosition = identifier.getParserPosition();
    switch(identifier.names.size()) {
        case 1:
            for (ScopeChild child : scope.children) {
                final int before = types.size();
                if (child.namespace.getRowType().isDynamicStruct()) {
                    // don't expand star if the underneath table is dynamic.
                    // Treat this star as a special field in validation/conversion and
                    // wait until execution time to expand this star.
                    final SqlNode exp = new SqlIdentifier(ImmutableList.of(child.name, DynamicRecordType.DYNAMIC_STAR_PREFIX), startPosition);
                    addToSelectList(selectItems, aliases, types, exp, scope, includeSystemVars);
                } else {
                    final SqlNode from = child.namespace.getNode();
                    final SqlValidatorNamespace fromNs = getNamespace(from, scope);
                    assert fromNs != null;
                    final RelDataType rowType = fromNs.getRowType();
                    for (RelDataTypeField field : rowType.getFieldList()) {
                        String columnName = field.getName();
                        // TODO: do real implicit collation here
                        final SqlIdentifier exp = new SqlIdentifier(ImmutableList.of(child.name, columnName), startPosition);
                        // Don't add expanded rolled up columns
                        if (!isRolledUpColumn(exp, scope)) {
                            addOrExpandField(selectItems, aliases, types, includeSystemVars, scope, exp, field);
                        }
                    }
                }
                if (child.nullable) {
                    for (int i = before; i < types.size(); i++) {
                        final Map.Entry<String, RelDataType> entry = types.get(i);
                        final RelDataType type = entry.getValue();
                        if (!type.isNullable()) {
                            types.set(i, Pair.of(entry.getKey(), typeFactory.createTypeWithNullability(type, true)));
                        }
                    }
                }
            }
            return true;
        default:
            final SqlIdentifier prefixId = identifier.skipLast(1);
            final SqlValidatorScope.ResolvedImpl resolved = new SqlValidatorScope.ResolvedImpl();
            final SqlNameMatcher nameMatcher = scope.validator.catalogReader.nameMatcher();
            scope.resolve(prefixId.names, nameMatcher, true, resolved);
            if (resolved.count() == 0) {
                // or "select r.* from e"
                throw newValidationError(prefixId, RESOURCE.unknownIdentifier(prefixId.toString()));
            }
            final RelDataType rowType = resolved.only().rowType();
            if (rowType.isDynamicStruct()) {
                // don't expand star if the underneath table is dynamic.
                addToSelectList(selectItems, aliases, types, prefixId.plus(DynamicRecordType.DYNAMIC_STAR_PREFIX, startPosition), scope, includeSystemVars);
            } else if (rowType.isStruct()) {
                for (RelDataTypeField field : rowType.getFieldList()) {
                    String columnName = field.getName();
                    // TODO: do real implicit collation here
                    addOrExpandField(selectItems, aliases, types, includeSystemVars, scope, prefixId.plus(columnName, startPosition), field);
                }
            } else {
                throw newValidationError(prefixId, RESOURCE.starRequiresRecordType());
            }
            return true;
    }
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) SqlNode(org.apache.calcite.sql.SqlNode)

Example 40 with SqlParserPos

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserPos in project hive by apache.

the class RexNodeExprFactory method createIntervalHourConstantExpr.

/**
 * {@inheritDoc}
 */
@Override
protected RexLiteral createIntervalHourConstantExpr(String value) {
    HiveIntervalDayTime v = new HiveIntervalDayTime(0, Integer.parseInt(value), 0, 0, 0);
    BigDecimal secsValueBd = BigDecimal.valueOf(v.getTotalSeconds() * 1000);
    BigDecimal nanosValueBd = BigDecimal.valueOf((v).getNanos(), 6);
    return rexBuilder.makeIntervalLiteral(secsValueBd.add(nanosValueBd), new SqlIntervalQualifier(TimeUnit.MILLISECOND, null, new SqlParserPos(1, 1)));
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) HiveIntervalDayTime(org.apache.hadoop.hive.common.type.HiveIntervalDayTime) BigDecimal(java.math.BigDecimal)

Aggregations

SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)42 SqlNode (org.apache.calcite.sql.SqlNode)17 SqlIntervalQualifier (org.apache.calcite.sql.SqlIntervalQualifier)11 RelDataType (org.apache.calcite.rel.type.RelDataType)9 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)9 BigDecimal (java.math.BigDecimal)8 HiveIntervalDayTime (org.apache.hadoop.hive.common.type.HiveIntervalDayTime)6 SqlCall (org.apache.calcite.sql.SqlCall)5 ArrayList (java.util.ArrayList)4 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)4 SqlNodeList (org.apache.calcite.sql.SqlNodeList)4 ImmutableList (com.google.common.collect.ImmutableList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 RexNode (org.apache.calcite.rex.RexNode)3 SqlNumericLiteral (org.apache.calcite.sql.SqlNumericLiteral)3 SqlOperator (org.apache.calcite.sql.SqlOperator)3 Calendar (java.util.Calendar)2 IdentityHashMap (java.util.IdentityHashMap)2 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)2