MCPcopy Create free account
hub / github.com/apache/datafusion / get_field_to_sql

Method get_field_to_sql

datafusion/sql/src/unparser/expr.rs:722–779  ·  view source on GitHub ↗
(&self, args: &[Expr])

Source from the content-addressed store, hash-verified

720 }
721
722 fn get_field_to_sql(&self, args: &[Expr]) -> Result<ast::Expr> {
723 if args.len() < 2 {
724 return internal_err!(
725 "get_field must have at least 2 arguments, got {}",
726 args.len()
727 );
728 }
729
730 // Extract all field names (args[1..])
731 let mut fields = Vec::with_capacity(args.len() - 1);
732 for arg in &args[1..] {
733 let field = match arg {
734 Expr::Literal(lit, _) => self.new_ident_quoted_if_needs(lit.to_string()),
735 _ => {
736 return internal_err!(
737 "get_field expects field arguments to be strings, but received: {:?}",
738 arg
739 );
740 }
741 };
742 fields.push(field);
743 }
744
745 match &args[0] {
746 Expr::Column(col) => {
747 let mut id = match self.col_to_sql(col)? {
748 ast::Expr::Identifier(ident) => vec![ident],
749 ast::Expr::CompoundIdentifier(idents) => idents,
750 other => {
751 return internal_err!(
752 "expected col_to_sql to return an Identifier or CompoundIdentifier, but received: {:?}",
753 other
754 );
755 }
756 };
757 id.extend(fields);
758 Ok(ast::Expr::CompoundIdentifier(id))
759 }
760 Expr::ScalarFunction(struct_expr) => {
761 let root = self
762 .scalar_function_to_sql(struct_expr.func.name(), &struct_expr.args)?;
763 let access_chain = fields
764 .into_iter()
765 .map(|field| ast::AccessExpr::Dot(ast::Expr::Identifier(field)))
766 .collect();
767 Ok(ast::Expr::CompoundFieldAccess {
768 root: Box::new(root),
769 access_chain,
770 })
771 }
772 _ => {
773 internal_err!(
774 "get_field expects first argument to be column or scalar function, but received: {:?}",
775 &args[0]
776 )
777 }
778 }
779 }

Callers 1

Calls 13

IdentifierClass · 0.85
newFunction · 0.85
col_to_sqlMethod · 0.80
collectMethod · 0.80
lenMethod · 0.45
to_stringMethod · 0.45
pushMethod · 0.45
extendMethod · 0.45
nameMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected