public String convertSql(String orig) { // for jdbc source, convert quote from backtick to double quote String converted = orig.replaceAll("`", "\""); if (!configurer.skipHandleDefault()) { String escapedDefault = SqlDialect.CALCITE .quoteIdentifier(configurer.useUppercaseDefault() ? "DEFAULT" : "default"); converted = converted.replaceAll("(?i)default\\.", escapedDefault + "."); // use Calcite dialect to cater to SqlParser converted = converted.replaceAll("\"(?i)default\"\\.", escapedDefault + "."); } if (!configurer.skipDefaultConvert()) { try { SqlNode sqlNode = SqlParser.create(converted).parseQuery(); sqlNode = sqlNode.accept(sqlNodeConverter); converted = sqlWriter.format(sqlNode); } catch (Throwable e) { logger.error("Failed to default convert sql, will use the input: {}", orig, e); } finally { sqlWriter.reset(); } } converted = configurer.fixAfterDefaultConvert(converted); return converted; }
@Override public boolean matches(RelOptRuleCall call) { final HiveAggregate agg = call.rel(0); final HiveJdbcConverter converter = call.rel(1); for (AggregateCall relOptRuleOperand : agg.getAggCallList()) { SqlAggFunction f = relOptRuleOperand.getAggregation(); if (f instanceof HiveSqlCountAggFunction) { //count distinct with more that one argument is not supported HiveSqlCountAggFunction countAgg = (HiveSqlCountAggFunction)f; if (countAgg.isDistinct() && 1 < relOptRuleOperand.getArgList().size()) { return false; } } SqlKind kind = f.getKind(); if (!converter.getJdbcDialect().supportsAggregateFunction(kind)) { return false; } } return true; }
@Override public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { if (call instanceof SqlSelect) { callsUnparseCallOnSqlSelect[0] = true; } super.unparseCall(writer, call, leftPrec, rightPrec); } };
public String toString() { return "SUBSTITUTE(" + CalciteSqlDialect.DEFAULT.quoteStringLiteral(name) + ")"; } }
public void identifier(String name) { String qName = name; if (isQuoteAllIdentifiers() || dialect.identifierNeedsToBeQuoted(name)) { qName = dialect.quoteIdentifier(name); } maybeWhitespace(qName); pw.print(qName); charCount += qName.length(); setNeedWhitespace(true); }
private SqlNode toSql(RelDataType type) { switch (dialect.getDatabaseProduct()) { case MYSQL: switch (type.getSqlTypeName()) { case VARCHAR: // MySQL doesn't have a VARCHAR type, only CHAR. return new SqlDataTypeSpec(new SqlIdentifier("CHAR", POS), type.getPrecision(), -1, null, null, POS); case INTEGER: return new SqlDataTypeSpec(new SqlIdentifier("_UNSIGNED", POS), type.getPrecision(), -1, null, null, POS); } break; } if (type instanceof BasicSqlType) { return new SqlDataTypeSpec( new SqlIdentifier(type.getSqlTypeName().name(), POS), type.getPrecision(), type.getScale(), type.getCharset() != null && dialect.supportsCharSet() ? type.getCharset().name() : null, null, POS); } return SqlTypeUtil.convertTypeToSpec(type); //throw new AssertionError(type); // TODO: implement }
final String viewSql = createView.getQuery().toSqlString(new SqlDialect(SqlDialect.CALCITE.getDatabaseProduct(), SqlDialect.CALCITE.getDatabaseProduct().name(), ParserConfig.QUOTING.string)).getSql(); final ConvertedRelNode convertedRelNode = PrelTransformer.validateAndConvert(config, createView.getQuery()); final RelDataType validatedRowType = convertedRelNode.getValidatedRowType();
/** * Converts a string into a string literal. For example, <code>can't * run</code> becomes <code>'can''t run'</code>. */ public String quoteStringLiteral(String val) { if (containsNonAscii(val)) { final StringBuilder buf = new StringBuilder(); quoteStringLiteralUnicode(buf, val); return buf.toString(); } else { val = FakeUtil.replace(val, "'", "''"); return "'" + val + "'"; } }
public SqlNode getCastSpec(RelDataType type) { if (type instanceof BasicSqlType) { int precision = type.getPrecision(); switch (type.getSqlTypeName()) { case VARCHAR: // if needed, adjust varchar length to max length supported by the system int maxPrecision = getTypeSystem().getMaxPrecision(type.getSqlTypeName()); if (type.getPrecision() > maxPrecision) { precision = maxPrecision; } } return new SqlDataTypeSpec( new SqlIdentifier(type.getSqlTypeName().name(), SqlParserPos.ZERO), precision, type.getScale(), type.getCharset() != null && supportsCharSet() ? type.getCharset().name() : null, null, SqlParserPos.ZERO); } return SqlTypeUtil.convertTypeToSpec(type); }
DatabaseProduct(String databaseProductName, String quoteString, NullCollation nullCollation) { Objects.requireNonNull(databaseProductName); Objects.requireNonNull(nullCollation); dialect = Suppliers.memoize(() -> { final SqlDialect dialect = SqlDialectFactoryImpl.simple(DatabaseProduct.this); if (dialect != null) { return dialect; } return new SqlDialect(SqlDialect.EMPTY_CONTEXT .withDatabaseProduct(DatabaseProduct.this) .withDatabaseProductName(databaseProductName) .withIdentifierQuoteString(quoteString) .withNullCollation(nullCollation)); })::get; }
public SqlNode getCastSpec(RelDataType type) { if (type instanceof BasicSqlType) { return new SqlDataTypeSpec( new SqlIdentifier(type.getSqlTypeName().name(), SqlParserPos.ZERO), type.getPrecision(), type.getScale(), type.getCharset() != null && supportsCharSet() ? type.getCharset().name() : null, null, SqlParserPos.ZERO); } return SqlTypeUtil.convertTypeToSpec(type); }
public void addOrderItem(List<SqlNode> orderByList, RelFieldCollation field) { if (field.nullDirection != RelFieldCollation.NullDirection.UNSPECIFIED) { boolean first = field.nullDirection == RelFieldCollation.NullDirection.FIRST; SqlNode nullDirectionNode = dialect.emulateNullDirection(context.field(field.getFieldIndex()), first, field.direction.isDescending()); if (nullDirectionNode != null) { orderByList.add(nullDirectionNode); field = new RelFieldCollation(field.getFieldIndex(), field.getDirection(), RelFieldCollation.NullDirection.UNSPECIFIED); } } orderByList.add(context.toSql(field)); }
/** Converts a collation to an ORDER BY item. */ public SqlNode toSql(RelFieldCollation collation) { SqlNode node = field(collation.getFieldIndex()); switch (collation.getDirection()) { case DESCENDING: case STRICTLY_DESCENDING: node = SqlStdOperatorTable.DESC.createCall(POS, node); } if (collation.nullDirection != dialect.defaultNullDirection(collation.direction)) { switch (collation.nullDirection) { case FIRST: node = SqlStdOperatorTable.NULLS_FIRST.createCall(POS, node); break; case LAST: node = SqlStdOperatorTable.NULLS_LAST.createCall(POS, node); break; } } return node; }
public void unparse( SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { assert call.operandCount() >= 2; final SqlWriter.Frame frame = writer.startList( SqlWriter.FrameTypeEnum.SIMPLE); call.operand(0).unparse(writer, leftPrec, getLeftPrec()); final boolean needsSpace = true; writer.setNeedWhitespace(needsSpace); if (writer.getDialect().allowsAs()) { writer.sep("AS"); writer.setNeedWhitespace(needsSpace); } call.operand(1).unparse(writer, getRightPrec(), rightPrec); if (call.operandCount() > 2) { final SqlWriter.Frame frame1 = writer.startList(SqlWriter.FrameTypeEnum.SIMPLE, "(", ")"); for (SqlNode operand : Util.skip(call.getOperandList(), 2)) { writer.sep(",", false); operand.unparse(writer, 0, 0); } writer.endList(frame1); } writer.endList(frame); }
@Override public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { if (call instanceof SqlSelect) { callsUnparseCallOnSqlSelect[0] = true; } super.unparseCall(writer, call, leftPrec, rightPrec); } };
public String toString() { return "SUBSTITUTE(" + CalciteSqlDialect.DEFAULT.quoteStringLiteral(name) + ")"; } }
public void identifier(String name) { String qName = name; if (isQuoteAllIdentifiers() || dialect.identifierNeedsToBeQuoted(name)) { qName = dialect.quoteIdentifier(name); } maybeWhitespace(qName); pw.print(qName); charCount += qName.length(); setNeedWhitespace(true); }
/** * Converts a string into a string literal. For example, <code>can't * run</code> becomes <code>'can''t run'</code>. */ public String quoteStringLiteral(String val) { if (containsNonAscii(val)) { final StringBuilder buf = new StringBuilder(); quoteStringLiteralUnicode(buf, val); return buf.toString(); } else { val = FakeUtil.replace(val, "'", "''"); return "'" + val + "'"; } }
DatabaseProduct(String databaseProductName, String quoteString, NullCollation nullCollation) { Objects.requireNonNull(databaseProductName); Objects.requireNonNull(nullCollation); dialect = Suppliers.memoize(() -> { final SqlDialect dialect = SqlDialectFactoryImpl.simple(DatabaseProduct.this); if (dialect != null) { return dialect; } return new SqlDialect(SqlDialect.EMPTY_CONTEXT .withDatabaseProduct(DatabaseProduct.this) .withDatabaseProductName(databaseProductName) .withIdentifierQuoteString(quoteString) .withNullCollation(nullCollation)); })::get; }
void addOrderItem(List<SqlNode> orderByList, RelFieldCollation field) { if (field.nullDirection != RelFieldCollation.NullDirection.UNSPECIFIED) { final boolean first = field.nullDirection == RelFieldCollation.NullDirection.FIRST; SqlNode nullDirectionNode = dialect.emulateNullDirection(field(field.getFieldIndex()), first, field.direction.isDescending()); if (nullDirectionNode != null) { orderByList.add(nullDirectionNode); field = new RelFieldCollation(field.getFieldIndex(), field.getDirection(), RelFieldCollation.NullDirection.UNSPECIFIED); } } orderByList.add(toSql(field)); }