|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.eigenbase.sql.SqlOperator
org.eigenbase.sql.fun.SqlCaseOperator
public class SqlCaseOperator
An operator describing a CASE, NULLIF or
COALESCE expression. All of these forms are normalized at parse time
to a to a simple CASE statement like this:
CASE
WHEN <when expression_0> THEN <then expression_0>
WHEN <when expression_1> THEN <then expression_1>
...
WHEN <when expression_N> THEN <then expression_N>
ELSE <else expression>
END
The switched form of the CASE statement is normalized to the
simple form by inserting calls to the = operator. For example,
becomesCASE x + y WHEN 1 THEN 'fee' WHEN 2 THEN 'fie' ELSE 'foe' END
CASE
WHEN Equals(x + y, 1) THEN 'fee'
WHEN Equals(x + y, 2) THEN 'fie'
ELSE 'foe'
END
REVIEW jhyde 2004/3/19 Does Equals handle NULL semantics
correctly?
COALESCE(x, y, z) becomes
CASE
WHEN x IS NOT NULL THEN x
WHEN y IS NOT NULL THEN y
ELSE z
END
NULLIF(x, -1) becomes
CASE
WHEN x = -1 THEN NULL
ELSE x
END
Note that some of these normalizations cause expressions to be duplicated. This may make it more difficult to write optimizer rules (because the rules will have to deduce that expressions are equivalent). It also requires that some part of the planning process (probably the generator of the calculator program) does common sub-expression elimination.
REVIEW jhyde 2004/3/19. Expanding expressions at parse time has some other
drawbacks. It is more difficult to give meaningful validation errors: given
COALESCE(DATE '2004-03-18', 3.5), do we issue a type-checking
error against a CASE operator? Second, I'd like to use the
SqlNode object model to generate SQL to send to 3rd-party databases,
but there's now no way to represent a call to COALESCE or NULLIF. All in all,
it would be better to have operators for COALESCE, NULLIF, and both simple
and switched forms of CASE, then translate to simple CASE when building the
RexNode tree.
The arguments are physically represented as follows:
SqlNodeList
whenList.SqlNodeList
thenList.SqlNode.
| Field Summary | |
|---|---|
private static SqlWriter.FrameType |
CaseFrameType
|
| Fields inherited from class org.eigenbase.sql.SqlOperator |
|---|
MaxPrec, NL |
| Constructor Summary | |
|---|---|
SqlCaseOperator()
|
|
| Method Summary | |
|---|---|
boolean |
checkOperandTypes(SqlCallBinding callBinding,
boolean throwOnFailure)
Checks that the operand values in a SqlCall to this operator are
valid. |
SqlCall |
createCall(SqlLiteral functionQualifier,
SqlParserPos pos,
SqlNode... operands)
Creates a call to this operand with an array of operands. |
SqlCase |
createSwitchedCall(SqlParserPos pos,
SqlNode caseIdentifier,
SqlNodeList whenList,
SqlNodeList thenList,
SqlNode elseClause)
Creates a call to the switched form of the case operator, viz: CASE caseIdentifier |
RelDataType |
deriveType(SqlValidator validator,
SqlValidatorScope scope,
SqlCall call)
Derives the type of a call to this operator. |
SqlOperandCountRange |
getOperandCountRange()
Returns a constraint on the number of operands expected by this operator. |
SqlSyntax |
getSyntax()
Returns the syntactic type of this operator. |
RelDataType |
inferReturnType(SqlOperatorBinding opBinding)
Infers the return type of an invocation of this operator; only called after the number and types of operands have already been validated. |
private RelDataType |
inferTypeFromOperands(RelDataTypeFactory typeFactory,
RelDataType[] argTypes)
|
private RelDataType |
inferTypeFromValidator(SqlCallBinding callBinding)
|
void |
unparse(SqlWriter writer,
SqlNode[] operands,
int leftPrec,
int rightPrec)
Writes a SQL representation of a call to this operator to a writer, including parentheses if the operators on either side are of greater precedence. |
void |
validateCall(SqlCall call,
SqlValidator validator,
SqlValidatorScope scope,
SqlValidatorScope operandScope)
Validates a call to this operator. |
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
private static final SqlWriter.FrameType CaseFrameType
| Constructor Detail |
|---|
public SqlCaseOperator()
| Method Detail |
|---|
public void validateCall(SqlCall call,
SqlValidator validator,
SqlValidatorScope scope,
SqlValidatorScope operandScope)
SqlOperatorThis method should not perform type-derivation or perform validation
related related to types. That is done later, by SqlOperator.deriveType(SqlValidator, SqlValidatorScope, SqlCall). This method
should focus on structural validation.
A typical implementation of this method first validates the operands, then performs some operator-specific logic. The default implementation just validates the operands.
This method is the default implementation of SqlCall.validate(org.eigenbase.sql.validate.SqlValidator, org.eigenbase.sql.validate.SqlValidatorScope);
but note that some sub-classes of SqlCall never call this method.
validateCall in class SqlOperatorcall - the call to this operatorvalidator - the active validatorscope - validator scopeoperandScope - validator scope in which to validate operands to this
call; usually equal to scope, but not always because some operators
introduce new scopesSqlNode.validateExpr(SqlValidator, SqlValidatorScope),
SqlOperator.deriveType(SqlValidator, SqlValidatorScope, SqlCall)
public RelDataType deriveType(SqlValidator validator,
SqlValidatorScope scope,
SqlCall call)
SqlOperatorThis method is an intrinsic part of the validation process so, unlike
SqlOperator.inferReturnType(org.eigenbase.sql.SqlOperatorBinding), specific operators would not typically override
this method.
deriveType in class SqlOperatorvalidator - Validatorscope - Scope of validationcall - Call to this operator
public boolean checkOperandTypes(SqlCallBinding callBinding,
boolean throwOnFailure)
SqlOperatorSqlCall to this operator are
valid. Subclasses must either override this method or supply an instance
of SqlOperandTypeChecker to the constructor.
checkOperandTypes in class SqlOperatorcallBinding - description of callthrowOnFailure - whether to throw an exception if check fails
(otherwise returns false in that case)
public RelDataType inferReturnType(SqlOperatorBinding opBinding)
SqlOperatorSqlReturnTypeInference to the constructor.
inferReturnType in class SqlOperatoropBinding - description of invocation (not necessarily a SqlCall)
private RelDataType inferTypeFromValidator(SqlCallBinding callBinding)
private RelDataType inferTypeFromOperands(RelDataTypeFactory typeFactory,
RelDataType[] argTypes)
public SqlOperandCountRange getOperandCountRange()
SqlOperatorSqlOperandTypeChecker associated with this
operator.
getOperandCountRange in class SqlOperatorpublic SqlSyntax getSyntax()
SqlOperator
getSyntax in class SqlOperator
public SqlCall createCall(SqlLiteral functionQualifier,
SqlParserPos pos,
SqlNode... operands)
SqlOperatorThe position of the resulting call is the union of the
pos and the positions of all of the operands.
createCall in class SqlOperatorfunctionQualifier - function qualifier (e.g. "DISTINCT"), may bepos - parser position of the identifier of the calloperands - array of operands
public SqlCase createSwitchedCall(SqlParserPos pos,
SqlNode caseIdentifier,
SqlNodeList whenList,
SqlNodeList thenList,
SqlNode elseClause)
CASE caseIdentifier
WHEN whenList[0] THEN thenList[0]
WHEN whenList[1] THEN thenList[1]
...
ELSE elseClause
END
public void unparse(SqlWriter writer,
SqlNode[] operands,
int leftPrec,
int rightPrec)
SqlOperatorThe default implementation of this method delegates to SqlSyntax.unparse(org.eigenbase.sql.SqlWriter, org.eigenbase.sql.SqlOperator, org.eigenbase.sql.SqlNode[], int, int).
unparse in class SqlOperator
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||