Java Code Examples for org.apache.calcite.sql.SqlKind#CAST
The following examples show how to use
org.apache.calcite.sql.SqlKind#CAST .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReduceExpressionsRule.java From Bats with Apache License 2.0 | 6 votes |
private void addResult(RexNode exp) { // Cast of literal can't be reduced, so skip those (otherwise we'd // go into an infinite loop as we add them back). if (exp.getKind() == SqlKind.CAST) { RexCall cast = (RexCall) exp; RexNode operand = cast.getOperands().get(0); if (operand instanceof RexLiteral) { return; } } constExprs.add(exp); // In the case where the expression corresponds to a UDR argument, // we need to preserve casts. Note that this only applies to // the topmost argument, not expressions nested within the UDR // call. // // REVIEW zfong 6/13/08 - Are there other expressions where we // also need to preserve casts? if (parentCallTypeStack.isEmpty()) { addCasts.add(false); } else { addCasts.add(isUdf(parentCallTypeStack.peek())); } }
Example 2
Source File: ReduceExpressionsRule.java From calcite with Apache License 2.0 | 6 votes |
private void addResult(RexNode exp) { // Cast of literal can't be reduced, so skip those (otherwise we'd // go into an infinite loop as we add them back). if (exp.getKind() == SqlKind.CAST) { RexCall cast = (RexCall) exp; RexNode operand = cast.getOperands().get(0); if (operand instanceof RexLiteral) { return; } } constExprs.add(exp); // In the case where the expression corresponds to a UDR argument, // we need to preserve casts. Note that this only applies to // the topmost argument, not expressions nested within the UDR // call. // // REVIEW zfong 6/13/08 - Are there other expressions where we // also need to preserve casts? if (parentCallTypeStack.isEmpty()) { addCasts.add(false); } else { addCasts.add(isUdf(parentCallTypeStack.peek())); } }
Example 3
Source File: ElasticsearchRules.java From calcite with Apache License 2.0 | 6 votes |
@Override public String visitCall(RexCall call) { final String name = isItemCall(call); if (name != null) { return name; } final List<String> strings = visitList(call.operands); if (call.getKind() == SqlKind.CAST) { return call.getOperands().get(0).accept(this); } if (call.getOperator() == SqlStdOperatorTable.ITEM) { final RexNode op1 = call.getOperands().get(1); if (op1 instanceof RexLiteral && op1.getType().getSqlTypeName() == SqlTypeName.INTEGER) { return stripQuotes(strings.get(0)) + "[" + ((RexLiteral) op1).getValue2() + "]"; } } throw new IllegalArgumentException("Translation of " + call + " is not supported by ElasticsearchProject"); }
Example 4
Source File: DrillMergeProjectRule.java From Bats with Apache License 2.0 | 6 votes |
public static List<RexNode> simplifyCast(List<RexNode> projectExprs) { final List<RexNode> list = new ArrayList<>(); for (RexNode rex: projectExprs) { if (rex.getKind() == SqlKind.CAST) { RexNode operand = ((RexCall) rex).getOperands().get(0); while (operand.getKind() == SqlKind.CAST && operand.getType().equals(rex.getType())) { rex = operand; operand = ((RexCall) rex).getOperands().get(0); } } list.add(rex); } return list; }
Example 5
Source File: DremioRexBuilder.java From dremio-oss with Apache License 2.0 | 6 votes |
@Override public RexNode makeCast( RelDataType type, RexNode exp, boolean matchNullability) { // Special case: bypassing Calcite for interval types if (!(exp instanceof RexLiteral) && SqlTypeUtil.isExactNumeric(type) && SqlTypeUtil.isInterval(exp.getType())) { return makeAbstractCast(type, exp); } RexNode castRexNode = super.makeCast(type, exp, matchNullability); // If we have a CAST(A, TYPE) and A is already of the same TYPE (including nullability), // then return just A. if (castRexNode instanceof RexCall && castRexNode.getKind() == SqlKind.CAST && castRexNode.getType().equals(((RexCall) castRexNode).getOperands().get(0).getType())) { return ((RexCall) castRexNode).getOperands().get(0); } return castRexNode; }
Example 6
Source File: RelOptUtil.java From calcite with Apache License 2.0 | 5 votes |
/** Decomposes the WHERE clause of a view into predicates that constraint * a column to a particular value. * * <p>This method is key to the validation of a modifiable view. Columns that * are constrained to a single value can be omitted from the * SELECT clause of a modifiable view. * * @param projectMap Mapping from column ordinal to the expression that * populate that column, to be populated by this method * @param filters List of remaining filters, to be populated by this method * @param constraint Constraint to be analyzed */ public static void inferViewPredicates(Map<Integer, RexNode> projectMap, List<RexNode> filters, RexNode constraint) { for (RexNode node : conjunctions(constraint)) { switch (node.getKind()) { case EQUALS: final List<RexNode> operands = ((RexCall) node).getOperands(); RexNode o0 = operands.get(0); RexNode o1 = operands.get(1); if (o0 instanceof RexLiteral) { o0 = operands.get(1); o1 = operands.get(0); } if (o0.getKind() == SqlKind.CAST) { o0 = ((RexCall) o0).getOperands().get(0); } if (o0 instanceof RexInputRef && o1 instanceof RexLiteral) { final int index = ((RexInputRef) o0).getIndex(); if (projectMap.get(index) == null) { projectMap.put(index, o1); continue; } } } filters.add(node); } }
Example 7
Source File: GeodeFilter.java From calcite with Apache License 2.0 | 5 votes |
/** * Translate a binary relation. */ private String translateMatch2(RexNode node) { // We currently only use equality, but inequalities on clustering keys // should be possible in the future RexNode child; switch (node.getKind()) { case EQUALS: return translateBinary("=", "=", (RexCall) node); case LESS_THAN: return translateBinary("<", ">", (RexCall) node); case LESS_THAN_OR_EQUAL: return translateBinary("<=", ">=", (RexCall) node); case GREATER_THAN: return translateBinary(">", "<", (RexCall) node); case GREATER_THAN_OR_EQUAL: return translateBinary(">=", "<=", (RexCall) node); case INPUT_REF: return translateBinary2("=", node, rexBuilder.makeLiteral(true)); case NOT: child = ((RexCall) node).getOperands().get(0); if (child.getKind() == SqlKind.CAST) { child = ((RexCall) child).getOperands().get(0); } if (child.getKind() == SqlKind.INPUT_REF) { return translateBinary2("=", child, rexBuilder.makeLiteral(false)); } break; case CAST: return translateMatch2(((RexCall) node).getOperands().get(0)); default: break; } throw new AssertionError("Cannot translate " + node + ", kind=" + node.getKind()); }
Example 8
Source File: SolrRules.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override public String visitCall(RexCall call) { final List<String> strings = visitList(call.operands); if (call.getKind() == SqlKind.CAST) { return strings.get(0); } return super.visitCall(call); }
Example 9
Source File: SqlImplementor.java From dremio-oss with Apache License 2.0 | 5 votes |
/** * Removes cast from string. * * <p>For example, {@code x > CAST('2015-01-07' AS DATE)} * becomes {@code x > '2015-01-07'}. */ private static RexNode stripCastFromString(RexNode node) { switch (node.getKind()) { case EQUALS: case IS_NOT_DISTINCT_FROM: case NOT_EQUALS: case GREATER_THAN: case GREATER_THAN_OR_EQUAL: case LESS_THAN: case LESS_THAN_OR_EQUAL: final RexCall call = (RexCall) node; final RexNode o0 = call.operands.get(0); final RexNode o1 = call.operands.get(1); if (o0.getKind() == SqlKind.CAST && o1.getKind() != SqlKind.CAST) { final RexNode o0b = ((RexCall) o0).getOperands().get(0); switch (o0b.getType().getSqlTypeName()) { case CHAR: case VARCHAR: return call.clone(call.getType(), ImmutableList.of(o0b, o1)); } } if (o1.getKind() == SqlKind.CAST && o0.getKind() != SqlKind.CAST) { final RexNode o1b = ((RexCall) o1).getOperands().get(0); switch (o1b.getType().getSqlTypeName()) { case CHAR: case VARCHAR: return call.clone(call.getType(), ImmutableList.of(o0, o1b)); } } } return node; }
Example 10
Source File: SqlCastFunction.java From calcite with Apache License 2.0 | 5 votes |
public SqlCastFunction() { super("CAST", SqlKind.CAST, null, InferTypes.FIRST_KNOWN, null, SqlFunctionCategory.SYSTEM); }
Example 11
Source File: RexSimplify.java From Bats with Apache License 2.0 | 5 votes |
private RexNode simplifyIsNull(RexNode a) { if (!a.getType().isNullable() && isSafeExpression(a)) { return rexBuilder.makeLiteral(false); } if (RexUtil.isNull(a)) { return rexBuilder.makeLiteral(true); } if (a.getKind() == SqlKind.CAST) { return null; } switch (Strong.policy(a.getKind())) { case NOT_NULL: return rexBuilder.makeLiteral(false); case ANY: // "f" is a strong operator, so "f(operand0, operand1) IS NULL" simplifies // to "operand0 IS NULL OR operand1 IS NULL" final List<RexNode> operands = new ArrayList<>(); for (RexNode operand : ((RexCall) a).getOperands()) { final RexNode simplified = simplifyIsNull(operand); if (simplified == null) { operands.add(rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, operand)); } else { operands.add(simplified); } } return RexUtil.composeDisjunction(rexBuilder, operands, false); case AS_IS: default: return null; } }
Example 12
Source File: RexSimplify.java From Bats with Apache License 2.0 | 5 votes |
private RexNode simplifyIsNotNull(RexNode a) { if (!a.getType().isNullable() && isSafeExpression(a)) { return rexBuilder.makeLiteral(true); } if (predicates.pulledUpPredicates.contains(a)) { return rexBuilder.makeLiteral(true); } if (a.getKind() == SqlKind.CAST) { return null; } switch (Strong.policy(a.getKind())) { case NOT_NULL: return rexBuilder.makeLiteral(true); case ANY: // "f" is a strong operator, so "f(operand0, operand1) IS NOT NULL" // simplifies to "operand0 IS NOT NULL AND operand1 IS NOT NULL" final List<RexNode> operands = new ArrayList<>(); for (RexNode operand : ((RexCall) a).getOperands()) { final RexNode simplified = simplifyIsNotNull(operand); if (simplified == null) { operands.add(rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, operand)); } else if (simplified.isAlwaysFalse()) { return rexBuilder.makeLiteral(false); } else { operands.add(simplified); } } return RexUtil.composeConjunction(rexBuilder, operands); case CUSTOM: switch (a.getKind()) { case LITERAL: return rexBuilder.makeLiteral(!((RexLiteral) a).isNull()); default: throw new AssertionError("every CUSTOM policy needs a handler, " + a.getKind()); } case AS_IS: default: return null; } }
Example 13
Source File: RexToTestCodeShuttle.java From calcite with Apache License 2.0 | 5 votes |
@Override public String visitCall(RexCall call) { SqlOperator operator = call.getOperator(); String method = OP_METHODS.get(operator); StringBuilder sb = new StringBuilder(); if (method != null) { sb.append(method); sb.append('('); } else { sb.append("rexBuilder.makeCall("); sb.append("SqlStdOperatorTable."); sb.append(operator.getName().replace(' ', '_')); sb.append(", "); } List<RexNode> operands = call.getOperands(); for (int i = 0; i < operands.size(); i++) { RexNode operand = operands.get(i); if (i > 0) { sb.append(", "); } sb.append(operand.accept(this)); } if (operator.kind == SqlKind.CAST) { sb.append(", t"); appendSqlType(sb, call.getType()); sb.append('('); if (call.getType().isNullable()) { sb.append("true"); } sb.append(')'); } sb.append(')'); return sb.toString(); }
Example 14
Source File: SqlCastFunction.java From Bats with Apache License 2.0 | 5 votes |
public SqlCastFunction() { super( "CAST", SqlKind.CAST, null, InferTypes.FIRST_KNOWN, null, SqlFunctionCategory.SYSTEM); }
Example 15
Source File: RelDecorrelator.java From flink with Apache License 2.0 | 4 votes |
@Override public RexNode visitCall(final RexCall call) { RexNode newCall; boolean[] update = {false}; List<RexNode> clonedOperands = visitList(call.operands, update); if (update[0]) { SqlOperator operator = call.getOperator(); boolean isSpecialCast = false; if (operator instanceof SqlFunction) { SqlFunction function = (SqlFunction) operator; if (function.getKind() == SqlKind.CAST) { if (call.operands.size() < 2) { isSpecialCast = true; } } } final RelDataType newType; if (!isSpecialCast) { // TODO: ideally this only needs to be called if the result // type will also change. However, since that requires // support from type inference rules to tell whether a rule // decides return type based on input types, for now all // operators will be recreated with new type if any operand // changed, unless the operator has "built-in" type. newType = rexBuilder.deriveReturnType(operator, clonedOperands); } else { // Use the current return type when creating a new call, for // operators with return type built into the operator // definition, and with no type inference rules, such as // cast function with less than 2 operands. // TODO: Comments in RexShuttle.visitCall() mention other // types in this category. Need to resolve those together // and preferably in the base class RexShuttle. newType = call.getType(); } newCall = rexBuilder.makeCall( newType, operator, clonedOperands); } else { newCall = call; } if (projectPulledAboveLeftCorrelator && (nullIndicator != null)) { return createCaseExpression( nullIndicator, rexBuilder.constantNull(), newCall); } return newCall; }
Example 16
Source File: DremioRelToSqlConverter.java From dremio-oss with Apache License 2.0 | 4 votes |
/** * Process the Result from visiting a Project node's child. * * @param project The project node. * @param childResult The Result from calling visit() on the child of project. * @return The Result of visiting the Project node itself. */ protected DremioRelToSqlConverter.Result processProjectChild(Project project, DremioRelToSqlConverter.Result childResult) { // unlike the super impl #visit(Project), this expands star ("*") if additional push downs are enabled Pointer<Boolean> isPartial = new Pointer<>(false); project.accept(new StatelessRelShuttleImpl() { @Override public RelNode visit(TableScan scan) { if (scan instanceof ScanRelBase) { ScanRelBase tableScan = (ScanRelBase) scan; if (tableScan.getTableMetadata().getSchema().getFieldCount() != tableScan.getProjectedColumns().size()) { isPartial.value = true; } } return scan; } }); PlannerSettings plannerSettings = PrelUtil.getPlannerSettings(project.getCluster()); if (plannerSettings != null && !plannerSettings.getOptions().getOption(PlannerSettings.JDBC_PUSH_DOWN_PLUS) && !isPartial.value && isStar(project.getChildExps(), project.getInput().getRowType(), project.getRowType())) { return childResult; } final DremioRelToSqlConverter.Builder builder = childResult.builder(project, SqlImplementor.Clause.SELECT); final List<SqlNode> selectList = new ArrayList<>(); for (RexNode ref : project.getChildExps()) { SqlNode sqlExpr = builder.context.toSql(null, simplifyDatetimePlus(ref, project.getCluster().getRexBuilder())); if ((getDialect().shouldInjectNumericCastToProject() && isDecimal(ref.getType())) || (getDialect().shouldInjectApproxNumericCastToProject() && isApproximateNumeric(ref.getType()))) { if (!((sqlExpr.getKind() == SqlKind.CAST) || (sqlExpr.getKind() == SqlKind.AS && ((SqlBasicCall) sqlExpr).operand(0).getKind() == SqlKind.CAST))) { // Add an explicit cast around this projection. sqlExpr = SqlStdOperatorTable.CAST.createCall(POS, sqlExpr, getDialect().getCastSpec(ref.getType())); } } addSelect(selectList, sqlExpr, project.getRowType()); } builder.setSelect(new SqlNodeList(selectList, POS)); return builder.result(); }
Example 17
Source File: RelDecorrelator.java From calcite with Apache License 2.0 | 4 votes |
@Override public RexNode visitCall(final RexCall call) { RexNode newCall; boolean[] update = {false}; List<RexNode> clonedOperands = visitList(call.operands, update); if (update[0]) { SqlOperator operator = call.getOperator(); boolean isSpecialCast = false; if (operator instanceof SqlFunction) { SqlFunction function = (SqlFunction) operator; if (function.getKind() == SqlKind.CAST) { if (call.operands.size() < 2) { isSpecialCast = true; } } } final RelDataType newType; if (!isSpecialCast) { // TODO: ideally this only needs to be called if the result // type will also change. However, since that requires // support from type inference rules to tell whether a rule // decides return type based on input types, for now all // operators will be recreated with new type if any operand // changed, unless the operator has "built-in" type. newType = rexBuilder.deriveReturnType(operator, clonedOperands); } else { // Use the current return type when creating a new call, for // operators with return type built into the operator // definition, and with no type inference rules, such as // cast function with less than 2 operands. // TODO: Comments in RexShuttle.visitCall() mention other // types in this category. Need to resolve those together // and preferably in the base class RexShuttle. newType = call.getType(); } newCall = rexBuilder.makeCall( newType, operator, clonedOperands); } else { newCall = call; } if (projectPulledAboveLeftCorrelator && (nullIndicator != null)) { return createCaseExpression(nullIndicator, null, newCall); } return newCall; }
Example 18
Source File: MongoRules.java From calcite with Apache License 2.0 | 4 votes |
@Override public String visitCall(RexCall call) { String name = isItem(call); if (name != null) { return "'$" + name + "'"; } final List<String> strings = visitList(call.operands); if (call.getKind() == SqlKind.CAST) { return strings.get(0); } String stdOperator = MONGO_OPERATORS.get(call.getOperator()); if (stdOperator != null) { return "{" + stdOperator + ": [" + Util.commaList(strings) + "]}"; } if (call.getOperator() == SqlStdOperatorTable.ITEM) { final RexNode op1 = call.operands.get(1); if (op1 instanceof RexLiteral && op1.getType().getSqlTypeName() == SqlTypeName.INTEGER) { if (!Bug.CALCITE_194_FIXED) { return "'" + stripQuotes(strings.get(0)) + "[" + ((RexLiteral) op1).getValue2() + "]'"; } return strings.get(0) + "[" + strings.get(1) + "]"; } } if (call.getOperator() == SqlStdOperatorTable.CASE) { StringBuilder sb = new StringBuilder(); StringBuilder finish = new StringBuilder(); // case(a, b, c) -> $cond:[a, b, c] // case(a, b, c, d) -> $cond:[a, b, $cond:[c, d, null]] // case(a, b, c, d, e) -> $cond:[a, b, $cond:[c, d, e]] for (int i = 0; i < strings.size(); i += 2) { sb.append("{$cond:["); finish.append("]}"); sb.append(strings.get(i)); sb.append(','); sb.append(strings.get(i + 1)); sb.append(','); if (i == strings.size() - 3) { sb.append(strings.get(i + 2)); break; } if (i == strings.size() - 2) { sb.append("null"); break; } } sb.append(finish); return sb.toString(); } throw new IllegalArgumentException("Translation of " + call.toString() + " is not supported by MongoProject"); }
Example 19
Source File: RelDecorrelator.java From Bats with Apache License 2.0 | 4 votes |
@Override public RexNode visitCall(final RexCall call) { RexNode newCall; boolean[] update = { false }; List<RexNode> clonedOperands = visitList(call.getOperands(), update); if (update[0]) { SqlOperator operator = call.getOperator(); boolean isSpecialCast = false; if (operator instanceof SqlFunction) { SqlFunction function = (SqlFunction) operator; if (function.getKind() == SqlKind.CAST) { if (call.getOperands().size() < 2) { isSpecialCast = true; } } } final RelDataType newType; if (!isSpecialCast) { // TODO: ideally this only needs to be called if the result // type will also change. However, since that requires // support from type inference rules to tell whether a rule // decides return type based on input types, for now all // operators will be recreated with new type if any operand // changed, unless the operator has "built-in" type. newType = rexBuilder.deriveReturnType(operator, clonedOperands); } else { // Use the current return type when creating a new call, for // operators with return type built into the operator // definition, and with no type inference rules, such as // cast function with less than 2 operands. // TODO: Comments in RexShuttle.visitCall() mention other // types in this category. Need to resolve those together // and preferably in the base class RexShuttle. newType = call.getType(); } newCall = rexBuilder.makeCall(newType, operator, clonedOperands); } else { newCall = call; } if (projectPulledAboveLeftCorrelator && (nullIndicator != null)) { return createCaseExpression(nullIndicator, rexBuilder.constantNull(), newCall); } return newCall; }
Example 20
Source File: SqlCastOperator.java From calcite with Apache License 2.0 | 4 votes |
SqlCastOperator() { super("::", SqlKind.CAST, 94, true, null, InferTypes.FIRST_KNOWN, null); }