/** * Returns whether a node is a literal. * * <p>Many constructs which require literals also accept <code>CAST(NULL AS * <i>type</i>)</code>. This method does not accept casts, so you should * call {@link #isNullLiteral} first. * * @param node The node, never null. * @return Whether the node is a literal */ public static boolean isLiteral(SqlNode node) { return isLiteral(node, false); }
public boolean checkSingleOperandType( SqlCallBinding callBinding, SqlNode node, int iFormalOperand, boolean throwOnFailure) { Util.discard(iFormalOperand); if (SqlUtil.isNullLiteral(node, true)) { if (allowNull) { return true; } if (throwOnFailure) { throw callBinding.newError( RESOURCE.argumentMustNotBeNull( callBinding.getOperator().getName())); } return false; } if (!SqlUtil.isLiteral(node) && !SqlUtil.isLiteralChain(node)) { if (throwOnFailure) { throw callBinding.newError( RESOURCE.argumentMustBeLiteral( callBinding.getOperator().getName())); } return false; } return true; }
/** * Returns whether a node is a literal. * * <p>Many constructs which require literals also accept <code>CAST(NULL AS * <i>type</i>)</code>. This method does not accept casts, so you should * call {@link #isNullLiteral} first. * * @param node The node, never null. * @return Whether the node is a literal */ public static boolean isLiteral(SqlNode node) { return isLiteral(node, false); }
public boolean checkSingleOperandType( SqlCallBinding callBinding, SqlNode node, int iFormalOperand, boolean throwOnFailure) { Util.discard(iFormalOperand); if (SqlUtil.isNullLiteral(node, true)) { if (allowNull) { return true; } if (throwOnFailure) { throw callBinding.newError( RESOURCE.argumentMustNotBeNull( callBinding.getOperator().getName())); } return false; } if (!SqlUtil.isLiteral(node) && !SqlUtil.isLiteralChain(node)) { if (throwOnFailure) { throw callBinding.newError( RESOURCE.argumentMustBeLiteral( callBinding.getOperator().getName())); } return false; } return true; }
@Override public boolean isOperandLiteral(int ordinal, boolean allowCast) { return SqlUtil.isLiteral(call.operand(ordinal), allowCast); }
@Override public boolean isOperandLiteral(int ordinal, boolean allowCast) { return SqlUtil.isLiteral(call.operand(ordinal), allowCast); }
if (node.getKind() == SqlKind.CAST) { SqlCall call = (SqlCall) node; if (isLiteral(call.operand(0), false)) {
if (node.getKind() == SqlKind.CAST) { SqlCall call = (SqlCall) node; if (isLiteral(call.operand(0), false)) {
return null; if (SqlUtil.isLiteral(right)) { return ((SqlLiteral) right).getValue();
return null; if (SqlUtil.isLiteral(right)) { return ((SqlLiteral) right).getValue();
/** Tests {@link org.apache.calcite.sql.SqlUtil#isLiteral(SqlNode, boolean)}, * which was added to enhance Calcite's public API * <a href="https://issues.apache.org/jira/browse/CALCITE-1219">[CALCITE-1219] * Add a method to SqlOperatorBinding to determine whether operand is a * literal</a>. */ @Test public void testSqlNodeLiteral() { final SqlNode literal = SqlLiteral.createExactNumeric( "0", SqlParserPos.ZERO); final SqlNode castLiteral = SqlStdOperatorTable.CAST.createCall( SqlParserPos.ZERO, literal, integerType); final SqlNode castCastLiteral = SqlStdOperatorTable.CAST.createCall( SqlParserPos.ZERO, castLiteral, integerType); // SqlLiteral is considered as a Literal assertSame(true, SqlUtil.isLiteral(literal, true)); // CAST(SqlLiteral as type) is considered as a Literal assertSame(true, SqlUtil.isLiteral(castLiteral, true)); // CAST(CAST(SqlLiteral as type) as type) is NOT considered as a Literal assertSame(false, SqlUtil.isLiteral(castCastLiteral, true)); }
/** Tests {@link org.apache.calcite.sql.SqlUtil#isLiteral(SqlNode, boolean)}, * which was added to enhance Calcite's public API * <a href="https://issues.apache.org/jira/browse/CALCITE-1219">[CALCITE-1219] * Add a method to SqlOperatorBinding to determine whether operand is a * literal</a>. */ @Test public void testSqlNodeLiteral() { final SqlNode literal = SqlLiteral.createExactNumeric( "0", SqlParserPos.ZERO); final SqlNode castLiteral = SqlStdOperatorTable.CAST.createCall( SqlParserPos.ZERO, literal, integerType); final SqlNode castCastLiteral = SqlStdOperatorTable.CAST.createCall( SqlParserPos.ZERO, castLiteral, integerType); // SqlLiteral is considered as a Literal assertSame(true, SqlUtil.isLiteral(literal, true)); // CAST(SqlLiteral as type) is considered as a Literal assertSame(true, SqlUtil.isLiteral(castLiteral, true)); // CAST(CAST(SqlLiteral as type) as type) is NOT considered as a Literal assertSame(false, SqlUtil.isLiteral(castCastLiteral, true)); }