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

The following examples show how to use org.apache.calcite.sql.SqlWriter#keyword() . 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: SqlCreateTable.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  writer.keyword("CREATE");
  if (isTemporary.booleanValue()) {
    writer.keyword("TEMPORARY");
  }
  writer.keyword("TABLE");
  if (tableNonExistenceCheck.booleanValue()) {
    writer.keyword("IF");
    writer.keyword("NOT");
    writer.keyword("EXISTS");
  }
  tblName.unparse(writer, leftPrec, rightPrec);
  if (fieldList.size() > 0) {
    SqlHandlerUtil.unparseSqlNodeList(writer, leftPrec, rightPrec, fieldList);
  }
  if (partitionColumns.size() > 0) {
    writer.keyword("PARTITION BY");
    SqlHandlerUtil.unparseSqlNodeList(writer, leftPrec, rightPrec, partitionColumns);
  }
  writer.keyword("AS");
  query.unparse(writer, leftPrec, rightPrec);
}
 
Example 2
Source File: SqlRollupOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
private void unparseCube(SqlWriter writer, SqlCall call) {
  writer.keyword(call.getOperator().getName());
  final SqlWriter.Frame frame =
      writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL, "(", ")");
  for (SqlNode operand : call.getOperandList()) {
    writer.sep(",");
    if (operand.getKind() == SqlKind.ROW) {
      final SqlWriter.Frame frame2 =
          writer.startList(SqlWriter.FrameTypeEnum.SIMPLE, "(", ")");
      for (SqlNode operand2 : ((SqlCall) operand).getOperandList()) {
        writer.sep(",");
        operand2.unparse(writer, 0, 0);
      }
      writer.endList(frame2);
    } else if (operand instanceof SqlNodeList
        && ((SqlNodeList) operand).size() == 0) {
      writer.keyword("()");
    } else {
      operand.unparse(writer, 0, 0);
    }
  }
  writer.endList(frame);
}
 
Example 3
Source File: SqlAddReplaceColumns.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	super.unparse(writer, leftPrec, rightPrec);
	if (replace) {
		writer.keyword("REPLACE");
	} else {
		writer.keyword("ADD");
	}
	writer.keyword("COLUMNS");
	SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.create("sds"), "(", ")");
	for (SqlNode column : newColumns) {
		printIndent(writer);
		column.unparse(writer, leftPrec, rightPrec);
	}
	writer.newlineAndIndent();
	writer.endList(frame);
}
 
Example 4
Source File: RichSqlInsert.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	writer.startList(SqlWriter.FrameTypeEnum.SELECT);
	String insertKeyword = "INSERT INTO";
	if (isUpsert()) {
		insertKeyword = "UPSERT INTO";
	} else if (isOverwrite()) {
		insertKeyword = "INSERT OVERWRITE";
	}
	writer.sep(insertKeyword);
	final int opLeft = getOperator().getLeftPrec();
	final int opRight = getOperator().getRightPrec();
	getTargetTable().unparse(writer, opLeft, opRight);
	if (getTargetColumnList() != null) {
		getTargetColumnList().unparse(writer, opLeft, opRight);
	}
	writer.newlineAndIndent();
	if (staticPartitions != null && staticPartitions.size() > 0) {
		writer.keyword("PARTITION");
		staticPartitions.unparse(writer, opLeft, opRight);
		writer.newlineAndIndent();
	}
	getSource().unparse(writer, 0, 0);
}
 
Example 5
Source File: SqlJsonQueryFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
private void unparseEmptyOrErrorBehavior(SqlWriter writer,
    SqlJsonQueryEmptyOrErrorBehavior emptyBehavior) {
  switch (emptyBehavior) {
  case NULL:
    writer.keyword("NULL");
    break;
  case ERROR:
    writer.keyword("ERROR");
    break;
  case EMPTY_ARRAY:
    writer.keyword("EMPTY ARRAY");
    break;
  case EMPTY_OBJECT:
    writer.keyword("EMPTY OBJECT");
    break;
  default:
    throw new IllegalStateException("unreachable code");
  }
}
 
Example 6
Source File: SqlTableLike.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	writer.keyword("LIKE");
	sourceTable.unparse(writer, leftPrec, rightPrec);
	SqlWriter.Frame frame = writer.startList("(", ")");
	for (SqlTableLikeOption option : options) {
		writer.newlineAndIndent();
		writer.print("  ");
		writer.keyword(option.mergingStrategy.toString());
		writer.keyword(option.featureOption.toString());
	}
	writer.newlineAndIndent();
	writer.endList(frame);
}
 
