Java Code Examples for org.apache.calcite.sql.SqlUtil#newContextException()

The following examples show how to use org.apache.calcite.sql.SqlUtil#newContextException() . 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: SqlParserUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
public static SqlTimestampLiteral parseTimestampLiteral(String s,
    SqlParserPos pos) {
  final String dateStr = parseString(s);
  final DateTimeUtils.PrecisionTime pt =
      DateTimeUtils.parsePrecisionDateTimeLiteral(dateStr,
          Format.PER_THREAD.get().timestamp, DateTimeUtils.UTC_ZONE, -1);
  if (pt == null) {
    throw SqlUtil.newContextException(pos,
        RESOURCE.illegalLiteral("TIMESTAMP", s,
            RESOURCE.badFormat(DateTimeUtils.TIMESTAMP_FORMAT_STRING).str()));
  }
  final TimestampString ts =
      TimestampString.fromCalendarFields(pt.getCalendar())
          .withFraction(pt.getFraction());
  return SqlLiteral.createTimestamp(ts, pt.getPrecision(), pos);
}
 
Example 2
Source File: SqlDropTableExtension.java    From kareldb with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(CalcitePrepare.Context context) {
    final List<String> path = context.getDefaultSchemaPath();
    CalciteSchema schema = context.getRootSchema();
    for (String p : path) {
        schema = schema.getSubSchema(p, true);
    }

    final Pair<CalciteSchema, String> pair =
        SqlDdlNodes.schema(context, true, name);
    switch (getKind()) {
        case DROP_TABLE:
            Schema schemaPlus = schema.plus().unwrap(Schema.class);
            boolean existed = schemaPlus.dropTable(name.getSimple());
            pair.left.removeTable(name.getSimple());
            if (!existed && !ifExists) {
                throw SqlUtil.newContextException(name.getParserPosition(),
                    RESOURCE.tableNotFound(name.getSimple()));
            }
            break;
        default:
            throw new AssertionError(getKind());
    }
}
 
Example 3
Source File: SqlParserUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
public static SqlTimestampLiteral parseTimestampLiteral(String s,
    SqlParserPos pos) {
  final String dateStr = parseString(s);
  final Format format = Format.PER_THREAD.get();
  DateTimeUtils.PrecisionTime pt = null;
  // Allow timestamp literals with and without time fields (as does
  // PostgreSQL); TODO: require time fields except in Babel's lenient mode
  final DateFormat[] dateFormats = {format.timestamp, format.date};
  for (DateFormat dateFormat : dateFormats) {
    pt = DateTimeUtils.parsePrecisionDateTimeLiteral(dateStr,
        dateFormat, DateTimeUtils.UTC_ZONE, -1);
    if (pt != null) {
      break;
    }
  }
  if (pt == null) {
    throw SqlUtil.newContextException(pos,
        RESOURCE.illegalLiteral("TIMESTAMP", s,
            RESOURCE.badFormat(DateTimeUtils.TIMESTAMP_FORMAT_STRING).str()));
  }
  final TimestampString ts =
      TimestampString.fromCalendarFields(pt.getCalendar())
          .withFraction(pt.getFraction());
  return SqlLiteral.createTimestamp(ts, pt.getPrecision(), pos);
}
 
Example 4
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Executes a {@code CREATE VIEW} command. */
public void execute(SqlCreateView create,
    CalcitePrepare.Context context) {
  final Pair<CalciteSchema, String> pair =
      schema(context, true, create.name);
  final SchemaPlus schemaPlus = pair.left.plus();
  for (Function function : schemaPlus.getFunctions(pair.right)) {
    if (function.getParameters().isEmpty()) {
      if (!create.getReplace()) {
        throw SqlUtil.newContextException(create.name.getParserPosition(),
            RESOURCE.viewExists(pair.right));
      }
      pair.left.removeFunction(pair.right);
    }
  }
  final SqlNode q = renameColumns(create.columnList, create.query);
  final String sql = q.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
  final ViewTableMacro viewTableMacro =
      ViewTable.viewMacro(schemaPlus, sql, pair.left.path(null),
          context.getObjectPath(), false);
  final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
  Util.discard(x);
  schemaPlus.add(pair.right, viewTableMacro);
}
 
