You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "zeroshade (via GitHub)" <gi...@apache.org> on 2023/03/31 20:25:31 UTC

[GitHub] [arrow] zeroshade commented on a diff in pull request #34806: GH-34790: [Go]: Add array.Diff()

zeroshade commented on code in PR #34806:
URL: https://github.com/apache/arrow/pull/34806#discussion_r1154859243


##########
go/arrow/array/diff.go:
##########
@@ -0,0 +1,276 @@
+// 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 array
+
+import (
+	"errors"
+	"fmt"
+
+	"github.com/apache/arrow/go/v12/arrow"
+	"github.com/apache/arrow/go/v12/arrow/memory"
+)
+
+// Diff compares two arrays, returning an edit script which expresses the difference
+// between them. The edit script can be applied to the base array to produce the target.
+//
+// An edit script is an array of struct(insert bool, run_length int64).
+// Each element of "insert" determines whether an element was inserted into (true)
+// or deleted from (false) base. Each insertion or deletion is followed by a run of
+// elements which are unchanged from base to target; the length of this run is stored
+// in "run_length". (Note that the edit script begins and ends with a run of shared
+// elements but both fields of the struct must have the same length. To accommodate this
+// the first element of "insert" should be ignored.)
+//
+// For example for base "hlloo" and target "hello", the edit script would be
+// [
+//
+//	{"insert": false, "run_length": 1}, // leading run of length 1 ("h")
+//	{"insert": true, "run_length": 3}, // insert("e") then a run of length 3 ("llo")
+//	{"insert": false, "run_length": 0} // delete("o") then an empty run
+//
+// ]
+//
+// base: baseline for comparison
+// target: an array of identical type to base whose elements differ from base's
+// mem: memory to store the result will be allocated from this memory pool
+func Diff(base, target arrow.Array, mem memory.Allocator) (*Struct, error) {

Review Comment:
   I think we've been trying to put the allocator as the first argument in methods (similar to the convention of `context.Context` being the first parameter), so can we move the allocator to the first argument?



##########
go/arrow/array/diff.go:
##########
@@ -0,0 +1,276 @@
+// 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 array
+
+import (
+	"errors"
+	"fmt"
+
+	"github.com/apache/arrow/go/v12/arrow"
+	"github.com/apache/arrow/go/v12/arrow/memory"
+)
+
+// Diff compares two arrays, returning an edit script which expresses the difference
+// between them. The edit script can be applied to the base array to produce the target.
+//
+// An edit script is an array of struct(insert bool, run_length int64).
+// Each element of "insert" determines whether an element was inserted into (true)
+// or deleted from (false) base. Each insertion or deletion is followed by a run of
+// elements which are unchanged from base to target; the length of this run is stored
+// in "run_length". (Note that the edit script begins and ends with a run of shared
+// elements but both fields of the struct must have the same length. To accommodate this
+// the first element of "insert" should be ignored.)
+//
+// For example for base "hlloo" and target "hello", the edit script would be
+// [
+//
+//	{"insert": false, "run_length": 1}, // leading run of length 1 ("h")
+//	{"insert": true, "run_length": 3}, // insert("e") then a run of length 3 ("llo")
+//	{"insert": false, "run_length": 0} // delete("o") then an empty run
+//
+// ]
+//
+// base: baseline for comparison
+// target: an array of identical type to base whose elements differ from base's
+// mem: memory to store the result will be allocated from this memory pool
+func Diff(base, target arrow.Array, mem memory.Allocator) (*Struct, error) {
+	if base.DataType().Fingerprint() != target.DataType().Fingerprint() {
+		return nil, errors.New("only taking the diff of like-typed arrays is supported")
+	}
+	switch base.DataType().ID() {
+	case arrow.EXTENSION:
+		return Diff(base.(ExtensionArray).Storage(), target.(ExtensionArray).Storage(), mem)
+	case arrow.DICTIONARY:
+		return nil, fmt.Errorf("diffing arrays of type %s is not implemented", base.DataType().String())
+	case arrow.RUN_END_ENCODED:
+		return nil, fmt.Errorf("diffing arrays of type %s is not implemented", base.DataType().String())

Review Comment:
   can you use `%w` and `arrow.ErrNotImplemented` when constructing this error?



##########
go/arrow/array/diff.go:
##########
@@ -0,0 +1,276 @@
+// 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 array
+
+import (
+	"errors"
+	"fmt"
+
+	"github.com/apache/arrow/go/v12/arrow"
+	"github.com/apache/arrow/go/v12/arrow/memory"
+)
+
+// Diff compares two arrays, returning an edit script which expresses the difference
+// between them. The edit script can be applied to the base array to produce the target.
+//
+// An edit script is an array of struct(insert bool, run_length int64).
+// Each element of "insert" determines whether an element was inserted into (true)
+// or deleted from (false) base. Each insertion or deletion is followed by a run of
+// elements which are unchanged from base to target; the length of this run is stored
+// in "run_length". (Note that the edit script begins and ends with a run of shared
+// elements but both fields of the struct must have the same length. To accommodate this
+// the first element of "insert" should be ignored.)
+//
+// For example for base "hlloo" and target "hello", the edit script would be
+// [
+//
+//	{"insert": false, "run_length": 1}, // leading run of length 1 ("h")
+//	{"insert": true, "run_length": 3}, // insert("e") then a run of length 3 ("llo")
+//	{"insert": false, "run_length": 0} // delete("o") then an empty run
+//
+// ]
+//
+// base: baseline for comparison
+// target: an array of identical type to base whose elements differ from base's
+// mem: memory to store the result will be allocated from this memory pool
+func Diff(base, target arrow.Array, mem memory.Allocator) (*Struct, error) {
+	if base.DataType().Fingerprint() != target.DataType().Fingerprint() {
+		return nil, errors.New("only taking the diff of like-typed arrays is supported")
+	}

Review Comment:
   Probably better to use `arrow.TypeEqual(base.DataType(), target.DataType())`



##########
go/arrow/array/diff.go:
##########
@@ -0,0 +1,276 @@
+// 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 array
+
+import (
+	"errors"
+	"fmt"
+
+	"github.com/apache/arrow/go/v12/arrow"
+	"github.com/apache/arrow/go/v12/arrow/memory"
+)
+
+// Diff compares two arrays, returning an edit script which expresses the difference
+// between them. The edit script can be applied to the base array to produce the target.
+//
+// An edit script is an array of struct(insert bool, run_length int64).
+// Each element of "insert" determines whether an element was inserted into (true)
+// or deleted from (false) base. Each insertion or deletion is followed by a run of
+// elements which are unchanged from base to target; the length of this run is stored
+// in "run_length". (Note that the edit script begins and ends with a run of shared
+// elements but both fields of the struct must have the same length. To accommodate this
+// the first element of "insert" should be ignored.)
+//
+// For example for base "hlloo" and target "hello", the edit script would be
+// [
+//
+//	{"insert": false, "run_length": 1}, // leading run of length 1 ("h")
+//	{"insert": true, "run_length": 3}, // insert("e") then a run of length 3 ("llo")
+//	{"insert": false, "run_length": 0} // delete("o") then an empty run
+//
+// ]
+//
+// base: baseline for comparison
+// target: an array of identical type to base whose elements differ from base's
+// mem: memory to store the result will be allocated from this memory pool
+func Diff(base, target arrow.Array, mem memory.Allocator) (*Struct, error) {
+	if base.DataType().Fingerprint() != target.DataType().Fingerprint() {
+		return nil, errors.New("only taking the diff of like-typed arrays is supported")
+	}
+	switch base.DataType().ID() {
+	case arrow.EXTENSION:
+		return Diff(base.(ExtensionArray).Storage(), target.(ExtensionArray).Storage(), mem)
+	case arrow.DICTIONARY:
+		return nil, fmt.Errorf("diffing arrays of type %s is not implemented", base.DataType().String())
+	case arrow.RUN_END_ENCODED:
+		return nil, fmt.Errorf("diffing arrays of type %s is not implemented", base.DataType().String())
+	}
+	d := newQuadraticSpaceMyersDiff(base, target)
+	r, err := d.Diff(mem)
+	if err != nil {
+		if r != nil {
+			r.Release()
+		}
+		return nil, err
+	}
+	return r, nil
+}
+
+// editPoint represents an intermediate state in the comparison of two arrays
+type editPoint struct {
+	base   int
+	target int
+}
+
+func (e editPoint) Equals(other editPoint) bool {
+	return e.base == other.base && e.target == other.target
+}
+
+type quadraticSpaceMyersDiff struct {
+	base         arrow.Array
+	target       arrow.Array
+	finishIndex  int
+	editCount    int
+	endpointBase []int
+	insert       []bool
+	baseBegin    int
+	targetBegin  int
+	baseEnd      int
+	targetEnd    int
+}
+
+func newQuadraticSpaceMyersDiff(base, target arrow.Array) *quadraticSpaceMyersDiff {
+	d := &quadraticSpaceMyersDiff{
+		base:         base,
+		target:       target,
+		finishIndex:  -1,
+		editCount:    0,
+		endpointBase: []int{},
+		insert:       []bool{},
+		baseBegin:    0,
+		targetBegin:  0,
+		baseEnd:      base.Len(),
+		targetEnd:    target.Len(),
+	}
+	d.endpointBase = []int{d.extendFrom(editPoint{d.baseBegin, d.targetBegin}).base}
+	if d.baseEnd-d.baseBegin == d.targetEnd-d.targetBegin && d.endpointBase[0] == d.baseEnd {
+		// trivial case: base == target
+		d.finishIndex = 0
+	}
+	return d
+}
+
+func (d *quadraticSpaceMyersDiff) valuesEqual(baseIndex, targetIndex int) bool {
+	baseNull := d.base.IsNull(baseIndex)
+	targetNull := d.target.IsNull(targetIndex)
+	if baseNull || targetNull {
+		return baseNull && targetNull
+	}
+	return SliceEqual(d.base, int64(baseIndex), int64(baseIndex+1), d.target, int64(targetIndex), int64(targetIndex+1))

Review Comment:
   yea, i can see where an `UntypedValue` method would potentially be a more efficient alternative here, but given the current things available this works.



##########
go/arrow/array/diff.go:
##########
@@ -0,0 +1,276 @@
+// 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 array
+
+import (
+	"errors"
+	"fmt"
+
+	"github.com/apache/arrow/go/v12/arrow"
+	"github.com/apache/arrow/go/v12/arrow/memory"
+)
+
+// Diff compares two arrays, returning an edit script which expresses the difference
+// between them. The edit script can be applied to the base array to produce the target.
+//
+// An edit script is an array of struct(insert bool, run_length int64).
+// Each element of "insert" determines whether an element was inserted into (true)
+// or deleted from (false) base. Each insertion or deletion is followed by a run of
+// elements which are unchanged from base to target; the length of this run is stored
+// in "run_length". (Note that the edit script begins and ends with a run of shared
+// elements but both fields of the struct must have the same length. To accommodate this
+// the first element of "insert" should be ignored.)
+//
+// For example for base "hlloo" and target "hello", the edit script would be
+// [
+//
+//	{"insert": false, "run_length": 1}, // leading run of length 1 ("h")
+//	{"insert": true, "run_length": 3}, // insert("e") then a run of length 3 ("llo")
+//	{"insert": false, "run_length": 0} // delete("o") then an empty run
+//
+// ]
+//
+// base: baseline for comparison
+// target: an array of identical type to base whose elements differ from base's
+// mem: memory to store the result will be allocated from this memory pool
+func Diff(base, target arrow.Array, mem memory.Allocator) (*Struct, error) {

Review Comment:
   Is there really a particular benefit to returning the edits as an actual arrow Struct column vs just returning a native Go object?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org