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

[spark] Fix the loadTable of SparkSource can't recognize correct catalog #4233

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,17 @@
<td>Boolean</td>
<td>If true, allow to merge data types if the two types meet the rules for explicit casting.</td>
</tr>
<tr>
<td><h5>database</h5></td>
<td style="word-wrap: break-word;">false</td>
<td>String</td>
<td>The read or write database name.</td>
</tr>
<tr>
<td><h5>table</h5></td>
<td style="word-wrap: break-word;">false</td>
<td>String</td>
<td>The read or write table name.</td>
</tr>
</tbody>
</table>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
/** Options for spark connector. */
public class SparkConnectorOptions {

public static final ConfigOption<String> DATABASE =
key("database")
.stringType()
.noDefaultValue()
.withDescription("The read or write database name.");

public static final ConfigOption<String> TABLE =
key("table")
.stringType()
.noDefaultValue()
.withDescription("The read or write table name.");

public static final ConfigOption<Boolean> MERGE_SCHEMA =
key("write.merge-schema")
.booleanType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@

package org.apache.paimon.spark

import org.apache.paimon.catalog.CatalogContext
import org.apache.paimon.catalog.{CatalogContext, CatalogFactory, Identifier}
import org.apache.paimon.options.Options
import org.apache.paimon.spark.commands.WriteIntoPaimonTable
import org.apache.paimon.spark.sources.PaimonSink
import org.apache.paimon.spark.util.OptionUtils.mergeSQLConf
import org.apache.paimon.table.{DataTable, FileStoreTable, FileStoreTableFactory}
import org.apache.paimon.table.system.AuditLogTable
import org.apache.paimon.utils.StringUtils

import org.apache.spark.sql.{DataFrame, SaveMode => SparkSaveMode, SparkSession, SQLContext}
import org.apache.spark.sql.connector.catalog.{SessionConfigSupport, Table}
Expand Down Expand Up @@ -79,12 +80,26 @@ class SparkSource
SparkSource.toBaseRelation(table, sqlContext)
}

private def loadTable(options: JMap[String, String]): DataTable = {
private def loadTable(parameters: JMap[String, String]): DataTable = {
val catalogContext = CatalogContext.create(
Options.fromMap(mergeSQLConf(options)),
Options.fromMap(mergeSQLConf(parameters)),
SparkSession.active.sessionState.newHadoopConf())
val table = FileStoreTableFactory.create(catalogContext)
if (Options.fromMap(options).get(SparkConnectorOptions.READ_CHANGELOG)) {
val options = Options.fromMap(parameters)
val databaseName = options.get(SparkConnectorOptions.DATABASE)
val tableName = options.get(SparkConnectorOptions.TABLE)
val table =
if (
!StringUtils.isNullOrWhitespaceOnly(databaseName) && !StringUtils.isNullOrWhitespaceOnly(
tableName)
) {
CatalogFactory
.createCatalog(catalogContext)
.getTable(new Identifier(databaseName, tableName))
.asInstanceOf[FileStoreTable]
} else {
FileStoreTableFactory.create(catalogContext)
}
if (options.get(SparkConnectorOptions.READ_CHANGELOG)) {
new AuditLogTable(table)
} else {
table
Expand Down
Loading