Example 5
Source File: SqlParserUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static SqlDateLiteral parseDateLiteral(String s, SqlParserPos pos) {
  final String dateStr = parseString(s);
  final Calendar cal =
      DateTimeUtils.parseDateFormat(dateStr, Format.PER_THREAD.get().date,
          DateTimeUtils.UTC_ZONE);
  if (cal == null) {
    throw SqlUtil.newContextException(pos,
        RESOURCE.illegalLiteral("DATE", s,
            RESOURCE.badFormat(DateTimeUtils.DATE_FORMAT_STRING).str()));
  }
  final DateString d = DateString.fromCalendarFields(cal);
  return SqlLiteral.createDate(d, pos);
}
 
Example 6
Source File: SqlParserUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static SqlTimeLiteral parseTimeLiteral(String s, SqlParserPos pos) {
  final String dateStr = parseString(s);
  final DateTimeUtils.PrecisionTime pt =
      DateTimeUtils.parsePrecisionDateTimeLiteral(dateStr,
          Format.PER_THREAD.get().time, DateTimeUtils.UTC_ZONE, -1);
  if (pt == null) {
    throw SqlUtil.newContextException(pos,
        RESOURCE.illegalLiteral("TIME", s,
            RESOURCE.badFormat(DateTimeUtils.TIME_FORMAT_STRING).str()));
  }
  final TimeString t = TimeString.fromCalendarFields(pt.getCalendar())
      .withFraction(pt.getFraction());
  return SqlLiteral.createTime(t, pt.getPrecision(), pos);
}
 
Example 7
Source File: SqlParserUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static SqlIntervalLiteral parseIntervalLiteral(SqlParserPos pos,
    int sign, String s, SqlIntervalQualifier intervalQualifier) {
  final String intervalStr = parseString(s);
  if (intervalStr.equals("")) {
    throw SqlUtil.newContextException(pos,
        RESOURCE.illegalIntervalLiteral(s + " "
            + intervalQualifier.toString(), pos.toString()));
  }
  return SqlLiteral.createInterval(sign, intervalStr, intervalQualifier, pos);
}
 
Example 8
Source File: SqlParserUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static SqlIntervalLiteral parseIntervalLiteral(SqlParserPos pos,
    int sign, String s, SqlIntervalQualifier intervalQualifier) {
  final String intervalStr = parseString(s);
  if (intervalStr.equals("")) {
    throw SqlUtil.newContextException(pos,
        RESOURCE.illegalIntervalLiteral(s + " "
            + intervalQualifier.toString(), pos.toString()));
  }
  return SqlLiteral.createInterval(sign, intervalStr, intervalQualifier, pos);
}
 
Example 9
Source File: SqlParserUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static SqlTimeLiteral parseTimeLiteral(String s, SqlParserPos pos) {
  final String dateStr = parseString(s);
  final DateTimeUtils.PrecisionTime pt =
      DateTimeUtils.parsePrecisionDateTimeLiteral(dateStr,
          Format.PER_THREAD.get().time, DateTimeUtils.UTC_ZONE, -1);
  if (pt == null) {
    throw SqlUtil.newContextException(pos,
        RESOURCE.illegalLiteral("TIME", s,
            RESOURCE.badFormat(DateTimeUtils.TIME_FORMAT_STRING).str()));
  }
  final TimeString t = TimeString.fromCalendarFields(pt.getCalendar())
      .withFraction(pt.getFraction());
  return SqlLiteral.createTime(t, pt.getPrecision(), pos);
}
 