Example 7
Source File: SqlExplainJson.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  writer.keyword("EXPLAIN");
  writer.keyword("JSON");
  if(phase != null) {
    phase.unparse(writer, 0, 0);
  }
  writer.keyword("FOR");
  query.unparse(writer, leftPrec, rightPrec);
}
 
Example 8
Source File: SqlColumnListConstructor.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void unparse(
    SqlWriter writer,
    SqlCall call,
    int leftPrec,
    int rightPrec) {
  writer.keyword("ROW");
  final SqlWriter.Frame frame = writer.startList("(", ")");
  for (SqlNode operand : call.getOperandList()) {
    writer.sep(",");
    operand.unparse(writer, leftPrec, rightPrec);
  }
  writer.endList(frame);
}
 
Example 9
Source File: SqlTruncateTable.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  writer.keyword("TRUNCATE");
  if (tableKeywordPresent) {
    writer.keyword("TABLE");
  }
  if (tableExistenceCheck) {
    writer.keyword("IF");
    writer.keyword("EXISTS");
  }
  tableName.unparse(writer, leftPrec, rightPrec);
}
 
Example 10
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 11
Source File: SqlRefreshTable.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  writer.keyword("ALTER");
  writer.keyword("TABLE");
  table.unparse(writer, leftPrec, rightPrec);
  writer.keyword("REFRESH");
  writer.keyword("METADATA");

  if (deleteUnavail.getValue() != null) {
    if (deleteUnavail.booleanValue()) {
      writer.keyword("DELETE");
      writer.keyword("WHEN");
      writer.keyword("MISSING");
    } else {
      writer.keyword("MAINTAIN");
      writer.keyword("WHEN");
      writer.keyword("MISSING");
    }
  }

  if (forceUp.getValue() != null) {
    if (forceUp.booleanValue()) {
      writer.keyword("FORCE");
      writer.keyword("UPDATE");
    } else {
      writer.keyword("LAZY");
      writer.keyword("UPDATE");
    }
  }

  if (promotion.getValue() != null) {
    if (promotion.booleanValue()) {
      writer.keyword("AUTO");
      writer.keyword("PROMOTION");
    } else {
      writer.keyword("AVOID");
      writer.keyword("PROMOTION");
    }
  }
}
 
Example 12
Source File: SqlDescribeHiveTable.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	writer.keyword("DESCRIBE");
	if (extended) {
		writer.keyword("EXTENDED");
	} else if (formatted) {
		writer.keyword("FORMATTED");
	}
	tableNameIdentifier.unparse(writer, leftPrec, rightPrec);
}
 
Example 13
Source File: SqlCreateView.java    From dremio-oss 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 (replaceView) {
    writer.keyword("OR");
    writer.keyword("REPLACE");
  }
  writer.keyword("VIEW");
  viewName.unparse(writer, leftPrec, rightPrec);
  if (fieldList.size() > 0) {
    SqlHandlerUtil.unparseSqlNodeList(writer, leftPrec, rightPrec, fieldList);
  }
  writer.keyword("AS");
  query.unparse(writer, leftPrec, rightPrec);
}
 
Example 14
Source File: SqlAlterHiveTableAddReplaceColumn.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	writer.keyword("ALTER TABLE");
	tableIdentifier.unparse(writer, leftPrec, rightPrec);
	SqlNodeList partitionSpec = getPartitionSpec();
	if (partitionSpec != null && partitionSpec.size() > 0) {
		writer.keyword("PARTITION");
		partitionSpec.unparse(writer, getOperator().getLeftPrec(), getOperator().getRightPrec());
	}
	if (isReplace()) {
		writer.keyword("REPLACE");
	} else {
		writer.keyword("ADD");
	}
	writer.keyword("COLUMNS");
	SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.create("sds"), "(", ")");
	for (SqlNode column : origColumns) {
		printIndent(writer);
		column.unparse(writer, leftPrec, rightPrec);
	}
	writer.newlineAndIndent();
	writer.endList(frame);
	if (cascade) {
		writer.keyword("CASCADE");
	} else {
		writer.keyword("RESTRICT");
	}
}
 
