use of org.apache.calcite.sql.SqlCall in project calcite by apache.
the class SqlValidatorImpl method validateQuery.
public void validateQuery(SqlNode node, SqlValidatorScope scope, RelDataType targetRowType) {
final SqlValidatorNamespace ns = getNamespace(node, scope);
if (node.getKind() == SqlKind.TABLESAMPLE) {
List<SqlNode> operands = ((SqlCall) node).getOperandList();
SqlSampleSpec sampleSpec = SqlLiteral.sampleValue(operands.get(1));
if (sampleSpec instanceof SqlSampleSpec.SqlTableSampleSpec) {
validateFeature(RESOURCE.sQLFeature_T613(), node.getParserPosition());
} else if (sampleSpec instanceof SqlSampleSpec.SqlSubstitutionSampleSpec) {
validateFeature(RESOURCE.sQLFeatureExt_T613_Substitution(), node.getParserPosition());
}
}
validateNamespace(ns, targetRowType);
if (node == top) {
validateModality(node);
}
validateAccess(node, ns.getTable(), SqlAccessEnum.SELECT);
}
use of org.apache.calcite.sql.SqlCall in project calcite by apache.
the class SqlValidatorImpl method getNamespace.
private SqlValidatorNamespace getNamespace(SqlNode node, SqlValidatorScope scope) {
if (node instanceof SqlIdentifier && scope instanceof DelegatingScope) {
final SqlIdentifier id = (SqlIdentifier) node;
final DelegatingScope idScope = (DelegatingScope) ((DelegatingScope) scope).getParent();
return getNamespace(id, idScope);
} else if (node instanceof SqlCall) {
// Handle extended identifiers.
final SqlCall call = (SqlCall) node;
switch(call.getOperator().getKind()) {
case EXTEND:
final SqlIdentifier id = (SqlIdentifier) call.getOperandList().get(0);
final DelegatingScope idScope = (DelegatingScope) scope;
return getNamespace(id, idScope);
case AS:
final SqlNode nested = call.getOperandList().get(0);
switch(nested.getKind()) {
case EXTEND:
return getNamespace(nested, scope);
}
break;
}
}
return getNamespace(node);
}
use of org.apache.calcite.sql.SqlCall in project calcite by apache.
the class SqlValidatorImpl method checkConstraint.
/**
* Validates insert values against the constraint of a modifiable view.
*
* @param validatorTable Table that may wrap a ModifiableViewTable
* @param source The values being inserted
* @param targetRowType The target type for the view
*/
private void checkConstraint(SqlValidatorTable validatorTable, SqlNode source, RelDataType targetRowType) {
final ModifiableViewTable modifiableViewTable = validatorTable.unwrap(ModifiableViewTable.class);
if (modifiableViewTable != null && source instanceof SqlCall) {
final Table table = modifiableViewTable.unwrap(Table.class);
final RelDataType tableRowType = table.getRowType(typeFactory);
final List<RelDataTypeField> tableFields = tableRowType.getFieldList();
// Get the mapping from column indexes of the underlying table
// to the target columns and view constraints.
final Map<Integer, RelDataTypeField> tableIndexToTargetField = SqlValidatorUtil.getIndexToFieldMap(tableFields, targetRowType);
final Map<Integer, RexNode> projectMap = RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType, typeFactory);
// Determine columns (indexed to the underlying table) that need
// to be validated against the view constraint.
final ImmutableBitSet targetColumns = ImmutableBitSet.of(tableIndexToTargetField.keySet());
final ImmutableBitSet constrainedColumns = ImmutableBitSet.of(projectMap.keySet());
final ImmutableBitSet constrainedTargetColumns = targetColumns.intersect(constrainedColumns);
// Validate insert values against the view constraint.
final List<SqlNode> values = ((SqlCall) source).getOperandList();
for (final int colIndex : constrainedTargetColumns.asList()) {
final String colName = tableFields.get(colIndex).getName();
final RelDataTypeField targetField = tableIndexToTargetField.get(colIndex);
for (SqlNode row : values) {
final SqlCall call = (SqlCall) row;
final SqlNode sourceValue = call.operand(targetField.getIndex());
final ValidationError validationError = new ValidationError(sourceValue, RESOURCE.viewConstraintNotSatisfied(colName, Util.last(validatorTable.getQualifiedName())));
RelOptUtil.validateValueAgainstConstraint(sourceValue, projectMap.get(colIndex), validationError);
}
}
}
}
use of org.apache.calcite.sql.SqlCall in project calcite by apache.
the class SqlValidatorImpl method registerSetop.
private void registerSetop(SqlValidatorScope parentScope, SqlValidatorScope usingScope, SqlNode node, SqlNode enclosingNode, String alias, boolean forceNullable) {
SqlCall call = (SqlCall) node;
final SetopNamespace setopNamespace = createSetopNamespace(call, enclosingNode);
registerNamespace(usingScope, alias, setopNamespace, forceNullable);
// A setop is in the same scope as its parent.
scopes.put(call, parentScope);
for (SqlNode operand : call.getOperandList()) {
registerQuery(parentScope, null, operand, operand, null, false);
}
}
use of org.apache.calcite.sql.SqlCall in project calcite by apache.
the class SqlUserDefinedTableMacro method getValue.
private static Object getValue(SqlNode right) throws NonLiteralException {
switch(right.getKind()) {
case ARRAY_VALUE_CONSTRUCTOR:
final List<Object> list = Lists.newArrayList();
for (SqlNode o : ((SqlCall) right).getOperandList()) {
list.add(getValue(o));
}
return ImmutableNullableList.copyOf(list);
case MAP_VALUE_CONSTRUCTOR:
final ImmutableMap.Builder<Object, Object> builder2 = ImmutableMap.builder();
final List<SqlNode> operands = ((SqlCall) right).getOperandList();
for (int i = 0; i < operands.size(); i += 2) {
final SqlNode key = operands.get(i);
final SqlNode value = operands.get(i + 1);
builder2.put(getValue(key), getValue(value));
}
return builder2.build();
default:
if (SqlUtil.isNullLiteral(right, true)) {
return null;
}
if (SqlUtil.isLiteral(right)) {
return ((SqlLiteral) right).getValue();
}
if (right.getKind() == SqlKind.DEFAULT) {
// currently NULL is the only default value
return null;
}
throw new NonLiteralException();
}
}
Aggregations