Example 10
Source File: SqlParserUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static SqlDateLiteral parseDateLiteral(String s, SqlParserPos pos) {
  final String dateStr = parseString(s);
  final Calendar cal =
      DateTimeUtils.parseDateFormat(dateStr, Format.PER_THREAD.get().date,
          DateTimeUtils.UTC_ZONE);
  if (cal == null) {
    throw SqlUtil.newContextException(pos,
        RESOURCE.illegalLiteral("DATE", s,
            RESOURCE.badFormat(DateTimeUtils.DATE_FORMAT_STRING).str()));
  }
  final DateString d = DateString.fromCalendarFields(cal);
  return SqlLiteral.createDate(d, pos);
}
 
Example 11
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Executes a {@code CREATE SCHEMA} command. */
public void execute(SqlCreateSchema create,
    CalcitePrepare.Context context) {
  final Pair<CalciteSchema, String> pair = schema(context, true, create.name);
  final SchemaPlus subSchema0 = pair.left.plus().getSubSchema(pair.right);
  if (subSchema0 != null) {
    if (!create.getReplace() && !create.ifNotExists) {
      throw SqlUtil.newContextException(create.name.getParserPosition(),
          RESOURCE.schemaExists(pair.right));
    }
  }
  final Schema subSchema = new AbstractSchema();
  pair.left.add(pair.right, subSchema);
}
 
Example 12
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Executes a {@code DROP SCHEMA} command. */
public void execute(SqlDropSchema drop,
    CalcitePrepare.Context context) {
  final List<String> path = context.getDefaultSchemaPath();
  CalciteSchema schema = context.getRootSchema();
  for (String p : path) {
    schema = schema.getSubSchema(p, true);
  }
  final boolean existed = schema.removeSubSchema(drop.name.getSimple());
  if (!existed && !drop.ifExists) {
    throw SqlUtil.newContextException(drop.name.getParserPosition(),
        RESOURCE.schemaNotFound(drop.name.getSimple()));
  }
}
 
Example 13
Source File: RexCallBinding.java    From calcite with Apache License 2.0 4 votes vote down vote up
public CalciteException newError(
    Resources.ExInst<SqlValidatorException> e) {
  return SqlUtil.newContextException(SqlParserPos.ZERO, e);
}
 
Example 14
Source File: Aggregate.java    From Bats with Apache License 2.0 4 votes vote down vote up
public CalciteException newError(
    Resources.ExInst<SqlValidatorException> e) {
  return SqlUtil.newContextException(SqlParserPos.ZERO, e);
}
 
Example 15
Source File: Aggregate.java    From calcite with Apache License 2.0 4 votes vote down vote up
public CalciteException newError(
    Resources.ExInst<SqlValidatorException> e) {
  return SqlUtil.newContextException(SqlParserPos.ZERO, e);
}
 
Example 16
Source File: ExtensionDdlExecutor.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Executes a {@code CREATE TABLE} command. Called via reflection. */
public void execute(SqlCreateTable create, CalcitePrepare.Context context) {
  final CalciteSchema schema =
      Schemas.subSchema(context.getRootSchema(),
          context.getDefaultSchemaPath());
  final JavaTypeFactory typeFactory = context.getTypeFactory();
  final RelDataType queryRowType;
  if (create.query != null) {
    // A bit of a hack: pretend it's a view, to get its row type
    final String sql =
        create.query.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
    final ViewTableMacro viewTableMacro =
        ViewTable.viewMacro(schema.plus(), sql, schema.path(null),
            context.getObjectPath(), false);
    final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
    queryRowType = x.getRowType(typeFactory);

    if (create.columnList != null
        && queryRowType.getFieldCount() != create.columnList.size()) {
      throw SqlUtil.newContextException(create.columnList.getParserPosition(),
          RESOURCE.columnCountMismatch());
    }
  } else {
    queryRowType = null;
  }
  final RelDataTypeFactory.Builder builder = typeFactory.builder();
  if (create.columnList != null) {
    final SqlValidator validator = new ContextSqlValidator(context, false);
    create.forEachNameType((name, typeSpec) ->
        builder.add(name.getSimple(), typeSpec.deriveType(validator, true)));
  } else {
    if (queryRowType == null) {
      // "CREATE TABLE t" is invalid; because there is no "AS query" we need
      // a list of column names and types, "CREATE TABLE t (INT c)".
      throw SqlUtil.newContextException(create.name.getParserPosition(),
          RESOURCE.createTableRequiresColumnList());
    }
    builder.addAll(queryRowType.getFieldList());
  }
  final RelDataType rowType = builder.build();
  schema.add(create.name.getSimple(),
      new MutableArrayTable(create.name.getSimple(),
          RelDataTypeImpl.proto(rowType)));
  if (create.query != null) {
    populate(create.name, create.query, context);
  }
}
 
