|
| 1 | +// Copyright (c) 2026 ADBC Drivers Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package databricks |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/apache/arrow-go/v18/arrow" |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | +) |
| 23 | + |
| 24 | +func TestBuildTableName(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + catalog string |
| 28 | + schema string |
| 29 | + table string |
| 30 | + want string |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "table only", |
| 34 | + table: "my_table", |
| 35 | + want: "`my_table`", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "schema and table", |
| 39 | + schema: "my_schema", |
| 40 | + table: "my_table", |
| 41 | + want: "`my_schema`.`my_table`", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "fully qualified", |
| 45 | + catalog: "my_catalog", |
| 46 | + schema: "my_schema", |
| 47 | + table: "my_table", |
| 48 | + want: "`my_catalog`.`my_schema`.`my_table`", |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "with backticks in name", |
| 52 | + catalog: "my`catalog", |
| 53 | + schema: "my`schema", |
| 54 | + table: "my`table", |
| 55 | + want: "`my``catalog`.`my``schema`.`my``table`", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "catalog without schema", |
| 59 | + catalog: "my_catalog", |
| 60 | + table: "my_table", |
| 61 | + want: "`my_catalog`.`my_table`", |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tt := range tests { |
| 66 | + t.Run(tt.name, func(t *testing.T) { |
| 67 | + got := buildTableName(tt.catalog, tt.schema, tt.table) |
| 68 | + assert.Equal(t, tt.want, got) |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestQuoteIdentifier(t *testing.T) { |
| 74 | + tests := []struct { |
| 75 | + input string |
| 76 | + want string |
| 77 | + }{ |
| 78 | + {"simple", "`simple`"}, |
| 79 | + {"with space", "`with space`"}, |
| 80 | + {"with`backtick", "`with``backtick`"}, |
| 81 | + {"multiple``backticks", "`multiple````backticks`"}, |
| 82 | + {"", "``"}, |
| 83 | + } |
| 84 | + |
| 85 | + for _, tt := range tests { |
| 86 | + t.Run(tt.input, func(t *testing.T) { |
| 87 | + assert.Equal(t, tt.want, quoteIdentifier(tt.input)) |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestArrowTypeToDatabricksType(t *testing.T) { |
| 93 | + tests := []struct { |
| 94 | + name string |
| 95 | + dataType arrow.DataType |
| 96 | + want string |
| 97 | + }{ |
| 98 | + {"bool", arrow.FixedWidthTypes.Boolean, "BOOLEAN"}, |
| 99 | + {"int8", arrow.PrimitiveTypes.Int8, "TINYINT"}, |
| 100 | + {"int16", arrow.PrimitiveTypes.Int16, "SMALLINT"}, |
| 101 | + {"int32", arrow.PrimitiveTypes.Int32, "INT"}, |
| 102 | + {"int64", arrow.PrimitiveTypes.Int64, "BIGINT"}, |
| 103 | + {"uint8", arrow.PrimitiveTypes.Uint8, "SMALLINT"}, |
| 104 | + {"uint16", arrow.PrimitiveTypes.Uint16, "INT"}, |
| 105 | + {"uint32", arrow.PrimitiveTypes.Uint32, "BIGINT"}, |
| 106 | + {"uint64", arrow.PrimitiveTypes.Uint64, "BIGINT"}, |
| 107 | + {"float32", arrow.PrimitiveTypes.Float32, "FLOAT"}, |
| 108 | + {"float64", arrow.PrimitiveTypes.Float64, "DOUBLE"}, |
| 109 | + {"string", arrow.BinaryTypes.String, "STRING"}, |
| 110 | + {"large_string", arrow.BinaryTypes.LargeString, "STRING"}, |
| 111 | + {"binary", arrow.BinaryTypes.Binary, "BINARY"}, |
| 112 | + {"large_binary", arrow.BinaryTypes.LargeBinary, "BINARY"}, |
| 113 | + {"date32", arrow.PrimitiveTypes.Date32, "DATE"}, |
| 114 | + {"date64", arrow.PrimitiveTypes.Date64, "DATE"}, |
| 115 | + {"timestamp_with_tz", &arrow.TimestampType{Unit: arrow.Microsecond, TimeZone: "UTC"}, "TIMESTAMP"}, |
| 116 | + {"timestamp_without_tz", &arrow.TimestampType{Unit: arrow.Microsecond}, "TIMESTAMP_NTZ"}, |
| 117 | + {"decimal128", &arrow.Decimal128Type{Precision: 10, Scale: 2}, "DECIMAL(10, 2)"}, |
| 118 | + {"decimal128_large", &arrow.Decimal128Type{Precision: 38, Scale: 18}, "DECIMAL(38, 18)"}, |
| 119 | + } |
| 120 | + |
| 121 | + for _, tt := range tests { |
| 122 | + t.Run(tt.name, func(t *testing.T) { |
| 123 | + assert.Equal(t, tt.want, arrowTypeToDatabricksType(tt.dataType)) |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestPendingCopy(t *testing.T) { |
| 129 | + p := &pendingCopy{ |
| 130 | + path: "Volumes/cat/sch/vol/staging/abc123.parquet", |
| 131 | + rows: 42, |
| 132 | + } |
| 133 | + |
| 134 | + assert.Equal(t, "Volumes/cat/sch/vol/staging/abc123.parquet", p.String()) |
| 135 | + assert.Equal(t, int64(42), p.Rows()) |
| 136 | +} |
0 commit comments