Java Code Examples for org.apache.calcite.sql.SqlWriter#literal()

The following examples show how to use org.apache.calcite.sql.SqlWriter#literal() . 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: Db2SqlDialect.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public void unparseSqlIntervalLiteral(SqlWriter writer,
    SqlIntervalLiteral literal, int leftPrec, int rightPrec) {
  // A duration is a positive or negative number representing an interval of time.
  // If one operand is a date, the other labeled duration of YEARS, MONTHS, or DAYS.
  // If one operand is a time, the other must be labeled duration of HOURS, MINUTES, or SECONDS.
  // If one operand is a timestamp, the other operand can be any of teh duration.

  SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) literal.getValue();
  if (interval.getSign() == -1) {
    writer.print("-");
  }
  writer.literal(literal.getValue().toString());
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
      RelDataTypeSystem.DEFAULT);
}
 
Example 2
Source File: BigQuerySqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** BigQuery interval syntax: INTERVAL int64 time_unit. */
@Override public void unparseSqlIntervalLiteral(
        SqlWriter writer, SqlIntervalLiteral literal, int leftPrec, int rightPrec) {
  SqlIntervalLiteral.IntervalValue interval =
          (SqlIntervalLiteral.IntervalValue) literal.getValue();
  writer.keyword("INTERVAL");
  if (interval.getSign() == -1) {
    writer.print("-");
  }
  Long intervalValueInLong;
  try {
    intervalValueInLong = Long.parseLong(literal.getValue().toString());
  } catch (NumberFormatException e) {
    throw new RuntimeException("Only INT64 is supported as the interval value for BigQuery.");
  }
  writer.literal(intervalValueInLong.toString());
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
          RelDataTypeSystem.DEFAULT);
}
 
Example 3
Source File: Db2SqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void unparseSqlIntervalLiteral(SqlWriter writer,
    SqlIntervalLiteral literal, int leftPrec, int rightPrec) {
  // A duration is a positive or negative number representing an interval of time.
  // If one operand is a date, the other labeled duration of YEARS, MONTHS, or DAYS.
  // If one operand is a time, the other must be labeled duration of HOURS, MINUTES, or SECONDS.
  // If one operand is a timestamp, the other operand can be any of teh duration.

  SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) literal.getValue();
  if (interval.getSign() == -1) {
    writer.print("-");
  }
  writer.literal(literal.getValue().toString());
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
      RelDataTypeSystem.DEFAULT);
}
 
Example 4
Source File: ClickHouseSqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void unparseDateTimeLiteral(SqlWriter writer,
    SqlAbstractDateTimeLiteral literal, int leftPrec, int rightPrec) {
  String toFunc;
  if (literal instanceof SqlDateLiteral) {
    toFunc = "toDate";
  } else if (literal instanceof SqlTimestampLiteral) {
    toFunc = "toDateTime";
  } else if (literal instanceof SqlTimeLiteral) {
    toFunc = "toTime";
  } else {
    throw new RuntimeException("ClickHouse does not support DateTime literal: "
        + literal);
  }

  writer.literal(toFunc + "('" + literal.toFormattedString() + "')");
}
 
Example 5
Source File: MssqlSqlDialect.java    From Bats with Apache License 2.0 5 votes vote down vote up
private void unparseSqlIntervalLiteralMssql(
    SqlWriter writer, SqlIntervalLiteral literal, int sign) {
  final SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) literal.getValue();
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
      RelDataTypeSystem.DEFAULT);
  writer.sep(",", true);
  if (interval.getSign() * sign == -1) {
    writer.print("-");
  }
  writer.literal(literal.getValue().toString());
}
 
Example 6
Source File: SqlSchema.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  writer.keyword("CREATE");

  if (SqlCreateType.OR_REPLACE == getSqlCreateType()) {
    writer.keyword("OR");
    writer.keyword("REPLACE");
  }

  writer.keyword("SCHEMA");
  writer.literal(getSchema());

  super.unparse(writer, leftPrec, rightPrec);

  if (load != null) {
    writer.keyword("LOAD");
    load.unparse(writer, leftPrec, rightPrec);
  }

  if (path != null) {
    writer.keyword("PATH");
    path.unparse(writer, leftPrec, rightPrec);
  }

  if (properties != null) {
    writer.keyword("PROPERTIES");
    writer.keyword("(");

    for (int i = 1; i < properties.size(); i += 2) {
      if (i != 1) {
        writer.keyword(",");
      }
      properties.get(i - 1).unparse(writer, leftPrec, rightPrec);
      writer.keyword("=");
      properties.get(i).unparse(writer, leftPrec, rightPrec);
    }

    writer.keyword(")");
  }
}
 
Example 7
Source File: BigQuerySqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * For usage of TRIM, LTRIM and RTRIM in BQ see
 * <a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#trim">
 *  BQ Trim Function</a>.
 */