Example 17
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Executes {@code DROP FUNCTION}, {@code DROP TABLE},
 * {@code DROP MATERIALIZED VIEW}, {@code DROP TYPE},
 * {@code DROP VIEW} commands. */
public void execute(SqlDropObject drop,
    CalcitePrepare.Context context) {
  final List<String> path = context.getDefaultSchemaPath();
  CalciteSchema schema = context.getRootSchema();
  for (String p : path) {
    schema = schema.getSubSchema(p, true);
  }
  final boolean existed;
  switch (drop.getKind()) {
  case DROP_TABLE:
  case DROP_MATERIALIZED_VIEW:
    existed = schema.removeTable(drop.name.getSimple());
    if (!existed && !drop.ifExists) {
      throw SqlUtil.newContextException(drop.name.getParserPosition(),
          RESOURCE.tableNotFound(drop.name.getSimple()));
    }
    break;
  case DROP_VIEW:
    // Not quite right: removes any other functions with the same name
    existed = schema.removeFunction(drop.name.getSimple());
    if (!existed && !drop.ifExists) {
      throw SqlUtil.newContextException(drop.name.getParserPosition(),
          RESOURCE.viewNotFound(drop.name.getSimple()));
    }
    break;
  case DROP_TYPE:
    existed = schema.removeType(drop.name.getSimple());
    if (!existed && !drop.ifExists) {
      throw SqlUtil.newContextException(drop.name.getParserPosition(),
          RESOURCE.typeNotFound(drop.name.getSimple()));
    }
    break;
  case DROP_FUNCTION:
    existed = schema.removeFunction(drop.name.getSimple());
    if (!existed && !drop.ifExists) {
      throw SqlUtil.newContextException(drop.name.getParserPosition(),
          RESOURCE.functionNotFound(drop.name.getSimple()));
    }
    break;
  case OTHER_DDL:
  default:
    throw new AssertionError(drop.getKind());
  }
}
 
