org.apache.calcite.sql.SqlKind Java Examples
The following examples show how to use
org.apache.calcite.sql.SqlKind.
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: RexSimplify.java From calcite with Apache License 2.0 | 6 votes |
private static boolean isUpperBound(final RexNode e) { final List<RexNode> operands; switch (e.getKind()) { case LESS_THAN: case LESS_THAN_OR_EQUAL: operands = ((RexCall) e).getOperands(); return RexUtil.isReferenceOrAccess(operands.get(0), true) && operands.get(1).isA(SqlKind.LITERAL); case GREATER_THAN: case GREATER_THAN_OR_EQUAL: operands = ((RexCall) e).getOperands(); return RexUtil.isReferenceOrAccess(operands.get(1), true) && operands.get(0).isA(SqlKind.LITERAL); default: return false; } }
Example #2
Source File: DateRangeRules.java From Bats with Apache License 2.0 | 6 votes |
private RexNode compareFloorCeil(SqlKind comparison, RexNode operand, RexLiteral timeLiteral, TimeUnitRange timeUnit, boolean floor) { RangeSet<Calendar> rangeSet = operandRanges.get(operand); if (rangeSet == null) { rangeSet = ImmutableRangeSet.<Calendar> of().complement(); } final RangeSet<Calendar> s2 = TreeRangeSet.create(); final Calendar c = timestampValue(timeLiteral); final Range<Calendar> range = floor ? floorRange(timeUnit, comparison, c) : ceilRange(timeUnit, comparison, c); s2.add(range); // Intersect old range set with new. s2.removeAll(rangeSet.complement()); operandRanges.put(operand, ImmutableRangeSet.copyOf(s2)); if (range.isEmpty()) { return rexBuilder.makeLiteral(false); } return toRex(operand, range); }
Example #3
Source File: DateRangeRules.java From Bats with Apache License 2.0 | 6 votes |
private Range<Calendar> extractRange(TimeUnitRange timeUnit, SqlKind comparison, Calendar c) { switch (comparison) { case EQUALS: return Range.closedOpen(round(c, timeUnit, true), round(c, timeUnit, false)); case LESS_THAN: return Range.lessThan(round(c, timeUnit, true)); case LESS_THAN_OR_EQUAL: return Range.lessThan(round(c, timeUnit, false)); case GREATER_THAN: return Range.atLeast(round(c, timeUnit, false)); case GREATER_THAN_OR_EQUAL: return Range.atLeast(round(c, timeUnit, true)); default: throw new AssertionError(comparison); } }
Example #4
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 #5
Source File: RexSimplify.java From calcite with Apache License 2.0 | 6 votes |
private static <C extends Comparable<C>> Range<C> range(SqlKind comparison, C c) { switch (comparison) { case EQUALS: return Range.singleton(c); case LESS_THAN: return Range.lessThan(c); case LESS_THAN_OR_EQUAL: return Range.atMost(c); case GREATER_THAN: return Range.greaterThan(c); case GREATER_THAN_OR_EQUAL: return Range.atLeast(c); default: throw new AssertionError(); } }
Example #6
Source File: PredicateAnalyzer.java From dremio-oss with Apache License 2.0 | 6 votes |
private QueryExpression postfix(RexCall call) { Preconditions.checkArgument(call.getKind() == SqlKind.IS_NULL || call.getKind() == SqlKind.IS_NOT_NULL); if (call.getOperands().size() != 1) { throw new PredicateAnalyzerException(format("Unsupported operator: [%s]", call)); } Expression a = call.getOperands().get(0).accept(this); // Fields cannot be queried without being indexed final NamedFieldExpression fieldExpression = (NamedFieldExpression) a; if (fieldExpression.getAnnotation() != null && fieldExpression.getAnnotation().isNotIndexed()) { throw new PredicateAnalyzerException("Cannot handle " + call.getKind() + " expression for field not indexed, " + call); } // Elasticsearch does not want is null/is not null (exists query) for _id and _index, although it supports for all other metadata column isColumn(a, call, ElasticsearchConstants.ID, true); isColumn(a, call, ElasticsearchConstants.INDEX, true); QueryExpression operand = QueryExpression.create((TerminalExpression)a); return call.getKind() == SqlKind.IS_NOT_NULL ? operand.exists() : operand.notExists(); }
Example #7
Source File: DruidJsonFilter.java From calcite with Apache License 2.0 | 6 votes |
@Nullable private static DruidJsonFilter toIsNullKindDruidFilter(RexNode rexNode, RelDataType rowType, DruidQuery druidQuery) { if (rexNode.getKind() != SqlKind.IS_NULL && rexNode.getKind() != SqlKind.IS_NOT_NULL) { throw new AssertionError( DruidQuery.format("Expecting IS_NULL or IS_NOT_NULL but got [%s]", rexNode.getKind())); } final RexCall rexCall = (RexCall) rexNode; final RexNode refNode = rexCall.getOperands().get(0); Pair<String, ExtractionFunction> druidColumn = DruidQuery .toDruidColumn(refNode, rowType, druidQuery); final String columnName = druidColumn.left; final ExtractionFunction extractionFunction = druidColumn.right; if (columnName == null) { return null; } if (rexNode.getKind() == SqlKind.IS_NOT_NULL) { return toNotDruidFilter(new JsonSelector(columnName, null, extractionFunction)); } return new JsonSelector(columnName, null, extractionFunction); }
Example #8
Source File: RexSimplify.java From calcite with Apache License 2.0 | 6 votes |
private static boolean isLowerBound(final RexNode e) { final List<RexNode> operands; switch (e.getKind()) { case LESS_THAN: case LESS_THAN_OR_EQUAL: operands = ((RexCall) e).getOperands(); return RexUtil.isReferenceOrAccess(operands.get(1), true) && operands.get(0).isA(SqlKind.LITERAL); case GREATER_THAN: case GREATER_THAN_OR_EQUAL: operands = ((RexCall) e).getOperands(); return RexUtil.isReferenceOrAccess(operands.get(0), true) && operands.get(1).isA(SqlKind.LITERAL); default: return false; } }
Example #9
Source File: RexImplicationChecker.java From calcite with Apache License 2.0 | 6 votes |
private boolean isOppositeOp(SqlKind fKind, SqlKind sKind) { switch (sKind) { case GREATER_THAN: case GREATER_THAN_OR_EQUAL: if (!(fKind == SqlKind.LESS_THAN) && !(fKind == SqlKind.LESS_THAN_OR_EQUAL)) { return false; } break; case LESS_THAN: case LESS_THAN_OR_EQUAL: if (!(fKind == SqlKind.GREATER_THAN) && !(fKind == SqlKind.GREATER_THAN_OR_EQUAL)) { return false; } break; default: return false; } return true; }
Example #10
Source File: CalciteSqlCompilerTest.java From incubator-pinot with Apache License 2.0 | 6 votes |
@Test public void testSelectAs() { PinotQuery pinotQuery = CalciteSqlParser.compileToPinotQuery( "select sum(A) as sum_A, count(B) as count_B from vegetables where g IN (12, 13, 15.2, 17)"); Function func = pinotQuery.getFilterExpression().getFunctionCall(); Assert.assertEquals(func.getOperator(), SqlKind.IN.name()); Assert.assertEquals(func.getOperands().get(0).getIdentifier().getName(), "g"); Assert.assertEquals(func.getOperands().get(1).getLiteral().getLongValue(), 12L); Assert.assertEquals(func.getOperands().get(2).getLiteral().getLongValue(), 13L); Assert.assertEquals(func.getOperands().get(3).getLiteral().getDoubleValue(), 15.2); Assert.assertEquals(func.getOperands().get(4).getLiteral().getLongValue(), 17L); PinotQuery2BrokerRequestConverter converter = new PinotQuery2BrokerRequestConverter(); BrokerRequest tempBrokerRequest = converter.convert(pinotQuery); Assert.assertEquals(tempBrokerRequest.getQuerySource().getTableName(), "vegetables"); Assert.assertNull(tempBrokerRequest.getSelections()); Assert.assertEquals(tempBrokerRequest.getAggregationsInfo().get(0).getAggregationType(), "SUM"); Assert.assertEquals(tempBrokerRequest.getAggregationsInfo().get(1).getAggregationType(), "COUNT"); Assert.assertEquals(tempBrokerRequest.getFilterQuery().getColumn(), "g"); Assert.assertEquals(tempBrokerRequest.getFilterQuery().getValue().size(), 4); Assert.assertEquals(tempBrokerRequest.getFilterQuery().getValue().get(0), "12"); Assert.assertEquals(tempBrokerRequest.getFilterQuery().getValue().get(1), "13"); Assert.assertEquals(tempBrokerRequest.getFilterQuery().getValue().get(2), "15.2"); Assert.assertEquals(tempBrokerRequest.getFilterQuery().getValue().get(3), "17"); }
Example #11
Source File: FlinkAggregateRemoveRule.java From flink with Apache License 2.0 | 6 votes |
@Override public boolean matches(RelOptRuleCall call) { final Aggregate aggregate = call.rel(0); final RelNode input = call.rel(1); if (aggregate.getGroupCount() == 0 || aggregate.indicator || aggregate.getGroupType() != Aggregate.Group.SIMPLE) { return false; } for (AggregateCall aggCall : aggregate.getAggCallList()) { SqlKind aggCallKind = aggCall.getAggregation().getKind(); // TODO supports more AggregateCalls boolean isAllowAggCall = aggCallKind == SqlKind.SUM || aggCallKind == SqlKind.MIN || aggCallKind == SqlKind.MAX || aggCall.getAggregation() instanceof SqlAuxiliaryGroupAggFunction; if (!isAllowAggCall || aggCall.filterArg >= 0 || aggCall.getArgList().size() != 1) { return false; } } final RelMetadataQuery mq = call.getMetadataQuery(); return SqlFunctions.isTrue(mq.areColumnsUnique(input, aggregate.getGroupSet())); }
Example #12
Source File: PredicateAnalyzer.java From dremio-oss with Apache License 2.0 | 6 votes |
@Override public RexNode visitCall(RexCall call) { if (call.getOperator().getKind() == SqlKind.NOT) { RexNode child = call.getOperands().get(0); if (child.getKind() == SqlKind.LIKE) { List<RexNode> operands = FluentIterable.from(((RexCall) child).getOperands()).transform(new Function<RexNode, RexNode>() { @Override public RexNode apply(RexNode rexNode) { return rexNode.accept(NotLikeConverter.this); } }).toList(); return rexBuilder.makeCall(SqlStdOperatorTable.NOT_LIKE, operands); } } return super.visitCall(call); }
Example #13
Source File: SqlStdOperatorTable.java From Bats with Apache License 2.0 | 6 votes |
/** Returns the operator for {@code ALL comparisonKind}. */ public static SqlQuantifyOperator all(SqlKind comparisonKind) { switch (comparisonKind) { case EQUALS: return ALL_EQ; case NOT_EQUALS: return ALL_NE; case LESS_THAN: return ALL_LT; case LESS_THAN_OR_EQUAL: return ALL_LE; case GREATER_THAN: return ALL_GT; case GREATER_THAN_OR_EQUAL: return ALL_GE; default: throw new AssertionError(comparisonKind); } }
Example #14
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 #15
Source File: EnumerableTraitsUtils.java From calcite with Apache License 2.0 | 6 votes |
/** * Determine whether there is mapping between project input and output fields. * Bail out if sort relies on non-trivial expressions. */ private static boolean isCollationOnTrivialExpr( List<RexNode> projects, RelDataTypeFactory typeFactory, Mappings.TargetMapping map, RelFieldCollation fc, boolean passDown) { final int index = fc.getFieldIndex(); int target = map.getTargetOpt(index); if (target < 0) { return false; } final RexNode node = passDown ? projects.get(index) : projects.get(target); if (node.isA(SqlKind.CAST)) { // Check whether it is a monotonic preserving cast final RexCall cast = (RexCall) node; RelFieldCollation newFieldCollation = Objects.requireNonNull(RexUtil.apply(map, fc)); final RexCallBinding binding = RexCallBinding.create(typeFactory, cast, ImmutableList.of(RelCollations.of(newFieldCollation))); if (cast.getOperator().getMonotonicity(binding) == SqlMonotonicity.NOT_MONOTONIC) { return false; } } return true; }
Example #16
Source File: SetopNamespace.java From calcite with Apache License 2.0 | 6 votes |
public RelDataType validateImpl(RelDataType targetRowType) { switch (call.getKind()) { case UNION: case INTERSECT: case EXCEPT: final SqlValidatorScope scope = validator.scopes.get(call); for (SqlNode operand : call.getOperandList()) { if (!(operand.isA(SqlKind.QUERY))) { throw validator.newValidationError(operand, RESOURCE.needQueryOp(operand.toString())); } validator.validateQuery(operand, scope, targetRowType); } return call.getOperator().deriveType( validator, scope, call); default: throw new AssertionError("Not a query: " + call.getKind()); } }
Example #17
Source File: SqlToOperationConverter.java From flink with Apache License 2.0 | 6 votes |
/** * This is the main entrance for executing all kinds of DDL/DML {@code SqlNode}s, different * SqlNode will have it's implementation in the #convert(type) method whose 'type' argument * is subclass of {@code SqlNode}. * * @param flinkPlanner FlinkPlannerImpl to convert sql node to rel node * @param sqlNode SqlNode to execute on */ public static Operation convert(FlinkPlannerImpl flinkPlanner, SqlNode sqlNode) { // validate the query final SqlNode validated = flinkPlanner.validate(sqlNode); SqlToOperationConverter converter = new SqlToOperationConverter(flinkPlanner); if (validated instanceof SqlCreateTable) { return converter.convertCreateTable((SqlCreateTable) validated); } if (validated instanceof SqlDropTable) { return converter.convertDropTable((SqlDropTable) validated); } else if (validated instanceof RichSqlInsert) { return converter.convertSqlInsert((RichSqlInsert) validated); } else if (validated.getKind().belongsTo(SqlKind.QUERY)) { return converter.convertSqlQuery(validated); } else { throw new TableException("Unsupported node type " + validated.getClass().getSimpleName()); } }
Example #18
Source File: RexImplicationChecker.java From Bats with Apache License 2.0 | 6 votes |
private boolean isEquivalentOp(SqlKind fKind, SqlKind sKind) { switch (sKind) { case GREATER_THAN: case GREATER_THAN_OR_EQUAL: if (!(fKind == SqlKind.GREATER_THAN) && !(fKind == SqlKind.GREATER_THAN_OR_EQUAL)) { return false; } break; case LESS_THAN: case LESS_THAN_OR_EQUAL: if (!(fKind == SqlKind.LESS_THAN) && !(fKind == SqlKind.LESS_THAN_OR_EQUAL)) { return false; } break; default: return false; } return true; }
Example #19
Source File: RexImplicationChecker.java From calcite with Apache License 2.0 | 6 votes |
private boolean isEquivalentOp(SqlKind fKind, SqlKind sKind) { switch (sKind) { case GREATER_THAN: case GREATER_THAN_OR_EQUAL: if (!(fKind == SqlKind.GREATER_THAN) && !(fKind == SqlKind.GREATER_THAN_OR_EQUAL)) { return false; } break; case LESS_THAN: case LESS_THAN_OR_EQUAL: if (!(fKind == SqlKind.LESS_THAN) && !(fKind == SqlKind.LESS_THAN_OR_EQUAL)) { return false; } break; default: return false; } return true; }
Example #20
Source File: MssqlSqlDialect.java From calcite with Apache License 2.0 | 6 votes |
@Override public void unparseSqlDatetimeArithmetic(SqlWriter writer, SqlCall call, SqlKind sqlKind, int leftPrec, int rightPrec) { final SqlWriter.Frame frame = writer.startFunCall("DATEADD"); SqlNode operand = call.operand(1); if (operand instanceof SqlIntervalLiteral) { //There is no DATESUB method available, so change the sign. unparseSqlIntervalLiteralMssql( writer, (SqlIntervalLiteral) operand, sqlKind == SqlKind.MINUS ? -1 : 1); } else { operand.unparse(writer, leftPrec, rightPrec); } writer.sep(",", true); call.operand(0).unparse(writer, leftPrec, rightPrec); writer.endList(frame); }
Example #21
Source File: FilterableTable.java From kareldb with Apache License 2.0 | 6 votes |
private Comparable[] findKeyFieldAccess(DataContext root, RexNode filter, SqlKind operator) { int[] keyIndices = getKeyIndices(); List<String> keyFields = getRelDef().getKeyFields(); Comparable[] keyValues = new Comparable[keyIndices.length]; for (int i = 0; i < keyIndices.length; i++) { int keyIndex = keyIndices[i]; String keyField = keyFields.get(i); ColumnType type = ColumnType.of(getRowType().getField(keyField, true, false).getType().getSqlTypeName()); List<Comparable> values = scanFilterForKeyField(root, filter, keyIndex, operator, type); if (values.isEmpty()) { return null; } keyValues[i] = values.get(0); } return keyValues; }
Example #22
Source File: GeodeRules.java From calcite with Apache License 2.0 | 6 votes |
/** * Check if the node is a supported predicate (primary field condition). * * @param node Condition node to check * @param fieldNames Names of all columns in the table * @return True if the node represents an equality predicate on a primary key */ private boolean isEqualityOnKey(RexNode node, List<String> fieldNames) { if (isBooleanColumnReference(node, fieldNames)) { return true; } if (!SqlKind.COMPARISON.contains(node.getKind())) { return false; } RexCall call = (RexCall) node; final RexNode left = call.operands.get(0); final RexNode right = call.operands.get(1); if (checkConditionContainsInputRefOrLiterals(left, right, fieldNames)) { return true; } return checkConditionContainsInputRefOrLiterals(right, left, fieldNames); }
Example #23
Source File: RelStructuredTypeFlattener.java From Bats with Apache License 2.0 | 6 votes |
@Override public RexNode visitCall(RexCall rexCall) { if (rexCall.isA(SqlKind.CAST)) { RexNode input = rexCall.getOperands().get(0).accept(this); RelDataType targetType = removeDistinct(rexCall.getType()); return rexBuilder.makeCast(targetType, input); } if (!rexCall.isA(SqlKind.COMPARISON)) { return super.visitCall(rexCall); } RexNode lhs = rexCall.getOperands().get(0); if (!lhs.getType().isStruct()) { // NOTE jvs 9-Mar-2005: Calls like IS NULL operate // on the representative null indicator. Since it comes // first, we don't have to do any special translation. return super.visitCall(rexCall); } // NOTE jvs 22-Mar-2005: Likewise, the null indicator takes // care of comparison null semantics without any special casing. return flattenComparison(rexBuilder, rexCall.getOperator(), rexCall.getOperands()); }
Example #24
Source File: MockSqlOperatorTable.java From calcite with Apache License 2.0 | 5 votes |
public RampFunction() { super("RAMP", SqlKind.OTHER_FUNCTION, null, null, OperandTypes.NUMERIC, SqlFunctionCategory.USER_DEFINED_FUNCTION); }
Example #25
Source File: RexProgramTestBase.java From calcite with Apache License 2.0 | 5 votes |
/** * Asserts that given node has expected string representation with account of node type * @param message extra message that clarifies where the node came from * @param expected expected string representation of the node * @param node node to check */ protected void assertNode(String message, String expected, RexNode node) { String actual; if (node.isA(SqlKind.CAST) || node.isA(SqlKind.NEW_SPECIFICATION)) { // toString contains type (see RexCall.toString) actual = node.toString(); } else { actual = node + ":" + node.getType() + (node.getType().isNullable() ? "" : " NOT NULL"); } assertEquals(expected, actual, message); }
Example #26
Source File: SqlQuantifyOperator.java From calcite with Apache License 2.0 | 5 votes |
/** * Creates a SqlQuantifyOperator. * * @param kind Either ALL or SOME * @param comparisonKind Either <code><</code>, <code>≤</code>, * <code>></code>, <code>≥</code>, * <code>=</code> or <code><></code>. */ SqlQuantifyOperator(SqlKind kind, SqlKind comparisonKind) { super(comparisonKind.sql + " " + kind, kind); this.comparisonKind = Objects.requireNonNull(comparisonKind); Preconditions.checkArgument(comparisonKind == SqlKind.EQUALS || comparisonKind == SqlKind.NOT_EQUALS || comparisonKind == SqlKind.LESS_THAN_OR_EQUAL || comparisonKind == SqlKind.LESS_THAN || comparisonKind == SqlKind.GREATER_THAN_OR_EQUAL || comparisonKind == SqlKind.GREATER_THAN); Preconditions.checkArgument(kind == SqlKind.SOME || kind == SqlKind.ALL); }
Example #27
Source File: SqlLeadLagAggFunction.java From calcite with Apache License 2.0 | 5 votes |
public SqlLeadLagAggFunction(SqlKind kind) { super(kind.name(), null, kind, RETURN_TYPE, null, OPERAND_TYPES, SqlFunctionCategory.NUMERIC, false, true, Optionality.FORBIDDEN); Preconditions.checkArgument(kind == SqlKind.LEAD || kind == SqlKind.LAG); }
Example #28
Source File: SqlUserDefinedAggFunction.java From calcite with Apache License 2.0 | 5 votes |
/** Creates a SqlUserDefinedAggFunction. */ public SqlUserDefinedAggFunction(SqlIdentifier opName, SqlReturnTypeInference returnTypeInference, SqlOperandTypeInference operandTypeInference, SqlOperandTypeChecker operandTypeChecker, AggregateFunction function, boolean requiresOrder, boolean requiresOver, Optionality requiresGroupOrder, RelDataTypeFactory typeFactory) { super(Util.last(opName.names), opName, SqlKind.OTHER_FUNCTION, returnTypeInference, operandTypeInference, operandTypeChecker, SqlFunctionCategory.USER_DEFINED_FUNCTION, requiresOrder, requiresOver, requiresGroupOrder); this.function = function; this.typeFactory = typeFactory; }
Example #29
Source File: SqlLeadLagAggFunction.java From Bats with Apache License 2.0 | 5 votes |
public SqlLeadLagAggFunction(SqlKind kind) { super(kind.name(), null, kind, RETURN_TYPE, null, OPERAND_TYPES, SqlFunctionCategory.NUMERIC, false, true, Optionality.FORBIDDEN); Preconditions.checkArgument(kind == SqlKind.LEAD || kind == SqlKind.LAG); }
Example #30
Source File: SideParser.java From alchemy with Apache License 2.0 | 5 votes |
public static SqlNode[] createEqualNodes(SqlKind sqlKind) { SqlNode[] nodes = new SqlNode[2]; if (SqlKind.AND == sqlKind) { nodes[0] = SqlLiteral.createExactNumeric("1", new SqlParserPos(0, 0)); nodes[1] = SqlLiteral.createExactNumeric("1", new SqlParserPos(0, 0)); } else { nodes[0] = SqlLiteral.createExactNumeric("0", new SqlParserPos(0, 0)); nodes[1] = SqlLiteral.createExactNumeric("1", new SqlParserPos(0, 0)); } return nodes; }