Example 15
Source File: MysqlSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void unparseSqlIntervalQualifier(SqlWriter writer,
    SqlIntervalQualifier qualifier, RelDataTypeSystem typeSystem) {

  //  Unit Value         | Expected Format
  // --------------------+-------------------------------------------
  //  MICROSECOND        | MICROSECONDS
  //  SECOND             | SECONDS
  //  MINUTE             | MINUTES
  //  HOUR               | HOURS
  //  DAY                | DAYS
  //  WEEK               | WEEKS
  //  MONTH              | MONTHS
  //  QUARTER            | QUARTERS
  //  YEAR               | YEARS
  //  MINUTE_SECOND      | 'MINUTES:SECONDS'
  //  HOUR_MINUTE        | 'HOURS:MINUTES'
  //  DAY_HOUR           | 'DAYS HOURS'
  //  YEAR_MONTH         | 'YEARS-MONTHS'
  //  MINUTE_MICROSECOND | 'MINUTES:SECONDS.MICROSECONDS'
  //  HOUR_MICROSECOND   | 'HOURS:MINUTES:SECONDS.MICROSECONDS'
  //  SECOND_MICROSECOND | 'SECONDS.MICROSECONDS'
  //  DAY_MINUTE         | 'DAYS HOURS:MINUTES'
  //  DAY_MICROSECOND    | 'DAYS HOURS:MINUTES:SECONDS.MICROSECONDS'
  //  DAY_SECOND         | 'DAYS HOURS:MINUTES:SECONDS'
  //  HOUR_SECOND        | 'HOURS:MINUTES:SECONDS'

  if (!qualifier.useDefaultFractionalSecondPrecision()) {
    throw new AssertionError("Fractional second precision is not supported now ");
  }

  final String start = validate(qualifier.timeUnitRange.startUnit).name();
  if (qualifier.timeUnitRange.startUnit == TimeUnit.SECOND
      || qualifier.timeUnitRange.endUnit == null) {
    writer.keyword(start);
  } else {
    writer.keyword(start + "_" + qualifier.timeUnitRange.endUnit.name());
  }
}
 
Example 16
Source File: SqlShowCatalogs.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(
		SqlWriter writer,
		int leftPrec,
		int rightPrec) {
	writer.keyword("SHOW CATALOGS");
}
 
Example 17
Source File: SqlAlterHiveDatabaseProps.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	super.unparse(writer, leftPrec, rightPrec);
	writer.keyword("DBPROPERTIES");
	SqlWriter.Frame withFrame = writer.startList("(", ")");
	for (SqlNode property : originPropList) {
		printIndent(writer);
		property.unparse(writer, leftPrec, rightPrec);
	}
	writer.newlineAndIndent();
	writer.endList(withFrame);
}
 
Example 18
Source File: SqlUseDatabase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(
		SqlWriter writer,
		int leftPrec,
		int rightPrec) {
	writer.keyword("USE");
	databaseName.unparse(writer, leftPrec, rightPrec);
}
 
Example 19
Source File: SqlShowViews.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	writer.keyword("SHOW VIEWS");
}
 
Example 20
Source File: SqlCreateHiveTable.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	writer.keyword("CREATE");
	if (isTemporary()) {
		writer.keyword("TEMPORARY");
	}
	if (isExternal) {
		writer.keyword("EXTERNAL");
	}
	writer.keyword("TABLE");
	if (ifNotExists) {
		writer.keyword("IF NOT EXISTS");
	}
	getTableName().unparse(writer, leftPrec, rightPrec);
	// columns
	SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.create("sds"), "(", ")");
	unparseColumns(creationContext,
			origColList,
			writer, leftPrec, rightPrec);
	for (SqlTableConstraint tableConstraint : creationContext.constraints) {
		printIndent(writer);
		tableConstraint.getConstraintNameIdentifier().ifPresent(name -> {
			writer.keyword("CONSTRAINT");
			name.unparse(writer, leftPrec, rightPrec);
		});
		writer.keyword("PRIMARY KEY");
		SqlWriter.Frame pkFrame = writer.startList("(", ")");
		tableConstraint.getColumns().unparse(writer, leftPrec, rightPrec);
		writer.endList(pkFrame);
		creationContext.pkTrait.unparse(writer, leftPrec, rightPrec);
	}
	writer.newlineAndIndent();
	writer.endList(frame);
	// table comment
	getComment().ifPresent(c -> {
		writer.keyword("COMMENT");
		c.unparse(writer, leftPrec, rightPrec);
	});
	// partitions
	if (origPartColList.size() > 0) {
		writer.newlineAndIndent();
		writer.keyword("PARTITIONED BY");
		SqlWriter.Frame partitionedByFrame = writer.startList("(", ")");
		unparseColumns(creationContext,
				origPartColList,
				writer, leftPrec, rightPrec);
		writer.newlineAndIndent();
		writer.endList(partitionedByFrame);
	}
	// row format
	unparseRowFormat(writer, leftPrec, rightPrec);
	// stored as
	unparseStoredAs(writer, leftPrec, rightPrec);
	// location
	if (location != null) {
		writer.newlineAndIndent();
		writer.keyword("LOCATION");
		location.unparse(writer, leftPrec, rightPrec);
	}
	// properties
	if (originPropList.size() > 0) {
		writer.newlineAndIndent();
		writer.keyword("TBLPROPERTIES");
		unparsePropList(originPropList, writer, leftPrec, rightPrec);
	}
}