Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add column.getItem to Columns #79

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions internal/tests/integration/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ func TestIntegration_BuiltinFunctions(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 10, len(res))
}

func TestIntegration_ColumnGetItem(t *testing.T) {
ctx := context.Background()
spark, err := sql.NewSessionBuilder().Remote("sc://localhost").Build(ctx)
if err != nil {
t.Fatal(err)
}

df, _ := spark.Sql(ctx, "select sequence(1,10) as s")
df, err = df.Select(ctx, functions.Col("s").GetItem(2))
assert.NoError(t, err)
res, err := df.Collect(ctx)
assert.NoError(t, err)
assert.Equal(t, int32(3), res[0].Values()[0])
}
4 changes: 4 additions & 0 deletions spark/sql/column/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (c Column) Desc() Column {
})
}

func (c Column) GetItem(key any) Column {
return NewColumn(NewUnresolvedExtractValue("getItem", c.expr, NewLiteral(key)))
}

func (c Column) Asc() Column {
return NewColumn(&sortExpression{
child: c.expr,
Expand Down
35 changes: 35 additions & 0 deletions spark/sql/column/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,37 @@ func (c *caseWhenExpression) ToProto(ctx context.Context) (*proto.Expression, er
return fun.ToProto(ctx)
}

type unresolvedExtractValue struct {
name string
child expression
extraction expression
}

func (u *unresolvedExtractValue) DebugString() string {
return fmt.Sprintf("%s(%s, %s)", u.name, u.child.DebugString(), u.extraction.DebugString())
}

func (u *unresolvedExtractValue) ToProto(ctx context.Context) (*proto.Expression, error) {
expr := newProtoExpression()
child, err := u.child.ToProto(ctx)
if err != nil {
return nil, err
}

extraction, err := u.extraction.ToProto(ctx)
if err != nil {
return nil, err
}

expr.ExprType = &proto.Expression_UnresolvedExtractValue_{
UnresolvedExtractValue: &proto.Expression_UnresolvedExtractValue{
Child: child,
Extraction: extraction,
},
}
return expr, nil
}

type unresolvedFunction struct {
name string
args []expression
Expand Down Expand Up @@ -209,6 +240,10 @@ func (u *unresolvedFunction) ToProto(ctx context.Context) (*proto.Expression, er
return expr, nil
}

func NewUnresolvedExtractValue(name string, child expression, extraction expression) expression {
return &unresolvedExtractValue{name: name, child: child, extraction: extraction}
}

func NewUnresolvedFunction(name string, args []expression, isDistinct bool) expression {
return &unresolvedFunction{name: name, args: args, isDistinct: isDistinct}
}
Expand Down
Loading