MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / invent_column_name

Function invent_column_name

src/sql/src/plan/query.rs:3512–3628  ·  view source on GitHub ↗

`table_func_names` is a mapping from a UUID to the original function name. The UUIDs are identifiers that have been rewritten from some table function expression, and this mapping restores the original names.

(
    ecx: &ExprContext,
    expr: &Expr<Aug>,
    table_func_names: &BTreeMap<String, Ident>,
)

Source from the content-addressed store, hash-verified

3510// name. The UUIDs are identifiers that have been rewritten from some table
3511// function expression, and this mapping restores the original names.
3512fn invent_column_name(
3513 ecx: &ExprContext,
3514 expr: &Expr<Aug>,
3515 table_func_names: &BTreeMap<String, Ident>,
3516) -> Result<Option<ColumnName>, PlanError> {
3517 // We follow PostgreSQL exactly here, which has some complicated rules
3518 // around "high" and "low" quality names. Low quality names override other
3519 // low quality names but not high quality names.
3520 //
3521 // See: https://github.com/postgres/postgres/blob/1f655fdc3/src/backend/parser/parse_target.c#L1716-L1728
3522
3523 #[derive(Debug)]
3524 enum NameQuality {
3525 Low,
3526 High,
3527 }
3528
3529 fn invent(
3530 ecx: &ExprContext,
3531 expr: &Expr<Aug>,
3532 table_func_names: &BTreeMap<String, Ident>,
3533 ) -> Result<Option<(ColumnName, NameQuality)>, PlanError> {
3534 Ok(match expr {
3535 Expr::Identifier(names) => {
3536 if let [name] = names.as_slice() {
3537 if let Some(table_func_name) = table_func_names.get(name.as_str()) {
3538 return Ok(Some((
3539 normalize::column_name(table_func_name.clone()),
3540 NameQuality::High,
3541 )));
3542 }
3543 }
3544 names
3545 .last()
3546 .map(|n| (normalize::column_name(n.clone()), NameQuality::High))
3547 }
3548 Expr::Value(v) => match v {
3549 // Per PostgreSQL, `bool` and `interval` literals take on the name
3550 // of their type, but not other literal types.
3551 Value::Boolean(_) => Some(("bool".into(), NameQuality::High)),
3552 Value::Interval(_) => Some(("interval".into(), NameQuality::High)),
3553 _ => None,
3554 },
3555 Expr::Function(func) => {
3556 let (schema, item) = match &func.name {
3557 ResolvedItemName::Item {
3558 qualifiers,
3559 full_name,
3560 ..
3561 } => (&qualifiers.schema_spec, full_name.item.clone()),
3562 // Name resolution should have rejected anything other than
3563 // `Item` for a function call.
3564 _ => {
3565 bail_internal!("function name did not resolve to an item: {:?}", func.name)
3566 }
3567 };
3568
3569 if schema == &SchemaSpecifier::from(ecx.qcx.scx.catalog.get_mz_internal_schema_id())

Callers 1

expand_select_itemFunction · 0.85

Calls 2

inventFunction · 0.85
mapMethod · 0.45

Tested by

no test coverage detected