private void unparseTrim(SqlWriter writer, SqlCall call, int leftPrec,
    int rightPrec) {
  final String operatorName;
  SqlLiteral trimFlag = call.operand(0);
  SqlLiteral valueToTrim = call.operand(1);
  switch (trimFlag.getValueAs(SqlTrimFunction.Flag.class)) {
  case LEADING:
    operatorName = "LTRIM";
    break;
  case TRAILING:
    operatorName = "RTRIM";
    break;
  default:
    operatorName = call.getOperator().getName();
    break;
  }
  final SqlWriter.Frame trimFrame = writer.startFunCall(operatorName);
  call.operand(2).unparse(writer, leftPrec, rightPrec);

  // If the trimmed character is a non-space character, add it to the target SQL.
  // eg: TRIM(BOTH 'A' from 'ABCD'
  // Output Query: TRIM('ABC', 'A')
  if (!valueToTrim.toValue().matches("\\s+")) {
    writer.literal(",");
    call.operand(1).unparse(writer, leftPrec, rightPrec);
  }
  writer.endFunCall(trimFrame);
}
 
Example 8
Source File: OracleSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void unparseDateTimeLiteral(SqlWriter writer,
    SqlAbstractDateTimeLiteral literal, int leftPrec, int rightPrec) {
  if (literal instanceof SqlTimestampLiteral) {
    writer.literal("TO_TIMESTAMP('"
        + literal.toFormattedString() + "', 'YYYY-MM-DD HH24:MI:SS.FF')");
  } else if (literal instanceof SqlDateLiteral) {
    writer.literal("TO_DATE('"
        + literal.toFormattedString() + "', 'YYYY-MM-DD')");
  } else if (literal instanceof SqlTimeLiteral) {
    writer.literal("TO_TIME('"
        + literal.toFormattedString() + "', 'HH24:MI:SS.FF')");
  } else {
    super.unparseDateTimeLiteral(writer, literal, leftPrec, rightPrec);
  }
}
 
Example 9
Source File: MssqlSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void unparseSqlIntervalLiteralMssql(
    SqlWriter writer, SqlIntervalLiteral literal, int sign) {
  final SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) literal.getValue();
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
      RelDataTypeSystem.DEFAULT);
  writer.sep(",", true);
  if (interval.getSign() * sign == -1) {
    writer.print("-");
  }
  writer.literal(literal.getValue().toString());
}
 
Example 10
Source File: MssqlSqlDialect.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public void unparseDateTimeLiteral(SqlWriter writer,
    SqlAbstractDateTimeLiteral literal, int leftPrec, int rightPrec) {
  writer.literal("'" + literal.toFormattedString() + "'");
}
 
Example 11
Source File: SqlLiteralChainOperator.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void unparse(
    SqlWriter writer,
    SqlCall call,
    int leftPrec,
    int rightPrec) {
  final SqlWriter.Frame frame = writer.startList("", "");
  SqlCollation collation = null;
  for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
    SqlLiteral rand = (SqlLiteral) operand.e;
    if (operand.i > 0) {
      // SQL:2003 says there must be a newline between string
      // fragments.
      writer.newlineAndIndent();
    }
    if (rand instanceof SqlCharStringLiteral) {
      NlsString nls = ((SqlCharStringLiteral) rand).getNlsString();
      if (operand.i == 0) {
        collation = nls.getCollation();

        // print with prefix
        writer.literal(nls.asSql(true, false));
      } else {
        // print without prefix
        writer.literal(nls.asSql(false, false));
      }
    } else if (operand.i == 0) {
      // print with prefix
      rand.unparse(writer, leftPrec, rightPrec);
    } else {
      // print without prefix
      if (rand.getTypeName() == SqlTypeName.BINARY) {
        BitString bs = (BitString) rand.getValue();
        writer.literal("'" + bs.toHexString() + "'");
      } else {
        writer.literal("'" + rand.toValue() + "'");
      }
    }
  }
  if (collation != null) {
    collation.unparse(writer, 0, 0);
  }
  writer.endList(frame);
}
 
Example 12
Source File: MssqlSqlDialect.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public void unparseDateTimeLiteral(SqlWriter writer,
    SqlAbstractDateTimeLiteral literal, int leftPrec, int rightPrec) {
  writer.literal("'" + literal.toFormattedString() + "'");
}
 
Example 13
Source File: SqlLiteralChainOperator.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void unparse(
    SqlWriter writer,
    SqlCall call,
    int leftPrec,
    int rightPrec) {
  final SqlWriter.Frame frame = writer.startList("", "");
  SqlCollation collation = null;
  for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
    SqlLiteral rand = (SqlLiteral) operand.e;
    if (operand.i > 0) {
      // SQL:2003 says there must be a newline between string
      // fragments.
      writer.newlineAndIndent();
    }
    if (rand instanceof SqlCharStringLiteral) {
      NlsString nls = ((SqlCharStringLiteral) rand).getNlsString();
      if (operand.i == 0) {
        collation = nls.getCollation();

        // print with prefix
        writer.literal(nls.asSql(true, false, writer.getDialect()));
      } else {
        // print without prefix
        writer.literal(nls.asSql(false, false, writer.getDialect()));
      }
    } else if (operand.i == 0) {
      // print with prefix
      rand.unparse(writer, leftPrec, rightPrec);
    } else {
      // print without prefix
      if (rand.getTypeName() == SqlTypeName.BINARY) {
        BitString bs = (BitString) rand.getValue();
        writer.literal("'" + bs.toHexString() + "'");
      } else {
        writer.literal("'" + rand.toValue() + "'");
      }
    }
  }
  if (collation != null) {
    collation.unparse(writer);
  }
  writer.endList(frame);
}