-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-48951] Adding
column
and functions
packages
### What changes were proposed in this pull request? This patch provides additional base capabilities that are needed to parallelize development more by adding very skeleton behavior for the `Column` abstraction in Spark. This allows the users to use the following APIs: ``` df, _ := spark.Sql("select * from range(100)") col, _ := df.Col("id") df, _ := df.Filter(col.Gt(functions.Lit(50)) df.Show(ctx, 100, false) ``` ### Why are the changes needed? Compatibility ### Does this PR introduce _any_ user-facing change? Adds the necessary public API for `Column` and `functions`. ### How was this patch tested? Added new tests. Closes #35 from grundprinzip/plans_and_exprs. Authored-by: Martin Grund <martin.grund@databricks.com> Signed-off-by: Martin Grund <martin.grund@databricks.com>
- Loading branch information
1 parent
3123425
commit 53797bd
Showing
17 changed files
with
735 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package column | ||
|
||
import proto "github.com/apache/spark-connect-go/v35/internal/generated" | ||
|
||
type Column struct { | ||
expr Expression | ||
} | ||
|
||
func (c *Column) ToPlan() (*proto.Expression, error) { | ||
return c.expr.ToPlan() | ||
} | ||
|
||
func (c Column) Lt(other Column) Column { | ||
return NewColumn(NewUnresolvedFunction("<", []Expression{c.expr, other.expr}, false)) | ||
} | ||
|
||
func (c Column) Le(other Column) Column { | ||
return NewColumn(NewUnresolvedFunction("<=", []Expression{c.expr, other.expr}, false)) | ||
} | ||
|
||
func (c Column) Gt(other Column) Column { | ||
return NewColumn(NewUnresolvedFunction(">", []Expression{c.expr, other.expr}, false)) | ||
} | ||
|
||
func (c Column) Ge(other Column) Column { | ||
return NewColumn(NewUnresolvedFunction(">=", []Expression{c.expr, other.expr}, false)) | ||
} | ||
|
||
func (c Column) Eq(other Column) Column { | ||
return NewColumn(NewUnresolvedFunction("==", []Expression{c.expr, other.expr}, false)) | ||
} | ||
|
||
func (c Column) Neq(other Column) Column { | ||
cmp := NewUnresolvedFunction("==", []Expression{c.expr, other.expr}, false) | ||
return NewColumn(NewUnresolvedFunction("not", []Expression{cmp}, false)) | ||
} | ||
|
||
func (c Column) Mul(other Column) Column { | ||
return NewColumn(NewUnresolvedFunction("*", []Expression{c.expr, other.expr}, false)) | ||
} | ||
|
||
func (c Column) Div(other Column) Column { | ||
return NewColumn(NewUnresolvedFunction("/", []Expression{c.expr, other.expr}, false)) | ||
} | ||
|
||
func NewColumn(expr Expression) Column { | ||
return Column{ | ||
expr: expr, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package column | ||
|
||
import ( | ||
"testing" | ||
|
||
proto "github.com/apache/spark-connect-go/v35/internal/generated" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestColumnFunctions(t *testing.T) { | ||
col1 := NewColumn(NewColumnReference("col1")) | ||
col2 := NewColumn(NewColumnReference("col2")) | ||
|
||
tests := []struct { | ||
name string | ||
arg Column | ||
want *proto.Expression | ||
}{ | ||
{ | ||
name: "TestNewUnresolvedFunction", | ||
arg: NewColumn(NewUnresolvedFunction("id", nil, false)), | ||
want: &proto.Expression{ | ||
ExprType: &proto.Expression_UnresolvedFunction_{ | ||
UnresolvedFunction: &proto.Expression_UnresolvedFunction{ | ||
FunctionName: "id", | ||
IsDistinct: false, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "TestComparison", | ||
arg: col1.Lt(col2), | ||
want: &proto.Expression{ | ||
ExprType: &proto.Expression_UnresolvedFunction_{ | ||
UnresolvedFunction: &proto.Expression_UnresolvedFunction{ | ||
FunctionName: "<", | ||
IsDistinct: false, | ||
Arguments: []*proto.Expression{ | ||
{ | ||
ExprType: &proto.Expression_UnresolvedAttribute_{ | ||
UnresolvedAttribute: &proto.Expression_UnresolvedAttribute{ | ||
UnparsedIdentifier: "col1", | ||
}, | ||
}, | ||
}, | ||
{ | ||
ExprType: &proto.Expression_UnresolvedAttribute_{ | ||
UnresolvedAttribute: &proto.Expression_UnresolvedAttribute{ | ||
UnparsedIdentifier: "col2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := tt.arg.ToPlan() | ||
assert.NoError(t, err) | ||
expected := tt.want | ||
assert.Equalf(t, expected, got, "Input: %v", tt.arg.expr.DebugString()) | ||
}) | ||
} | ||
} |
Oops, something went wrong.