Search in sources :

Example 41 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 42 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)

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