cube-js / cube

📊 Cube — The Semantic Layer for Building Data Applications
https://cube.dev
Other
17.95k stars 1.78k forks source link

CubeServer driverFactory incorrect dataSource when using SQL API #8653

Closed cemhany closed 1 week ago

cemhany commented 2 months ago

Hello.

Describe the bug DriverContext of "driverFactory" contains "default" as "dataSource" when "sql" method is used. But works as expected with "load" method.

To Reproduce 1- Instantiate a CubeServerCore with "driverFactory" creation option.

const core = new CubejsServerCore({
        ...
        driverFactory: async (context: DriverContext) => {
                console.log("DataSource: " + context.dataSource);
                ...
        }
        ...
});

2- Instantiate a cube api instance with "cube"

const api = cube('', {
        apiUrl: 'http://localhost:4000/cubejs-api/v1'
});

3- Call "load" method on cube

cube.load(query);

You will see that the actual source of the cube model(s) will be printed to console

4- Call "sql" method on cube

cube.sql(query);

You will see that 'default' will be printed to console

Expected behavior I would expect both method calls to print actual dataSource name

Version: 0.35.67 [@cubejs-backend/server-core] 0.35.23 [@cubejs-client/core]

Additional context I am using the following create options when creating the core if it matters: contextToAppId schemaVersion driverFactory repositoryFactory checkAuth

igorlukanin commented 2 months ago

Hi @cemhany 👋

According to the docs, driver_factory is called (at least) once per data_source for every orchestrator id.

First of all, you probably need to define context_to_orchestrator_id if you'd like to provide different driver instances to different tenants of your app.

Second, I wasn't able to reproduce this. I see that sql triggers driver_factory both for the default and "custom" data source.

Here's my config:

module.exports = {
  driverFactory: (context) => {
    console.log(``)
    console.log(`Security context: ${JSON.stringify(context.securityContext)}`)
    console.log(`Data source: ${context.dataSource}`)

    return {
      type: 'duckdb'
    }
  },

  contextToOrchestratorId: (context) => {
    return `${context.securityContext.name}`;
  }
}

Here's my console command:

curl \
  -H "Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ._XEngvIuxOcA-j7y_upRUbXli4DLToNf7HxH1XNmxSc" \
  -G \
  --data-urlencode 'query={"measures":["data_source_foo.count"]}' \
  https://remaining-powersville.gcp-us-central1.cubecloudapp.dev/user/igor%40cube.dev/13/cubejs-api/v1/sql

Here's what I see in the logs:

Screenshot 2024-08-29 at 15 33 09

If you can try my configuration and report what you observe, that would be great.

igorlukanin commented 1 week ago

Closing due to inactivity.