use of org.apache.calcite.sql.SqlCall in project flink 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 SqlFloorFunction method unparseDatetimeFunction.
/**
* Most dialects that natively support datetime floor will use this.
* In those cases the call will look like TRUNC(datetime, 'year').
*
* @param writer SqlWriter
* @param call SqlCall
* @param funName Name of the sql function to call
* @param datetimeFirst Specify the order of the datetime & timeUnit
* arguments
*/
public static void unparseDatetimeFunction(SqlWriter writer, SqlCall call, String funName, Boolean datetimeFirst) {
SqlFunction func = new SqlFunction(funName, SqlKind.OTHER_FUNCTION, ReturnTypes.ARG0_NULLABLE_VARYING, null, null, SqlFunctionCategory.STRING);
SqlCall call1;
if (datetimeFirst) {
call1 = call;
} else {
// switch order of operands
SqlNode op1 = call.operand(0);
SqlNode op2 = call.operand(1);
call1 = call.getOperator().createCall(call.getParserPosition(), op2, op1);
}
SqlUtil.unparseFunctionSyntax(func, writer, call1);
}
use of org.apache.calcite.sql.SqlCall in project calcite by apache.
the class SqlCase method createSwitched.
/**
* Creates a call to the switched form of the case operator, viz:
*
* <blockquote><code>CASE value<br>
* WHEN whenList[0] THEN thenList[0]<br>
* WHEN whenList[1] THEN thenList[1]<br>
* ...<br>
* ELSE elseClause<br>
* END</code></blockquote>
*/
public static SqlCase createSwitched(SqlParserPos pos, SqlNode value, SqlNodeList whenList, SqlNodeList thenList, SqlNode elseClause) {
if (null != value) {
List<SqlNode> list = whenList.getList();
for (int i = 0; i < list.size(); i++) {
SqlNode e = list.get(i);
final SqlCall call;
if (e instanceof SqlNodeList) {
call = SqlStdOperatorTable.IN.createCall(pos, value, e);
} else {
call = SqlStdOperatorTable.EQUALS.createCall(pos, value, e);
}
list.set(i, call);
}
}
if (null == elseClause) {
elseClause = SqlLiteral.createNull(pos);
}
return new SqlCase(pos, null, whenList, thenList, elseClause);
}
use of org.apache.calcite.sql.SqlCall in project calcite by apache.
the class OracleSqlDialect method unparseCall.
@Override
public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
if (call.getOperator() == SqlStdOperatorTable.SUBSTRING) {
SqlUtil.unparseFunctionSyntax(OracleSqlOperatorTable.SUBSTR, writer, call);
} else {
switch(call.getKind()) {
case FLOOR:
if (call.operandCount() != 2) {
super.unparseCall(writer, call, leftPrec, rightPrec);
return;
}
final SqlLiteral timeUnitNode = call.operand(1);
final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class);
SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, timeUnit.name(), timeUnitNode.getParserPosition());
SqlFloorFunction.unparseDatetimeFunction(writer, call2, "TRUNC", true);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
}
}
use of org.apache.calcite.sql.SqlCall in project calcite by apache.
the class HsqldbSqlDialect method unparseCall.
@Override
public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
switch(call.getKind()) {
case FLOOR:
if (call.operandCount() != 2) {
super.unparseCall(writer, call, leftPrec, rightPrec);
return;
}
final SqlLiteral timeUnitNode = call.operand(1);
final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class);
final String translatedLit = convertTimeUnit(timeUnit);
SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, translatedLit, timeUnitNode.getParserPosition());
SqlFloorFunction.unparseDatetimeFunction(writer, call2, "TRUNC", true);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
}
Aggregations