Java Code Examples for org.apache.calcite.sql.SqlLiteral#createExactNumeric()
The following examples show how to use
org.apache.calcite.sql.SqlLiteral#createExactNumeric() .
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: SqlOperatorBindingTest.java From calcite with Apache License 2.0 | 6 votes |
/** 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 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)); }
Example 2
Source File: DrillCompoundIdentifier.java From Bats with Apache License 2.0 | 5 votes |
public SqlNode getNode(SqlNode node) { SqlLiteral literal; if (isArray) { literal = SqlLiteral.createExactNumeric(value, parserPos); } else { literal = SqlLiteral.createCharString(value, parserPos); } return new SqlBasicCall(SqlStdOperatorTable.ITEM, new SqlNode[]{node, literal}, parserPos); }
Example 3
Source File: DrillSqlWorker.java From Bats with Apache License 2.0 | 5 votes |
private static SqlNode wrapWithAutoLimit(SqlNode sqlNode, int queryMaxRows) { SqlNumericLiteral autoLimitLiteral = SqlLiteral.createExactNumeric(String.valueOf(queryMaxRows), SqlParserPos.ZERO); if (sqlNode.getKind() == SqlKind.ORDER_BY) { SqlOrderBy orderBy = (SqlOrderBy) sqlNode; return new SqlOrderBy(orderBy.getParserPosition(), orderBy.query, orderBy.orderList, orderBy.offset, autoLimitLiteral); } return new SqlOrderBy(SqlParserPos.ZERO, sqlNode, SqlNodeList.EMPTY, null, autoLimitLiteral); }
Example 4
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; }
Example 5
Source File: ConvSqlWriter.java From kylin-on-parquet-v2 with Apache License 2.0 | 5 votes |
private void doWriteFetchNext(SqlNode fetch, SqlNode offset) { if (offset == null && !configurer.allowNoOffset()) offset = SqlLiteral.createExactNumeric("0", SqlParserPos.ZERO); if (fetch != null && !configurer.allowNoOrderByWithFetch() && lastFrame != null && lastFrame.getFrameType() != FrameTypeEnum.ORDER_BY_LIST) { // MSSQL requires ORDER_BY list for FETCH clause, so must append one here. DUMMY_ORDER_BY_NODE.unparse(this, 0, 0); } if (offset != null) { this.newlineAndIndent(); final Frame offsetFrame = this.startList(FrameTypeEnum.OFFSET); this.keyword("OFFSET"); offset.unparse(this, -1, -1); this.keyword("ROWS"); this.endList(offsetFrame); } if (fetch != null) { if (!configurer.allowFetchNoRows() && fetch instanceof SqlNumericLiteral) if (((SqlNumericLiteral) fetch).toValue().equals("0")) fetch = SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO); this.newlineAndIndent(); final Frame fetchFrame = this.startList(FrameTypeEnum.FETCH); this.keyword("FETCH"); this.keyword("NEXT"); fetch.unparse(this, -1, -1); this.keyword("ROWS"); this.keyword("ONLY"); this.endList(fetchFrame); } }
Example 6
Source File: CompoundIdentifier.java From dremio-oss with Apache License 2.0 | 5 votes |
public SqlNode getNode(SqlNode node){ SqlLiteral literal; if(isArray){ literal = SqlLiteral.createExactNumeric(value, parserPos); }else{ literal = SqlLiteral.createCharString(value, parserPos); } return new SqlBasicCall(SqlStdOperatorTable.ITEM, new SqlNode[]{ node, literal }, parserPos); }
Example 7
Source File: ConvSqlWriter.java From kylin with Apache License 2.0 | 5 votes |
private void doWriteFetchNext(SqlNode fetch, SqlNode offset) { if (offset == null && !configurer.allowNoOffset()) offset = SqlLiteral.createExactNumeric("0", SqlParserPos.ZERO); if (fetch != null && !configurer.allowNoOrderByWithFetch() && lastFrame != null && lastFrame.getFrameType() != FrameTypeEnum.ORDER_BY_LIST) { // MSSQL requires ORDER_BY list for FETCH clause, so must append one here. DUMMY_ORDER_BY_NODE.unparse(this, 0, 0); } if (offset != null) { this.newlineAndIndent(); final Frame offsetFrame = this.startList(FrameTypeEnum.OFFSET); this.keyword("OFFSET"); offset.unparse(this, -1, -1); this.keyword("ROWS"); this.endList(offsetFrame); } if (fetch != null) { if (!configurer.allowFetchNoRows() && fetch instanceof SqlNumericLiteral) if (((SqlNumericLiteral) fetch).toValue().equals("0")) fetch = SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO); this.newlineAndIndent(); final Frame fetchFrame = this.startList(FrameTypeEnum.FETCH); this.keyword("FETCH"); this.keyword("NEXT"); fetch.unparse(this, -1, -1); this.keyword("ROWS"); this.keyword("ONLY"); this.endList(fetchFrame); } }
Example 8
Source File: MergeTableLikeUtilTest.java From flink with Apache License 2.0 | 5 votes |
private SqlNode plus(String column, String value) { return new SqlBasicCall( SqlStdOperatorTable.PLUS, new SqlNode[]{ identifier(column), SqlLiteral.createExactNumeric(value, SqlParserPos.ZERO)}, SqlParserPos.ZERO ); }
Example 9
Source File: MssqlSqlDialect.java From calcite with Apache License 2.0 | 5 votes |
/** {@inheritDoc} * * <p>MSSQL does not support NULLS FIRST, so we emulate using CASE * expressions. For example, * * <blockquote>{@code ORDER BY x NULLS FIRST}</blockquote> * * <p>becomes * * <blockquote> * {@code ORDER BY CASE WHEN x IS NULL THEN 0 ELSE 1 END, x} * </blockquote> */ @Override public SqlNode emulateNullDirection(SqlNode node, boolean nullsFirst, boolean desc) { // Default ordering preserved if (nullCollation.isDefaultOrder(nullsFirst, desc)) { return null; } // Grouping node should preserve grouping, no emulation needed if (node.getKind() == SqlKind.GROUPING) { return node; } // Emulate nulls first/last with case ordering final SqlParserPos pos = SqlParserPos.ZERO; final SqlNodeList whenList = SqlNodeList.of(SqlStdOperatorTable.IS_NULL.createCall(pos, node)); final SqlNode oneLiteral = SqlLiteral.createExactNumeric("1", pos); final SqlNode zeroLiteral = SqlLiteral.createExactNumeric("0", pos); if (nullsFirst) { // IS NULL THEN 0 ELSE 1 END return SqlStdOperatorTable.CASE.createCall(null, pos, null, whenList, SqlNodeList.of(zeroLiteral), oneLiteral); } else { // IS NULL THEN 1 ELSE 0 END return SqlStdOperatorTable.CASE.createCall(null, pos, null, whenList, SqlNodeList.of(oneLiteral), zeroLiteral); } }
Example 10
Source File: RexToSqlNodeConverterImpl.java From Bats with Apache License 2.0 | 4 votes |
public SqlNode convertLiteral(RexLiteral literal) { // Numeric if (SqlTypeFamily.EXACT_NUMERIC.getTypeNames().contains( literal.getTypeName())) { return SqlLiteral.createExactNumeric( literal.getValue().toString(), SqlParserPos.ZERO); } if (SqlTypeFamily.APPROXIMATE_NUMERIC.getTypeNames().contains( literal.getTypeName())) { return SqlLiteral.createApproxNumeric( literal.getValue().toString(), SqlParserPos.ZERO); } // Timestamp if (SqlTypeFamily.TIMESTAMP.getTypeNames().contains( literal.getTypeName())) { return SqlLiteral.createTimestamp( literal.getValueAs(TimestampString.class), 0, SqlParserPos.ZERO); } // Date if (SqlTypeFamily.DATE.getTypeNames().contains( literal.getTypeName())) { return SqlLiteral.createDate( literal.getValueAs(DateString.class), SqlParserPos.ZERO); } // Time if (SqlTypeFamily.TIME.getTypeNames().contains( literal.getTypeName())) { return SqlLiteral.createTime( literal.getValueAs(TimeString.class), 0, SqlParserPos.ZERO); } // String if (SqlTypeFamily.CHARACTER.getTypeNames().contains( literal.getTypeName())) { return SqlLiteral.createCharString( ((NlsString) (literal.getValue())).getValue(), SqlParserPos.ZERO); } // Boolean if (SqlTypeFamily.BOOLEAN.getTypeNames().contains( literal.getTypeName())) { return SqlLiteral.createBoolean( (Boolean) literal.getValue(), SqlParserPos.ZERO); } // Null if (SqlTypeFamily.NULL == literal.getTypeName().getFamily()) { return SqlLiteral.createNull(SqlParserPos.ZERO); } return null; }
Example 11
Source File: DrillAvgVarianceConvertlet.java From Bats with Apache License 2.0 | 4 votes |
private SqlNode expandVariance( final SqlNode arg, boolean biased, boolean sqrt) { /* stddev_pop(x) ==> * power( * (sum(x * x) - sum(x) * sum(x) / count(x)) * / count(x), * .5) * stddev_samp(x) ==> * power( * (sum(x * x) - sum(x) * sum(x) / count(x)) * / (count(x) - 1), * .5) * var_pop(x) ==> * (sum(x * x) - sum(x) * sum(x) / count(x)) * / count(x) * var_samp(x) ==> * (sum(x * x) - sum(x) * sum(x) / count(x)) * / (count(x) - 1) */ final SqlParserPos pos = SqlParserPos.ZERO; // cast the argument to double final SqlNode castHighArg = CastHighOp.createCall(pos, arg); final SqlNode argSquared = SqlStdOperatorTable.MULTIPLY.createCall(pos, castHighArg, castHighArg); final SqlNode sumArgSquared = DrillCalciteSqlAggFunctionWrapper.SUM.createCall(pos, argSquared); final SqlNode sum = DrillCalciteSqlAggFunctionWrapper.SUM.createCall(pos, castHighArg); final SqlNode sumSquared = SqlStdOperatorTable.MULTIPLY.createCall(pos, sum, sum); final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, castHighArg); final SqlNode avgSumSquared = SqlStdOperatorTable.DIVIDE.createCall( pos, sumSquared, count); final SqlNode diff = SqlStdOperatorTable.MINUS.createCall( pos, sumArgSquared, avgSumSquared); final SqlNode denominator; if (biased) { denominator = count; } else { final SqlNumericLiteral one = SqlLiteral.createExactNumeric("1", pos); denominator = SqlStdOperatorTable.MINUS.createCall( pos, count, one); } final SqlNode diffAsDouble = CastHighOp.createCall(pos, diff); final SqlNode div = SqlStdOperatorTable.DIVIDE.createCall( pos, diffAsDouble, denominator); SqlNode result = div; if (sqrt) { final SqlNumericLiteral half = SqlLiteral.createExactNumeric("0.5", pos); result = SqlStdOperatorTable.POWER.createCall(pos, div, half); } return result; }
Example 12
Source File: RexNodeConverter.java From flink with Apache License 2.0 | 4 votes |
private RexWindowBound createBound(Expression bound, SqlKind sqlKind) { if (bound instanceof CallExpression) { CallExpression callExpr = (CallExpression) bound; FunctionDefinition func = callExpr.getFunctionDefinition(); if (BuiltInFunctionDefinitions.UNBOUNDED_ROW.equals(func) || BuiltInFunctionDefinitions.UNBOUNDED_RANGE .equals(func)) { SqlNode unbounded = sqlKind.equals(SqlKind.PRECEDING) ? SqlWindow .createUnboundedPreceding(SqlParserPos.ZERO) : SqlWindow.createUnboundedFollowing(SqlParserPos.ZERO); return RexWindowBound.create(unbounded, null); } else if (BuiltInFunctionDefinitions.CURRENT_ROW.equals(func) || BuiltInFunctionDefinitions.CURRENT_RANGE .equals(func)) { SqlNode currentRow = SqlWindow.createCurrentRow(SqlParserPos.ZERO); return RexWindowBound.create(currentRow, null); } else { throw new IllegalArgumentException("Unexpected expression: " + bound); } } else if (bound instanceof ValueLiteralExpression) { RelDataType returnType = typeFactory .createFieldTypeFromLogicalType(new DecimalType(true, 19, 0)); SqlOperator sqlOperator = new SqlPostfixOperator( sqlKind.name(), sqlKind, 2, new OrdinalReturnTypeInference(0), null, null); SqlNode[] operands = new SqlNode[] { SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO) }; SqlNode node = new SqlBasicCall(sqlOperator, operands, SqlParserPos.ZERO); ValueLiteralExpression literalExpr = (ValueLiteralExpression) bound; RexNode literalRexNode = literalExpr.getValueAs(Double.class).map( v -> relBuilder.literal(BigDecimal.valueOf((Double) v))).orElse( relBuilder.literal(extractValue(literalExpr, Object.class))); List<RexNode> expressions = new ArrayList<>(); expressions.add(literalRexNode); RexNode rexNode = relBuilder.getRexBuilder().makeCall(returnType, sqlOperator, expressions); return RexWindowBound.create(node, rexNode); } else { throw new TableException("Unexpected expression: " + bound); } }
Example 13
Source File: OverConvertRule.java From flink with Apache License 2.0 | 4 votes |
private RexWindowBound createBound(ConvertContext context, Expression bound, SqlKind sqlKind) { if (bound instanceof CallExpression) { CallExpression callExpr = (CallExpression) bound; FunctionDefinition func = callExpr.getFunctionDefinition(); if (BuiltInFunctionDefinitions.UNBOUNDED_ROW.equals(func) || BuiltInFunctionDefinitions.UNBOUNDED_RANGE .equals(func)) { SqlNode unbounded = sqlKind.equals(SqlKind.PRECEDING) ? SqlWindow .createUnboundedPreceding(SqlParserPos.ZERO) : SqlWindow.createUnboundedFollowing(SqlParserPos.ZERO); return RexWindowBound.create(unbounded, null); } else if (BuiltInFunctionDefinitions.CURRENT_ROW.equals(func) || BuiltInFunctionDefinitions.CURRENT_RANGE .equals(func)) { SqlNode currentRow = SqlWindow.createCurrentRow(SqlParserPos.ZERO); return RexWindowBound.create(currentRow, null); } else { throw new IllegalArgumentException("Unexpected expression: " + bound); } } else if (bound instanceof ValueLiteralExpression) { RelDataType returnType = context.getTypeFactory() .createFieldTypeFromLogicalType(new DecimalType(true, 19, 0)); SqlOperator sqlOperator = new SqlPostfixOperator( sqlKind.name(), sqlKind, 2, new OrdinalReturnTypeInference(0), null, null); SqlNode[] operands = new SqlNode[] { SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO) }; SqlNode node = new SqlBasicCall(sqlOperator, operands, SqlParserPos.ZERO); ValueLiteralExpression literalExpr = (ValueLiteralExpression) bound; RexNode literalRexNode = literalExpr.getValueAs(BigDecimal.class) .map(v -> context.getRelBuilder().literal(v)) .orElse(context.getRelBuilder().literal(extractValue(literalExpr, Object.class))); List<RexNode> expressions = new ArrayList<>(); expressions.add(literalRexNode); RexNode rexNode = context.getRelBuilder().getRexBuilder().makeCall( returnType, sqlOperator, expressions); return RexWindowBound.create(node, rexNode); } else { throw new TableException("Unexpected expression: " + bound); } }
Example 14
Source File: SqlImplementor.java From calcite with Apache License 2.0 | 4 votes |
/** Once you have a Result of implementing a child relational expression, * call this method to create a Builder to implement the current relational * expression by adding additional clauses to the SQL query. * * <p>You need to declare which clauses you intend to add. If the clauses * are "later", you can add to the same query. For example, "GROUP BY" comes * after "WHERE". But if they are the same or earlier, this method will * start a new SELECT that wraps the previous result. * * <p>When you have called * {@link Builder#setSelect(SqlNodeList)}, * {@link Builder#setWhere(SqlNode)} etc. call * {@link Builder#result(SqlNode, Collection, RelNode, Map)} * to fix the new query. * * @param rel Relational expression being implemented * @param clauses Clauses that will be generated to implement current * relational expression * @return A builder */ public Builder builder(RelNode rel, Clause... clauses) { final boolean needNew = needNewSubQuery(rel, clauses); SqlSelect select; Expressions.FluentList<Clause> clauseList = Expressions.list(); if (needNew) { select = subSelect(); } else { select = asSelect(); clauseList.addAll(this.clauses); } clauseList.appendAll(clauses); final Context newContext; Map<String, RelDataType> newAliases = null; final SqlNodeList selectList = select.getSelectList(); if (selectList != null) { newContext = new Context(dialect, selectList.size()) { public SqlNode field(int ordinal) { final SqlNode selectItem = selectList.get(ordinal); switch (selectItem.getKind()) { case AS: return ((SqlCall) selectItem).operand(0); } return selectItem; } @Override public SqlNode orderField(int ordinal) { // If the field expression is an unqualified column identifier // and matches a different alias, use an ordinal. // For example, given // SELECT deptno AS empno, empno AS x FROM emp ORDER BY emp.empno // we generate // SELECT deptno AS empno, empno AS x FROM emp ORDER BY 2 // "ORDER BY empno" would give incorrect result; // "ORDER BY x" is acceptable but is not preferred. final SqlNode node = field(ordinal); if (node instanceof SqlIdentifier && ((SqlIdentifier) node).isSimple()) { final String name = ((SqlIdentifier) node).getSimple(); for (Ord<SqlNode> selectItem : Ord.zip(selectList)) { if (selectItem.i != ordinal) { final String alias = SqlValidatorUtil.getAlias(selectItem.e, -1); if (name.equalsIgnoreCase(alias)) { return SqlLiteral.createExactNumeric( Integer.toString(ordinal + 1), SqlParserPos.ZERO); } } } } return node; } }; } else { boolean qualified = !dialect.hasImplicitTableAlias() || aliases.size() > 1; // basically, we did a subSelect() since needNew is set and neededAlias is not null // now, we need to make sure that we need to update the alias context. // if our aliases map has a single element: <neededAlias, rowType>, // then we don't need to rewrite the alias but otherwise, it should be updated. if (needNew && neededAlias != null && (aliases.size() != 1 || !aliases.containsKey(neededAlias))) { newAliases = ImmutableMap.of(neededAlias, rel.getInput(0).getRowType()); newContext = aliasContext(newAliases, qualified); } else { newContext = aliasContext(aliases, qualified); } } return new Builder(rel, clauseList, select, newContext, isAnon(), needNew && !aliases.containsKey(neededAlias) ? newAliases : aliases); }
Example 15
Source File: RexToSqlNodeConverterImpl.java From calcite with Apache License 2.0 | 4 votes |
public SqlNode convertLiteral(RexLiteral literal) { // Numeric if (SqlTypeFamily.EXACT_NUMERIC.getTypeNames().contains( literal.getTypeName())) { return SqlLiteral.createExactNumeric( literal.getValue().toString(), SqlParserPos.ZERO); } if (SqlTypeFamily.APPROXIMATE_NUMERIC.getTypeNames().contains( literal.getTypeName())) { return SqlLiteral.createApproxNumeric( literal.getValue().toString(), SqlParserPos.ZERO); } // Timestamp if (SqlTypeFamily.TIMESTAMP.getTypeNames().contains( literal.getTypeName())) { return SqlLiteral.createTimestamp( literal.getValueAs(TimestampString.class), 0, SqlParserPos.ZERO); } // Date if (SqlTypeFamily.DATE.getTypeNames().contains( literal.getTypeName())) { return SqlLiteral.createDate( literal.getValueAs(DateString.class), SqlParserPos.ZERO); } // Time if (SqlTypeFamily.TIME.getTypeNames().contains( literal.getTypeName())) { return SqlLiteral.createTime( literal.getValueAs(TimeString.class), 0, SqlParserPos.ZERO); } // String if (SqlTypeFamily.CHARACTER.getTypeNames().contains( literal.getTypeName())) { return SqlLiteral.createCharString( ((NlsString) (literal.getValue())).getValue(), SqlParserPos.ZERO); } // Boolean if (SqlTypeFamily.BOOLEAN.getTypeNames().contains( literal.getTypeName())) { return SqlLiteral.createBoolean( (Boolean) literal.getValue(), SqlParserPos.ZERO); } // Null if (SqlTypeFamily.NULL == literal.getTypeName().getFamily()) { return SqlLiteral.createNull(SqlParserPos.ZERO); } return null; }