protected RelOptTable getTargetTable(SqlNode call) { SqlValidatorNamespace targetNs = validator.getNamespace(call).resolve(); return SqlValidatorUtil.getRelOptTable(targetNs, catalogReader, null, null); }
private String deriveAlias( final SqlNode node, Collection<String> aliases, final int ordinal) { String alias = validator.deriveAlias(node, ordinal); if ((alias == null) || aliases.contains(alias)) { String aliasBase = (alias == null) ? "EXPR$" : alias; for (int j = 0;; j++) { alias = aliasBase + j; if (!aliases.contains(alias)) { break; } } } aliases.add(alias); return alias; }
protected RelDataType uniquifyFields(RelDataType rowType) { return validator.getTypeFactory().createStructType( RelOptUtil.getFieldTypeList(rowType), SqlValidatorUtil.uniquify(rowType.getFieldNames())); }
/** * Returns the type inferred for a dynamic parameter. * * @param index 0-based index of dynamic parameter * @return inferred type, never null */ public RelDataType getDynamicParamType(int index) { SqlNode sqlNode = dynamicParamSqlNodes.get(index); if (sqlNode == null) { throw Util.needToImplement("dynamic param type inference"); } return validator.getValidatedNodeType(sqlNode); }
List<SqlNode> orderList) { SqlNodeList selectList = select.getSelectList(); selectList = validator.expandStar(selectList, select, false); SqlNode expr2 = validator.expandOrderExpr(select, expr); exprs.add(bb.convertExpression(expr2)); fieldNames.add(deriveAlias(expr, aliases, i)); fieldNames = SqlValidatorUtil.uniquify(fieldNames);
identifier = bb.scope.fullyQualify(identifier); namespace = bb.scope.resolve(identifier.names.get(0), null, null); name = namespace.translate(name); namespace = null;
SqlNode converted = validator.expandOrderExpr(select, orderItem); final SelectScope selectScope = validator.getRawSelectScope(select); int ordinal = -1; for (SqlNode selectItem : selectScope.getExpandedSelectList()) { ++ordinal; if (converted.equalsDeep(stripAs(selectItem), false)) {
final SqlUserDefinedTableMacro udf = (SqlUserDefinedTableMacro) operator; final TranslatableTable table = udf.getTable(typeFactory, call.getOperandList()); final RelDataType rowType = table.getRowType(typeFactory); if (operator instanceof SqlUserDefinedTableFunction) { SqlUserDefinedTableFunction udtf = (SqlUserDefinedTableFunction) operator; elementType = udtf.getElementType(typeFactory, call.getOperandList()); } else { elementType = null; rexCall, elementType, validator.getValidatedNodeType(call), columnMappings); bb.setRoot(callRel, true);
final SqlValidatorScope[] ancestorScopes = {null}; SqlValidatorNamespace foundNs = scope.resolve(name, ancestorScopes, offsets); if (foundNs == null) { return null; String correlName = createCorrel(); mapCorrelToDeferred.put(correlName, lookup); final RelDataType rowType = foundNs.getRowType(); return rexBuilder.makeCorrel(rowType, correlName);
final boolean top) { if (needsValidation) { query = validator.validate(query);
final SqlValidatorScope seekScope = (seek instanceof SqlSelect) ? validator.getSelectScope((SqlSelect) seek) : null; final Blackboard seekBb = createBlackboard(seekScope, null);
/** * Converts an expression from {@link SqlNode} to {@link RexNode} format. * * @param node Expression to translate * @return Converted expression */ public RexNode convertExpression( SqlNode node) { Map<String, RelDataType> nameToTypeMap = Collections.emptyMap(); Blackboard bb = createBlackboard( new ParameterScope((SqlValidatorImpl) validator, nameToTypeMap), null); return bb.convertExpression(node); }
/** * Converts a SELECT statement's parse tree into a relational expression. */ public RelNode convertSelect(SqlSelect select) { final SqlValidatorScope selectScope = validator.getWhereScope(select); final Blackboard bb = createBlackboard(selectScope, null); convertSelectImpl(bb, select); return bb.root; }
/** * Converts a SELECT statement's parse tree into a relational expression. */ public RelNode convertValues( SqlCall values, RelDataType targetRowType) { final SqlValidatorScope scope = validator.getOverScope(values); assert scope != null; final Blackboard bb = createBlackboard(scope, null); convertValuesImpl(bb, values, targetRowType); return bb.root; }
protected boolean checkConvertedRowType( SqlNode query, RelDataType convertedRowType) { RelDataType validatedRowType = validator.getValidatedNodeType(query); validatedRowType = uniquifyFields(validatedRowType); return RelOptUtil.equal( "validated row type", validatedRowType, "converted row type", convertedRowType, false); }
/** * Creates an AggConverter. * * <p>The <code>select</code> parameter provides enough context to name * aggregate calls which are top-level select list items. * * @param bb Blackboard * @param select Query being translated; provides context to give */ public AggConverter(Blackboard bb, SqlSelect select) { this.bb = bb; // Collect all expressions used in the select list so that aggregate // calls can be named correctly. final SqlNodeList selectList = select.getSelectList(); for (int i = 0; i < selectList.size(); i++) { SqlNode selectItem = selectList.get(i); String name = null; if (SqlUtil.isCallTo( selectItem, SqlStdOperatorTable.AS)) { final SqlCall call = (SqlCall) selectItem; selectItem = call.operand(0); name = call.operand(1).toString(); } if (name == null) { name = validator.deriveAlias(selectItem, i); } nameMap.put(selectItem.toString(), name); } }
/** * Converts an expression from {@link SqlNode} to {@link RexNode} format, * mapping identifier references to predefined expressions. * * @param node Expression to translate * @param nameToNodeMap map from String to {@link RexNode}; when an * {@link SqlIdentifier} is encountered, it is used as a * key and translated to the corresponding value from * this map * @return Converted expression */ public RexNode convertExpression( SqlNode node, Map<String, RexNode> nameToNodeMap) { final Map<String, RelDataType> nameToTypeMap = new HashMap<String, RelDataType>(); for (Map.Entry<String, RexNode> entry : nameToNodeMap.entrySet()) { nameToTypeMap.put(entry.getKey(), entry.getValue().getType()); } Blackboard bb = createBlackboard( new ParameterScope((SqlValidatorImpl) validator, nameToTypeMap), nameToNodeMap); return bb.convertExpression(node); }
validator.getValidatedNodeType(call); assert targetRowType != null; RelNode sourceRel =
private void checkConvertedType(SqlNode query, RelNode result) { if (!query.isA(SqlKind.DML)) { // Verify that conversion from SQL to relational algebra did // not perturb any type information. (We can't do this if the // SQL statement is something like an INSERT which has no // validator type information associated with its result, // hence the namespace check above.) RelDataType convertedRowType = result.getRowType(); if (!checkConvertedRowType(query, convertedRowType)) { RelDataType validatedRowType = validator.getValidatedNodeType(query); validatedRowType = uniquifyFields(validatedRowType); throw Util.newInternal( "Conversion to relational algebra failed to preserve " + "datatypes:\n" + "validated type:\n" + validatedRowType.getFullTypeString() + "\nconverted type:\n" + convertedRowType.getFullTypeString() + "\nrel:\n" + RelOptUtil.toString(result)); } } }