Example 18
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override public SqlNode visit(SqlCall call) {
	SqlKind kind = call.getKind();
	List<SqlNode> operands = call.getOperandList();
	List<SqlNode> newOperands = new ArrayList<>();

	// This code is a workaround for CALCITE-2707
	if (call.getFunctionQuantifier() != null
		&& call.getFunctionQuantifier().getValue() == SqlSelectKeyword.DISTINCT) {
		final SqlParserPos pos = call.getParserPosition();
		throw SqlUtil.newContextException(pos, Static.RESOURCE.functionQuantifierNotAllowed(call.toString()));
	}
	// This code is a workaround for CALCITE-2707

	if (isLogicalNavigation(kind) || isPhysicalNavigation(kind)) {
		SqlNode inner = operands.get(0);
		SqlNode offset = operands.get(1);

		// merge two straight prev/next, update offset
		if (isPhysicalNavigation(kind)) {
			SqlKind innerKind = inner.getKind();
			if (isPhysicalNavigation(innerKind)) {
				List<SqlNode> innerOperands = ((SqlCall) inner).getOperandList();
				SqlNode innerOffset = innerOperands.get(1);
				SqlOperator newOperator = innerKind == kind
					? SqlStdOperatorTable.PLUS : SqlStdOperatorTable.MINUS;
				offset = newOperator.createCall(SqlParserPos.ZERO,
					offset, innerOffset);
				inner = call.getOperator().createCall(SqlParserPos.ZERO,
					innerOperands.get(0), offset);
			}
		}
		SqlNode newInnerNode =
			inner.accept(new NavigationExpander(call.getOperator(), offset));
		if (op != null) {
			newInnerNode = op.createCall(SqlParserPos.ZERO, newInnerNode,
				this.offset);
		}
		return newInnerNode;
	}

	if (operands.size() > 0) {
		for (SqlNode node : operands) {
			if (node != null) {
				SqlNode newNode = node.accept(new NavigationExpander());
				if (op != null) {
					newNode = op.createCall(SqlParserPos.ZERO, newNode, offset);
				}
				newOperands.add(newNode);
			} else {
				newOperands.add(null);
			}
		}
		return call.getOperator().createCall(SqlParserPos.ZERO, newOperands);
	} else {
		if (op == null) {
			return call;
		} else {
			return op.createCall(SqlParserPos.ZERO, call, offset);
		}
	}
}
 
Example 19
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override public SqlNode visit(SqlCall call) {
	SqlKind kind = call.getKind();
	List<SqlNode> operands = call.getOperandList();
	List<SqlNode> newOperands = new ArrayList<>();

	// This code is a workaround for CALCITE-2707
	if (call.getFunctionQuantifier() != null
		&& call.getFunctionQuantifier().getValue() == SqlSelectKeyword.DISTINCT) {
		final SqlParserPos pos = call.getParserPosition();
		throw SqlUtil.newContextException(pos, Static.RESOURCE.functionQuantifierNotAllowed(call.toString()));
	}
	// This code is a workaround for CALCITE-2707

	if (isLogicalNavigation(kind) || isPhysicalNavigation(kind)) {
		SqlNode inner = operands.get(0);
		SqlNode offset = operands.get(1);

		// merge two straight prev/next, update offset
		if (isPhysicalNavigation(kind)) {
			SqlKind innerKind = inner.getKind();
			if (isPhysicalNavigation(innerKind)) {
				List<SqlNode> innerOperands = ((SqlCall) inner).getOperandList();
				SqlNode innerOffset = innerOperands.get(1);
				SqlOperator newOperator = innerKind == kind
					? SqlStdOperatorTable.PLUS : SqlStdOperatorTable.MINUS;
				offset = newOperator.createCall(SqlParserPos.ZERO,
					offset, innerOffset);
				inner = call.getOperator().createCall(SqlParserPos.ZERO,
					innerOperands.get(0), offset);
			}
		}
		SqlNode newInnerNode =
			inner.accept(new NavigationExpander(call.getOperator(), offset));
		if (op != null) {
			newInnerNode = op.createCall(SqlParserPos.ZERO, newInnerNode,
				this.offset);
		}
		return newInnerNode;
	}

	if (operands.size() > 0) {
		for (SqlNode node : operands) {
			if (node != null) {
				SqlNode newNode = node.accept(new NavigationExpander());
				if (op != null) {
					newNode = op.createCall(SqlParserPos.ZERO, newNode, offset);
				}
				newOperands.add(newNode);
			} else {
				newOperands.add(null);
			}
		}
		return call.getOperator().createCall(SqlParserPos.ZERO, newOperands);
	} else {
		if (op == null) {
			return call;
		} else {
			return op.createCall(SqlParserPos.ZERO, call, offset);
		}
	}
}
 
Example 20
Source File: RexCallBinding.java    From Bats with Apache License 2.0 4 votes vote down vote up
public CalciteException newError(
    Resources.ExInst<SqlValidatorException> e) {
  return SqlUtil.newContextException(SqlParserPos.ZERO, e);
}